CppEphem
convcoord.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * getcirs.cpp: CppEphem *
3  * ----------------------------------------------------------------------- *
4  * Copyright © 2019 JCardenzana *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <map>
25 #include <string>
26 #include <time.h>
27 #include <algorithm>
28 
29 // CppEphem HEADERS
30 #include "CppEphem.h"
31 #include "CEExecOptions.h"
32 
33 
34 /**********************************************************************/
38 {
39  CEExecOptions opts("convcoord");
40 
41  // Add a program version and description
42  opts.AddProgramDescription(std::string() +
43  "Converts a user supplied set of coordinates to another coordinate system. " +
44  "Additional parameters can be supplied to provide more accurate results.");
45 
46  // Set the options
47  opts.AddCirsPar();
48  opts.AddIcrsPar();
49  opts.AddGalacticPar();
50  opts.AddEclipticPar();
51  opts.AddObservedPar();
52  opts.AddObserverPars();
53 
54  opts.AddStringParam("to", "Coordinate system to convert into (\"CIRS\", \"ICRS\", \"Galactic\", \"Observed\", \"Ecliptic\")", "");
55 
56  // Add parameters to modify the corrections files
57  opts.AddCorrFilePars();
58 
59  return opts;
60 }
61 
62 /**********************************************************************/
65 inline
67 {
68  // Preliminary values
69  std::string parname;
70  CESkyCoordType coord_type;
71  CEAngleType angle_type_x = CEAngleType::DEGREES;
72  CEAngleType angle_type_y = CEAngleType::DEGREES;
73 
74  // Check for delimiter
75  char delim(0);
76  if (opts.AsString("delim").size() == 1) {
77  delim = opts.AsString("delim")[0];
78  angle_type_x = CEAngleType::DMS; // True for most coord-types
79  angle_type_y = CEAngleType::DMS; // Always the case for y-coordinate
80  }
81 
82  // Extract the coordinate type
83  if (opts.AsString("cirs").size() > 0) {
84  parname = "cirs";
85  coord_type = CESkyCoordType::CIRS;
86  if (int(delim)) {
87  angle_type_x = CEAngleType::HMS;
88  }
89  } else if (opts.AsString("icrs").size() > 0) {
90  parname = "icrs";
91  coord_type = CESkyCoordType::ICRS;
92  if (int(delim)) {
93  angle_type_x = CEAngleType::HMS;
94  }
95  } else if (opts.AsString("galactic").size() > 0) {
96  parname = "galactic";
97  coord_type = CESkyCoordType::GALACTIC;
98  } else if (opts.AsString("observed").size() > 0) {
99  parname = "observed";
100  coord_type = CESkyCoordType::OBSERVED;
101  } else if (opts.AsString("ecliptic").size() > 0) {
102  parname = "ecliptic";
103  coord_type = CESkyCoordType::ECLIPTIC;
104  }
105 
106  // Load the angles
107  std::vector<std::string> angles = CLOptionsHelper::split(opts.AsString(parname), ',');
108  CEAngle xcoord;
109  CEAngle ycoord;
110  ycoord.SetAngle(angles[1].c_str(), angle_type_y, delim);
111  xcoord.SetAngle(angles[0].c_str(), angle_type_x, delim);
112 
113  return CESkyCoord(xcoord, ycoord, coord_type);
114 }
115 
116 /**********************************************************************/
119 {
120  // Get the coordinate system type (lower case)
121  std::string user_type = opts.AsString("to");
122  if (user_type.size() == 0) {
123  throw std::invalid_argument("Must supply coordinate system to convert to (specify the \"to\" parameter)");
124  }
125  std::for_each(user_type.begin(), user_type.end(), [](char & c) {
126  c = ::tolower(c);
127  });
128 
129  // Return the system based on a user supplied value
131  if (user_type == "cirs") {
132  ret_type = CESkyCoordType::CIRS;
133  } else if (user_type == "icrs") {
134  ret_type = CESkyCoordType::ICRS;
135  } else if (user_type == "galactic") {
136  ret_type = CESkyCoordType::GALACTIC;
137  } else if (user_type == "observed") {
138  ret_type = CESkyCoordType::OBSERVED;
139  } else if (user_type == "ecliptic") {
140  ret_type = CESkyCoordType::ECLIPTIC;
141  }
142 
143  return ret_type;
144 }
145 
146 /**********************************************************************/
148 void PrintCoordConvResults(const CESkyCoord& outcoord,
149  const CESkyCoord& incoord,
150  const CEDate& date,
151  const CEObserver& obs)
152 {
153  // Create a list of coordinate names
154  std::map<CESkyCoordType, std::vector<std::string>> types;
155  types[CESkyCoordType::CIRS] = {"CIRS", "Right Ascension", "Declination"};
156  types[CESkyCoordType::ICRS] = {"ICRS", "Right Ascension", "Declination"};
157  types[CESkyCoordType::GALACTIC] = {"Galactic", "Longitude", "Latitude"};
158  types[CESkyCoordType::OBSERVED] = {"Observed", "Azimuth", "Zenith"};
159  types[CESkyCoordType::ECLIPTIC] = {"Ecliptic", "Longitude", "Latitude"};
160 
161  std::vector<std::string> outnames = types[outcoord.GetCoordSystem()];
162  std::vector<std::string> innames = types[incoord.GetCoordSystem()];
163 
164  // Print output coordinates
165  std::printf("\n") ;
166  std::printf("******************************************\n");
167  std::printf(" Results of %s -> %s conversion\n",
168  innames[0].c_str(), outnames[0].c_str());
169  std::printf("******************************************\n");
170  std::printf("%s Coordinates (output)\n", outnames[0].c_str());
171  std::printf(" %-15s: %f degrees\n", outnames[1].c_str(), outcoord.XCoord().Deg());
172  std::printf(" %-15s: %+f degrees\n", outnames[2].c_str(), outcoord.YCoord().Deg());
173  if (outnames[2] == "Zenith") {
174  std::printf(" %-15s: %+f degrees\n", "Altitude", 90.0-outcoord.YCoord().Deg());
175  }
176  std::printf("%s Coordinates (input)\n", innames[0].c_str());
177  std::printf(" %-15s: %f degrees\n", innames[1].c_str(), incoord.XCoord().Deg());
178  std::printf(" %-15s: %+f degrees\n", innames[2].c_str(), incoord.YCoord().Deg());
179  std::printf("Julian Date : %f\n", date.JD());
180  if ((outcoord.GetCoordSystem() == CESkyCoordType::OBSERVED) ||
181  (incoord.GetCoordSystem() == CESkyCoordType::OBSERVED)) {
182  std::printf("Observer Info\n");
183  std::printf(" Longitude : %f deg\n", obs.Longitude_Deg());
184  std::printf(" Latitude : %+f deg\n", obs.Latitude_Deg());
185  std::printf(" Elevation : %f meters\n", obs.Elevation_m());
186  std::printf(" Pressure : %f hPa\n", obs.Pressure_hPa());
187  std::printf(" Temperature : %f Celsius\n", obs.Temperature_C());
188  std::printf(" Relative Humid.: %f\n", obs.RelativeHumidity());
189  }
190  std::printf("\n");
191 }
192 
193 /**********************************************************************/
195 int main(int argc, char** argv) {
196 
197  // Get the options from the command line
198  CEExecOptions opts = DefineOpts();
199  if (opts.ParseCommandLine(argc, argv)) return 0;
200 
201  // Setup correction term files
202  opts.SetCorrFiles();
204 
205  // Create a date
206  CEDate date(opts.AsDouble("juliandate"), CEDateType::JD);
207 
208  // Create the observer
209  CEObserver obs = opts.GenObserver();
210 
211  // Get the input coordinates
212  CESkyCoord incoord = GetInCoord(opts);
213 
214  // Convert the coordinates
215  CESkyCoordType outtype = GetOutType(opts);
216  CESkyCoord cirs_coord = incoord.ConvertTo(outtype, date, obs);
217 
218  // Print the results
219  PrintCoordConvResults(cirs_coord, incoord, date, obs);
220 
221  return 0;
222 }
CEObserver::Elevation_m
double Elevation_m() const
Return altitude in meters above sea level.
Definition: CEObserver.h:166
CESkyCoordType::ICRS
RA, Dec (referenced at the barycenter of the solarsystem)
CESkyCoordType
CESkyCoordType
The following enum specifies what coordinates this object represents.
Definition: CESkyCoord.h:39
CESkyCoordType::CIRS
RA, Dec (referenced at the center of the Earth)
CEDate
Definition: CEDate.h:43
CEObserver::Temperature_C
double Temperature_C() const
Return temperature in degrees Celsius.
Definition: CEObserver.h:186
CppEphem::c
double c()
speed of light (meters/second)
Definition: CENamespace.h:68
CESkyCoord
Definition: CESkyCoord.h:49
CEExecOptions.h
CEAngleType
CEAngleType
Definition: CEAngle.h:30
CEExecOptions::GenObserver
CEObserver GenObserver(void)
Generate an observer object.
Definition: CEExecOptions.h:307
CEObserver::RelativeHumidity
double RelativeHumidity() const
Return relative humidity.
Definition: CEObserver.h:216
CESkyCoordType::GALACTIC
Galacitc longitude, latitude.
CppEphem.h
CEAngle
Definition: CEAngle.h:38
CEAngle::SetAngle
void SetAngle(const double &angle, const CEAngleType &angle_type=CEAngleType::RADIANS)
Set the angle from a double.
Definition: CEAngle.cpp:369
CEObserver::Pressure_hPa
double Pressure_hPa() const
Return atmospheric pressure in units of hPa.
Definition: CEObserver.h:176
CESkyCoord::GetCoordSystem
CESkyCoordType GetCoordSystem(void) const
Return coordinate system.
Definition: CESkyCoord.h:251
CEAngleType::DEGREES
JD
Julian Date.
Definition: CEDate.h:56
CEExecOptions::SetCorrFiles
void SetCorrFiles(void)
Defines the corrections terms from user supplied options.
Definition: CEExecOptions.h:324
CEObserver::Latitude_Deg
double Latitude_Deg() const
Return geographic latitude in degrees.
Definition: CEObserver.h:156
GetOutType
CESkyCoordType GetOutType(CEExecOptions &opts)
Definition: convcoord.cpp:117
GetInCoord
CESkyCoord GetInCoord(CEExecOptions &opts)
Get the user supplied coordinates.
Definition: convcoord.cpp:65
main
int main(int argc, char **argv)
Definition: convcoord.cpp:194
CEObserver
Definition: CEObserver.h:28
CppEphem::CorrectionsInterp
void CorrectionsInterp(bool set_interp)
Set the corrections object to use interpolation.
Definition: CENamespace.cpp:99
DefineOpts
CEExecOptions DefineOpts()
Define the command line options for this program.
Definition: convcoord.cpp:36
CEObserver::Longitude_Deg
double Longitude_Deg() const
Return observer geographic longitude in degrees.
Definition: CEObserver.h:136
PrintCoordConvResults
void PrintCoordConvResults(const CESkyCoord &outcoord, const CESkyCoord &incoord, const CEDate &date, const CEObserver &obs)
Definition: convcoord.cpp:147
CEDate::JD
virtual double JD() const
Get the Julian date represented by this object.
Definition: CEDate.h:150
CppEphem::StrOpt::split
void split(const std::string &s, const char &delim, std::vector< std::string > *elems)
Method for splitting a string based on some delimiter into a vector of strings.
Definition: CENamespace.cpp:209
CEAngleType::DMS
CESkyCoordType::ECLIPTIC
Ecliptic longitude, latitude.
CEAngle::Deg
static CEAngle Deg(const double &angle)
Return angle (radians) constructed from a degree angle.
Definition: CEAngle.cpp:317
CEAngleType::HMS
CEExecOptions
Definition: CEExecOptions.h:9
CESkyCoord::ConvertTo
CESkyCoord ConvertTo(const CESkyCoordType &output_coord_type, const CEDate &date=CEDate(), const CEObserver &observer=CEObserver())
Convert these coordinates to another coordinate system NOTE: If this object is not OBSERVED coordinat...
Definition: CESkyCoord.cpp:786
CESkyCoord::XCoord
virtual CEAngle XCoord(const CEDate &jd=CppEphem::julian_date_J2000()) const
Return x coordinate at given Julian date.
Definition: CESkyCoord.h:227
CESkyCoordType::OBSERVED
Azimuth, Zenith (requires additional observer information)
CESkyCoord::YCoord
virtual CEAngle YCoord(const CEDate &jd=CppEphem::julian_date_J2000()) const
Return y coordinate at given Julian date.
Definition: CESkyCoord.h:240