37 # pragma warning (disable: 4127 4701)
40 #include "Gravity.usage"
42 int main(
int argc,
char* argv[]) {
44 using namespace GeographicLib;
49 std::string istring, ifile, ofile, cdelim;
60 unsigned mode = GRAVITY;
61 for (
int m = 1; m < argc; ++m) {
62 std::string arg(argv[m]);
64 if (++m == argc)
return usage(1,
true);
66 }
else if (arg ==
"-d") {
67 if (++m == argc)
return usage(1,
true);
69 }
else if (arg ==
"-G")
77 else if (arg ==
"-c") {
78 if (m + 2 >= argc)
return usage(1,
true);
84 if (!(std::abs(lat) <= 90))
86 h = Utility::num<real>(std::string(argv[++m]));
89 catch (
const std::exception& e) {
90 std::cerr <<
"Error decoding argument of " << arg <<
": "
94 }
else if (arg ==
"-p") {
95 if (++m == argc)
return usage(1,
true);
97 prec = Utility::num<int>(std::string(argv[m]));
99 catch (
const std::exception&) {
100 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
103 }
else if (arg ==
"-v")
105 else if (arg ==
"--input-string") {
106 if (++m == argc)
return usage(1,
true);
108 }
else if (arg ==
"--input-file") {
109 if (++m == argc)
return usage(1,
true);
111 }
else if (arg ==
"--output-file") {
112 if (++m == argc)
return usage(1,
true);
114 }
else if (arg ==
"--line-separator") {
115 if (++m == argc)
return usage(1,
true);
116 if (std::string(argv[m]).size() != 1) {
117 std::cerr <<
"Line separator must be a single character\n";
121 }
else if (arg ==
"--comment-delimiter") {
122 if (++m == argc)
return usage(1,
true);
124 }
else if (arg ==
"--version") {
126 << argv[0] <<
": GeographicLib version "
127 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
130 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
132 std::cout<<
"\nDefault gravity path = \""
134 <<
"\"\nDefault gravity name = \""
141 if (!ifile.empty() && !istring.empty()) {
142 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
145 if (ifile ==
"-") ifile.clear();
146 std::ifstream infile;
147 std::istringstream instring;
148 if (!ifile.empty()) {
149 infile.open(ifile.c_str());
150 if (!infile.is_open()) {
151 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
154 }
else if (!istring.empty()) {
155 std::string::size_type m = 0;
157 m = istring.find(lsep, m);
158 if (m == std::string::npos)
162 instring.str(istring);
164 std::istream* input = !ifile.empty() ? &infile :
165 (!istring.empty() ? &instring : &std::cin);
167 std::ofstream outfile;
168 if (ofile ==
"-") ofile.clear();
169 if (!ofile.empty()) {
170 outfile.open(ofile.c_str());
171 if (!outfile.is_open()) {
172 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
176 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
180 prec = std::min(16, prec < 0 ? 5 : prec);
184 prec = std::min(14, prec < 0 ? 3 : prec);
188 prec = std::min(12, prec < 0 ? 4 : prec);
195 if (!Math::isfinite<real>(h))
197 else if (mode == UNDULATION && h != 0)
198 throw GeographicErr(
"Height should be zero for geoid undulations");
201 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n"
204 <<
"Date & Time: " << g.
DateTime() <<
"\n";
211 std::string s, stra, strb;
212 while (std::getline(*input, s)) {
214 std::string eol(
"\n");
215 if (!cdelim.empty()) {
216 std::string::size_type m = s.find(cdelim);
217 if (m != std::string::npos) {
218 eol =
" " + s.substr(m) +
"\n";
222 std::istringstream str(s);
231 if (lon < -540 || lon >= 540)
233 " not in [-540d, 540d)");
235 if (!(str >> stra >> strb))
241 if (mode == UNDULATION && h != 0)
242 throw GeographicErr(
"Height must be zero for geoid heights");
251 c.Gravity(lon, gx, gy, gz);
253 g.
Gravity(lat, lon, h, gx, gy, gz);
255 *output << Utility::str<real>(gx, prec) <<
" "
256 << Utility::str<real>(gy, prec) <<
" "
257 << Utility::str<real>(gz, prec) << eol;
262 real deltax, deltay, deltaz;
264 c.Disturbance(lon, deltax, deltay, deltaz);
266 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
269 *output << Utility::str<real>(deltax * 1e5, prec) <<
" "
270 << Utility::str<real>(deltay * 1e5, prec) <<
" "
271 << Utility::str<real>(deltaz * 1e5, prec)
279 c.SphericalAnomaly(lon, Dg01, xi, eta);
285 *output << Utility::str<real>(Dg01, prec) <<
" "
286 << Utility::str<real>(xi, prec) <<
" "
287 << Utility::str<real>(eta, prec) << eol;
294 *output << Utility::str<real>(N, prec) << eol;
299 catch (
const std::exception& e) {
300 *output <<
"ERROR: " << e.what() <<
"\n";
305 catch (
const std::exception& e) {
306 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
311 catch (
const std::exception& e) {
312 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
316 std::cerr <<
"Caught unknown exception\n";
const std::string & GravityFile() const
int main(int argc, char *argv[])
GeographicLib::Math::real real
void SphericalAnomaly(real lat, real lon, real h, real &Dg01, real &xi, real &eta) const
Header for GeographicLib::Utility class.
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
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)
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.