Skip to content

Commit

Permalink
provide CFDatetimeCoder from xarray.coders
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuehlbauer committed Dec 13, 2024
1 parent 3ba3e3f commit 1ab43eb
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np

from xarray.coders import CFDatetimeCoder
from xarray.coding import strings, times, variables
from xarray.coding.variables import SerializationWarning, pop_to
from xarray.core import indexing
Expand Down Expand Up @@ -88,7 +89,7 @@ def encode_cf_variable(
ensure_not_multiindex(var, name=name)

for coder in [
times.CFDatetimeCoder(),
CFDatetimeCoder(),
times.CFTimedeltaCoder(),
variables.CFScaleOffsetCoder(),
variables.CFMaskCoder(),
Expand All @@ -109,7 +110,7 @@ def decode_cf_variable(
var: Variable,
concat_characters: bool = True,
mask_and_scale: bool = True,
decode_times: bool | times.CFDatetimeCoder = True,
decode_times: bool | CFDatetimeCoder = True,
decode_endianness: bool = True,
stack_char_dim: bool = True,
use_cftime: bool | None = None,
Expand Down Expand Up @@ -194,25 +195,25 @@ def decode_cf_variable(
var = times.CFTimedeltaCoder().decode(var, name=name)
if decode_times:
# remove checks after end of deprecation cycle
if not isinstance(decode_times, times.CFDatetimeCoder):
if not isinstance(decode_times, CFDatetimeCoder):
if use_cftime is not None:
from warnings import warn

warn(
"Usage of 'use_cftime' as kwarg is deprecated. "
"Please initialize it with xarray.coders.CFDatetimeCoder and "
"Please initialize it with CFDatetimeCoder and "
"'decode_times' kwarg.",
DeprecationWarning,
stacklevel=2,
)
decode_times = times.CFDatetimeCoder(use_cftime=use_cftime)
decode_times = CFDatetimeCoder(use_cftime=use_cftime)
else:
if use_cftime is not None:
raise TypeError(
"Usage of 'use_cftime' as kwarg is not allowed, "
"if 'decode_times' is initialized with "
"xarray.coders.CFDatetimeCoder. Please add 'use_cftime' "
"when initializing xarray.coders.CFDatetimeCoder."
"CFDatetimeCoder. Please add 'use_cftime' "
"when initializing CFDatetimeCoder."
)
var = decode_times.decode(var, name=name)

Expand Down Expand Up @@ -325,9 +326,7 @@ def decode_cf_variables(
attributes: T_Attrs,
concat_characters: bool | Mapping[str, bool] = True,
mask_and_scale: bool | Mapping[str, bool] = True,
decode_times: bool
| times.CFDatetimeCoder
| Mapping[str, bool | times.CFDatetimeCoder] = True,
decode_times: bool | CFDatetimeCoder | Mapping[str, bool | CFDatetimeCoder] = True,
decode_coords: bool | Literal["coordinates", "all"] = True,
drop_variables: T_DropVariables = None,
use_cftime: bool | Mapping[str, bool] | None = None,
Expand Down Expand Up @@ -464,7 +463,7 @@ def decode_cf(
obj: T_DatasetOrAbstractstore,
concat_characters: bool = True,
mask_and_scale: bool = True,
decode_times: bool | times.CFDatetimeCoder = True,
decode_times: bool | CFDatetimeCoder = True,
decode_coords: bool | Literal["coordinates", "all"] = True,
drop_variables: T_DropVariables = None,
use_cftime: bool | None = None,
Expand All @@ -483,7 +482,7 @@ def decode_cf(
mask_and_scale : bool, optional
Lazily scale (using scale_factor and add_offset) and mask
(using _FillValue).
decode_times : bool or xr.coders.CFDatetimeCoder, optional
decode_times : bool or CFDatetimeCoder, optional
Decode cf times (e.g., integers since "hours since 2000-01-01") to
np.datetime64.
decode_coords : bool or {"coordinates", "all"}, optional
Expand Down Expand Up @@ -563,7 +562,7 @@ def cf_decoder(
attributes: T_Attrs,
concat_characters: bool = True,
mask_and_scale: bool = True,
decode_times: bool | times.CFDatetimeCoder = True,
decode_times: bool | CFDatetimeCoder = True,
) -> tuple[T_Variables, T_Attrs]:
"""
Decode a set of CF encoded variables and attributes.
Expand All @@ -580,7 +579,7 @@ def cf_decoder(
mask_and_scale : bool
Lazily scale (using scale_factor and add_offset) and mask
(using _FillValue).
decode_times : bool | xr.coders.CFDatetimeCoder
decode_times : bool | CFDatetimeCoder
Decode cf times ("hours since 2000-01-01") to np.datetime64.
Returns
Expand Down

0 comments on commit 1ab43eb

Please sign in to comment.