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

Remove in-house long time interval checking. #279

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 0 additions & 39 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,36 +964,6 @@ def is_time_reference(self):
"""
return self.calendar is not None

def is_long_time_interval(self):
"""
Defines whether this unit describes a time unit with a long time
interval ("months" or "years"). These long time intervals *are*
supported by `UDUNITS2` but are not supported by `cftime`. This
discrepancy means we cannot run self.num2date() on a time unit with
a long time interval.

Returns:
Boolean.

For example:

>>> import cf_units
>>> u = cf_units.Unit('days since epoch')
>>> u.is_long_time_interval()
False
>>> u = cf_units.Unit('years since epoch')
>>> u.is_long_time_interval()
True

"""
result = False
long_time_intervals = ["year", "month"]
if self.is_time_reference():
result = any(
interval in self.origin for interval in long_time_intervals
)
return result

def title(self, value):
"""
Return the unit value as a title string.
Expand Down Expand Up @@ -1929,15 +1899,6 @@ def cftime_unit(self):
if self.calendar is None:
raise ValueError("Unit has undefined calendar")

# `cftime` cannot parse long time intervals ("months" or "years").
if self.is_long_time_interval():
interval = self.origin.split(" ")[0]
emsg = (
'Time units with interval of "months", "years" '
'(or singular of these) cannot be processed, got "{!s}".'
)
raise ValueError(emsg.format(interval))

#
# ensure to strip out non-parsable 'UTC' postfix, which
# is generated by UDUNITS-2 formatted output
Expand Down
9 changes: 0 additions & 9 deletions cf_units/tests/integration/test_date2num.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import datetime

import numpy as np
import pytest

from cf_units import date2num

Expand Down Expand Up @@ -60,11 +59,3 @@ def test_discard_microsecond(self):
res = date2num(date, self.unit, self.calendar)

assert exp == res

def test_long_time_interval(self):
# This test should fail with an error that we need to catch properly.
unit = "years since 1970-01-01"
date = datetime.datetime(1970, 1, 1, 0, 0, 5)
exp_emsg = 'interval of "months", "years" .* got "years".'
with pytest.raises(ValueError, match=exp_emsg):
date2num(date, unit, self.calendar)
30 changes: 0 additions & 30 deletions cf_units/tests/unit/unit/test_Unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ def test_no_change(self):
# the same object.
assert result is not u

def test_long_time_interval(self):
u = Unit("months since 1970-01-01", calendar="standard")
with pytest.raises(ValueError, match="cannot be processed"):
u.change_calendar("proleptic_gregorian")

def test_wrong_calendar(self):
u = Unit("days since 1900-01-01", calendar="360_day")
with pytest.raises(
Expand Down Expand Up @@ -273,31 +268,6 @@ def test_type_conversion(self):
np.testing.assert_array_almost_equal(self.rads_array, result)


class Test_is_long_time_interval:
def test_short_time_interval(self):
# A short time interval is a time interval from seconds to days.
unit = Unit("seconds since epoch")
result = unit.is_long_time_interval()
assert not result

def test_long_time_interval(self):
# A long time interval is a time interval of months or years.
unit = Unit("months since epoch")
result = unit.is_long_time_interval()
assert result

def test_calendar(self):
# Check that a different calendar does not affect the result.
unit = Unit("months since epoch", calendar=cf_units.CALENDAR_360_DAY)
result = unit.is_long_time_interval()
assert result

def test_not_time_unit(self):
unit = Unit("K")
result = unit.is_long_time_interval()
assert not result


class Test_format:
def test_invalid_ut_unit(self):
# https://github.com/SciTools/cf-units/issues/133 flagged up that
Expand Down