seagrid.timetypes#
Canonical time types (temporal resolutions) and the periods they cover.
Data sources have used many spellings for the same temporal resolution
("day", "DAY", "daily", "mo", "MO", "8D", …). Every
public seagrid function accepts any of the aliases in ALIASES and
turns them into one of the canonical names in TIMETYPES with
normalize(). Code inside seagrid only ever compares canonical names.
Canonical name |
Period of one time step |
|---|---|
|
one hour, starting on the hour |
|
one calendar day |
|
an OBPG 8-day composite: day-of-year 1–8, 9–16, …, with the last period of each year ending on 31 December |
|
one calendar month |
|
one calendar month of a multi-year climatology; only the month of a date matters |
|
no time dimension (bathymetry, regions) |
Examples
>>> from seagrid import timetypes
>>> timetypes.normalize("MO")
'monthly'
>>> timetypes.period("2010-02-11", "8day")
(Timestamp('2010-02-10 00:00:00'), Timestamp('2010-02-17 00:00:00'))
Module Attributes
The canonical time types, from finest to coarsest. |
|
Accepted spellings (compared case-insensitively) for each canonical name. |
Functions
|
Return the start of every period of timetype that overlaps a range. |
|
Return the pandas frequency string of a time type. |
|
Return the canonical name of a time type. |
|
Return the first and last time step of the period containing dtm. |
|
Return the start of the period of timetype that contains dtm. |
- seagrid.timetypes.ALIASES: Final[dict[str, str]] = {'1d': 'daily', '1h': 'hourly', '1m': 'monthly', '8-day': '8day', '8d': '8day', '8day': '8day', '8days': '8day', 'clim': 'climatology', 'climatology': 'climatology', 'constant': 'static', 'd': 'daily', 'daily': 'daily', 'day': 'daily', 'eightday': '8day', 'fixed': 'static', 'h': 'hourly', 'hour': 'hourly', 'hourly': 'hourly', 'hr': 'hourly', 'm': 'monthly', 'mc': 'climatology', 'mo': 'monthly', 'mon': 'monthly', 'month': 'monthly', 'monthly': 'monthly', 'monthly_climatology': 'climatology', 'ms': 'monthly', 'none': 'static', 'p1d': 'daily', 'p1m': 'monthly', 'p8d': '8day', 'pt1h': 'hourly', 'static': 'static'}#
Accepted spellings (compared case-insensitively) for each canonical name.
- seagrid.timetypes.TIMETYPES: Final[tuple[str, ...]] = ('hourly', 'daily', '8day', 'monthly', 'climatology', 'static')#
The canonical time types, from finest to coarsest.
- seagrid.timetypes.date_range(start, end, timetype)[source]#
Return the start of every period of timetype that overlaps a range.
- Parameters:
start (
str,pandas.Timestamp,numpy.datetime64ordatetime) – First and last time of the range (inclusive).end (
str,pandas.Timestamp,numpy.datetime64ordatetime) – First and last time of the range (inclusive).timetype (
str) – Time type name or alias. Not"static".
- Returns:
pandas.DatetimeIndex– Period start times, ascending. The first element is the start of the period containing start, so it may be before start.- Return type:
DatetimeIndex
Examples
>>> [str(d.date()) for d in date_range("2010-01-20", "2010-03-05", "monthly")] ['2010-01-01', '2010-02-01', '2010-03-01']
- seagrid.timetypes.freq(timetype)[source]#
Return the pandas frequency string of a time type.
- Parameters:
timetype (
str) – Time type name or alias.- Returns:
str– A pandas offset alias:"h","D"or"MS".- Raises:
ValueError – For
"8day", whose periods are not evenly spaced (usedate_range()), and for"static".- Return type:
- seagrid.timetypes.normalize(timetype)[source]#
Return the canonical name of a time type.
- Parameters:
timetype (
str) – A canonical name or any alias inALIASES, in any case.- Returns:
- Raises:
ValueError – If timetype is not a known name or alias.
- Return type:
Examples
>>> normalize("DAY"), normalize("8D"), normalize("mo") ('daily', '8day', 'monthly')
- seagrid.timetypes.period(dtm, timetype)[source]#
Return the first and last time step of the period containing dtm.
- Parameters:
dtm (
str,pandas.Timestamp,numpy.datetime64ordatetime) – Any time inside the period.timetype (
str) – Time type name or alias. Not"static".
- Returns:
start, end (
pandas.Timestamp) – For"hourly"both are the hour containing dtm. For the other time types start is the first day and end the last day of the period (both at midnight), so a daily period hasstart == end.- Raises:
ValueError – If timetype is
"static"or unknown.- Return type:
tuple[Timestamp, Timestamp]
Examples
>>> period("2010-02-11 13:20", "monthly") (Timestamp('2010-02-01 00:00:00'), Timestamp('2010-02-28 00:00:00')) >>> period("2010-12-30", "8day") (Timestamp('2010-12-27 00:00:00'), Timestamp('2010-12-31 00:00:00'))
- seagrid.timetypes.period_start(dtm, timetype)[source]#
Return the start of the period of timetype that contains dtm.
- Parameters:
dtm (
str,pandas.Timestamp,numpy.datetime64ordatetime) – Any time inside the period.timetype (
str) – Time type name or alias. Not"static".
- Returns:
pandas.Timestamp– The first instant of the period.- Return type:
Timestamp
See also
periodstart and last day (or hour) of the period.