Skip to content

Commit

Permalink
Added special unit conversion m -> DU for total column ozone (toz) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
schlunma authored Jan 23, 2024
1 parent ce3ddb8 commit 2751720
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/recipe/preprocessor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,8 @@ Currently, the following special conversions are supported:

* ``precipitation_flux`` (``kg m-2 s-1``) --
``lwe_precipitation_rate`` (``mm day-1``)
* ``equivalent_thickness_at_stp_of_atmosphere_ozone_content`` (``m``) --
``equivalent_thickness_at_stp_of_atmosphere_ozone_content`` (``DU``)

.. hint::
Names in the list correspond to ``standard_names`` of the input data.
Expand Down
14 changes: 10 additions & 4 deletions esmvalcore/preprocessor/_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"""
import logging

from cf_units import Unit
import iris
import numpy as np
from cf_units import Unit

logger = logging.getLogger(__name__)

Expand All @@ -21,6 +21,10 @@
('precipitation_flux', 'kg m-2 s-1'),
('lwe_precipitation_rate', 'mm s-1'),
],
[
('equivalent_thickness_at_stp_of_atmosphere_ozone_content', 'm'),
('equivalent_thickness_at_stp_of_atmosphere_ozone_content', '1e5 DU'),
],
]


Expand All @@ -41,7 +45,7 @@ def _try_special_conversions(cube, units):
if (cube.standard_name == std_name and
cube.units.is_convertible(special_units)):
for (target_std_name, target_units) in special_case:
if target_std_name == std_name:
if target_units == special_units:
continue

# Step 2: find suitable target name and units
Expand Down Expand Up @@ -79,8 +83,10 @@ def convert_units(cube, units):
Currently, the following special conversions are supported:
* ``precipitation_flux`` (``kg m-2 s-1``) --
``lwe_precipitation_rate`` (``mm day-1``)
* ``precipitation_flux`` (``kg m-2 s-1``) --
``lwe_precipitation_rate`` (``mm day-1``)
* ``equivalent_thickness_at_stp_of_atmosphere_ozone_content`` (``m``) --
``equivalent_thickness_at_stp_of_atmosphere_ozone_content`` (``DU``)
Names in the list correspond to ``standard_names`` of the input data.
Conversions are allowed from each quantity to any other quantity given in a
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/preprocessor/_units/test_convert_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ def test_convert_compatible_units(self):
self.assertEqual(result.units, expected_units)
self.assert_array_equal(result.data, expected_data)

def test_convert_ozone_content_m_to_du(self):
"""Test special conversion of ozone_content."""
self.arr.standard_name = (
'equivalent_thickness_at_stp_of_atmosphere_ozone_content'
)
self.arr.units = 'm'
result = convert_units(self.arr, 'DU')
self.assertEqual(
result.standard_name,
'equivalent_thickness_at_stp_of_atmosphere_ozone_content',
)
self.assertEqual(result.units, 'DU')
np.testing.assert_allclose(
result.data,
[[0.0, 1e5], [2e5, 3e5]],
)

def test_convert_ozone_content_du_to_m(self):
"""Test special conversion of ozone_content."""
self.arr.standard_name = (
'equivalent_thickness_at_stp_of_atmosphere_ozone_content'
)
self.arr.units = 'DU'
result = convert_units(self.arr, 'mm')
self.assertEqual(
result.standard_name,
'equivalent_thickness_at_stp_of_atmosphere_ozone_content',
)
self.assertEqual(result.units, 'mm')
np.testing.assert_allclose(
result.data,
[[0.0, 1e-2], [2e-2, 3e-2]],
)

def test_convert_precipitation_flux(self):
"""Test special conversion of precipitation_flux."""
self.arr.standard_name = 'precipitation_flux'
Expand Down

0 comments on commit 2751720

Please sign in to comment.