23# pragma warning (disable: 4127)
26#include "MagneticField.usage"
28int main(
int argc,
const char*
const argv[]) {
32 Utility::set_digits();
33 bool verbose =
false, longfirst =
false;
35 std::string model = MagneticModel::DefaultMagneticName();
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]));
95 lat = DMS::Decode(std::string(argv[++m]), ind);
96 if (ind == DMS::LONGITUDE)
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 = \""
171 << MagneticModel::DefaultMagneticPath()
172 <<
"\"\nDefault magnetic name = \""
173 << MagneticModel::DefaultMagneticName()
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);
218 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
222 const MagneticModel m(model, dir, Geocentric::WGS84(), Nmax, Mmax);
223 if ((timeset || circle)
224 && (!isfinite(time) ||
228 " too far outside allowed range [" +
229 Utility::str(m.
MinTime()) +
"," +
230 Utility::str(m.
MaxTime()) +
"]");
236 "km too far outside allowed range [" +
237 Utility::str(m.
MinHeight()/1000) +
"km," +
238 Utility::str(m.
MaxHeight()/1000) +
"km]");
240 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n"
243 <<
"Date & Time: " << m.
DateTime() <<
"\n"
251 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
252 std::cerr <<
"WARNING: Time " << time
253 <<
" outside allowed range ["
256 std::cerr <<
"WARNING: Height " << h/1000
257 <<
"km outside allowed range ["
262 std::string s, eol, stra, strb;
263 std::istringstream str;
264 while (std::getline(*input, s)) {
267 if (!cdelim.empty()) {
268 std::string::size_type n = s.find(cdelim);
269 if (n != std::string::npos) {
270 eol =
" " + s.substr(n) +
"\n";
274 str.clear(); str.str(s);
275 if (!(timeset || circle)) {
278 time = Utility::fractionalyear<real>(stra);
281 " too far outside allowed range [" +
282 Utility::str(m.
MinTime()) +
"," +
286 std::cerr <<
"WARNING: Time " << time
287 <<
" outside allowed range ["
295 lon = DMS::Decode(strb, ind);
296 if (ind == DMS::LATITUDE)
299 if (!(str >> stra >> strb))
301 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
306 "km too far outside allowed range [" +
307 Utility::str(m.
MinHeight()/1000) +
"km," +
308 Utility::str(m.
MaxHeight()/1000) +
"km]");
310 std::cerr <<
"WARNING: Height " << h/1000
311 <<
"km outside allowed range ["
320 real bx, by, bz, bxt, byt, bzt;
322 c(lon, bx, by, bz, bxt, byt, bzt);
324 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
325 real H, F, D, I, Ht, Ft, Dt, It;
326 MagneticModel::FieldComponents(bx, by, bz, bxt, byt, bzt,
327 H, F, D, I, Ht, Ft, Dt, It);
329 *output << DMS::Encode(D, prec + 1, DMS::NUMBER) <<
" "
330 << DMS::Encode(I, prec + 1, DMS::NUMBER) <<
" "
331 << Utility::str(H, prec) <<
" "
332 << Utility::str(by, prec) <<
" "
333 << Utility::str(bx, prec) <<
" "
334 << Utility::str(-bz, prec) <<
" "
335 << Utility::str(F, prec) << eol;
337 *output << DMS::Encode(Dt, prec + 1, DMS::NUMBER) <<
" "
338 << DMS::Encode(It, prec + 1, DMS::NUMBER) <<
" "
339 << Utility::str(Ht, prec) <<
" "
340 << Utility::str(byt, prec) <<
" "
341 << Utility::str(bxt, prec) <<
" "
342 << Utility::str(-bzt, prec) <<
" "
343 << Utility::str(Ft, prec) << eol;
345 catch (
const std::exception& e) {
346 *output <<
"ERROR: " << e.what() <<
"\n";
351 catch (
const std::exception& e) {
352 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
357 catch (
const std::exception& e) {
358 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
362 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Header for GeographicLib::MagneticCircle class.
int main(int argc, const char *const argv[])
Header for GeographicLib::MagneticModel class.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
Geomagnetic field on a circle of latitude.
Model of the earth's magnetic field.
MagneticCircle Circle(real t, real lat, real h) const
Math::real MinTime() const
const std::string & Description() const
const std::string & MagneticFile() const
Math::real MaxTime() const
Math::real MaxHeight() const
const std::string & DateTime() const
const std::string & MagneticModelName() const
Math::real MinHeight() const
Namespace for GeographicLib.