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

Regridding to larger grid not resulting in NaNs where no starting data #31

Closed
kjdoore opened this issue Feb 15, 2024 · 4 comments · Fixed by #33
Closed

Regridding to larger grid not resulting in NaNs where no starting data #31

kjdoore opened this issue Feb 15, 2024 · 4 comments · Fixed by #33

Comments

@kjdoore
Copy link
Collaborator

kjdoore commented Feb 15, 2024

When regridding from a smaller spatial extent to a larger spatial extent, I would expect NaNs to be the resulting values in regions of the target grid where no data was present in the original data. This is the result when regridding using the methods that utilized xarray.interp (i.e., linear, cubic, nearest). However, this is not the case for conservative and most_common. I have included an example below.

import numpy as np
import xarray as xr
import xarray_regrid

grid = xarray_regrid.Grid(
    north=48,
    east=48,
    south=0,
    west=0,
    resolution_lat=8,
    resolution_lon=8,
)
target_ds = xarray_regrid.create_regridding_dataset(grid)

data = np.array(
    [
        [2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0],
        [2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
        [3, 3, 3, 3, 0, 0, 0, 0, 1, 1, 1],
        [3, 3, 0, 3, 0, 0, 0, 0, 1, 1, 1],
    ]
)
lat_coords = np.linspace(0, 40, num=11)
lon_coords = np.linspace(0, 40, num=11)

ds = xr.Dataset(data_vars={"lc": (["longitude", "latitude"], data)},
                coords={"longitude": (["longitude"], lon_coords),
                        "latitude": (["latitude"], lat_coords)},
                attrs={"test": "not empty"})

ds.regrid.conservative(target_ds, latitude_coord='latitude')
ds.regrid.most_common(target_ds)
@kjdoore
Copy link
Collaborator Author

kjdoore commented Feb 15, 2024

Similar to #14

@BSchilperoort
Copy link
Contributor

This would be a good issue to fix. I think that the best way to implement this for the conservative regridder would be to compute a mask (only if the data cannot cover the target grid), and replace all values under that mask with np.nan

Doing this inside the actual routines is challenging, as we have to mask NaNs out some way to avoid the entire matrix becoming NaN.

@kjdoore
Copy link
Collaborator Author

kjdoore commented Feb 15, 2024

Yeah, the masking could work. Were you thinking that this would be something that occurs after the regridding? Like a final step?

@BSchilperoort
Copy link
Contributor

I think it's the most simple solution. It would be possible to reduce the target grid for regridding (so it fits the data) and then pad NaNs after regridding, but that's a bit more complex and possibly not worth the effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants