35 # pragma warning (disable: 4127)
38 #include "MagneticField.usage"
40 int main(
int argc,
char* argv[]) {
48 std::string istring, ifile, ofile, cdelim;
50 real time = 0, lat = 0, h = 0;
51 bool timeset =
false, circle =
false, rate =
false;
52 real hguard = 500000, tguard = 50;
55 for (
int m = 1; m < argc; ++m) {
56 std::string arg(argv[m]);
58 if (++m == argc)
return usage(1,
true);
60 }
else if (arg ==
"-d") {
61 if (++m == argc)
return usage(1,
true);
63 }
else if (arg ==
"-t") {
64 if (++m == argc)
return usage(1,
true);
66 time = Utility::fractionalyear<real>(std::string(argv[m]));
70 catch (
const std::exception& e) {
71 std::cerr <<
"Error decoding argument of " << arg <<
": "
75 }
else if (arg ==
"-c") {
76 if (m + 3 >= argc)
return usage(1,
true);
79 time = Utility::fractionalyear<real>(std::string(argv[++m]));
84 if (!(abs(lat) <= 90))
86 h = Utility::num<real>(std::string(argv[++m]));
90 catch (
const std::exception& e) {
91 std::cerr <<
"Error decoding argument of " << arg <<
": "
95 }
else if (arg ==
"-r")
97 else if (arg ==
"-p") {
98 if (++m == argc)
return usage(1,
true);
100 prec = Utility::num<int>(std::string(argv[m]));
102 catch (
const std::exception&) {
103 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
106 }
else if (arg ==
"-T") {
107 if (++m == argc)
return usage(1,
true);
109 tguard = Utility::num<real>(std::string(argv[m]));
111 catch (
const std::exception& e) {
112 std::cerr <<
"Error decoding argument of " << arg <<
": "
116 }
else if (arg ==
"-H") {
117 if (++m == argc)
return usage(1,
true);
119 hguard = Utility::num<real>(std::string(argv[m]));
121 catch (
const std::exception& e) {
122 std::cerr <<
"Error decoding argument of " << arg <<
": "
126 }
else if (arg ==
"-v")
128 else if (arg ==
"--input-string") {
129 if (++m == argc)
return usage(1,
true);
131 }
else if (arg ==
"--input-file") {
132 if (++m == argc)
return usage(1,
true);
134 }
else if (arg ==
"--output-file") {
135 if (++m == argc)
return usage(1,
true);
137 }
else if (arg ==
"--line-separator") {
138 if (++m == argc)
return usage(1,
true);
139 if (std::string(argv[m]).size() != 1) {
140 std::cerr <<
"Line separator must be a single character\n";
144 }
else if (arg ==
"--comment-delimiter") {
145 if (++m == argc)
return usage(1,
true);
147 }
else if (arg ==
"--version") {
149 << argv[0] <<
": GeographicLib version "
150 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
153 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
155 std::cout<<
"\nDefault magnetic path = \""
157 <<
"\"\nDefault magnetic name = \""
164 if (!ifile.empty() && !istring.empty()) {
165 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
168 if (ifile ==
"-") ifile.clear();
169 std::ifstream infile;
170 std::istringstream instring;
171 if (!ifile.empty()) {
172 infile.open(ifile.c_str());
173 if (!infile.is_open()) {
174 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
177 }
else if (!istring.empty()) {
178 std::string::size_type m = 0;
180 m = istring.find(lsep, m);
181 if (m == std::string::npos)
185 instring.str(istring);
187 std::istream* input = !ifile.empty() ? &infile :
188 (!istring.empty() ? &instring : &std::cin);
190 std::ofstream outfile;
191 if (ofile ==
"-") ofile.clear();
192 if (!ofile.empty()) {
193 outfile.open(ofile.c_str());
194 if (!outfile.is_open()) {
195 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
199 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
201 tguard = std::max(
real(0), tguard);
202 hguard = std::max(
real(0), hguard);
207 if ((timeset || circle)
212 " too far outside allowed range [" +
220 "km too far outside allowed range [" +
224 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n"
227 <<
"Date & Time: " << m.
DateTime() <<
"\n"
235 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
236 std::cerr <<
"WARNING: Time " << time
237 <<
" outside allowed range ["
240 std::cerr <<
"WARNING: Height " << h/1000
241 <<
"km outside allowed range ["
246 std::string s, stra, strb;
247 while (std::getline(*input, s)) {
249 std::string eol(
"\n");
250 if (!cdelim.empty()) {
251 std::string::size_type m = s.find(cdelim);
252 if (m != std::string::npos) {
253 eol =
" " + s.substr(m) +
"\n";
257 std::istringstream str(s);
258 if (!(timeset || circle)) {
261 time = Utility::fractionalyear<real>(stra);
264 " too far outside allowed range [" +
269 std::cerr <<
"WARNING: Time " << time
270 <<
" outside allowed range ["
281 if (lon < -540 || lon >= 540)
283 " not in [-540d, 540d)");
285 if (!(str >> stra >> strb))
292 "km too far outside allowed range [" +
296 std::cerr <<
"WARNING: Height " << h/1000
297 <<
"km outside allowed range ["
306 real bx, by, bz, bxt, byt, bzt;
308 c(lon, bx, by, bz, bxt, byt, bzt);
310 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
311 real H, F, D, I, Ht, Ft, Dt, It;
313 H, F, D, I, Ht, Ft, Dt, It);
331 catch (
const std::exception& e) {
332 *output <<
"ERROR: " << e.what() <<
"\n";
337 catch (
const std::exception& e) {
338 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
343 catch (
const std::exception& e) {
344 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
348 std::cerr <<
"Caught unknown exception\n";
Math::real MinTime() const
const std::string & MagneticModelName() const
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
static bool isfinite(T x)
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 int extra_digits()
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)
Namespace for GeographicLib.
Header for GeographicLib::MagneticModel class.
static std::string str(T x, int p=-1)
static std::string DefaultMagneticName()
static int set_digits(int ndigits=0)
Exception handling for GeographicLib.
const std::string & DateTime() const
static std::string DefaultMagneticPath()
Math::real MinHeight() const
Header for GeographicLib::DMS class.