Skip to content

Commit

Permalink
Merge pull request #418 from davidhassell/bounds-units
Browse files Browse the repository at this point in the history
Fix for setting bounds with different but equivalent units
  • Loading branch information
davidhassell authored Jul 13, 2022
2 parents ca49807 + 4a23fc9 commit 8ce4c63
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
version 3.13.1
--------------

**2022-??-??**

* Fix bug when setting coordinate bounds with different units to their
parent coordinates
(https://github.com/NCAS-CMS/cf-python/issues/417)

----
version 3.13.0
--------------

Expand Down
4 changes: 2 additions & 2 deletions cf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
"""
__Conventions__ = "CF-1.9"
__date__ = "2022-06-23"
__version__ = "3.13.0"
__date__ = "2022-??-??"
__version__ = "3.13.1"

_requires = (
"numpy",
Expand Down
12 changes: 7 additions & 5 deletions cf/mixin/propertiesdatabounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -2625,11 +2625,13 @@ def set_bounds(self, bounds, copy=True):
units = bounds.Units
self_units = self.Units

if data is not None and units and not units.equivalent(self_units):
raise ValueError(
f"Can't set bounds: {bounds!r} units of {bounds.Units!r} are "
f"not equivalent to {self.Units!r}, the units of {self!r}"
)
if data is not None and units:
if not units.equivalent(self_units):
raise ValueError(
f"Can't set bounds: {bounds!r} units of {bounds.Units!r} "
f"are not equivalent to {self.Units!r}, the units of "
f"{self!r}"
)

bounds.Units = self_units

Expand Down
22 changes: 22 additions & 0 deletions cf/test/test_DimensionCoordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,28 @@ def test_DimensionCoordinate__setitem__(self):
self.assertTrue(d.data.equals(-e.data, verbose=3))
self.assertTrue(d.bounds.equals(self.dim.bounds, verbose=3))

def test_DimensionCoordinate_set_bounds(self):
c = cf.DimensionCoordinate()
data = cf.Data(
[1296000.0], units="seconds since 1900-01-01", calendar="360_day"
)
c.set_data(data)

bounds = cf.Bounds()
data = cf.Data(
[[0.0, 30]], units="days since 1900-01-01", calendar="360_day"
)
bounds.set_data(data)

c.set_bounds(bounds)

self.assertEqual(
c.bounds.Units,
cf.Units(units="seconds since 1900-01-01", calendar="360_day"),
)

self.assertTrue((c.bounds.data.array == [0, 2592000]).all())


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down

0 comments on commit 8ce4c63

Please sign in to comment.