GeographicLib  1.43
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  * 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, char* argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
34  bool verbose = 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;
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 == "-G")
58  mode = GRAVITY;
59  else if (arg == "-D")
60  mode = DISTURBANCE;
61  else if (arg == "-A")
62  mode = ANOMALY;
63  else if (arg == "-H")
64  mode = UNDULATION;
65  else if (arg == "-c") {
66  if (m + 2 >= argc) return usage(1, true);
67  try {
68  using std::abs;
69  DMS::flag ind;
70  lat = DMS::Decode(std::string(argv[++m]), ind);
71  if (ind == DMS::LONGITUDE)
72  throw GeographicErr("Bad hemisphere letter on latitude");
73  if (!(abs(lat) <= 90))
74  throw GeographicErr("Latitude not in [-90d, 90d]");
75  h = Utility::num<real>(std::string(argv[++m]));
76  circle = true;
77  }
78  catch (const std::exception& e) {
79  std::cerr << "Error decoding argument of " << arg << ": "
80  << e.what() << "\n";
81  return 1;
82  }
83  } else if (arg == "-p") {
84  if (++m == argc) return usage(1, true);
85  try {
86  prec = Utility::num<int>(std::string(argv[m]));
87  }
88  catch (const std::exception&) {
89  std::cerr << "Precision " << argv[m] << " is not a number\n";
90  return 1;
91  }
92  } else if (arg == "-v")
93  verbose = true;
94  else if (arg == "--input-string") {
95  if (++m == argc) return usage(1, true);
96  istring = argv[m];
97  } else if (arg == "--input-file") {
98  if (++m == argc) return usage(1, true);
99  ifile = argv[m];
100  } else if (arg == "--output-file") {
101  if (++m == argc) return usage(1, true);
102  ofile = argv[m];
103  } else if (arg == "--line-separator") {
104  if (++m == argc) return usage(1, true);
105  if (std::string(argv[m]).size() != 1) {
106  std::cerr << "Line separator must be a single character\n";
107  return 1;
108  }
109  lsep = argv[m][0];
110  } else if (arg == "--comment-delimiter") {
111  if (++m == argc) return usage(1, true);
112  cdelim = argv[m];
113  } else if (arg == "--version") {
114  std::cout << argv[0] << ": GeographicLib version "
115  << GEOGRAPHICLIB_VERSION_STRING << "\n";
116  return 0;
117  } else {
118  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
119  if (arg == "-h")
120  std::cout<< "\nDefault gravity path = \""
122  << "\"\nDefault gravity name = \""
124  << "\"\n";
125  return retval;
126  }
127  }
128 
129  if (!ifile.empty() && !istring.empty()) {
130  std::cerr << "Cannot specify --input-string and --input-file together\n";
131  return 1;
132  }
133  if (ifile == "-") ifile.clear();
134  std::ifstream infile;
135  std::istringstream instring;
136  if (!ifile.empty()) {
137  infile.open(ifile.c_str());
138  if (!infile.is_open()) {
139  std::cerr << "Cannot open " << ifile << " for reading\n";
140  return 1;
141  }
142  } else if (!istring.empty()) {
143  std::string::size_type m = 0;
144  while (true) {
145  m = istring.find(lsep, m);
146  if (m == std::string::npos)
147  break;
148  istring[m] = '\n';
149  }
150  instring.str(istring);
151  }
152  std::istream* input = !ifile.empty() ? &infile :
153  (!istring.empty() ? &instring : &std::cin);
154 
155  std::ofstream outfile;
156  if (ofile == "-") ofile.clear();
157  if (!ofile.empty()) {
158  outfile.open(ofile.c_str());
159  if (!outfile.is_open()) {
160  std::cerr << "Cannot open " << ofile << " for writing\n";
161  return 1;
162  }
163  }
164  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
165 
166  switch (mode) {
167  case GRAVITY:
168  prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
169  break;
170  case DISTURBANCE:
171  case ANOMALY:
172  prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
173  break;
174  case UNDULATION:
175  default:
176  prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
177  break;
178  }
179  int retval = 0;
180  try {
181  const GravityModel g(model, dir);
182  if (circle) {
183  if (!Math::isfinite(h))
184  throw GeographicErr("Bad height");
185  else if (mode == UNDULATION && h != 0)
186  throw GeographicErr("Height should be zero for geoid undulations");
187  }
188  if (verbose) {
189  std::cerr << "Gravity file: " << g.GravityFile() << "\n"
190  << "Name: " << g.GravityModelName() << "\n"
191  << "Description: " << g.Description() << "\n"
192  << "Date & Time: " << g.DateTime() << "\n";
193  }
194  unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
195  (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
196  (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
197  GravityModel::GEOID_HEIGHT))); // mode == UNDULATION
198  const GravityCircle c(circle ? g.Circle(lat, h, mask) : GravityCircle());
199  std::string s, stra, strb;
200  while (std::getline(*input, s)) {
201  try {
202  std::string eol("\n");
203  if (!cdelim.empty()) {
204  std::string::size_type m = s.find(cdelim);
205  if (m != std::string::npos) {
206  eol = " " + s.substr(m) + "\n";
207  s = s.substr(0, m);
208  }
209  }
210  std::istringstream str(s);
211  real lon;
212  if (circle) {
213  if (!(str >> strb))
214  throw GeographicErr("Incomplete input: " + s);
215  DMS::flag ind;
216  lon = DMS::Decode(strb, ind);
217  if (ind == DMS::LATITUDE)
218  throw GeographicErr("Bad hemisphere letter on " + strb);
219  if (lon < -540 || lon >= 540)
220  throw GeographicErr("Longitude " + strb +
221  " not in [-540d, 540d)");
222  } else {
223  if (!(str >> stra >> strb))
224  throw GeographicErr("Incomplete input: " + s);
225  DMS::DecodeLatLon(stra, strb, lat, lon);
226  h = 0;
227  if (!(str >> h)) // h is optional
228  str.clear();
229  if (mode == UNDULATION && h != 0)
230  throw GeographicErr("Height must be zero for geoid heights");
231  }
232  if (str >> stra)
233  throw GeographicErr("Extra junk in input: " + s);
234  switch (mode) {
235  case GRAVITY:
236  {
237  real gx, gy, gz;
238  if (circle) {
239  c.Gravity(lon, gx, gy, gz);
240  } else {
241  g.Gravity(lat, lon, h, gx, gy, gz);
242  }
243  *output << Utility::str(gx, prec) << " "
244  << Utility::str(gy, prec) << " "
245  << Utility::str(gz, prec) << eol;
246  }
247  break;
248  case DISTURBANCE:
249  {
250  real deltax, deltay, deltaz;
251  if (circle) {
252  c.Disturbance(lon, deltax, deltay, deltaz);
253  } else {
254  g.Disturbance(lat, lon, h, deltax, deltay, deltaz);
255  }
256  // Convert to mGals
257  *output << Utility::str(deltax * 1e5, prec) << " "
258  << Utility::str(deltay * 1e5, prec) << " "
259  << Utility::str(deltaz * 1e5, prec)
260  << eol;
261  }
262  break;
263  case ANOMALY:
264  {
265  real Dg01, xi, eta;
266  if (circle)
267  c.SphericalAnomaly(lon, Dg01, xi, eta);
268  else
269  g.SphericalAnomaly(lat, lon, h, Dg01, xi, eta);
270  Dg01 *= 1e5; // Convert to mGals
271  xi *= 3600; // Convert to arcsecs
272  eta *= 3600;
273  *output << Utility::str(Dg01, prec) << " "
274  << Utility::str(xi, prec) << " "
275  << Utility::str(eta, prec) << eol;
276  }
277  break;
278  case UNDULATION:
279  default:
280  {
281  real N = circle ? c.GeoidHeight(lon) : g.GeoidHeight(lat, lon);
282  *output << Utility::str(N, prec) << eol;
283  }
284  break;
285  }
286  }
287  catch (const std::exception& e) {
288  *output << "ERROR: " << e.what() << "\n";
289  retval = 1;
290  }
291  }
292  }
293  catch (const std::exception& e) {
294  std::cerr << "Error reading " << model << ": " << e.what() << "\n";
295  retval = 1;
296  }
297  return retval;
298  }
299  catch (const std::exception& e) {
300  std::cerr << "Caught exception: " << e.what() << "\n";
301  return 1;
302  }
303  catch (...) {
304  std::cerr << "Caught unknown exception\n";
305  return 1;
306  }
307 }
const std::string & GravityFile() const
int main(int argc, char *argv[])
Definition: Gravity.cpp:29
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
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:614
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:185
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:246
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:276
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:382
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.