23int 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);
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);
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);
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);
95 +
"d, " + std::to_string(
Math::qd) +
"d]");
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);
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);
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);
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)) {
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";