-
Notifications
You must be signed in to change notification settings - Fork 183
Add convert_delft3d_to_sgrid #2774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
erikvansebille
merged 14 commits into
Parcels-code:main
from
erikvansebille:delft3d-support
Aug 3, 2026
+285
−1
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bbe40b1
First attempt at convert_delft3d_to_sgrid
erikvansebille 4f8e267
Merge branch 'main' into delft3d-support
erikvansebille ddc31ab
Adding Delft3D tutorial notebook
erikvansebille 41ed6f7
Making explicit that delft3d_to_sgrid is for structured grids
erikvansebille a7e2e6c
Fixing padding
erikvansebille aff4b76
Fixing warning
erikvansebille f1d26a6
Add interpolation method
erikvansebille 882b469
Adding tutorial to docs page
erikvansebille baa3579
Rename tutorial_delft3d
erikvansebille d957916
Suppress progressbar in notebook
erikvansebille 1f2bc8d
Merge branch 'main' into delft3d-support
erikvansebille 7647cde
Update review comments
erikvansebille 17d091a
Merge branch 'main' into delft3d-support
erikvansebille 01aea1a
Add warnings to convert imports
erikvansebille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "0", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# 🖥️ Delft3D tutorial" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "1", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "This tutorial shows how to load in [Delft3D](https://www.deltares.nl/en/software/delft3d-4-suite/) model output files (in NetCDF format) into Parcels.\n", | ||
| "\n", | ||
| "## Structured Grids\n", | ||
| "Special about Delft3D is that its structured grid contains NaN values for points that don't exist in the model domain. The hashtable approach in Parcels v4 supports these NaN grid cells out of the box, so that you can use the model output directly without any preprocessing." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "2", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import cmocean as cmo\n", | ||
| "import matplotlib.pyplot as plt\n", | ||
| "import numpy as np\n", | ||
| "import pandas as pd\n", | ||
| "\n", | ||
| "import parcels\n", | ||
| "import parcels.tutorial" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "3", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "The example below is of a small domain in the Port of Rotterdam. We first pick the coorindates and the velocity fields from the dataset, and then convert them to the sgrid conventions. Finally, we create a FieldSet from the converted dataset." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "4", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "```{note}\n", | ||
| "While Delft3D provides velocities on a CGrid, we currently can't use the `CGrid_Velocity` interpolator on the velocity fields. This is because the velocities have been rotated to east and north in the output - but the Parcels interpolator expects them in the `M` and `N` (along-grid) directions. Therefore, we use the `XFreeslip` interpolator instead.\n", | ||
| "```" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "5", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "ds = parcels.tutorial.open_dataset(\"Delft3D_data/Rotterdam_tiny\")\n", | ||
| "coords = ds[[\"XZETA\", \"YZETA\", \"SIGMA_C\"]]\n", | ||
| "ds_fset = parcels.convert.delft3d_to_sgrid(\n", | ||
| " fields={\"U\": ds[\"VELU\"], \"V\": ds[\"VELV\"]}, coords=coords\n", | ||
| ")\n", | ||
| "fieldset = parcels.FieldSet.from_sgrid_conventions(ds_fset)\n", | ||
| "\n", | ||
| "# Set the interpolation method for the UV field to XFreeslip (see note above)\n", | ||
| "fieldset.UV.interp_method = parcels.interpolators.XFreeslip()\n", | ||
| "\n", | ||
| "fieldset = fieldset.to_windowed_arrays()\n", | ||
| "fieldset.describe()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "6", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Now we define a grid of a few particles to release in the domain, and run a simple advection simulation. The particles are advected by the Delft3D velocity fields, and we can visualize their trajectories." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "7", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Define a set of starting points\n", | ||
| "points_x, points_y = np.meshgrid(\n", | ||
| " np.linspace(93000, 93300, 5), np.linspace(436300, 436600, 5)\n", | ||
| ")\n", | ||
| "z = np.full(points_x.shape, 0.06) # Depth of 0.06 m\n", | ||
| "pset = parcels.ParticleSet(fieldset, x=points_x, y=points_y, z=z)\n", | ||
| "\n", | ||
| "# St up an output file to save particle trajectories\n", | ||
| "outputfile = parcels.ParticleFile(\n", | ||
| " \"Delft3D_structured.parquet\",\n", | ||
| " outputdt=np.timedelta64(60, \"s\"),\n", | ||
| " mode=\"w\",\n", | ||
| ")\n", | ||
| "\n", | ||
| "\n", | ||
| "# Define a custom error handling kernel to delete particles on any error\n", | ||
| "def DeleteOnAnyError(particles, fieldset):\n", | ||
| " any_error = particles.state >= 50 # This captures all Errors\n", | ||
| " particles[any_error].state = parcels.StatusCode.Delete\n", | ||
| "\n", | ||
| "\n", | ||
| "# Run the particle set with the advection kernel and the custom error handling kernel\n", | ||
| "pset.execute(\n", | ||
| " [parcels.kernels.AdvectionRK2, DeleteOnAnyError],\n", | ||
| " endtime=fieldset.time_interval.right,\n", | ||
| " dt=np.timedelta64(60, \"s\"),\n", | ||
| " output_file=outputfile,\n", | ||
| " verbose_progress=False,\n", | ||
| ")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "8", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "df = parcels.read_particlefile(\"Delft3D_structured.parquet\")\n", | ||
| "\n", | ||
| "fig, ax = plt.subplots(figsize=(8, 6))\n", | ||
| "\n", | ||
| "# Plot background velocity field\n", | ||
| "time_idx = 0\n", | ||
| "layer_idx = 0\n", | ||
| "speed = (\n", | ||
| " np.sqrt(ds[\"VELV\"] ** 2 + ds[\"VELU\"] ** 2)\n", | ||
| " .isel(TIME=time_idx, LAYER=layer_idx)\n", | ||
| " .compute()\n", | ||
| ")\n", | ||
| "tpc = plt.pcolor(\n", | ||
| " speed.XZETA.values,\n", | ||
| " speed.YZETA.values,\n", | ||
| " speed,\n", | ||
| " edgecolors=\"black\",\n", | ||
| " cmap=\"cmo.speed\",\n", | ||
| " vmin=0,\n", | ||
| " vmax=np.max(np.abs(speed)),\n", | ||
| ")\n", | ||
| "time_str = pd.to_datetime(speed.TIME.values).strftime(\"%Y-%m-%d %H:%M:%S\")\n", | ||
| "fig.colorbar(\n", | ||
| " tpc,\n", | ||
| " ax=ax,\n", | ||
| " label=f\"Flow speed on {time_str} and {speed.SIGMA_C.values:.2f}m depth [m/s]\",\n", | ||
| ")\n", | ||
| "\n", | ||
| "for traj in df.partition_by(\"particle_id\"):\n", | ||
| " ax.plot(traj[\"x\"], traj[\"y\"], \"b\", alpha=0.8)\n", | ||
| "\n", | ||
| "ax.plot(points_x, points_y, \"m.\", label=\"initial particle positions\")\n", | ||
| "ax.set_aspect(\"equal\", adjustable=\"box\")\n", | ||
| "ax.set_xlabel(ds.XZETA.long_name)\n", | ||
| "ax.set_ylabel(ds.YZETA.long_name)\n", | ||
| "plt.show()" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Parcels:docs (3.14.6)", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.14.6" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if I recall correctly, NetCDF wasn't the default output format for Delft3D. Best to be explicit here I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! The default output for Delft3D is netcdf (it isn't for SWASH; see #2764). But your suggestion is good anyways