GeographicLib 2.1.2
Gravity.cpp
Go to the documentation of this file.
1/**
2 * \file Gravity.cpp
3 * \brief Command line utility for evaluating gravity fields
4 *
5 * Copyright (c) Charles Karney (2011-2022) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * See the <a href="Gravity.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
18#include <GeographicLib/DMS.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about constant conditional and enum-float expressions
23// and potentially uninitialized local variables
24# pragma warning (disable: 4127 5055 4701)
25#endif
26
27#include "Gravity.usage"
28
29int main(int argc, const char* const argv[]) {
30 try {
31 using namespace GeographicLib;
32 typedef Math::real real;
33 Utility::set_digits();
34 bool verbose = false, longfirst = false;
35 std::string dir;
36 std::string model = GravityModel::DefaultGravityName();
37 std::string istring, ifile, ofile, cdelim;
38 char lsep = ';';
39 real lat = 0, h = 0;
40 bool circle = false;
41 int prec = -1, Nmax = -1, Mmax = -1;
42 enum {
43 GRAVITY = 0,
44 DISTURBANCE = 1,
45 ANOMALY = 2,
46 UNDULATION = 3,
47 };
48 unsigned mode = GRAVITY;
49 for (int m = 1; m < argc; ++m) {
50 std::string arg(argv[m]);
51 if (arg == "-n") {
52 if (++m == argc) return usage(1, true);
53 model = argv[m];
54 } else if (arg == "-d") {
55 if (++m == argc) return usage(1, true);
56 dir = argv[m];
57 } else if (arg == "-N") {
58 if (++m == argc) return usage(1, true);
59 try {
60 Nmax = Utility::val<int>(std::string(argv[m]));
61 if (Nmax < 0) {
62 std::cerr << "Maximum degree " << argv[m] << " is negative\n";
63 return 1;
64 }
65 }
66 catch (const std::exception&) {
67 std::cerr << "Precision " << argv[m] << " is not a number\n";
68 return 1;
69 }
70 } else if (arg == "-M") {
71 if (++m == argc) return usage(1, true);
72 try {
73 Mmax = Utility::val<int>(std::string(argv[m]));
74 if (Mmax < 0) {
75 std::cerr << "Maximum order " << argv[m] << " is negative\n";
76 return 1;
77 }
78 }
79 catch (const std::exception&) {
80 std::cerr << "Precision " << argv[m] << " is not a number\n";
81 return 1;
82 }
83 } else if (arg == "-G")
84 mode = GRAVITY;
85 else if (arg == "-D")
86 mode = DISTURBANCE;
87 else if (arg == "-A")
88 mode = ANOMALY;
89 else if (arg == "-H")
90 mode = UNDULATION;
91 else if (arg == "-c") {
92 if (m + 2 >= argc) return usage(1, true);
93 try {
94 using std::fabs;
95 DMS::flag ind;
96 lat = DMS::Decode(std::string(argv[++m]), ind);
97 if (ind == DMS::LONGITUDE)
98 throw GeographicErr("Bad hemisphere letter on latitude");
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]));
103 circle = true;
104 }
105 catch (const std::exception& e) {
106 std::cerr << "Error decoding argument of " << arg << ": "
107 << e.what() << "\n";
108 return 1;
109 }
110 } else if (arg == "-w")
111 longfirst = !longfirst;
112 else if (arg == "-p") {
113 if (++m == argc) return usage(1, true);
114 try {
115 prec = Utility::val<int>(std::string(argv[m]));
116 }
117 catch (const std::exception&) {
118 std::cerr << "Precision " << argv[m] << " is not a number\n";
119 return 1;
120 }
121 } else if (arg == "-v")
122 verbose = true;
123 else if (arg == "--input-string") {
124 if (++m == argc) return usage(1, true);
125 istring = argv[m];
126 } else if (arg == "--input-file") {
127 if (++m == argc) return usage(1, true);
128 ifile = argv[m];
129 } else if (arg == "--output-file") {
130 if (++m == argc) return usage(1, true);
131 ofile = argv[m];
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";
136 return 1;
137 }
138 lsep = argv[m][0];
139 } else if (arg == "--comment-delimiter") {
140 if (++m == argc) return usage(1, true);
141 cdelim = argv[m];
142 } else if (arg == "--version") {
143 std::cout << argv[0] << ": GeographicLib version "
144 << GEOGRAPHICLIB_VERSION_STRING << "\n";
145 return 0;
146 } else {
147 int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
148 if (arg == "-h")
149 std::cout<< "\nDefault gravity path = \""
150 << GravityModel::DefaultGravityPath()
151 << "\"\nDefault gravity name = \""
152 << GravityModel::DefaultGravityName()
153 << "\"\n";
154 return retval;
155 }
156 }
157
158 if (!ifile.empty() && !istring.empty()) {
159 std::cerr << "Cannot specify --input-string and --input-file together\n";
160 return 1;
161 }
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";
169 return 1;
170 }
171 } else if (!istring.empty()) {
172 std::string::size_type m = 0;
173 while (true) {
174 m = istring.find(lsep, m);
175 if (m == std::string::npos)
176 break;
177 istring[m] = '\n';
178 }
179 instring.str(istring);
180 }
181 std::istream* input = !ifile.empty() ? &infile :
182 (!istring.empty() ? &instring : &std::cin);
183
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";
190 return 1;
191 }
192 }
193 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
194
195 switch (mode) {
196 case GRAVITY:
197 prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
198 break;
199 case DISTURBANCE:
200 case ANOMALY:
201 prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
202 break;
203 case UNDULATION:
204 default:
205 prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
206 break;
207 }
208 int retval = 0;
209 try {
210 using std::isfinite;
211 const GravityModel g(model, dir, Nmax, Mmax);
212 if (circle) {
213 if (!isfinite(h))
214 throw GeographicErr("Bad height");
215 else if (mode == UNDULATION && h != 0)
216 throw GeographicErr("Height should be zero for geoid undulations");
217 }
218 if (verbose) {
219 std::cerr << "Gravity file: " << g.GravityFile() << "\n"
220 << "Name: " << g.GravityModelName() << "\n"
221 << "Description: " << g.Description() << "\n"
222 << "Date & Time: " << g.DateTime() << "\n";
223 }
224 unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
225 (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
226 (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
227 GravityModel::GEOID_HEIGHT))); // mode == UNDULATION
228 const GravityCircle c(circle ? g.Circle(lat, h, mask) : GravityCircle());
229 std::string s, eol, stra, strb;
230 std::istringstream str;
231 while (std::getline(*input, s)) {
232 try {
233 eol = "\n";
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";
238 s = s.substr(0, m);
239 }
240 }
241 str.clear(); str.str(s);
242 real lon;
243 if (circle) {
244 if (!(str >> strb))
245 throw GeographicErr("Incomplete input: " + s);
246 DMS::flag ind;
247 lon = DMS::Decode(strb, ind);
248 if (ind == DMS::LATITUDE)
249 throw GeographicErr("Bad hemisphere letter on " + strb);
250 } else {
251 if (!(str >> stra >> strb))
252 throw GeographicErr("Incomplete input: " + s);
253 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
254 h = 0;
255 if (!(str >> h)) // h is optional
256 str.clear();
257 if (mode == UNDULATION && h != 0)
258 throw GeographicErr("Height must be zero for geoid heights");
259 }
260 if (str >> stra)
261 throw GeographicErr("Extra junk in input: " + s);
262 switch (mode) {
263 case GRAVITY:
264 {
265 real gx, gy, gz;
266 if (circle) {
267 c.Gravity(lon, gx, gy, gz);
268 } else {
269 g.Gravity(lat, lon, h, gx, gy, gz);
270 }
271 *output << Utility::str(gx, prec) << " "
272 << Utility::str(gy, prec) << " "
273 << Utility::str(gz, prec) << eol;
274 }
275 break;
276 case DISTURBANCE:
277 {
278 real deltax, deltay, deltaz;
279 if (circle) {
280 c.Disturbance(lon, deltax, deltay, deltaz);
281 } else {
282 g.Disturbance(lat, lon, h, deltax, deltay, deltaz);
283 }
284 // Convert to mGals
285 *output << Utility::str(deltax * 100000, prec) << " "
286 << Utility::str(deltay * 100000, prec) << " "
287 << Utility::str(deltaz * 100000, prec)
288 << eol;
289 }
290 break;
291 case ANOMALY:
292 {
293 real Dg01, xi, eta;
294 if (circle)
295 c.SphericalAnomaly(lon, Dg01, xi, eta);
296 else
297 g.SphericalAnomaly(lat, lon, h, Dg01, xi, eta);
298 Dg01 *= 100000; // Convert to mGals
299 xi *= Math::ds; // Convert to arcsecs
300 eta *= Math::ds;
301 *output << Utility::str(Dg01, prec) << " "
302 << Utility::str(xi, prec) << " "
303 << Utility::str(eta, prec) << eol;
304 }
305 break;
306 case UNDULATION:
307 default:
308 {
309 real N = circle ? c.GeoidHeight(lon) : g.GeoidHeight(lat, lon);
310 *output << Utility::str(N, prec) << eol;
311 }
312 break;
313 }
314 }
315 catch (const std::exception& e) {
316 *output << "ERROR: " << e.what() << "\n";
317 retval = 1;
318 }
319 }
320 }
321 catch (const std::exception& e) {
322 std::cerr << "Error reading " << model << ": " << e.what() << "\n";
323 retval = 1;
324 }
325 return retval;
326 }
327 catch (const std::exception& e) {
328 std::cerr << "Caught exception: " << e.what() << "\n";
329 return 1;
330 }
331 catch (...) {
332 std::cerr << "Caught unknown exception\n";
333 return 1;
334 }
335}
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::GravityCircle class.
Header for GeographicLib::GravityModel class.
int main(int argc, const char *const argv[])
Definition: Gravity.cpp:29
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
Definition: Constants.hpp:316
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.
Definition: Accumulator.cpp:12