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