Skip to content

DOCS: Add tutorial for writing converter functions - #2939

Draft
VeckoTheGecko wants to merge 11 commits into
Parcels-code:mainfrom
VeckoTheGecko:push-snmzxtpwntxv
Draft

VeckoTheGecko wants to merge 11 commits into
Parcels-code:mainfrom
VeckoTheGecko:push-snmzxtpwntxv

Conversation

@VeckoTheGecko

Copy link
Copy Markdown
Contributor

Description

(Will populate PR template later - submitting now for review discussion)

Checklist

  • Closes #xxxx
  • Tests added
  • This PR targets the correct branch (main for normal development, v3-support for v3 support)

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Describe how you used it (e.g., by pasting your prompt):

@VeckoTheGecko

Copy link
Copy Markdown
Contributor Author

I merged #2938 (which was quite cut and dry) so that we could focus on this

@VeckoTheGecko
VeckoTheGecko marked this pull request as draft October 6, 2026 14:39
@VeckoTheGecko

Copy link
Copy Markdown
Contributor Author

keen to hear your thoughts here @erikvansebille .

I think there are some items in What a convert function needs to produce that need to be worked on (either via copy edits here, or via improvements to the code itself - so that more stuff is pulled from the available metadata) - but other than that this is very much in line with my understanding of SGRID.

@VeckoTheGecko VeckoTheGecko changed the title Add tutorial_writing_convert_functions.md DOCS: Add tutorial for writing converter functions Oct 6, 2026

@erikvansebille erikvansebille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very useful manual! See below some comments/thoughts/questions

Comment thread docs/user_guide/examples/tutorial_writing_convert_functions.md Outdated
Comment on lines +9 to +10
Parcels reads structured-grid model data through {py:func}`parcels.FieldSet.from_sgrid_conventions`. This function
does not guess how your model grid is laid out. Instead, it reads [SGRID](https://sgrid.github.io/sgrid/) metadata

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the "does not ... instead" wording a bit confusing. Why not the simpler

Suggested change
Parcels reads structured-grid model data through {py:func}`parcels.FieldSet.from_sgrid_conventions`. This function
does not guess how your model grid is laid out. Instead, it reads [SGRID](https://sgrid.github.io/sgrid/) metadata
Parcels reads structured-grid model data through {py:func}`parcels.FieldSet.from_sgrid_conventions`. This function reads [SGRID](https://sgrid.github.io/sgrid/) metadata

Comment thread docs/user_guide/examples/explanation_writing_convert_functions.md
Comment thread docs/user_guide/examples/explanation_writing_convert_functions.md Outdated
walks through writing a converter for a made-up model, and shows how to check the result with `describe()`.

```{note}
This guide covers structured grids only (SGRID). Unstructured grids use the UGRID conventions and are not covered here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wyatt-fluidnumerics and @fluidnumericsJoe do you want to extend this manual to UGRID too? In a next PR?

{py:func}`parcels.FieldSet.describe` to check that the vector fields were found and that the mesh is what you expect:

```{code-cell}
fieldset = parcels.FieldSet.from_sgrid_conventions(ds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that there's a warning? Should we mention anything about it? Or fix it in the convert function?

Image

Comment on lines +281 to +283
print(f"x: {pset.x[0]:.4f} (expected ~{expected_x:.4f})")
print(f"y: {pset.y[0]:.4f} (expected ~{expected_y:.4f})")
print(f"z: {pset.z[0]:.4f} (expected ~{expected_z:.4f})")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also do these as asserts, so that the RTD fails if for any reason they don't agree?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

| `No variable found in dataset with 'cf_role' attribute set to 'grid_topology'` | No SGRID metadata has been attached. | Add the `grid` variable (Step 2, part 5). |
| `DataArray 'U' with dims (...) has dimensions {'kt'} that are not associated with a direction` | A field dimension is not listed in the SGRID metadata. | Add it to `face_dimensions` or `vertical_dimensions`, or drop the extra dimension (e.g. with `.isel`). |
| `No variable named 'lat'` | The node coordinates have not been renamed. | Rename them to `lon` and `lat`, and make sure `node_coordinates` is `"lon lat"`. |
| `Coordinate 'lon' of your dataset has no 'units' attribute` | Parcels cannot tell whether the mesh is spherical or flat. | Set `units` (e.g. `"degrees_east"` or `"m"`), or pass `mesh=` to `from_sgrid_conventions`. |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to be even more explicit how to set units?

Comment thread docs/user_guide/examples/explanation_writing_convert_functions.md Outdated
| _No error, but particles don't move vertically, or move the wrong way_ | `W` is not named `W`, or its sign convention is positive upward. | Rename it to `W`, and negate it if the model uses positive upward. |

## A template to start from

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add one sentence what to do with the template below? (although the title is relatively self-explanatory)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy leaving it as is. Adding "copy paste the following into where you're writing your code" doesn't really add much value I think

This branch has not been deployed

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

Labels

None yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

2 participants