Repository: pangeo-data/xESMF

Issue:
More options for `grid_global`
I find myself using the utility function `xe.util.grid_global()` quite a bit, but was wondering if it would be possible to enable to change the default longitude convention (-180, 180) to e.g. (0,360). Its a minor thing, but this would help me with my current, very Pacific focused work. 

Maybe this is already possible? If not, id be happy to submit a PR.

On a bit longer tangent: This functionality has quite a bit of overlap with [xgcm.autogenerate](https://xgcm.readthedocs.io/en/latest/api.html#module-xgcm.autogenerate), which is old, clunky, and will probably be dropped in the next xgcm release. I would love to get peoples opinion here if we could combine the ability to create completely new lon/lat datasets with bounds (xesmf) with the ability to generate bounds for gridded datasets which only provide cell center coordinates (xgcm.autogenerate). 

Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index e84cc1e..9370243 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -4,6 +4,10 @@ What's new
 0.6.4 (unreleased)
 ------------------
 
+New features
+~~~~~~~~~~~~
+- Support both [-180, 180] and [0, 360] conventions in `grid_global` (:issue:`149`). By `David Huard <https://github.com/huard>`_
+
 Documentation
 ~~~~~~~~~~~~~
 - Fix API doc build (:pull:`194`). By `David Huard <https://github.com/huard>`_
diff --git a/xesmf/util.py b/xesmf/util.py
index ee379aa..b44abc6 100644
--- a/xesmf/util.py
+++ b/xesmf/util.py
@@ -126,7 +126,7 @@ def cf_grid_2d(lon0_b, lon1_b, d_lon, lat0_b, lat1_b, d_lat):
     return ds
 
 
-def grid_global(d_lon, d_lat, cf=False):
+def grid_global(d_lon, d_lat, cf=False, lon1=180):
     """
     Global 2D rectilinear grid centers and bounds
 
@@ -138,6 +138,9 @@ def grid_global(d_lon, d_lat, cf=False):
       Latitude step size, i.e. grid resolution
     cf : bool
       Return a CF compliant grid.
+    lon1 : {180, 360}
+      Right longitude bound. According to which convention is used longitudes will
+      vary from -180 to 180 or from 0 to 360.
 
     Returns
     -------
@@ -157,10 +160,12 @@ def grid_global(d_lon, d_lat, cf=False):
             'might not cover the globe uniformally'.format(d_lat)
         )
 
+    lon0 = lon1 - 360
+
     if cf:
-        return cf_grid_2d(-180, 180, d_lon, -90, 90, d_lat)
+        return cf_grid_2d(lon0, lon1, d_lon, -90, 90, d_lat)
 
-    return grid_2d(-180, 180, d_lon, -90, 90, d_lat)
+    return grid_2d(lon0, lon1, d_lon, -90, 90, d_lat)
 
 
 def _flatten_poly_list(polys):


Test patch:
diff --git a/xesmf/tests/test_util.py b/xesmf/tests/test_util.py
index 502da2f..1ca8f9b 100644
--- a/xesmf/tests/test_util.py
+++ b/xesmf/tests/test_util.py
@@ -13,6 +13,12 @@ def test_grid_global():
     assert ds['lon_b'].values.shape == refshape_b
     assert ds['lat_b'].values.shape == refshape_b
 
+    ds = xe.util.grid_global(1.5, 1.5, lon1=180)
+    assert ds['lon_b'].isel(x_b=-1)[-1] == 180
+
+    ds = xe.util.grid_global(1.5, 1.5, lon1=360)
+    assert ds['lon_b'].isel(x_b=-1)[-1] == 360
+
 
 def test_grid_global_bad_resolution():
     with pytest.warns(UserWarning):