Repository: regionmask/regionmask

Issue:
edgepoints at -180°E/ 0°E not masked if lon contains NaN
Points at 180°W or 0°E can be wrongly not assigned to regions if `lat` contains `NaN` values (which is very unlikely but not necessarily impossible).

```python
import numpy as np
import regionmask

lon = np.arange(0, 360, 10)
lat = np.arange(90, -91, -1)

lon_nan = np.concatenate([lon, [np.nan]])

polygon = [[-180, 90], [180, 90], [180, -90], [-180, -90]]


mask_nan = regionmask.Regions([polygon]).mask(lon_nan, lat)
mask = regionmask.Regions([polygon]).mask(lon, lat)

assert mask_nan.isel(lon=slice(None, -1)).equals(mask)
```

![edgepoints_nan](https://github.com/regionmask/regionmask/assets/10194086/3ab67f1e-7caa-42f1-a0b1-a6d32b380f5d)

<details><summary>Details</summary>
<p>

```python
import matplotlib.pyplot as plt


mask = regionmask.defined_regions.ar6.all.mask(lon, lat)
mask_nan = regionmask.defined_regions.ar6.all.mask(lon_nan, lat)

f, axs = plt.subplots(1, 2)

mask_nan.isel(lon=slice(None, -1)).plot(ax=axs[1], add_colorbar=False)
mask.plot(ax=axs[0], add_colorbar=False)

for ax in axs:
    ax.set_aspect("equal")

axs[0].set_title("No NaN")
axs[1].set_title("NaN")
plt.savefig("edgepoints_nan.png", dpi=300, bbox_inches="tight")

```


</p>
</details> 



Discussion:
codecov-commenter: ## [Codecov](https://app.codecov.io/gh/regionmask/regionmask/pull/428?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) Report
> Merging [#428](https://app.codecov.io/gh/regionmask/regionmask/pull/428?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) (433a3b5) into [main](https://app.codecov.io/gh/regionmask/regionmask/commit/b9a99de8799b15432795278f98cd3e826add00bf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask) (b9a99de) will **not change** coverage.
> The diff coverage is `100.00%`.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please [install the Github App Integration](https://github.com/apps/codecov) for your organization. [Read more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask).

```diff
@@           Coverage Diff           @@
##             main     #428   +/-   ##
=======================================
  Coverage   98.13%   98.13%           
=======================================
  Files          18       18           
  Lines        1502     1502           
=======================================
  Hits         1474     1474           
  Misses         28       28           
```

| Flag | Coverage Δ | |
|---|---|---|
| bare-minimum | `90.01% <100.00%> (ø)` | |
| min-all-deps | `96.00% <100.00%> (ø)` | |
| shapely_lt_2 | `94.80% <100.00%> (ø)` | |
| unittests | `98.13% <100.00%> (ø)` | |

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://app.codecov.io/gh/regionmask/regionmask/pull/428?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://app.codecov.io/gh/regionmask/regionmask/pull/428?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=regionmask#diff-cmVnaW9ubWFzay9jb3JlL21hc2sucHk=) | `99.39% <100.00%> (ø)` | |



Patch:
diff --git a/docs/whats_new.rst b/docs/whats_new.rst
index 6e8e4a7..eafc769 100644
--- a/docs/whats_new.rst
+++ b/docs/whats_new.rst
@@ -50,6 +50,8 @@ New regions
 Bug Fixes
 ~~~~~~~~~
 
+- Fix the detection of edge points at -180°E or 0°E if longitude values contain ``NA``
+  values (:issue:`426`).
 - Fix the wrapping of longitudes that contain ``NA`` values and simplify the ``_wrapAngle``
   function. Note the wrapping does not dependend on the longitude coordinates since
   :pull:`271` and thus this bug did not affect users since v0.8.0 (:pull:`425`).
diff --git a/regionmask/core/mask.py b/regionmask/core/mask.py
index 94918c2..260268a 100644
--- a/regionmask/core/mask.py
+++ b/regionmask/core/mask.py
@@ -501,7 +501,7 @@ def _mask_edgepoints_shapely(
         mask_unassigned = np.isnan(mask)
 
     # find points at -180°E/0°E
-    if lon.min() < 0:
+    if np.nanmin(lon) < 0:
         LON_180W_or_0E = np.isclose(LON, -180.0) & mask_unassigned
     else:
         LON_180W_or_0E = np.isclose(LON, 0.0) & mask_unassigned


Test patch:
diff --git a/regionmask/tests/test_mask.py b/regionmask/tests/test_mask.py
index 7b13d5a..8935937 100644
--- a/regionmask/tests/test_mask.py
+++ b/regionmask/tests/test_mask.py
@@ -876,6 +876,20 @@ def test_mask_whole_grid(method, regions, lon):
     assert mask.sel(lat=-90).isnull().all()
 
 
+@pytest.mark.parametrize("regions", [r_GLOB_180, r_GLOB_360])
+@pytest.mark.parametrize("lon", [lon180, lon360])
+def test_mask_whole_grid_nan_lon(regions, lon):
+    # https://github.com/regionmask/regionmask/issues/426
+
+    lat = np.arange(90, -91, -10)
+    lon = np.concatenate([lon, [np.NaN]])
+    mask = regions.mask(lon, lat)
+
+    assert mask.isel(lon=-1).isnull().all()
+
+    assert (mask.isel(lon=slice(None, -1)) == 0).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):