# Regrid from an irregular grid by summing

**URL:** <https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968>\
**Category:** Technical\
**Tags:** python, xarray, regrid\
**Created:** [7 July 2023 03:32 UTC](https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968 "2023-07-07T03:32:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougrichardson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.access-hive.org.au/dougrichardson/32/523_2.png) [@dougrichardson](https://forum.access-hive.org.au/u/dougrichardson)\
**Post date:** [7 July 2023 03:32 UTC](https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968/1 "2023-07-07T03:32:41Z")

</div>

I have an array of Australia’s population density, `pop`. The data have spatial point dimensions (`x` and `y`) in units of metres ([GDA94 / Australian Albers - EPSG:3577](https://epsg.io/3577)). Using `pyproj`, I transformed the projection to a regular lat/lon grid and added these as new coordinates to my array. The two projections look like this:

```auto
import xarray as xr
import matplotlib.pyplot as plt

pop = xr.open_dataset("/g/data/w42/dr6273/work/data/ABS/apg22r_1_0_0.nc")["population_density"]

fig, ax = plt.subplots(1, 2, figsize=(7,3))
pop.plot.imshow(ax=ax[0], add_colorbar=False, vmax=5)
pop.plot.pcolormesh("lon", "lat", ax=ax[1], add_colorbar=False, vmax=5)
ax[0].set_title("Point grid")
ax[1].set_title("Regular lat/lon")
plt.tight_layout()

```

![image](https://us1.discourse-cdn.com/flex020/uploads/access1/original/1X/7071d464489a98852c8ce9c9393b417fbcb5850d.png)

 ![image](https://us1.discourse-cdn.com/flex020/uploads/access1/original/1X/f0e470e207ccd69669d2c6336813f9830e68ff5b.png)

I want to regrid this to the coarser ERA5 grid. Ideally, this would be by summing all the higher resolution cells that lie within each ERA5 grid cell. It doesn’t seem like `xesmf` offers this option.

If my array had regular lat/lon dimensions I think I could solve this (using `groupby_bins` or something). But, as lat/lon are irregularly-spaced coordinates its not clear how to approach this. By irregular, I mean the difference in lat/lon between consecutive x/y cells is not uniform. This figure shows the difference in lon values for a given y:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/access1/original/1X/f83dc29d2575e68ecd7ffe7e1c1d4dc77fe8b3ec.png)

If I didn’t care about summing, I could probably use `xesmf` with conservative regridding, but that raises another problem in that ERA5/NCI does not seem to supply the lat/lon bounds, and I’m unsure how I could manually add my own bounds to the irregular lat/lon in `pop`.

Does anyone have any suggestions? I’m a bit stuck with it!

---

<div class="post-metadata">

**Author:** ![Scott](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.access-hive.org.au/scott/32/37_2.png) [@Scott](https://forum.access-hive.org.au/u/Scott)\
**Post date:** [7 July 2023 04:04 UTC](https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968/2 "2023-07-07T04:04:45Z")

</div>

Can you bin in x,y space and then convert the grid coordinates with pyproj?

---

<div class="post-metadata">

**Author:** ![dougrichardson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.access-hive.org.au/dougrichardson/32/523_2.png) [@dougrichardson](https://forum.access-hive.org.au/u/dougrichardson)\
**Post date:** [7 July 2023 04:39 UTC](https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968/3 "2023-07-07T04:39:21Z")

</div>

Thanks for the suggestion @Scott. I’m not sure about this - to obtain the desired output grid in x,y space would presumably require converting the ERA5 lat/lon grid to x,y space, which would be irregular, and the problem would be the same. I need the population per lat/lon cell…

---

<div class="post-metadata">

**Author:** ![Aidan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.access-hive.org.au/aidan/32/42_2.png) [@Aidan](https://forum.access-hive.org.au/u/Aidan)\
**Post date:** [7 July 2023 05:41 UTC](https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968/4 "2023-07-07T05:41:31Z")

</div>

> [@dougrichardson](#):
>
> ERA5/NCI does not seem to supply the lat/lon bounds, and I’m unsure how I could manually add my own bounds to the irregular lat/lon in `pop`.

In a different topic @dale.roberts provided a Jupyter notebook that utilised an Iris function [guess\_bounds](https://scitools-iris.readthedocs.io/en/latest/generated/api/iris.coords.html#iris.coords.Coord.guess_bounds) that might be useful in your case:

> [@Regridding reanalysis to n48 results in too cold climatology](https://forum.access-hive.org.au/t/regridding-reanalysis-to-n48-results-in-too-cold-climatology/877/4):
>
> Hi @pia I’ve had a chance to revisit this a bit today based on @Aidan’s comment. I’ve come up with [this notebook](https://notebooksharing.space/view/237d632b1d29dfdfce80b4f784b2b8f0050f23d9842c7955fe0f1c67351cfe17#displayOptions=) which seems to have had the desired effect on the means. The hires and lowres means now match, and they’ve increased to a more reasonable 13-17 degrees C now that the polar grid cells are weighted lower. I acknowledge I’ve probably done this in the least practical way possible, but I’m hoping Cunningham’s Law prevails here and someone else has a better answer.

According to the [ESMF docs](https://earthsystemmodeling.org/regrid/)

> - First-order conservative: Preserves the integral of the source field across the regridding. For this method, weight calculation is based on the ratio of source cell area overlapped with the corresponding destination cell area. If the user areas option (see below) is not used, then the areas used in this calculation are those calculated by ESMF and thus the ones for which the conservation holds. The user areas option allows the user to adjust the interpolation weights so that conservation is based on user-supplied areas.

Which _sounds_ like it should sum when regridding. Easy enough to check.

---

<div class="post-metadata">

**Author:** ![dougrichardson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.access-hive.org.au/dougrichardson/32/523_2.png) [@dougrichardson](https://forum.access-hive.org.au/u/dougrichardson)\
**Post date:** [10 July 2023 01:25 UTC](https://forum.access-hive.org.au/t/regrid-from-an-irregular-grid-by-summing/968/5 "2023-07-10T01:25:03Z")

</div>

Thanks a lot @Aidan. I tried using `iris.guess_bounds` with conservative remapping, but that method does not sum. However, your comment prompted me to read about the other regridding methods properly, and it seems as though the following method will do what I want:

> - Nearest destination to source: Each source point is mapped to the closest destination point. A destination point can be mapped to multiple source points, in which case the destination is the sum of the source values. Some destination points may not be mapped.

In short, the solution was to use `xesmf` with the method `nearest_d2s`.

In case others come across this, I’ve put my code on my GitHub, including the steps on how to convert the projection: [https://github.com/dougrichardson/tutorials/blob/main/population\_mask.ipynb](https://github.com/dougrichardson/tutorials/blob/main/population_mask.ipynb)
