23 # pragma warning (disable: 4127) 26 #include "MagneticField.usage" 28 int main(
int argc,
char* argv[]) {
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")
85 else if (arg ==
"-p") {
86 if (++m == argc)
return usage(1,
true);
88 prec = Utility::num<int>(std::string(argv[m]));
90 catch (
const std::exception&) {
91 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
94 }
else if (arg ==
"-T") {
95 if (++m == argc)
return usage(1,
true);
97 tguard = Utility::num<real>(std::string(argv[m]));
99 catch (
const std::exception& e) {
100 std::cerr <<
"Error decoding argument of " << arg <<
": " 104 }
else if (arg ==
"-H") {
105 if (++m == argc)
return usage(1,
true);
107 hguard = 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 ==
"-v")
116 else if (arg ==
"--input-string") {
117 if (++m == argc)
return usage(1,
true);
119 }
else if (arg ==
"--input-file") {
120 if (++m == argc)
return usage(1,
true);
122 }
else if (arg ==
"--output-file") {
123 if (++m == argc)
return usage(1,
true);
125 }
else if (arg ==
"--line-separator") {
126 if (++m == argc)
return usage(1,
true);
127 if (std::string(argv[m]).size() != 1) {
128 std::cerr <<
"Line separator must be a single character\n";
132 }
else if (arg ==
"--comment-delimiter") {
133 if (++m == argc)
return usage(1,
true);
135 }
else if (arg ==
"--version") {
136 std::cout << argv[0] <<
": GeographicLib version " 137 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
140 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
142 std::cout<<
"\nDefault magnetic path = \"" 144 <<
"\"\nDefault magnetic name = \"" 151 if (!ifile.empty() && !istring.empty()) {
152 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
155 if (ifile ==
"-") ifile.clear();
156 std::ifstream infile;
157 std::istringstream instring;
158 if (!ifile.empty()) {
159 infile.open(ifile.c_str());
160 if (!infile.is_open()) {
161 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
164 }
else if (!istring.empty()) {
165 std::string::size_type m = 0;
167 m = istring.find(lsep, m);
168 if (m == std::string::npos)
172 instring.str(istring);
174 std::istream* input = !ifile.empty() ? &infile :
175 (!istring.empty() ? &instring : &std::cin);
177 std::ofstream outfile;
178 if (ofile ==
"-") ofile.clear();
179 if (!ofile.empty()) {
180 outfile.open(ofile.c_str());
181 if (!outfile.is_open()) {
182 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
186 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
188 tguard = std::max(
real(0), tguard);
189 hguard = std::max(
real(0), hguard);
194 if ((timeset || circle)
199 " too far outside allowed range [" +
207 "km too far outside allowed range [" +
211 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n" 214 <<
"Date & Time: " << m.
DateTime() <<
"\n" 222 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
223 std::cerr <<
"WARNING: Time " << time
224 <<
" outside allowed range [" 227 std::cerr <<
"WARNING: Height " << h/1000
228 <<
"km outside allowed range [" 233 std::string s, stra, strb;
234 while (std::getline(*input, s)) {
236 std::string eol(
"\n");
237 if (!cdelim.empty()) {
238 std::string::size_type n = s.find(cdelim);
239 if (n != std::string::npos) {
240 eol =
" " + s.substr(n) +
"\n";
244 std::istringstream str(s);
245 if (!(timeset || circle)) {
248 time = Utility::fractionalyear<real>(stra);
251 " too far outside allowed range [" +
256 std::cerr <<
"WARNING: Time " << time
257 <<
" outside allowed range [" 268 if (lon < -540 || lon >= 540)
270 " not in [-540d, 540d)");
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 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.