Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding while cropping needs to stay within sane bounds for shapefiles that span the whole Earth #626

Merged
merged 12 commits into from
May 19, 2020
Merged
9 changes: 8 additions & 1 deletion esmvalcore/preprocessor/_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,21 @@ def _crop_cube(cube, start_longitude, start_latitude, end_longitude,
lon_bound = lon_coord.core_bounds()[0]
lon_step = lon_bound[1] - lon_bound[0]
start_longitude -= lon_step
if start_longitude < 0:
start_longitude = 0
end_longitude += lon_step
if end_longitude > 360:
end_longitude = 360.
lat_bound = lat_coord.core_bounds()[0]
lat_step = lat_bound[1] - lat_bound[0]
start_latitude -= lat_step
if start_latitude < -90:
start_latitude = -90.
end_latitude += lat_step
if end_latitude > 90.:
end_latitude = 90.
cube = extract_region(cube, start_longitude, end_longitude,
start_latitude, end_latitude)

return cube


Expand Down
402 changes: 0 additions & 402 deletions esmvalcore/preprocessor/ne_masks/ne_10m_ocean.README.html

This file was deleted.

1 change: 0 additions & 1 deletion esmvalcore/preprocessor/ne_masks/ne_10m_ocean.VERSION.txt

This file was deleted.

1 change: 0 additions & 1 deletion esmvalcore/preprocessor/ne_masks/ne_10m_ocean.cpg

This file was deleted.

Binary file removed esmvalcore/preprocessor/ne_masks/ne_10m_ocean.dbf
Binary file not shown.
1 change: 0 additions & 1 deletion esmvalcore/preprocessor/ne_masks/ne_10m_ocean.prj

This file was deleted.

Binary file removed esmvalcore/preprocessor/ne_masks/ne_10m_ocean.shp
Binary file not shown.
Binary file removed esmvalcore/preprocessor/ne_masks/ne_10m_ocean.shx
Binary file not shown.
51 changes: 51 additions & 0 deletions tests/unit/preprocessor/_area/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import pytest
from cf_units import Unit
from iris.cube import Cube
from shapely.geometry import Polygon, mapping

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


def _create_sample_full_cube():
cube = Cube(np.zeros((4, 180, 360)), var_name='co2', units='J')
cube.add_dim_coord(
iris.coords.DimCoord(
np.array([10., 40., 70., 110.]),
standard_name='time',
units=Unit('days since 1950-01-01 00:00:00', calendar='gregorian'),
),
0,
)
cube.add_dim_coord(
iris.coords.DimCoord(
np.arange(-90., 90., 1.),
standard_name='latitude',
units='degrees',
),
1,
)
cube.add_dim_coord(
iris.coords.DimCoord(
np.arange(0., 360., 1.),
standard_name='longitude',
units='degrees',
),
2,
)

cube.coord("time").guess_bounds()
cube.coord("longitude").guess_bounds()
cube.coord("latitude").guess_bounds()

return cube


def test_crop_cube(make_testcube, square_shape, tmp_path):
"""Test for cropping a cube by shape bounds."""
with fiona.open(tmp_path / 'test_shape.shp') as geometries:
Expand All @@ -385,6 +420,22 @@ def test_crop_cube(make_testcube, square_shape, tmp_path):
np.testing.assert_array_equal(result.data, expected)


def test_crop_cube_with_ne_file():
"""Test for cropping a cube by shape bounds."""
shp_file = "esmvalcore/preprocessor/ne_masks/ne_50m_ocean.shp"
with fiona.open(shp_file) as geometries:
cube = _create_sample_full_cube()
copy_bounds = list(geometries.bounds)
copy_bounds[2] = 370.
copy_bounds[1] = -99.
copy_bounds[3] = 100.
result = _crop_cube(cube, *tuple(copy_bounds))
result = (result.coord("latitude").points[-1],
result.coord("longitude").points[-1])
expected = (89., 359.)
np.testing.assert_allclose(result, expected)


@pytest.mark.parametrize('crop', [True, False])
def test_extract_shape(make_testcube, square_shape, tmp_path, crop):
"""Test for extracting a region with shapefile"""
Expand Down