npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

geomagnetism

v0.2.0

Published

Computes geomagnetic field information

Downloads

6,416

Readme

geomagnetism

NPM version Coverage Status

geomagnetism provides a convenient interface for retrieving the Earth's magnetic declination (variation between magnetic north and true north) at a given location and date. It leverages NOAA's World Magnetic Model (WMM) to compute properties such as declination, inclination, horizontal intensity, and more.

$ npm install geomagnetism --save

Usage

The main entry point is the model() function. If you pass it a Date object, it will compute results for that specific date. If you do not pass a date, it will default to the current date. Once you have a model, you can query it using the point() method, which expects an array representing latitude and longitude. Optionally, you can provide an altitude as a third element in the array.

  • Latitude and longitude are required: [lat, lon]
  • Altitude (optional) can be provided: [lat, lon, altitude_in_kilometers]

Example: Current Date

const geomagnetism = require('geomagnetism');

// If no date is passed, the current date is used
const currentModel = geomagnetism.model();
const info = currentModel.point([44.53461, -109.05572]); // only lat/lon
console.log('Declination (current date):', info.decl, 'degrees');

Example: Specific Date

const geomagnetism = require('geomagnetism');

// If a date is passed, that specific date is used
const specificDateModel = geomagnetism.model(new Date('2020-10-05'));
const info = specificDateModel.point([44.53461, -109.05572]);
console.log('Declination on 2020-10-05:', info.decl, 'degrees');

Example: Including Altitude

const geomagnetism = require('geomagnetism');

// Provide altitude as the third array item (in kilometers above mean sea level)
const model = geomagnetism.model(new Date('2022-01-01'));
const infoWithAltitude = model.point([44.53461, -109.05572, 1.5]); 
console.log('Declination at altitude:', infoWithAltitude.decl, 'degrees');

Handling Out-of-Range Dates

The geomagnetism.model() function determines the best available World Magnetic Model for the date you provide. If the given date falls outside the known range of the WMM data files, by default (allowOutOfBoundsModel = false), the function will throw an error indicating that no suitable model is found for the given date.

If you would prefer that the library "fall back" to the closest available model rather than throwing an error, you can explicitly pass true to allowOutOfBoundsModel option.

geomagnetism.model(date, { allowOutOfBoundsModel: true });
  • date: (optional) A JavaScript Date object. Defaults to the current date if not specified.
  • allowOutOfBoundsModel: (optional) A boolean. If not specified or false, throws an error if the date is out of range. If true, falls back to the nearest available model.

Examples:

const geomagnetism = require('geomagnetism');

// Strict behavior (default): If the date is out of range, an error is thrown
try {
  let model = geomagnetism.model(new Date('1900-01-01'));  // allowOutOfBoundsModel defaults to false
  let info = model.point([44.53461, -109.05572]);
} catch (err) {
  console.error('Error (strict mode):', err.message);
}

// Fallback behavior: If the date is out of range, it uses the closest model
let fallbackModel = geomagnetism.model(new Date('1900-01-01'), { allowOutOfBoundsModel: true });
let fallbackInfo = fallbackModel.point([44.53461, -109.05572]);
console.log('Declination using fallback model:', fallbackInfo.decl);

Available Data & Models

geomagnetism ships with multiple versions of the WMM data, each valid for a certain time range. The library automatically picks the correct model based on the date you supply. Older models are included to gracefully handle historical dates when using the fallback behavior.

License

Copyright © 2015–2024 Natural Atlas, Inc. & Contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.