<jupyter_start><jupyter_text>Tutorial on how to 'delay' the start of particle advection In many applications, it is needed to 'delay' the start of particle advection. For example because particles need to be released at different times throughout an experiment. Or because particles need to be released at a conatant rate from the same set of locations.This tutorial will show how this can be done. We start with importing the relevant modules.<jupyter_code>%matplotlib inline
from parcels import FieldSet, ParticleSet, JITParticle, plotTrajectoriesFile
from parcels import AdvectionRK4
import numpy as np
from datetime import timedelta as delta<jupyter_output><empty_output><jupyter_text>First import a `FieldSet` (from the Peninsula example, in this case)<jupyter_code>fieldset = FieldSet.from_parcels('Peninsula_data/peninsula', allow_time_extrapolation = True)<jupyter_output><empty_output><jupyter_text>Now, there are two ways to delay the start of particles. Either by defining the whole `ParticleSet` at initialisation and giving each particle its own `time`. Or by using the `repeatdt` argument. We will show both options here  Assigning each particle its own `time`  The simplest way to delaye the start of a particle is to use the `time` argument for each particle<jupyter_code>npart = 10  # number of particles to be released
lon = 3e3 * np.ones(npart)
lat = np.linspace(3e3 , 45e3, npart, dtype=np.float32)
time = np.arange(0, npart) * delta(hours=1).total_seconds()  # release every particle one hour later

pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=lon, lat=lat, time=time)<jupyter_output><empty_output><jupyter_text>Then we can execute the `pset` as usual<jupyter_code>output_file = pset.ParticleFile(name="DelayParticle_time.nc", outputdt=delta(hours=1))
pset.execute(AdvectionRK4, runtime=delta(hours=24), dt=delta(minutes=5),
             output_file=output_file)
output_file.export()  # export the trajectory data to a netcdf file<jupyter_output>INFO: Compiled JITParticleAdvectionRK4 ==> /var/folders/r2/8593q8z93kd7t4j9kbb_f7p00000gr/T/parcels-504/5fba5501ea36ea205605260b5254062f.so
100% (86400.0 of 86400.0) |##############| Elapsed Time: 0:00:00 Time:  0:00:00<jupyter_text>And then finally, we can show a movie of the particles. Note that the southern-most particles start to move first.<jupyter_code>plotTrajectoriesFile('DelayParticle_time.nc', mode='movie2d_notebook')<jupyter_output><empty_output><jupyter_text>Using the `repeatdt` argument  The second method to delay the start of particle releases is to use the `repeatdt` argument when constructing a `ParticleSet`. This is especially useful if you want to repeatedly release particles from the same set of locations.<jupyter_code>npart = 10  # number of particles to be released
lon = 3e3 * np.ones(npart)
lat = np.linspace(3e3 , 45e3, npart, dtype=np.float32)
repeatdt = delta(hours=3)  # release from the same set of locations every 3 hours

pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=lon, lat=lat, repeatdt=repeatdt)<jupyter_output><empty_output><jupyter_text>Now we again define an output file and execute the `pset` as usual.<jupyter_code>output_file = pset.ParticleFile(name="DelayParticle_releasedt", outputdt=delta(hours=1))
pset.execute(AdvectionRK4, runtime=delta(hours=24), dt=delta(minutes=5),
             output_file=output_file)<jupyter_output>INFO: Compiled JITParticleAdvectionRK4 ==> /var/folders/r2/8593q8z93kd7t4j9kbb_f7p00000gr/T/parcels-504/694933c823154d73a884aab64a89b625.so
100% (86400.0 of 86400.0) |##############| Elapsed Time: 0:00:02 Time:  0:00:02<jupyter_text>And we get an animation where a new particle is released every 3 hours from each start location<jupyter_code>output_file.export()  # export the trajectory data to a netcdf file
plotTrajectoriesFile('DelayParticle_releasedt.nc', mode='movie2d_notebook')<jupyter_output><empty_output><jupyter_text>Note that, if you want to if you want to at some point stop the repeatdt, the easiest implementation is to use two calls to `pset.execute()`. For example, if in the above example you only want four releases of the pset, you could do the following<jupyter_code>pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=lon, lat=lat, repeatdt=repeatdt)
output_file = pset.ParticleFile(name="DelayParticle_releasedt_9hrs", outputdt=delta(hours=1))

# first run for 3 * 3 hrs
pset.execute(AdvectionRK4, runtime=delta(hours=9), dt=delta(minutes=5),
             output_file=output_file)

# now stop the repeated release
pset.repeatdt = None

# now continue running for the remaining 15 hours
pset.execute(AdvectionRK4, runtime=delta(hours=15), dt=delta(minutes=5),
             output_file=output_file)

output_file.export()  # export the trajectory data to a netcdf file
plotTrajectoriesFile('DelayParticle_releasedt_9hrs.nc', mode='movie2d_notebook')<jupyter_output>INFO: Compiled JITParticleAdvectionRK4 ==> /var/folders/r2/8593q8z93kd7t4j9kbb_f7p00000gr/T/parcels-504/aca27122514704372f3f6e56d1ca8af6.so
100% (28800.0 of 28800.0) |##############| Elapsed Time: 0:00:00 Time:  0:00:00
100% (54000.0 of 54000.0) |##############| Elapsed Time: 0:00:00 Time:  0:00:00
{'fraction_non_alphanumeric': 0.09891808346213292, 'fraction_numerical': 0.05525502318392581, 'mean_word_length': 6.5797950219619326, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '57400', 'license': 'MIT', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1725, 'n_words': 495, 'path': 'parcels/examples/tutorial_delaystart.ipynb', 'repo_name': 'MariusWiggert/parcels', 'size': '5176'}