GeographicLib 2.4

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