Skip to content

Commit ec9499e

Browse files
xco2 derivation (#913)
* xco2 derivation * Fix some spelling mistakes * Change xch4 to xco2 #2 Co-authored-by: Valeriu Predoi <valeriu.predoi@gmail.com>
1 parent ec6aa6f commit ec9499e

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Derivation of variable ``xco2``."""
2+
3+
from iris import Constraint
4+
5+
from ._baseclass import DerivedVariableBase
6+
from ._shared import column_average
7+
8+
9+
class DerivedVariable(DerivedVariableBase):
10+
"""Derivation of variable ``xco2``."""
11+
12+
@staticmethod
13+
def required(project):
14+
"""Declare the variables needed for derivation."""
15+
required = [
16+
{'short_name': 'co2'},
17+
{'short_name': 'hus'},
18+
{'short_name': 'zg'},
19+
{'short_name': 'ps'},
20+
]
21+
return required
22+
23+
@staticmethod
24+
def calculate(cubes):
25+
"""Calculate the column-averaged atmospheric CO2 [1e-6]."""
26+
co2_cube = cubes.extract_strict(
27+
Constraint(name='mole_fraction_of_carbon_dioxide_in_air'))
28+
print(co2_cube)
29+
hus_cube = cubes.extract_strict(Constraint(name='specific_humidity'))
30+
zg_cube = cubes.extract_strict(Constraint(name='geopotential_height'))
31+
ps_cube = cubes.extract_strict(Constraint(name='surface_air_pressure'))
32+
33+
# Column-averaged CO2
34+
xco2_cube = column_average(co2_cube, hus_cube, zg_cube, ps_cube)
35+
xco2_cube.convert_units('1')
36+
37+
return xco2_cube
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)