Skip to content

Commit 92b62f2

Browse files
Merge pull request #1920 from OceanParcels/1844-timestamps
Remove timestamps keyword from Field and FieldSet
2 parents e290e19 + 5ec95c9 commit 92b62f2

File tree

11 files changed

+71
-294
lines changed

11 files changed

+71
-294
lines changed

docs/documentation/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Parcels has several documentation and tutorial Jupyter notebooks and scripts whi
2525
../examples/tutorial_periodic_boundaries.ipynb
2626
../examples/tutorial_interpolation.ipynb
2727
../examples/tutorial_unitconverters.ipynb
28-
../examples/tutorial_timestamps.ipynb
2928

3029

3130
.. nbgallery::

docs/examples/example_globcurrent.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Callable
12
from datetime import timedelta
23
from glob import glob
34

@@ -8,11 +9,21 @@
89
import parcels
910

1011

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+
1125
def set_globcurrent_fieldset(
1226
filename=None,
13-
indices=None,
14-
use_xarray=False,
15-
timestamps=None,
1627
):
1728
if filename is None:
1829
data_folder = parcels.download_example_dataset("GlobCurrent_example_data")
@@ -23,25 +34,18 @@ def set_globcurrent_fieldset(
2334
"U": "eastward_eulerian_current_velocity",
2435
"V": "northward_eulerian_current_velocity",
2536
}
26-
if timestamps is None:
27-
dimensions = {"lat": "lat", "lon": "lon", "time": "time"}
28-
else:
29-
dimensions = {"lat": "lat", "lon": "lon"}
30-
if use_xarray:
31-
ds = xr.open_mfdataset(filename, combine="by_coords")
32-
return parcels.FieldSet.from_xarray_dataset(
33-
ds,
34-
variables,
35-
dimensions,
36-
)
37-
else:
38-
return parcels.FieldSet.from_netcdf(
39-
filename,
40-
variables,
41-
dimensions,
42-
indices,
43-
timestamps=timestamps,
44-
)
37+
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+
)
43+
44+
return parcels.FieldSet.from_xarray_dataset(
45+
ds,
46+
variables,
47+
dimensions,
48+
)
4549

4650

