Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Raster.from_xarray() to create raster from a xr.DataArray #521

Merged
merged 10 commits into from
Mar 19, 2024

Conversation

rhugonnet
Copy link
Member

@rhugonnet rhugonnet commented Mar 17, 2024

This PR adds Raster.from_xarray() to create a raster from an Xarray dataset (this loads the data, in the future will be able to cast functions directly on the Xarray object through #446).

Correct the behaviour of to_xarray() which was not converting the data to floating when converting, and not masking nodata values (as default was False for reading mask in open_rasterio()), leading to unmasked nodata values for integer raster types. Now converts to floating by defaults to mask properly as NaNs (only way of masking with Xarray).

Additionally, this function adds two new arguments to Raster.raster_equal(): strict_masked and warn_failure_reason. The former allows to check for less strict equality regarding the mask (masked cells can have different underlying values, as those are never used anyway), and the latter is to raise an understandable error when raster_equal() assertions fail.
We lose information when converting to an Xarray dataset with NaNs (masked values are transformed to NaNs and cannot be retrieved back when re-creating a raster), so we can only check equality with strict_masked=False.

Resolves #519
Resolves #522

@@ -1523,6 +1529,7 @@ def set_nodata(

# Update the nodata value
self._nodata = new_nodata
self.data.fill_value = new_nodata
Copy link
Member Author

@rhugonnet rhugonnet Mar 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This little addition was necessary to make raster_equal (which checks fill_values are equal between raster's masked arrays) happy when set_nodata is called!

@rhugonnet rhugonnet changed the title Add Raster.from_xarray() to convert from a Xarray.DataArray Add Raster.from_xarray() to create raster from a xr.DataArray Mar 17, 2024
Copy link
Member

@adehecq adehecq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice !

@rhugonnet rhugonnet merged commit af2171c into GlacioHack:main Mar 19, 2024
13 checks passed
@rhugonnet rhugonnet deleted the add_from_xarray branch March 19, 2024 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants