Skip to content

Commit e63cb6e

Browse files
committed
o cleanup code make it more readable
1 parent 132a739 commit e63cb6e

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

uxarray/core/utils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ def _open_dataset_with_fallback(filename_or_obj, chunks=None, **kwargs):
2828
xr.Dataset
2929
The opened dataset
3030
"""
31+
# First attempt: use xarray's default read engine
32+
original_error = None
3133
try:
32-
# Try opening with xarray's default read engine
3334
return xr.open_dataset(filename_or_obj, chunks=chunks, **kwargs)
34-
except Exception as first_error:
35-
# If it fails, try with the "netcdf4" engine as backup
35+
except Exception as e:
36+
original_error = e
37+
38+
# Second attempt: fallback to netcdf4 engine
39+
try:
3640
# Extract engine from kwargs to prevent duplicate parameter error
3741
engine = kwargs.pop("engine", "netcdf4")
38-
try:
39-
return xr.open_dataset(
40-
filename_or_obj, engine=engine, chunks=chunks, **kwargs
41-
)
42-
except Exception:
43-
# If both attempts fail, raise the original error to preserve the original behavior
44-
raise first_error
42+
return xr.open_dataset(filename_or_obj, engine=engine, chunks=chunks, **kwargs)
43+
except Exception:
44+
# If both attempts fail, raise the original error to preserve behavior
45+
raise original_error
4546

4647

4748
def _map_dims_to_ugrid(

0 commit comments

Comments
 (0)