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