Skip to content

Commit 9717b3b

Browse files
committed
Handle nan's in amplitude surface function, set cellsize as user input
1 parent 31701d1 commit 9717b3b

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

mhkit/dolfyn/adp/clean.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def water_depth_from_amplitude(ds, thresh=10, nfilt=None) -> None:
171171
# the echo profile
172172
edf = np.diff(ds["amp" + tag[0]].values.astype(np.int16), axis=1)
173173
inds2 = (
174-
np.max(
174+
np.nanmax(
175175
(edf < 0)
176176
* np.arange(ds["vel" + tag[0]].shape[1] - 1, dtype=np.uint8)[None, :, None],
177177
axis=1,
@@ -185,11 +185,11 @@ def water_depth_from_amplitude(ds, thresh=10, nfilt=None) -> None:
185185
# Combine them:
186186
D = np.vstack((d1, d2))
187187
# Take the median value as the estimate of the surface:
188-
d = np.median(D, axis=0)
188+
d = np.nanmedian(D, axis=0)
189189

190190
# Throw out values that do not increase near the surface by *thresh*
191191
for ip in range(ds["vel" + tag[0]].shape[1]):
192-
itmp = np.min(inds[:, ip])
192+
itmp = np.nanmin(inds[:, ip])
193193
if (edf[itmp:, :, ip] < thresh).all():
194194
d[ip] = np.nan
195195

@@ -238,7 +238,7 @@ def water_depth_from_pressure(ds, salinity=35) -> None:
238238
ds : xarray.Dataset
239239
The full adcp dataset
240240
salinity: numeric
241-
Water salinity in psu. Default = 35
241+
Water salinity in PSU. Default = 35
242242
243243
Returns
244244
-------
@@ -337,7 +337,7 @@ def nan_beyond_surface(*args, **kwargs):
337337

338338

339339
def remove_surface_interference(
340-
ds, val=np.nan, beam_angle=None, inplace=False
340+
ds, val=np.nan, beam_angle=None, cell_size=None, inplace=False
341341
) -> Optional[xr.Dataset]:
342342
"""
343343
Mask the values of 3D data (vel, amp, corr, echo) that are beyond the surface.
@@ -350,6 +350,8 @@ def remove_surface_interference(
350350
Specifies the value to set the bad values to. Default is `numpy.nan`
351351
beam_angle : int
352352
ADCP beam inclination angle in degrees. Default = dataset.attrs['beam_angle']
353+
cell_size : float
354+
ADCP beam cellsize in meters. Default = dataset.attrs['cell_size']
353355
inplace : bool
354356
When True the existing data object is modified. When False
355357
a copy is returned. Default = False
@@ -370,46 +372,57 @@ def remove_surface_interference(
370372
raise KeyError(
371373
"Depth variable 'depth' does not exist in input dataset."
372374
"Please calculate 'depth' using the function 'water_depth_from_pressure'"
373-
"or 'water_depth_from_amplitude."
375+
"or 'water_depth_from_amplitude, or it can be found from the 'dist_bt'"
376+
"(bottom track) or 'dist_alt' (altimeter) variables, if available."
374377
)
375378

376379
if beam_angle is None:
377380
if hasattr(ds, "beam_angle"):
378381
beam_angle = np.deg2rad(ds.attrs["beam_angle"])
379382
else:
380-
raise Exception(
383+
raise KeyError(
381384
"'beam_angle` not found in dataset attributes. "
382385
"Please supply the ADCP's beam angle."
383386
)
384387
else:
385388
beam_angle = np.deg2rad(beam_angle)
386389

390+
if cell_size is None:
391+
# Fetch cell size
392+
cell_sizes = [
393+
a
394+
for a in ds.attrs
395+
if (
396+
("cell_size" in a)
397+
and ("_bt" not in a)
398+
and ("_alt" not in a)
399+
and ("wave" not in a)
400+
)
401+
]
402+
if cell_sizes:
403+
cs = cell_sizes[0]
404+
else:
405+
raise KeyError(
406+
"'cell_size` not found in dataset attributes. "
407+
"Please supply the ADCP's cell size."
408+
)
409+
else:
410+
cs = [cell_size]
411+
387412
if not inplace:
388413
ds = ds.copy(deep=True)
389414

390415
# Get all variables with 'range' coordinate
391416
profile_vars = [h for h in ds.keys() if any(s for s in ds[h].dims if "range" in s)]
392417

393-
# Fetch cell size
394-
cs = [
395-
a
396-
for a in ds.attrs
397-
if (
398-
("cell_size" in a)
399-
and ("_bt" not in a)
400-
and ("_alt" not in a)
401-
and ("wave" not in a)
402-
)
403-
]
404-
405418
# Apply range_offset if available
406419
range_offset = __check_for_range_offset(ds)
407420
if range_offset:
408421
range_limit = (
409-
(ds["depth"] - range_offset) * np.cos(beam_angle) - ds.attrs[cs[0]]
422+
(ds["depth"] - range_offset) * np.cos(beam_angle) - ds.attrs[cs]
410423
) + range_offset
411424
else:
412-
range_limit = ds["depth"] * np.cos(beam_angle) - ds.attrs[cs[0]]
425+
range_limit = ds["depth"] * np.cos(beam_angle) - ds.attrs[cs]
413426

414427
# Echosounder data needs only be trimmed at water surface
415428
if "echo" in profile_vars:

0 commit comments

Comments
 (0)