23 # pragma warning (disable: 4127) 26 #include "MagneticField.usage" 28 int main(
int argc,
const char*
const 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;
41 int prec = 1, Nmax = -1, Mmax = -1;
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 ==
"-N") {
52 if (++m == argc)
return usage(1,
true);
54 Nmax = Utility::val<int>(std::string(argv[m]));
56 std::cerr <<
"Maximum degree " << argv[m] <<
" is negative\n";
60 catch (
const std::exception&) {
61 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
64 }
else if (arg ==
"-M") {
65 if (++m == argc)
return usage(1,
true);
67 Mmax = Utility::val<int>(std::string(argv[m]));
69 std::cerr <<
"Maximum order " << argv[m] <<
" is negative\n";
73 catch (
const std::exception&) {
74 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
77 }
else if (arg ==
"-t") {
78 if (++m == argc)
return usage(1,
true);
80 time = Utility::fractionalyear<real>(std::string(argv[m]));
84 catch (
const std::exception& e) {
85 std::cerr <<
"Error decoding argument of " << arg <<
": " 89 }
else if (arg ==
"-c") {
90 if (m + 3 >= argc)
return usage(1,
true);
93 time = Utility::fractionalyear<real>(std::string(argv[++m]));
98 if (!(abs(lat) <= 90))
100 h = Utility::val<real>(std::string(argv[++m]));
104 catch (
const std::exception& e) {
105 std::cerr <<
"Error decoding argument of " << arg <<
": " 109 }
else if (arg ==
"-r")
111 else if (arg ==
"-w")
112 longfirst = !longfirst;
113 else if (arg ==
"-p") {
114 if (++m == argc)
return usage(1,
true);
116 prec = Utility::val<int>(std::string(argv[m]));
118 catch (
const std::exception&) {
119 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
122 }
else if (arg ==
"-T") {
123 if (++m == argc)
return usage(1,
true);
125 tguard = Utility::val<real>(std::string(argv[m]));
127 catch (
const std::exception& e) {
128 std::cerr <<
"Error decoding argument of " << arg <<
": " 132 }
else if (arg ==
"-H") {
133 if (++m == argc)
return usage(1,
true);
135 hguard = Utility::val<real>(std::string(argv[m]));
137 catch (
const std::exception& e) {
138 std::cerr <<
"Error decoding argument of " << arg <<
": " 142 }
else if (arg ==
"-v")
144 else if (arg ==
"--input-string") {
145 if (++m == argc)
return usage(1,
true);
147 }
else if (arg ==
"--input-file") {
148 if (++m == argc)
return usage(1,
true);
150 }
else if (arg ==
"--output-file") {
151 if (++m == argc)
return usage(1,
true);
153 }
else if (arg ==
"--line-separator") {
154 if (++m == argc)
return usage(1,
true);
155 if (std::string(argv[m]).size() != 1) {
156 std::cerr <<
"Line separator must be a single character\n";
160 }
else if (arg ==
"--comment-delimiter") {
161 if (++m == argc)
return usage(1,
true);
163 }
else if (arg ==
"--version") {
164 std::cout << argv[0] <<
": GeographicLib version " 165 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
168 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
170 std::cout<<
"\nDefault magnetic path = \"" 172 <<
"\"\nDefault magnetic name = \"" 179 if (!ifile.empty() && !istring.empty()) {
180 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
183 if (ifile ==
"-") ifile.clear();
184 std::ifstream infile;
185 std::istringstream instring;
186 if (!ifile.empty()) {
187 infile.open(ifile.c_str());
188 if (!infile.is_open()) {
189 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
192 }
else if (!istring.empty()) {
193 std::string::size_type m = 0;
195 m = istring.find(lsep, m);
196 if (m == std::string::npos)
200 instring.str(istring);
202 std::istream* input = !ifile.empty() ? &infile :
203 (!istring.empty() ? &instring : &std::cin);
205 std::ofstream outfile;
206 if (ofile ==
"-") ofile.clear();
207 if (!ofile.empty()) {
208 outfile.open(ofile.c_str());
209 if (!outfile.is_open()) {
210 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
214 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
216 tguard = std::max(
real(0), tguard);
217 hguard = std::max(
real(0), hguard);
223 if ((timeset || circle)
224 && (!isfinite(time) ||
228 " too far outside allowed range [" +
236 "km too far outside allowed range [" +
240 std::cerr <<
"Magnetic file: " << m.
MagneticFile() <<
"\n" 243 <<
"Date & Time: " << m.
DateTime() <<
"\n" 251 if ((timeset || circle) && (time < m.
MinTime() || time > m.
MaxTime()))
252 std::cerr <<
"WARNING: Time " << time
253 <<
" outside allowed range [" 256 std::cerr <<
"WARNING: Height " << h/1000
257 <<
"km outside allowed range [" 262 std::string s, eol, stra, strb;
263 std::istringstream str;
264 while (std::getline(*input, s)) {
267 if (!cdelim.empty()) {
268 std::string::size_type n = s.find(cdelim);
269 if (n != std::string::npos) {
270 eol =
" " + s.substr(n) +
"\n";
274 str.clear(); str.str(s);
275 if (!(timeset || circle)) {
278 time = Utility::fractionalyear<real>(stra);
281 " too far outside allowed range [" +
286 std::cerr <<
"WARNING: Time " << time
287 <<
" outside allowed range [" 299 if (!(str >> stra >> strb))
306 "km too far outside allowed range [" +
310 std::cerr <<
"WARNING: Height " << h/1000
311 <<
"km outside allowed range [" 320 real bx, by, bz, bxt, byt, bzt;
322 c(lon, bx, by, bz, bxt, byt, bzt);
324 m(time, lat, lon, h, bx, by, bz, bxt, byt, bzt);
325 real H, F, D, I, Ht, Ft, Dt, It;
327 H, F, D, I, Ht, Ft, Dt, It);
345 catch (
const std::exception& e) {
346 *output <<
"ERROR: " << e.what() <<
"\n";
351 catch (
const std::exception& e) {
352 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
357 catch (
const std::exception& e) {
358 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
362 std::cerr <<
"Caught unknown exception\n";
Math::real MaxTime() const
Math::real MinHeight() const
const std::string & Description() const
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
Header for GeographicLib::MagneticCircle class.
Model of the earth's magnetic field.
Geomagnetic field on a circle of latitude.
static void FieldComponents(real Bx, real By, real Bz, real &H, real &F, real &D, real &I)
static std::string DefaultMagneticName()
static int extra_digits()
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
int main(int argc, const char *const argv[])
static const Geocentric & WGS84()
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 DefaultMagneticPath()
const std::string & MagneticModelName() const
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)
Math::real MaxHeight() const
Exception handling for GeographicLib.
const std::string & MagneticFile() const
Math::real MinTime() const
const std::string & DateTime() const
MagneticCircle Circle(real t, real lat, real h) const
Header for GeographicLib::DMS class.