Skip to content

Commit be14654

Browse files
Adding tests for particleset creation
and fixing setting lonlatdepth_dtype
1 parent 532f912 commit be14654

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

parcels/particleset.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ def __init__(
159159

160160
self._data = xr.Dataset(
161161
{
162-
"lon": (["trajectory"], lon),
163-
"lat": (["trajectory"], lat),
164-
"depth": (["trajectory"], depth),
162+
"lon": (["trajectory"], lon.astype(lonlatdepth_dtype)),
163+
"lat": (["trajectory"], lat.astype(lonlatdepth_dtype)),
164+
"depth": (["trajectory"], depth.astype(lonlatdepth_dtype)),
165165
"time": (["trajectory"], time),
166166
"dt": (["trajectory"], np.timedelta64(1, "ns") * np.ones(len(pid_orig))),
167167
"state": (["trajectory"], np.zeros((len(pid_orig)), dtype=np.int32)),
168-
"lon_nextloop": (["trajectory"], lon),
169-
"lat_nextloop": (["trajectory"], lat),
170-
"depth_nextloop": (["trajectory"], depth),
168+
"lon_nextloop": (["trajectory"], lon.astype(lonlatdepth_dtype)),
169+
"lat_nextloop": (["trajectory"], lat.astype(lonlatdepth_dtype)),
170+
"depth_nextloop": (["trajectory"], depth.astype(lonlatdepth_dtype)),
171171
"time_nextloop": (["trajectory"], time),
172172
},
173173
coords={

tests/v4/test_particleset.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,36 @@ def DoNothing(particle, fieldset, time):
3434
pass
3535

3636

37+
def test_pset_create_lon_lat(fieldset):
38+
npart = 100
39+
lon = np.linspace(0, 1, npart, dtype=np.float32)
40+
lat = np.linspace(1, 0, npart, dtype=np.float32)
41+
pset = ParticleSet(fieldset, lon=lon, lat=lat, pclass=Particle)
42+
assert np.allclose([p.lon for p in pset], lon, rtol=1e-12)
43+
assert np.allclose([p.lat for p in pset], lat, rtol=1e-12)
44+
45+
46+
@pytest.mark.parametrize("lonlatdepth_dtype", [np.float64, np.float32])
47+
def test_pset_create_line(fieldset, lonlatdepth_dtype):
48+
npart = 100
49+
lon = np.linspace(0, 1, npart, dtype=lonlatdepth_dtype)
50+
lat = np.linspace(1, 0, npart, dtype=lonlatdepth_dtype)
51+
pset = ParticleSet.from_line(
52+
fieldset, size=npart, start=(0, 1), finish=(1, 0), pclass=Particle, lonlatdepth_dtype=lonlatdepth_dtype
53+
)
54+
assert np.allclose([p.lon for p in pset], lon, rtol=1e-12)
55+
assert np.allclose([p.lat for p in pset], lat, rtol=1e-12)
56+
assert isinstance(pset[0].lat, lonlatdepth_dtype)
57+
58+
59+
def test_create_empty_pset(fieldset):
60+
pset = ParticleSet(fieldset, pclass=Particle)
61+
assert pset.size == 0
62+
63+
pset.execute(DoNothing, endtime=1.0, dt=1.0)
64+
assert pset.size == 0
65+
66+
3767
@pytest.mark.parametrize(
3868
"time, expectation",
3969
[

0 commit comments

Comments
 (0)