Add wrap_longitude helper for post-processing trajectory longitudes#40
Merged
Conversation
The solver integrates position without normalising it, so a particle crossing the antimeridian accumulates an unbounded longitude (181, 200, ...) — the correct continuous representation for a trajectory. Add a post-processing helper that folds longitudes back into a canonical window ([-180, 180) by default, or [0, 360) via lower=0.0) for plotting and analysis, leaving the integration untouched. Element-wise, shape-preserving, and jit/vmap/grad-safe. Documented not to be applied to longitudes fed into finite-difference velocity/distance along a trajectory, where the wrap discontinuity would corrupt them (haversine is unaffected, being periodic in longitude).
vadmbertr
marked this pull request as ready for review
July 9, 2026 07:33
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
The solver integrates position with pure arithmetic (
y_new = y + dt·…) and never normalizes it, so a particle drifting across the antimeridian accumulates an unbounded longitude (181°,200°, …). That is the correct continuous representation for a trajectory — the forcing interpolation already folds unbounded longitudes for periodic grids, andhaversineis periodic in longitude, so nothing downstream breaks. But raw unwrapped longitude is awkward for plotting and analysis.This adds an optional post-processing helper to fold longitudes back into a canonical window. The solver is intentionally left untouched: wrapping during integration would inject ±180° seam jumps that break line plots and corrupt any finite-difference velocity/dispersion computed along the trajectory.
Change
geo.py: newwrap_longitude(lon, period=360.0, lower=-180.0)→ folds into the half-open window[lower, lower + period). Defaults give the[-180, 180)convention;lower=0.0gives[0, 360). Element-wise, shape-preserving,jit/vmap/grad-safe (jnp.mod(lon - lower, period) + lower). Exported from the package top level.The docstring warns not to feed wrapped longitudes into finite-difference velocity/distance along a trajectory (the wrap discontinuity corrupts them); wrapping is for presentation only.
Scope
Longitude only. Latitude pole overshoot (
lat > ±90, wherecos(lat)changes sign) is intentionally out of scope here.Tests
New
TestWrapLongitudeintests/test_geo.py: default[-180,180)window (181→-179,-181→179,200→-160,540→-180),[0,360)vialower=0.0, in-window values unchanged, idempotence, shape preservation with a[lon,lat]-column example (latitude untouched), andjit/vmap/gradsafety. Full suite: 348 passed, 2 skipped; ruff clean.