unifhy.TimeDomain.from_datetime_sequence
- classmethod TimeDomain.from_datetime_sequence(datetimes, units=None, calendar=None)
Instantiate a
TimeDomain
from a sequence of datetime objects.- Parameters
- datetimes: one-dimensional array-like object
The array of datetime objects defining the temporal dimension. May be any type that can be cast to a
numpy.ndarray
. Must contain datetime objects (i.e.datetime.datetime
,cftime.datetime
,numpy.datetime64
).Note: the first datetime of the array is the beginning of the first timestep, and the last datetime is the end of the last timestep (not its start).
Parameter example:
datetimes=[datetime.datetime(1970, 1, 1, 0, 0, 1), datetime.datetime(1970, 1, 1, 0, 0, 2), datetime.datetime(1970, 1, 1, 0, 0, 3), datetime.datetime(1970, 1, 1, 0, 0, 4)]
Parameter example:
datetimes=(cftime.DatetimeAllLeap(2000, 1, 6), cftime.DatetimeAllLeap(2000, 1, 8), cftime.DatetimeAllLeap(2000, 1, 10))
Parameter example:
datetimes=numpy.arange( numpy.datetime64('1970-01-01 09:00:00'), numpy.datetime64('1970-01-11 09:00:00'), datetime.timedelta(days=3) )
- units:
str
, optional Reference in time for the timestamps that will be generated, following the format ‘unit_of_time since reference_datetime’. If not provided, set to default value ‘seconds since 1970-01-01 00:00:00Z’.
Parameter example:
units='seconds since 1970-01-01'
Parameter example:
units='days since 2000-01-01 00:00:00'
- calendar:
str
, optional Calendar to be used for the reference in time of units. If not provided, inference from datetime object is attempted on the ‘calendar’ attribute (if it exists) of the first item in datetimes, otherwise set to default ‘gregorian’.
Parameter example:
calendar='all_leap'
Parameter example:
calendar='365_day'
- Returns
Examples
>>> from datetime import datetime >>> td = TimeDomain.from_datetime_sequence( ... datetimes=[datetime(1970, 1, 1), datetime(1970, 1, 2), ... datetime(1970, 1, 3), datetime(1970, 1, 4)], ... units='seconds since 1970-01-01 00:00:00', ... calendar='standard' ... ) >>> print(td) TimeDomain( time (3,): [1970-01-01 00:00:00, 1970-01-02 00:00:00, 1970-01-03 00:00:00] standard bounds (3, 2): [[1970-01-01 00:00:00, ..., 1970-01-04 00:00:00]] standard calendar: standard units: seconds since 1970-01-01 00:00:00 period: 3 days, 0:00:00 timedelta: 1 day, 0:00:00 )