Repository: esmvalgroup/esmvalcore
Issue: extract_region preprocessor no longer works on irregular grids

Hi,

we recently changed the irregular grid section of the `extract_region` preprocessor in `preprocessor/_area.py` from this:

```
    # irregular grids
    lats = cube.coord('latitude').points
    lons = cube.coord('longitude').points
    mask = np.ma.array(cube.data).mask
    mask += np.ma.masked_where(lats < start_latitude, lats).mask
    mask += np.ma.masked_where(lats > end_latitude, lats).mask
    mask += np.ma.masked_where(lons > start_longitude, lons).mask
    mask += np.ma.masked_where(lons > end_longitude, lons).mask
    cube.data = np.ma.masked_where(mask, cube.data)
    return cube
```

to this:
```
    # irregular grids
    lats = cube.coord('latitude').points
    lons = cube.coord('longitude').points
    select_lats = start_latitude < lats < end_latitude
    select_lons = start_longitude < lons < end_longitude
    selection = select_lats & select_lons
    data = da.ma.masked_where(~selection, cube.core_data())
    return cube.copy(data)
```

The new code doesn't work for irregular grids. It produces the error message:
```
  File "/users/modellers/ledm/workspace/ESMValToolTest/ESMValTool/esmvalcore/preprocessor/_area.py", line 82, in extract_region
    select_lats = start_latitude < lats < end_latitude
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

```
Any suggestions on how to fix it and keep it lazy? For now, I've reverted my local code back to the previous version. (but I'm not going to push it!)

The reason that we didn't catch the problem at the time is because the ocean recipes that use this were designed to demonstrate the diagnostics, not test the preprocessor. I'm changing recipe_ocean_example.yml to include an irregular grid CMIP5 model for future testing.



Changed files:
esmvalcore/preprocessor/_area.py

diff --git a/esmvalcore/preprocessor/_area.py b/esmvalcore/preprocessor/_area.py
index 9b5cf74bb..5e8b1f781 100644
--- a/esmvalcore/preprocessor/_area.py
+++ b/esmvalcore/preprocessor/_area.py
@@ -57,16 +57,21 @@ def extract_region(cube, start_longitude, end_longitude, start_latitude,
     end_latitude = float(end_latitude)
 
     if cube.coord('latitude').ndim == 1:
+        # Iris check if any point of the cell is inside the region
+        # To check only the center, ignore_bounds must be set to
+        # True (default) is False
         region_subset = cube.intersection(
             longitude=(start_longitude, end_longitude),
-            latitude=(start_latitude, end_latitude))
+            latitude=(start_latitude, end_latitude),
+            ignore_bounds=True,
+        )
         region_subset = region_subset.intersection(longitude=(0., 360.))
         return region_subset
     # irregular grids
     lats = cube.coord('latitude').points
     lons = cube.coord('longitude').points
-    select_lats = start_latitude < lats < end_latitude
-    select_lons = start_longitude < lons < end_longitude
+    select_lats = start_latitude <= lats <= end_latitude
+    select_lons = start_longitude <= lons <= end_longitude
     selection = select_lats & select_lons
     data = da.ma.masked_where(~selection, cube.core_data())
     return cube.copy(data)