rioxarray icon indicating copy to clipboard operation
rioxarray copied to clipboard

Dataset.to_raset fails if nodata for some of the dataset variables is None

Open GILAB-RS opened this issue 2 years ago • 3 comments

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

GILAB-RS avatar Feb 02 '23 15:02 GILAB-RS

Please provide a simple reproducible example. Thanks!

snowman2 avatar Feb 02 '23 15:02 snowman2

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")

test.zarr.zip

GILAB-RS avatar Feb 03 '23 09:02 GILAB-RS

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

snowman2 avatar Feb 03 '23 14:02 snowman2