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;
39 bool timeset =
false, circle =
false, rate =
false;
40 real hguard = 500000, tguard = 50;
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 ==
"-t") {
52 if (++
m == argc)
return usage(1,
true);
54 time = Utility::fractionalyear<real>(std::string(argv[
m]));
58 catch (
const std::exception&
e) {
59 std::cerr <<
"Error decoding argument of " <<
arg <<
": "
63 }
else if (
arg ==
"-c") {
64 if (
m + 3 >= argc)
return usage(1,
true);
67 time = Utility::fractionalyear<real>(std::string(argv[++
m]));
74 h = Utility::val<real>(std::string(argv[++
m]));
78 catch (
const std::exception&
e) {
79 std::cerr <<
"Error decoding argument of " <<
arg <<
": "
83 }
else if (
arg ==
"-r")
86 longfirst = !longfirst;
87 else if (
arg ==
"-p") {
88 if (++
m == argc)
return usage(1,
true);
90 prec = Utility::val<int>(std::string(argv[
m]));
92 catch (
const std::exception&) {
93 std::cerr <<
"Precision " << argv[
m] <<
" is not a number\n";
96 }
else if (
arg ==
"-T") {
97 if (++
m == argc)
return usage(1,
true);
99 tguard = Utility::val<real>(std::string(argv[
m]));
101 catch (
const std::exception&
e) {
102 std::cerr <<
"Error decoding argument of " <<
arg <<
": "
106 }
else if (
arg ==
"-H") {
107 if (++
m == argc)
return usage(1,
true);
109 hguard = Utility::val<real>(std::string(argv[
m]));
111 catch (
const std::exception&
e) {
112 std::cerr <<
"Error decoding argument of " <<
arg <<
": "
116 }
else if (
arg ==
"-v")
118 else if (
arg ==
"--input-string") {
119 if (++
m == argc)
return usage(1,
true);
121 }
else if (
arg ==
"--input-file") {
122 if (++
m == argc)
return usage(1,
true);
124 }
else if (
arg ==
"--output-file") {
125 if (++
m == argc)
return usage(1,
true);
127 }
else if (
arg ==
"--line-separator") {
128 if (++
m == argc)
return usage(1,
true);
129 if (std::string(argv[
m]).
size() != 1) {
130 std::cerr <<
"Line separator must be a single character\n";
134 }
else if (
arg ==
"--comment-delimiter") {
135 if (++
m == argc)
return usage(1,
true);
137 }
else if (
arg ==
"--version") {
138 std::cout << argv[0] <<
": GeographicLib version "
142 int retval = usage(!(
arg ==
"-h" ||
arg ==
"--help"),
arg !=
"--help");
144 std::cout<<
"\nDefault magnetic path = \""
146 <<
"\"\nDefault magnetic name = \""
153 if (!ifile.empty() && !istring.empty()) {
154 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
157 if (ifile ==
"-") ifile.clear();
158 std::ifstream infile;
159 std::istringstream instring;
160 if (!ifile.empty()) {
161 infile.open(ifile.c_str());
162 if (!infile.is_open()) {
163 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
166 }
else if (!istring.empty()) {
167 std::string::size_type
m = 0;
169 m = istring.find(lsep,
m);
170 if (
m == std::string::npos)
174 instring.str(istring);
176 std::istream* input = !ifile.empty() ? &infile :
177 (!istring.empty() ? &instring : &std::cin);
179 std::ofstream outfile;
180 if (ofile ==
"-") ofile.clear();
181 if (!ofile.empty()) {
182 outfile.open(ofile.c_str());
183 if (!outfile.is_open()) {
184 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
188 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
196 if ((timeset || circle)
198 time <
m.MinTime() - tguard ||
199 time >
m.MaxTime() + tguard))
201 " too far outside allowed range [" +
206 h <
m.MinHeight() - hguard ||
207 h >
m.MaxHeight() + hguard))
209 "km too far outside allowed range [" +
213 std::cerr <<
"Magnetic file: " <<
m.MagneticFile() <<
"\n"
214 <<
"Name: " <<
m.MagneticModelName() <<
"\n"
215 <<
"Description: " <<
m.Description() <<
"\n"
216 <<
"Date & Time: " <<
m.DateTime() <<
"\n"
218 <<
m.MinTime() <<
","
219 <<
m.MaxTime() <<
"]\n"
221 <<
m.MinHeight()/1000 <<
"km,"
222 <<
m.MaxHeight()/1000 <<
"km]\n";
224 if ((timeset || circle) && (
time <
m.MinTime() ||
time >
m.MaxTime()))
225 std::cerr <<
"WARNING: Time " <<
time
226 <<
" outside allowed range ["
227 <<
m.MinTime() <<
"," <<
m.MaxTime() <<
"]\n";
228 if (circle && (
h <
m.MinHeight() ||
h >
m.MaxHeight()))
229 std::cerr <<
"WARNING: Height " <<
h/1000
230 <<
"km outside allowed range ["
231 <<
m.MinHeight()/1000 <<
"km,"
232 <<
m.MaxHeight()/1000 <<
"km]\n";
235 std::string
s, eol, stra, strb;
236 std::istringstream
str;
237 while (std::getline(*input,
s)) {
240 if (!cdelim.empty()) {
241 std::string::size_type
n =
s.find(cdelim);
242 if (
n != std::string::npos) {
243 eol =
" " +
s.substr(
n) +
"\n";
248 if (!(timeset || circle)) {
251 time = Utility::fractionalyear<real>(stra);
252 if (
time <
m.MinTime() - tguard ||
time >
m.MaxTime() + tguard)
254 " too far outside allowed range [" +
259 std::cerr <<
"WARNING: Time " <<
time
260 <<
" outside allowed range ["
261 <<
m.MinTime() <<
"," <<
m.MaxTime() <<
"]\n";
272 if (!(
str >> stra >> strb))
277 if (
h <
m.MinHeight() - hguard ||
h >
m.MaxHeight() + hguard)
279 "km too far outside allowed range [" +
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";
293 real bx, by, bz, bxt, byt, bzt;
295 c(
lon, bx, by, bz, bxt, byt, bzt);
300 H,
F,
D,
I, Ht, Ft, Dt, It);
318 catch (
const std::exception&
e) {
319 *output <<
"ERROR: " <<
e.what() <<
"\n";
324 catch (
const std::exception&
e) {
325 std::cerr <<
"Error reading " <<
model <<
": " <<
e.what() <<
"\n";
330 catch (
const std::exception&
e) {
331 std::cerr <<
"Caught exception: " <<
e.what() <<
"\n";
335 std::cerr <<
"Caught unknown exception\n";