Skip to content

Commit 384748d

Browse files
Merge pull request #626 from ESMValGroup/checks_on_crop_coords
Padding while cropping needs to stay within sane bounds for shapefiles that span the whole Earth
2 parents 310bb57 + 6fc01d7 commit 384748d

File tree

9 files changed

+59
-406
lines changed

9 files changed

+59
-406
lines changed

esmvalcore/preprocessor/_area.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,21 @@ def _crop_cube(cube, start_longitude, start_latitude, end_longitude,
331331
lon_bound = lon_coord.core_bounds()[0]
332332
lon_step = lon_bound[1] - lon_bound[0]
333333
start_longitude -= lon_step
334+
if start_longitude < 0:
335+
start_longitude = 0
334336
end_longitude += lon_step
337+
if end_longitude > 360:
338+
end_longitude = 360.
335339
lat_bound = lat_coord.core_bounds()[0]
336340
lat_step = lat_bound[1] - lat_bound[0]
337341
start_latitude -= lat_step
342+
if start_latitude < -90:
343+
start_latitude = -90.
338344
end_latitude += lat_step
345+
if end_latitude > 90.:
346+
end_latitude = 90.
339347
cube = extract_region(cube, start_longitude, end_longitude,
340348
start_latitude, end_latitude)
341-
342349
return cube
343350

344351

esmvalcore/preprocessor/ne_masks/ne_10m_ocean.README.html

-402
This file was deleted.

esmvalcore/preprocessor/ne_masks/ne_10m_ocean.VERSION.txt

-1
This file was deleted.

esmvalcore/preprocessor/ne_masks/ne_10m_ocean.cpg

-1
This file was deleted.
-138 Bytes
Binary file not shown.

esmvalcore/preprocessor/ne_masks/ne_10m_ocean.prj

-1
This file was deleted.
-6.84 MB
Binary file not shown.
-108 Bytes
Binary file not shown.

tests/unit/preprocessor/_area/test_area.py

+51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import pytest
99
from cf_units import Unit
10+
from iris.cube import Cube
1011
from shapely.geometry import Polygon, mapping
1112

1213
import tests
@@ -377,6 +378,40 @@ def square_composite_shape(request, tmp_path):
377378
return np.ma.masked_array(vals, mask)
378379

379380

381+
def _create_sample_full_cube():
382+
cube = Cube(np.zeros((4, 180, 360)), var_name='co2', units='J')
383+
cube.add_dim_coord(
384+
iris.coords.DimCoord(
385+
np.array([10., 40., 70., 110.]),
386+
standard_name='time',
387+
units=Unit('days since 1950-01-01 00:00:00', calendar='gregorian'),
388+
),
389+
0,
390+
)
391+
cube.add_dim_coord(
392+
iris.coords.DimCoord(
393+
np.arange(-90., 90., 1.),
394+
standard_name='latitude',
395+
units='degrees',
396+
),
397+
1,
398+
)
399+
cube.add_dim_coord(
400+
iris.coords.DimCoord(
401+
np.arange(0., 360., 1.),
402+
standard_name='longitude',
403+
units='degrees',
404+
),
405+
2,
406+
)
407+
408+
cube.coord("time").guess_bounds()
409+
cube.coord("longitude").guess_bounds()
410+
cube.coord("latitude").guess_bounds()
411+
412+
return cube
413+
414+
380415
def test_crop_cube(make_testcube, square_shape, tmp_path):
381416
"""Test for cropping a cube by shape bounds."""
382417
with fiona.open(tmp_path / 'test_shape.shp') as geometries:
@@ -385,6 +420,22 @@ def test_crop_cube(make_testcube, square_shape, tmp_path):
385420
np.testing.assert_array_equal(result.data, expected)
386421

387422

423+
def test_crop_cube_with_ne_file():
424+
"""Test for cropping a cube by shape bounds."""
425+
shp_file = "esmvalcore/preprocessor/ne_masks/ne_50m_ocean.shp"
426+
with fiona.open(shp_file) as geometries:
427+
cube = _create_sample_full_cube()
428+
copy_bounds = list(geometries.bounds)
429+
copy_bounds[2] = 370.
430+
copy_bounds[1] = -99.
431+
copy_bounds[3] = 100.
432+
result = _crop_cube(cube, *tuple(copy_bounds))
433+
result = (result.coord("latitude").points[-1],
434+
result.coord("longitude").points[-1])
435+
expected = (89., 359.)
436+
np.testing.assert_allclose(result, expected)
437+
438+
388439
@pytest.mark.parametrize('crop', [True, False])
389440
def test_extract_shape(make_testcube, square_shape, tmp_path, crop):
390441
"""Test for extracting a region with shapefile"""

0 commit comments

Comments
 (0)