Skip to content

Commit e7e37ef

Browse files
committed
Fix test_time_is_age test
1 parent 7184e1f commit e7e37ef

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

tests/test_particlefile.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from parcels.convert import copernicusmarine_to_sgrid
3131
from parcels.interpolators import XLinear, XLinear_Velocity
3232
from parcels.kernels import AdvectionRK4
33+
from tests import utils
3334
from tests.common_kernels import DoNothing
3435

3536

@@ -306,13 +307,13 @@ def IncreaseAge(particles, fieldset): # pragma: no cover
306307

307308
pset.execute(IncreaseAge, runtime=np.timedelta64(npart * 2, "s"), dt=np.timedelta64(1, "s"), output_file=ofile)
308309

309-
pytest.skip("# TODO: Need to figure out how times work with parquet output (#2386)")
310-
ds = xr.open_zarr(tmp_parquet)
311-
age = ds["age"][:].values.astype("timedelta64[s]")
312-
ds_timediff = np.zeros_like(age)
313-
for i in range(npart):
314-
ds_timediff[i, :] = ds.time.values[i, :] - time[i]
315-
np.testing.assert_equal(age, ds_timediff)
310+
# df = pd.read_parquet(tmp_parquet)
311+
df = utils.read_particlefile(tmp_parquet)
312+
313+
# Map sorted trajectory IDs to release times (0, 1, ..., npart-1 seconds)
314+
for index, df_traj in df.groupby("trajectory"):
315+
release_time = time[index]
316+
np.testing.assert_equal(df_traj["age"].astype("timedelta64[s]").values, (df_traj["time"] - release_time).values)
316317

317318

318319
def test_reset_dt(fieldset, tmp_parquet):

tests/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import cftime
1010
import numpy as np
11+
import pandas as pd
1112
import pyarrow as pa
1213
import pyarrow.parquet as pq
1314
import xarray as xr
@@ -178,3 +179,35 @@ def assert_cftime_like_particlefile(parquet_path: Path) -> None:
178179
"CF-time values in Parquet did not get properly decoded. Are the attributes correct?"
179180
)
180181
return
182+
183+
184+
def read_particlefile(path: Path, decode_times: bool = True) -> pd.DataFrame:
185+
assert path.suffix == ".parquet", "Only Parquet files are supported"
186+
187+
table = pq.read_table(path)
188+
189+
try:
190+
time_field = table.field("time")
191+
except KeyError as e:
192+
raise ValueError(
193+
f"Could not find 'time' column in parquet file. Are you sure {path=!r} is a particlefile?"
194+
) from e
195+
196+
try:
197+
assert b"units" in time_field.metadata
198+
except AssertionError as e:
199+
raise ValueError(f"Could not find 'units' in the 'time' column metadata for parquet {path=!r}.") from e
200+
201+
attrs = {k.decode(): v.decode() for k, v in time_field.metadata.items()}
202+
203+
df = pd.read_parquet(path)
204+
if not decode_times:
205+
return df
206+
207+
values = table.column("time").to_numpy()
208+
var = xr.Variable(("time",), values, attrs)
209+
values = xr.coders.CFDatetimeCoder(time_unit="s").decode(var).values
210+
211+
df["time"] = values
212+
213+
return df

0 commit comments

Comments
 (0)