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