Skip to content

Commit 61430c9

Browse files
committed
Review feedback
1 parent 9fcb5bf commit 61430c9

4 files changed

Lines changed: 12 additions & 43 deletions

File tree

src/parcels/_core/particlefile.py

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
from __future__ import annotations
44

5-
from datetime import datetime, timedelta
5+
from datetime import timedelta
66
from pathlib import Path
77
from typing import TYPE_CHECKING, Any, Literal
88

9-
import cftime
109
import numpy as np
1110
import pandas as pd
1211
import pyarrow as pa
@@ -52,10 +51,8 @@ class ParticleFile:
5251
5352
Parameters
5453
----------
55-
name : str
56-
Basename of the output file. This can also be a Zarr store object.
57-
particleset :
58-
ParticleSet to output
54+
path : PathLike
55+
Path of the output Parquet file.
5956
outputdt :
6057
Interval which dictates the update frequency of file output
6158
while ParticleFile is given as an argument of ParticleSet.execute()
@@ -94,12 +91,8 @@ def __init__(self, path: PathLike, outputdt):
9491
if not path.parent.exists():
9592
raise ValueError(f"Folder location for {path=!r} does not exist. Create the folder location first.")
9693

97-
self._maxids = 0
98-
self._pids_written = {}
9994
self.extra_metadata = {}
10095

101-
# TODO v4: Add check that if create_new_zarrfile is False, the store already exists
102-
10396
def __repr__(self) -> str:
10497
return particlefile_repr(self)
10598

@@ -155,9 +148,6 @@ def write(self, pset: ParticleSet, time, indices=None):
155148
pa.table({v.name: pa.array(particle_data[v.name][indices_to_write]) for v in vars_to_write}),
156149
)
157150

158-
# if len(indices_to_write) == 0: # TODO: Remove this?
159-
# return
160-
161151
def close(self):
162152
if self._writer is not None:
163153
self._writer.close()
@@ -192,25 +182,6 @@ def _to_write_particles(particle_data, time):
192182
)[0]
193183

194184

