Skip to content

Commit

Permalink
Merge pull request #778 from davidhassell/um-version
Browse files Browse the repository at this point in the history
Include the UM version as a field property when reading UM files
  • Loading branch information
davidhassell authored Jun 11, 2024
2 parents 7a09fef + 65fbc8d commit e367231
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

version NEXTRELEASE
version NEXTVERSION
-------------------

**2024-??-??**

* Include the UM version as a field property when reading UM files
(https://github.com/NCAS-CMS/cf-python/issues/777)
* Fix bug where `cf.example_fields` returned a `list`
of Fields rather than a `Fieldlist`
(https://github.com/NCAS-CMS/cf-python/issues/725)
Expand Down
6 changes: 3 additions & 3 deletions cf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -10479,9 +10479,9 @@ def convolution_filter(
new_bounds[0 : length - lower_offset, 1:] = old_bounds[
lower_offset:length, 1:
]
new_bounds[length - lower_offset : length, 1:] = (
old_bounds[length - 1, 1:]
)
new_bounds[
length - lower_offset : length, 1:
] = old_bounds[length - 1, 1:]

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

Expand Down
16 changes: 16 additions & 0 deletions cf/read_write/um/umread.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,22 @@ def __init__(
cf_properties["stash_code"] = str(stash)
cf_properties["submodel"] = str(submodel)

# Convert the UM version to a string and provide it as a
# CF property. E.g. 405 -> '4.5', 606.3 -> '6.6.3', 1002
# -> '10.2'
#
# Note: We don't just do `divmod(self.um_version, 100)`
# because if self.um_version has a fractional part
# then it would likely get altered in the divmod
# calculation.
a, b = divmod(int(self.um_version), 100)
fraction = str(self.um_version).split(".")[-1]
um = f"{a}.{b}"
if fraction != "0" and fraction != str(self.um_version):
um += f".{fraction}"

cf_properties["um_version"] = um

# --------------------------------------------------------
# Set the data and extra data
# --------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,9 @@ def test_Data_BINARY_AND_UNARY_OPERATORS(self):
except Exception:
pass
else:
self.assertTrue((x**d).all(), "{}**{}".format(x, repr(d)))
self.assertTrue(
(x**d).all(), "{}**{}".format(x, repr(d))
)

self.assertTrue(
d.__truediv__(x).equals(
Expand Down
9 changes: 8 additions & 1 deletion cf/test/test_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_PP_read_um(self):
g = cf.read(self.ppextradata, um={"fmt": "pp"})[0]
self.assertTrue(f.equals(g))

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

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

def test_PP_um_version(self):
f = cf.read(self.ppfile)[0]
self.assertEqual(f.get_property("um_version"), "11.0")

f = cf.read(self.ppfile, um={"version": "6.6.3"})[0]
self.assertEqual(f.get_property("um_version"), "6.6.3")


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down
6 changes: 3 additions & 3 deletions cf/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,9 +1461,9 @@ def linear(
else:
# Bounds exist
if methods:
weights[(da_key,)] = (
f"linear {f.constructs.domain_axis_identity(da_key)}"
)
weights[
(da_key,)
] = f"linear {f.constructs.domain_axis_identity(da_key)}"
else:
weights[(da_key,)] = dim.cellsize

Expand Down

0 comments on commit e367231

Please sign in to comment.