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