Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure when creating 'T: mean within days time: minimum over days'' collapse #810

Merged
merged 4 commits into from
Sep 10, 2024
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
2 changes: 2 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ version NEXTVERSION
(https://github.com/NCAS-CMS/cf-python/issues/784)
* Include the UM version as a field property when reading UM files
(https://github.com/NCAS-CMS/cf-python/issues/777)
* Fix bug that caused climatological time collapses within/over days
to fail (https://github.com/NCAS-CMS/cf-python/issues/809)
* New keyword parameter to `cf.Field.derivative`:
``ignore_coordinate_units``
(https://github.com/NCAS-CMS/cf-python/issues/807)
Expand Down
1 change: 0 additions & 1 deletion cf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -8392,7 +8392,6 @@ def _group_weights(weights, iaxis, index):
within_days.Units.istime
and TimeDuration(24, "hours") % within_days
):
# % Data(1, 'day'): # % within_days:
raise ValueError(
f"Can't collapse: within_days={within_days!r} "
"is not an exact factor of 1 day"
Expand Down
13 changes: 12 additions & 1 deletion cf/test/test_TimeDuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,18 @@ def test_TimeDuration(self):
self.assertEqual(
cf.TimeDuration(36, "calendar_months") // 8.25, cf.M(4.0)
)
self.assertEqual(cf.TimeDuration(36, "calendar_months") % 10, cf.M(6))

for y in (
10,
cf.Data(10, "calendar_months"),
cf.TimeDuration(10, "calendar_months"),
):
self.assertEqual(
cf.TimeDuration(36, "calendar_months") % y, cf.M(6)
)

for y in (10, cf.Data(10, "hours"), cf.h(10), cf.D(5 / 12), cf.m(600)):
self.assertEqual(cf.h(36) % y, cf.h(6))

self.assertEqual(
cf.TimeDuration(24, "hours") + cf.TimeDuration(0.5, "days"),
Expand Down
17 changes: 17 additions & 0 deletions cf/test/test_collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ def test_Field_collapse_CLIMATOLOGICAL_TIME(self):
print(g.constructs)
self.assertEqual(list(g.shape), expected_shape)

def test_Field_collapse_CLIMATOLOGICAL_TIME_within_days(self):
verbose = False

f = cf.example_field(5)

g = f.collapse(
"T: mean within days time: minimum over days", within_days=cf.h(12)
)
expected_shape = list(f.shape)
expected_shape[0] = 2

if verbose:
print("\n", f)
print(g)
print(g.constructs)
self.assertEqual(list(g.shape), expected_shape)

def test_Field_collapse(self):
verbose = False

Expand Down
12 changes: 5 additions & 7 deletions cf/timeduration.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ def _apply_binary_comparison(self, other, op):
op: `str`, the binary comparison operator to apply.

"""

if isinstance(other, (self.__class__, int, float)):
return bool(self._binary_operation(other, op))

Expand Down Expand Up @@ -737,15 +736,14 @@ def _apply_binary_arithmetic(
do not skip.

"""
check_simple_types = [int, float]
if may_be_datetime:
check_simple_types.append(self.__class__)

if isinstance(other, tuple(check_simple_types)):
if isinstance(other, (int, float, self.__class__)):
# 'other' is a number or another TimeDuration object => we
# can use the usual binary operation method.
return self._binary_operation(other, op, aug_assignment)

if may_be_datetime and hasattr(other, "timetuple"):
# other is a date-time object
# 'other' is a date-time object => we must use the special
# datetime arithmetic operation.
try:
return self._datetime_arithmetic(other, getattr(operator, op))
except TypeError:
Expand Down