Skip to content

Commit

Permalink
Merge pull request pydata#472 from shoyer/h5netcdf-complex
Browse files Browse the repository at this point in the history
Add support for reading/writing complex numbers with h5netcdf
  • Loading branch information
shoyer committed Jul 14, 2015
2 parents 4702421 + 05296c0 commit 0e77e5a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
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

0 comments on commit 0e77e5a

Please sign in to comment.