xarray-contrib/xarray-regrid

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

Closed this issue · 4 comments

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)

Similar to #14

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.

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

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.