Skip to content

Commit 0e77e5a

Browse files
committed
Merge pull request #472 from shoyer/h5netcdf-complex
Add support for reading/writing complex numbers with h5netcdf
2 parents 4702421 + 05296c0 commit 0e77e5a

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Enhancements
2323
thread lock by default for reading from netCDF files. This avoids possible
2424
segmentation faults for reading from netCDF4 files when HDF5 is not
2525
configured properly for concurrent access (:issue:`444`).
26+
- Added support for serializing arrays of complex numbers with `engine='h5netcdf'`.
2627
- The new :py:func:`~xray.save_mfdataset` function allows for saving multiple
2728
datasets to disk simultaneously. This is useful when processing large datasets
2829
with dask.array. For example, to save a dataset too big to fit into memory

xray/backends/netCDF4_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ def _nc4_values_and_dtype(var):
5757
if len(var) > 0:
5858
var = var.astype('O')
5959
dtype = str
60-
elif var.dtype.kind in ['i', 'u', 'f', 'S']:
60+
elif var.dtype.kind == 'S':
6161
# use character arrays instead of unicode, because unicode suppot in
6262
# netCDF4 is still rather buggy
6363
data, dims = maybe_convert_to_char_array(var.data, var.dims)
6464
var = Variable(dims, data, var.attrs, var.encoding)
6565
dtype = var.dtype
66+
elif var.dtype.kind in ['i', 'u', 'f', 'c']:
67+
dtype = var.dtype
6668
else:
6769
raise ValueError('cannot infer dtype for netCDF4 variable')
6870
return var, dtype

xray/test/test_backends.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,11 @@ def test_orthogonal_indexing(self):
699699
# doesn't work for h5py (without using dask as an intermediate layer)
700700
pass
701701

702+
def test_complex(self):
703+
expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))})
704+
with self.roundtrip(expected) as actual:
705+
self.assertDatasetEqual(expected, actual)
706+
702707

703708
@requires_dask
704709
@requires_netCDF4

0 commit comments

Comments
 (0)