23 # pragma warning (disable: 4127)
26 #include "MagneticField.usage"
28 int main(
int argc,
const char*
const argv[]) {
33 bool verbose =
false, longfirst =
false;
36 std::string istring, ifile, ofile, cdelim;
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;
43 for (
int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
46 if (++m == argc)
return usage(1,
true);
48 }
else if (arg ==
"-d") {
49 if (++m == argc)
return usage(1,
true);
51 }
else if (arg ==
"-N") {
52 if (++m == argc)
return usage(1,
true);
54 Nmax = Utility::val<int>(std::string(argv[m]));
56 std::cerr <<
"Maximum degree " << argv[m] <<
" is negative\n";
60 catch (
const std::exception&) {
61 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
64 }
else if (arg ==
"-M") {
65 if (++m == argc)
return usage(1,
true);
67 Mmax = Utility::val<int>(std::string(argv[m]));
69 std::cerr <<
"Maximum order " << argv[m] <<
" is negative\n";
73 catch (
const std::exception&) {
74 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
77 }
else if (arg ==
"-t") {
78 if (++m == argc)
return usage(1,
true);
80 time = Utility::fractionalyear<real>(std::string(argv[m]));
84 catch (
const std::exception& e) {
85 std::cerr <<
"Error decoding argument of " << arg <<
": "
89 }
else if (arg ==
"-c") {
90 if (m + 3 >= argc)
return usage(1,
true);
93 time = Utility::fractionalyear<real>(std::string(argv[++m]));
98 if (!(abs(lat) <= 90))
100 h = Utility::val<real>(std::string(argv[++m]));
104 catch (
const std::exception& e) {
105 std::cerr <<
"Error decoding argument of " << arg <<
": "
109 }
else if (arg ==
"-r")
111 else if (arg ==
"-w")
112 longfirst = !longfirst;
113 else if (arg ==
"-p") {
114 if (++m == argc)
return usage(1,
true);
116 prec = Utility::val<int>(std::string(argv[m]));
118 catch (
const std::exception&) {
119 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
122 }
else if (arg ==
"-T") {
123 if (++m == argc)
return usage(1,
true);
125 tguard = Utility::val<real>(std::string(argv[m]));
127 catch (
const std::exception& e) {
128 std::cerr <<
"Error decoding argument of " << arg <<
": "
132 }
else if (arg ==
"-H") {
133 if (++m == argc)
return usage(1,
true);
135 hguard = Utility::val<real>(std::string(argv[m]));
137 catch (
const std::exception& e) {
138 std::cerr <<
"Error decoding argument of " << arg <<
": "
142 }
else if (arg ==
"-v")
144 else if (arg ==
"--input-string") {
145 if (++m == argc)
return usage(1,
true);
147 }
else if (arg ==
"--input-file") {
148 if (++m == argc)
return usage(1,
true);
150 }
else if (arg ==
"--output-file") {
151 if (++m == argc)
return usage(1,
true);
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";
160 }
else if (arg ==
"--comment-delimiter") {
161 if (++m == argc)
return usage(1,
true);
163 }
else if (arg ==
"--version") {
164 std::cout << argv[0] <<
": GeographicLib version "
165 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
168 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
170 std::cout<<
"\nDefault magnetic path = \""
172 <<
"\"\nDefault magnetic name = \""
179 if (!ifile.empty() && !istring.empty()) {
180 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
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";
192 }
else if (!istring.empty()) {
193 std::string::size_type m = 0;
195 m = istring.find(lsep, m);
196 if (m == std::string::npos)
200 instring.str(istring);
202 std::istream* input = !ifile.empty() ? &infile :
203 (!istring.empty() ? &instring : &std::cin);
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";
214 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
216 tguard = std::max(
real(0), tguard);
217 hguard = std::max(
real(0), hguard);
222 if ((timeset || circle)
227 " too far outside allowed range [" +
235 "km too far outside allowed range [" +
239 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n"
242 <<
"Date & Time: " << m.
DateTime() <<
"\n"
250 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
251 std::cerr <<
"WARNING: Time " << time
252 <<
" outside allowed range ["
255 std::cerr <<
"WARNING: Height " << h/1000
256 <<
"km outside allowed range ["
261 std::string s, eol, stra, strb;
262 std::istringstream str;
263 while (std::getline(*input, s)) {
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";
273 str.clear(); str.str(s);
274 if (!(timeset || circle)) {
277 time = Utility::fractionalyear<real>(stra);
280 " too far outside allowed range [" +
285 std::cerr <<
"WARNING: Time " << time
286 <<
" outside allowed range ["
298 if (!(str >> stra >> strb))
305 "km too far outside allowed range [" +
309 std::cerr <<
"WARNING: Height " << h/1000
310 <<
"km outside allowed range ["
319 real bx, by, bz, bxt, byt, bzt;
321 c(lon, bx, by, bz, bxt, byt, bzt);
323 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
324 real H, F, D, I, Ht, Ft, Dt, It;
326 H, F, D, I, Ht, Ft, Dt, It);
344 catch (
const std::exception& e) {
345 *output <<
"ERROR: " << e.what() <<
"\n";
350 catch (
const std::exception& e) {
351 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
356 catch (
const std::exception& e) {
357 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
361 std::cerr <<
"Caught unknown exception\n";