Skip to content

Commit

Permalink
Drop scalar variables that are dimensions too.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmav99 committed Mar 29, 2023
1 parent 6eb1fa7 commit 0cfe753
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions inspectds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import enum
import pathlib
import typing
import warnings

import typer
import xarray as xr
Expand Down Expand Up @@ -128,6 +129,7 @@ def inspect_dataset(
) -> int:
if dataset_type is DATASET_TYPE.AUTO:
dataset_type = infer_dataset_type(path)

open_dataset_kwargs: dict[str, typing.Any] = {}
if dataset_type == DATASET_TYPE.GRIB:
open_dataset_kwargs.update(
Expand All @@ -151,17 +153,34 @@ def inspect_dataset(
)
else:
raise ValueError("WTF??? Unknown Dataset type...")
try:
ds = xr.open_dataset(
filename_or_obj=path,
mask_and_scale=mask_and_scale,
**open_dataset_kwargs,
)
except Exception as exc:
typer.echo(f"Couldn't open {dataset_type.value} dataset: {str(exc)}")
raise typer.Exit()

# Some netcdf files are not compatible with Xarray
# More specifically you can't have a dimension as a variable too.
# https://github.com/pydata/xarray/issues/1709#issuecomment-343714896
# When we find such variables we drop them:
drop_variables: list[str] = []
while True:
try:
ds = xr.open_dataset(
filename_or_obj=path,
mask_and_scale=mask_and_scale,
drop_variables=drop_variables,
**open_dataset_kwargs,
)
except Exception as exc:
if "already exists as a scalar variable" in str(exc):
to_be_dropped = str(exc).split("'")[-2]
drop_variables.append(to_be_dropped)
warnings.warn(f"Dropping scalar variable: {to_be_dropped}", RuntimeWarning)
else:
typer.echo(f"Couldn't open {dataset_type.value} dataset: {str(exc)}")
raise typer.Exit()
else:
break

if full:
dimensions = coordinates = variables = variable_attributes = global_attributes = True

echo_dataset(
ds=ds,
dimensions=dimensions,
Expand Down

0 comments on commit 0cfe753

Please sign in to comment.