Skip to main content

Pure Python IGRF

Project description

Pure Python International Geomagnetic Reference Field

DOI

Pure Python code to calculate IGRF model predictions. The IGRF is a model of the Earth's main magnetic field that is updated every 5 years. See https://www.ngdc.noaa.gov/IAGA/vmod/igrf.html for details and https://doi.org/10.1186/s40623-020-01163-9 for even more details.

The code is vectorized, so calculations should be pretty fast.

TODO: Some more testing

How?

The only dependencies are Numpy and Pandas. There is no install. Just copy ppigrf.py and IGRF13.shc to your working directory and you are good to go. Or have the ppigrf directory somewhere that Python can find it. Then you should be able to import like this:

import ppigrf

IGRF model values depend on time and position. Time can be given as one or more datetimes. The position can be specified in either geodetic or geocentric coordinates. Example with geodetic coordinates:

from datetime import datetime
lon = 5.32415  # degrees east
lat = 60.39299 # degrees north
h   = 0        # kilometers above sea level
date = datetime(2021, 3, 28)

Be, Bn, Bu = ppigrf.igrf(lon, lat, h, date) # returns east, north, up

Geodetic coordinates take the ellipsoidal shape of the Earth into account. The northward component returned by the igrf function is tangential to the ellipsoid, and in general not tangential to an Earth centered sphere. The upward component is perpendicular to the ellipsoid, and in general not perpendicular to the sphere.

In some cases it is more convenient to work in geocentric coordinates, which are purely spherical. To do that, use the igrf_gc function:

r     = 6500 # kilometers from center of Earht
theta = 30   # colatitude in degrees
phi   = 4    # degrees east (same as lon)

Br, Btheta, Bphi = ppigrf.igrf_gc(r, theta, phi, date) # returns radial, south, east

It is also possible to calculate magnetic field values on a grid. The code uses broadcasting rules for the coordinate inputs. You can also pass multiple dates. If you pass K time stamps, together with coordinates with a combined shape of e.g., (L, M, N), the output will have shape (K, L, M, N). Example:

lon = np.array([20, 120, 220])
lat = np.array([[60, 60, 60], [-60, -60, -60]])
h   = 0
dates = [datetime(y, 1, 1) for y in np.arange(1960, 2021, 20)]
Be, Bn, Bu = ppigrf.igrf(lon, lat, h, dates)

The output will have shape (4, 2, 3).

Why?

There are lots of Python modules that can calculate IGRF values. Most are wrappers of Fortran code, which can be tricky to compile. This version is pure Python. For most applications it is still quite fast. I also prefer the ppigrf interface over the alternatives.

The code is also super portable: Just copy ppigrf.py and the .shc file to your project and you're done.

Notes

The model coefficients are read from an .shc file. This is a file format that is used for certain spherical harmonic magnetic field models. See a selection of .shc model files here: https://github.com/ESA-VirES/MagneticModel/blob/staging/eoxmagmod/eoxmagmod/data/

It should be straightforward to swap the IGRF .shc file with another model, but keep in mind that the time dependence may be implemented differently in other models. IGRF, and this code, uses linear interpolation between model updates, and changing the model file will not change the ppigrf interpolation setup.

The code is vectorized, so it will be quite fast, but probably not as fast as compiled Fortran code. One application which may require more optimization is field line tracing: In the current implementation, the coefficients are loaded and interpolated in time for every function call, which gives a lot of unnecessary overhead.

Contact

If you find errors, please let me know!

You don't need permission to copy or use this code, but I would be happy to know if it is useful for anyone else.

karl.laundal at uib.no

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ppigrf-1.0.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

ppigrf-1.0.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file ppigrf-1.0.0.tar.gz.

File metadata

  • Download URL: ppigrf-1.0.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for ppigrf-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1088adea9b44b82df34c1fcaa4a2f9cdb4f9fcfbca4b3523afed9e866fbb9bd6
MD5 3f35213c111259d5e66c586c1200a4d3
BLAKE2b-256 de86a07f522b91b27780aefe434f531fef750d04a682cdac9e82ec78b2126e8c

See more details on using hashes here.

File details

Details for the file ppigrf-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ppigrf-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for ppigrf-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eba6fb5bff11425417b0e39a224c4a521cd91385eebe08fa0f9135995130e8c5
MD5 67bf398ad3982cc5684a0c681923f55d
BLAKE2b-256 8b9d93061beae95e4fb0f5a7c1c795629a4e67f0bbfb22c44c3cd863814fc643

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page