Skip to content

Commit e367231

Browse files
authored
Merge pull request #778 from davidhassell/um-version
Include the UM version as a field property when reading UM files
2 parents 7a09fef + 65fbc8d commit e367231

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

Changelog.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
2-
version NEXTRELEASE
1+
version NEXTVERSION
32
-------------------
43

54
**2024-??-??**
65

6+
* Include the UM version as a field property when reading UM files
7+
(https://github.com/NCAS-CMS/cf-python/issues/777)
78
* Fix bug where `cf.example_fields` returned a `list`
89
of Fields rather than a `Fieldlist`
910
(https://github.com/NCAS-CMS/cf-python/issues/725)

cf/field.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10479,9 +10479,9 @@ def convolution_filter(
1047910479
new_bounds[0 : length - lower_offset, 1:] = old_bounds[
1048010480
lower_offset:length, 1:
1048110481
]
10482-
new_bounds[length - lower_offset : length, 1:] = (
10483-
old_bounds[length - 1, 1:]
10484-
)
10482+
new_bounds[
10483+
length - lower_offset : length, 1:
10484+
] = old_bounds[length - 1, 1:]
1048510485

1048610486
coord.set_bounds(self._Bounds(data=new_bounds))
1048710487

cf/read_write/um/umread.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,22 @@ def __init__(
10401040
cf_properties["stash_code"] = str(stash)
10411041
cf_properties["submodel"] = str(submodel)
10421042

1043+
# Convert the UM version to a string and provide it as a
1044+
# CF property. E.g. 405 -> '4.5', 606.3 -> '6.6.3', 1002
1045+
# -> '10.2'
1046+
#
1047+
# Note: We don't just do `divmod(self.um_version, 100)`
1048+
# because if self.um_version has a fractional part
1049+
# then it would likely get altered in the divmod
1050+
# calculation.
1051+
a, b = divmod(int(self.um_version), 100)
1052+
fraction = str(self.um_version).split(".")[-1]
1053+
um = f"{a}.{b}"
1054+
if fraction != "0" and fraction != str(self.um_version):
1055+
um += f".{fraction}"
1056+
1057+
cf_properties["um_version"] = um
1058+
10431059
# --------------------------------------------------------
10441060
# Set the data and extra data
10451061
# --------------------------------------------------------

cf/test/test_Data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,9 @@ def test_Data_BINARY_AND_UNARY_OPERATORS(self):
23892389
except Exception:
23902390
pass
23912391
else:
2392-
self.assertTrue((x**d).all(), "{}**{}".format(x, repr(d)))
2392+
self.assertTrue(
2393+
(x**d).all(), "{}**{}".format(x, repr(d))
2394+
)
23932395

23942396
self.assertTrue(
23952397
d.__truediv__(x).equals(

cf/test/test_pp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_PP_read_um(self):
5757
g = cf.read(self.ppextradata, um={"fmt": "pp"})[0]
5858
self.assertTrue(f.equals(g))
5959

60-
for vn in (4.5, 405, "4.5", None):
60+
for vn in (4.5, 405, "4.5"):
6161
g = cf.read(self.ppextradata, um={"fmt": "pp", "version": vn})[0]
6262
self.assertTrue(f.equals(g))
6363

@@ -138,6 +138,13 @@ def test_PP_extra_data(self):
138138
self.assertTrue(f.dimension_coordinate("time", default=False))
139139
self.assertTrue(f.auxiliary_coordinate("longitude", default=False))
140140

141+
def test_PP_um_version(self):
142+
f = cf.read(self.ppfile)[0]
143+
self.assertEqual(f.get_property("um_version"), "11.0")
144+
145+
f = cf.read(self.ppfile, um={"version": "6.6.3"})[0]
146+
self.assertEqual(f.get_property("um_version"), "6.6.3")
147+
141148

142149
if __name__ == "__main__":
143150
print("Run date:", datetime.datetime.now())

cf/weights.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,9 +1461,9 @@ def linear(
14611461
else:
14621462
# Bounds exist
14631463
if methods:
1464-
weights[(da_key,)] = (
1465-
f"linear {f.constructs.domain_axis_identity(da_key)}"
1466-
)
1464+
weights[
1465+
(da_key,)
1466+
] = f"linear {f.constructs.domain_axis_identity(da_key)}"
14671467
else:
14681468
weights[(da_key,)] = dim.cellsize
14691469

0 commit comments

Comments
 (0)