GeographicLib 1.52
Loading...
Searching...
No Matches
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-2020) <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 expressions
23# pragma warning (disable: 4127)
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::abs;
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 (!(abs(lat) <= 90))
99 throw GeographicErr("Latitude not in [-90d, 90d]");
100 h = Utility::val<real>(std::string(argv[++m]));
101 timeset = false;
102 circle = true;
103 }
104 catch (const std::exception& e) {
105 std::cerr << "Error decoding argument of " << arg << ": "
106 << e.what() << "\n";
107 return 1;
108 }
109 } else if (arg == "-r")
110 rate = !rate;
111 else if (arg == "-w")
112 longfirst = !longfirst;
113 else if (arg == "-p") {
114 if (++m == argc) return usage(1, true);
115 try {
116 prec = Utility::val<int>(std::string(argv[m]));
117 }
118 catch (const std::exception&) {
119 std::cerr << "Precision " << argv[m] << " is not a number\n";
120 return 1;
121 }
122 } else if (arg == "-T") {
123 if (++m == argc) return usage(1, true);
124 try {
125 tguard = Utility::val<real>(std::string(argv[m]));
126 }
127 catch (const std::exception& e) {
128 std::cerr << "Error decoding argument of " << arg << ": "
129 << e.what() << "\n";
130 return 1;
131 }
132 } else if (arg == "-H") {
133 if (++m == argc) return usage(1, true);
134 try {
135 hguard = Utility::val<real>(std::string(argv[m]));
136 }
137 catch (const std::exception& e) {
138 std::cerr << "Error decoding argument of " << arg << ": "
139 << e.what() << "\n";
140 return 1;
141 }
142 } else if (arg == "-v")
143 verbose = true;
144 else if (arg == "--input-string") {
145 if (++m == argc) return usage(1, true);
146 istring = argv[m];
147 } else if (arg == "--input-file") {
148 if (++m == argc) return usage(1, true);
149 ifile = argv[m];
150 } else if (arg == "--output-file") {
151 if (++m == argc) return usage(1, true);
152 ofile = argv[m];
153 } else if (arg == "--line-separator") {
154 if (++m == argc) return usage(1, true);
155 if (std::string(argv[m]).size() != 1) {
156 std::cerr << "Line separator must be a single character\n";
157 return 1;
158 }
159 lsep = argv[m][0];
160 } else if (arg == "--comment-delimiter") {
161 if (++m == argc) return usage(1, true);
162 cdelim = argv[m];
163 } else if (arg == "--version") {
164 std::cout << argv[0] << ": GeographicLib version "
165 << GEOGRAPHICLIB_VERSION_STRING << "\n";
166 return 0;
167 } else {
168 int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
169 if (arg == "-h")
170 std::cout<< "\nDefault magnetic path = \""
171 << MagneticModel::DefaultMagneticPath()
172 << "\"\nDefault magnetic name = \""
173 << MagneticModel::DefaultMagneticName()
174 << "\"\n";
175 return retval;
176 }
177 }
178
179 if (!ifile.empty() && !istring.empty()) {
180 std::cerr << "Cannot specify --input-string and --input-file together\n";
181 return 1;
182 }
183 if (ifile == "-") ifile.clear();
184 std::ifstream infile;
185 std::istringstream instring;
186 if (!ifile.empty()) {
187 infile.open(ifile.c_str());
188 if (!infile.is_open()) {
189 std::cerr << "Cannot open " << ifile << " for reading\n";
190 return 1;
191 }
192 } else if (!istring.empty()) {
193 std::string::size_type m = 0;
194 while (true) {
195 m = istring.find(lsep, m);
196 if (m == std::string::npos)
197 break;
198 istring[m] = '\n';
199 }
200 instring.str(istring);
201 }
202 std::istream* input = !ifile.empty() ? &infile :
203 (!istring.empty() ? &instring : &std::cin);
204
205 std::ofstream outfile;
206 if (ofile == "-") ofile.clear();
207 if (!ofile.empty()) {
208 outfile.open(ofile.c_str());
209 if (!outfile.is_open()) {
210 std::cerr << "Cannot open " << ofile << " for writing\n";
211 return 1;
212 }
213 }
214 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
215
216 tguard = std::max(real(0), tguard);
217 hguard = std::max(real(0), hguard);
218 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
219 int retval = 0;
220 try {
221 using std::isfinite;
222 const MagneticModel m(model, dir, Geocentric::WGS84(), Nmax, Mmax);
223 if ((timeset || circle)
224 && (!isfinite(time) ||
225 time < m.MinTime() - tguard ||
226 time > m.MaxTime() + tguard))
227 throw GeographicErr("Time " + Utility::str(time) +
228 " too far outside allowed range [" +
229 Utility::str(m.MinTime()) + "," +
230 Utility::str(m.MaxTime()) + "]");
231 if (circle
232 && (!isfinite(h) ||
233 h < m.MinHeight() - hguard ||
234 h > m.MaxHeight() + hguard))
235 throw GeographicErr("Height " + Utility::str(h/1000) +
236 "km too far outside allowed range [" +
237 Utility::str(m.MinHeight()/1000) + "km," +
238 Utility::str(m.MaxHeight()/1000) + "km]");
239 if (verbose) {
240 std::cerr << "Magnetic file: " << m.MagneticFile() << "\n"
241 << "Name: " << m.MagneticModelName() << "\n"
242 << "Description: " << m.Description() << "\n"
243 << "Date & Time: " << m.DateTime() << "\n"
244 << "Time range: ["
245 << m.MinTime() << ","
246 << m.MaxTime() << "]\n"
247 << "Height range: ["
248 << m.MinHeight()/1000 << "km,"
249 << m.MaxHeight()/1000 << "km]\n";
250 }
251 if ((timeset || circle) && (time < m.MinTime() || time > m.MaxTime()))
252 std::cerr << "WARNING: Time " << time
253 << " outside allowed range ["
254 << m.MinTime() << "," << m.MaxTime() << "]\n";
255 if (circle && (h < m.MinHeight() || h > m.MaxHeight()))
256 std::cerr << "WARNING: Height " << h/1000
257 << "km outside allowed range ["
258 << m.MinHeight()/1000 << "km,"
259 << m.MaxHeight()/1000 << "km]\n";
260 const MagneticCircle c(circle ? m.Circle(time, lat, h) :
262 std::string s, eol, stra, strb;
263 std::istringstream str;
264 while (std::getline(*input, s)) {
265 try {
266 eol = "\n";
267 if (!cdelim.empty()) {
268 std::string::size_type n = s.find(cdelim);
269 if (n != std::string::npos) {
270 eol = " " + s.substr(n) + "\n";
271 s = s.substr(0, n);
272 }
273 }
274 str.clear(); str.str(s);
275 if (!(timeset || circle)) {
276 if (!(str >> stra))
277 throw GeographicErr("Incomplete input: " + s);
278 time = Utility::fractionalyear<real>(stra);
279 if (time < m.MinTime() - tguard || time > m.MaxTime() + tguard)
280 throw GeographicErr("Time " + Utility::str(time) +
281 " too far outside allowed range [" +
282 Utility::str(m.MinTime()) + "," +
283 Utility::str(m.MaxTime()) +
284 "]");
285 if (time < m.MinTime() || time > m.MaxTime())
286 std::cerr << "WARNING: Time " << time
287 << " outside allowed range ["
288 << m.MinTime() << "," << m.MaxTime() << "]\n";
289 }
290 real lon;
291 if (circle) {
292 if (!(str >> strb))
293 throw GeographicErr("Incomplete input: " + s);
294 DMS::flag ind;
295 lon = DMS::Decode(strb, ind);
296 if (ind == DMS::LATITUDE)
297 throw GeographicErr("Bad hemisphere letter on " + strb);
298 } else {
299 if (!(str >> stra >> strb))
300 throw GeographicErr("Incomplete input: " + s);
301 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
302 h = 0; // h is optional
303 if (str >> h) {
304 if (h < m.MinHeight() - hguard || h > m.MaxHeight() + hguard)
305 throw GeographicErr("Height " + Utility::str(h/1000) +
306 "km too far outside allowed range [" +
307 Utility::str(m.MinHeight()/1000) + "km," +
308 Utility::str(m.MaxHeight()/1000) + "km]");
309 if (h < m.MinHeight() || h > m.MaxHeight())
310 std::cerr << "WARNING: Height " << h/1000
311 << "km outside allowed range ["
312 << m.MinHeight()/1000 << "km,"
313 << m.MaxHeight()/1000 << "km]\n";
314 }
315 else
316 str.clear();
317 }
318 if (str >> stra)
319 throw GeographicErr("Extra junk in input: " + s);
320 real bx, by, bz, bxt, byt, bzt;
321 if (circle)
322 c(lon, bx, by, bz, bxt, byt, bzt);
323 else
324 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
325 real H, F, D, I, Ht, Ft, Dt, It;
326 MagneticModel::FieldComponents(bx, by, bz, bxt, byt, bzt,
327 H, F, D, I, Ht, Ft, Dt, It);
328
329 *output << DMS::Encode(D, prec + 1, DMS::NUMBER) << " "
330 << DMS::Encode(I, prec + 1, DMS::NUMBER) << " "
331 << Utility::str(H, prec) << " "
332 << Utility::str(by, prec) << " "
333 << Utility::str(bx, prec) << " "
334 << Utility::str(-bz, prec) << " "
335 << Utility::str(F, prec) << eol;
336 if (rate)
337 *output << DMS::Encode(Dt, prec + 1, DMS::NUMBER) << " "
338 << DMS::Encode(It, prec + 1, DMS::NUMBER) << " "
339 << Utility::str(Ht, prec) << " "
340 << Utility::str(byt, prec) << " "
341 << Utility::str(bxt, prec) << " "
342 << Utility::str(-bzt, prec) << " "
343 << Utility::str(Ft, prec) << eol;
344 }
345 catch (const std::exception& e) {
346 *output << "ERROR: " << e.what() << "\n";
347 retval = 1;
348 }
349 }
350 }
351 catch (const std::exception& e) {
352 std::cerr << "Error reading " << model << ": " << e.what() << "\n";
353 retval = 1;
354 }
355 return retval;
356 }
357 catch (const std::exception& e) {
358 std::cerr << "Caught exception: " << e.what() << "\n";
359 return 1;
360 }
361 catch (...) {
362 std::cerr << "Caught unknown exception\n";
363 return 1;
364 }
365}
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:315
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