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") {
137 << argv[0] <<
": GeographicLib version "
138 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
141 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
143 std::cout<<
"\nDefault magnetic path = \""
145 <<
"\"\nDefault magnetic name = \""
152 if (!ifile.empty() && !istring.empty()) {
153 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
156 if (ifile ==
"-") ifile.clear();
157 std::ifstream infile;
158 std::istringstream instring;
159 if (!ifile.empty()) {
160 infile.open(ifile.c_str());
161 if (!infile.is_open()) {
162 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
165 }
else if (!istring.empty()) {
166 std::string::size_type m = 0;
168 m = istring.find(lsep, m);
169 if (m == std::string::npos)
173 instring.str(istring);
175 std::istream* input = !ifile.empty() ? &infile :
176 (!istring.empty() ? &instring : &std::cin);
178 std::ofstream outfile;
179 if (ofile ==
"-") ofile.clear();
180 if (!ofile.empty()) {
181 outfile.open(ofile.c_str());
182 if (!outfile.is_open()) {
183 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
187 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
189 tguard = std::max(
real(0), tguard);
190 hguard = std::max(
real(0), hguard);
195 if ((timeset || circle)
200 " too far outside allowed range [" +
208 "km too far outside allowed range [" +
212 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n"
215 <<
"Date & Time: " << m.
DateTime() <<
"\n"
223 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
224 std::cerr <<
"WARNING: Time " << time
225 <<
" outside allowed range ["
228 std::cerr <<
"WARNING: Height " << h/1000
229 <<
"km outside allowed range ["
234 std::string s, stra, strb;
235 while (std::getline(*input, s)) {
237 std::string eol(
"\n");
238 if (!cdelim.empty()) {
239 std::string::size_type n = s.find(cdelim);
240 if (n != std::string::npos) {
241 eol =
" " + s.substr(n) +
"\n";
245 std::istringstream str(s);
246 if (!(timeset || circle)) {
249 time = Utility::fractionalyear<real>(stra);
252 " too far outside allowed range [" +
257 std::cerr <<
"WARNING: Time " << time
258 <<
" outside allowed range ["
269 if (lon < -540 || lon >= 540)
271 " not in [-540d, 540d)");
273 if (!(str >> stra >> strb))
280 "km too far outside allowed range [" +
284 std::cerr <<
"WARNING: Height " << h/1000
285 <<
"km outside allowed range ["
294 real bx, by, bz, bxt, byt, bzt;
296 c(lon, bx, by, bz, bxt, byt, bzt);
298 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
299 real H, F, D, I, Ht, Ft, Dt, It;
301 H, F, D, I, Ht, Ft, Dt, It);
319 catch (
const std::exception& e) {
320 *output <<
"ERROR: " << e.what() <<
"\n";
325 catch (
const std::exception& e) {
326 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
331 catch (
const std::exception& e) {
332 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
336 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.