37 # pragma warning (disable: 4127 4701)
40 #include "Gravity.usage"
42 int main(
int argc,
char* argv[]) {
44 using namespace GeographicLib;
50 std::string istring, ifile, ofile, cdelim;
61 unsigned mode = GRAVITY;
62 for (
int m = 1; m < argc; ++m) {
63 std::string arg(argv[m]);
65 if (++m == argc)
return usage(1,
true);
67 }
else if (arg ==
"-d") {
68 if (++m == argc)
return usage(1,
true);
70 }
else if (arg ==
"-G")
78 else if (arg ==
"-c") {
79 if (m + 2 >= argc)
return usage(1,
true);
86 if (!(abs(lat) <= 90))
88 h = Utility::num<real>(std::string(argv[++m]));
91 catch (
const std::exception& e) {
92 std::cerr <<
"Error decoding argument of " << arg <<
": "
96 }
else if (arg ==
"-p") {
97 if (++m == argc)
return usage(1,
true);
99 prec = Utility::num<int>(std::string(argv[m]));
101 catch (
const std::exception&) {
102 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
105 }
else if (arg ==
"-v")
107 else if (arg ==
"--input-string") {
108 if (++m == argc)
return usage(1,
true);
110 }
else if (arg ==
"--input-file") {
111 if (++m == argc)
return usage(1,
true);
113 }
else if (arg ==
"--output-file") {
114 if (++m == argc)
return usage(1,
true);
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";
123 }
else if (arg ==
"--comment-delimiter") {
124 if (++m == argc)
return usage(1,
true);
126 }
else if (arg ==
"--version") {
128 << argv[0] <<
": GeographicLib version "
129 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
132 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
134 std::cout<<
"\nDefault gravity path = \""
136 <<
"\"\nDefault gravity name = \""
143 if (!ifile.empty() && !istring.empty()) {
144 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
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";
156 }
else if (!istring.empty()) {
157 std::string::size_type m = 0;
159 m = istring.find(lsep, m);
160 if (m == std::string::npos)
164 instring.str(istring);
166 std::istream* input = !ifile.empty() ? &infile :
167 (!istring.empty() ? &instring : &std::cin);
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";
178 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
199 else if (mode == UNDULATION && h != 0)
200 throw GeographicErr(
"Height should be zero for geoid undulations");
203 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n"
206 <<
"Date & Time: " << g.
DateTime() <<
"\n";
213 std::string s, stra, strb;
214 while (std::getline(*input, s)) {
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";
224 std::istringstream str(s);
233 if (lon < -540 || lon >= 540)
235 " not in [-540d, 540d)");
237 if (!(str >> stra >> strb))
243 if (mode == UNDULATION && h != 0)
244 throw GeographicErr(
"Height must be zero for geoid heights");
253 c.Gravity(lon, gx, gy, gz);
255 g.
Gravity(lat, lon, h, gx, gy, gz);
264 real deltax, deltay, deltaz;
266 c.Disturbance(lon, deltax, deltay, deltaz);
268 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
281 c.SphericalAnomaly(lon, Dg01, xi, eta);
301 catch (
const std::exception& e) {
302 *output <<
"ERROR: " << e.what() <<
"\n";
307 catch (
const std::exception& e) {
308 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
313 catch (
const std::exception& e) {
314 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
318 std::cerr <<
"Caught unknown exception\n";