Speed up calculations - #4
Merged
Merged
Conversation
kristianjohansson
approved these changes
Sep 7, 2026
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.
Speed up WMM calculations (~120x)
Why
wmmsub()re-readWMM.COFfrom disk, allocated and freed all model memoryon every single point. The Python wrapper looped point-by-point, creating
fresh
ctypesobjects and doing anos.chdir()round-trip per call (neededbecause the C code opened the coefficient file by relative path).
For a 0.1° global grid (6.2M points) that meant 6.2M file reads.
Results
Single-threaded, per point:
What changed
C (
wmm_point_sub.c)wmm_init(cof_path)/wmm_free(). Coefficients and scratchbuffers are loaded once and reused.
wmm_eval_latlon_grid(),wmm_eval_grid(),wmm_eval_many().(a/r)^(n+2)depend only on latitude and altitude, sogrid evaluation hoists them out of the longitude loop; only the
cos/sin(mλ)recurrence and the summation run per point.
n/m, butMAG_PcupLowrebuilt them — with a
malloc/free— on every point. They are now computedonce at init.
wmmsub()is kept for backward compatibility.Python (
base.py)ctypescall per array instead of one per point; numpy buffers are passedthrough without copying.
WMM.COF, so the per-pointos.chdir()hack is gone.assertin the hot path with real exceptions (assertis strippedunder
-O).transect()is now exported from the package.Build
Releasewith-O3(previously no build type was set at all, sothe shared library was built unoptimized).
Breaking change
wmm()now returns adictof numpy arrays instead of anxarray.Dataset: