24 # pragma warning (disable: 4127 4701)
27 #include "Gravity.usage"
29 int main(
int argc,
char* argv[]) {
31 using namespace GeographicLib;
37 std::string istring, ifile, ofile, cdelim;
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 ==
"-G")
65 else if (arg ==
"-c") {
66 if (m + 2 >= argc)
return usage(1,
true);
73 if (!(abs(lat) <= 90))
75 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 ==
"-p") {
84 if (++m == argc)
return usage(1,
true);
86 prec = Utility::num<int>(std::string(argv[m]));
88 catch (
const std::exception&) {
89 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
92 }
else if (arg ==
"-v")
94 else if (arg ==
"--input-string") {
95 if (++m == argc)
return usage(1,
true);
97 }
else if (arg ==
"--input-file") {
98 if (++m == argc)
return usage(1,
true);
100 }
else if (arg ==
"--output-file") {
101 if (++m == argc)
return usage(1,
true);
103 }
else if (arg ==
"--line-separator") {
104 if (++m == argc)
return usage(1,
true);
105 if (std::string(argv[m]).size() != 1) {
106 std::cerr <<
"Line separator must be a single character\n";
110 }
else if (arg ==
"--comment-delimiter") {
111 if (++m == argc)
return usage(1,
true);
113 }
else if (arg ==
"--version") {
114 std::cout << argv[0] <<
": GeographicLib version "
115 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
118 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
120 std::cout<<
"\nDefault gravity path = \""
122 <<
"\"\nDefault gravity name = \""
129 if (!ifile.empty() && !istring.empty()) {
130 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
133 if (ifile ==
"-") ifile.clear();
134 std::ifstream infile;
135 std::istringstream instring;
136 if (!ifile.empty()) {
137 infile.open(ifile.c_str());
138 if (!infile.is_open()) {
139 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
142 }
else if (!istring.empty()) {
143 std::string::size_type m = 0;
145 m = istring.find(lsep, m);
146 if (m == std::string::npos)
150 instring.str(istring);
152 std::istream* input = !ifile.empty() ? &infile :
153 (!istring.empty() ? &instring : &std::cin);
155 std::ofstream outfile;
156 if (ofile ==
"-") ofile.clear();
157 if (!ofile.empty()) {
158 outfile.open(ofile.c_str());
159 if (!outfile.is_open()) {
160 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
164 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
185 else if (mode == UNDULATION && h != 0)
186 throw GeographicErr(
"Height should be zero for geoid undulations");
189 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n"
192 <<
"Date & Time: " << g.
DateTime() <<
"\n";
199 std::string s, stra, strb;
200 while (std::getline(*input, s)) {
202 std::string eol(
"\n");
203 if (!cdelim.empty()) {
204 std::string::size_type m = s.find(cdelim);
205 if (m != std::string::npos) {
206 eol =
" " + s.substr(m) +
"\n";
210 std::istringstream str(s);
219 if (lon < -540 || lon >= 540)
221 " not in [-540d, 540d)");
223 if (!(str >> stra >> strb))
229 if (mode == UNDULATION && h != 0)
230 throw GeographicErr(
"Height must be zero for geoid heights");
239 c.Gravity(lon, gx, gy, gz);
241 g.
Gravity(lat, lon, h, gx, gy, gz);
250 real deltax, deltay, deltaz;
252 c.Disturbance(lon, deltax, deltay, deltaz);
254 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
267 c.SphericalAnomaly(lon, Dg01, xi, eta);
287 catch (
const std::exception& e) {
288 *output <<
"ERROR: " << e.what() <<
"\n";
293 catch (
const std::exception& e) {
294 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
299 catch (
const std::exception& e) {
300 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
304 std::cerr <<
"Caught unknown exception\n";
const std::string & GravityFile() const
GeographicLib::Math::real real
void SphericalAnomaly(real lat, real lon, real h, real &Dg01, real &xi, real &eta) const
Header for GeographicLib::Utility class.
static bool isfinite(T x)
Header for GeographicLib::GravityModel class.
Math::real Gravity(real lat, real lon, real h, real &gx, real &gy, real &gz) const
const std::string & GravityModelName() const
static int extra_digits()
Math::real Disturbance(real lat, real lon, real h, real &deltax, real &deltay, real &deltaz) const
Math::real GeoidHeight(real lat, real lon) const
const std::string & Description() const
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
GravityCircle Circle(real lat, real h, unsigned caps=ALL) const
static Math::real Decode(const std::string &dms, flag &ind)
static std::string str(T x, int p=-1)
int main(int argc, char *argv[])
static int set_digits(int ndigits=0)
Model of the earth's gravity field.
static std::string DefaultGravityName()
Exception handling for GeographicLib.
static std::string DefaultGravityPath()
const std::string & DateTime() const
Header for GeographicLib::GravityCircle class.
Gravity on a circle of latitude.
Header for GeographicLib::DMS class.