Skip to content

Commit 23c0218

Browse files
committed
Bug fix in spectral processor
There was a bug in the flip_spec function that made it work incorrectly for some specific transformations (specific orientiation of the directional vector and directional resolution). This bug has been fixed and unit tests have been expanded. The boundary reader for reading WW3 netcdf-files was corrected for a bug in reading the directions from the xarray dataset and some function handles were harmonized with the rest of the code.
1 parent 204f7f9 commit 23c0218

File tree

4 files changed

+228
-187
lines changed

4 files changed

+228
-187
lines changed

dnora/aux.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,13 @@ def flip_spec(spec, D):
331331
spec_flip = np.zeros(spec.shape)
332332

333333
ind = np.arange(0,len(D), dtype='int')
334-
dD = np.diff(D).mean()
334+
#dD = np.diff(D).mean()
335+
dD = 360/len(D)
335336
steps = D/dD # How many delta-D from 0
336337

337-
ind_flip = ((ind - 2*steps).astype(int) + len(D)) % len(D)
338+
# Need to move indeces the other way if the vector is decreasing than if it is increasing
339+
direction = np.sign(np.median(np.diff(D)))
340+
ind_flip = ((ind - 2*steps*direction).astype(int) + len(D)) % len(D)
338341

339342
spec_flip=spec[..., list(ind_flip)]
340343

@@ -360,8 +363,8 @@ def shift_spec(spec, D, shift=0):
360363
ind = np.arange(0,len(D), dtype='int')
361364
dD = 360/len(D)
362365

363-
if abs(np.floor(dD)-dD) > 0:
364-
raise Exception ('Shift needs to be multiple of frequency resolution! Otherwise interpolation would be needed.')
366+
if abs(np.floor(shift/dD)-shift/dD) > 0:
367+
raise Exception (f'Shift {shift} needs to be multiple of frequency resolution {dD}, but shift/dD={shift/dD}! Otherwise interpolation would be needed.')
365368

366369
ind_flip = ((ind + int(shift/dD)).astype(int) + len(D)) % len(D)
367370

dnora/bnd/read.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __call__(self, start_time, end_time, inds) -> Tuple:
9191

9292

9393
class File_WW3Nc(BoundaryReader):
94-
def __init__(self, folder: str='', filestring: str='ww3_T0', datestring: str='%Y%m%dT%H%M', stride: int=6, hours_per_file: int=73, last_file: str='', lead_time: int=0) -> None:
94+
def __init__(self, folder: str='', filename: str='ww3_T0', dateftm: str='%Y%m%dT%H%M', stride: int=6, hours_per_file: int=73, last_file: str='', lead_time: int=0) -> None:
9595
self.stride = copy(stride)
9696
self.hours_per_file = copy(hours_per_file)
9797
self.lead_time = copy(lead_time)
@@ -102,8 +102,8 @@ def __init__(self, folder: str='', filestring: str='ww3_T0', datestring: str='%Y
102102
else:
103103
self.folder = copy(folder)
104104

105-
self.filestring = copy(filestring)
106-
self.datestring = copy(datestring)
105+
self.filestring = copy(filename)
106+
self.datestring = copy(dateftm)
107107

108108
return
109109

@@ -149,7 +149,7 @@ def __call__(self, start_time, end_time, inds) -> Tuple:
149149

150150
time = bnd.time.values
151151
freq = bnd.frequency.values
152-
dirs = bnd.directions.values
152+
dirs = bnd.direction.values
153153
spec = bnd.efth.values
154154
lon = bnd.longitude.values[0,:]
155155
lat = bnd.latitude.values[0,:]

tests/unittests/run_unit_tests.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
ls utest*.py > utests.txt
4+
5+
while read test; do
6+
echo "Running test: "$test
7+
python $test
8+
done < utests.txt
9+
10+
rm utests.txt

0 commit comments

Comments
 (0)