Search code examples
netcdfpython-xarraycdo-climatenco

Xarray - how to regrid or sum up area variables for different grid resolutions?


I have a dataset at a given resolution and its variable is an Area measure. When regridding to another resolution, the techniques are usually interpolation (bilinear, conservative, etc.), but this would only work for variables where the value is independent of the size of the grid (Think of temperature or precipitation). Because I'm using an Area variable, this would not work as if I want to upscale the resolution of the grid, the Area should be summed instead of interpolated. Therefore, I would like to upscale or group or sum my Area variable according to the change in coordinates (If I upscale 2x across lat and lon, the area would be multiplied by 4, as in the example below), but I do not know how to do it.

Example below:

What I currently have:

test_stack
Out[20]: 
<xarray.DataArray 'Harvested_area' (time: 1, lat: 2, lon: 2)>
array([[[10, 10],
        [10, 10]]])
Coordinates:
  * time     (time) int32 1981
  * lat      (lat) float64 6.246 6.237
  * lon      (lon) float64 -74.25 -74.24

What I would like to have:

sum_test_stack
Out[20]: 
<xarray.DataArray 'Harvested_area' (time: 1, lat: 1, lon: 1)>
array([[[40]]])
Coordinates:
  * time     (time) int32 1981
  * lat      (lat) float64 6.246
  * lon      (lon) float64 -74.25

Thank you!


Solution

  • Try looking at xarray.DataArray.coarsen, using a simple sum as the function. https://docs.xarray.dev/en/stable/generated/xarray.DataArray.coarsen.html