Open xigrug opened 5 years ago
I also encounter the issue that the way the projection ist stored as an objects breaks the xarray.DataArray method to_netcdf()
I did a work around using the following function:
def write_xarray_to_netcdf(xarray_array, output_path,mode='w', format='NETCDF4', group=None, engine=None,
encoding=None):
"""writes and xarray in a netcdf format outputfile
Uses the xarray typical for wrf-python. The projection objects are transformed into strings
to be able to use them as netcdf attributes
:param xarray_array: xarray.DataArray
:param output_path: str
:param format: 'NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_64BIT' or 'NETCDF3_CLASSIC'
default: 'NETCDF4'
:param group: str, default None
:param engine: 'netcdf4', 'scipy' or 'h5netcdf'
:param encoding: dict, default: None
"""
xarray_array_out = xarray_array.copy(deep=True)
# coordinates are extracted from variable
del xarray_array_out.attrs['coordinates']
# wrf-python projection object cannot be processed
xarray_array_out.attrs['projection'] = str(xarray_array_out.attrs['projection'])
xarray_array_out.to_netcdf(path=output_path, mode=mode, format=format, group=group,
engine=engine,
encoding=encoding)
There is also an issue considering wrf data to netcdf at the xarray package.
However, I think writing to netcdf is common for wrf user and it might be worth adding a function to this package or overriding the xarray method. This might be related to the more gerneal issue #16 .
If there already is a simpler way to do this with this package, I'd be grateful to know
Hi all, I would like to plot figures after importing data from the netcdf file written. I was wondering if it is possible to convert back the attrs information from string to the original wrf.projection object format? Any suggestion would be much appreciated.
Is it possible to convert back the attrs information from string to the original wrf.projection object format?
Options:
proj = ds.attrs["projection"]
ds.attrs["projection"] = str(proj)
ds.to_netcdf(...)
ds.attrs["projection"] = proj
Here is a value:
hgt_850.to_netcdf("hgt_850.nc")
TypeError Traceback (most recent call last)