35 # pragma warning (disable: 4127)
38 #include "MagneticField.usage"
40 int main(
int argc,
char* argv[]) {
42 using namespace GeographicLib;
47 std::string istring, ifile, ofile, cdelim;
49 real time = 0, lat = 0, h = 0;
50 bool timeset =
false, circle =
false, rate =
false;
51 real hguard = 500000, tguard = 50;
54 for (
int m = 1; m < argc; ++m) {
55 std::string arg(argv[m]);
57 if (++m == argc)
return usage(1,
true);
59 }
else if (arg ==
"-d") {
60 if (++m == argc)
return usage(1,
true);
62 }
else if (arg ==
"-t") {
63 if (++m == argc)
return usage(1,
true);
65 time = Utility::fractionalyear<real>(std::string(argv[m]));
69 catch (
const std::exception& e) {
70 std::cerr <<
"Error decoding argument of " << arg <<
": "
74 }
else if (arg ==
"-c") {
75 if (m + 3 >= argc)
return usage(1,
true);
77 time = Utility::fractionalyear<real>(std::string(argv[++m]));
82 if (!(std::abs(lat) <= 90))
84 h = Utility::num<real>(std::string(argv[++m]));
88 catch (
const std::exception& e) {
89 std::cerr <<
"Error decoding argument of " << arg <<
": "
93 }
else if (arg ==
"-r")
95 else if (arg ==
"-p") {
96 if (++m == argc)
return usage(1,
true);
98 prec = Utility::num<int>(std::string(argv[m]));
100 catch (
const std::exception&) {
101 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
104 }
else if (arg ==
"-T") {
105 if (++m == argc)
return usage(1,
true);
107 tguard = Utility::num<real>(std::string(argv[m]));
109 catch (
const std::exception& e) {
110 std::cerr <<
"Error decoding argument of " << arg <<
": "
114 }
else if (arg ==
"-H") {
115 if (++m == argc)
return usage(1,
true);
117 hguard = Utility::num<real>(std::string(argv[m]));
119 catch (
const std::exception& e) {
120 std::cerr <<
"Error decoding argument of " << arg <<
": "
124 }
else if (arg ==
"-v")
126 else if (arg ==
"--input-string") {
127 if (++m == argc)
return usage(1,
true);
129 }
else if (arg ==
"--input-file") {
130 if (++m == argc)
return usage(1,
true);
132 }
else if (arg ==
"--output-file") {
133 if (++m == argc)
return usage(1,
true);
135 }
else if (arg ==
"--line-separator") {
136 if (++m == argc)
return usage(1,
true);
137 if (std::string(argv[m]).size() != 1) {
138 std::cerr <<
"Line separator must be a single character\n";
142 }
else if (arg ==
"--comment-delimiter") {
143 if (++m == argc)
return usage(1,
true);
145 }
else if (arg ==
"--version") {
147 << argv[0] <<
": GeographicLib version "
148 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
151 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
153 std::cout<<
"\nDefault magnetic path = \""
155 <<
"\"\nDefault magnetic name = \""
162 if (!ifile.empty() && !istring.empty()) {
163 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
166 if (ifile ==
"-") ifile.clear();
167 std::ifstream infile;
168 std::istringstream instring;
169 if (!ifile.empty()) {
170 infile.open(ifile.c_str());
171 if (!infile.is_open()) {
172 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
175 }
else if (!istring.empty()) {
176 std::string::size_type m = 0;
178 m = istring.find(lsep, m);
179 if (m == std::string::npos)
183 instring.str(istring);
185 std::istream* input = !ifile.empty() ? &infile :
186 (!istring.empty() ? &instring : &std::cin);
188 std::ofstream outfile;
189 if (ofile ==
"-") ofile.clear();
190 if (!ofile.empty()) {
191 outfile.open(ofile.c_str());
192 if (!outfile.is_open()) {
193 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
197 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
199 tguard = std::max(
real(0), tguard);
200 hguard = std::max(
real(0), hguard);
201 prec = std::min(10, std::max(0, prec));
205 if ((timeset || circle)
206 && (!Math::isfinite<real>(time) ||
210 " too far outside allowed range [" +
214 && (!Math::isfinite<real>(h) ||
218 "km too far outside allowed range [" +
222 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n"
225 <<
"Date & Time: " << m.
DateTime() <<
"\n"
233 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
234 std::cerr <<
"WARNING: Time " << time
235 <<
" outside allowed range ["
238 std::cerr <<
"WARNING: Height " << h/1000
239 <<
"km outside allowed range ["
244 std::string s, stra, strb;
245 while (std::getline(*input, s)) {
247 std::string eol(
"\n");
248 if (!cdelim.empty()) {
249 std::string::size_type m = s.find(cdelim);
250 if (m != std::string::npos) {
251 eol =
" " + s.substr(m) +
"\n";
255 std::istringstream str(s);
256 if (!(timeset || circle)) {
259 time = Utility::fractionalyear<real>(stra);
262 " too far outside allowed range [" +
267 std::cerr <<
"WARNING: Time " << time
268 <<
" outside allowed range ["
279 if (lon < -540 || lon >= 540)
281 " not in [-540d, 540d)");
283 if (!(str >> stra >> strb))
290 "km too far outside allowed range [" +
294 std::cerr <<
"WARNING: Height " << h/1000
295 <<
"km outside allowed range ["
304 real bx, by, bz, bxt, byt, bzt;
306 c(lon, bx, by, bz, bxt, byt, bzt);
308 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
309 real H, F, D, I, Ht, Ft, Dt, It;
311 H, F, D, I, Ht, Ft, Dt, It);
315 << Utility::str<real>(H, prec) <<
" "
316 << Utility::str<real>(by, prec) <<
" "
317 << Utility::str<real>(bx, prec) <<
" "
318 << Utility::str<real>(-bz, prec) <<
" "
319 << Utility::str<real>(F, prec) << eol;
323 << Utility::str<real>(Ht, prec) <<
" "
324 << Utility::str<real>(byt, prec) <<
" "
325 << Utility::str<real>(bxt, prec) <<
" "
326 << Utility::str<real>(-bzt, prec) <<
" "
327 << Utility::str<real>(Ft, prec) << eol;
329 catch (
const std::exception& e) {
330 *output <<
"ERROR: " << e.what() <<
"\n";
335 catch (
const std::exception& e) {
336 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
341 catch (
const std::exception& e) {
342 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
346 std::cerr <<
"Caught unknown exception\n";
Math::real MinTime() const
const std::string & MagneticModelName() const
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
Header for GeographicLib::MagneticCircle class.
Math::real MaxHeight() const
Math::real MaxTime() const
Model of the earth's magnetic field.
Geomagnetic field on a circle of latitude.
MagneticCircle Circle(real t, real lat, real h) const
const std::string & MagneticFile() const
static void FieldComponents(real Bx, real By, real Bz, real &H, real &F, real &D, real &I)
const std::string & Description() const
int main(int argc, char *argv[])
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
static Math::real Decode(const std::string &dms, flag &ind)
Header for GeographicLib::MagneticModel class.
static std::string str(T x, int p=-1)
static std::string DefaultMagneticName()
Exception handling for GeographicLib.
const std::string & DateTime() const
static std::string DefaultMagneticPath()
Math::real MinHeight() const
Header for GeographicLib::DMS class.