Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why, for the fraction component, you didn't just accept the raw numeric and then seeing this I realise it is to do with the floating point conversion:

>>> divmod(606.3, 100)
(6.0, 6.2999999999999545)

Might be worth a one-line comment, since I was curious enough to look into that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea: b0119f5

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