You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using Xee to write a netCDF file may yield the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[<ipython-input-7-0bc080b1dc4b>](https://localhost:8080/#) in <cell line: 1>()
----> 1 ds.to_netcdf("out.nc")
7 frames
[/usr/local/lib/python3.10/dist-packages/xarray/core/dataset.py](https://localhost:8080/#) in to_netcdf(self, path, mode, format, group, engine, encoding, unlimited_dims, compute, invalid_netcdf, auto_complex)
2344 from xarray.backends.api import to_netcdf
2345
-> 2346 return to_netcdf( # type: ignore[return-value] # mypy cannot resolve the overloads:(
2347 self,
2348 path,
[/usr/local/lib/python3.10/dist-packages/xarray/backends/api.py](https://localhost:8080/#) in to_netcdf(dataset, path_or_file, mode, format, group, engine, encoding, unlimited_dims, compute, multifile, invalid_netcdf, auto_complex)
1870 # TODO: allow this work (setting up the file for writing array data)
1871 # to be parallelized with dask
-> 1872 dump_to_store(
1873 dataset, store, writer, encoding=encoding, unlimited_dims=unlimited_dims
1874 )
[/usr/local/lib/python3.10/dist-packages/xarray/backends/api.py](https://localhost:8080/#) in dump_to_store(dataset, store, writer, encoder, encoding, unlimited_dims)
1917 variables, attrs = encoder(variables, attrs)
1918
-> 1919 store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
1920
1921
[/usr/local/lib/python3.10/dist-packages/xarray/backends/common.py](https://localhost:8080/#) in store(self, variables, attributes, check_encoding_set, writer, unlimited_dims)
373 variables, attributes = self.encode(variables, attributes)
374
--> 375 self.set_attributes(attributes)
376 self.set_dimensions(variables, unlimited_dims=unlimited_dims)
377 self.set_variables(
[/usr/local/lib/python3.10/dist-packages/xarray/backends/common.py](https://localhost:8080/#) in set_attributes(self, attributes)
390 """
391 for k, v in attributes.items():
--> 392 self.set_attribute(k, v)
393
394 def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=None):
[/usr/local/lib/python3.10/dist-packages/xarray/backends/scipy_.py](https://localhost:8080/#) in set_attribute(self, key, value)
226 def set_attribute(self, key, value):
227 self._validate_attr_key(key)
--> 228 value = encode_nc3_attr_value(value)
229 setattr(self.ds, key, value)
230
[/usr/local/lib/python3.10/dist-packages/xarray/backends/netcdf3.py](https://localhost:8080/#) in encode_nc3_attr_value(value)
94 value = value.encode(STRING_ENCODING)
95 else:
---> 96 value = coerce_nc3_dtype(np.atleast_1d(value))
97 if value.ndim > 1:
98 raise ValueError("netCDF attributes must be 1-dimensional")
[/usr/local/lib/python3.10/dist-packages/xarray/backends/netcdf3.py](https://localhost:8080/#) in coerce_nc3_dtype(arr)
81 cast_arr = arr.astype(new_dtype)
82 if not (cast_arr == arr).all():
---> 83 raise ValueError(
84 COERCION_VALUE_ERROR.format(dtype=dtype, new_dtype=new_dtype)
85 )
ValueError: could not safely cast array from int64 to int32. While it is not always the case, a common reason for this is that xarray has deemed it safest to encode np.datetime64[ns] or np.timedelta64[ns] values with int64 values representing units of 'nanoseconds'. This is either due to the fact that the times are known to require nanosecond precision for an accurate round trip, or that the times are unknown prior to writing due to being contained in a chunked array. Ways to work around this are either to use a backend that supports writing int64 values, or to manually specify the encoding['units'] and encoding['dtype'] (e.g. 'seconds since 1970-01-01' and np.dtype('int32')) on the time variable(s) such that the times can be serialized in a netCDF3 file (note that depending on the situation, however, this latter option may result in an inaccurate round trip).
This can be resolved by installing the netCDF4 Python package, which is not available by default in Google Colab.
Using Xee to write a netCDF file may yield the following error:
This can be resolved by installing the netCDF4 Python package, which is not available by default in Google Colab.
!pip install netCDF4
Notebook demonstrating the error and workaround:
https://colab.research.google.com/gist/tylere/798f73d6ebfb0eb92bb92bb79a6ae390/netcdf-file-writing-issue.ipynb
The text was updated successfully, but these errors were encountered: