Skip to content

Commit 1dd9dbc

Browse files
committed
dependency switch from h5netcdf to netcdf4
1 parent a5f0fdc commit 1dd9dbc

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

mhkit/dolfyn/io/api.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import scipy.io as sio
23
import xarray as xr
34
import pkg_resources
@@ -95,10 +96,21 @@ def save(dataset, filename):
9596
if 'config' in key:
9697
dataset.attrs.pop(key)
9798

99+
# handling complex values
100+
dataset.attrs['complex_vars'] = []
101+
for var in dataset.data_vars:
102+
if np.iscomplexobj(dataset[var]):
103+
dataset[var+'_real'] = dataset[var].real
104+
dataset[var+'_imag'] = dataset[var].imag
105+
106+
dataset = dataset.drop(var)
107+
dataset.attrs['complex_vars'].append(var)
108+
98109
dataset.to_netcdf(filename,
99110
format='NETCDF4',
100-
engine='h5netcdf',
101-
invalid_netcdf=True)
111+
engine='netcdf4', #'h5netcdf',
112+
#invalid_netcdf=True,
113+
)
102114

103115

104116
def load(filename):
@@ -115,15 +127,23 @@ def load(filename):
115127
filename += '.nc'
116128

117129
# this engine reorders attributes into alphabetical order
118-
ds = xr.load_dataset(filename, engine='h5netcdf')
119-
130+
ds = xr.load_dataset(filename,
131+
engine='netcdf4', #'h5netcdf',
132+
)
133+
120134
# xarray converts single list items to ints or strings
121-
if hasattr(ds, 'rotate_vars') and len(ds.rotate_vars)==1:
122-
ds.attrs['rotate_vars'] = list(ds.rotate_vars)
135+
if hasattr(ds, 'rotate_vars') and len(ds.rotate_vars[0])==1:
136+
ds.attrs['rotate_vars'] = [ds.rotate_vars]
123137

124-
# reloads lists as numpy arrays???
138+
# reloads lists as numpy arrays??? oi vey netcdf
125139
if hasattr(ds, 'rotate_vars') and type(ds.rotate_vars) is not list:
126140
ds.attrs['rotate_vars'] = list(ds.rotate_vars)
141+
142+
if hasattr(ds, 'complex_vars'):
143+
for var in ds.complex_vars:
144+
ds[var] = ds[var+'_real'] + ds[var+'_imag'] * 1j
145+
ds = ds.drop_vars([var+'_real', var+'_imag'])
146+
ds.attrs.pop('complex_vars')
127147

128148
return ds
129149

mhkit/dolfyn/io/nortek2_defs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def __call__(self, array):
186186
('batt_V', 'H', [], LinFunc(0.1, dtype=dt16)), # Volts
187187
('mag', 'h', [3], LinFunc(0.1, dtype=dt32), 'uT'),
188188
('accel', 'h', [3], LinFunc(1. / 16384 * grav, dtype=dt32), 'm/s^2'),
189-
('ambig_vel', 'h', [], LinFunc(0.001, dtype=dt16), 'm/s'),
189+
('ambig_vel', 'h', [], LinFunc(0.001, dtype=dt32), 'm/s'),
190190
('data_desc', 'H', [], None),
191191
('xmit_energy', 'H', [], None, 'dB'),
192192
('vel_scale', 'b', [], None),
@@ -225,7 +225,7 @@ def __call__(self, array):
225225
('batt_V', 'H', [], LinFunc(0.1, dtype=dt16)), # Volts
226226
('mag', 'h', [3], None, 'gauss'),
227227
('accel', 'h', [3], LinFunc(1. / 16384 * grav, dtype=dt32), 'm/s^2'),
228-
('ambig_vel', 'I', [], LinFunc(0.001, dtype=dt16), 'm/s'),
228+
('ambig_vel', 'I', [], LinFunc(0.001, dtype=dt32), 'm/s'),
229229
('data_desc', 'H', [], None),
230230
('xmit_energy', 'H', [], None, 'dB'),
231231
('vel_scale', 'b', [], None),
@@ -293,7 +293,7 @@ def calc_echo_struct(config, nc):
293293
'alt_raw', 'p_gd', 'std']]):
294294
raise Exception("Echosounder ping contains invalid data?")
295295
if flags['echo']:
296-
dd += [('echo', 'H', [nc], LinFunc(0.01, dtype=dt16), 'dB')]
296+
dd += [('echo', 'H', [nc], LinFunc(0.01, dtype=dt32), 'dB')]
297297
if flags['ahrs']:
298298
dd += _ahrs_def
299299
return DataDef(dd)
@@ -308,18 +308,18 @@ def calc_burst_struct(config, nb, nc):
308308
dd.append(('vel', 'h', [nb, nc], None, 'm/s'))
309309
if flags['amp']:
310310
dd.append(('amp', 'B', [nb, nc],
311-
LinFunc(0.5, dtype=dt16), 'dB'))
311+
LinFunc(0.5, dtype=dt32), 'dB'))
312312
if flags['corr']:
313313
dd.append(('corr', 'B', [nb, nc], None, '%'))
314314
if flags['alt']:
315315
# There may be a problem here with reading 32bit floats if
316316
# nb and nc are odd?
317-
dd += [('alt_dist', 'f', [], LinFunc(dtype=dt16), 'm'),
317+
dd += [('alt_dist', 'f', [], LinFunc(dtype=dt32), 'm'),
318318
('alt_quality', 'H', [], LinFunc(0.01,dtype=dt32), 'dB'),
319319
('alt_status', 'H', [], None)]
320320
if flags['ast']:
321321
dd += [
322-
('ast_dist', 'f', [], LinFunc(dtype=dt16), 'm'),
322+
('ast_dist', 'f', [], LinFunc(dtype=dt32), 'm'),
323323
('ast_quality', 'H', [], LinFunc(0.01,dtype=dt32), 'dB'),
324324
('ast_offset_time', 'h', [], LinFunc(0.0001, dtype=dt32), 's'),
325325
('ast_pressure', 'f', [], None, 'dbar'),

0 commit comments

Comments
 (0)