GeographicLib  1.50
MagneticModel.hpp
Go to the documentation of this file.
1 /**
2  * \file MagneticModel.hpp
3  * \brief Header for GeographicLib::MagneticModel class
4  *
5  * Copyright (c) Charles Karney (2011-2019) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_MAGNETICMODEL_HPP)
11 #define GEOGRAPHICLIB_MAGNETICMODEL_HPP 1
12 
16 
17 #if defined(_MSC_VER)
18 // Squelch warnings about dll vs vector
19 # pragma warning (push)
20 # pragma warning (disable: 4251)
21 #endif
22 
23 namespace GeographicLib {
24 
25  class MagneticCircle;
26 
27  /**
28  * \brief Model of the earth's magnetic field
29  *
30  * Evaluate the earth's magnetic field according to a model. At present only
31  * internal magnetic fields are handled. These are due to the earth's code
32  * and crust; these vary slowly (over many years). Excluded are the effects
33  * of currents in the ionosphere and magnetosphere which have daily and
34  * annual variations.
35  *
36  * See \ref magnetic for details of how to install the magnetic models and
37  * the data format.
38  *
39  * See
40  * - General information:
41  * - http://geomag.org/models/index.html
42  * - WMM2010:
43  * - https://ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml
44  * - https://ngdc.noaa.gov/geomag/WMM/data/WMM2010/WMM2010COF.zip
45  * - WMM2015 (deprecated):
46  * - https://ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml
47  * - https://ngdc.noaa.gov/geomag/WMM/data/WMM2015/WMM2015COF.zip
48  * - WMM2015V2:
49  * - https://ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml
50  * - https://ngdc.noaa.gov/geomag/WMM/data/WMM2015/WMM2015v2COF.zip
51  * - IGRF11:
52  * - https://ngdc.noaa.gov/IAGA/vmod/igrf.html
53  * - https://ngdc.noaa.gov/IAGA/vmod/igrf11coeffs.txt
54  * - https://ngdc.noaa.gov/IAGA/vmod/geomag70_linux.tar.gz
55  * - EMM2010:
56  * - https://ngdc.noaa.gov/geomag/EMM/index.html
57  * - https://ngdc.noaa.gov/geomag/EMM/data/geomag/EMM2010_Sph_Windows_Linux.zip
58  * - EMM2015:
59  * - https://ngdc.noaa.gov/geomag/EMM/index.html
60  * - https://www.ngdc.noaa.gov/geomag/EMM/data/geomag/EMM2015_Sph_Linux.zip
61  * - EMM2017:
62  * - https://ngdc.noaa.gov/geomag/EMM/index.html
63  * - https://www.ngdc.noaa.gov/geomag/EMM/data/geomag/EMM2017_Sph_Linux.zip
64  *
65  * Example of use:
66  * \include example-MagneticModel.cpp
67  *
68  * <a href="MagneticField.1.html">MagneticField</a> is a command-line utility
69  * providing access to the functionality of MagneticModel and MagneticCircle.
70  **********************************************************************/
71 
73  private:
74  typedef Math::real real;
75  static const int idlength_ = 8;
76  std::string _name, _dir, _description, _date, _filename, _id;
77  real _t0, _dt0, _tmin, _tmax, _a, _hmin, _hmax;
78  int _Nmodels, _Nconstants, _nmx, _mmx;
80  Geocentric _earth;
81  std::vector< std::vector<real> > _G;
82  std::vector< std::vector<real> > _H;
83  std::vector<SphericalHarmonic> _harm;
84  void Field(real t, real lat, real lon, real h, bool diffp,
85  real& Bx, real& By, real& Bz,
86  real& Bxt, real& Byt, real& Bzt) const;
87  void ReadMetadata(const std::string& name);
88  MagneticModel(const MagneticModel&); // copy constructor not allowed
89  MagneticModel& operator=(const MagneticModel&); // nor copy assignment
90  public:
91 
92  /** \name Setting up the magnetic model
93  **********************************************************************/
94  ///@{
95  /**
96  * Construct a magnetic model.
97  *
98  * @param[in] name the name of the model.
99  * @param[in] path (optional) directory for data file.
100  * @param[in] earth (optional) Geocentric object for converting
101  * coordinates; default Geocentric::WGS84().
102  * @param[in] Nmax (optional) if non-negative, truncate the degree of the
103  * model this value.
104  * @param[in] Mmax (optional) if non-negative, truncate the order of the
105  * model this value.
106  * @exception GeographicErr if the data file cannot be found, is
107  * unreadable, or is corrupt, or if \e Mmax > \e Nmax.
108  * @exception std::bad_alloc if the memory necessary for storing the model
109  * can't be allocated.
110  *
111  * A filename is formed by appending ".wmm" (World Magnetic Model) to the
112  * name. If \e path is specified (and is non-empty), then the file is
113  * loaded from directory, \e path. Otherwise the path is given by the
114  * DefaultMagneticPath().
115  *
116  * This file contains the metadata which specifies the properties of the
117  * model. The coefficients for the spherical harmonic sums are obtained
118  * from a file obtained by appending ".cof" to metadata file (so the
119  * filename ends in ".wwm.cof").
120  *
121  * The model is not tied to a particular ellipsoidal model of the earth.
122  * The final earth argument to the constructor specifies an ellipsoid to
123  * allow geodetic coordinates to the transformed into the spherical
124  * coordinates used in the spherical harmonic sum.
125  *
126  * If \e Nmax &ge; 0 and \e Mmax < 0, then \e Mmax is set to \e Nmax.
127  * After the model is loaded, the maximum degree and order of the model can
128  * be found by the Degree() and Order() methods.
129  **********************************************************************/
130  explicit MagneticModel(const std::string& name,
131  const std::string& path = "",
132  const Geocentric& earth = Geocentric::WGS84(),
133  int Nmax = -1, int Mmax = -1);
134  ///@}
135 
136  /** \name Compute the magnetic field
137  **********************************************************************/
138  ///@{
139  /**
140  * Evaluate the components of the geomagnetic field.
141  *
142  * @param[in] t the time (years).
143  * @param[in] lat latitude of the point (degrees).
144  * @param[in] lon longitude of the point (degrees).
145  * @param[in] h the height of the point above the ellipsoid (meters).
146  * @param[out] Bx the easterly component of the magnetic field (nanotesla).
147  * @param[out] By the northerly component of the magnetic field
148  * (nanotesla).
149  * @param[out] Bz the vertical (up) component of the magnetic field
150  * (nanotesla).
151  **********************************************************************/
152  void operator()(real t, real lat, real lon, real h,
153  real& Bx, real& By, real& Bz) const {
154  real dummy;
155  Field(t, lat, lon, h, false, Bx, By, Bz, dummy, dummy, dummy);
156  }
157 
158  /**
159  * Evaluate the components of the geomagnetic field and their time
160  * derivatives
161  *
162  * @param[in] t the time (years).
163  * @param[in] lat latitude of the point (degrees).
164  * @param[in] lon longitude of the point (degrees).
165  * @param[in] h the height of the point above the ellipsoid (meters).
166  * @param[out] Bx the easterly component of the magnetic field (nanotesla).
167  * @param[out] By the northerly component of the magnetic field
168  * (nanotesla).
169  * @param[out] Bz the vertical (up) component of the magnetic field
170  * (nanotesla).
171  * @param[out] Bxt the rate of change of \e Bx (nT/yr).
172  * @param[out] Byt the rate of change of \e By (nT/yr).
173  * @param[out] Bzt the rate of change of \e Bz (nT/yr).
174  **********************************************************************/
175  void operator()(real t, real lat, real lon, real h,
176  real& Bx, real& By, real& Bz,
177  real& Bxt, real& Byt, real& Bzt) const {
178  Field(t, lat, lon, h, true, Bx, By, Bz, Bxt, Byt, Bzt);
179  }
180 
181  /**
182  * Create a MagneticCircle object to allow the geomagnetic field at many
183  * points with constant \e lat, \e h, and \e t and varying \e lon to be
184  * computed efficiently.
185  *
186  * @param[in] t the time (years).
187  * @param[in] lat latitude of the point (degrees).
188  * @param[in] h the height of the point above the ellipsoid (meters).
189  * @exception std::bad_alloc if the memory necessary for creating a
190  * MagneticCircle can't be allocated.
191  * @return a MagneticCircle object whose MagneticCircle::operator()(real
192  * lon) member function computes the field at particular values of \e
193  * lon.
194  *
195  * If the field at several points on a circle of latitude need to be
196  * calculated then creating a MagneticCircle and using its member functions
197  * will be substantially faster, especially for high-degree models.
198  **********************************************************************/
199  MagneticCircle Circle(real t, real lat, real h) const;
200 
201  /**
202  * Compute various quantities dependent on the magnetic field.
203  *
204  * @param[in] Bx the \e x (easterly) component of the magnetic field (nT).
205  * @param[in] By the \e y (northerly) component of the magnetic field (nT).
206  * @param[in] Bz the \e z (vertical, up positive) component of the magnetic
207  * field (nT).
208  * @param[out] H the horizontal magnetic field (nT).
209  * @param[out] F the total magnetic field (nT).
210  * @param[out] D the declination of the field (degrees east of north).
211  * @param[out] I the inclination of the field (degrees down from
212  * horizontal).
213  **********************************************************************/
214  static void FieldComponents(real Bx, real By, real Bz,
215  real& H, real& F, real& D, real& I) {
216  real Ht, Ft, Dt, It;
217  FieldComponents(Bx, By, Bz, real(0), real(1), real(0),
218  H, F, D, I, Ht, Ft, Dt, It);
219  }
220 
221  /**
222  * Compute various quantities dependent on the magnetic field and its rate
223  * of change.
224  *
225  * @param[in] Bx the \e x (easterly) component of the magnetic field (nT).
226  * @param[in] By the \e y (northerly) component of the magnetic field (nT).
227  * @param[in] Bz the \e z (vertical, up positive) component of the magnetic
228  * field (nT).
229  * @param[in] Bxt the rate of change of \e Bx (nT/yr).
230  * @param[in] Byt the rate of change of \e By (nT/yr).
231  * @param[in] Bzt the rate of change of \e Bz (nT/yr).
232  * @param[out] H the horizontal magnetic field (nT).
233  * @param[out] F the total magnetic field (nT).
234  * @param[out] D the declination of the field (degrees east of north).
235  * @param[out] I the inclination of the field (degrees down from
236  * horizontal).
237  * @param[out] Ht the rate of change of \e H (nT/yr).
238  * @param[out] Ft the rate of change of \e F (nT/yr).
239  * @param[out] Dt the rate of change of \e D (degrees/yr).
240  * @param[out] It the rate of change of \e I (degrees/yr).
241  **********************************************************************/
242  static void FieldComponents(real Bx, real By, real Bz,
243  real Bxt, real Byt, real Bzt,
244  real& H, real& F, real& D, real& I,
245  real& Ht, real& Ft, real& Dt, real& It);
246  ///@}
247 
248  /** \name Inspector functions
249  **********************************************************************/
250  ///@{
251  /**
252  * @return the description of the magnetic model, if available, from the
253  * Description file in the data file; if absent, return "NONE".
254  **********************************************************************/
255  const std::string& Description() const { return _description; }
256 
257  /**
258  * @return date of the model, if available, from the ReleaseDate field in
259  * the data file; if absent, return "UNKNOWN".
260  **********************************************************************/
261  const std::string& DateTime() const { return _date; }
262 
263  /**
264  * @return full file name used to load the magnetic model.
265  **********************************************************************/
266  const std::string& MagneticFile() const { return _filename; }
267 
268  /**
269  * @return "name" used to load the magnetic model (from the first argument
270  * of the constructor, but this may be overridden by the model file).
271  **********************************************************************/
272  const std::string& MagneticModelName() const { return _name; }
273 
274  /**
275  * @return directory used to load the magnetic model.
276  **********************************************************************/
277  const std::string& MagneticModelDirectory() const { return _dir; }
278 
279  /**
280  * @return the minimum height above the ellipsoid (in meters) for which
281  * this MagneticModel should be used.
282  *
283  * Because the model will typically provide useful results
284  * slightly outside the range of allowed heights, no check of \e t
285  * argument is made by MagneticModel::operator()() or
286  * MagneticModel::Circle.
287  **********************************************************************/
288  Math::real MinHeight() const { return _hmin; }
289 
290  /**
291  * @return the maximum height above the ellipsoid (in meters) for which
292  * this MagneticModel should be used.
293  *
294  * Because the model will typically provide useful results
295  * slightly outside the range of allowed heights, no check of \e t
296  * argument is made by MagneticModel::operator()() or
297  * MagneticModel::Circle.
298  **********************************************************************/
299  Math::real MaxHeight() const { return _hmax; }
300 
301  /**
302  * @return the minimum time (in years) for which this MagneticModel should
303  * be used.
304  *
305  * Because the model will typically provide useful results
306  * slightly outside the range of allowed times, no check of \e t
307  * argument is made by MagneticModel::operator()() or
308  * MagneticModel::Circle.
309  **********************************************************************/
310  Math::real MinTime() const { return _tmin; }
311 
312  /**
313  * @return the maximum time (in years) for which this MagneticModel should
314  * be used.
315  *
316  * Because the model will typically provide useful results
317  * slightly outside the range of allowed times, no check of \e t
318  * argument is made by MagneticModel::operator()() or
319  * MagneticModel::Circle.
320  **********************************************************************/
321  Math::real MaxTime() const { return _tmax; }
322 
323  /**
324  * @return \e a the equatorial radius of the ellipsoid (meters). This is
325  * the value of \e a inherited from the Geocentric object used in the
326  * constructor.
327  **********************************************************************/
328  Math::real EquatorialRadius() const { return _earth.EquatorialRadius(); }
329 
330  /**
331  * @return \e f the flattening of the ellipsoid. This is the value
332  * inherited from the Geocentric object used in the constructor.
333  **********************************************************************/
334  Math::real Flattening() const { return _earth.Flattening(); }
335 
336  /**
337  * @return \e Nmax the maximum degree of the components of the model.
338  **********************************************************************/
339  int Degree() const { return _nmx; }
340 
341  /**
342  * @return \e Mmax the maximum order of the components of the model.
343  **********************************************************************/
344  int Order() const { return _mmx; }
345 
346  /**
347  * \deprecated An old name for EquatorialRadius().
348  **********************************************************************/
349  // GEOGRAPHICLIB_DEPRECATED("Use EquatorialRadius()")
350  Math::real MajorRadius() const { return EquatorialRadius(); }
351  ///@}
352 
353  /**
354  * @return the default path for magnetic model data files.
355  *
356  * This is the value of the environment variable
357  * GEOGRAPHICLIB_MAGNETIC_PATH, if set; otherwise, it is
358  * $GEOGRAPHICLIB_DATA/magnetic if the environment variable
359  * GEOGRAPHICLIB_DATA is set; otherwise, it is a compile-time default
360  * (/usr/local/share/GeographicLib/magnetic on non-Windows systems and
361  * C:/ProgramData/GeographicLib/magnetic on Windows systems).
362  **********************************************************************/
363  static std::string DefaultMagneticPath();
364 
365  /**
366  * @return the default name for the magnetic model.
367  *
368  * This is the value of the environment variable
369  * GEOGRAPHICLIB_MAGNETIC_NAME, if set; otherwise, it is "wmm2015v2". The
370  * MagneticModel class does not use this function; it is just provided as a
371  * convenience for a calling program when constructing a MagneticModel
372  * object.
373  **********************************************************************/
374  static std::string DefaultMagneticName();
375  };
376 
377 } // namespace GeographicLib
378 
379 #if defined(_MSC_VER)
380 # pragma warning (pop)
381 #endif
382 
383 #endif // GEOGRAPHICLIB_MAGNETICMODEL_HPP
Math::real MinHeight() const
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:92
const std::string & Description() const
Math::real MajorRadius() const
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Math::real EquatorialRadius() const
Definition: Geocentric.hpp:248
Model of the earth&#39;s magnetic field.
Geomagnetic field on a circle of latitude.
static void FieldComponents(real Bx, real By, real Bz, real &H, real &F, real &D, real &I)
void operator()(real t, real lat, real lon, real h, real &Bx, real &By, real &Bz, real &Bxt, real &Byt, real &Bzt) const
const std::string & MagneticModelDirectory() const
Geocentric coordinates
Definition: Geocentric.hpp:67
void operator()(real t, real lat, real lon, real h, real &Bx, real &By, real &Bz) const
static const Geocentric & WGS84()
Definition: Geocentric.cpp:31
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
const std::string & MagneticModelName() const
Header for GeographicLib::Geocentric class.
Math::real Flattening() const
Math::real Flattening() const
Definition: Geocentric.hpp:255
Header for GeographicLib::SphericalHarmonic class.
Math::real EquatorialRadius() const
Math::real MaxHeight() const
Header for GeographicLib::Constants class.
const std::string & MagneticFile() const
const std::string & DateTime() const