rioxarray
rioxarray copied to clipboard
Dataset.to_raset fails if nodata for some of the dataset variables is None
Basically the title is self-explaining, I ran into this issue recently.
This is the failing line: https://github.com/corteva/rioxarray/blob/master/rioxarray/raster_dataset.py#L520
The problem is caused by the line > https://github.com/corteva/rioxarray/blob/master/rioxarray/raster_dataset.py#L510 It should instead write: nodatavals.append(self._obj[data_var].rio.nodata if self._obj[data_var].rio.nodata is not None)
None values for the nodata attr in individual DataArrays are eitherway ignored in the final function that writes the raster: https://github.com/corteva/rioxarray/blob/5fcf0cea9c2bd524ac48d87eacb2c846fc3478f2/rioxarray/raster_array.py#L1144
Cheers
Please provide a simple reproducible example. Thanks!
Honestly,
it was harder to reproduce than expected. Here is my use case in short: I download a bunch of geotiffs for an area (satellite images in different dates), and put them together in a .zarr dataset. I've attached a dataset with one date. And all data vars have nodata np.nan here. However, when I do some calculations in order to construct a new variable in the dataset, that variable has None set as the nodata, which causes the error in the post above.
Steps to reproduce:
import xarray as xr
ds = xr.open_zarr("test.zarr")
ds['test'] = ds['B02']/B['B03']
ds.B02.rio.nodata
ds.test.rio.nodata
ds.rio.to_raster("notworking.tif")
The nodata value needs to be consistent if you want to write it to a raster. That means it needs to be None or NaN for all bands. However, in your scenario, some are NaN, which indicates that it has been masked and scaled.
These references may be helpful for you:
- https://corteva.github.io/rioxarray/stable/getting_started/nodata_management.html
- https://corteva.github.io/rioxarray/stable/getting_started/manage_information_loss.html