pangeo-data / xESMF

Universal Regridder for Geospatial Data
http://xesmf.readthedocs.io/
MIT License
183 stars 32 forks source link

`xesmf.util.grid_global` can generate points outside the expected domain #181

Closed spencerkclark closed 1 year ago

spencerkclark commented 2 years ago

I'm guessing there can be a smaller example than this, but I encountered this recently in my work. Notice that there is a longitude center coordinate greater than 180 degrees:

>>> import xesmf
>>> d_lon = 360 / 4320
>>> d_lat = 180 / 2160
>>> xesmf.util.grid_global(d_lon, d_lat).lon
<xarray.DataArray 'lon' (y: 2160, x: 4321)>
array([[-179.95833333, -179.875     , -179.79166667, ...,  179.875     ,
         179.95833333,  180.04166667],
       [-179.95833333, -179.875     , -179.79166667, ...,  179.875     ,
         179.95833333,  180.04166667],
       [-179.95833333, -179.875     , -179.79166667, ...,  179.875     ,
         179.95833333,  180.04166667],
       ...,
       [-179.95833333, -179.875     , -179.79166667, ...,  179.875     ,
         179.95833333,  180.04166667],
       [-179.95833333, -179.875     , -179.79166667, ...,  179.875     ,
         179.95833333,  180.04166667],
       [-179.95833333, -179.875     , -179.79166667, ...,  179.875     ,
         179.95833333,  180.04166667]])
Coordinates:
    lon      (y, x) float64 -180.0 -179.9 -179.8 -179.7 ... 179.9 180.0 180.0
    lat      (y, x) float64 -89.96 -89.96 -89.96 -89.96 ... 89.96 89.96 89.96
Dimensions without coordinates: y, x

More generally I'm +1 on @jbusecke's suggestion for more options in grid_global in #149; perhaps this issue could be kept in mind in such a refactor.

jbusecke commented 2 years ago

I have been wondering about this more. Perhaps this should not even be a functionality of xesmf? Should there be a stand alone package that can create these sorts of grids, but also add grid metrics (cell area, distance between different grid positions)? Climate models generate these routinely, I wonder if there is a way to expose this functionality in a user friendly python package. This would certainly help with https://github.com/xgcm/xgcm/issues/487 and also https://github.com/jbusecke/cmip6_preprocessing/issues/237. These were all efforts of mine to achieve this functionality, but I find none of them satisfactory (and this is a more general problem than xgcm or cmip6_preprocessing serves).

cc @raphaeldussin

spencerkclark commented 2 years ago

Indeed a more general purpose grid generation tool would be nice. It seems like the grid_global utility in xESMF was largely designed around making it easy to construct examples for documentation and testing, which is probably fine to leave as is. I agree given that it would be natural to include additional functionality to a more general tool that it would probably make sense for it to live outside xESMF.

jbusecke commented 2 years ago

also cc @dcherian