21 #include "MagneticField.usage" 23 int main(
int argc,
const char*
const argv[]) {
28 bool verbose =
false, longfirst =
false;
31 std::string istring, ifile, ofile, cdelim;
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;
38 for (
int m = 1; m < argc; ++m) {
39 std::string arg(argv[m]);
41 if (++m == argc)
return usage(1,
true);
43 }
else if (arg ==
"-d") {
44 if (++m == argc)
return usage(1,
true);
46 }
else if (arg ==
"-N") {
47 if (++m == argc)
return usage(1,
true);
49 Nmax = Utility::val<int>(std::string(argv[m]));
51 std::cerr <<
"Maximum degree " << argv[m] <<
" is negative\n";
55 catch (
const std::exception&) {
56 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
59 }
else if (arg ==
"-M") {
60 if (++m == argc)
return usage(1,
true);
62 Mmax = Utility::val<int>(std::string(argv[m]));
64 std::cerr <<
"Maximum order " << argv[m] <<
" is negative\n";
68 catch (
const std::exception&) {
69 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
72 }
else if (arg ==
"-t") {
73 if (++m == argc)
return usage(1,
true);
75 time = Utility::fractionalyear<real>(std::string(argv[m]));
79 catch (
const std::exception& e) {
80 std::cerr <<
"Error decoding argument of " << arg <<
": " 84 }
else if (arg ==
"-c") {
85 if (m + 3 >= argc)
return usage(1,
true);
88 time = Utility::fractionalyear<real>(std::string(argv[++m]));
95 +
"d, " + std::to_string(
Math::qd) +
"d]");
96 h = Utility::val<real>(std::string(argv[++m]));
100 catch (
const std::exception& e) {
101 std::cerr <<
"Error decoding argument of " << arg <<
": " 105 }
else if (arg ==
"-r")
107 else if (arg ==
"-w")
108 longfirst = !longfirst;
109 else if (arg ==
"-p") {
110 if (++m == argc)
return usage(1,
true);
112 prec = Utility::val<int>(std::string(argv[m]));
114 catch (
const std::exception&) {
115 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
118 }
else if (arg ==
"-T") {
119 if (++m == argc)
return usage(1,
true);
121 tguard = Utility::val<real>(std::string(argv[m]));
123 catch (
const std::exception& e) {
124 std::cerr <<
"Error decoding argument of " << arg <<
": " 128 }
else if (arg ==
"-H") {
129 if (++m == argc)
return usage(1,
true);
131 hguard = Utility::val<real>(std::string(argv[m]));
133 catch (
const std::exception& e) {
134 std::cerr <<
"Error decoding argument of " << arg <<
": " 138 }
else if (arg ==
"-v")
140 else if (arg ==
"--input-string") {
141 if (++m == argc)
return usage(1,
true);
143 }
else if (arg ==
"--input-file") {
144 if (++m == argc)
return usage(1,
true);
146 }
else if (arg ==
"--output-file") {
147 if (++m == argc)
return usage(1,
true);
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";
156 }
else if (arg ==
"--comment-delimiter") {
157 if (++m == argc)
return usage(1,
true);
159 }
else if (arg ==
"--version") {
160 std::cout << argv[0] <<
": GeographicLib version " 161 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
164 int retval =
usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
166 std::cout<<
"\nDefault magnetic path = \"" 168 <<
"\"\nDefault magnetic name = \"" 175 if (!ifile.empty() && !istring.empty()) {
176 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
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";
188 }
else if (!istring.empty()) {
189 std::string::size_type m = 0;
191 m = istring.find(lsep, m);
192 if (m == std::string::npos)
196 instring.str(istring);
198 std::istream* input = !ifile.empty() ? &infile :
199 (!istring.empty() ? &instring : &std::cin);
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";
210 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
213 tguard = fmax(
real(0), tguard);
214 hguard = fmax(
real(0), hguard);
220 if ((timeset || circle)
221 && (!isfinite(time) ||
225 " too far outside allowed range [" +
233 "km too far outside allowed range [" +
237 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n" 240 <<
"Date & Time: " << m.
DateTime() <<
"\n" 248 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
249 std::cerr <<
"WARNING: Time " << time
250 <<
" outside allowed range [" 253 std::cerr <<
"WARNING: Height " << h/1000
254 <<
"km outside allowed range [" 259 std::string s, eol, stra, strb;
260 std::istringstream str;
261 while (std::getline(*input, s)) {
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";
271 str.clear(); str.str(s);
272 if (!(timeset || circle)) {
275 time = Utility::fractionalyear<real>(stra);
278 " too far outside allowed range [" +
283 std::cerr <<
"WARNING: Time " << time
284 <<
" outside allowed range [" 296 if (!(str >> stra >> strb))
303 "km too far outside allowed range [" +
307 std::cerr <<
"WARNING: Height " << h/1000
308 <<
"km outside allowed range [" 317 real bx, by, bz, bxt, byt, bzt;
319 c(lon, bx, by, bz, bxt, byt, bzt);
321 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
322 real H, F, D, I, Ht, Ft, Dt, It;
324 H, F, D, I, Ht, Ft, Dt, It);
342 catch (
const std::exception& e) {
343 *output <<
"ERROR: " << e.what() <<
"\n";
348 catch (
const std::exception& e) {
349 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
354 catch (
const std::exception& e) {
355 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
359 std::cerr <<
"Caught unknown exception\n";
Math::real MaxTime() const
Math::real MinHeight() const
const std::string & Description() const
Header for GeographicLib::Utility class.
Header for GeographicLib::MagneticCircle class.
Model of the earth's magnetic field.
Geomagnetic field on a circle of latitude.
static void FieldComponents(real Bx, real By, real Bz, real &H, real &F, real &D, real &I)
static std::string DefaultMagneticName()
static int extra_digits()
int usage(int retval, bool)
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
int main(int argc, const char *const argv[])
static const Geocentric & WGS84()
static constexpr int qd
degrees per quarter turn
static Math::real Decode(const std::string &dms, flag &ind)
Namespace for GeographicLib.
Header for GeographicLib::MagneticModel class.
static std::string str(T x, int p=-1)
static std::string DefaultMagneticPath()
const std::string & MagneticModelName() const
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
GeographicLib::Math::real real
static int set_digits(int ndigits=0)
Math::real MaxHeight() const
Exception handling for GeographicLib.
const std::string & MagneticFile() const
Math::real MinTime() const
const std::string & DateTime() const
MagneticCircle Circle(real t, real lat, real h) const
Header for GeographicLib::DMS class.