GeographicLib  1.42
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
115  << argv[0] << ": GeographicLib version "
116  << GEOGRAPHICLIB_VERSION_STRING << "\n";
117  return 0;
118  } else {
119  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
120  if (arg == "-h")
121  std::cout<< "\nDefault gravity path = \""
123  << "\"\nDefault gravity name = \""
125  << "\"\n";
126  return retval;
127  }
128  }
129 
130  if (!ifile.empty() && !istring.empty()) {
131  std::cerr << "Cannot specify --input-string and --input-file together\n";
132  return 1;
133  }
134  if (ifile == "-") ifile.clear();
135  std::ifstream infile;
136  std::istringstream instring;
137  if (!ifile.empty()) {
138  infile.open(ifile.c_str());
139  if (!infile.is_open()) {
140  std::cerr << "Cannot open " << ifile << " for reading\n";
141  return 1;
142  }
143  } else if (!istring.empty()) {
144  std::string::size_type m = 0;
145  while (true) {
146  m = istring.find(lsep, m);
147  if (m == std::string::npos)
148  break;
149  istring[m] = '\n';
150  }
151  instring.str(istring);
152  }
153  std::istream* input = !ifile.empty() ? &infile :
154  (!istring.empty() ? &instring : &std::cin);
155 
156  std::ofstream outfile;
157  if (ofile == "-") ofile.clear();
158  if (!ofile.empty()) {
159  outfile.open(ofile.c_str());
160  if (!outfile.is_open()) {
161  std::cerr << "Cannot open " << ofile << " for writing\n";
162  return 1;
163  }
164  }
165  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
166 
167  switch (mode) {
168  case GRAVITY:
169  prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
170  break;
171  case DISTURBANCE:
172  case ANOMALY:
173  prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
174  break;
175  case UNDULATION:
176  default:
177  prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
178  break;
179  }
180  int retval = 0;
181  try {
182  const GravityModel g(model, dir);
183  if (circle) {
184  if (!Math::isfinite(h))
185  throw GeographicErr("Bad height");
186  else if (mode == UNDULATION && h != 0)
187  throw GeographicErr("Height should be zero for geoid undulations");
188  }
189  if (verbose) {
190  std::cerr << "Gravity file: " << g.GravityFile() << "\n"
191  << "Name: " << g.GravityModelName() << "\n"
192  << "Description: " << g.Description() << "\n"
193  << "Date & Time: " << g.DateTime() << "\n";
194  }
195  unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
196  (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
197  (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
198  GravityModel::GEOID_HEIGHT))); // mode == UNDULATION
199  const GravityCircle c(circle ? g.Circle(lat, h, mask) : GravityCircle());
200  std::string s, stra, strb;
201  while (std::getline(*input, s)) {
202  try {
203  std::string eol("\n");
204  if (!cdelim.empty()) {
205  std::string::size_type m = s.find(cdelim);
206  if (m != std::string::npos) {
207  eol = " " + s.substr(m) + "\n";
208  s = s.substr(0, m);
209  }
210  }
211  std::istringstream str(s);
212  real lon;
213  if (circle) {
214  if (!(str >> strb))
215  throw GeographicErr("Incomplete input: " + s);
216  DMS::flag ind;
217  lon = DMS::Decode(strb, ind);
218  if (ind == DMS::LATITUDE)
219  throw GeographicErr("Bad hemisphere letter on " + strb);
220  if (lon < -540 || lon >= 540)
221  throw GeographicErr("Longitude " + strb +
222  " not in [-540d, 540d)");
223  } else {
224  if (!(str >> stra >> strb))
225  throw GeographicErr("Incomplete input: " + s);
226  DMS::DecodeLatLon(stra, strb, lat, lon);
227  h = 0;
228  if (!(str >> h)) // h is optional
229  str.clear();
230  if (mode == UNDULATION && h != 0)
231  throw GeographicErr("Height must be zero for geoid heights");
232  }
233  if (str >> stra)
234  throw GeographicErr("Extra junk in input: " + s);
235  switch (mode) {
236  case GRAVITY:
237  {
238  real gx, gy, gz;
239  if (circle) {
240  c.Gravity(lon, gx, gy, gz);
241  } else {
242  g.Gravity(lat, lon, h, gx, gy, gz);
243  }
244  *output << Utility::str(gx, prec) << " "
245  << Utility::str(gy, prec) << " "
246  << Utility::str(gz, prec) << eol;
247  }
248  break;
249  case DISTURBANCE:
250  {
251  real deltax, deltay, deltaz;
252  if (circle) {
253  c.Disturbance(lon, deltax, deltay, deltaz);
254  } else {
255  g.Disturbance(lat, lon, h, deltax, deltay, deltaz);
256  }
257  // Convert to mGals
258  *output << Utility::str(deltax * 1e5, prec) << " "
259  << Utility::str(deltay * 1e5, prec) << " "
260  << Utility::str(deltaz * 1e5, prec)
261  << eol;
262  }
263  break;
264  case ANOMALY:
265  {
266  real Dg01, xi, eta;
267  if (circle)
268  c.SphericalAnomaly(lon, Dg01, xi, eta);
269  else
270  g.SphericalAnomaly(lat, lon, h, Dg01, xi, eta);
271  Dg01 *= 1e5; // Convert to mGals
272  xi *= 3600; // Convert to arcsecs
273  eta *= 3600;
274  *output << Utility::str(Dg01, prec) << " "
275  << Utility::str(xi, prec) << " "
276  << Utility::str(eta, prec) << eol;
277  }
278  break;
279  case UNDULATION:
280  default:
281  {
282  real N = circle ? c.GeoidHeight(lon) : g.GeoidHeight(lat, lon);
283  *output << Utility::str(N, prec) << eol;
284  }
285  break;
286  }
287  }
288  catch (const std::exception& e) {
289  *output << "ERROR: " << e.what() << "\n";
290  retval = 1;
291  }
292  }
293  }
294  catch (const std::exception& e) {
295  std::cerr << "Error reading " << model << ": " << e.what() << "\n";
296  retval = 1;
297  }
298  return retval;
299  }
300  catch (const std::exception& e) {
301  std::cerr << "Caught exception: " << e.what() << "\n";
302  return 1;
303  }
304  catch (...) {
305  std::cerr << "Caught unknown exception\n";
306  return 1;
307  }
308 }
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:596
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.