diff --git a/Changelog.rst b/Changelog.rst index 6e879831f3..2186cf4598 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -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) diff --git a/cf/field.py b/cf/field.py index 9220a9c0c2..61132fb6b3 100644 --- a/cf/field.py +++ b/cf/field.py @@ -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)) diff --git a/cf/read_write/um/umread.py b/cf/read_write/um/umread.py index 079c884e0f..051562945a 100644 --- a/cf/read_write/um/umread.py +++ b/cf/read_write/um/umread.py @@ -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 # -------------------------------------------------------- diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index fd61d0e9af..c94e60f563 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -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( diff --git a/cf/test/test_pp.py b/cf/test/test_pp.py index 0db5f9049e..ce75fe1b81 100644 --- a/cf/test/test_pp.py +++ b/cf/test/test_pp.py @@ -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)) @@ -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()) diff --git a/cf/weights.py b/cf/weights.py index 24a63aa8f7..63025febb7 100644 --- a/cf/weights.py +++ b/cf/weights.py @@ -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