CppEphem
planetephem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * planetephem.cpp: CppEphem *
3  * ----------------------------------------------------------------------- *
4  * Copyright © 2017-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 <algorithm>
23 #include <stdio.h>
24 
25 #include "CppEphem.h"
26 #include "CEExecOptions.h"
27 
28 
29 /**********************************************************************/
33 CEPlanet GetPlanet(const int& p_id) ;
34 void PrintEphemeris(CEObservation& obs,
35  const double& duration,
36  const double& step_size) ;
37 
38 /**********************************************************************/
41 int main(int argc, char** argv)
42 {
43  // Get the command line options
44  CEExecOptions opts = DefineOpts() ;
45  if (opts.ParseCommandLine(argc,argv)) return 0;
46 
47  // Setup the nutation and timing corrections information
48  opts.SetCorrFiles();
50 
51  // Create the observed object
52  CEPlanet planet = GetPlanet(opts.AsInt("planet")) ;
53  if (planet.Name().empty()) return 0 ;
54 
55  // Set the algorithm
56  std::string method = opts.AsString("method");
57  std::transform(method.begin(), method.end(), method.begin(), ::tolower);
58  if (method == "sofa") {
60  } else if (method == "jpl") {
62  } else {
63  throw CEException::invalid_value("planetephem.cpp",
64  "[ERROR] Invalid planet algorithm: " + method);
65  }
66 
67  // Create the date object
68  CEDate date(opts.AsDouble("startJD"), CEDateType::JD) ;
69 
70  // Create the observer
71  CEObserver observer = opts.GenObserver();
73 
74  CEObservation coords(&observer, &planet, &date) ;
75 
76  // Now list the information requested
77  PrintEphemeris(coords, opts.AsDouble("duration"), opts.AsDouble("step")) ;
78 
79  return 0;
80 }
81 
82 /**********************************************************************/
86 {
87  CEExecOptions opts("planetephem");
88 
89  // Add version and description
90  opts.AddProgramDescription(std::string() +
91  "Returns the apparent ICRS RA,Dec and observed Az,Alt coordinates for " +
92  "a given planet for a given observer. It is important also to note " +
93  "observed coordinates will be dubious very close to the horizon due " +
94  "to the atmospheric correction algorithms employed.");
95 
96  // Define the parameters
97  opts.AddIntParam("p,planet",
98  "Planet number (1=Mercury, 2=Venus, 4=Mars, 5=Jupiter, 6=Saturn, 7=Uranus, 8=Neptune)",
99  0);
100 
101  // Observer parameters
102  opts.AddObserverPars();
103 
104  // Time information
105  opts.AddDoubleParam("startJD",
106  "Starting time as a Julian date. Default will be the current Julian date",
108  opts.AddDoubleParam("duration",
109  "Duration for printing out results (in minutes). Results will be printed from 'startJD' to 'startJD + duration'. Default is for one 24 hour period",
110  24.0*60.0);
111  opts.AddDoubleParam("step",
112  "Number of minutes between each line of the print out.",
113  30.0);
114  opts.AddStringParam("method",
115  "Method for computing planet positions ('sofa' or 'jpl', default='sofa')",
116  "sofa");
117 
118  opts.AddCorrFilePars();
119  return opts ;
120 }
121 
122 /**********************************************************************/
125 CEPlanet GetPlanet(const int& p_id)
126 {
127  switch (p_id)
128  {
129  case 1:
130  return CEPlanet::Mercury();
131  case 2:
132  return CEPlanet::Venus();
133  case 4:
134  return CEPlanet::Mars();
135  case 5:
136  return CEPlanet::Jupiter();
137  case 6:
138  return CEPlanet::Saturn();
139  case 7:
140  return CEPlanet::Uranus();
141  case 8:
142  return CEPlanet::Neptune();
143  default:
144  std::string msg = std::string() + "[ERROR] Planet ID (" +
145  std::to_string(p_id) + ") is not valid!";
146  throw CEException::invalid_value("planetephem, GetPlanet()", msg);
147  }
148 }
149 
150 /**********************************************************************/
153 void PrintEphemeris(CEObservation& obs,
154  const double& duration,
155  const double& step_size)
156 {
157  // Lets get the objects in obs
158  CEDate* date = obs.Date();
159  CEPlanet* planet = dynamic_cast<CEPlanet*>( obs.Body() );
160  CEObserver* observer = obs.Observer();
161  std::vector<double> localtime = CETime::TimeDbl2Vect(date->GetTime(observer->UTCOffset()));
162 
163  // Print some information about the observer
164  std::vector<double> lon_dms = CEAngle(observer->Longitude_Rad()).DmsVect();
165  std::vector<double> lat_dms = CEAngle(observer->Latitude_Rad()).DmsVect();
166 
167  std::printf("\n") ;
168  std::printf("= OBSERVER ===================\n");
169  std::printf(" Longitude: %+4dd %02dm %4.1fs\n", int(lon_dms[0]), int(lon_dms[1]), lon_dms[2]+lon_dms[3]);
170  std::printf(" Latitude : %+3dd %02dm %4.1fs\n", int(lat_dms[0]), int(lat_dms[1]), lat_dms[2]+lat_dms[3]);
171  std::printf(" Elevation: %f m \n", observer->Elevation_m());
172  std::printf(" Pressure : %f hPa\n", observer->Pressure_hPa());
173  std::printf(" Temp : %f Celsius\n", observer->Temperature_C());
174  std::printf(" Humidity : %f %%\n", 100.0*observer->RelativeHumidity());
175  std::printf(" Wavelen. : %f micrometers\n", observer->Wavelength_um());
176  std::printf(" LocalTime: %02d:%02d:%04.1f\n", int(localtime[0]), int(localtime[1]), localtime[2]+localtime[3]);
177  std::printf(" UTCOffset: %2d hrs\n\n", int(observer->UTCOffset()));
178 
179  // Print some basic information regarding the planet itself
180  std::printf("= PLANET =====================\n");
181  std::printf(" Name : %s\n", planet->Name().c_str());
182  std::printf(" Mass : %e kg\n", planet->Mass_kg());
183  std::printf(" Radius: %f km\n", planet->Radius_m()/1000.0);
184  std::printf(" Albedo: %f\n\n", planet->Albedo());
185 
186  std::vector<double> ra;
187  std::vector<double> dec;
188 
189  // Now do the stuff we actually want
190  std::printf("= NOTES ======================\n");
191  std::printf(" * RA,DEC represent apparent ICRS coordinates for the observer\n");
192  std::printf(" * Coordinates should be considered dubious at small altitudes\n");
193  std::printf("\n");
194  std::printf(" JD LOCAL RA (appar.) DEC (appar.) Az Alt \n") ;
195  std::printf(" =======================================================================\n") ;
196  int max_steps = int(duration/step_size);
197  CESkyCoord obs_coords;
198  CESkyCoord appar_coords;
199  for (int s=0; s<=max_steps; s++) {
200 
201  // Get the observed coordinates
202  obs_coords.SetCoordinates(obs.GetAzimuth_Rad(), obs.GetZenith_Rad(),
204  // Convert to RA,DEC
205  appar_coords = obs_coords.ConvertToICRS(*date, *observer);
206  // Update the coordiantes of the planet
207  ra = appar_coords.XCoord().HmsVect();
208  dec = appar_coords.YCoord().DmsVect();
209 
210  std::printf(" %11.2f %08.1f %2.0fh %2.0fm %4.1fs %+3.0fd %2.0fm %4.1fs %8.3f %+7.3f\n",
211  double(*date), date->GetTime(observer->UTCOffset()),
212  ra[0], ra[1], ra[2] + ra[3],
213  dec[0], dec[1], dec[2] + dec[3],
214  obs_coords.XCoord().Deg(),
215  90.0-obs_coords.YCoord().Deg());
216 
217  // Update the date
218  date->SetDate(date->JD() + step_size/(60.0*24.0), CEDateType::JD);
219  }
220 
221  std::printf(" -------------------------------------------------------------------\n");
222 }
CEObserver::SetUTCOffset
void SetUTCOffset(const double &utc_offset)
Set the UTC offset for the observers time.
Definition: CEObserver.h:236
CEPlanet::Uranus
static CEPlanet Uranus()
Returns an object representing Uranus.
Definition: CEPlanet.cpp:391
CEAngle::DmsVect
std::vector< double > DmsVect(void) const
Return vector of doubles representing the {degrees, arcmin, arcsec, arcsec-fraction}.
Definition: CEAngle.cpp:291
CEDate
Definition: CEDate.h:43
CEPlanet::Mercury
static CEPlanet Mercury()
Returns an object representing Mercury.
Definition: CEPlanet.cpp:211
CEPlanet::Mars
static CEPlanet Mars()
Returns an object representing Mars.
Definition: CEPlanet.cpp:308
CEPlanet::Neptune
static CEPlanet Neptune()
Returns an object representing Neptune.
Definition: CEPlanet.cpp:419
CESkyCoord
Definition: CESkyCoord.h:49
CEObservation::Observer
CEObserver * Observer()
Access the underlying objects.
Definition: CEObservation.h:103
DefineOpts
CEExecOptions DefineOpts()
Forward declarations.
Definition: planetephem.cpp:84
CETime::TimeDbl2Vect
static std::vector< double > TimeDbl2Vect(const double &time)
Convert a time formatted as HHMMSS.SS into a vector.
Definition: CETime.cpp:277
CEExecOptions.h
CEDate::GetTime
virtual double GetTime(const double &utc_offset=0.0) const
Method for getting the current time.
Definition: CEDate.cpp:607
CEExecOptions::GenObserver
CEObserver GenObserver(void)
Generate an observer object.
Definition: CEExecOptions.h:307
CEPlanet::SetAlgorithm
void SetAlgorithm(const CEPlanetAlgo &new_algo)
Set the desired planet computation algorithm.
Definition: CEPlanet.h:425
SOFA
Use methods included in sofa software.
Definition: CEPlanet.h:48
CEExecOptions::AddObserverPars
void AddObserverPars(void)
Add parameters defining an observer and their atmospheric properties.
Definition: CEExecOptions.h:169
CEPlanet::Mass_kg
double Mass_kg()
Definition: CEPlanet.h:230
PrintEphemeris
void PrintEphemeris(CEObservation &obs, const double &duration, const double &step_size)
Prints the results of the planet position query.
Definition: planetephem.cpp:152
CEAngle::HmsVect
std::vector< double > HmsVect(void) const
Return vector of doubles representing the {hours, min, sec, sec-fraction}.
Definition: CEAngle.cpp:199
CppEphem.h
CEPlanet::Radius_m
double Radius_m()
Definition: CEPlanet.h:220
CEAngle
Definition: CEAngle.h:38
JPL
Use Keplerian algorithm outlined by JPL.
Definition: CEPlanet.h:49
JD
Julian Date.
Definition: CEDate.h:56
CEExecOptions::SetCorrFiles
void SetCorrFiles(void)
Defines the corrections terms from user supplied options.
Definition: CEExecOptions.h:324
CEBody::Name
std::string Name(void) const
Get the name of this object.
Definition: CEBody.h:95
CEPlanet::Venus
static CEPlanet Venus()
Returns an object representing Venus.
Definition: CEPlanet.cpp:236
CEPlanet::Albedo
double Albedo()
Definition: CEPlanet.h:240
CEPlanet
Definition: CEPlanet.h:35
CEObservation::Body
CEBody * Body()
Access underlying CEBody object.
Definition: CEObservation.h:113
CEPlanet::Saturn
static CEPlanet Saturn()
Returns an object representing Saturn.
Definition: CEPlanet.cpp:363
CETime::SystemUTCOffset_hrs
static double SystemUTCOffset_hrs()
Definition: CETime.h:74
CEObservation::GetAzimuth_Rad
virtual double GetAzimuth_Rad()
Definition: CEObservation.h:167
CEDate::CurrentJD
static double CurrentJD()
Static method for getting the current Julian date.
Definition: CEDate.cpp:632
CEObserver
Definition: CEObserver.h:28
CEException::invalid_value
Definition: CEException.h:73
CppEphem::CorrectionsInterp
void CorrectionsInterp(bool set_interp)
Set the corrections object to use interpolation.
Definition: CENamespace.cpp:99
CEObservation
Definition: CEObservation.h:33
CEExecOptions::AddCorrFilePars
void AddCorrFilePars(void)
Add parameter for the corrections file path.
Definition: CEExecOptions.h:207
CEDate::SetDate
virtual void SetDate(const double &date=CurrentJD(), const CEDateType &time_format=CEDateType::JD)
Set the date based on an actual date and the desired time_format.
Definition: CEDate.cpp:107
GetPlanet
CEPlanet GetPlanet(const int &p_id)
Returns the planet requested by the user.
Definition: planetephem.cpp:124
CEObservation::GetZenith_Rad
virtual double GetZenith_Rad()
Definition: CEObservation.h:188
CESkyCoord::SetCoordinates
virtual void SetCoordinates(const CEAngle &xcoord, const CEAngle &ycoord, const CESkyCoordType &coord_type=CESkyCoordType::ICRS) const
Set the coordinates of this object.
Definition: CESkyCoord.cpp:1000
CEObservation::Date
CEDate * Date()
Access underlying CEDate object.
Definition: CEObservation.h:124
CEDate::JD
virtual double JD() const
Get the Julian date represented by this object.
Definition: CEDate.h:150
CEPlanet::Jupiter
static CEPlanet Jupiter()
Returns an object representing Jupiter.
Definition: CEPlanet.cpp:335
CEAngle::Deg
static CEAngle Deg(const double &angle)
Return angle (radians) constructed from a degree angle.
Definition: CEAngle.cpp:317
main
int main(int argc, char **argv)
Main method.
Definition: planetephem.cpp:40
CEExecOptions
Definition: CEExecOptions.h:9
CESkyCoord::ConvertToICRS
CESkyCoord ConvertToICRS(const CEDate &date=CEDate(), const CEObserver &observer=CEObserver())
Convert this coordinate to ICRS coordinates.
Definition: CESkyCoord.cpp:854
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