24 # pragma warning (disable: 4127 4701)
27 #include "Gravity.usage"
29 int main(
int argc,
const char*
const argv[]) {
34 bool verbose =
false, longfirst =
false;
37 std::string istring, ifile, ofile, cdelim;
41 int prec = -1, Nmax = -1, Mmax = -1;
48 unsigned mode = GRAVITY;
49 for (
int m = 1; m < argc; ++m) {
50 std::string arg(argv[m]);
52 if (++m == argc)
return usage(1,
true);
54 }
else if (arg ==
"-d") {
55 if (++m == argc)
return usage(1,
true);
57 }
else if (arg ==
"-N") {
58 if (++m == argc)
return usage(1,
true);
60 Nmax = Utility::val<int>(std::string(argv[m]));
62 std::cerr <<
"Maximum degree " << argv[m] <<
" is negative\n";
66 catch (
const std::exception&) {
67 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
70 }
else if (arg ==
"-M") {
71 if (++m == argc)
return usage(1,
true);
73 Mmax = Utility::val<int>(std::string(argv[m]));
75 std::cerr <<
"Maximum order " << argv[m] <<
" is negative\n";
79 catch (
const std::exception&) {
80 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
83 }
else if (arg ==
"-G")
91 else if (arg ==
"-c") {
92 if (m + 2 >= argc)
return usage(1,
true);
101 +
"d, " + std::to_string(
Math::qd) +
"d]");
102 h = Utility::val<real>(std::string(argv[++m]));
105 catch (
const std::exception& e) {
106 std::cerr <<
"Error decoding argument of " << arg <<
": "
110 }
else if (arg ==
"-w")
111 longfirst = !longfirst;
112 else if (arg ==
"-p") {
113 if (++m == argc)
return usage(1,
true);
115 prec = Utility::val<int>(std::string(argv[m]));
117 catch (
const std::exception&) {
118 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
121 }
else if (arg ==
"-v")
123 else if (arg ==
"--input-string") {
124 if (++m == argc)
return usage(1,
true);
126 }
else if (arg ==
"--input-file") {
127 if (++m == argc)
return usage(1,
true);
129 }
else if (arg ==
"--output-file") {
130 if (++m == argc)
return usage(1,
true);
132 }
else if (arg ==
"--line-separator") {
133 if (++m == argc)
return usage(1,
true);
134 if (std::string(argv[m]).size() != 1) {
135 std::cerr <<
"Line separator must be a single character\n";
139 }
else if (arg ==
"--comment-delimiter") {
140 if (++m == argc)
return usage(1,
true);
142 }
else if (arg ==
"--version") {
143 std::cout << argv[0] <<
": GeographicLib version "
144 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
147 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
149 std::cout<<
"\nDefault gravity path = \""
151 <<
"\"\nDefault gravity name = \""
158 if (!ifile.empty() && !istring.empty()) {
159 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
162 if (ifile ==
"-") ifile.clear();
163 std::ifstream infile;
164 std::istringstream instring;
165 if (!ifile.empty()) {
166 infile.open(ifile.c_str());
167 if (!infile.is_open()) {
168 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
171 }
else if (!istring.empty()) {
172 std::string::size_type m = 0;
174 m = istring.find(lsep, m);
175 if (m == std::string::npos)
179 instring.str(istring);
181 std::istream* input = !ifile.empty() ? &infile :
182 (!istring.empty() ? &instring : &std::cin);
184 std::ofstream outfile;
185 if (ofile ==
"-") ofile.clear();
186 if (!ofile.empty()) {
187 outfile.open(ofile.c_str());
188 if (!outfile.is_open()) {
189 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
193 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
215 else if (mode == UNDULATION && h != 0)
216 throw GeographicErr(
"Height should be zero for geoid undulations");
219 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n"
222 <<
"Date & Time: " << g.
DateTime() <<
"\n";
229 std::string s, eol, stra, strb;
230 std::istringstream str;
231 while (std::getline(*input, s)) {
234 if (!cdelim.empty()) {
235 std::string::size_type m = s.find(cdelim);
236 if (m != std::string::npos) {
237 eol =
" " + s.substr(m) +
"\n";
241 str.clear(); str.str(s);
251 if (!(str >> stra >> strb))
257 if (mode == UNDULATION && h != 0)
258 throw GeographicErr(
"Height must be zero for geoid heights");
269 g.
Gravity(lat, lon, h, gx, gy, gz);
278 real deltax, deltay, deltaz;
282 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
315 catch (
const std::exception& e) {
316 *output <<
"ERROR: " << e.what() <<
"\n";
321 catch (
const std::exception& e) {
322 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
327 catch (
const std::exception& e) {
328 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
332 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Header for GeographicLib::GravityCircle class.
Header for GeographicLib::GravityModel class.
int main(int argc, const char *const argv[])
Header for GeographicLib::Utility class.
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
static Math::real Decode(const std::string &dms, flag &ind)
Exception handling for GeographicLib.
Gravity on a circle of latitude.
Math::real Gravity(real lon, real &gx, real &gy, real &gz) const
Math::real Disturbance(real lon, real &deltax, real &deltay, real &deltaz) const
Math::real GeoidHeight(real lon) const
void SphericalAnomaly(real lon, real &Dg01, real &xi, real &eta) const
Model of the earth's gravity field.
const std::string & DateTime() const
Math::real Disturbance(real lat, real lon, real h, real &deltax, real &deltay, real &deltaz) const
static std::string DefaultGravityPath()
void SphericalAnomaly(real lat, real lon, real h, real &Dg01, real &xi, real &eta) const
GravityCircle Circle(real lat, real h, unsigned caps=ALL) const
static std::string DefaultGravityName()
Math::real GeoidHeight(real lat, real lon) const
const std::string & GravityFile() const
Math::real Gravity(real lat, real lon, real h, real &gx, real &gy, real &gz) const
const std::string & Description() const
const std::string & GravityModelName() const
@ qd
degrees per quarter turn
static int extra_digits()
static int set_digits(int ndigits=0)
static std::string str(T x, int p=-1)
Namespace for GeographicLib.