|
| 1 | +"""Test derivation of ``xco2``.""" |
| 2 | +import iris |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +import esmvalcore.preprocessor._derive.xco2 as xco2 |
| 7 | + |
| 8 | +from .test_shared import get_cube |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def cubes(): |
| 13 | + """Input cubes for derivation of ``xco2``.""" |
| 14 | + co2_cube = get_cube([[[[1.0]], [[2.0]]]], air_pressure_coord=True, |
| 15 | + standard_name='mole_fraction_of_carbon_dioxide_in_air', |
| 16 | + var_name='co2', units='1e-6') |
| 17 | + hus_cube = get_cube([[[[0.2]], [[0.2]]]], air_pressure_coord=True, |
| 18 | + standard_name='specific_humidity', var_name='hus', |
| 19 | + units='%') |
| 20 | + zg_cube = get_cube([[[100.0]]], air_pressure_coord=False, |
| 21 | + standard_name='geopotential_height', var_name='zg', |
| 22 | + |
| 23 | + units='m') |
| 24 | + ps_cube = get_cube([[[100000.0]]], air_pressure_coord=False, |
| 25 | + standard_name='surface_air_pressure', var_name='ps', |
| 26 | + units='Pa') |
| 27 | + return iris.cube.CubeList([co2_cube, hus_cube, zg_cube, ps_cube]) |
| 28 | + |
| 29 | + |
| 30 | +def test_xco2_calculate(cubes): |
| 31 | + """Test function ``calculate``.""" |
| 32 | + derived_var = xco2.DerivedVariable() |
| 33 | + out_cube = derived_var.calculate(cubes) |
| 34 | + assert out_cube.shape == (1, 1, 1) |
| 35 | + assert out_cube.units == '1' |
| 36 | + assert out_cube.coords('time') |
| 37 | + assert out_cube.coords('air_pressure') |
| 38 | + assert out_cube.coords('latitude') |
| 39 | + assert out_cube.coords('longitude') |
| 40 | + np.testing.assert_allclose(out_cube.data, [[[1.85e-6]]]) |
| 41 | + np.testing.assert_allclose(out_cube.coord('time').points, [0.0]) |
| 42 | + np.testing.assert_allclose(out_cube.coord('air_pressure').points, 85000.0) |
| 43 | + np.testing.assert_allclose(out_cube.coord('air_pressure').bounds, |
| 44 | + [[80000.0, 90000.0]]) |
| 45 | + np.testing.assert_allclose(out_cube.coord('latitude').points, [45.0]) |
| 46 | + np.testing.assert_allclose(out_cube.coord('longitude').points, [10.0]) |
| 47 | + |
| 48 | + |
| 49 | +def test_xco2_required(): |
| 50 | + """Test function ``required``.""" |
| 51 | + derived_var = xco2.DerivedVariable() |
| 52 | + output = derived_var.required(None) |
| 53 | + assert output == [ |
| 54 | + {'short_name': 'co2'}, |
| 55 | + {'short_name': 'hus'}, |
| 56 | + {'short_name': 'zg'}, |
| 57 | + {'short_name': 'ps'}, |
| 58 | + ] |
0 commit comments