GeographicLib  2.6
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) <karney@alum.mit.edu> 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 #include "Gravity.usage"
22 
23 int main(int argc, const char* const argv[]) {
24  try {
25  using namespace GeographicLib;
26  typedef Math::real real;
28  bool verbose = false, longfirst = false;
29  std::string dir;
30  std::string model = GravityModel::DefaultGravityName();
31  std::string istring, ifile, ofile, cdelim;
32  char lsep = ';';
33  real lat = 0, h = 0;
34  bool circle = false;
35  int prec = -1, Nmax = -1, Mmax = -1;
36  enum {
37  GRAVITY = 0,
38  DISTURBANCE = 1,
39  ANOMALY = 2,
40  UNDULATION = 3,
41  };
42  unsigned mode = GRAVITY;
43  for (int m = 1; m < argc; ++m) {
44  std::string arg(argv[m]);
45  if (arg == "-n") {
46  if (++m == argc) return usage(1, true);
47  model = argv[m];
48  } else if (arg == "-d") {
49  if (++m == argc) return usage(1, true);
50  dir = argv[m];
51  } else if (arg == "-N") {
52  if (++m == argc) return usage(1, true);
53  try {
54  Nmax = Utility::val<int>(std::string(argv[m]));
55  if (Nmax < 0) {
56  std::cerr << "Maximum degree " << argv[m] << " is negative\n";
57  return 1;
58  }
59  }
60  catch (const std::exception&) {
61  std::cerr << "Precision " << argv[m] << " is not a number\n";
62  return 1;
63  }
64  } else if (arg == "-M") {
65  if (++m == argc) return usage(1, true);
66  try {
67  Mmax = Utility::val<int>(std::string(argv[m]));
68  if (Mmax < 0) {
69  std::cerr << "Maximum order " << argv[m] << " is negative\n";
70  return 1;
71  }
72  }
73  catch (const std::exception&) {
74  std::cerr << "Precision " << argv[m] << " is not a number\n";
75  return 1;
76  }
77  } else if (arg == "-G")
78  mode = GRAVITY;
79  else if (arg == "-D")
80  mode = DISTURBANCE;
81  else if (arg == "-A")
82  mode = ANOMALY;
83  else if (arg == "-H")
84  mode = UNDULATION;
85  else if (arg == "-c") {
86  if (m + 2 >= argc) return usage(1, true);
87  try {
88  using std::fabs;
89  DMS::flag ind;
90  lat = DMS::Decode(std::string(argv[++m]), ind);
91  if (ind == DMS::LONGITUDE)
92  throw GeographicErr("Bad hemisphere letter on latitude");
93  if (!(fabs(lat) <= Math::qd))
94  throw GeographicErr("Latitude not in [-" + std::to_string(Math::qd)
95  + "d, " + std::to_string(Math::qd) + "d]");
96  h = Utility::val<real>(std::string(argv[++m]));
97  circle = true;
98  }
99  catch (const std::exception& e) {
100  std::cerr << "Error decoding argument of " << arg << ": "
101  << e.what() << "\n";
102  return 1;
103  }
104  } else if (arg == "-w")
105  longfirst = !longfirst;
106  else if (arg == "-p") {
107  if (++m == argc) return usage(1, true);
108  try {
109  prec = Utility::val<int>(std::string(argv[m]));
110  }
111  catch (const std::exception&) {
112  std::cerr << "Precision " << argv[m] << " is not a number\n";
113  return 1;
114  }
115  } else if (arg == "-v")
116  verbose = true;
117  else if (arg == "--input-string") {
118  if (++m == argc) return usage(1, true);
119  istring = argv[m];
120  } else if (arg == "--input-file") {
121  if (++m == argc) return usage(1, true);
122  ifile = argv[m];
123  } else if (arg == "--output-file") {
124  if (++m == argc) return usage(1, true);
125  ofile = argv[m];
126  } else if (arg == "--line-separator") {
127  if (++m == argc) return usage(1, true);
128  if (std::string(argv[m]).size() != 1) {
129  std::cerr << "Line separator must be a single character\n";
130  return 1;
131  }
132  lsep = argv[m][0];
133  } else if (arg == "--comment-delimiter") {
134  if (++m == argc) return usage(1, true);
135  cdelim = argv[m];
136  } else if (arg == "--version") {
137  std::cout << argv[0] << ": GeographicLib version "
138  << GEOGRAPHICLIB_VERSION_STRING << "\n";
139  return 0;
140  } else {
141  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
142  if (arg == "-h")
143  std::cout<< "\nDefault gravity path = \""
145  << "\"\nDefault gravity name = \""
147  << "\"\n";
148  return retval;
149  }
150  }
151 
152  if (!ifile.empty() && !istring.empty()) {
153  std::cerr << "Cannot specify --input-string and --input-file together\n";
154  return 1;
155  }
156  if (ifile == "-") ifile.clear();
157  std::ifstream infile;
158  std::istringstream instring;
159  if (!ifile.empty()) {
160  infile.open(ifile.c_str());
161  if (!infile.is_open()) {
162  std::cerr << "Cannot open " << ifile << " for reading\n";
163  return 1;
164  }
165  } else if (!istring.empty()) {
166  std::string::size_type m = 0;
167  while (true) {
168  m = istring.find(lsep, m);
169  if (m == std::string::npos)
170  break;
171  istring[m] = '\n';
172  }
173  instring.str(istring);
174  }
175  std::istream* input = !ifile.empty() ? &infile :
176  (!istring.empty() ? &instring : &std::cin);
177 
178  std::ofstream outfile;
179  if (ofile == "-") ofile.clear();
180  if (!ofile.empty()) {
181  outfile.open(ofile.c_str());
182  if (!outfile.is_open()) {
183  std::cerr << "Cannot open " << ofile << " for writing\n";
184  return 1;
185  }
186  }
187  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
188 
189  switch (mode) {
190  case GRAVITY:
191  prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
192  break;
193  case DISTURBANCE:
194  case ANOMALY:
195  prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
196  break;
197  case UNDULATION:
198  default:
199  prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
200  break;
201  }
202  int retval = 0;
203  try {
204  using std::isfinite;
205  const GravityModel g(model, dir, Nmax, Mmax);
206  if (circle) {
207  if (!isfinite(h))
208  throw GeographicErr("Bad height");
209  else if (mode == UNDULATION && h != 0)
210  throw GeographicErr("Height should be zero for geoid undulations");
211  }
212  if (verbose) {
213  std::cerr << "Gravity file: " << g.GravityFile() << "\n"
214  << "Name: " << g.GravityModelName() << "\n"
215  << "Description: " << g.Description() << "\n"
216  << "Date & Time: " << g.DateTime() << "\n";
217  }
218  unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
219  (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
220  (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
221  GravityModel::GEOID_HEIGHT))); // mode == UNDULATION
222  const GravityCircle c(circle ? g.Circle(lat, h, mask) : GravityCircle());
223  std::string s, eol, stra, strb;
224  std::istringstream str;
225  while (std::getline(*input, s)) {
226  try {
227  eol = "\n";
228  if (!cdelim.empty()) {
229  std::string::size_type m = s.find(cdelim);
230  if (m != std::string::npos) {
231  eol = " " + s.substr(m) + "\n";
232  s = s.substr(0, m);
233  }
234  }
235  str.clear(); str.str(s);
236  real lon;
237  if (circle) {
238  if (!(str >> strb))
239  throw GeographicErr("Incomplete input: " + s);
240  DMS::flag ind;
241  lon = DMS::Decode(strb, ind);
242  if (ind == DMS::LATITUDE)
243  throw GeographicErr("Bad hemisphere letter on " + strb);
244  } else {
245  if (!(str >> stra >> strb))
246  throw GeographicErr("Incomplete input: " + s);
247  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
248  h = 0;
249  if (!(str >> h)) // h is optional
250  str.clear();
251  if (mode == UNDULATION && h != 0)
252  throw GeographicErr("Height must be zero for geoid heights");
253  }
254  if (str >> stra)
255  throw GeographicErr("Extra junk in input: " + s);
256  switch (mode) {
257  case GRAVITY:
258  {
259  real gx, gy, gz;
260  if (circle) {
261  c.Gravity(lon, gx, gy, gz);
262  } else {
263  g.Gravity(lat, lon, h, gx, gy, gz);
264  }
265  *output << Utility::str(gx, prec) << " "
266  << Utility::str(gy, prec) << " "
267  << Utility::str(gz, prec) << eol;
268  }
269  break;
270  case DISTURBANCE:
271  {
272  real deltax, deltay, deltaz;
273  if (circle) {
274  c.Disturbance(lon, deltax, deltay, deltaz);
275  } else {
276  g.Disturbance(lat, lon, h, deltax, deltay, deltaz);
277  }
278  // Convert to mGals
279  *output << Utility::str(deltax * 100000, prec) << " "
280  << Utility::str(deltay * 100000, prec) << " "
281  << Utility::str(deltaz * 100000, prec)
282  << eol;
283  }
284  break;
285  case ANOMALY:
286  {
287  real Dg01, xi, eta;
288  if (circle)
289  c.SphericalAnomaly(lon, Dg01, xi, eta);
290  else
291  g.SphericalAnomaly(lat, lon, h, Dg01, xi, eta);
292  Dg01 *= 100000; // Convert to mGals
293  xi *= Math::ds; // Convert to arcsecs
294  eta *= Math::ds;
295  *output << Utility::str(Dg01, prec) << " "
296  << Utility::str(xi, prec) << " "
297  << Utility::str(eta, prec) << eol;
298  }
299  break;
300  case UNDULATION:
301  default:
302  {
303  real N = circle ? c.GeoidHeight(lon) : g.GeoidHeight(lat, lon);
304  *output << Utility::str(N, prec) << eol;
305  }
306  break;
307  }
308  }
309  catch (const std::exception& e) {
310  *output << "ERROR: " << e.what() << "\n";
311  retval = 1;
312  }
313  }
314  }
315  catch (const std::exception& e) {
316  std::cerr << "Error reading " << model << ": " << e.what() << "\n";
317  retval = 1;
318  }
319  return retval;
320  }
321  catch (const std::exception& e) {
322  std::cerr << "Caught exception: " << e.what() << "\n";
323  return 1;
324  }
325  catch (...) {
326  std::cerr << "Caught unknown exception\n";
327  return 1;
328  }
329 }
static std::string DefaultGravityName()
const std::string & GravityModelName() const
const std::string & Description() const
static constexpr int ds
seconds per degree
Definition: Math.hpp:147
static std::string DefaultGravityPath()
Header for GeographicLib::Utility class.
Header for GeographicLib::GravityModel class.
const std::string & DateTime() const
static int extra_digits()
Definition: Math.cpp:49
int usage(int retval, bool)
Definition: Geod3ODE.cpp:41
int main(int argc, const char *const argv[])
Definition: Gravity.cpp:23
Math::real Gravity(real lat, real lon, real h, real &gx, real &gy, real &gz) const
static constexpr int qd
degrees per quarter turn
Definition: Math.hpp:142
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:40
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:161
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)
Definition: DMS.cpp:374
GeographicLib::Math::real real
Definition: Geod3Solve.cpp:25
static int set_digits(int ndigits=0)
Definition: Utility.cpp:184
Model of the earth&#39;s gravity field.
GravityCircle Circle(real lat, real h, unsigned caps=ALL) const
Exception handling for GeographicLib.
Definition: Constants.hpp:344
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.