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

Use mocked geopy.geocoders.Nominatim to avoid ReadTimeoutError #2005

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 99 additions & 113 deletions tests/integration/preprocessor/_regrid/test_extract_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,108 @@
import iris
import iris.fileformats
import numpy as np
import pytest
from iris.coords import CellMethod, DimCoord

import tests
from esmvalcore.preprocessor import extract_location


class Test(tests.Test):
def setUp(self):
"""Prepare tests."""
shape = (3, 45, 36)
data = np.arange(np.prod(shape)).reshape(shape)
self.cube = self._make_cube(data)

@staticmethod
def _make_cube(data):
"""Create a 3d synthetic test cube."""
z, y, x = data.shape

# Create the cube.
cm = CellMethod(method='mean',
coords='time',
intervals='20 minutes',
comments=None)
kwargs = dict(standard_name='air_temperature',
long_name='Air Temperature',
var_name='ta',
units='K',
attributes=dict(cube='attribute'),
cell_methods=(cm, ))
cube = iris.cube.Cube(data, **kwargs)

# Create a synthetic test latitude coordinate.
data = np.linspace(-90, 90, y)
cs = iris.coord_systems.GeogCS(iris.fileformats.pp.EARTH_RADIUS)
kwargs = dict(standard_name='latitude',
long_name='Latitude',
var_name='lat',
units='degrees_north',
attributes=dict(latitude='attribute'),
coord_system=cs)
ycoord = DimCoord(data, **kwargs)
ycoord.guess_bounds()
cube.add_dim_coord(ycoord, 1)

# Create a synthetic test longitude coordinate.
data = np.linspace(0, 360, x)
kwargs = dict(standard_name='longitude',
long_name='Longitude',
var_name='lon',
units='degrees_east',
attributes=dict(longitude='attribute'),
coord_system=cs)
xcoord = DimCoord(data, **kwargs)
xcoord.guess_bounds()
cube.add_dim_coord(xcoord, 2)
return cube

def test_extract_only_town_name(self):
"""Test only town name."""
point = extract_location(self.cube,
scheme='nearest',
location='Peñacaballera')
self.assertEqual(point.shape, (3, ))
np.testing.assert_equal(point.data, [1186, 2806, 4426])

def test_extract_town_name_and_region(self):
"""Test town plus region."""
point = extract_location(self.cube,
scheme='nearest',
location='Salamanca,Castilla y León')
self.assertEqual(point.shape, (3, ))
np.testing.assert_equal(point.data, [1186, 2806, 4426])

def test_extract_town_and_country(self):
"""Test town plus country."""
point = extract_location(self.cube,
scheme='nearest',
location='Salamanca,USA')
self.assertEqual(point.shape, (3, ))
np.testing.assert_equal(point.data, [1179, 2799, 4419])

def test_extract_all_params(self):
"""Test town plus region plus country."""
point = extract_location(self.cube,
scheme='nearest',
location='Salamanca,Castilla y León,Spain')
self.assertEqual(point.shape, (3, ))
print(point.data)
np.testing.assert_equal(point.data, [1186, 2806, 4426])

def test_extract_mountain(self):
"""Test town plus region plus country."""
point = extract_location(self.cube,
scheme='nearest',
location='Calvitero,Candelario')
self.assertEqual(point.shape, (3, ))
print(point.data)
np.testing.assert_equal(point.data, [1186, 2806, 4426])

def test_non_existing_location(self):
"""Test town plus region plus country."""
with self.assertRaises(ValueError):
extract_location(self.cube,
@pytest.fixture(autouse=True)
def mocked_geopy_geocoders_nominatim(mocker):
"""Mock :class:`geopy.geocoders.Nominatim`.

See https://github.com/ESMValGroup/ESMValCore/issues/1982.

"""
mocked_nominatim = mocker.patch(
'esmvalcore.preprocessor._regrid.Nominatim', autospec=True
)
geolocation_penacaballera = mocker.Mock(
latitude=40.3442754, longitude=-5.8606859
)
mocked_nominatim.return_value.geocode.side_effect = (
lambda x: geolocation_penacaballera if x == 'Peñacaballera' else None
)


@pytest.fixture
def test_cube():
"""Create a 3d synthetic test cube."""
shape = (3, 45, 36)
data = np.arange(np.prod(shape)).reshape(shape)
z, y, x = shape

# Create the cube.
cm = CellMethod(method='mean',
coords='time',
intervals='20 minutes',
comments=None)
kwargs = dict(standard_name='air_temperature',
long_name='Air Temperature',
var_name='ta',
units='K',
attributes=dict(cube='attribute'),
cell_methods=(cm, ))
cube = iris.cube.Cube(data, **kwargs)

# Create a synthetic test latitude coordinate.
data = np.linspace(-90, 90, y)
cs = iris.coord_systems.GeogCS(iris.fileformats.pp.EARTH_RADIUS)
kwargs = dict(standard_name='latitude',
long_name='Latitude',
var_name='lat',
units='degrees_north',
attributes=dict(latitude='attribute'),
coord_system=cs)
ycoord = DimCoord(data, **kwargs)
ycoord.guess_bounds()
cube.add_dim_coord(ycoord, 1)

# Create a synthetic test longitude coordinate.
data = np.linspace(0, 360, x)
kwargs = dict(standard_name='longitude',
long_name='Longitude',
var_name='lon',
units='degrees_east',
attributes=dict(longitude='attribute'),
coord_system=cs)
xcoord = DimCoord(data, **kwargs)
xcoord.guess_bounds()
cube.add_dim_coord(xcoord, 2)
return cube


def test_extract_successful(test_cube):
"""Test only town name."""
point = extract_location(test_cube,
scheme='nearest',
location='Minas Tirith,Gondor')

def test_no_location_parameter(self):
"""Test if no location supplied."""
with self.assertRaises(ValueError):
extract_location(self.cube,
scheme='nearest',
location=None)

def test_no_scheme_parameter(self):
"""Test if no scheme supplied."""
with self.assertRaises(ValueError):
extract_location(self.cube,
scheme=None,
location='Calvitero,Candelario')
location='Peñacaballera')
assert point.shape == (3, )
np.testing.assert_equal(point.data, [1186, 2806, 4426])


def test_non_existing_location(test_cube):
"""Test town plus region plus country."""
msg = "Requested location Minas Tirith,Gondor can not be found"
with pytest.raises(ValueError, match=msg):
extract_location(test_cube,
scheme='nearest',
location='Minas Tirith,Gondor')


def test_no_location_parameter(test_cube):
"""Test if no location supplied."""
msg = "Location needs to be specified."
with pytest.raises(ValueError, match=msg):
extract_location(test_cube,
scheme='nearest',
location=None)


def test_no_scheme_parameter(test_cube):
"""Test if no scheme supplied."""
msg = "Interpolation scheme needs to be specified."
with pytest.raises(ValueError, match=msg):
extract_location(test_cube,
scheme=None,
location='Calvitero,Candelario')