forked from SciTools/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more tests and split into integration and unit tests. Testing w…
…arnings/errors aswell.
- Loading branch information
Showing
4 changed files
with
162 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import math | ||
|
||
import cartopy.io.shapereader as shpreader | ||
import numpy as np | ||
|
||
import iris | ||
import iris.tests as tests | ||
from iris.util import apply_shapefile | ||
|
||
|
||
@tests.skip_data | ||
class TestCubeMasking(tests.IrisTest): | ||
ne_countries = shpreader.natural_earth( | ||
resolution="10m", category="cultural", name="admin_0_countries" | ||
) | ||
reader = shpreader.Reader(ne_countries) | ||
|
||
def testGlobal(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "global", "xyt", "SMALL_hires_wind_u_for_ipcc4.nc"] | ||
) | ||
test_global = iris.load_cube(path) | ||
ne_russia = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "Russia" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile(ne_russia, test_global) | ||
print(np.sum(masked_test.data)) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 76845.37, rel_tol=0.00001 | ||
), "Global data with Russia mask failed test" | ||
|
||
def testRotated(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "rotated", "xy", "rotPole_landAreaFraction.nc"] | ||
) | ||
test_rotated = iris.load_cube(path) | ||
ne_germany = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "Germany" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile(ne_germany, test_rotated) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 179.46872, rel_tol=0.00001 | ||
), "rotated europe data with German mask failed test" | ||
|
||
def testTransverseMercator(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "transverse_mercator", "tmean_1910_1910.nc"] | ||
) | ||
test_transverse = iris.load_cube(path) | ||
ne_uk = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "United Kingdom" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile(ne_uk, test_transverse) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 90740.25, rel_tol=0.00001 | ||
), "transverse mercator UK data with UK mask failed test" | ||
|
||
def testRotatedWeighted(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "rotated", "xy", "rotPole_landAreaFraction.nc"] | ||
) | ||
test_rotated = iris.load_cube(path) | ||
ne_germany = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "Germany" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile( | ||
ne_germany, test_rotated, minimum_weight=0.9 | ||
) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 125.60199, rel_tol=0.00001 | ||
), "rotated europe data with 0.9 weight germany mask failed test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,82 @@ | ||
import math | ||
|
||
import cartopy.io.shapereader as shpreader | ||
import numpy as np | ||
import pytest | ||
import shapely | ||
|
||
import iris | ||
from iris.coords import DimCoord | ||
import iris.cube | ||
from iris.exceptions import IrisUserWarning | ||
import iris.tests as tests | ||
from iris.util import apply_shapefile | ||
|
||
|
||
@tests.skip_data | ||
class TestCubeMasking(tests.IrisTest): | ||
ne_countries = shpreader.natural_earth( | ||
resolution="10m", category="cultural", name="admin_0_countries" | ||
class TestBasicCubeMasking(tests.IrisTest): | ||
basic_data = np.array([[1, 2], [3, 4]]) | ||
basic_cube = iris.cube.Cube(basic_data) | ||
coord = DimCoord( | ||
np.array([0, 1]), | ||
standard_name="projection_y_coordinate", | ||
bounds=[[0, 0.5], [0.5, 1]], | ||
units="1", | ||
) | ||
basic_cube.add_dim_coord(coord, 0) | ||
coord = DimCoord( | ||
np.array([0, 1]), | ||
standard_name="projection_x_coordinate", | ||
bounds=[[0, 0.5], [0.5, 1]], | ||
units="1", | ||
) | ||
reader = shpreader.Reader(ne_countries) | ||
basic_cube.add_dim_coord(coord, 1) | ||
|
||
def testGlobal(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "global", "xyt", "SMALL_hires_wind_u_for_ipcc4.nc"] | ||
) | ||
test_global = iris.load_cube(path) | ||
ne_russia = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "Russia" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile(ne_russia, test_global) | ||
print(np.sum(masked_test.data)) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 76845.37, rel_tol=0.00001 | ||
), "Global data with Russia mask failed test" | ||
def testBasicCubeIntersect(self): | ||
shape = shapely.geometry.box(0.6, 0.6, 1, 1) | ||
masked_cube = apply_shapefile(shape, self.basic_cube) | ||
assert ( | ||
np.sum(masked_cube.data) == 4 | ||
), f"basic cube masking failed test - expected 4 got {np.sum(masked_cube.data)}" | ||
|
||
def testRotated(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "rotated", "xy", "rotPole_landAreaFraction.nc"] | ||
def testBasicCubeIntersectLowWeight(self): | ||
shape = shapely.geometry.box(0.5, 0.5, 1, 1) | ||
masked_cube = apply_shapefile( | ||
shape, self.basic_cube, minimum_weight=0.2 | ||
) | ||
test_rotated = iris.load_cube(path) | ||
ne_germany = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "Germany" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile(ne_germany, test_rotated) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 179.46872, rel_tol=0.00001 | ||
), "rotated europe data with German mask failed test" | ||
assert ( | ||
np.sum(masked_cube.data) == 4 | ||
), f"basic cube masking weighting failed test - expected 4 got {np.sum(masked_cube.data)}" | ||
|
||
def testTransverseMercator(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "transverse_mercator", "tmean_1910_1910.nc"] | ||
def testBasicCubeIntersectHighWeight(self): | ||
shape = shapely.geometry.box(0.1, 0.6, 1, 1) | ||
masked_cube = apply_shapefile( | ||
shape, self.basic_cube, minimum_weight=0.5 | ||
) | ||
test_transverse = iris.load_cube(path) | ||
ne_uk = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "United Kingdom" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile(ne_uk, test_transverse) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 90740.25, rel_tol=0.00001 | ||
), "transverse mercator UK data with UK mask failed test" | ||
assert ( | ||
np.sum(masked_cube.data) == 7 | ||
), f"basic cube masking weighting failed test- expected 7 got {np.sum(masked_cube.data)}" | ||
|
||
def testRotatedWeighted(self): | ||
path = tests.get_data_path( | ||
["NetCDF", "rotated", "xy", "rotPole_landAreaFraction.nc"] | ||
) | ||
test_rotated = iris.load_cube(path) | ||
ne_germany = [ | ||
country.geometry | ||
for country in self.reader.records() | ||
if "Germany" in country.attributes["NAME_LONG"] | ||
][0] | ||
masked_test = apply_shapefile( | ||
ne_germany, test_rotated, minimum_weight=0.9 | ||
) | ||
assert math.isclose( | ||
np.sum(masked_test.data), 125.60199, rel_tol=0.00001 | ||
), "rotated europe data with 0.9 weight germany mask failed test" | ||
def testCubeListError(self): | ||
cubelist = iris.cube.CubeList([self.basic_cube]) | ||
shape = shapely.geometry.box(1, 1, 2, 2) | ||
with pytest.raises( | ||
TypeError, match="CubeList object rather than Cube" | ||
): | ||
apply_shapefile(shape, cubelist) | ||
|
||
def testNonCubeError(self): | ||
fake = None | ||
shape = shapely.geometry.box(1, 1, 2, 2) | ||
with pytest.raises(TypeError, match="Received non-Cube object"): | ||
apply_shapefile(shape, fake) | ||
|
||
def testLineShapeWarning(self): | ||
shape = shapely.geometry.LineString([(0, 0.75), (2, 0.75)]) | ||
with pytest.warns(IrisUserWarning, match="invalid type"): | ||
masked_cube = apply_shapefile( | ||
shape, self.basic_cube, minimum_weight=0.1 | ||
) | ||
assert ( | ||
np.sum(masked_cube.data) == 7 | ||
), f"basic cube masking against line failed test - expected 7 got {np.sum(masked_cube.data)}" | ||
|
||
def testShapeInvalid(self): | ||
shape = None | ||
with pytest.raises(TypeError): | ||
apply_shapefile(shape, self.basic_cube) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters