Skip to content

Add wrap_longitude helper for post-processing trajectory longitudes#40

Merged
vadmbertr merged 1 commit into
mainfrom
feature/wrap-longitude
Jul 9, 2026
Merged

Add wrap_longitude helper for post-processing trajectory longitudes#40
vadmbertr merged 1 commit into
mainfrom
feature/wrap-longitude

Conversation

@vadmbertr

@vadmbertr vadmbertr commented Jul 9, 2026

Copy link
Copy Markdown
Owner

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, and haversine is 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: new wrap_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.0 gives [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, where cos(lat) changes sign) is intentionally out of scope here.

Tests

New TestWrapLongitude in tests/test_geo.py: default [-180,180) window (181→-179, -181→179, 200→-160, 540→-180), [0,360) via lower=0.0, in-window values unchanged, idempotence, shape preservation with a [lon,lat]-column example (latitude untouched), and jit/vmap/grad safety. Full suite: 348 passed, 2 skipped; ruff clean.

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
vadmbertr marked this pull request as ready for review July 9, 2026 07:33
@vadmbertr
vadmbertr merged commit b5b596c into main Jul 9, 2026
1 check passed
@vadmbertr
vadmbertr deleted the feature/wrap-longitude branch July 9, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant