Skip to content

Commit

Permalink
Merge pull request #300 from davidhassell/cell-methods
Browse files Browse the repository at this point in the history
Fix bug that returned incorrect results when an invalid identifer is provided to `cf.Field.cell_methods`
  • Loading branch information
davidhassell authored Dec 5, 2024
2 parents 8d3b7d2 + 356e03d commit e0436b0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Version NEXTVERSION

**2024-??-??**

* Fix bug that returned incorrect results when an invalid identifer is
provided to `cf.Field.cell_methods`
(https://github.com/NCAS-CMS/cfdm/issues/299)
* Upgrades to allow cfdm to work with Python 3.12
(https://github.com/NCAS-CMS/cfdm/issues/302)
* Extension to the HDF5 chunks API
Expand Down
19 changes: 19 additions & 0 deletions cfdm/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,18 @@ def cell_methods(self, *identities, **filter_kwargs):
**Examples**
>>> f = {{package}}.example_field(1)
>>> print(f.cell_methods())
Constructs:
{'cellmethod0': <CellMethod: domainaxis1: domainaxis2: mean where land (interval: 0.1 degrees)>,
'cellmethod1': <CellMethod: domainaxis3: maximum>}
>>> print(f.cell_methods('time'))
Constructs:
{'cellmethod1': <CellMethod: domainaxis3: maximum>}
>>> print(f.cell_methods('bad identifier'))
Constructs:
{}
"""
cached = filter_kwargs.get("cached")
if cached is not None:
Expand Down Expand Up @@ -704,6 +716,13 @@ def cell_methods(self, *identities, **filter_kwargs):
keys.add(cm_key)

identities = ()
if not keys:
# Specify a key of None to ensure that no cell methods
# are selected. (If keys is an empty set then all cell
# methods are selected, which is not what we want,
# here.)
keys = (None,)

filter_kwargs = {
"filter_by_key": keys,
"todict": filter_kwargs.pop("todict", False),
Expand Down
4 changes: 4 additions & 0 deletions cfdm/test/test_Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ def test_Field_CONSTRUCTS(self):
for key, value in constructs.items():
self.assertIsInstance(value, cfdm.CellMethod)

# Check that no cell methods are returned when a bad
# identifier is provided
self.assertFalse(f.cell_methods("bad identifier"))

constructs = f.coordinate_references()
n = 2
self.assertEqual(len(constructs), n)
Expand Down

0 comments on commit e0436b0

Please sign in to comment.