I have some complex data (numpy dtype complex128) in an xarray data set which I want to save with to_netcdf. I get the following error:
TypeError: illegal primitive data type, must be one of dict_keys(['S1', 'i1', 'u1', 'i2', 'u2', 'i4', 'u4', 'i8', 'u8', 'f4', 'f8']), got complex128
I do understand that I am passing a data type to the underlying netCDF4 which is not supported. I also found https://unidata.github.io/netcdf4-python/ on compound data types with netcdf4. But unfortunately I don't see how I can apply that to my problem as I am not directly working with the netcdf4 library.
Can I save data of the data type complex128 to netcdf while keeping the data type (using xarray.DataArray.to_netcdf
)?
MWE:
import numpy as np
import xarray as xr
complex = [np.complex(1.0, 1.0), np.complex(2.0, 1.0), np.complex(3.0, 1.0), np.complex(4.0, 1.0)]
data = xr.DataArray(complex)
data.to_netcdf(r'test.nc')