24# pragma warning (disable: 4127 5055 4701)
27#include "Gravity.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 bool verbose =
false, longfirst =
false;
36 std::string model = GravityModel::DefaultGravityName();
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);
96 lat = DMS::Decode(std::string(argv[++m]), ind);
97 if (ind == DMS::LONGITUDE)
99 if (!(fabs(lat) <= Math::qd))
100 throw GeographicErr(
"Latitude not in [-" + std::to_string(Math::qd)
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 = \""
150 << GravityModel::DefaultGravityPath()
151 <<
"\"\nDefault gravity name = \""
152 << GravityModel::DefaultGravityName()
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;
197 prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
201 prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
205 prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
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";
224 unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
225 (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
226 (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
227 GravityModel::GEOID_HEIGHT)));
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);
247 lon = DMS::Decode(strb, ind);
248 if (ind == DMS::LATITUDE)
251 if (!(str >> stra >> strb))
253 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
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);
271 *output << Utility::str(gx, prec) <<
" "
272 << Utility::str(gy, prec) <<
" "
273 << Utility::str(gz, prec) << eol;
278 real deltax, deltay, deltaz;
282 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
285 *output << Utility::str(deltax * 100000, prec) <<
" "
286 << Utility::str(deltay * 100000, prec) <<
" "
287 << Utility::str(deltaz * 100000, prec)
301 *output << Utility::str(Dg01, prec) <<
" "
302 << Utility::str(xi, prec) <<
" "
303 << Utility::str(eta, prec) << eol;
310 *output << Utility::str(N, prec) << eol;
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.
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.
Math::real Disturbance(real lat, real lon, real h, real &deltax, real &deltay, real &deltaz) const
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
Math::real GeoidHeight(real lat, real lon) const
const std::string & GravityModelName() const
Math::real Gravity(real lat, real lon, real h, real &gx, real &gy, real &gz) const
const std::string & GravityFile() const
const std::string & Description() const
const std::string & DateTime() const
Namespace for GeographicLib.