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

xco2 derivation #913

Merged
merged 4 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions esmvalcore/preprocessor/_derive/xco2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Derivation of variable ``xco2``."""

from iris import Constraint

from ._baseclass import DerivedVariableBase
from ._shared import column_average


class DerivedVariable(DerivedVariableBase):
"""Derivation of variable ``xco2``."""

@staticmethod
def required(project):
"""Declare the variables needed for derivation."""
required = [
{'short_name': 'co2'},
{'short_name': 'hus'},
{'short_name': 'zg'},
{'short_name': 'ps'},
]
return required

@staticmethod
def calculate(cubes):
"""Calculate the column-averaged atmospheric CO2 [1e-6]."""
co2_cube = cubes.extract_strict(
Constraint(name='mole_fraction_of_carbon_dioxide_in_air'))
print(co2_cube)
hus_cube = cubes.extract_strict(Constraint(name='specific_humidity'))
zg_cube = cubes.extract_strict(Constraint(name='geopotential_height'))
ps_cube = cubes.extract_strict(Constraint(name='surface_air_pressure'))

# Column-averaged CO2
xco2_cube = column_average(co2_cube, hus_cube, zg_cube, ps_cube)
xco2_cube.convert_units('1')

return xco2_cube
58 changes: 58 additions & 0 deletions tests/unit/preprocessor/_derive/test_xco2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Test derivation of ``xco2``."""
import iris
import numpy as np
import pytest

import esmvalcore.preprocessor._derive.xco2 as xco2

from .test_shared import get_cube


@pytest.fixture
def cubes():
"""Input cubes for derivation of ``xco2``."""
co2_cube = get_cube([[[[1.0]], [[2.0]]]], air_pressure_coord=True,
standard_name='mole_fraction_of_carbon_dioxide_in_air',
var_name='co2', units='1e-6')
hus_cube = get_cube([[[[0.2]], [[0.2]]]], air_pressure_coord=True,
standard_name='specific_humidity', var_name='hus',
units='%')
zg_cube = get_cube([[[100.0]]], air_pressure_coord=False,
standard_name='geopotential_height', var_name='zg',

units='m')
ps_cube = get_cube([[[100000.0]]], air_pressure_coord=False,
standard_name='surface_air_pressure', var_name='ps',
units='Pa')
return iris.cube.CubeList([co2_cube, hus_cube, zg_cube, ps_cube])


def test_xco2_calculate(cubes):
"""Test function ``calculate``."""
derived_var = xco2.DerivedVariable()
out_cube = derived_var.calculate(cubes)
assert out_cube.shape == (1, 1, 1)
assert out_cube.units == '1'
assert out_cube.coords('time')
assert out_cube.coords('air_pressure')
assert out_cube.coords('latitude')
assert out_cube.coords('longitude')
np.testing.assert_allclose(out_cube.data, [[[1.85e-6]]])
np.testing.assert_allclose(out_cube.coord('time').points, [0.0])
np.testing.assert_allclose(out_cube.coord('air_pressure').points, 85000.0)
np.testing.assert_allclose(out_cube.coord('air_pressure').bounds,
[[80000.0, 90000.0]])
np.testing.assert_allclose(out_cube.coord('latitude').points, [45.0])
np.testing.assert_allclose(out_cube.coord('longitude').points, [10.0])


def test_xco2_required():
"""Test function ``required``."""
derived_var = xco2.DerivedVariable()
output = derived_var.required(None)
assert output == [
{'short_name': 'co2'},
{'short_name': 'hus'},
{'short_name': 'zg'},
{'short_name': 'ps'},
]