Skip to content

Commit

Permalink
implement feature for 4 additional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathijs Verhaegh committed Jul 12, 2024
1 parent a47363c commit 46de50f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
18 changes: 13 additions & 5 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@ def open_dataset(
cache: bool | None = None,
decode_cf: bool | None = None,
mask_and_scale: bool | Mapping[str, bool] | None = None,
decode_times: bool | None = None,
decode_timedelta: bool | None = None,
use_cftime: bool | None = None,
concat_characters: bool | None = None,
decode_times: bool | Mapping[str, bool] | None = None,
decode_timedelta: bool | Mapping[str, bool] | None = None,
use_cftime: bool | Mapping[str, bool] | None = None,
concat_characters: bool | Mapping[str, bool] | None = None,
decode_coords: Literal["coordinates", "all"] | bool | None = None,
drop_variables: str | Iterable[str] | None = None,
inline_array: bool = False,
Expand Down Expand Up @@ -464,12 +464,16 @@ def open_dataset(
decode_times : bool, optional
If True, decode times encoded in the standard NetCDF datetime format
into datetime objects. Otherwise, leave them encoded as numbers.
Pass a mapping, e.g. ``{"my_variable": False}``,
to toggle this feature per-variable individually.
This keyword may not be supported by all the backends.
decode_timedelta : bool, optional
If True, decode variables and coordinates with time units in
{"days", "hours", "minutes", "seconds", "milliseconds", "microseconds"}
into timedelta objects. If False, leave them encoded as numbers.
If None (default), assume the same value of decode_time.
Pass a mapping, e.g. ``{"my_variable": False}``,
to toggle this feature per-variable individually.
This keyword may not be supported by all the backends.
use_cftime: bool, optional
Only relevant if encoded dates come from a standard calendar
Expand All @@ -480,12 +484,16 @@ def open_dataset(
``cftime.datetime`` objects, regardless of whether or not they can be
represented using ``np.datetime64[ns]`` objects. If False, always
decode times to ``np.datetime64[ns]`` objects; if this is not possible
raise an error. This keyword may not be supported by all the backends.
raise an error. Pass a mapping, e.g. ``{"my_variable": False}``,
to toggle this feature per-variable individually.
This keyword may not be supported by all the backends.
concat_characters : bool, optional
If True, concatenate along the last dimension of character arrays to
form string arrays. Dimensions will only be concatenated over (and
removed) if they have no corresponding variable and if they are only
used as the last dimension of character arrays.
Pass a mapping, e.g. ``{"my_variable": False}``,
to toggle this feature per-variable individually.
This keyword may not be supported by all the backends.
decode_coords : bool or {"coordinates", "all"}, optional
Controls which variables are set as coordinate variables:
Expand Down
20 changes: 10 additions & 10 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ def _item_or_default(obj: Mapping[Any, T] | T, key: Hashable, default: T = None)
def decode_cf_variables(
variables: T_Variables,
attributes: T_Attrs,
concat_characters: bool = True,
mask_and_scale: bool | dict[str, bool] = True,
decode_times: bool = True,
concat_characters: bool | Mapping[str, bool] = True,
mask_and_scale: bool | Mapping[str, bool] = True,
decode_times: bool | Mapping[str, bool] = True,
decode_coords: bool | Literal["coordinates", "all"] = True,
drop_variables: T_DropVariables = None,
use_cftime: bool | None = None,
decode_timedelta: bool | None = None,
use_cftime: bool | Mapping[str, bool] | None = None,
decode_timedelta: bool | Mapping[str, bool] | None = None,
) -> tuple[T_Variables, T_Attrs, set[Hashable]]:
"""
Decode several CF encoded variables.
Expand Down Expand Up @@ -441,7 +441,7 @@ def stackable(dim: Hashable) -> bool:
if k in drop_variables:
continue
stack_char_dim = (
concat_characters
_item_or_default(concat_characters, k, True)
and v.dtype == "S1"
and v.ndim > 0
and stackable(v.dims[-1])
Expand All @@ -450,12 +450,12 @@ def stackable(dim: Hashable) -> bool:
new_vars[k] = decode_cf_variable(
k,
v,
concat_characters=concat_characters,
concat_characters=_item_or_default(concat_characters, k, True),
mask_and_scale=_item_or_default(mask_and_scale, k, True),
decode_times=decode_times,
decode_times=_item_or_default(decode_times, k, True),
stack_char_dim=stack_char_dim,
use_cftime=use_cftime,
decode_timedelta=decode_timedelta,
use_cftime=_item_or_default(use_cftime, k, True),
decode_timedelta=_item_or_default(decode_timedelta, k, True),
)
except Exception as e:
raise type(e)(f"Failed to decode variable {k!r}: {e}") from e
Expand Down

0 comments on commit 46de50f

Please sign in to comment.