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

"hourly"

one hour, starting on the hour

"daily"

one calendar day

"8day"

an OBPG 8-day composite: day-of-year 1–8, 9–16, …, with the last period of each year ending on 31 December

"monthly"

one calendar month

"climatology"

one calendar month of a multi-year climatology; only the month of a date matters

"static"

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

TIMETYPES

The canonical time types, from finest to coarsest.

ALIASES

Accepted spellings (compared case-insensitively) for each canonical name.

Functions

date_range(start, end, timetype)

Return the start of every period of timetype that overlaps a range.

freq(timetype)

Return the pandas frequency string of a time type.

normalize(timetype)

Return the canonical name of a time type.

period(dtm, timetype)

Return the first and last time step of the period containing dtm.

period_start(dtm, timetype)

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:
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 (use date_range()), and for "static".

Return type:

str

seagrid.timetypes.normalize(timetype)[source]#

Return the canonical name of a time type.

Parameters:

timetype (str) – A canonical name or any alias in ALIASES, in any case.

Returns:

str – One of TIMETYPES.

Raises:

ValueError – If timetype is not a known name or alias.

Return type:

str

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:
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 has start == 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:
Returns:

pandas.Timestamp – The first instant of the period.

Return type:

Timestamp

See also

period

start and last day (or hour) of the period.