earthkit.time.sequence - Manipulate sequence of dates
Sequence API
Sequences are described by subclasses of Sequence.
- class earthkit.time.sequence.Sequence
Abstract representation of a sequence of dates
Minimal implementation requirement:
__contains__. Implementingnextandpreviousis highly recommended for efficiency.- next(reference: date, strict: bool = True) date
Return the next date in the sequence, after
reference. IfstrictisFalseandreferenceis in the sequence, it is returned.
- previous(reference: date, strict: bool = True) date
Return the previous date in the sequence, before
reference. IfstrictisFalseandreferenceis in the sequence, it is returned.
- nearest(reference: date, resolve: str = 'previous') date
Return the date closest to
referencein the sequence. In case this is ambiguous,resolvedefines which date to use ("previous"or"next").
- range(start: date, end: date, include_start: bool = True, include_end: bool = True) Iterator[date]
Return all matching dates between
startandend.include_startandinclude_endcontrol whetherstartandendare returned if they are in the sequence.
- bracket(reference: date, num: int | Tuple[int, int] = 1, strict: bool = True) Iterator[date]
Return matching dates around
reference- Parameters:
- classmethod from_dict(seq_dict: dict) Sequence
Create a sequence from the given dictionary
The type of sequence is specified by the
typekey, and must match one of the known sequences, e.g.daily,weekly,monthly,yearly.Dictionary contents can vary depending on the sequence. Frequent items are:
days: list of recurring daysexcludes: specification of which days to skip
Raises
ValueErrorif the type is unknown
- classmethod from_resource(name: str) Sequence
Load a sequence from a resource file
nameshould be either the name of a known sequence (inearthkit.time.data.sequencesorEARTHKIT_TIME_SEQ_PATH, without the extension), or the path to a YAML fileRaises
FileNotFoundErrorif no corresponding resource is found
Built-in Sequences
- class earthkit.time.sequence.DailySequence(excludes: Container[int] = {})
Sequence of consecutive dates
Any day number (in the month) present in
excludeswill be skippedCan be created from a
dictwith items:type:"daily"excludes: (list of int, optional) days of the month to exclude
- class earthkit.time.sequence.WeeklySequence(days: int | Weekday | Iterable[int] | Iterable[Weekday])
Sequence of dates happening on given days of each week
Can be created from a
dictwith items:type:"weekly"days: (int, str, list of int, list of str) days of the week, either numeric (0 = Monday, …, 6 = Sunday) or unambiguous prefixes of names (e.g."M","tue","Friday")
- class earthkit.time.sequence.MonthlySequence(days: int | Iterable[int], excludes: Container[Tuple[int, int]] = {})
Sequence of dates happening on given days of each month
Any
(month, day)tuple present inexcludeswill be skippedCan be created from a
dictwith items:type:"monthly"days: (int, list of int) days of the month (1-31)excludes: (list of pairs of int, list of str, optional) days of the year to exclude, either in (month, day) or in “MMDD” form
- class earthkit.time.sequence.YearlySequence(days: Tuple[int, int] | Iterable[Tuple[int, int]], excludes: Container[date] = {})
Sequence of dates happening on given days of each year (in (month, day) format)
Can be created from a
dictwith items:type:"yearly"days: (str, pair of int, list of str, list of pairs of int) days of the year either in “MMDD” or in (month, day) form (1-12, 1-31)excludes: (list of str, list of triples of int) dates to exclude, either in “YYYYMMDD” or in (year, month, day) form