GeographicLib  1.50.1
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-2018) <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 
28 int main(int argc, const char* const argv[]) {
29  try {
30  using namespace GeographicLib;
31  typedef Math::real real;
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 = \""
172  << "\"\nDefault magnetic name = \""
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  const MagneticModel m(model, dir, Geocentric::WGS84(), Nmax, Mmax);
222  if ((timeset || circle)
223  && (!Math::isfinite(time) ||
224  time < m.MinTime() - tguard ||
225  time > m.MaxTime() + tguard))
226  throw GeographicErr("Time " + Utility::str(time) +
227  " too far outside allowed range [" +
228  Utility::str(m.MinTime()) + "," +
229  Utility::str(m.MaxTime()) + "]");
230  if (circle
231  && (!Math::isfinite(h) ||
232  h < m.MinHeight() - hguard ||
233  h > m.MaxHeight() + hguard))
234  throw GeographicErr("Height " + Utility::str(h/1000) +
235  "km too far outside allowed range [" +
236  Utility::str(m.MinHeight()/1000) + "km," +
237  Utility::str(m.MaxHeight()/1000) + "km]");
238  if (verbose) {
239  std::cerr << "Magnetic file: " << m.MagneticFile() << "\n"
240  << "Name: " << m.MagneticModelName() << "\n"
241  << "Description: " << m.Description() << "\n"
242  << "Date & Time: " << m.DateTime() << "\n"
243  << "Time range: ["
244  << m.MinTime() << ","
245  << m.MaxTime() << "]\n"
246  << "Height range: ["
247  << m.MinHeight()/1000 << "km,"
248  << m.MaxHeight()/1000 << "km]\n";
249  }
250  if ((timeset || circle) && (time < m.MinTime() || time > m.MaxTime()))
251  std::cerr << "WARNING: Time " << time
252  << " outside allowed range ["
253  << m.MinTime() << "," << m.MaxTime() << "]\n";
254  if (circle && (h < m.MinHeight() || h > m.MaxHeight()))
255  std::cerr << "WARNING: Height " << h/1000
256  << "km outside allowed range ["
257  << m.MinHeight()/1000 << "km,"
258  << m.MaxHeight()/1000 << "km]\n";
259  const MagneticCircle c(circle ? m.Circle(time, lat, h) :
260  MagneticCircle());
261  std::string s, eol, stra, strb;
262  std::istringstream str;
263  while (std::getline(*input, s)) {
264  try {
265  eol = "\n";
266  if (!cdelim.empty()) {
267  std::string::size_type n = s.find(cdelim);
268  if (n != std::string::npos) {
269  eol = " " + s.substr(n) + "\n";
270  s = s.substr(0, n);
271  }
272  }
273  str.clear(); str.str(s);
274  if (!(timeset || circle)) {
275  if (!(str >> stra))
276  throw GeographicErr("Incomplete input: " + s);
277  time = Utility::fractionalyear<real>(stra);
278  if (time < m.MinTime() - tguard || time > m.MaxTime() + tguard)
279  throw GeographicErr("Time " + Utility::str(time) +
280  " too far outside allowed range [" +
281  Utility::str(m.MinTime()) + "," +
282  Utility::str(m.MaxTime()) +
283  "]");
284  if (time < m.MinTime() || time > m.MaxTime())
285  std::cerr << "WARNING: Time " << time
286  << " outside allowed range ["
287  << m.MinTime() << "," << m.MaxTime() << "]\n";
288  }
289  real lon;
290  if (circle) {
291  if (!(str >> strb))
292  throw GeographicErr("Incomplete input: " + s);
293  DMS::flag ind;
294  lon = DMS::Decode(strb, ind);
295  if (ind == DMS::LATITUDE)
296  throw GeographicErr("Bad hemisphere letter on " + strb);
297  } else {
298  if (!(str >> stra >> strb))
299  throw GeographicErr("Incomplete input: " + s);
300  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
301  h = 0; // h is optional
302  if (str >> h) {
303  if (h < m.MinHeight() - hguard || h > m.MaxHeight() + hguard)
304  throw GeographicErr("Height " + Utility::str(h/1000) +
305  "km too far outside allowed range [" +
306  Utility::str(m.MinHeight()/1000) + "km," +
307  Utility::str(m.MaxHeight()/1000) + "km]");
308  if (h < m.MinHeight() || h > m.MaxHeight())
309  std::cerr << "WARNING: Height " << h/1000
310  << "km outside allowed range ["
311  << m.MinHeight()/1000 << "km,"
312  << m.MaxHeight()/1000 << "km]\n";
313  }
314  else
315  str.clear();
316  }
317  if (str >> stra)
318  throw GeographicErr("Extra junk in input: " + s);
319  real bx, by, bz, bxt, byt, bzt;
320  if (circle)
321  c(lon, bx, by, bz, bxt, byt, bzt);
322  else
323  m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
324  real H, F, D, I, Ht, Ft, Dt, It;
325  MagneticModel::FieldComponents(bx, by, bz, bxt, byt, bzt,
326  H, F, D, I, Ht, Ft, Dt, It);
327 
328  *output << DMS::Encode(D, prec + 1, DMS::NUMBER) << " "
329  << DMS::Encode(I, prec + 1, DMS::NUMBER) << " "
330  << Utility::str(H, prec) << " "
331  << Utility::str(by, prec) << " "
332  << Utility::str(bx, prec) << " "
333  << Utility::str(-bz, prec) << " "
334  << Utility::str(F, prec) << eol;
335  if (rate)
336  *output << DMS::Encode(Dt, prec + 1, DMS::NUMBER) << " "
337  << DMS::Encode(It, prec + 1, DMS::NUMBER) << " "
338  << Utility::str(Ht, prec) << " "
339  << Utility::str(byt, prec) << " "
340  << Utility::str(bxt, prec) << " "
341  << Utility::str(-bzt, prec) << " "
342  << Utility::str(Ft, prec) << eol;
343  }
344  catch (const std::exception& e) {
345  *output << "ERROR: " << e.what() << "\n";
346  retval = 1;
347  }
348  }
349  }
350  catch (const std::exception& e) {
351  std::cerr << "Error reading " << model << ": " << e.what() << "\n";
352  retval = 1;
353  }
354  return retval;
355  }
356  catch (const std::exception& e) {
357  std::cerr << "Caught exception: " << e.what() << "\n";
358  return 1;
359  }
360  catch (...) {
361  std::cerr << "Caught unknown exception\n";
362  return 1;
363  }
364 }
GeographicLib::MagneticModel::DateTime
const std::string & DateTime() const
Definition: MagneticModel.hpp:264
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
GeographicLib::MagneticModel::MinTime
Math::real MinTime() const
Definition: MagneticModel.hpp:313
MagneticModel.hpp
Header for GeographicLib::MagneticModel class.
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: DMS.cpp:256
GeographicLib::MagneticModel
Model of the earth's magnetic field.
Definition: MagneticModel.hpp:75
GeographicLib::MagneticModel::MaxTime
Math::real MaxTime() const
Definition: MagneticModel.hpp:324
GeographicLib::MagneticModel::Description
const std::string & Description() const
Definition: MagneticModel.hpp:258
GeographicLib::DMS::Decode
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
GeographicLib::Math::isfinite
static bool isfinite(T x)
Definition: Math.cpp:372
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:390
GeographicLib::Math::real
double real
Definition: Math.hpp:121
GeographicLib::DMS::NUMBER
@ NUMBER
Definition: DMS.hpp:67
GeographicLib::Math::extra_digits
static int extra_digits()
Definition: Math.cpp:52
GeographicLib::MagneticModel::DefaultMagneticName
static std::string DefaultMagneticName()
Definition: MagneticModel.cpp:273
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition: DMS.cpp:303
GeographicLib::MagneticModel::MagneticModelName
const std::string & MagneticModelName() const
Definition: MagneticModel.hpp:275
GeographicLib::MagneticModel::MaxHeight
Math::real MaxHeight() const
Definition: MagneticModel.hpp:302
Utility.hpp
Header for GeographicLib::Utility class.
main
int main(int argc, const char *const argv[])
Definition: MagneticField.cpp:28
GeographicLib::DMS::flag
flag
Definition: DMS.hpp:41
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition: DMS.hpp:56
GeographicLib::MagneticModel::FieldComponents
static void FieldComponents(real Bx, real By, real Bz, real &H, real &F, real &D, real &I)
Definition: MagneticModel.hpp:217
GeographicLib::MagneticModel::MagneticFile
const std::string & MagneticFile() const
Definition: MagneticModel.hpp:269
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition: DMS.hpp:51
MagneticCircle.hpp
Header for GeographicLib::MagneticCircle class.
GeographicLib::MagneticModel::Circle
MagneticCircle Circle(real t, real lat, real h) const
Definition: MagneticModel.cpp:224
GeographicLib::MagneticModel::MinHeight
Math::real MinHeight() const
Definition: MagneticModel.hpp:291
GeographicLib::Geocentric::WGS84
static const Geocentric & WGS84()
Definition: Geocentric.cpp:31
GeographicLib::Utility::set_digits
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
GeographicLib::MagneticCircle
Geomagnetic field on a circle of latitude.
Definition: MagneticCircle.hpp:37
DMS.hpp
Header for GeographicLib::DMS class.
GeographicLib::MagneticModel::DefaultMagneticPath
static std::string DefaultMagneticPath()
Definition: MagneticModel.cpp:260