16 #if !defined(GEOGRAPHICLIB_DATA)
18 # define GEOGRAPHICLIB_DATA \
19 "C:/Documents and Settings/All Users/Application Data/GeographicLib"
21 # define GEOGRAPHICLIB_DATA "/usr/local/share/GeographicLib"
25 #if !defined(MAGNETIC_DEFAULT_NAME)
26 # define MAGNETIC_DEFAULT_NAME "wmm2010"
31 # pragma warning (disable: 4996)
34 namespace GeographicLib {
38 MagneticModel::MagneticModel(
const std::string& name,
const std::string& path,
42 , _description(
"NONE")
44 , _t0(
Math::NaN<real>())
46 , _tmin(
Math::NaN<real>())
47 , _tmax(
Math::NaN<real>())
48 , _a(
Math::NaN<real>())
49 , _hmin(
Math::NaN<real>())
50 , _hmax(
Math::NaN<real>())
58 _G.resize(_Nmodels + 1);
59 _H.resize(_Nmodels + 1);
61 string coeff = _filename +
".cof";
62 ifstream coeffstr(coeff.c_str(), ios::binary);
65 char id[idlength_ + 1];
66 coeffstr.read(
id, idlength_);
70 if (_id !=
string(
id))
72 for (
int i = 0; i <= _Nmodels; ++i) {
75 if (!(M < 0 || _G[i][0] == 0))
79 int pos = int(coeffstr.tellg());
80 coeffstr.seekg(0, ios::end);
81 if (pos != coeffstr.tellg())
86 void MagneticModel::ReadMetadata(
const std::string& name) {
87 const char* spaces =
" \t\n\v\f\r";
88 _filename = _dir +
"/" + name +
".wmm";
89 ifstream metastr(_filename.c_str());
93 getline(metastr, line);
94 if (!(line.size() >= 6 && line.substr(0,5) ==
"WMMF-"))
95 throw GeographicErr(_filename +
" does not contain WMMF-n signature");
96 string::size_type n = line.find_first_of(spaces, 5);
97 if (n != string::npos)
99 string version = line.substr(5, n);
101 throw GeographicErr(
"Unknown version in " + _filename +
": " + version);
103 while (getline(metastr, line)) {
109 else if (key ==
"Description")
111 else if (key ==
"ReleaseDate")
113 else if (key ==
"Radius")
114 _a = Utility::num<real>(val);
115 else if (key ==
"Type") {
116 if (!(val ==
"Linear" || val ==
"linear"))
118 }
else if (key ==
"Epoch")
119 _t0 = Utility::num<real>(val);
120 else if (key ==
"DeltaEpoch")
121 _dt0 = Utility::num<real>(val);
122 else if (key ==
"NumModels")
123 _Nmodels = Utility::num<int>(val);
124 else if (key ==
"MinTime")
125 _tmin = Utility::num<real>(val);
126 else if (key ==
"MaxTime")
127 _tmax = Utility::num<real>(val);
128 else if (key ==
"MinHeight")
129 _hmin = Utility::num<real>(val);
130 else if (key ==
"MaxHeight")
131 _hmax = Utility::num<real>(val);
132 else if (key ==
"Normalization") {
133 if (val ==
"FULL" || val ==
"Full" || val ==
"full")
135 else if (val ==
"SCHMIDT" || val ==
"Schmidt" || val ==
"schmidt")
138 throw GeographicErr(
"Unknown normalization " + val);
139 }
else if (key ==
"ByteOrder") {
140 if (val ==
"Big" || val ==
"big")
141 throw GeographicErr(
"Only little-endian ordering is supported");
142 else if (!(val ==
"Little" || val ==
"little"))
143 throw GeographicErr(
"Unknown byte ordering " + val);
144 }
else if (key ==
"ID")
150 throw GeographicErr(
"Reference radius must be positive");
152 throw GeographicErr(
"Epoch time not defined");
154 throw GeographicErr(
"Min time exceeds max time");
156 throw GeographicErr(
"Min height exceeds max height");
157 if (
int(_id.size()) != idlength_)
158 throw GeographicErr(
"Invalid ID");
161 throw GeographicErr(
"DeltaEpoch must be positive");
171 int n = max(min(
int(floor(t / _dt0)), _Nmodels - 1), 0);
172 bool interpolate = n + 1 < _Nmodels;
175 real M[Geocentric::dim2_];
176 _earth.IntForward(lat, lon, h, X, Y, Z, M);
177 real BX0, BY0, BZ0, BX1, BY1, BZ1;
178 _harm[n](X, Y, Z, BX0, BY0, BZ0);
179 _harm[n + 1](X, Y, Z, BX1, BY1, BZ1);
182 BX1 = (BX1 - BX0) / _dt0;
183 BY1 = (BY1 - BY0) / _dt0;
184 BZ1 = (BZ1 - BZ0) / _dt0;
190 Geocentric::Unrotate(M, BX1, BY1, BZ1, Bxt, Byt, Bzt);
195 Geocentric::Unrotate(M, BX0, BY0, BZ0, Bx, By, Bz);
203 int n = max(min(
int(floor(t1 / _dt0)), _Nmodels - 1), 0);
204 bool interpolate = n + 1 < _Nmodels;
206 real X, Y, Z, M[Geocentric::dim2_];
207 _earth.IntForward(lat, 0, h, X, Y, Z, M);
211 M[7], M[8], t1, _dt0, interpolate,
212 _harm[n].Circle(X, Z,
true),
213 _harm[n + 1].Circle(X, Z,
true));
217 real Bxt, real Byt, real Bzt,
218 real& H, real& F, real& D, real& I,
220 real& Dt, real& It)
throw() {
222 Ht = H ? (Bx * Bxt + By * Byt) / H :
Math::hypot(Bxt, Byt);
223 D = (0 - (H ? atan2(-Bx, By) : atan2(-Bxt, Byt))) / Math::degree<real>();
224 Dt = (H ? (By * Bxt - Bx * Byt) /
Math::sq(H) : 0) / Math::degree<real>();
226 Ft = F ? (H * Ht + Bz * Bzt) / F :
Math::hypot(Ht, Bzt);
227 I = (F ? atan2(-Bz, H) : atan2(-Bzt, Ht)) / Math::degree<real>();
228 It = (F ? (Bz * Ht - H * Bzt) /
Math::sq(F) : 0) / Math::degree<real>();
233 char* magneticpath = getenv(
"MAGNETIC_PATH");
235 path = string(magneticpath);
238 char* datapath = getenv(
"GEOGRAPHICLIB_DATA");
240 path = string(datapath);
246 char* magneticname = getenv(
"MAGNETIC_NAME");
248 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.
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
Header for GeographicLib::MagneticModel class.
static std::string DefaultMagneticName()
#define MAGNETIC_DEFAULT_NAME
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.