From cb0366d7078bb48c8f2f854c526f40e6c7ad7654 Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Fri, 1 May 2026 15:00:13 +0200 Subject: [PATCH] Add failing unit test to check writing of initial sampling values --- tests/test_particlefile.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_particlefile.py b/tests/test_particlefile.py index 759ac5274..aed00832e 100755 --- a/tests/test_particlefile.py +++ b/tests/test_particlefile.py @@ -303,6 +303,24 @@ def IncreaseAge(particles, fieldset): # pragma: no cover np.testing.assert_equal(df_traj["age"].astype("timedelta64[s]").values, (df_traj["time"] - release_time).values) +def test_sampling_initial_value(fieldset, tmp_parquet): + # Test that inital value of a field gets sampled + + SampleParticle = get_default_particle(np.float64).add_variable(Variable("sample", initial=np.nan)) + + def SampleKernel(particles, fieldset): # pragma: no cover + particles.sample = fieldset.U[particles] + + pset = ParticleSet(fieldset, pclass=SampleParticle, lon=[0], lat=[0]) + ofile = ParticleFile(tmp_parquet, outputdt=np.timedelta64(1, "s")) + + pset.execute(SampleKernel, runtime=np.timedelta64(2, "s"), dt=np.timedelta64(1, "s"), output_file=ofile) + + df = parcels.read_particlefile(tmp_parquet) + assert np.isfinite(df["sample"][1:]).all() # should not be nan + assert np.isfinite(df["sample"][0]) # should not be nan + + def test_reset_dt(fieldset, tmp_parquet): # Assert that p.dt gets reset when a write_time is not a multiple of dt # for p.dt=0.02 to reach outputdt=0.05 and endtime=0.1, the steps should be [0.2, 0.2, 0.1, 0.2, 0.2, 0.1], resulting in 6 kernel executions