Time control functions¶
Some generally helpful time control functions for base QC.
- marine_qc.time_control.convert_date(params)[source]
Decorator to extract date components and inject them as function parameters.
This decorator intercepts the ‘date’ argument from the function call, splits it into its components (e.g., year, month, day), and assigns those components to specified parameters in the wrapped function. It supports scalar or sequence inputs for ‘date’.
- Parameters:
params (
listofstr) – List of parameter names corresponding to date components to be extracted and passed to the decorated function.- Return type:
- Returns:
Callable[...,Any]– A decorator that wraps a function, extracting date components before calling it.
Notes
The decorator expects the wrapped function to accept the parameters listed in params. If a parameter is missing, it raises a ValueError.
If the ‘date’ argument is None, the original function is called without modification.
Supports scalar-like ‘date’ values as well as iterable sequences.
Assumes a helper function split_date exists that splits a date into components and returns a dictionary mapping parameter names to their values.
- marine_qc.time_control.convert_date_to_hours(dates)[source]
Convert an array of datetimes to an array of hours since the first element.
- marine_qc.time_control.convert_time_in_hours(hour, minute, sec, zone, daylight_savings_time)[source]
Convert integer hour, minute, and second to time in decimal hours.
- marine_qc.time_control.day_in_year(year=None, month=1, day=1)[source]
Get the day in year from 1 to 365 or 366.
- Parameters:
- Return type:
- Returns:
int– Day in year. If year is not specified then the year is treated as a non-leap year and 29 February returns the same value as 1 March.
- marine_qc.time_control.day_in_year_array(month, day)[source]
Get the day in year from 1 to 365. Leap years are dealt with by allowing Feb 29 and Mar 1 to be the same day.
- Parameters:
month (
1D np.ndarray) – Array of months.day (
1D np.ndarray) – Array of days.
- Return type:
- Returns:
np.ndarray– Array of day number from 1-365.
- marine_qc.time_control.get_month_lengths(year)[source]
Return a list holding the lengths of the months in a given year.
- marine_qc.time_control.jul_day(year, month, day)[source]
Routine to calculate julian day. This is the weird Astronomical thing which counts from 1 Jan 4713 BC.
- Parameters:
- Return type:
- Returns:
int– Julian day.
Notes
This is one of those routines that looks baffling but works. No one is sure exactly how. It gets written once and then remains untouched for centuries, mysteriously working.
- marine_qc.time_control.leap_year(years_since_1980)[source]
Check if input year is a Leap year.
- marine_qc.time_control.leap_year_correction(time_in_hours, day, years_since_1980)[source]
Make leap year correction.
- marine_qc.time_control.pentad_to_month_day(p)[source]
Given a pentad number, return the month and day of the first day in the pentad.
- marine_qc.time_control.relative_year_number(year, reference=1979)[source]
Get number of year relative to reference year (1979 by default).
- marine_qc.time_control.split_date(date)[source]
Split datetime date into year, month, day and hour.
- marine_qc.time_control.time_difference(times1, times2)[source]
Convert two arrays of datetimes to the difference in hours.
- Parameters:
times1 (
SequenceDatetimeType) – 1-dimensional array of reference time points.times2 (
SequenceDatetimeType) – 1-dimensional array of time points to compare againsttimes1.
- Return type:
- Returns:
array-likeoffloat,shape (n,)– 1-dimensional array containing the time difference in hours computed astimes2 - times1.- Raises:
TypeError – If inspect_arrays does not return np.ndarrays.
- marine_qc.time_control.time_in_whole_days(time_in_hours, day, years_since_1980, leap)[source]
Calculate from time in hours to time in whole days.
- marine_qc.time_control.valid_month_day(year=None, month=1, day=1)[source]
Return True if month and day combination are allowed, False otherwise. Assumes that Feb 29th is valid.
- Parameters:
- Return type:
- Returns:
bool– True if month and day (or year month and day) are a valid combination (e.g. 12th March) and False if not (e.g. 30th February).
Notes
Assumes that February 29th is a valid date.
- marine_qc.time_control.which_pentad(month, day)[source]
Take month and day as inputs and return pentad in range 1-73.
- Parameters:
- Return type:
- Returns:
int– Pentad (5-day period) containing input day, from 1 (1 Jan-5 Jan) to 73 (27-31 Dec).- Raises:
ValueError – If month not in range 1-12 or day not in range 1-31.
Notes
The calculation is rather simple. It just loops through the year and adds up days till it reaches the day we are interested in. February 29th is treated as though it were March 1st in a regular year.
- marine_qc.time_control.which_pentad_array(month, day)[source]
Take month and day arrays as inputs and return array of pentads in range 1-73.
- Parameters:
month (
ndarray) – Month containing the day for which we want to calculate the pentad.day (
ndarray) – Day for the day for which we want to calculate the pentad.
- Return type:
- Returns:
ndarray– Pentad (5-day period) containing input day, from 1 (1 Jan-5 Jan) to 73 (27-31 Dec).