corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
507 stars 80 forks source link

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

Open GILAB-RS opened 1 year ago

GILAB-RS commented 1 year ago

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

snowman2 commented 1 year ago

Please provide a simple reproducible example. Thanks!

GILAB-RS commented 1 year ago

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

snowman2 commented 1 year ago

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: