Skip to content

Commit a2be5af

Browse files
committed
Clean up splitting dual profile files
1 parent e58c0cb commit a2be5af

File tree

10 files changed

+46
-49
lines changed

10 files changed

+46
-49
lines changed
201 KB
Binary file not shown.
45.7 KB
Binary file not shown.
549 KB
Binary file not shown.
145 KB
Binary file not shown.

mhkit/dolfyn/io/base.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,14 @@ def _create_dataset(data):
164164
"""
165165

166166
tag = ["_avg", "_b5", "_echo", "_bt", "_gps", "_altraw", "_altraw_avg", "_sl"]
167-
# dual profile where burst velocity is not measured but echo sounder is
168-
if ("vel" not in data["data_vars"]) and ("vel_avg" in data["data_vars"]):
169-
data["coords"]["time"] = data["coords"]["time_avg"]
167+
# If burst velocity not measured
168+
if "vel" not in data["data_vars"]:
169+
# dual profile where burst velocity is not measured but echo sounder is
170+
if "vel_avg" in data["data_vars"]:
171+
data["coords"]["time"] = data["coords"]["time_avg"]
172+
else:
173+
t_vars = [t for t in data["coords"] if "time" in t]
174+
data["coords"]["time"] = data["coords"][t_vars[0]]
170175

171176
ds_dict = {}
172177
for key in data["coords"]:

mhkit/dolfyn/io/nortek2.py

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -769,41 +769,29 @@ def _reduce(data):
769769
tm[irow, icol] = tmat["M" + str(irow + 1) + str(icol + 1)]
770770
dv["beam2inst_orientmat"] = tm
771771

772-
# If burst velocity isn't used, need to copy one for 'time'
773-
if "time" not in dc:
774-
for val in dc:
775-
if "time" in val:
776-
time = val
777-
dc["time"] = dc[time]
778-
779772

780773
def split_dp_datasets(ds):
781774
"""
782775
Splits a dataset containing dual profiles into individual profiles
783776
"""
784777

785778
ds2 = type(ds)()
786-
# Figure out which variables belong to which profile based on length of time variables
787-
t_dict = {}
788-
for t in ds.coords:
789-
if "time" in t:
790-
t_dict[t] = ds[t].size
791-
792-
other_coords = []
793-
for key, val in t_dict.items():
794-
if val != t_dict["time"]:
795-
if key.endswith("altraw"):
796-
# altraw goes with burst, altraw_avg goes with avg
797-
continue
798-
other_coords.append(key)
779+
# Get averaged time variables
780+
other_coords = [t for t in ds.coords if "_avg" in t]
781+
# Check if ice tracking was used
782+
if ("vel" in ds.data_vars) and ("time_bt" in ds.coords):
783+
if ds["time"].size != ds["time_bt"].size:
784+
other_coords.append("time_bt")
785+
799786
if other_coords:
800787
# Fetch variables, coordinates, and attrs for second profiling configuration
801788
other_vars = [
802-
v for v in ds.data_vars if any(x in ds[v].coords for x in other_coords)
789+
v
790+
for v in ds.data_vars
791+
if any(x in ds[v].coords for x in other_coords)
792+
or any(ds[v].shape[-1] == ds[x].size for x in other_coords)
803793
]
804-
other_tags = [s.split("_")[-1] for s in other_coords]
805-
other_coords += [v for v in ds.coords if any(x in v for x in other_tags)]
806-
other_attrs = [s for s in ds.attrs if any(x in s for x in other_tags)]
794+
other_attrs = [s for s in ds.attrs if "_avg" in s]
807795
critical_attrs = [
808796
"inst_model",
809797
"inst_make",
@@ -825,19 +813,13 @@ def split_dp_datasets(ds):
825813
ds2.attrs["rotate_vars"] = rotate_vars2
826814
# Set orientation matricies
827815
ds2["beam2inst_orientmat"] = ds["beam2inst_orientmat"]
828-
# drop echo sounder tag for following code
829-
if "echo" in other_tags:
830-
other_tags.remove("echo")
831-
if ds.attrs["has_imu"] and other_tags:
832-
ds2 = ds2.rename({"orientmat_" + other_tags[0]: "orientmat"})
816+
if ds.attrs["has_imu"]:
817+
ds2 = ds2.rename({"orientmat_avg": "orientmat"})
833818
else:
834819
ds2["orientmat"] = ds["orientmat"]
835820
# Set original coordinate system
836-
if other_tags:
837-
cy = ds2.attrs["coord_sys_axes_" + other_tags[0]]
838-
ds2.attrs["coord_sys"] = {"XYZ": "inst", "ENU": "earth", "beam": "beam"}[cy]
839-
else:
840-
ds2.attrs["coord_sys"] = ds.attrs["coord_sys"]
821+
cy = ds2.attrs["coord_sys_axes_avg"]
822+
ds2.attrs["coord_sys"] = {"XYZ": "inst", "ENU": "earth", "beam": "beam"}[cy]
841823
# Need to add beam coordinate
842824
if "beam" not in ds2.dims:
843825
ds2 = ds2.assign_coords({"beam": ds["beam"], "dir": ds["dir"]})
@@ -849,4 +831,7 @@ def split_dp_datasets(ds):
849831
for itm in rotate_vars2:
850832
ds.attrs["rotate_vars"].remove(itm)
851833

852-
return ds, ds2
834+
if ds2:
835+
return ds, ds2
836+
else:
837+
return ds

mhkit/tests/dolfyn/test_clean.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_clean_upADCP(self):
6262
td_awac = tp.dat_awac.copy(deep=True)
6363
td_sig = tp.dat_sig_tide.copy(deep=True)
6464
td_rdi = tp.dat_rdi.copy(deep=True)
65-
td_dual = tp.dat_sig_dp2.copy(deep=True)
65+
td_dual = tp.dat_sig_dp1_ice.copy(deep=True)
6666

6767
apm.clean.water_depth_from_pressure(td_awac, salinity=30)
6868
apm.clean.remove_surface_interference(td_awac, beam_angle=20, inplace=True)
@@ -95,7 +95,7 @@ def test_clean_upADCP(self):
9595

9696
def test_clean_downADCP(self):
9797
td = tp.dat_sig_ie.copy(deep=True)
98-
td_dual = tp.dat_sig_dp2.copy(deep=True)
98+
td_dual = tp.dat_sig_dp1_ice.copy(deep=True)
9999

100100
# First remove bad data
101101
td["vel"] = apm.clean.val_exceeds_thresh(td.vel, thresh=3)

mhkit/tests/dolfyn/test_read_adp.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
dat_sig_skip = load("Sig_SkippedPings01.nc")
3939
dat_sig_badt = load("Sig1000_BadTime01.nc")
4040
dat_sig5_leiw = load("Sig500_last_ensemble_is_whole.nc")
41-
dat_sig_dp2_all = load("Sig500_dp_all.nc")
42-
dat_sig_dp2_echo = load("Sig1000_dp_echo.nc")
41+
dat_sig_dp1_all = load("Sig500_dp_ice1.nc")
42+
dat_sig_dp1_ice = load("Sig500_dp_ice2.nc")
43+
dat_sig_dp2_echo = load("Sig1000_dp_echo1.nc")
44+
dat_sig_dp2_avg = load("Sig1000_dp_echo2.nc")
4345

4446

4547
class io_adp_testcase(unittest.TestCase):
@@ -111,9 +113,8 @@ def test_io_nortek2(self):
111113
td_sig_raw_avg = read("Sig100_raw_avg.ad2cp", nens=nens, rebuild_index=True)
112114
td_sig_avg = read("Sig100_avg.ad2cp", nens=nens, rebuild_index=True)
113115
td_sig_rt = read("Sig1000_online.ad2cp", nens=nens, rebuild_index=True)
114-
# Only need to test 2nd dataset
115-
td_sig_dp1_all, td_sig_dp2_all = read("Sig500_dp_all.ad2cp", rebuild_index=True)
116-
td_sig_dp1_avg, td_sig_dp2_echo = read(
116+
td_sig_dp1_all, td_sig_dp1_ice = read("Sig500_dp_ice.ad2cp", rebuild_index=True)
117+
td_sig_dp2_echo, td_sig_dp2_avg = read(
117118
"Sig1000_dp_echo.ad2cp", rebuild_index=True
118119
)
119120

@@ -139,7 +140,7 @@ def test_io_nortek2(self):
139140
os.remove(tb.exdt("Sig_SkippedPings01.ad2cp.index"))
140141
os.remove(tb.exdt("Sig500_last_ensemble_is_whole.ad2cp.index"))
141142
os.remove(tb.rfnm("Sig1000_BadTime01.ad2cp.index"))
142-
os.remove(tb.exdt("Sig500_dp_all.ad2cp.index"))
143+
os.remove(tb.exdt("Sig500_dp_ice.ad2cp.index"))
143144
os.remove(tb.exdt("Sig1000_dp_echo.ad2cp.index"))
144145

145146
if make_data:
@@ -155,8 +156,10 @@ def test_io_nortek2(self):
155156
save(td_sig_skip, "Sig_SkippedPings01.nc")
156157
save(td_sig_badt, "Sig1000_BadTime01.nc")
157158
save(td_sig5_leiw, "Sig500_last_ensemble_is_whole.nc")
158-
save(td_sig_dp2_all, "dual_profile.nc")
159-
save(td_sig_dp2_echo, "Sig1000_dp_echo.nc")
159+
save(td_sig_dp1_all, "Sig500_dp_ice1.nc")
160+
save(td_sig_dp1_ice, "Sig500_dp_ice2.nc")
161+
save(td_sig_dp2_echo, "Sig1000_dp_echo1.nc")
162+
save(td_sig_dp2_avg, "Sig1000_dp_echo2.nc")
160163
return
161164

162165
assert_allclose(td_sig, dat_sig, atol=1e-6)
@@ -171,8 +174,10 @@ def test_io_nortek2(self):
171174
assert_allclose(td_sig5_leiw, dat_sig5_leiw, atol=1e-6)
172175
assert_allclose(td_sig_skip, dat_sig_skip, atol=1e-6)
173176
assert_allclose(td_sig_badt, dat_sig_badt, atol=1e-6)
174-
assert_allclose(td_sig_dp2_all, dat_sig_dp2_all, atol=1e-6)
177+
assert_allclose(td_sig_dp1_all, dat_sig_dp1_all, atol=1e-6)
178+
assert_allclose(td_sig_dp1_ice, dat_sig_dp1_ice, atol=1e-6)
175179
assert_allclose(td_sig_dp2_echo, dat_sig_dp2_echo, atol=1e-6)
180+
assert_allclose(td_sig_dp2_avg, dat_sig_dp2_avg, atol=1e-6)
176181

177182
def test_nortek2_crop(self):
178183
# Test file cropping function

mhkit/tests/dolfyn/test_rotate_adp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def test_rotate_earth2inst(self):
118118
rotate2(td_sig, "inst", inplace=True)
119119
td_sig_i = load("Sig1000_IMU_rotate_inst2earth.nc")
120120
rotate2(td_sig_i, "inst", inplace=True)
121+
td_sig_avg = load("Sig100_avg")
122+
rotate2(td_sig_avg, "inst", inplace=True)
121123

122124
cd_rdi = load("RDI_test01_rotate_beam2inst.nc")
123125
cd_wr2 = tr.dat_wr2

0 commit comments

Comments
 (0)