Gravity.cpp
Go to the documentation of this file.
1 
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;
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::val<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 == "-w")
84  longfirst = !longfirst;
85  else if (arg == "-p") {
86  if (++m == argc) return usage(1, true);
87  try {
88  prec = Utility::val<int>(std::string(argv[m]));
89  }
90  catch (const std::exception&) {
91  std::cerr << "Precision " << argv[m] << " is not a number\n";
92  return 1;
93  }
94  } else if (arg == "-v")
95  verbose = true;
96  else if (arg == "--input-string") {
97  if (++m == argc) return usage(1, true);
98  istring = argv[m];
99  } else if (arg == "--input-file") {
100  if (++m == argc) return usage(1, true);
101  ifile = argv[m];
102  } else if (arg == "--output-file") {
103  if (++m == argc) return usage(1, true);
104  ofile = argv[m];
105  } else if (arg == "--line-separator") {
106  if (++m == argc) return usage(1, true);
107  if (std::string(argv[m]).size() != 1) {
108  std::cerr << "Line separator must be a single character\n";
109  return 1;
110  }
111  lsep = argv[m][0];
112  } else if (arg == "--comment-delimiter") {
113  if (++m == argc) return usage(1, true);
114  cdelim = argv[m];
115  } else if (arg == "--version") {
116  std::cout << argv[0] << ": GeographicLib version "
117  << GEOGRAPHICLIB_VERSION_STRING << "\n";
118  return 0;
119  } else {
120  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
121  if (arg == "-h")
122  std::cout<< "\nDefault gravity path = \""
124  << "\"\nDefault gravity name = \""
126  << "\"\n";
127  return retval;
128  }
129  }
130 
131  if (!ifile.empty() && !istring.empty()) {
132  std::cerr << "Cannot specify --input-string and --input-file together\n";
133  return 1;
134  }
135  if (ifile == "-") ifile.clear();
136  std::ifstream infile;
137  std::istringstream instring;
138  if (!ifile.empty()) {
139  infile.open(ifile.c_str());
140  if (!infile.is_open()) {
141  std::cerr << "Cannot open " << ifile << " for reading\n";
142  return 1;
143  }
144  } else if (!istring.empty()) {
145  std::string::size_type m = 0;
146  while (true) {
147  m = istring.find(lsep, m);
148  if (m == std::string::npos)
149  break;
150  istring[m] = '\n';
151  }
152  instring.str(istring);
153  }
154  std::istream* input = !ifile.empty() ? &infile :
155  (!istring.empty() ? &instring : &std::cin);
156 
157  std::ofstream outfile;
158  if (ofile == "-") ofile.clear();
159  if (!ofile.empty()) {
160  outfile.open(ofile.c_str());
161  if (!outfile.is_open()) {
162  std::cerr << "Cannot open " << ofile << " for writing\n";
163  return 1;
164  }
165  }
166  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
167 
168  switch (mode) {
169  case GRAVITY:
170  prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
171  break;
172  case DISTURBANCE:
173  case ANOMALY:
174  prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
175  break;
176  case UNDULATION:
177  default:
178  prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
179  break;
180  }
181  int retval = 0;
182  try {
183  const GravityModel g(model, dir);
184  if (circle) {
185  if (!Math::isfinite(h))
186  throw GeographicErr("Bad height");
187  else if (mode == UNDULATION && h != 0)
188  throw GeographicErr("Height should be zero for geoid undulations");
189  }
190  if (verbose) {
191  std::cerr << "Gravity file: " << g.GravityFile() << "\n"
192  << "Name: " << g.GravityModelName() << "\n"
193  << "Description: " << g.Description() << "\n"
194  << "Date & Time: " << g.DateTime() << "\n";
195  }
196  unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
197  (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
198  (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
199  GravityModel::GEOID_HEIGHT))); // mode == UNDULATION
200  const GravityCircle c(circle ? g.Circle(lat, h, mask) : GravityCircle());
201  std::string s, eol, stra, strb;
202  std::istringstream str;
203  while (std::getline(*input, s)) {
204  try {
205  eol = "\n";
206  if (!cdelim.empty()) {
207  std::string::size_type m = s.find(cdelim);
208  if (m != std::string::npos) {
209  eol = " " + s.substr(m) + "\n";
210  s = s.substr(0, m);
211  }
212  }
213  str.clear(); str.str(s);
214  real lon;
215  if (circle) {
216  if (!(str >> strb))
217  throw GeographicErr("Incomplete input: " + s);
218  DMS::flag ind;
219  lon = DMS::Decode(strb, ind);
220  if (ind == DMS::LATITUDE)
221  throw GeographicErr("Bad hemisphere letter on " + strb);
222  } else {
223  if (!(str >> stra >> strb))
224  throw GeographicErr("Incomplete input: " + s);
225  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
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 * 100000, prec) << " "
258  << Utility::str(deltay * 100000, prec) << " "
259  << Utility::str(deltaz * 100000, 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 *= 100000; // 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 }
ind
std::vector< int > ind
Definition: Slicing_stdvector_cxx11.cpp:1
GeographicLib::Math::extra_digits
static int extra_digits()
Definition: Math.hpp:187
GeographicLib::GravityCircle
Gravity on a circle of latitude.
Definition: GravityCircle.hpp:41
main
int main(int argc, const char *const argv[])
Definition: Gravity.cpp:29
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
screwPose2::xi
Vector xi
Definition: testPose2.cpp:148
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
GeographicLib
Namespace for GeographicLib.
Definition: JacobiConformal.hpp:15
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: src/DMS.cpp:252
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
GeographicLib::Math::isfinite
static bool isfinite(T x)
Definition: Math.hpp:806
real
float real
Definition: datatypes.h:10
h
const double h
Definition: testSimpleHelicopter.cpp:19
GravityModel.hpp
Header for GeographicLib::GravityModel class.
GeographicLib::DMS::Decode
static Math::real Decode(const std::string &dms, flag &ind)
Definition: src/DMS.cpp:28
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:389
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
GeographicLib::Math::real
double real
Definition: Math.hpp:129
GravityCircle.hpp
Header for GeographicLib::GravityCircle class.
mode
static const DiscreteKey mode(modeKey, 2)
GeographicLib::GravityModel::DISTURBANCE
@ DISTURBANCE
Definition: GravityModel.hpp:137
arg
Definition: cast.h:1412
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS::flag
flag
Definition: DMS.hpp:41
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition: DMS.hpp:56
GeographicLib::GravityModel::GEOID_HEIGHT
@ GEOID_HEIGHT
Definition: GravityModel.hpp:153
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
str::str
str(const char *c, const SzType &n)
Definition: pytypes.h:1563
model
noiseModel::Diagonal::shared_ptr model
Definition: doc/Code/Pose2SLAMExample.cpp:7
g
void g(const string &key, int i)
Definition: testBTree.cpp:41
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition: DMS.hpp:51
GeographicLib::GravityModel::DefaultGravityName
static std::string DefaultGravityName()
Definition: src/GravityModel.cpp:354
str
Definition: pytypes.h:1558
gtsam.examples.CombinedImuFactorExample.GRAVITY
float GRAVITY
Definition: CombinedImuFactorExample.py:29
GeographicLib::GravityModel::DefaultGravityPath
static std::string DefaultGravityPath()
Definition: src/GravityModel.cpp:341
GEOGRAPHICLIB_VERSION_STRING
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
GeographicLib::GravityModel::SPHERICAL_ANOMALY
@ SPHERICAL_ANOMALY
Definition: GravityModel.hpp:148
GeographicLib::GravityModel::GRAVITY
@ GRAVITY
Definition: GravityModel.hpp:132
min
#define min(a, b)
Definition: datatypes.h:19
abs
#define abs(x)
Definition: datatypes.h:17
N
#define N
Definition: igam.h:9
lon
static const double lon
Definition: testGeographicLib.cpp:34
GeographicLib::GravityModel
Model of the earth's gravity field.
Definition: GravityModel.hpp:83
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
real
Definition: main.h:100
GeographicLib::Utility::set_digits
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
DMS.hpp
Header for GeographicLib::DMS class.
lat
static const double lat
Definition: testGeographicLib.cpp:34


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:23