Skip to content

Add support for reading/writing complex numbers with h5netcdf #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Enhancements
thread lock by default for reading from netCDF files. This avoids possible
segmentation faults for reading from netCDF4 files when HDF5 is not
configured properly for concurrent access (:issue:`444`).
- Added support for serializing arrays of complex numbers with `engine='h5netcdf'`.
- The new :py:func:`~xray.save_mfdataset` function allows for saving multiple
datasets to disk simultaneously. This is useful when processing large datasets
with dask.array. For example, to save a dataset too big to fit into memory
Expand Down
4 changes: 3 additions & 1 deletion xray/backends/netCDF4_.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ def _nc4_values_and_dtype(var):
if len(var) > 0:
var = var.astype('O')
dtype = str
elif var.dtype.kind in ['i', 'u', 'f', 'S']:
elif var.dtype.kind == 'S':
# use character arrays instead of unicode, because unicode suppot in
# netCDF4 is still rather buggy
data, dims = maybe_convert_to_char_array(var.data, var.dims)
var = Variable(dims, data, var.attrs, var.encoding)
dtype = var.dtype
elif var.dtype.kind in ['i', 'u', 'f', 'c']:
dtype = var.dtype
else:
raise ValueError('cannot infer dtype for netCDF4 variable')
return var, dtype
Expand Down
5 changes: 5 additions & 0 deletions xray/test/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ def test_orthogonal_indexing(self):
# doesn't work for h5py (without using dask as an intermediate layer)
pass

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


@requires_dask
@requires_netCDF4
Expand Down