Repository: regionmask/regionmask

Issue:
Dealing with unusual longitude range
Hello,
I have the following problem. One of the climate models I am dealing with has its grid longitudes ranging from -300 to +60.
Hence, all my regionmask objects that are in between +60 and +180 are ignored when I apply them on my dataset.
It seems like the `wrap_lon` argument only considers the cases 0...+360 and -180...+180. Is there a way such that it can handle also longitudes like the one above?
If it helps, the `cdo sinfov` command outputs the following:
```bash
    -1 : Institut Source   T Steptype Levels Num    Points Num Dtype : Parameter name
     1 : unknown  GFDL-CM4 c instant       1   1   1555200   1  F32z : areacello     
   Grid coordinates :
     1 : curvilinear              : points=1555200 (1440x1080)
                              lon : -299.9995 to 60.03369 degrees_east  circular
                              lat : -81.60305 to 89.93877 degrees_north
                        available : cellbounds
                          mapping : Projection
                               xh : -299.7242 to 60.02771 degrees_east
                               yh : -80.38924 to 89.94596 degrees_north
   Vertical coordinates :
     1 : surface                  : levels=1
```
Any help is highly appreciated :-)

Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) Report
> Merging [#271](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) (44a408a) into [master](https://codecov.io/gh/regionmask/regionmask/commit/61022f14429fcf5fc91c4cd4d62764700df5b480?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) (61022f1) will **decrease** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/regionmask/regionmask/pull/271/graphs/tree.svg?width=650&height=150&src=pr&token=WI9PSJZIB7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask)](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask)

```diff
@@            Coverage Diff             @@
##           master     #271      +/-   ##
==========================================
- Coverage   98.97%   98.96%   -0.01%     
==========================================
  Files          14       14              
  Lines        1068     1067       -1     
==========================================
- Hits         1057     1056       -1     
  Misses         11       11              
```

| Flag | Coverage Δ | |
|---|---|---|
| py36-bare-minimum | `86.15% <100.00%> (-0.02%)` | :arrow_down: |
| py36-min-all-deps | `?` | |
| unittests | `98.96% <100.00%> (-0.01%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) | Coverage Δ | |
|---|---|---|
| [regionmask/core/mask.py](https://codecov.io/gh/regionmask/regionmask/pull/271/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask#diff-cmVnaW9ubWFzay9jb3JlL21hc2sucHk=) | `99.55% <100.00%> (-0.01%)` | :arrow_down: |
| [regionmask/core/utils.py](https://codecov.io/gh/regionmask/regionmask/pull/271/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask#diff-cmVnaW9ubWFzay9jb3JlL3V0aWxzLnB5) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask). Last update [61022f1...44a408a](https://codecov.io/gh/regionmask/regionmask/pull/271?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask).


Patch:
diff --git a/docs/whats_new.rst b/docs/whats_new.rst
index a689350..be56f0e 100644
--- a/docs/whats_new.rst
+++ b/docs/whats_new.rst
@@ -41,6 +41,9 @@ Enhancements
   (:issue:`123`).
 - Can now create masks for regions with arbitrary coordinates e.g. for coordinate reference
   systems that are not lat/ lon based by setting ``wrap_lon=False`` (:issue:`151`).
+- The extent of the longitude coordinates is no longer checked to determine the wrap,
+  now only the extent of the mask is considered (:issue:`249`). This should allow to
+  infer ``wrap_lon`` correctly for more cases (:issue:`213`).
 
 Deprecations
 ~~~~~~~~~~~~
diff --git a/regionmask/core/mask.py b/regionmask/core/mask.py
index ac895b0..0eabd96 100644
--- a/regionmask/core/mask.py
+++ b/regionmask/core/mask.py
@@ -129,11 +129,9 @@ def _mask(
     # automatically detect whether wrapping is necessary
     if wrap_lon is None:
         msg_add = "Set `wrap_lon=False` to skip this check."
-        grid_is_180 = _is_180(lon.min(), lon.max(), msg_add=msg_add)
-
         regions_is_180 = _is_180(*lon_bounds, msg_add=msg_add)
 
-        wrap_lon_ = not regions_is_180 == grid_is_180
+        wrap_lon_ = 180 if regions_is_180 else 360
     else:
         wrap_lon_ = wrap_lon
 
diff --git a/regionmask/core/utils.py b/regionmask/core/utils.py
index 65cb86a..7392542 100644
--- a/regionmask/core/utils.py
+++ b/regionmask/core/utils.py
@@ -93,10 +93,10 @@ def _wrapAngle(lon, wrap_lon=True):
 
     wl = int(wrap_lon)
 
-    if wl == 180 or (lon.max() > 180 and not wl == 360):
+    if wl == 180 or (wl != 360 and lon.max() > 180):
         new_lon = _wrapAngle180(lon.copy())
 
-    if wl == 360 or (lon.min() < 0 and not wl == 180):
+    if wl == 360 or (wl != 180 and lon.min() < 0):
         new_lon = _wrapAngle360(lon.copy())
 
     # check if they are still unique


Test patch:
diff --git a/regionmask/tests/test_mask.py b/regionmask/tests/test_mask.py
index ed84bd4..5e33e72 100644
--- a/regionmask/tests/test_mask.py
+++ b/regionmask/tests/test_mask.py
@@ -136,11 +136,11 @@ def test_mask_xarray_name(method):
     assert msk.name == "region"
 
 
-@pytest.mark.parametrize("ndims", [(2, 1), (1, 2), (0, 1)])
+@pytest.mark.parametrize("ndims", [(2, 1), (1, 2)])
 def test_mask_unequal_ndim(ndims):
 
-    lon = np.zeros(shape=ndims[0] * (2,))
-    lat = np.zeros(shape=ndims[1] * (2,))
+    lon = np.arange(ndims[0] * 2).reshape(ndims[0] * (2,))
+    lat = np.arange(ndims[1] * 2).reshape(ndims[1] * (2,))
 
     with pytest.raises(ValueError, match="Equal number of dimensions required"):
         dummy_region.mask(lon, lat)
@@ -764,6 +764,18 @@ def test_mask_whole_grid(method, regions, lon):
     assert mask.sel(lat=-90).isnull().all()
 
 
+@pytest.mark.parametrize("method", MASK_METHODS)
+@pytest.mark.parametrize("regions", [r_GLOB_180, r_GLOB_360])
+def test_mask_whole_grid_unusual_lon(method, regions):
+    # https://github.com/regionmask/regionmask/issues/213
+
+    lat = np.arange(90, -91, -2.5)
+    lon = np.arange(-300, 60, 2.5)
+    mask = regions.mask(lon, lat, method=method)
+
+    assert (mask == 0).all()
+
+
 def test_inject_mask_docstring():
 
     result = _inject_mask_docstring(True, True)