Skip to content

Commit aeb3d76

Browse files
Remove to_write="once" option (#2609)
* Removing to_write="once" option * Update v4-migration.md * Also remove the (empty) unit test * Update docstring * Removing section on initial values and to_write=once --------- Co-authored-by: Vecko <36369090+VeckoTheGecko@users.noreply.github.com>
1 parent e79ca8f commit aeb3d76

5 files changed

Lines changed: 10 additions & 101 deletions

File tree

docs/user_guide/examples/tutorial_interaction.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
"# Create custom particle class with extra variable that indicates\n",
113113
"# whether the interaction kernel should be executed on this particle.\n",
114114
"InteractingParticle = parcels.Particle.add_variable(\n",
115-
" parcels.Variable(\"attractor\", dtype=np.bool_, to_write=\"once\"),\n",
115+
" parcels.Variable(\"attractor\", dtype=np.bool_),\n",
116116
")\n",
117117
"\n",
118118
"attractor = [\n",

docs/user_guide/examples/tutorial_sampling.ipynb

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -303,97 +303,12 @@
303303
"Note that the Kernels above return the value of `U` and `V` in the units of the grid. That means that for a spherical grid, the velocities are in **degrees/s**. To convert these to **m/s**, see the [UnitConversion tutorial](https://docs.oceanparcels.org/en/latest/examples/tutorial_unitconverters.html).\n",
304304
"</div>"
305305
]
306-
},
307-
{
308-
"cell_type": "markdown",
309-
"metadata": {},
310-
"source": [
311-
"## Sampling initial values\n"
312-
]
313-
},
314-
{
315-
"attachments": {},
316-
"cell_type": "markdown",
317-
"metadata": {},
318-
"source": [
319-
"In some simulations only the particles initial value within the field is of interest: the variable does not need to be known along the entire trajectory. To reduce computing we can specify the `to_write` argument to the temperature `Variable`. This argument can have three values: `True`, `False` or `'once'`. It determines whether to write the `Variable` to the output file. If we want to know only the initial value, we can enter `'once'` and only the first value will be written to the output file.\n"
320-
]
321-
},
322-
{
323-
"cell_type": "code",
324-
"execution_count": null,
325-
"metadata": {},
326-
"outputs": [],
327-
"source": [
328-
"SampleParticleOnce = parcels.Particle.add_variable(\n",
329-
" parcels.Variable(\"temperature\", initial=np.nan, to_write=\"once\")\n",
330-
")\n",
331-
"\n",
332-
"pset = parcels.ParticleSet(\n",
333-
" fieldset=fieldset,\n",
334-
" pclass=SampleParticleOnce,\n",
335-
" lon=lon,\n",
336-
" lat=lat,\n",
337-
" time=time,\n",
338-
" z=z,\n",
339-
")"
340-
]
341-
},
342-
{
343-
"cell_type": "code",
344-
"execution_count": null,
345-
"metadata": {
346-
"tags": [
347-
"hide-output"
348-
]
349-
},
350-
"outputs": [],
351-
"source": [
352-
"output_file = parcels.ParticleFile(\"writeonce.zarr\", outputdt=timedelta(hours=1))\n",
353-
"\n",
354-
"pset.execute(\n",
355-
" [parcels.kernels.AdvectionRK2, SampleT],\n",
356-
" runtime=timedelta(hours=24),\n",
357-
" dt=timedelta(minutes=5),\n",
358-
" output_file=output_file,\n",
359-
")"
360-
]
361-
},
362-
{
363-
"cell_type": "markdown",
364-
"metadata": {},
365-
"source": [
366-
"We can compare the output where only the initial value is written to output, with the original simulation, where the temperature at each outputdt is written:"
367-
]
368-
},
369-
{
370-
"cell_type": "code",
371-
"execution_count": null,
372-
"metadata": {},
373-
"outputs": [],
374-
"source": [
375-
"ds_particles_once = xr.open_zarr(\"writeonce.zarr\")\n",
376-
"\n",
377-
"plt.figure()\n",
378-
"ax = plt.axes()\n",
379-
"ax.set_ylabel(\"Temperature [$^{\\\\circ}$C]\")\n",
380-
"ax.set_xlabel(\"Observation Number (-)\")\n",
381-
"ax.set_ylim(22.2, 24.5)\n",
382-
"l1 = ax.plot(ds_particles.obs, ds_particles.temperature.T, color=\"red\")\n",
383-
"l2 = ax.plot(\n",
384-
" ds_particles_once.obs,\n",
385-
" np.tile(ds_particles_once.temperature, (ds_particles_once.lon.shape[1], 1)),\n",
386-
" color=\"tab:blue\",\n",
387-
")\n",
388-
"ax.legend([l1[0], l2[0]], [\"Write every outputdt\", \"Write once\"])\n",
389-
"plt.show()"
390-
]
391306
}
392307
],
393308
"metadata": {
394309
"celltoolbar": "Raw-celnotatie",
395310
"kernelspec": {
396-
"display_name": "docs",
311+
"display_name": "Parcels:docs (3.14.4)",
397312
"language": "python",
398313
"name": "python3"
399314
},
@@ -407,7 +322,7 @@
407322
"name": "python",
408323
"nbconvert_exporter": "python",
409324
"pygments_lexer": "ipython3",
410-
"version": "3.14.2"
325+
"version": "3.14.4"
411326
},
412327
"pycharm": {
413328
"stem_cell": {

docs/user_guide/v4-migration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Version 4 of Parcels is unreleased at the moment. The information in this migrat
4040
- `ParticleFile` writing behaviour now errors out if there's existing output (this be being further discussed in https://github.com/Parcels-code/Parcels/issues/2593 )
4141
- A utility to read in ParticleFile output is now available. `parcels.read_particlefile()`
4242
- "trajectory" is now called "particle_id" in the particle file output
43+
- The `to_write="once"`option has been removed. A variable can now only be either written at every output time step, or not written at all.
4344

4445
## Field
4546

src/parcels/_core/particle.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import operator
4-
from typing import Any, Literal
4+
from typing import Any
55

66
import numpy as np
77

@@ -11,7 +11,7 @@
1111
from parcels._reprs import particleclass_repr, variable_repr
1212

1313
__all__ = ["Particle", "ParticleClass", "Variable"]
14-
_TO_WRITE_OPTIONS = [True, False, "once"]
14+
_TO_WRITE_OPTIONS = [True, False]
1515

1616

1717
class Variable:
@@ -26,9 +26,8 @@ class Variable:
2626
initial :
2727
Initial value of the variable. Note that this can also be a Field object,
2828
which will then be sampled at the location of the particle
29-
to_write : bool, 'once', optional
30-
Boolean or 'once'. Controls whether Variable is written to NetCDF file.
31-
If to_write = 'once', the variable will be written as a time-independent 1D array
29+
to_write : bool, optional
30+
Controls whether Variable is written to output file.
3231
attrs : dict, optional
3332
Attributes to be stored with the variable when written to file. This can include metadata such as units, long_name, etc.
3433
"""
@@ -38,7 +37,7 @@ def __init__(
3837
name,
3938
dtype: np.dtype[Any] | type[np.generic] = np.float32,
4039
initial=0,
41-
to_write: bool | Literal["once"] = True,
40+
to_write: bool = True,
4241
attrs: dict | None = None,
4342
):
4443
_assert_str_and_python_varname(name)
@@ -157,7 +156,6 @@ def get_default_particle(spatial_dtype: type[np.float32] | type[np.float64]) ->
157156
Variable(
158157
"particle_id",
159158
dtype=np.int64,
160-
to_write="once",
161159
attrs={
162160
"long_name": "Unique identifier for each particle",
163161
"cf_role": "trajectory_id",

tests/test_particlefile.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ def test_write_dtypes_pfile(fieldset, tmp_parquet):
142142
assert tab[f"v_{d.__name__}"].type == pa.from_numpy_dtype(d)
143143

144144

145-
def test_variable_written_once():
146-
# Test that a vaiable is only written once. This should also work with gradual particle release (so the written once time is actually after the release of the particle)
147-
...
148-
149-
150145
@pytest.mark.skip(reason="Pending ParticleFile refactor; see issue #2386")
151146
@pytest.mark.parametrize("dt", [-np.timedelta64(1, "s"), np.timedelta64(1, "s")])
152147
@pytest.mark.parametrize("maxvar", [2, 4, 10])
@@ -156,7 +151,7 @@ def test_pset_repeated_release_delayed_adding_deleting(fieldset, tmp_parquet, dt
156151
fieldset.add_constant("maxvar", maxvar)
157152

158153
MyParticle = Particle.add_variable(
159-
[Variable("sample_var", initial=0.0), Variable("v_once", dtype=np.float64, initial=0.0, to_write="once")]
154+
[Variable("sample_var", initial=0.0), Variable("v_once", dtype=np.float64, initial=0.0)]
160155
)
161156

162157
pset = ParticleSet(

0 commit comments

Comments
 (0)