16 #if !defined(GEOGRAPHICLIB_DATA)
18 # define GEOGRAPHICLIB_DATA "C:/ProgramData/GeographicLib"
20 # define GEOGRAPHICLIB_DATA "/usr/local/share/GeographicLib"
24 #if !defined(GEOGRAPHICLIB_MAGNETIC_DEFAULT_NAME)
25 # define GEOGRAPHICLIB_MAGNETIC_DEFAULT_NAME "wmm2010"
30 # pragma warning (disable: 4996)
37 MagneticModel::MagneticModel(
const std::string& name,
const std::string& path,
41 , _description(
"NONE")
57 _G.resize(_Nmodels + 1);
58 _H.resize(_Nmodels + 1);
60 string coeff = _filename +
".cof";
61 ifstream coeffstr(coeff.c_str(), ios::binary);
64 char id[idlength_ + 1];
65 coeffstr.read(
id, idlength_);
69 if (_id !=
string(
id))
71 for (
int i = 0; i <= _Nmodels; ++i) {
74 if (!(M < 0 || _G[i][0] == 0))
78 int pos = int(coeffstr.tellg());
79 coeffstr.seekg(0, ios::end);
80 if (pos != coeffstr.tellg())
85 void MagneticModel::ReadMetadata(
const std::string& name) {
86 const char* spaces =
" \t\n\v\f\r";
87 _filename = _dir +
"/" + name +
".wmm";
88 ifstream metastr(_filename.c_str());
92 getline(metastr, line);
93 if (!(line.size() >= 6 && line.substr(0,5) ==
"WMMF-"))
94 throw GeographicErr(_filename +
" does not contain WMMF-n signature");
95 string::size_type n = line.find_first_of(spaces, 5);
96 if (n != string::npos)
98 string version = line.substr(5, n);
100 throw GeographicErr(
"Unknown version in " + _filename +
": " + version);
102 while (getline(metastr, line)) {
108 else if (key ==
"Description")
110 else if (key ==
"ReleaseDate")
112 else if (key ==
"Radius")
113 _a = Utility::num<real>(val);
114 else if (key ==
"Type") {
115 if (!(val ==
"Linear" || val ==
"linear"))
117 }
else if (key ==
"Epoch")
118 _t0 = Utility::num<real>(val);
119 else if (key ==
"DeltaEpoch")
120 _dt0 = Utility::num<real>(val);
121 else if (key ==
"NumModels")
122 _Nmodels = Utility::num<int>(val);
123 else if (key ==
"MinTime")
124 _tmin = Utility::num<real>(val);
125 else if (key ==
"MaxTime")
126 _tmax = Utility::num<real>(val);
127 else if (key ==
"MinHeight")
128 _hmin = Utility::num<real>(val);
129 else if (key ==
"MaxHeight")
130 _hmax = Utility::num<real>(val);
131 else if (key ==
"Normalization") {
132 if (val ==
"FULL" || val ==
"Full" || val ==
"full")
134 else if (val ==
"SCHMIDT" || val ==
"Schmidt" || val ==
"schmidt")
137 throw GeographicErr(
"Unknown normalization " + val);
138 }
else if (key ==
"ByteOrder") {
139 if (val ==
"Big" || val ==
"big")
140 throw GeographicErr(
"Only little-endian ordering is supported");
141 else if (!(val ==
"Little" || val ==
"little"))
142 throw GeographicErr(
"Unknown byte ordering " + val);
143 }
else if (key ==
"ID")
149 throw GeographicErr(
"Reference radius must be positive");
151 throw GeographicErr(
"Epoch time not defined");
153 throw GeographicErr(
"Min time exceeds max time");
155 throw GeographicErr(
"Min height exceeds max height");
156 if (
int(_id.size()) != idlength_)
157 throw GeographicErr(
"Invalid ID");
160 throw GeographicErr(
"DeltaEpoch must be positive");
170 int n = max(min(
int(floor(t / _dt0)), _Nmodels - 1), 0);
171 bool interpolate = n + 1 < _Nmodels;
174 real M[Geocentric::dim2_];
175 _earth.IntForward(lat, lon, h, X, Y, Z, M);
176 real BX0, BY0, BZ0, BX1, BY1, BZ1;
177 _harm[n](X, Y, Z, BX0, BY0, BZ0);
178 _harm[n + 1](X, Y, Z, BX1, BY1, BZ1);
181 BX1 = (BX1 - BX0) / _dt0;
182 BY1 = (BY1 - BY0) / _dt0;
183 BZ1 = (BZ1 - BZ0) / _dt0;
189 Geocentric::Unrotate(M, BX1, BY1, BZ1, Bxt, Byt, Bzt);
194 Geocentric::Unrotate(M, BX0, BY0, BZ0, Bx, By, Bz);
202 int n = max(min(
int(floor(t1 / _dt0)), _Nmodels - 1), 0);
203 bool interpolate = n + 1 < _Nmodels;
205 real X, Y, Z, M[Geocentric::dim2_];
206 _earth.IntForward(lat, 0, h, X, Y, Z, M);
210 M[7], M[8], t1, _dt0, interpolate,
211 _harm[n].Circle(X, Z,
true),
212 _harm[n + 1].Circle(X, Z,
true));
216 real Bxt, real Byt, real Bzt,
217 real& H, real& F, real& D, real& I,
219 real& Dt, real& It) {
221 Ht = H ? (Bx * Bxt + By * Byt) / H :
Math::hypot(Bxt, Byt);
222 D = (0 - (H ? atan2(-Bx, By) : atan2(-Bxt, Byt))) /
Math::degree();
225 Ft = F ? (H * Ht + Bz * Bzt) / F :
Math::hypot(Ht, Bzt);
226 I = (F ? atan2(-Bz, H) : atan2(-Bzt, Ht)) /
Math::degree();
232 char* magneticpath = getenv(
"GEOGRAPHICLIB_MAGNETIC_PATH");
234 path = string(magneticpath);
237 char* datapath = getenv(
"GEOGRAPHICLIB_DATA");
239 path = string(datapath);
245 char* magneticname = getenv(
"GEOGRAPHICLIB_MAGNETIC_NAME");
247 name = string(magneticname);
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
static bool isfinite(T x)
Header for GeographicLib::MagneticCircle class.
Mathematical functions needed by GeographicLib.
#define GEOGRAPHICLIB_MAGNETIC_DEFAULT_NAME
Geomagnetic field on a circle of latitude.
MagneticCircle Circle(real t, real lat, real h) const
static void FieldComponents(real Bx, real By, real Bz, real &H, real &F, real &D, real &I)
static void readcoeffs(std::istream &stream, int &N, int &M, std::vector< real > &C, std::vector< real > &S)
#define GEOGRAPHICLIB_DATA
Namespace for GeographicLib.
Header for GeographicLib::MagneticModel class.
static std::string DefaultMagneticName()
Exception handling for GeographicLib.
static std::string DefaultMagneticPath()
Spherical harmonic series.
static bool ParseLine(const std::string &line, std::string &key, std::string &val)
Header for GeographicLib::SphericalEngine class.