cf.AuxiliaryCoordinate.convert_reference_time¶
-
AuxiliaryCoordinate.
convert_reference_time
(*args, **kwargs)[source]¶ Convert reference time data values to have new units.
Conversion is done by decoding the reference times to date-time objects and then re-encoding them for the new units.
Any conversions are possible, but this method is primarily for conversions which require a change in the date-times originally encoded. For example, use this method to reinterpret data values in units of “months” since a reference time to data values in “calendar months” since a reference time. This is often necessary when units of “calendar months” were intended but encoded as “months”, which have special definition. See the note and examples below for more details.
For conversions which do not require a change in the date-times implied by the data values, this method will be considerably slower than a simple reassignment of the units. For example, if the original units are
'days since 2000-12-1'
thenc.Units = cf.Units('days since 1901-1-1')
will give the same result and be considerably faster thanc.convert_reference_time(cf.Units('days since 1901-1-1'))
.Note
It is recommended that the units “year” and “month” be used with caution, as explained in the following excerpt from the CF conventions: “The Udunits package defines a year to be exactly 365.242198781 days (the interval between 2 successive passages of the sun through vernal equinox). It is not a calendar year. Udunits includes the following definitions for years: a common_year is 365 days, a leap_year is 366 days, a Julian_year is 365.25 days, and a Gregorian_year is 365.2425 days. For similar reasons the unit
month
, which is defined to be exactly year/12, should also be used with caution.- Parameters
- units:
Units
, optional The reference time units to convert to. By default the units days since the original reference time in the original calendar.
- Parameter example:
If the original units are
'months since 2000-1-1'
in the Gregorian calendar then the default units to convert to are'days since 2000-1-1'
in the Gregorian calendar.
- calendar_months:
bool
, optional If True then treat units of
'months'
as if they were calendar months (in whichever calendar is originally specified), rather than a 12th of the interval between 2 successive passages of the sun through vernal equinox (i.e. 365.242198781/12 days).- calendar_years:
bool
, optional If True then treat units of
'years'
as if they were calendar years (in whichever calendar is originally specified), rather than the interval between 2 successive passages of the sun through vernal equinox (i.e. 365.242198781 days).- inplace:
bool
, optional If True then do the operation in-place and return
None
.- i: deprecated at version 3.0.0
Use the inplace parameter instead.
- units:
- Returns
AuxiliaryCoordinate
orNone
The construct with converted reference time data values.
Examples:
>>> print(f.array) [1 2 3 4] >>> f.Units <Units: months since 2000-1-1> >>> print(f.datetime_array) [datetime.datetime(2000, 1, 31, 10, 29, 3, 831197) TODO datetime.datetime(2000, 3, 1, 20, 58, 7, 662441) datetime.datetime(2000, 4, 1, 7, 27, 11, 493645) datetime.datetime(2000, 5, 1, 17, 56, 15, 324889)] >>> f.convert_reference_time(calendar_months=True, inplace=True) >>> print(f.datetime_array) [datetime.datetime(2000, 2, 1, 0, 0) TODOx datetime.datetime(2000, 3, 1, 0, 0) datetime.datetime(2000, 4, 1, 0, 0) datetime.datetime(2000, 5, 1, 0, 0)] >>> print(f.array) [ 31. 60. 91. 121.] >>> f.Units <Units: days since 2000-1-1>