input4mips_validation.xarray_helpers.time#
input4mips_validation.xarray_helpers.time
#
Helpers for time handling
MONTHS_PER_YEAR: int = 12
module-attribute
#
Months per year
NonUniqueYearMonths
#
Bases: ValueError
Raised when the user tries to convert to year-month with non-unique values
This happens when the datetime values lead to year-month values that are not unique
Source code in src/input4mips_validation/xarray_helpers/time.py
__init__(unique_vals, counts)
#
Initialise the error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unique_vals
|
Iterable[tuple[int, int]]
|
Unique values. In each tuple, the first value is the year and the second is the month. |
required |
counts
|
Iterable[int]
|
Counts of the number of time each unique value appeared in the original array |
required |
Source code in src/input4mips_validation/xarray_helpers/time.py
add_time_bounds(ds, monthly_time_bounds=True, yearly_time_bounds=False, output_dim_bounds='bounds', create_cftime_datetime=None)
#
Add time bounds to a dataset
This should be pushed upstream to cf-xarray at some point probably
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Dataset to which to add time bounds |
required |
monthly_time_bounds
|
bool
|
Are we looking at monthly data i.e. should the time bounds run from the start of one month to the next (which isn't regular spacing but is most often what is desired/required) |
True
|
yearly_time_bounds
|
bool
|
Are we looking at yearly data i.e. should the time bounds run from the start of one year to the next (which isn't regular spacing but is sometimes what is desired/required) |
False
|
output_dim_bounds
|
str
|
What is the name of the bounds dimension
(either already in |
'bounds'
|
create_cftime_datetime
|
Callable[[int, int, int], datetime] | None
|
Function to use to create cftime.datetime objects from date information. If not supplied, we use cftime.datetime. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset with time bounds
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Both |
Notes
There is no copy here, ds is modified in place
(call xarray.Dataset.copy before passing if you don't want this).
Source code in src/input4mips_validation/xarray_helpers/time.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
default_convert_year_month_to_cftime(year, month)
#
Convert year-month information to cftime.datetime, default implementation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
int
|
Year |
required |
month
|
int
|
Month |
required |
Returns:
| Type | Description |
|---|---|
Equivalent [cftime.datetime][]
|
|
Source code in src/input4mips_validation/xarray_helpers/time.py
get_start_of_next_month(y, m, convert_year_month_to_cftime=None)
#
Get start of next month
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
int
|
Year |
required |
m
|
int
|
Month |
required |
convert_year_month_to_cftime
|
Callable[[int, int], datetime] | None
|
Callable to use to convert year-month to cftime.datetime.
If not supplied, we use
|
None
|
Returns:
| Type | Description |
|---|---|
Start of next month
|
|
Source code in src/input4mips_validation/xarray_helpers/time.py
split_time_to_year_month(inp, time_axis='time')
#
Convert the time dimension to year and month without stacking
This means there is still a single time dimension in the output, but there is now also accompanying year and month information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Dataset
|
Data to convert |
required |
Returns:
| Type | Description |
|---|---|
Data with year and month information for the time axis
|
|
Raises:
| Type | Description |
|---|---|
NonUniqueYearMonths
|
The years and months are not unique |
Source code in src/input4mips_validation/xarray_helpers/time.py
xr_time_min_max_to_single_value(v)
#
Convert the results from calling min or max to a single value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
DataArray
|
The results of calling |
required |
Returns:
| Type | Description |
|---|---|
The single minimum or maximum value,
|
converted from being an xarray.DataArray. |