GeographicLib 2.1.2
MagneticField.cpp
Go to the documentation of this file.
1/**
2 * \file MagneticField.cpp
3 * \brief Command line utility for evaluating magnetic fields
4 *
5 * Copyright (c) Charles Karney (2011-2022) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * See the <a href="MagneticField.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
18#include <GeographicLib/DMS.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about constant conditional and enum-float expressions
23# pragma warning (disable: 4127 5055)
24#endif
25
26#include "MagneticField.usage"
27
28int main(int argc, const char* const argv[]) {
29 try {
30 using namespace GeographicLib;
31 typedef Math::real real;
32 Utility::set_digits();
33 bool verbose = false, longfirst = false;
34 std::string dir;
35 std::string model = MagneticModel::DefaultMagneticName();
36 std::string istring, ifile, ofile, cdelim;
37 char lsep = ';';
38 real time = 0, lat = 0, h = 0;
39 bool timeset = false, circle = false, rate = false;
40 real hguard = 500000, tguard = 50;
41 int prec = 1, Nmax = -1, Mmax = -1;
42
43 for (int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
45 if (arg == "-n") {
46 if (++m == argc) return usage(1, true);
47 model = argv[m];
48 } else if (arg == "-d") {
49 if (++m == argc) return usage(1, true);
50 dir = argv[m];
51 } else if (arg == "-N") {
52 if (++m == argc) return usage(1, true);
53 try {
54 Nmax = Utility::val<int>(std::string(argv[m]));
55 if (Nmax < 0) {
56 std::cerr << "Maximum degree " << argv[m] << " is negative\n";
57 return 1;
58 }
59 }
60 catch (const std::exception&) {
61 std::cerr << "Precision " << argv[m] << " is not a number\n";
62 return 1;
63 }
64 } else if (arg == "-M") {
65 if (++m == argc) return usage(1, true);
66 try {
67 Mmax = Utility::val<int>(std::string(argv[m]));
68 if (Mmax < 0) {
69 std::cerr << "Maximum order " << argv[m] << " is negative\n";
70 return 1;
71 }
72 }
73 catch (const std::exception&) {
74 std::cerr << "Precision " << argv[m] << " is not a number\n";
75 return 1;
76 }
77 } else if (arg == "-t") {
78 if (++m == argc) return usage(1, true);
79 try {
80 time = Utility::fractionalyear<real>(std::string(argv[m]));
81 timeset = true;
82 circle = false;
83 }
84 catch (const std::exception& e) {
85 std::cerr << "Error decoding argument of " << arg << ": "
86 << e.what() << "\n";
87 return 1;
88 }
89 } else if (arg == "-c") {
90 if (m + 3 >= argc) return usage(1, true);
91 try {
92 using std::fabs;
93 time = Utility::fractionalyear<real>(std::string(argv[++m]));
94 DMS::flag ind;
95 lat = DMS::Decode(std::string(argv[++m]), ind);
96 if (ind == DMS::LONGITUDE)
97 throw GeographicErr("Bad hemisphere letter on latitude");
98 if (!(fabs(lat) <= Math::qd))
99 throw GeographicErr("Latitude not in [-" + std::to_string(Math::qd)
100 + "d, " + std::to_string(Math::qd) + "d]");
101 h = Utility::val<real>(std::string(argv[++m]));
102 timeset = false;
103 circle = true;
104 }
105 catch (const std::exception& e) {
106 std::cerr << "Error decoding argument of " << arg << ": "
107 << e.what() << "\n";
108 return 1;
109 }
110 } else if (arg == "-r")
111 rate = !rate;
112 else if (arg == "-w")
113 longfirst = !longfirst;
114 else if (arg == "-p") {
115 if (++m == argc) return usage(1, true);
116 try {
117 prec = Utility::val<int>(std::string(argv[m]));
118 }
119 catch (const std::exception&) {
120 std::cerr << "Precision " << argv[m] << " is not a number\n";
121 return 1;
122 }
123 } else if (arg == "-T") {
124 if (++m == argc) return usage(1, true);
125 try {
126 tguard = Utility::val<real>(std::string(argv[m]));
127 }
128 catch (const std::exception& e) {
129 std::cerr << "Error decoding argument of " << arg << ": "
130 << e.what() << "\n";
131 return 1;
132 }
133 } else if (arg == "-H") {
134 if (++m == argc) return usage(1, true);
135 try {
136 hguard = Utility::val<real>(std::string(argv[m]));
137 }
138 catch (const std::exception& e) {
139 std::cerr << "Error decoding argument of " << arg << ": "
140 << e.what() << "\n";
141 return 1;
142 }
143 } else if (arg == "-v")
144 verbose = true;
145 else if (arg == "--input-string") {
146 if (++m == argc) return usage(1, true);
147 istring = argv[m];
148 } else if (arg == "--input-file") {
149 if (++m == argc) return usage(1, true);
150 ifile = argv[m];
151 } else if (arg == "--output-file") {
152 if (++m == argc) return usage(1, true);
153 ofile = argv[m];
154 } else if (arg == "--line-separator") {
155 if (++m == argc) return usage(1, true);
156 if (std::string(argv[m]).size() != 1) {
157 std::cerr << "Line separator must be a single character\n";
158 return 1;
159 }
160 lsep = argv[m][0];
161 } else if (arg == "--comment-delimiter") {
162 if (++m == argc) return usage(1, true);
163 cdelim = argv[m];
164 } else if (arg == "--version") {
165 std::cout << argv[0] << ": GeographicLib version "
166 << GEOGRAPHICLIB_VERSION_STRING << "\n";
167 return 0;
168 } else {
169 int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
170 if (arg == "-h")
171 std::cout<< "\nDefault magnetic path = \""
172 << MagneticModel::DefaultMagneticPath()
173 << "\"\nDefault magnetic name = \""
174 << MagneticModel::DefaultMagneticName()
175 << "\"\n";
176 return retval;
177 }
178 }
179
180 if (!ifile.empty() && !istring.empty()) {
181 std::cerr << "Cannot specify --input-string and --input-file together\n";
182 return 1;
183 }
184 if (ifile == "-") ifile.clear();
185 std::ifstream infile;
186 std::istringstream instring;
187 if (!ifile.empty()) {
188 infile.open(ifile.c_str());
189 if (!infile.is_open()) {
190 std::cerr << "Cannot open " << ifile << " for reading\n";
191 return 1;
192 }
193 } else if (!istring.empty()) {
194 std::string::size_type m = 0;
195 while (true) {
196 m = istring.find(lsep, m);
197 if (m == std::string::npos)
198 break;
199 istring[m] = '\n';
200 }
201 instring.str(istring);
202 }
203 std::istream* input = !ifile.empty() ? &infile :
204 (!istring.empty() ? &instring : &std::cin);
205
206 std::ofstream outfile;
207 if (ofile == "-") ofile.clear();
208 if (!ofile.empty()) {
209 outfile.open(ofile.c_str());
210 if (!outfile.is_open()) {
211 std::cerr << "Cannot open " << ofile << " for writing\n";
212 return 1;
213 }
214 }
215 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
216
217 using std::fmax;
218 tguard = fmax(real(0), tguard);
219 hguard = fmax(real(0), hguard);
220 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
221 int retval = 0;
222 try {
223 using std::isfinite;
224 const MagneticModel m(model, dir, Geocentric::WGS84(), Nmax, Mmax);
225 if ((timeset || circle)
226 && (!isfinite(time) ||
227 time < m.MinTime() - tguard ||
228 time > m.MaxTime() + tguard))
229 throw GeographicErr("Time " + Utility::str(time) +
230 " too far outside allowed range [" +
231 Utility::str(m.MinTime()) + "," +
232 Utility::str(m.MaxTime()) + "]");
233 if (circle
234 && (!isfinite(h) ||
235 h < m.MinHeight() - hguard ||
236 h > m.MaxHeight() + hguard))
237 throw GeographicErr("Height " + Utility::str(h/1000) +
238 "km too far outside allowed range [" +
239 Utility::str(m.MinHeight()/1000) + "km," +
240 Utility::str(m.MaxHeight()/1000) + "km]");
241 if (verbose) {
242 std::cerr << "Magnetic file: " << m.MagneticFile() << "\n"
243 << "Name: " << m.MagneticModelName() << "\n"
244 << "Description: " << m.Description() << "\n"
245 << "Date & Time: " << m.DateTime() << "\n"
246 << "Time range: ["
247 << m.MinTime() << ","
248 << m.MaxTime() << "]\n"
249 << "Height range: ["
250 << m.MinHeight()/1000 << "km,"
251 << m.MaxHeight()/1000 << "km]\n";
252 }
253 if ((timeset || circle) && (time < m.MinTime() || time > m.MaxTime()))
254 std::cerr << "WARNING: Time " << time
255 << " outside allowed range ["
256 << m.MinTime() << "," << m.MaxTime() << "]\n";
257 if (circle && (h < m.MinHeight() || h > m.MaxHeight()))
258 std::cerr << "WARNING: Height " << h/1000
259 << "km outside allowed range ["
260 << m.MinHeight()/1000 << "km,"
261 << m.MaxHeight()/1000 << "km]\n";
262 const MagneticCircle c(circle ? m.Circle(time, lat, h) :
264 std::string s, eol, stra, strb;
265 std::istringstream str;
266 while (std::getline(*input, s)) {
267 try {
268 eol = "\n";
269 if (!cdelim.empty()) {
270 std::string::size_type n = s.find(cdelim);
271 if (n != std::string::npos) {
272 eol = " " + s.substr(n) + "\n";
273 s = s.substr(0, n);
274 }
275 }
276 str.clear(); str.str(s);
277 if (!(timeset || circle)) {
278 if (!(str >> stra))
279 throw GeographicErr("Incomplete input: " + s);
280 time = Utility::fractionalyear<real>(stra);
281 if (time < m.MinTime() - tguard || time > m.MaxTime() + tguard)
282 throw GeographicErr("Time " + Utility::str(time) +
283 " too far outside allowed range [" +
284 Utility::str(m.MinTime()) + "," +
285 Utility::str(m.MaxTime()) +
286 "]");
287 if (time < m.MinTime() || time > m.MaxTime())
288 std::cerr << "WARNING: Time " << time
289 << " outside allowed range ["
290 << m.MinTime() << "," << m.MaxTime() << "]\n";
291 }
292 real lon;
293 if (circle) {
294 if (!(str >> strb))
295 throw GeographicErr("Incomplete input: " + s);
296 DMS::flag ind;
297 lon = DMS::Decode(strb, ind);
298 if (ind == DMS::LATITUDE)
299 throw GeographicErr("Bad hemisphere letter on " + strb);
300 } else {
301 if (!(str >> stra >> strb))
302 throw GeographicErr("Incomplete input: " + s);
303 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
304 h = 0; // h is optional
305 if (str >> h) {
306 if (h < m.MinHeight() - hguard || h > m.MaxHeight() + hguard)
307 throw GeographicErr("Height " + Utility::str(h/1000) +
308 "km too far outside allowed range [" +
309 Utility::str(m.MinHeight()/1000) + "km," +
310 Utility::str(m.MaxHeight()/1000) + "km]");
311 if (h < m.MinHeight() || h > m.MaxHeight())
312 std::cerr << "WARNING: Height " << h/1000
313 << "km outside allowed range ["
314 << m.MinHeight()/1000 << "km,"
315 << m.MaxHeight()/1000 << "km]\n";
316 }
317 else
318 str.clear();
319 }
320 if (str >> stra)
321 throw GeographicErr("Extra junk in input: " + s);
322 real bx, by, bz, bxt, byt, bzt;
323 if (circle)
324 c(lon, bx, by, bz, bxt, byt, bzt);
325 else
326 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
327 real H, F, D, I, Ht, Ft, Dt, It;
328 MagneticModel::FieldComponents(bx, by, bz, bxt, byt, bzt,
329 H, F, D, I, Ht, Ft, Dt, It);
330
331 *output << DMS::Encode(D, prec + 1, DMS::NUMBER) << " "
332 << DMS::Encode(I, prec + 1, DMS::NUMBER) << " "
333 << Utility::str(H, prec) << " "
334 << Utility::str(by, prec) << " "
335 << Utility::str(bx, prec) << " "
336 << Utility::str(-bz, prec) << " "
337 << Utility::str(F, prec) << eol;
338 if (rate)
339 *output << DMS::Encode(Dt, prec + 1, DMS::NUMBER) << " "
340 << DMS::Encode(It, prec + 1, DMS::NUMBER) << " "
341 << Utility::str(Ht, prec) << " "
342 << Utility::str(byt, prec) << " "
343 << Utility::str(bxt, prec) << " "
344 << Utility::str(-bzt, prec) << " "
345 << Utility::str(Ft, prec) << eol;
346 }
347 catch (const std::exception& e) {
348 *output << "ERROR: " << e.what() << "\n";
349 retval = 1;
350 }
351 }
352 }
353 catch (const std::exception& e) {
354 std::cerr << "Error reading " << model << ": " << e.what() << "\n";
355 retval = 1;
356 }
357 return retval;
358 }
359 catch (const std::exception& e) {
360 std::cerr << "Caught exception: " << e.what() << "\n";
361 return 1;
362 }
363 catch (...) {
364 std::cerr << "Caught unknown exception\n";
365 return 1;
366 }
367}
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::MagneticCircle class.
int main(int argc, const char *const argv[])
Header for GeographicLib::MagneticModel class.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Geomagnetic field on a circle of latitude.
Model of the earth's magnetic field.
MagneticCircle Circle(real t, real lat, real h) const
const std::string & Description() const
const std::string & MagneticFile() const
Math::real MaxHeight() const
const std::string & DateTime() const
const std::string & MagneticModelName() const
Math::real MinHeight() const
Namespace for GeographicLib.
Definition: Accumulator.cpp:12