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