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