195-
def _get_calendar_and_units(time_interval: TimeInterval) -> dict[str, str]: # TODO: Remove?
196-
calendar = None
197-
units = "seconds"
198-
if time_interval:
199-
if isinstance(time_interval.left, (np.datetime64, datetime)):
200-
calendar = "standard"
201-
elif isinstance(time_interval.left, cftime.datetime):
202-
calendar = time_interval.left.calendar
203-
204-
if calendar is not None:
205-
units += f" since {time_interval.left}"
206-
207-
attrs = {"units": units}
208-
if calendar is not None:
209-
attrs["calendar"] = calendar
210-
211-
return attrs
212-
213-
214185
def read_particlefile(path: PathLike, decode_times: bool = True) -> pd.DataFrame:
215186
"""Read a Parcels particlefile (Parquet format) into a pandas DataFrame.
216187

tests-v3/test_advection.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22
import pytest
3-
import pandas as pd
43
import xarray as xr
54

65
from parcels import (
@@ -80,7 +79,7 @@ def test_analyticalAgrid():
8079
@pytest.mark.parametrize("v", [1, -0.3, 0, -1])
8180
@pytest.mark.parametrize("w", [None, 1, -0.3, 0, -1])
8281
@pytest.mark.parametrize("direction", [1, -1])
83-
def test_uniform_analytical(u, v, w, direction, tmp_parquet):
82+
def test_uniform_analytical(u, v, w, direction, tmp_zarrfile):
8483
lon = np.arange(0, 15, dtype=np.float32)
8584
lat = np.arange(0, 15, dtype=np.float32)
8685
if w is not None:
@@ -100,14 +99,14 @@ def test_uniform_analytical(u, v, w, direction, tmp_parquet):
10099
x0, y0, z0 = 6.1, 6.2, 20
101100
pset = ParticleSet(fieldset, pclass=Particle, lon=x0, lat=y0, depth=z0)
102101

103-
outfile = pset.ParticleFile(name=tmp_parquet, outputdt=1, chunks=(1, 1))
102+
outfile = pset.ParticleFile(name=tmp_zarrfile, outputdt=1, chunks=(1, 1))
104103
pset.execute(AdvectionAnalytical, runtime=4, dt=direction, output_file=outfile)
105104
assert np.abs(pset.lon - x0 - pset.time * u) < 1e-6
106105
assert np.abs(pset.lat - y0 - pset.time * v) < 1e-6
107106
if w is not None:
108107
assert np.abs(pset.depth - z0 - pset.time * w) < 1e-4
109108

110-
ds = xr.open_zarr(tmp_parquet)
109+
ds = xr.open_zarr(tmp_zarrfile)
111110
times = (direction * ds["time"][:]).values.astype("timedelta64[s]")[0]
112111
timeref = np.arange(1, 5).astype("timedelta64[s]")
113112
assert np.allclose(times, timeref, atol=np.timedelta64(1, "ms"))

tests-v3/test_fieldset_sampling.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from math import cos, pi
44

55
import numpy as np
6-
import pandas as pd
76
import pytest
87
import xarray as xr
98

@@ -774,7 +773,7 @@ def test_multiple_grid_addlater_error():
774773
assert fail
775774

776775

777-
def test_fieldset_sampling_updating_order(tmp_parquet):
776+
def test_fieldset_sampling_updating_order(tmp_zarrfile):
778777
def calc_p(t, y, x):
779778
return 10 * t + x + 0.2 * y
780779

@@ -806,10 +805,10 @@ def SampleP(particle, fieldset, time): # pragma: no cover
806805

807806
kernels = [AdvectionRK4, SampleP]
808807

809-
pfile = pset.ParticleFile(tmp_parquet, outputdt=1)
808+
pfile = pset.ParticleFile(tmp_zarrfile, outputdt=1)
810809
pset.execute(kernels, endtime=1, dt=1, output_file=pfile)
811810

812-
ds = xr.open_zarr(tmp_parquet)
811+
ds = xr.open_zarr(tmp_zarrfile)
813812
for t in range(len(ds["obs"])):
814813
for i in range(len(ds["trajectory"])):
815814
assert np.isclose(

tests-v3/test_particlesets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_pset_create_list_with_customvariable(fieldset):
3939

4040

4141
@pytest.mark.parametrize("restart", [True, False])
42-
def test_pset_create_fromparticlefile(fieldset, restart, tmp_parquet):
42+
def test_pset_create_fromparticlefile(fieldset, restart, tmp_zarrfile):
4343
lon = np.linspace(0, 1, 10, dtype=np.float32)
4444
lat = np.linspace(1, 0, 10, dtype=np.float32)
4545

@@ -48,7 +48,7 @@ def test_pset_create_fromparticlefile(fieldset, restart, tmp_parquet):
4848
TestParticle = TestParticle.add_variable("p3", np.float64, to_write="once")
4949

5050
pset = ParticleSet(fieldset, lon=lon, lat=lat, depth=[4] * len(lon), pclass=TestParticle, p3=np.arange(len(lon)))
51-
pfile = pset.ParticleFile(tmp_parquet, outputdt=1)
51+
pfile = pset.ParticleFile(tmp_zarrfile, outputdt=1)
5252

5353
def Kernel(particle, fieldset, time): # pragma: no cover
5454
particle.p = 2.0
@@ -58,7 +58,7 @@ def Kernel(particle, fieldset, time): # pragma: no cover
5858
pset.execute(Kernel, runtime=2, dt=1, output_file=pfile)
5959

6060
pset_new = ParticleSet.from_particlefile(
61-
fieldset, pclass=TestParticle, filename=tmp_parquet, restart=restart, repeatdt=1
61+
fieldset, pclass=TestParticle, filename=tmp_zarrfile, restart=restart, repeatdt=1
6262
)
6363

6464
for var in ["lon", "lat", "depth", "time", "p", "p2", "p3"]:

0 commit comments

Comments
 (0)