4751
@pytest.mark.v4remove(
@@ -51,15 +55,15 @@ def set_globcurrent_fieldset(
5155
"use_xarray", [True, pytest.param(False, marks=pytest.mark.xfail)]
5256
)
5357
def test_globcurrent_fieldset(use_xarray):
54-
fieldset = set_globcurrent_fieldset(use_xarray=use_xarray)
58+
fieldset = set_globcurrent_fieldset()
5559
assert fieldset.U.lon.size == 81
5660
assert fieldset.U.lat.size == 41
5761
assert fieldset.V.lon.size == 81
5862
assert fieldset.V.lat.size == 41
5963

6064
if not use_xarray:
6165
indices = {"lon": [5], "lat": range(20, 30)}
62-
fieldsetsub = set_globcurrent_fieldset(indices=indices, use_xarray=use_xarray)
66+
fieldsetsub = set_globcurrent_fieldset(indices=indices)
6367
assert np.allclose(fieldsetsub.U.lon, fieldset.U.lon[indices["lon"]])
6468
assert np.allclose(fieldsetsub.U.lat, fieldset.U.lat[indices["lat"]])
6569
assert np.allclose(fieldsetsub.V.lon, fieldset.V.lon[indices["lon"]])
@@ -69,21 +73,20 @@ def test_globcurrent_fieldset(use_xarray):
6973
@pytest.mark.parametrize(
7074
"dt, lonstart, latstart", [(3600.0, 25, -35), (-3600.0, 20, -39)]
7175
)
72-
@pytest.mark.parametrize("use_xarray", [True, False])
73-
def test_globcurrent_fieldset_advancetime(dt, lonstart, latstart, use_xarray):
76+
def test_globcurrent_fieldset_advancetime(dt, lonstart, latstart):
7477
data_folder = parcels.download_example_dataset("GlobCurrent_example_data")
7578
basepath = str(data_folder / "20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc")
7679
files = sorted(glob(str(basepath)))
7780

78-
fieldsetsub = set_globcurrent_fieldset(files[0:10], use_xarray=use_xarray)
81+
fieldsetsub = set_globcurrent_fieldset(files[0:10])
7982
psetsub = parcels.ParticleSet.from_list(
8083
fieldset=fieldsetsub,
8184
pclass=parcels.Particle,
8285
lon=[lonstart],
8386
lat=[latstart],
8487
)
8588

86-
fieldsetall = set_globcurrent_fieldset(files[0:10], use_xarray=use_xarray)
89+
fieldsetall = set_globcurrent_fieldset(files[0:10])
8790
psetall = parcels.ParticleSet.from_list(
8891
fieldset=fieldsetall,
8992
pclass=parcels.Particle,
@@ -100,9 +103,8 @@ def test_globcurrent_fieldset_advancetime(dt, lonstart, latstart, use_xarray):
100103
assert abs(psetsub[0].lon - psetall[0].lon) < 1e-4
101104

102105

103-
@pytest.mark.parametrize("use_xarray", [True, False])
104-
def test_globcurrent_particles(use_xarray):
105-
fieldset = set_globcurrent_fieldset(use_xarray=use_xarray)
106+
def test_globcurrent_particles():
107+
fieldset = set_globcurrent_fieldset()
106108

107109
lonstart = [25]
108110
latstart = [-35]
@@ -119,6 +121,10 @@ def test_globcurrent_particles(use_xarray):
119121
assert abs(pset[0].lat - -35.3) < 1
120122

121123

124+
@pytest.mark.v4remove
125+
@pytest.mark.xfail(
126+
reason="Can't patch metadata without using xarray. v4 will natively use xarray anyway. GH1919."
127+
)
122128
@pytest.mark.parametrize("dt", [-300, 300])
123129
def test_globcurrent_xarray_vs_netcdf(dt):
124130
fieldsetNetcdf = set_globcurrent_fieldset(use_xarray=False)
@@ -139,6 +145,10 @@ def test_globcurrent_xarray_vs_netcdf(dt):
139145
assert np.allclose(psetN[0].lat, psetX[0].lat)
140146

141147

148+
@pytest.mark.v4remove
149+
@pytest.mark.xfail(
150+
reason="Timeslices will be removed in v4, as users will be able to use xarray directly."
151+
)
142152
@pytest.mark.parametrize("dt", [-300, 300])
143153
def test_globcurrent_netcdf_timestamps(dt):
144154
fieldsetNetcdf = set_globcurrent_fieldset()
@@ -200,9 +210,8 @@ def test__particles_init_time():
200210
assert pset[0].time - pset4[0].time == 0
201211

202212

203-
@pytest.mark.parametrize("use_xarray", [True, False])
204-
def test_globcurrent_time_extrapolation_error(use_xarray):
205-
fieldset = set_globcurrent_fieldset(use_xarray=use_xarray)
213+
def test_globcurrent_time_extrapolation_error():
214+
fieldset = set_globcurrent_fieldset()
206215
pset = parcels.ParticleSet(
207216
fieldset,
208217
pclass=parcels.Particle,

docs/examples/tutorial_timestamps.ipynb

Lines changed: 0 additions & 134 deletions
This file was deleted.

docs/v4/TODO.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ List of tasks that are important to do before the release of version 4 (but can'
55
- [ ] Make migration guide for v3 to v4
66
- [ ] Just prior to release: Update conda feedstock recipe dependencies (remove cgen and compiler dependencies). Make sure that recipe is up-to-date.
77
- [ ] Revamp the oceanparcels.org landing page, and perhaps also consider new logo/branding?
8-
- [ ] Look into xarray and whether users can create periodic datasets without increasing the size of the original dataset (i.e., no compromise alternative to `time_periodic` param in v3). Update docs accordingly.
98
- [ ] Rerun all the tutorials so that their output is in line with new v4 print statements etc
9+
- Documentation
10+
- [ ] Look into xarray and whether users can create periodic datasets without increasing the size of the original dataset (i.e., no compromise alternative to `time_periodic` param in v3). Update docs accordingly.
11+
- [ ] Look into xarray and whether users can create datasets from snapshots assigning different time dimensions without increasing the size of the original dataset (i.e., no compromise alternative to `timestamps` param in v3). Update docs accordingly.

0 commit comments

Comments
 (0)