Skip to content

Commit f468da3

Browse files
authored
Promote unknown units to dimensionless in aux factories (#3965)
* promote unknown to dimensionless units in aux factories * patch aux factories to promote unknown to dimensionless units for formula terms * add whatnew PR for entry
1 parent 8222f65 commit f468da3

File tree

13 files changed

+636
-5
lines changed

13 files changed

+636
-5
lines changed

docs/iris/src/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def autolog(message):
8282
if iris.__version__ == "dev":
8383
version = "dev"
8484
else:
85-
# major.feature(.minor)-dev -> major.minor
86-
version = ".".join(iris.__version__.split("-")[0].split(".")[:2])
85+
# major.minor.patch-dev -> major.minor.patch
86+
version = ".".join(iris.__version__.split("-")[0].split(".")[:3])
8787
# The full version, including alpha/beta/rc tags.
8888
release = iris.__version__
8989

docs/iris/src/whatsnew/3.0.1.rst

Lines changed: 520 additions & 0 deletions
Large diffs are not rendered by default.

docs/iris/src/whatsnew/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Iris versions.
1010
.. toctree::
1111
:maxdepth: 1
1212

13+
3.0.1.rst
1314
3.0.rst
1415
2.4.rst
1516
2.3.rst

lib/iris/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def callback(cube, field, filename):
106106

107107

108108
# Iris revision.
109-
__version__ = "3.0.0"
109+
__version__ = "3.0.1"
110110

111111
# Restrict the names imported when using "from iris import *"
112112
__all__ = [

lib/iris/aux_factory.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from abc import ABCMeta, abstractmethod
1212
import warnings
1313

14+
import cf_units
1415
import dask.array as da
1516
import numpy as np
1617

@@ -619,6 +620,10 @@ def _check_dependencies(delta, sigma, surface_air_pressure):
619620
warnings.warn(msg, UserWarning, stacklevel=2)
620621

621622
# Check units.
623+
if sigma is not None and sigma.units.is_unknown():
624+
# Be graceful, and promote unknown to dimensionless units.
625+
sigma.units = cf_units.Unit("1")
626+
622627
if sigma is not None and not sigma.units.is_dimensionless():
623628
raise ValueError("Invalid units: sigma must be dimensionless.")
624629
if (
@@ -863,6 +868,10 @@ def _check_dependencies(sigma, eta, depth, depth_c, nsigma, zlev):
863868
)
864869
raise ValueError(msg)
865870

871+
if sigma is not None and sigma.units.is_unknown():
872+
# Be graceful, and promote unknown to dimensionless units.
873+
sigma.units = cf_units.Unit("1")
874+
866875
if sigma is not None and not sigma.units.is_dimensionless():
867876
msg = (
868877
"Invalid units: sigma coordinate {!r} "
@@ -1127,6 +1136,10 @@ def _check_dependencies(sigma, eta, depth):
11271136
warnings.warn(msg, UserWarning, stacklevel=2)
11281137

11291138
# Check units.
1139+
if sigma is not None and sigma.units.is_unknown():
1140+
# Be graceful, and promote unknown to dimensionless units.
1141+
sigma.units = cf_units.Unit("1")
1142+
11301143
if sigma is not None and not sigma.units.is_dimensionless():
11311144
msg = (
11321145
"Invalid units: sigma coordinate {!r} "
@@ -1335,6 +1348,10 @@ def _check_dependencies(s, c, eta, depth, depth_c):
13351348
# Check units.
13361349
coords = ((s, "s"), (c, "c"))
13371350
for coord, term in coords:
1351+
if coord is not None and coord.units.is_unknown():
1352+
# Be graceful, and promote unknown to dimensionless units.
1353+
coord.units = cf_units.Unit("1")
1354+
13381355
if coord is not None and not coord.units.is_dimensionless():
13391356
msg = (
13401357
"Invalid units: {} coordinate {!r} "
@@ -1551,6 +1568,10 @@ def _check_dependencies(s, eta, depth, a, b, depth_c):
15511568
raise ValueError(msg)
15521569

15531570
# Check units.
1571+
if s is not None and s.units.is_unknown():
1572+
# Be graceful, and promote unknown to dimensionless units.
1573+
s.units = cf_units.Unit("1")
1574+
15541575
if s is not None and not s.units.is_dimensionless():
15551576
msg = (
15561577
"Invalid units: s coordinate {!r} "
@@ -1776,6 +1797,10 @@ def _check_dependencies(s, c, eta, depth, depth_c):
17761797
# Check units.
17771798
coords = ((s, "s"), (c, "c"))
17781799
for coord, term in coords:
1800+
if coord is not None and coord.units.is_unknown():
1801+
# Be graceful, and promote unknown to dimensionless units.
1802+
coord.units = cf_units.Unit("1")
1803+
17791804
if coord is not None and not coord.units.is_dimensionless():
17801805
msg = (
17811806
"Invalid units: {} coordinate {!r} "

lib/iris/fileformats/netcdf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@ def coord_from_term(term):
719719
warnings.warn(msg)
720720
coord_a = coord_from_term("a")
721721
if coord_a is not None:
722+
if coord_a.units.is_unknown():
723+
# Be graceful, and promote unknown to dimensionless units.
724+
coord_a.units = "1"
722725
delta = coord_a * coord_p0.points[0]
723726
delta.units = coord_a.units * coord_p0.units
724727
delta.rename("vertical pressure")

lib/iris/tests/unit/aux_factory/test_HybridPressureFactory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ def test_factory_metadata(self):
113113
self.assertIsNone(factory.coord_system)
114114
self.assertEqual(factory.attributes, {})
115115

116+
def test_promote_sigma_units_unknown_to_dimensionless(self):
117+
sigma = mock.Mock(units=cf_units.Unit("unknown"), nbounds=0)
118+
factory = HybridPressureFactory(
119+
delta=self.delta,
120+
sigma=sigma,
121+
surface_air_pressure=self.surface_air_pressure,
122+
)
123+
self.assertEqual("1", factory.dependencies["sigma"].units)
124+
116125

117126
class Test_dependencies(tests.IrisTest):
118127
def setUp(self):

lib/iris/tests/unit/aux_factory/test_OceanSFactory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ def test_depth_incompatible_units(self):
137137
with self.assertRaises(ValueError):
138138
OceanSFactory(**self.kwargs)
139139

140+
def test_promote_s_units_unknown_to_dimensionless(self):
141+
s = mock.Mock(units=Unit("unknown"), nbounds=0)
142+
self.kwargs["s"] = s
143+
factory = OceanSFactory(**self.kwargs)
144+
self.assertEqual("1", factory.dependencies["s"].units)
145+
140146

141147
class Test_dependencies(tests.IrisTest):
142148
def setUp(self):

lib/iris/tests/unit/aux_factory/test_OceanSg1Factory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ def test_depth_incompatible_units(self):
121121
with self.assertRaises(ValueError):
122122
OceanSg1Factory(**self.kwargs)
123123

124+
def test_promote_c_and_s_units_unknown_to_dimensionless(self):
125+
c = mock.Mock(units=Unit("unknown"), nbounds=0)
126+
s = mock.Mock(units=Unit("unknown"), nbounds=0)
127+
self.kwargs["c"] = c
128+
self.kwargs["s"] = s
129+
factory = OceanSg1Factory(**self.kwargs)
130+
self.assertEqual("1", factory.dependencies["c"].units)
131+
self.assertEqual("1", factory.dependencies["s"].units)
132+
124133

125134
class Test_dependencies(tests.IrisTest):
126135
def setUp(self):

lib/iris/tests/unit/aux_factory/test_OceanSg2Factory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ def test_depth_incompatible_units(self):
121121
with self.assertRaises(ValueError):
122122
OceanSg2Factory(**self.kwargs)
123123

124+
def test_promote_c_and_s_units_unknown_to_dimensionless(self):
125+
c = mock.Mock(units=Unit("unknown"), nbounds=0)
126+
s = mock.Mock(units=Unit("unknown"), nbounds=0)
127+
self.kwargs["c"] = c
128+
self.kwargs["s"] = s
129+
factory = OceanSg2Factory(**self.kwargs)
130+
self.assertEqual("1", factory.dependencies["c"].units)
131+
self.assertEqual("1", factory.dependencies["s"].units)
132+
124133

125134
class Test_dependencies(tests.IrisTest):
126135
def setUp(self):

0 commit comments

Comments
 (0)