Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# and so this Gregorian date can be compared to the proleptic
# Gregorian date d2.
d3 = num2date(0, 'days since 3000-01-01', 'standard')
assert d1 < d2
assert d2 < d3
# check all comparisons
assert d1 != d2
assert d1 <= d2
assert d2 > d1
assert d2 >= d1
# check datetime hash
d1 = datetimex(1995, 1, 1)
d2 = datetime(1995, 1, 1)
d3 = datetimex(2001, 2, 30)
assert hash(d1) == hash(d1)
assert hash(d1) == hash(d2)
assert hash(d1) != hash(d3)
assert hash(d3) == hash(d3)
# check datetime immutability
# using assertRaises as a context manager
# only works with python >= 2.7 (issue #497).
immutability_tests = {"year": 1999,
"month": 6,
"day": 5,
"hour": 10,
"minute": 33,
"second": 45,
"dayofwk": 1,
"dayofyr": 52,
self.assertEqual(t1+1, t2)
# list
t1, t2 = date2num([datetime(1582, 10, 4), datetime(1582, 10, 15)], units, calendar='gregorian')
self.assertEqual(t1+1, t2)
t1, t2 = date2num([datetime(1582, 10, 4), datetime(1582, 10, 15)], units, calendar='standard')
self.assertEqual(t1+1, t2)
# this should fail: days missing in Gregorian calendar
try:
t1, t2, t3 = date2num([datetime(1582, 10, 4), datetime(1582, 10, 10), datetime(1582, 10, 15)], units, calendar='standard')
except ValueError:
pass
# test fix for issue #596 - julian day calculations wrong for negative years,
# caused incorrect rountrip num2date(date2num(date)) roundtrip for dates with year
# < 0.
u = utime("seconds since 1-1-1",calendar='julian')
date1 = datetimex(-1, 1, 1)
date2 = u.num2date(u.date2num(date1))
assert (date2.year == date1.year)
assert (date2.month == date1.month)
assert (date2.day == date1.day)
assert (date2.hour == date1.hour)
assert (date2.minute == date1.minute)
assert (date2.second == date1.second)
assert_almost_equal(JulianDayFromDate(date1), 1721057.5)
# issue 596 - negative years fail in utime.num2date
u = utime("seconds since 1-1-1", "proleptic_gregorian")
d = u.num2date(u.date2num(datetimex(-1, 1, 1)))
assert (d.year == -1)
assert (d.month == 1)
assert (d.day == 1)
assert (d.hour == 0)
# test fix for issue #659 (proper treatment of negative time values).
# The date d3 is well after the Julian/Gregorian transition
# and so this Gregorian date can be compared to the proleptic
# Gregorian date d2.
d3 = num2date(0, 'days since 3000-01-01', 'standard')
assert d1 < d2
assert d2 < d3
# check all comparisons
assert d1 != d2
assert d1 <= d2
assert d2 > d1
assert d2 >= d1
# check datetime hash
d1 = datetimex(1995, 1, 1)
d2 = datetime(1995, 1, 1)
d3 = datetimex(2001, 2, 30)
assert hash(d1) == hash(d1)
assert hash(d1) == hash(d2)
assert hash(d1) != hash(d3)
assert hash(d3) == hash(d3)
# check datetime immutability
# using assertRaises as a context manager
# only works with python >= 2.7 (issue #497).
immutability_tests = {"year": 1999,
"month": 6,
"day": 5,
"hour": 10,
"minute": 33,
"second": 45,
and fp_coord is not None
and fp_coord.has_bounds()
and clim_season_coord is not None
and "clim_season" in cube.cell_methods[-1].coord_names
and clim_season_coord.points[0] == "djf"
):
# Climatological time mean - spanning years - djf.
pp.lbtim.ia = 0
pp.lbtim.ib = 3
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
if pp.t1.month == 12:
pp.t1 = cftime.datetime(pp.t1.year)
else:
pp.t1 = cftime.datetime(pp.t1.year - 1, 12, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 3, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
elif (
time_coord is not None
and time_coord.has_bounds()
and lower_bound_yr != upper_bound_yr
and fp_coord is not None
and fp_coord.has_bounds()
shape = value.shape
value = value.reshape(-1)
first_value = value[0]
else:
# Don't do anything with numeric types.
if munits.ConversionInterface.is_numlike(value):
return value
first_value = value
if not isinstance(first_value, (CalendarDateTime, cftime.datetime)):
raise ValueError('The values must be numbers or instances of '
'"nc_time_axis.CalendarDateTime" or '
'"cftime.datetime".')
if isinstance(first_value, CalendarDateTime):
if not isinstance(first_value.datetime, cftime.datetime):
raise ValueError('The datetime attribute of the '
'CalendarDateTime object must be of type '
'`cftime.datetime`.')
ut = cftime.utime(cls.standard_unit, calendar=first_value.calendar)
if isinstance(value, (CalendarDateTime, cftime.datetime)):
value = [value]
if isinstance(first_value, CalendarDateTime):
result = ut.date2num([v.datetime for v in value])
else:
result = ut.date2num(value)
if shape is not None:
result = result.reshape(shape)
time_coord is not None
and time_coord.has_bounds()
and lower_bound_yr != upper_bound_yr
and fp_coord is not None
and fp_coord.has_bounds()
and clim_season_coord is not None
and "clim_season" in cube.cell_methods[-1].coord_names
and clim_season_coord.points[0] == "mam"
):
# Climatological time mean - spanning years - mam.
pp.lbtim.ia = 0
pp.lbtim.ib = 3
# TODO: wut?
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = cftime.datetime(pp.t1.year, 3, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 6, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
elif (
time_coord is not None
and time_coord.has_bounds()
and lower_bound_yr != upper_bound_yr
and fp_coord is not None
def axisinfo(unit, axis):
"""
Returns the :class:`~matplotlib.units.AxisInfo` for *unit*.
*unit* is a tzinfo instance or None.
The *axis* argument is required but not used.
"""
calendar, date_unit, date_type = unit
majloc = NetCDFTimeDateLocator(4, calendar=calendar,
date_unit=date_unit)
majfmt = NetCDFTimeDateFormatter(majloc, calendar=calendar,
time_units=date_unit)
if date_type is CalendarDateTime:
datemin = CalendarDateTime(cftime.datetime(2000, 1, 1),
calendar=calendar)
datemax = CalendarDateTime(cftime.datetime(2010, 1, 1),
calendar=calendar)
else:
datemin = date_type(2000, 1, 1)
datemax = date_type(2010, 1, 1)
return munits.AxisInfo(majloc=majloc, majfmt=majfmt, label='',
default_limits=(datemin, datemax))
"""
Returns the :class:`~matplotlib.units.AxisInfo` for *unit*.
*unit* is a tzinfo instance or None.
The *axis* argument is required but not used.
"""
calendar, date_unit, date_type = unit
majloc = NetCDFTimeDateLocator(4, calendar=calendar,
date_unit=date_unit)
majfmt = NetCDFTimeDateFormatter(majloc, calendar=calendar,
time_units=date_unit)
if date_type is CalendarDateTime:
datemin = CalendarDateTime(cftime.datetime(2000, 1, 1),
calendar=calendar)
datemax = CalendarDateTime(cftime.datetime(2010, 1, 1),
calendar=calendar)
else:
datemin = date_type(2000, 1, 1)
datemax = date_type(2010, 1, 1)
return munits.AxisInfo(majloc=majloc, majfmt=majfmt, label='',
default_limits=(datemin, datemax))
and time_coord.has_bounds()
and lower_bound_yr != upper_bound_yr
and fp_coord is not None
and fp_coord.has_bounds()
and clim_season_coord is not None
and "clim_season" in cube.cell_methods[-1].coord_names
and clim_season_coord.points[0] == "mam"
):
# Climatological time mean - spanning years - mam.
pp.lbtim.ia = 0
pp.lbtim.ib = 3
# TODO: wut?
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = cftime.datetime(pp.t1.year, 3, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 6, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
elif (
time_coord is not None
and time_coord.has_bounds()
and lower_bound_yr != upper_bound_yr
and fp_coord is not None
and fp_coord.has_bounds()
primary_elements,
vector_element_arrays_and_dims,
) = optimal_array_structure(ordering_definitions, element_definitions)
# Replace time tuples in the result with real datetime-like values.
# N.B. so we *don't* do this on the whole (expanded) input arrays.
for name in ("t1", "t2"):
if name in vector_element_arrays_and_dims:
arr, dims = vector_element_arrays_and_dims[name]
arr_shape = arr.shape[:-1]
extra_length = arr.shape[-1]
# Flatten out the array apart from the last dimension,
# convert to cftime objects, then reshape back.
arr = np.array(
[
cftime.datetime(*args)
for args in arr.reshape(-1, extra_length)
]
).reshape(arr_shape)
vector_element_arrays_and_dims[name] = (arr, dims)
# Write the private cache values, exposed as public properties.
self._vector_dims_shape = dims_shape
self._primary_dimension_elements = primary_elements
self._element_arrays_and_dims = vector_element_arrays_and_dims
# Do all this only once.
self._structure_calculated = True