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);
99 if (!(abs(lat) <= 90))
101 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 ==
"-w")
110 longfirst = !longfirst;
111 else if (arg ==
"-p") {
112 if (++m == argc)
return usage(1,
true);
114 prec = Utility::val<int>(std::string(argv[m]));
116 catch (
const std::exception&) {
117 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
120 }
else if (arg ==
"-v")
122 else if (arg ==
"--input-string") {
123 if (++m == argc)
return usage(1,
true);
125 }
else if (arg ==
"--input-file") {
126 if (++m == argc)
return usage(1,
true);
128 }
else if (arg ==
"--output-file") {
129 if (++m == argc)
return usage(1,
true);
131 }
else if (arg ==
"--line-separator") {
132 if (++m == argc)
return usage(1,
true);
133 if (std::string(argv[m]).size() != 1) {
134 std::cerr <<
"Line separator must be a single character\n";
138 }
else if (arg ==
"--comment-delimiter") {
139 if (++m == argc)
return usage(1,
true);
141 }
else if (arg ==
"--version") {
142 std::cout << argv[0] <<
": GeographicLib version " 143 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
146 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
148 std::cout<<
"\nDefault gravity path = \"" 150 <<
"\"\nDefault gravity name = \"" 157 if (!ifile.empty() && !istring.empty()) {
158 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
161 if (ifile ==
"-") ifile.clear();
162 std::ifstream infile;
163 std::istringstream instring;
164 if (!ifile.empty()) {
165 infile.open(ifile.c_str());
166 if (!infile.is_open()) {
167 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
170 }
else if (!istring.empty()) {
171 std::string::size_type m = 0;
173 m = istring.find(lsep, m);
174 if (m == std::string::npos)
178 instring.str(istring);
180 std::istream* input = !ifile.empty() ? &infile :
181 (!istring.empty() ? &instring : &std::cin);
183 std::ofstream outfile;
184 if (ofile ==
"-") ofile.clear();
185 if (!ofile.empty()) {
186 outfile.open(ofile.c_str());
187 if (!outfile.is_open()) {
188 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
192 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
214 else if (mode == UNDULATION && h != 0)
215 throw GeographicErr(
"Height should be zero for geoid undulations");
218 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n" 221 <<
"Date & Time: " << g.
DateTime() <<
"\n";
228 std::string s, eol, stra, strb;
229 std::istringstream str;
230 while (std::getline(*input, s)) {
233 if (!cdelim.empty()) {
234 std::string::size_type m = s.find(cdelim);
235 if (m != std::string::npos) {
236 eol =
" " + s.substr(m) +
"\n";
240 str.clear(); str.str(s);
250 if (!(str >> stra >> strb))
256 if (mode == UNDULATION && h != 0)
257 throw GeographicErr(
"Height must be zero for geoid heights");
266 c.Gravity(lon, gx, gy, gz);
268 g.
Gravity(lat, lon, h, gx, gy, gz);
277 real deltax, deltay, deltaz;
279 c.Disturbance(lon, deltax, deltay, deltaz);
281 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
294 c.SphericalAnomaly(lon, Dg01, xi, eta);
314 catch (
const std::exception& e) {
315 *output <<
"ERROR: " << e.what() <<
"\n";
320 catch (
const std::exception& e) {
321 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
326 catch (
const std::exception& e) {
327 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
331 std::cerr <<
"Caught unknown exception\n";
static std::string DefaultGravityName()
const std::string & GravityModelName() const
const std::string & Description() const
GeographicLib::Math::real real
static std::string DefaultGravityPath()
Header for GeographicLib::Utility class.
Header for GeographicLib::GravityModel class.
const std::string & DateTime() const
static int extra_digits()
int main(int argc, const char *const argv[])
Math::real Gravity(real lat, real lon, real h, real &gx, real &gy, real &gz) const
static Math::real Decode(const std::string &dms, flag &ind)
Namespace for GeographicLib.
static std::string str(T x, int p=-1)
Math::real Disturbance(real lat, real lon, real h, real &deltax, real &deltay, real &deltaz) 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)
Model of the earth's gravity field.
GravityCircle Circle(real lat, real h, unsigned caps=ALL) const
Exception handling for GeographicLib.
const std::string & GravityFile() const
Header for GeographicLib::GravityCircle class.
void SphericalAnomaly(real lat, real lon, real h, real &Dg01, real &xi, real &eta) const
Gravity on a circle of latitude.
Math::real GeoidHeight(real lat, real lon) const
Header for GeographicLib::DMS class.