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

Load as dataset and use name in data_vars #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
13 changes: 9 additions & 4 deletions src/openeo_test_suite/lib/workflows/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ def load_netcdf_dataarray(
band_dim_name: str = "bands",
) -> xarray.DataArray:
"""Load a data cube from a NetCDF file as a xarray DataArray"""

# TODO: avoid the try-except, and just work from `open_dataset`?
try:
# TODO: avoid the try-except, and just work from `open_dataset`?
data = xarray.open_dataarray(path)
except ValueError:
data = xarray.open_dataset(path, decode_coords="all")
# VITO/CDSE write a Dataset even if there are no bands, with a default name 'var'
if len(data.data_vars) == 1 and [v for v in data.data_vars] == ["var"]:
data = data["var"]
# EODC writes a Dataset even if there are no bands, with a default name 'name'
elif len(data.data_vars) == 1 and [v for v in data.data_vars] == ["name"]:
soxofaan marked this conversation as resolved.
Show resolved Hide resolved
data = data["name"]
else:
data = data.to_dataarray(dim=band_dim_name)
data = data.to_array(dim=band_dim_name)
except ValueError:
data = xarray.open_dataarray(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you switch to trying open_dataset first, is it actually still necessary to do the try-except? As far as I know, open_dataset will also work on data arrays, so doing open_dataarray on except might be useless


return data


Expand Down
Loading