|
| 1 | +"""Test derivation of `ohc`.""" |
| 2 | +import cf_units |
| 3 | +import iris |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +import esmvalcore.preprocessor._derive.siextent as siextent |
| 8 | +from esmvalcore.exceptions import RecipeError |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def cubes_sic(): |
| 13 | + sic_name = 'sea_ice_area_fraction' |
| 14 | + time_coord = iris.coords.DimCoord([0., 1., 2.], |
| 15 | + standard_name='time') |
| 16 | + sic_cube = iris.cube.Cube([[[20, 10], [10, 10]], |
| 17 | + [[10, 10], [10, 10]], |
| 18 | + [[10, 10], [10, 10]]], |
| 19 | + units='%', |
| 20 | + standard_name=sic_name, |
| 21 | + var_name='sic', |
| 22 | + dim_coords_and_dims=[(time_coord, 0)]) |
| 23 | + return iris.cube.CubeList([sic_cube]) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def cubes_siconca(): |
| 28 | + sic_name = 'sea_ice_area_fraction' |
| 29 | + time_coord = iris.coords.DimCoord([0., 1., 2.], |
| 30 | + standard_name='time') |
| 31 | + sic_cube = iris.cube.Cube([[[20, 10], [10, 10]], |
| 32 | + [[10, 10], [10, 10]], |
| 33 | + [[10, 10], [10, 10]]], |
| 34 | + units='%', |
| 35 | + standard_name=sic_name, |
| 36 | + var_name='siconca', |
| 37 | + dim_coords_and_dims=[(time_coord, 0)]) |
| 38 | + return iris.cube.CubeList([sic_cube]) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def cubes(): |
| 43 | + sic_name = 'sea_ice_area_fraction' |
| 44 | + time_coord = iris.coords.DimCoord([0., 1., 2.], |
| 45 | + standard_name='time') |
| 46 | + sic_cube = iris.cube.Cube([[[20, 10], [10, 10]], |
| 47 | + [[10, 10], [10, 10]], |
| 48 | + [[10, 10], [10, 10]]], |
| 49 | + units='%', |
| 50 | + standard_name=sic_name, |
| 51 | + var_name='sic', |
| 52 | + dim_coords_and_dims=[(time_coord, 0)]) |
| 53 | + siconca_cube = iris.cube.Cube([[[20, 10], [10, 10]], |
| 54 | + [[10, 10], [10, 10]], |
| 55 | + [[10, 10], [10, 10]]], |
| 56 | + units='%', |
| 57 | + standard_name=sic_name, |
| 58 | + var_name='siconca', |
| 59 | + dim_coords_and_dims=[(time_coord, 0)]) |
| 60 | + return iris.cube.CubeList([sic_cube, siconca_cube]) |
| 61 | + |
| 62 | + |
| 63 | +def test_siextent_calculation_sic(cubes_sic): |
| 64 | + """Test function ``calculate`` when sic is available.""" |
| 65 | + derived_var = siextent.DerivedVariable() |
| 66 | + out_cube = derived_var.calculate(cubes_sic) |
| 67 | + assert out_cube.units == cf_units.Unit('m2') |
| 68 | + out_data = out_cube.data |
| 69 | + expected = np.ma.ones_like(cubes_sic[0].data) |
| 70 | + expected.mask = True |
| 71 | + expected[0][0][0] = 1. |
| 72 | + np.testing.assert_array_equal(out_data.mask, expected.mask) |
| 73 | + np.testing.assert_array_equal(out_data[0][0][0], expected[0][0][0]) |
| 74 | + |
| 75 | + |
| 76 | +def test_siextent_calculation_siconca(cubes_siconca): |
| 77 | + """Test function ``calculate`` when siconca is available.""" |
| 78 | + derived_var = siextent.DerivedVariable() |
| 79 | + out_cube = derived_var.calculate(cubes_siconca) |
| 80 | + assert out_cube.units == cf_units.Unit('m2') |
| 81 | + out_data = out_cube.data |
| 82 | + expected = np.ma.ones_like(cubes_siconca[0].data) |
| 83 | + expected.mask = True |
| 84 | + expected[0][0][0] = 1. |
| 85 | + np.testing.assert_array_equal(out_data.mask, expected.mask) |
| 86 | + np.testing.assert_array_equal(out_data[0][0][0], expected[0][0][0]) |
| 87 | + |
| 88 | + |
| 89 | +def test_siextent_calculation(cubes): |
| 90 | + """Test function ``calculate`` when sic and siconca are available.""" |
| 91 | + derived_var = siextent.DerivedVariable() |
| 92 | + out_cube = derived_var.calculate(cubes) |
| 93 | + assert out_cube.units == cf_units.Unit('m2') |
| 94 | + out_data = out_cube.data |
| 95 | + expected = np.ma.ones_like(cubes[0].data) |
| 96 | + expected.mask = True |
| 97 | + expected[0][0][0] = 1. |
| 98 | + np.testing.assert_array_equal(out_data.mask, expected.mask) |
| 99 | + np.testing.assert_array_equal(out_data[0][0][0], expected[0][0][0]) |
| 100 | + |
| 101 | + |
| 102 | +def test_siextent_no_data(cubes_sic): |
| 103 | + derived_var = siextent.DerivedVariable() |
| 104 | + cubes_sic[0].var_name = 'wrong' |
| 105 | + msg = ('Derivation of siextent failed due to missing variables ' |
| 106 | + 'sic and siconca.') |
| 107 | + with pytest.raises(RecipeError, match=msg): |
| 108 | + derived_var.calculate(cubes_sic) |
| 109 | + |
| 110 | + |
| 111 | +def test_siextent_required(): |
| 112 | + """Test function ``required``.""" |
| 113 | + derived_var = siextent.DerivedVariable() |
| 114 | + output = derived_var.required(None) |
| 115 | + assert output == [ |
| 116 | + {'short_name': 'sic', 'optional': 'true'}, |
| 117 | + {'short_name': 'siconca', 'optional': 'true'} |
| 118 | + ] |
0 commit comments