Skip to content


Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time
Online demo:

Adapted from the geomagc software and World Magnetic Model of the NOAA Satellite and Information Service, National Geophysical Data Center

geoMagFactory() requires a world magnetic model (WMM) object. The helper function cof2Obj(), available in cof2Obj.js, takes the text of WMM.COF and returns an object suitable for geoMagFactory(). A syncronous XMLHttpRequest to fetch the WMM.COF is recommended in a web environment. The helper function syncXHR(), available in syncXHR.js, takes the url of the WMM.COF file and returns the WMM.COF file as text.

geoMagFactory(wmm) returns a function which can compute the Earth's magnetic field.
The returned function requires two arguments, latitude and longitude (in decimal degrees), and, optionally, altitude in feet (default is 0), and a date object (default is the current system time).

var cof = syncXHR('http://host/path/WMM.COF'),
    wmm = cof2Obj(cof),
    geoMag = geoMagFactory(wmm),
    latitude = 40.0,                // decimal degrees (north is positive)
    longitude = -80.0,              // decimal degrees (east is positive)
    altitude = 0,                   // feet (optional, default is 0)
    time = new Date(2012, 4, 20),   // optional, default is the current system time
    myGeoMag = geoMag(latitude,longitude,altitude,time),
    magneticVariation = myGeoMag.dec,   // Geomagnetic declination (variation)
                                        // in decimal degrees
                                        // -- east is positive
    magneticDip = myGeoMag.dip, // Geomagnetic dip in decimal degrees
                                // (down is positive)
    magneticFieldIntensity = myGeoMag.ti,   // Total Intensity of the
                                            // geomagnetic field in  nanoteslas
    magneticBH =,   // Horizontal Intensity of the geomagnetic
                                // field in nT
    magneticBX = myGeoMag.bx,   // North Component of the geomagnetic field in nT
    magneticBY =,   // East Component of the geomagnetic field in nT
    magneticBZ =,   // Vertical Component of the geomagnetic field
                                // (down is positive)
    lat =, // input latitude
    lon = myGeoMag.lon; // input longitude