Skip to content

add tmp file write/read step#358

Draft
j-atkins wants to merge 1 commit into
migrate-v4from
v4-tmp-files
Draft

add tmp file write/read step#358
j-atkins wants to merge 1 commit into
migrate-v4from
v4-tmp-files

Conversation

@j-atkins

Copy link
Copy Markdown
Collaborator

DRAFT

This PR implements a write-to-tmp files step, to improve performance for Parcels v4 fieldset ingestion combined with copernicusmarine streaming (i.e. #357).

@j-atkins

j-atkins commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

@erikvansebille, initial experimentation shows that the tmp file performance fix is indeed a simple implementation and substantially improves actual computation performance, but the tradeoff here is a slow-down by the fact that VirtualShip, in most cases, calls to global datasets from copernicusmarine.

In the case of e.g. a Drifter simulation the ds's (single level) are therefore ~6GB and are not super quick to write/read to/from disk, resulting in a simple single Drifter simulation (63 day lifetime) taking about 3.5 minutes all in, the majority of which time is writing the file to disk. Should note though that this is still a performance increase relative to .to_windowed_array() attempts!

A .sel() to a smaller spatial window (see quick example below) doesn't improve much, presumably something to do with the comms across the streaming.

  def _via_tmp_ds(self, ds) -> xr.Dataset:
      """Create and re-load a temporary local dataset."""
      tmpdir = tempfile.TemporaryDirectory()
      tmp_fpath = Path(tmpdir.name).joinpath("tmp.nc")

      # reduce dataset to smaller spatial window to reduce filesize
      HALO_DEGREES = 20.0  # TODO ...arbitrary but want a healthy buffer for drifters
      ds_minimised = ds.sel(
          lat=slice(self.min_lat - HALO_DEGREES, self.max_lat + HALO_DEGREES),
          lon=slice(self.min_lon - HALO_DEGREES, self.max_lon + HALO_DEGREES),
      )

      ds_minimised.to_netcdf(tmp_fpath)
      del ds, ds_minimised

      return xr.open_dataset(tmp_fpath)

I'll experiment further with file size reduction strategies (e.g. at the copernicusmarine.open_dataset() stage) to improve speed.

Of course the motivation for calling to global datasets in the first place was to avoid any risk of out of bounds errors so that will also be a priority.

@j-atkins

j-atkins commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

In the case of e.g. a Drifter simulation the ds's (single level) are therefore ~6GB and are not super quick to write/read >to/from disk, resulting in a simple single Drifter simulation (63 day lifetime) taking about 3.5 minutes all in, the majority of >which time is writing the file to disk. Should note though that this is still a performance increase relative to .to_windowed_array() attempts!

Following up on this, the file writing speeds don't appear to be overwhelmingly tied to the the spatial extent of the requested ds, in fact they appear more sensitive to the temporal extent (perhaps to do with chunking strategy somewhere on the copernicusmarine end).

Performance for other instruments, such as CTDs and Argo Floats, is pretty good relative to using Parcels v3 though. So, overall, it's promising and it appears that it's just Drifters which slow down (a bit but not massively) relative to v3.

@j-atkins

Copy link
Copy Markdown
Collaborator Author

I think it would probably be a good idea to reduce the file sizes for Drifter simulations, even if just tmp files, because ~6GB global datasets feel a bit unnecessary and potentially problematic. Even adding a generous spatial buffer (+/- 30 degrees) would reduce the size substantially whilst avoiding out of bounds errors in nearly all cases.

This would effectively create a hard limit on lifetimes (velocity dependent) but perhaps a necessary (temporary) trade-off for now. Also, at least this wouldn't crash the simulations, as of #348...

What do you think @erikvansebille?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant