=head1 NAME

MagneticField -- compute the earth's magnetic field

=head1 SYNOPSIS

B<MagneticField> [ B<-n> I<name> ] [ B<-d> I<dir> ]
[ B<-N> I<Nmax> ] [ B<-M> I<Mmax> ]
[ B<-t> I<time> | B<-c> I<time> I<lat> I<h> ]
[ B<-r> ] [ B<-w> ] [ B<-T> I<tguard> ] [ B<-H> I<hguard> ] [ B<-p> I<prec> ]
[ B<-v> ]
[ B<--comment-delimiter> I<commentdelim> ]
[ B<--version> | B<-h> | B<--help> ]
[ B<--input-file> I<infile> | B<--input-string> I<instring> ]
[ B<--line-separator> I<linesep> ]
[ B<--output-file> I<outfile> ]

=head1 DESCRIPTION

B<MagneticField> reads in times and positions on standard input and
prints out the geomagnetic field on standard output and, optionally, its
rate of change.

The input line is of the form I<time> I<lat> I<lon> I<h>. I<time> is a
date of the form 2012-07-03, a fractional year such as 2012.5, or the
string "now".  I<lat> and I<lon> are the latitude and longitude
expressed as decimal degrees or degrees, minutes, and seconds; for
details on the allowed formats for latitude and longitude, see the
C<GEOGRAPHIC COORDINATES> section of GeoConvert(1).  I<h> is the height
above the ellipsoid in meters; this is optional and defaults to zero.
Alternatively, I<time> can be given on the command line as the argument
to the B<-t> option, in which case it should not be included on the
input lines.  Finally, the magnetic field can be computed at various
points on a circle of latitude (constant I<time>, I<lat>, and I<h>) via
the B<-c> option; in this case only the longitude should be given on the
input lines.

The output consists of the following 7 items:

  the declination (the direction of the horizontal component of
    the magnetic field measured clockwise from north) in degrees,
  the inclination (the direction of the magnetic field measured
    down from the horizontal) in degrees,
  the horizontal field in nanotesla (nT),
  the north component of the field in nT,
  the east component of the field in nT,
  the vertical component of the field in nT (down is positive),
  the total field in nT.

If the B<-r> option is given, a second line is printed giving the rates
of change of these quantities in degrees/yr and nT/yr.

The WGS84 ellipsoid is used, I<a> = 6378137 m, I<f> = 1/298.257223563.

=head1 OPTIONS

=over

=item B<-n> I<name>

use magnetic field model I<name> instead of the default C<wmm2020>.  See
L</MODELS>.

=item B<-d> I<dir>

read magnetic models from I<dir> instead of the default.  See
L</MODELS>.

=item B<-N> I<Nmax>

limit the degree of the model to I<Nmax>.

=item B<-M> I<Mmax>

limit the order of the model to I<Mmax>.

=item B<-t> I<time>

evaluate the field at I<time> instead of reading the time from the input
lines.

=item B<-c> I<time> I<lat> I<h>

evaluate the field on a circle of latitude given by I<time>, I<lat>,
I<h> instead of reading these quantities from the input lines.  In this
case, B<MagneticField> can calculate the field considerably more
quickly.

=item B<-r>

toggle whether to report the rates of change of the field.

=item B<-w>

toggle the longitude first flag (it starts off); if the flag is on, then
on input and output, longitude precedes latitude (except that, on input,
this can be overridden by a hemisphere designator, I<N>, I<S>, I<E>,
I<W>).

=item B<-T> I<tguard>

signal an error if I<time> lies I<tguard> years (default 50 yr) beyond
the range for the model.

=item B<-H> I<hguard>

signal an error if I<h> lies I<hguard> meters (default 500000 m) beyond
the range for the model.

=item B<-p> I<prec>

set the output precision to I<prec> (default 1).  Fields are printed
with precision with I<prec> decimal places; angles use I<prec> + 1
places.

=item B<-v>

print information about the magnetic model on standard error before
processing the input.

=item B<--comment-delimiter> I<commentdelim>

set the comment delimiter to I<commentdelim> (e.g., "#" or "//").  If
set, the input lines will be scanned for this delimiter and, if found,
the delimiter and the rest of the line will be removed prior to
processing and subsequently appended to the output line (separated by a
space).

=item B<--version>

print version and exit.

=item B<-h>

print usage, the default magnetic path and name, and exit.

=item B<--help>

print full documentation and exit.

=item B<--input-file> I<infile>

read input from the file I<infile> instead of from standard input; a file
name of "-" stands for standard input.

=item B<--input-string> I<instring>

read input from the string I<instring> instead of from standard input.
All occurrences of the line separator character (default is a semicolon)
in I<instring> are converted to newlines before the reading begins.

=item B<--line-separator> I<linesep>

set the line separator character to I<linesep>.  By default this is a
semicolon.

=item B<--output-file> I<outfile>

write output to the file I<outfile> instead of to standard output; a
file name of "-" stands for standard output.

