24 # pragma warning (disable: 4127 4701)
27 #include "Gravity.usage"
29 int main(
int argc,
const char*
const argv[]) {
34 bool verbose =
false, longfirst =
false;
37 std::string istring, ifile, ofile, cdelim;
41 int prec = -1, Nmax = -1, Mmax = -1;
48 unsigned mode = GRAVITY;
49 for (
int m = 1; m < argc; ++m) {
50 std::string arg(argv[m]);
52 if (++m == argc)
return usage(1,
true);
54 }
else if (arg ==
"-d") {
55 if (++m == argc)
return usage(1,
true);
57 }
else if (arg ==
"-N") {
58 if (++m == argc)
return usage(1,
true);
60 Nmax = Utility::val<int>(std::string(argv[m]));
62 std::cerr <<
"Maximum degree " << argv[m] <<
" is negative\n";
66 catch (
const std::exception&) {
67 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
70 }
else if (arg ==
"-M") {
71 if (++m == argc)
return usage(1,
true);
73 Mmax = Utility::val<int>(std::string(argv[m]));
75 std::cerr <<
"Maximum order " << argv[m] <<
" is negative\n";
79 catch (
const std::exception&) {
80 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
83 }
else if (arg ==
"-G")
91 else if (arg ==
"-c") {
92 if (m + 2 >= argc)
return usage(1,
true);
99 if (!(abs(lat) <= 90))
101 h = Utility::val<real>(std::string(argv[++m]));
104 catch (
const std::exception& e) {
105 std::cerr <<
"Error decoding argument of " << arg <<
": "
109 }
else if (arg ==
"-w")
110 longfirst = !longfirst;
111 else if (arg ==
"-p") {
112 if (++m == argc)
return usage(1,
true);
114 prec = Utility::val<int>(std::string(argv[m]));
116 catch (
const std::exception&) {
117 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
120 }
else if (arg ==
"-v")
122 else if (arg ==
"--input-string") {
123 if (++m == argc)
return usage(1,
true);
125 }
else if (arg ==
"--input-file") {
126 if (++m == argc)
return usage(1,
true);
128 }
else if (arg ==
"--output-file") {
129 if (++m == argc)
return usage(1,
true);
131 }
else if (arg ==
"--line-separator") {
132 if (++m == argc)
return usage(1,
true);
133 if (std::string(argv[m]).size() != 1) {
134 std::cerr <<
"Line separator must be a single character\n";
138 }
else if (arg ==
"--comment-delimiter") {
139 if (++m == argc)
return usage(1,
true);
141 }
else if (arg ==
"--version") {
142 std::cout << argv[0] <<
": GeographicLib version "
143 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
146 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
148 std::cout<<
"\nDefault gravity path = \""
150 <<
"\"\nDefault gravity name = \""
157 if (!ifile.empty() && !istring.empty()) {
158 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
161 if (ifile ==
"-") ifile.clear();
162 std::ifstream infile;
163 std::istringstream instring;
164 if (!ifile.empty()) {
165 infile.open(ifile.c_str());
166 if (!infile.is_open()) {
167 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
170 }
else if (!istring.empty()) {
171 std::string::size_type m = 0;
173 m = istring.find(lsep, m);
174 if (m == std::string::npos)
178 instring.str(istring);
180 std::istream* input = !ifile.empty() ? &infile :
181 (!istring.empty() ? &instring : &std::cin);
183 std::ofstream outfile;
184 if (ofile ==
"-") ofile.clear();
185 if (!ofile.empty()) {
186 outfile.open(ofile.c_str());
187 if (!outfile.is_open()) {
188 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
192 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
213 else if (mode == UNDULATION && h != 0)
214 throw GeographicErr(
"Height should be zero for geoid undulations");
217 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n"
220 <<
"Date & Time: " << g.
DateTime() <<
"\n";
227 std::string s, eol, stra, strb;
228 std::istringstream str;
229 while (std::getline(*input, s)) {
232 if (!cdelim.empty()) {
233 std::string::size_type m = s.find(cdelim);
234 if (m != std::string::npos) {
235 eol =
" " + s.substr(m) +
"\n";
239 str.clear(); str.str(s);
249 if (!(str >> stra >> strb))
255 if (mode == UNDULATION && h != 0)
256 throw GeographicErr(
"Height must be zero for geoid heights");
265 c.Gravity(lon, gx, gy, gz);
267 g.
Gravity(lat, lon, h, gx, gy, gz);
276 real deltax, deltay, deltaz;
278 c.Disturbance(lon, deltax, deltay, deltaz);
280 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
293 c.SphericalAnomaly(lon, Dg01, xi, eta);
313 catch (
const std::exception& e) {
314 *output <<
"ERROR: " << e.what() <<
"\n";
319 catch (
const std::exception& e) {
320 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
325 catch (
const std::exception& e) {
326 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
330 std::cerr <<
"Caught unknown exception\n";