Skip to content

Commit a83d4fe

Browse files
committed
update-patching
1 parent 4cb37ca commit a83d4fe

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

docs/examples/example_globcurrent.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections.abc import Callable
21
from datetime import timedelta
32
from glob import glob
43

@@ -9,19 +8,6 @@
98
import parcels
109

1110

12-
def Unit_to_units(d: dict) -> dict:
13-
if "Unit" in d:
14-
d["units"] = d.pop("Unit")
15-
return d
16-
17-
18-
def xarray_patch_metadata(ds: xr.Dataset, f: Callable[[dict], dict]) -> xr.Dataset:
19-
"""Convert attrs"""
20-
for var in ds.variables:
21-
ds[var].attrs = f(ds[var].attrs)
22-
return ds
23-
24-
2511
def set_globcurrent_fieldset(
2612
filename=None,
2713
):
@@ -35,11 +21,7 @@ def set_globcurrent_fieldset(
3521
"V": "northward_eulerian_current_velocity",
3622
}
3723
dimensions = {"lat": "lat", "lon": "lon", "time": "time"}
38-
ds = (
39-
xr.open_mfdataset(filename, combine="by_coords")
40-
.pipe(xarray_patch_metadata, Unit_to_units)
41-
.pipe(xr.decode_cf)
42-
)
24+
ds = xr.open_mfdataset(filename, combine="by_coords")
4325

4426
return parcels.FieldSet.from_xarray_dataset(
4527
ds,

parcels/tools/_v3to4.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Temporary utilities to help with the transition from v3 to v4 of Parcels.
3+
4+
TODO v4: Remove this module. Move functions that are still relevant into other modules
5+
"""
6+
7+
from collections.abc import Callable
8+
9+
import xarray as xr
10+
11+
12+
def Unit_to_units(d: dict) -> dict:
13+
if "Unit" in d:
14+
d["units"] = d.pop("Unit")
15+
return d
16+
17+
18+
def xarray_patch_metadata(ds: xr.Dataset, f: Callable[[dict], dict]) -> xr.Dataset:
19+
"""Convert attrs"""
20+
for var in ds.variables:
21+
ds[var].attrs = f(ds[var].attrs)
22+
return ds
23+
24+
25+
def patch_dataset_v4_compat(ds: xr.Dataset) -> xr.Dataset:
26+
"""Patches an xarray dataset to be compatible with v4"""
27+
return ds.pipe(xarray_patch_metadata, Unit_to_units)

parcels/tools/exampledata_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from urllib.request import urlretrieve
55

66
import platformdirs
7+
import xarray as xr
8+
9+
from parcels.tools._v3to4 import patch_dataset_v4_compat
710

811
__all__ = ["download_example_dataset", "get_data_home", "list_example_datasets"]
912

@@ -146,5 +149,6 @@ def download_example_dataset(dataset: str, data_home=None):
146149
if not filepath.exists():
147150
url = f"{example_data_url}/{dataset}/{filename}"
148151
urlretrieve(url, str(filepath))
152+
xr.load_dataset(filepath).pipe(patch_dataset_v4_compat).to_netcdf(filepath)
149153

150154
return dataset_folder

0 commit comments

Comments
 (0)