-
Notifications
You must be signed in to change notification settings - Fork 168
Adding a tutorial to show gsw use in Kernels #2248
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
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ed9ce5
Adding a tutorial to show gsw use in Kernels
erikvansebille ff3cf12
Removing EOS and TEOS10 kernels
erikvansebille 4db1844
Merge branch 'v4-dev' into gsw_tutorial
erikvansebille a4e8425
Merge branch 'v4-dev' into gsw_tutorial
erikvansebille 869f66c
Merge branch 'v4-dev' into gsw_tutorial
erikvansebille 5b541c0
Moving gsw to general import section
erikvansebille b2c5043
Merge branch 'v4-dev' into gsw_tutorial
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,155 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "0", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Using the `gsw` toolbox to compute density" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "1", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "This tutorial shows how to use the [`gsw` toolbox](https://teos-10.github.io/GSW-Python/) (the Gibbs SeaWater Oceanographic Toolbox of TEOS-10) within Parcels to compute density from temperature and salinity fields. The `gsw` toolbox can be installed via `conda install gsw`." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "2", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "First, load the necessary libraries and the data:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "3", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import numpy as np\n", | ||
| "import xarray as xr\n", | ||
| "\n", | ||
| "import parcels\n", | ||
| "\n", | ||
| "# Load the CopernicusMarine data in the Agulhas region from the example_datasets\n", | ||
| "example_dataset_folder = parcels.download_example_dataset(\n", | ||
| " \"CopernicusMarine_data_for_Argo_tutorial\"\n", | ||
| ")\n", | ||
| "\n", | ||
| "ds = xr.open_mfdataset(f\"{example_dataset_folder}/*.nc\", combine=\"by_coords\")\n", | ||
| "\n", | ||
| "# TODO check how we can get good performance without loading full dataset in memory\n", | ||
| "ds.load() # load the dataset into memory\n", | ||
| "\n", | ||
| "fieldset = parcels.FieldSet.from_copernicusmarine(ds)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "4", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Now, define a custom Particle class that includes temperature, salinity, and density as variables, and create a ParticleSet with one particle at a known location:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "5", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "GSWParticle = parcels.Particle.add_variable(\n", | ||
| " [\n", | ||
| " parcels.Variable(\"temp\", dtype=np.float32, initial=np.nan),\n", | ||
| " parcels.Variable(\"salt\", dtype=np.float32, initial=np.nan),\n", | ||
| " parcels.Variable(\"density\", dtype=np.float32, initial=np.nan),\n", | ||
| " ]\n", | ||
| ")\n", | ||
| "\n", | ||
| "# Initiate one Argo float in the Agulhas Current\n", | ||
| "pset = parcels.ParticleSet(\n", | ||
| " fieldset=fieldset,\n", | ||
| " pclass=GSWParticle,\n", | ||
| " lon=[32],\n", | ||
| " lat=[-31],\n", | ||
| " depth=[200],\n", | ||
| ")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "6", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Now (as the core part of this tutorial) define a custom kernel that uses the `gsw` toolbox to compute density from temperature and salinity:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "7", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "def ParcelsGSW(particles, fieldset):\n", | ||
| " import gsw\n", | ||
| "\n", | ||
| " particles.temp = fieldset.thetao[particles]\n", | ||
| " particles.salt = fieldset.so[particles]\n", | ||
| " pressure = gsw.p_from_z(-particles.depth, particles.lat)\n", | ||
| " particles.density = gsw.density.rho(particles.salt, particles.temp, pressure)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "8", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Finally, run the `ParcelsGSW` Kernel for one timestep and check (for Continuous Integration purposes) that the computed density is as expected:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "9", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "pset.execute(ParcelsGSW, runtime=np.timedelta64(1, \"s\"), dt=np.timedelta64(1, \"s\"))\n", | ||
| "\n", | ||
| "np.testing.assert_allclose(pset.density, [1026.8281], rtol=1e-5)\n", | ||
| "\n", | ||
| "print(\n", | ||
| " f\"Temperature: {pset.temp[0]:.2f}, Salinity: {pset.salt[0]:.2f}, Density: {pset.density[0]:.2f}\"\n", | ||
| ")" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "parcels", | ||
| "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.13.5" | ||
| } | ||
| }, | ||
| "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
Oops, something went wrong.
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.
I would recommend against promoting putting the import inside the kernel - what do you think? It might have additional overhead, and generally we don't do imports inside functions like this
Should we add this to the notebooks running pixi workflow as per #2317 (review) ? Its also not in the docs website - but I think that's fine for now as Reint reconsiders the overall structure
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.
Fixed in 5b541c0
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.
I propose that @reint-fischer adds this notebook as part of his workflow