Skip to content

Conversation

@VeckoTheGecko
Copy link
Contributor

@VeckoTheGecko VeckoTheGecko commented Dec 12, 2025

Towards #2003

Changes:

  • See commits and descriptions

The testing as it stands for from_conventions is just testing init on the datasets provided.

I think there's room for future improvements (which I've put as comments in the body)

This acts as the base for future methods (from_nemo etc) for ingesting structured data. Here is what I have in mind:

def from_nemo(field_data: dict[str, xr.Dataset], ds_mesh: xr.Dataset) -> xr.Dataset:
    # Not 100% sure about the function namings, location in the package, and the parameter API. I think that a single xr.Dataset should be returned so users can make modifications.

    ...  # work with field_data and ds_mesh
    ds = ...

    ds["grid"] = xr.DataArray(
        data=np.array([0]),
        attrs=sgrid.Grid2DMetadata(...).to_attrs(),
    )
    # ? does the "location" attribute need to be added to each field? (as per SGRID conventions)
    # ? would it be nice to use `sgrid.rename_dims()` to rename all dimensions to a common naming scheme used for Parcels from_* methods?
    # ? attach other cf-convention attributes? (standard_name etc.)
    # ? Add logger.info or logger.warning messages during ingestion for items the user needs to be aware of?

    return ds

# user code
ds_sgrid_compliant = from_nemo(...)

print(ds_sgrid_compliant)
# Looks good!
FieldSet.from_sgrid_conventions(ds_sgrid_compliant)
# OR
# Looks like the x variable is defined on the cell centers
ds_sgrid_compliant['x'].attrs['location'] = ...
# fixed
FieldSet.from_sgrid_conventions(ds_sgrid_compliant)

Copy link
Member

@erikvansebille erikvansebille left a comment

Choose a reason for hiding this comment

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

Looks good! A few comments/questions below

See https://sgrid.github.io/ for more information on the SGRID conventions.
"""
ds = ds.copy()
Copy link
Member

Choose a reason for hiding this comment

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

Why is it necessary to make a copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a shallow copy - from_fesom2 and from_copernicusmarine do them both to avoid changes to the dataset propogating to the original dataset


fields = {}
if "U" in ds.data_vars and "V" in ds.data_vars:
fields["U"] = Field("U", ds["U"], grid, XLinear)
Copy link
Member

Choose a reason for hiding this comment

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

But this interpolator may not be the default for nemo; so how would from_nemo overwrite the the interpolator here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it should be the default no? Since XLinear would be aware of the grid positioning?

Otherwise we would need to expose this as a param to the function

Copy link
Member

Choose a reason for hiding this comment

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

No, XLinear is only the default for an A-grid layout. For a C-Grid layout, the velocities should be CGrid_Velocity and any other field should be CGrid_Tracer. SO it depends on the Grid topology

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed in person. Outcome: I think we can find where the variables are on the grid via the sgrid conventions, and then use that to choose the interpolator. We'll merge for now, I can rework from_copernicusmarine to use this new code path, and we can explore in future how we can specify this in SGRID and adapt our interpolators

Nodes and edges were the wrong way around
In node_dimensions its X then Y axis
This method ingests datasets using SGRID convention metadata
alongside tests. This is a first iteration and can be improved in future
I think this is better to be explicit that this isn't the execution path for ugrid conventions. Down the line we could provide a `from_conventions` - but at the moment I say we shouldn't (since its not clear how uxarray handles metadata).
@VeckoTheGecko VeckoTheGecko enabled auto-merge (squash) December 12, 2025 12:04
@VeckoTheGecko VeckoTheGecko merged commit 6e47206 into Parcels-code:v4-dev Dec 12, 2025
9 of 10 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Parcels development Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants