Skip to content

Make vertical direction analysis more robust (direction of W and ErrorThroughSurface) - #3078

Merged
erikvansebille merged 19 commits into
Parcels-code:mainfrom
erikvansebille:use_positive_depth_in_outofbounds
Dec 2, 2026
Merged

erikvansebille merged 19 commits into
Parcels-code:mainfrom
erikvansebille:use_positive_depth_in_outofbounds

Conversation

@erikvansebille

@erikvansebille erikvansebille commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Description

Until now, the code that distinguished between an ErrorThroughSurface and an ErrorOutOfBounds in the vertical dimension quietly assumed that depth was pointing downward. This PR uses the positive attribute of the depth axis to determine whether a LEFT or a RIGHT out of bounds is a ThroughSurface error

This PR also adds a requirement for the vertical coordinate to have a positive attribute (either up or down). This is in line with the CF-convention: https://cfconventions.org/cf-conventions/cf-conventions.html#vertical-coordinate

Not only will this make the ThroughSurfaceError more robust (because we can now definitely tell what is the surface and what is the ocean floor or top of atmosphere), but it also allows for more stringent checks whether the direction of positive depth and positive W are aligned.

This PR therefore also makes the flipping of the sign of W in convert functions more robust - these are now only flipped when the direction of positive depth is different from the direction of positive W

Checklist

  • 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):

Asked CoPilot to assist me in making these changes and writing unit tests

@erikvansebille erikvansebille changed the title Use positive depth in Distinguishing OutOfBounds and ThroughSurface Errors Use positive depth in distinguishing OutOfBounds and ThroughSurface Errors Nov 26, 2026
Adding a `_assert_has_positive_attribute` method on model.py to check if the vertical axis has a `positive` attribute. This is in line with CF conventions

@wyatt-fluidnumerics wyatt-fluidnumerics left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, just the one question below!

Comment thread src/parcels/_core/field.py Resolved
@erikvansebille
erikvansebille enabled auto-merge (squash) November 30, 2026 15:33
@erikvansebille erikvansebille changed the title Use positive depth in distinguishing OutOfBounds and ThroughSurface Errors Make vertical direction analysis more robust (direction of W and ErrorThroughSurface) Nov 30, 2026
When positive z is up, we can assume that positive W is also up. it would be very strange if that then points down

The issue between mismatched Z and W direction is only when positive Z is down

@wyatt-fluidnumerics wyatt-fluidnumerics left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great changes overall, just a few things I am wondering about.

Additionally, should we add documentation for this attribute in docs/user_guide/convert_writing_functions.md?

if "positive" not in depth.attrs:
raise ValueError(
f"Vertical coordinate {depth.name!r} must have a 'positive' attribute specifying 'up' or 'down'."
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we verify here that the attribute itself is either "up" or "down"? Something like

if depth.attrs["positive"] not in ("up", "down"):
    raise ValueError(
        f"Vertical coordinate "positive" attribute must be "up" or "down", got depth.attrs["positive"]"
    )

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea! Added in 868cf1d

Comment thread src/parcels/convert.py
interface_dim, center_dim = _detect_vertical_coordinates(ds, _FESOM2_VERTICAL_DIMS)
return _rename_vertical_dims(ds, interface_dim, center_dim)
ds = _rename_vertical_dims(ds, interface_dim, center_dim)
ds = _maybe_flip_w_to_positive_depth_direction(ds)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

_maybe_flip_w_to_positive_depth_direction only looks at ds.depth.attrs, for UxDatasets we need to look at either zf or zc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in a80b948

Comment thread src/parcels/_core/field.py Outdated
particles.state,
)
if "Z" in grid_positions:
positive = field.grid.depth_positive_direction

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we call .lower() on positive here or is it okay for this to be case sensitive?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 2c5614d

@erikvansebille

Copy link
Copy Markdown
Member Author

Additionally, should we add documentation for this attribute in docs/user_guide/convert_writing_functions.md?

Yes, very good idea. Added in 9815ea6

@erikvansebille
erikvansebille merged commit a57e633 into Parcels-code:main Dec 2, 2026
22 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Parcels development Dec 2, 2026
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