23 # pragma warning (disable: 4127)
26 #include "MagneticField.usage"
28 int main(
int argc,
char* 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;
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]));
72 if (!(abs(lat) <= 90))
74 h = Utility::num<real>(std::string(argv[++m]));
78 catch (
const std::exception& e) {
79 std::cerr <<
"Error decoding argument of " << arg <<
": "
83 }
else if (arg ==
"-r")
87 else if (arg ==
"-p") {
88 if (++m == argc)
return usage(1,
true);
90 prec = Utility::num<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::num<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::num<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 "
139 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
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;
190 tguard = std::max(
real(0), tguard);
191 hguard = std::max(
real(0), hguard);
196 if ((timeset || circle)
201 " too far outside allowed range [" +
209 "km too far outside allowed range [" +
213 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n"
216 <<
"Date & Time: " << m.
DateTime() <<
"\n"
224 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
225 std::cerr <<
"WARNING: Time " << time
226 <<
" outside allowed range ["
229 std::cerr <<
"WARNING: Height " << h/1000
230 <<
"km outside allowed range ["
235 std::string s, eol, stra, strb;
236 std::istringstream str(s);
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";
247 str.clear(); str.str(s);
248 if (!(timeset || circle)) {
251 time = Utility::fractionalyear<real>(stra);
254 " too far outside allowed range [" +
259 std::cerr <<
"WARNING: Time " << time
260 <<
" outside allowed range ["
272 if (!(str >> stra >> strb))
279 "km too far outside allowed range [" +
283 std::cerr <<
"WARNING: Height " << h/1000
284 <<
"km outside allowed range ["
293 real bx, by, bz, bxt, byt, bzt;
295 c(lon, bx, by, bz, bxt, byt, bzt);
297 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
298 real H, F, D, I, Ht, Ft, Dt, It;
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";
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 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 void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
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.