Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-variable specification of boolean parameters in open_dataset #9218

Merged
merged 17 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def open_dataset(
chunks: T_Chunks = None,
cache: bool | None = None,
decode_cf: bool | None = None,
mask_and_scale: bool | None = None,
mask_and_scale: bool | dict[str, bool] | None = None,
Ostheer marked this conversation as resolved.
Show resolved Hide resolved
decode_times: bool | None = None,
decode_timedelta: bool | None = None,
use_cftime: bool | None = None,
Expand Down Expand Up @@ -451,14 +451,16 @@ def open_dataset(
decode_cf : bool, optional
Whether to decode these variables, assuming they were saved according
to CF conventions.
mask_and_scale : bool, optional
mask_and_scale : bool or dict, optional
Ostheer marked this conversation as resolved.
Show resolved Hide resolved
If True, replace array values equal to `_FillValue` with NA and scale
values according to the formula `original_values * scale_factor +
add_offset`, where `_FillValue`, `scale_factor` and `add_offset` are
taken from variable attributes (if they exist). If the `_FillValue` or
`missing_value` attribute contains multiple values a warning will be
issued and all array values matching one of the multiple values will
be replaced by NA. This keyword may not be supported by all the backends.
be replaced by NA. 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_times : bool, optional
If True, decode times encoded in the standard NetCDF datetime format
into datetime objects. Otherwise, leave them encoded as numbers.
Expand Down
11 changes: 9 additions & 2 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,18 @@ def _update_bounds_encoding(variables: T_Variables) -> None:
bounds_encoding.setdefault("calendar", encoding["calendar"])


def _item_or_default(obj: Mapping | Any, key: Hashable, default: Any = None):
Ostheer marked this conversation as resolved.
Show resolved Hide resolved
"""
Return item by key if obj is mapping and key is present, else return default value.
"""
return obj.get(key, default) if isinstance(obj, Mapping) else obj


def decode_cf_variables(
variables: T_Variables,
attributes: T_Attrs,
concat_characters: bool = True,
mask_and_scale: bool = True,
mask_and_scale: bool | dict[str, bool] = True,
decode_times: bool = True,
decode_coords: bool | Literal["coordinates", "all"] = True,
drop_variables: T_DropVariables = None,
Expand Down Expand Up @@ -441,7 +448,7 @@ def stackable(dim: Hashable) -> bool:
k,
v,
concat_characters=concat_characters,
mask_and_scale=mask_and_scale,
mask_and_scale=_item_or_default(mask_and_scale, k, True),
decode_times=decode_times,
stack_char_dim=stack_char_dim,
use_cftime=use_cftime,
Expand Down
Loading