Replies: 3 comments 3 replies
-
Had this same issue, and searching for answers led me to this discussion, as well as the site that explains the encoding options for netcdf4: https://unidata.github.io/netcdf4-python/#Dataset.createVariable. |
Beta Was this translation helpful? Give feedback.
-
stepped on this, too encoding = {var: {zlib": "true"} for var in dataset}
dataset.to_netcdf("/tmp/foo.nc", format="NETCDF4", encoding=encoding) I've enabled compression, and as a result I've lost some unrelated attributes on variables (CF spatial reference in my context). this is confusing and should be documented EDIT: this worked for me for var in dataset:
dataset[var].encoding.update(dict(zlib=True, complevel=5))
dataset.to_netcdf(temp_file_path, format="NETCDF4") RE-EDIT: it failed again with different set of attributes, in paritcular when "_FillValue" is set, it fails with weird error:
where 65535 is _FillValue, and 2460 is axis 0 shape (y: 2460x: 2550). I have no idea why encoding is mixing attributes with shapes (?!) RE-RE-EDIT: it was due to DataArray vs Dataset. This worked for me comp = dict(zlib=True, complevel=5)
if type(data) is xr.Dataset:
for var in data:
data[var].encoding.update(comp)
if type(data) is xr.DataArray:
data.encoding.update(comp)
data.to_netcdf(temp_file_path, format="NETCDF4") |
Beta Was this translation helpful? Give feedback.
-
it would probably be worth noting in the documentation that the Line 1272 in 2ef82c5 so it's not really surprising that the existing encoding is lost. xref #5954 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What is the best practice when compressing a netCDF file when calling
to_netcdf
?From: https://stackoverflow.com/questions/40766037/specify-encoding-compression-for-many-variables-in-xarray-dataset-when-write-to
However, it seems like it misses some information from the encoding that way. For example
_FillValue
seems to get lost.And with:
I end up with:
However, this works:
Related: #6929
Beta Was this translation helpful? Give feedback.
All reactions