23int main(
int argc,
const char*
const argv[]) {
27 Utility::set_digits();
28 bool verbose =
false, longfirst =
false;
30 std::string model = GravityModel::DefaultGravityName();
31 std::string istring, ifile, ofile, cdelim;
35 int prec = -1, Nmax = -1, Mmax = -1;
42 unsigned mode = GRAVITY;
43 for (
int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
46 if (++m == argc)
return usage(1,
true);
48 }
else if (arg ==
"-d") {
49 if (++m == argc)
return usage(1,
true);
51 }
else if (arg ==
"-N") {
52 if (++m == argc)
return usage(1,
true);
54 Nmax = Utility::val<int>(std::string(argv[m]));
56 std::cerr <<
"Maximum degree " << argv[m] <<
" is negative\n";
60 catch (
const std::exception&) {
61 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
64 }
else if (arg ==
"-M") {
65 if (++m == argc)
return usage(1,
true);
67 Mmax = Utility::val<int>(std::string(argv[m]));
69 std::cerr <<
"Maximum order " << argv[m] <<
" is negative\n";
73 catch (
const std::exception&) {
74 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
77 }
else if (arg ==
"-G")
85 else if (arg ==
"-c") {
86 if (m + 2 >= argc)
return usage(1,
true);
90 lat = DMS::Decode(std::string(argv[++m]), ind);
91 if (ind == DMS::LONGITUDE)
93 if (!(fabs(lat) <= Math::qd))
94 throw GeographicErr(
"Latitude not in [-" + std::to_string(Math::qd)
95 +
"d, " + std::to_string(Math::qd) +
"d]");
96 h = Utility::val<real>(std::string(argv[++m]));
99 catch (
const std::exception& e) {
100 std::cerr <<
"Error decoding argument of " << arg <<
": "
104 }
else if (arg ==
"-w")
105 longfirst = !longfirst;
106 else if (arg ==
"-p") {
107 if (++m == argc)
return usage(1,
true);
109 prec = Utility::val<int>(std::string(argv[m]));
111 catch (
const std::exception&) {
112 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
115 }
else if (arg ==
"-v")
117 else if (arg ==
"--input-string") {
118 if (++m == argc)
return usage(1,
true);
120 }
else if (arg ==
"--input-file") {
121 if (++m == argc)
return usage(1,
true);
123 }
else if (arg ==
"--output-file") {
124 if (++m == argc)
return usage(1,
true);
126 }
else if (arg ==
"--line-separator") {
127 if (++m == argc)
return usage(1,
true);
128 if (std::string(argv[m]).size() != 1) {
129 std::cerr <<
"Line separator must be a single character\n";
133 }
else if (arg ==
"--comment-delimiter") {
134 if (++m == argc)
return usage(1,
true);
136 }
else if (arg ==
"--version") {
137 std::cout << argv[0] <<
": GeographicLib version "
138 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
141 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
143 std::cout<<
"\nDefault gravity path = \""
144 << GravityModel::DefaultGravityPath()
145 <<
"\"\nDefault gravity name = \""
146 << GravityModel::DefaultGravityName()
152 if (!ifile.empty() && !istring.empty()) {
153 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
156 if (ifile ==
"-") ifile.clear();
157 std::ifstream infile;
158 std::istringstream instring;
159 if (!ifile.empty()) {
160 infile.open(ifile.c_str());
161 if (!infile.is_open()) {
162 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
165 }
else if (!istring.empty()) {
166 std::string::size_type m = 0;
168 m = istring.find(lsep, m);
169 if (m == std::string::npos)
173 instring.str(istring);
175 std::istream* input = !ifile.empty() ? &infile :
176 (!istring.empty() ? &instring : &std::cin);
178 std::ofstream outfile;
179 if (ofile ==
"-") ofile.clear();
180 if (!ofile.empty()) {
181 outfile.open(ofile.c_str());
182 if (!outfile.is_open()) {
183 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
187 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
191 prec = std::min(16 + Math::extra_digits(), prec < 0 ? 5 : prec);
195 prec = std::min(14 + Math::extra_digits(), prec < 0 ? 3 : prec);
199 prec = std::min(12 + Math::extra_digits(), prec < 0 ? 4 : prec);
209 else if (mode == UNDULATION && h != 0)
210 throw GeographicErr(
"Height should be zero for geoid undulations");
213 std::cerr <<
"Gravity file: " << g.
GravityFile() <<
"\n"
216 <<
"Date & Time: " << g.
DateTime() <<
"\n";
218 unsigned mask = (mode == GRAVITY ? GravityModel::GRAVITY :
219 (mode == DISTURBANCE ? GravityModel::DISTURBANCE :
220 (mode == ANOMALY ? GravityModel::SPHERICAL_ANOMALY :
221 GravityModel::GEOID_HEIGHT)));
223 std::string s, eol, stra, strb;
224 std::istringstream str;
225 while (std::getline(*input, s)) {
228 if (!cdelim.empty()) {
229 std::string::size_type m = s.find(cdelim);
230 if (m != std::string::npos) {
231 eol =
" " + s.substr(m) +
"\n";
235 str.clear(); str.str(s);
241 lon = DMS::Decode(strb, ind);
242 if (ind == DMS::LATITUDE)
245 if (!(str >> stra >> strb))
247 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
251 if (mode == UNDULATION && h != 0)
252 throw GeographicErr(
"Height must be zero for geoid heights");
261 c.Gravity(lon, gx, gy, gz);
263 g.
Gravity(lat, lon, h, gx, gy, gz);
265 *output << Utility::str(gx, prec) <<
" "
266 << Utility::str(gy, prec) <<
" "
267 << Utility::str(gz, prec) << eol;
272 real deltax, deltay, deltaz;
274 c.Disturbance(lon, deltax, deltay, deltaz);
276 g.
Disturbance(lat, lon, h, deltax, deltay, deltaz);
279 *output << Utility::str(deltax * 100000, prec) <<
" "
280 << Utility::str(deltay * 100000, prec) <<
" "
281 << Utility::str(deltaz * 100000, prec)
289 c.SphericalAnomaly(lon, Dg01, xi, eta);
295 *output << Utility::str(Dg01, prec) <<
" "
296 << Utility::str(xi, prec) <<
" "
297 << Utility::str(eta, prec) << eol;
304 *output << Utility::str(N, prec) << eol;
309 catch (
const std::exception& e) {
310 *output <<
"ERROR: " << e.what() <<
"\n";
315 catch (
const std::exception& e) {
316 std::cerr <<
"Error reading " << model <<
": " << e.what() <<
"\n";
321 catch (
const std::exception& e) {
322 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
326 std::cerr <<
"Caught unknown exception\n";