44from datetime import datetime
55from pathlib import Path
66
7- from oceanarray .instrument import apply_microcat_calibration_from_txt , stage2_trim , trim_suggestion
8- from oceanarray .instrument import apply_microcat_calibration_from_txt
7+ from oceanarray .instrument import (
8+ apply_microcat_calibration_from_txt ,
9+ stage2_trim ,
10+ trim_suggestion ,
11+ )
912from oceanarray .rodb import rodbload
1013
14+
1115def test_trim_suggestion_basic ():
1216 time = pd .date_range ("2020-01-01" , periods = 10 , freq = "h" )
13- ds = xr .Dataset ({
14- "T" : ("TIME" , [0 ]* 3 + [10 ]* 4 + [0 ]* 3 ),
15- "C" : ("TIME" , [0 ]* 3 + [12 ]* 4 + [0 ]* 3 ),
16- "P" : ("TIME" , [0 ]* 3 + [8 ]* 4 + [0 ]* 3 ),
17- }, coords = {"TIME" : time })
17+ ds = xr .Dataset (
18+ {
19+ "T" : ("TIME" , [0 ] * 3 + [10 ] * 4 + [0 ] * 3 ),
20+ "C" : ("TIME" , [0 ] * 3 + [12 ] * 4 + [0 ] * 3 ),
21+ "P" : ("TIME" , [0 ] * 3 + [8 ] * 4 + [0 ] * 3 ),
22+ },
23+ coords = {"TIME" : time },
24+ )
1825
1926 start , end = trim_suggestion (ds , percent = 80 , threshold = 5 )
2027
@@ -36,31 +43,44 @@ def test_stage2_trim_single_sample():
3643
3744def test_apply_microcat_with_flags (tmp_path ):
3845 txt = tmp_path / "mock.microcat.txt"
39- txt .write_text ("""Conductivity: 1.0 2.0
46+ txt .write_text (
47+ """Conductivity: 1.0 2.0
4048Temperature: -0.5 0.5
4149Pressure: 0.0 1.0
4250Average conductivity applied? y
4351Average temperature applied? y
4452Average pressure applied? n
45- """ )
53+ """
54+ )
4655
4756 # Create dummy .use file
4857 time = pd .date_range ("2022-01-01" , periods = 3 , freq = "h" )
49- ds = xr .Dataset ({"T" : ("TIME" , [10.0 , 11.0 , 12.0 ]),
50- "C" : ("TIME" , [35.0 , 35.1 , 35.2 ]),
51- "P" : ("TIME" , [1000.0 , 1001.0 , 1002.0 ])},
52- coords = {"TIME" : time })
58+ ds = xr .Dataset (
59+ {
60+ "T" : ("TIME" , [10.0 , 11.0 , 12.0 ]),
61+ "C" : ("TIME" , [35.0 , 35.1 , 35.2 ]),
62+ "P" : ("TIME" , [1000.0 , 1001.0 , 1002.0 ]),
63+ },
64+ coords = {"TIME" : time },
65+ )
5366 use_path = tmp_path / "mock.use"
5467 ds .to_netcdf (use_path ) # write as .nc, simulate reading in `rodbload`
5568
5669 # Patch rodbload to return this dataset
5770 from oceanarray import instrument
71+
5872 instrument .rodb .rodbload = lambda _ : ds
5973
6074 ds_cal = apply_microcat_calibration_from_txt (txt , use_path )
61- assert "T" in ds_cal and np .allclose (ds_cal ["T" ].values , [10.0 + 0.0 , 11.0 + 0.0 , 12.0 + 0.0 ], atol = 1e-6 )
62- assert "C" in ds_cal and np .allclose (ds_cal ["C" ].values , [36.5 , 36.6 , 36.7 ], atol = 1e-6 )
63- assert "P" in ds_cal and np .allclose (ds_cal ["P" ].values , [1000.0 , 1001.0 , 1002.0 ], atol = 1e-6 ) # unchanged
75+ assert "T" in ds_cal and np .allclose (
76+ ds_cal ["T" ].values , [10.0 + 0.0 , 11.0 + 0.0 , 12.0 + 0.0 ], atol = 1e-6
77+ )
78+ assert "C" in ds_cal and np .allclose (
79+ ds_cal ["C" ].values , [36.5 , 36.6 , 36.7 ], atol = 1e-6
80+ )
81+ assert "P" in ds_cal and np .allclose (
82+ ds_cal ["P" ].values , [1000.0 , 1001.0 , 1002.0 ], atol = 1e-6
83+ ) # unchanged
6484
6585
6686def test_apply_microcat_calibration_from_txt (tmp_path ):
0 commit comments