=back

=head1 MODELS

B<MagneticField> computes the geomagnetic field using one of the
following models

    wmm2010, the World Magnetic Model 2010, which approximates the
      main magnetic field for the period 2010-2015.  See
      https://ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml
    wmm2015v2, the World Magnetic Model 2015, which approximates the
      main magnetic field for the period 2015-2020.  See
      https://ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml
    wmm2015, a deprecated version of wmm2015v2
    wmm2020, the World Magnetic Model 2020, which approximates the
      main magnetic field for the period 2020-2025.  See
      https://ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml
    igrf11, the International Geomagnetic Reference Field (11th
      generation), which approximates the main magnetic field for
      the period 1900-2015.  See
      https://ngdc.noaa.gov/IAGA/vmod/igrf.html
    igrf12, the International Geomagnetic Reference Field (12th
      generation), which approximates the main magnetic field for
      the period 1900-2020.  See
      https://ngdc.noaa.gov/IAGA/vmod/igrf.html
    emm2010, the Enhanced Magnetic Model 2010, which approximates
      the main and crustal magnetic fields for the period 2010-2015.
      See https://ngdc.noaa.gov/geomag/EMM/index.html
    emm2015, the Enhanced Magnetic Model 2015, which approximates
      the main and crustal magnetic fields for the period 2000-2020.
      See https://ngdc.noaa.gov/geomag/EMM/index.html
    emm2017, the Enhanced Magnetic Model 2017, which approximates
      the main and crustal magnetic fields for the period 2000-2022.
      See https://ngdc.noaa.gov/geomag/EMM/index.html

These models approximate the magnetic field due to the earth's core and
(in the case of emm20xx) its crust.  They neglect magnetic fields due to
the ionosphere, the magnetosphere, nearby magnetized materials,
electrical machinery, etc.

By default, the C<wmm2020> magnetic model is used.  This may changed by
setting the environment variable C<GEOGRAPHICLIB_MAGNETIC_NAME> or with
the B<-n> option.

The magnetic models will be loaded from a directory specified at compile
time.  This may changed by setting the environment variables
C<GEOGRAPHICLIB_MAGNETIC_PATH> or C<GEOGRAPHICLIB_DATA>, or with the
B<-d> option.  The B<-h> option prints the default magnetic path and
name.  Use the B<-v> option to ascertain the full path name of the data
file.

Instructions for downloading and installing magnetic models are
available at
L<https://geographiclib.sourceforge.io/html/magnetic.html#magneticinst>.

=head1 ENVIRONMENT

=over

=item B<GEOGRAPHICLIB_MAGNETIC_NAME>

Override the compile-time default magnetic name of C<wmm2020>.  The
B<-h> option reports the value of B<GEOGRAPHICLIB_MAGNETIC_NAME>, if
defined, otherwise it reports the compile-time value.  If the B<-n>
I<name> option is used, then I<name> takes precedence.

=item B<GEOGRAPHICLIB_MAGNETIC_PATH>

Override the compile-time default magnetic path.  This is typically
C</usr/local/share/GeographicLib/magnetic> on Unix-like systems and
C<C:/ProgramData/GeographicLib/magnetic> on Windows systems.  The B<-h>
option reports the value of B<GEOGRAPHICLIB_MAGNETIC_PATH>, if defined,
otherwise it reports the compile-time value.  If the B<-d> I<dir> option
is used, then I<dir> takes precedence.

=item B<GEOGRAPHICLIB_DATA>

Another way of overriding the compile-time default magnetic path.  If it
is set (and if B<GEOGRAPHICLIB_MAGNETIC_PATH> is not set), then
$B<GEOGRAPHICLIB_DATA>/magnetic is used.

=back

=head1 ERRORS

An illegal line of input will print an error message to standard output
beginning with C<ERROR:> and causes B<MagneticField> to return an exit
code of 1.  However, an error does not cause B<MagneticField> to
terminate; following lines will be converted.  If I<time> or I<h> are
outside the recommended ranges for the model (but inside the ranges
increase by I<tguard> and I<hguard>), a warning is printed on standard
error and the field (which may be inaccurate) is returned in the normal
way.

=head1 EXAMPLES

The magnetic field from WMM2020 in Timbuktu on 2020-01-01

    echo 2020-01-01 16:46:33N 3:00:34W 300 | MagneticField -r
    => -1.60 12.00 33973.5 33960.3 -948.1 7223.0 34732.8
       0.13 -0.02 21.8 23.9 77.9 -8.4 19.5

The first two numbers returned are the declination and inclination of
the field.  The second line gives the annual change.

=head1 SEE ALSO

GeoConvert(1), geographiclib-get-magnetic(8).

=head1 AUTHOR

B<MagneticField> was written by Charles Karney.

=head1 HISTORY

B<MagneticField> was added to GeographicLib,
L<https://geographiclib.sourceforge.io>, in version 1.15.
