Skip to content
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

utility function to save complex values as a netCDF file #4068

Closed
fujiisoup opened this issue May 16, 2020 · 3 comments
Closed

utility function to save complex values as a netCDF file #4068

fujiisoup opened this issue May 16, 2020 · 3 comments

Comments

@fujiisoup
Copy link
Member

Currently, we disallow to save complex values to a netCDF file.
Maybe netCDF itself does not support complex values, but there may be some workarounds.
It would be very handy for me.

The most naive workaround may be to split each complex value into a real and imaginary part, add some flags, and restore it when loading them from the file.
Maybe we may add a special suffix to the variable name?

>>> ds = xr.Dataset({'a': ('x': [1+2j, 2+3j])}, coords={'x': [0, 1]})
>>> ds.to_netcdf('tmp.nc', encode_complex=True)
>>> xr.load_netcdf('tmp.nc')
<xarray.Dataset>
Dimensions:    (x: 2)
Coordinates:
  * x          (x) int64 0 1
Data variables:
    a__real__  (x) int64 1 2
    a__imag__  (x) int64 2 3
>>> xr.load_netcdf('tmp.nc', decode_complex=True)
<xarray.Dataset>
Dimensions:  (x: 2)
Coordinates:
  * x        (x) int64 0 1
Data variables:
    a        (x) complex128 (1+2j) (2+3j) 

I think there may be a better way.
Any thoughts are welcome :)

p.s.
I just found that engine=h5netcdf can save complex values, but the file becomes an invalid netcdf file.
I'm not sure if it worth the trouble just to make a valid netCDF file.

@DWesl
Copy link
Contributor

DWesl commented May 24, 2020

For others reading this issue, the h5netcdf workaround was discussed in #3297, with further discussion on supporting complex numbers in netCDF in cf-convention/discuss#369.

The short version: engine="h5netcdf", invalid_netcdf=True will save these files, but the netCDF-C library doesn't understand the result. Reading with engine="h5netcdf" may be able to round-trip these files, but I haven't checked that.

There is a longer discussion of why netCDF-C doesn't understand these files at Unidata/netcdf-c#267. That specific issue is for booleans, but complex numbers are likely the same.

@dcherian
Copy link
Contributor

Personally, I think the h5netcdf workaround is good enough until there is a CF standard for writing complex numbers.

@fujiisoup
Copy link
Member Author

Thanks @DWesl
Maybe better to continue discussion either in #3297. I'll close this issue.
Thanks for pointing it out.

@dcherian

Personally, I think the h5netcdf workaround is good enough until there is a CF standard for writing complex numbers.

Agreed. Thanks for your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants