CppEphem
planetpositions.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * planetpositions.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 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <iostream>
32 #include <ncurses.h>
33 #include "CEExecOptions.h"
34 
35 #include "CppEphem.h"
36 
37 /**********************************************************************/
41 {
42  CEExecOptions options("planetpositions") ;
43  options.AddDoubleParam("L,longitude","Geographic observer longitude (degrees, east positive)",-93.62) ;
44  options.AddDoubleParam("B,latitude", "Geographic observer latitude (degrees)", 42.0347) ;
45  options.AddDoubleParam("e,elevation","Observer elevation above sea-level (meters)", 287.0) ;
46  options.AddDoubleParam("o,UTCOffset", "Observer offset from UTC time (default is current system UTC offset)", CETime::SystemUTCOffset_hrs()) ;
47  options.AddIntParam("u,UpdateFrequency", "Number of milliseconds between updates", 1000) ;
48  options.AddIntParam("a,algorithm","Sets the algorithm used to compute planet positions (1=SOFA,2=JPL)",1) ;
49  options.AddBoolParam("m,DrawMap", "Draws a crude map of the southern sky with visible planets", true) ;
50 
51  return options ;
52 }
53 
54 /**********************************************************************/
58 void DrawSkyMap(std::vector<CEObservation> &coords, int line) ;
59 void DrawGrid(int line) ;
60 int Description() ;
61 
62 
63 /**********************************************************************/
66 int main(int argc, char** argv)
67 {
68  // Parse the command line options
70  if (opt.ParseCommandLine(argc, argv)) return 0.0 ;
71 
72  // Create the object representing Mercury
73  std::vector<CEPlanet*> planets(8) ;
74  planets[0] = new CEPlanet(CEPlanet::Mercury()) ;
75  planets[1] = new CEPlanet(CEPlanet::Venus()) ;
76  planets[2] = new CEPlanet(CEPlanet::Mars()) ;
77  planets[3] = new CEPlanet(CEPlanet::Jupiter()) ;
78  planets[4] = new CEPlanet(CEPlanet::Saturn()) ;
79  planets[5] = new CEPlanet(CEPlanet::Uranus()) ;
80  planets[6] = new CEPlanet(CEPlanet::Neptune()) ;
81  planets[7] = new CEPlanet(CEPlanet::Pluto()) ;
82 
83  for (int i=0; i<planets.size(); i++) {
84  if (opt.AsInt("algorithm") == 1) {
85  planets[i]->SetAlgorithm(CEPlanetAlgo::SOFA) ;
86  } else if (opt.AsInt("algorithm") == 2) {
87  planets[i]->SetAlgorithm(CEPlanetAlgo::JPL) ;
88  }
89  }
90 
91  // Create an object that always represents the current instantaneous time
92  CEDate date ;
93 
94  // Create a default observer, longitude and latitude in degrees. I'm using Ames, IA, USA.
95  // Note that longitude is "east-positive" from Greenwich.
96  CEObserver observer(opt.AsDouble("longitude"), opt.AsDouble("latitude"),
97  opt.AsDouble("elevation"), CEAngleType::DEGREES, &date) ;
98  observer.SetUTCOffset(opt.AsDouble("UTCOffset")) ;
99 
100  // Now get the coordinates of the object as a constantly updating 'CEObservation' object
101  std::vector<CEObservation> coords(planets.size());
102  coords[0] = CEObservation(&observer, planets[0]) ;
103  coords[1] = CEObservation(&observer, planets[1]) ;
104  coords[2] = CEObservation(&observer, planets[2]) ;
105  coords[3] = CEObservation(&observer, planets[3]) ;
106  coords[4] = CEObservation(&observer, planets[4]) ;
107  coords[5] = CEObservation(&observer, planets[5]) ;
108  coords[6] = CEObservation(&observer, planets[6]) ;
109  coords[7] = CEObservation(&observer, planets[7]) ;
110 
111  int update_freq(opt.AsInt("UpdateFrequency")) ;
112 
113  // Now describe the results
114  WINDOW * mainwin;
115 
116  if ( (mainwin = initscr()) == NULL ) {
117  fprintf(stderr, "Error initialising ncurses.\n");
118  exit(EXIT_FAILURE);
119  }
120 
121  int line = Description() ;
122 
123  mvaddstr(line+6,5, " +-----------+---------------+---------------+--------+--------+---------+") ;
124  mvaddstr(line+7,5, " | Name | RA | DEC | Az | Alt | Zenith |") ;
125  mvaddstr(line+8,5, " +-----------+---------------+---------------+--------+--------+---------+") ;
126  mvaddstr(line+9+planets.size(),5, " +-----------+---------------+---------------+--------+--------+---------+") ;
127 
128  char str[200] ;
129  std::vector<double> ra_vec, dec_vec, az(planets.size()), zen(planets.size()) ;
130  double ra, dec ;
131  while (true) {
132  usleep(update_freq*1000);
133 
134  // Update the date to the current time
135  date.SetDate() ;
136  // Compute tdb
137  double jd_tdb = CEDate::UTC2TDB( date ) ;
138 
139  // Print date and time information
140  std::string jd_str = "JD : " + std::to_string(date) +" ("+ std::to_string(jd_tdb) +")";
141  std::string date_str = "Date : " + std::to_string(int(date.Gregorian())) ;
142  std::string utc_str = "UTC : " + std::to_string(date.GetTime_UTC()) ;
143  std::string local_str = "Local: " + std::to_string(date.GetTime(observer.UTCOffset())) ;
144  mvaddstr(line+1,1, jd_str.c_str()) ;
145  mvaddstr(line+2,1, utc_str.c_str()) ;
146  mvaddstr(line+3,1, date_str.c_str()) ;
147  mvaddstr(line+4,1, local_str.c_str()) ;
148 
149  // Loop through each planet and print it's respective information
150  for (int p=0; p<planets.size(); p++) {
151  // Get the RA,Dec
152  ra = planets[p]->XCoordinate_Deg(jd_tdb) ;
153  dec = planets[p]->YCoordinate_Deg(jd_tdb) ;
154  // Format the RA,Dec
155  ra_vec = CECoordinates::GetHMS(ra) ;
156  dec_vec = CECoordinates::GetDMS(dec) ;
157  // Get the alt,az
158 
159 
160  // Generate the new line to be printed
161  std::snprintf(str, 200, " |%10s | %2.0fh %2.0fm %4.1fs |%+3.0fd %2.0fm %4.1fs |%8.3f|%8.3f|%8.3f |",
162  planets[p]->Name().c_str(),
163  ra_vec[0], ra_vec[1], ra_vec[2],
164  dec_vec[0], dec_vec[1], dec_vec[2],
165  coords[p].GetAzimuth_Deg(),
166  90.0-coords[p].GetZenith_Deg(),
167  coords[p].GetZenith_Deg()) ;
168  // Add the line to the output
169  mvaddstr(line+9+p,5,str) ;
170  }
171 
172  // Draw a little picture that represents the southern sky for the observer
173  if (opt.AsBool("DrawMap")) DrawSkyMap(coords, line+9+planets.size()+2) ;
174 
175  refresh();
176  }
177 
178  delwin(mainwin);
179  endwin();
180  refresh();
181 }
182 
183 /**********************************************************************/
185 void DrawSkyMap(std::vector<CEObservation> &coords, int line)
186 {
187  DrawGrid(line) ;
188  // Loop through the planet objects and plot their alt,az values
189 // int x,y ;
190  double az,zen ;
191  for (int p=0; p<coords.size(); p++) {
192  az = coords[p].GetAzimuth_Deg() ;
193  if ((az < 90.0) || (az > 270.0)) continue ;
194  zen = coords[p].GetZenith_Deg() ;
195  if (zen > 90.0) continue ;
196  int x = 6+(az-90.0)/2.0 ;
197  int y = line+(zen/5.0)+1 ;
198  std::string abbrv( coords[p].Body()->Name() ) ;
199  abbrv[1] = '\0' ;
200  mvaddstr(y, x, abbrv.c_str()) ;
201  }
202 }
203 
204 /**********************************************************************/
206 void DrawGrid(int line)
207 {
208  // Draw the coordinate grid first
209  mvaddstr(line++, 1, " 90 +--------------+--------------+--------------+--------------+--------------+--------------+") ;
210  mvaddstr(line++, 1, "A | . . | . . |") ;
211  mvaddstr(line++, 1, "L 80 | . . | . . |") ;
212  mvaddstr(line++, 1, "T | . . | . . |") ;
213  mvaddstr(line++, 1, "I 70 | . . | . . |") ;
214  mvaddstr(line++, 1, "T | . . | . . |") ;
215  mvaddstr(line++, 1, "U 60 |............................................|............................................|") ;
216  mvaddstr(line++, 1, "D | . . | . . |") ;
217  mvaddstr(line++, 1, "E 50 | . . | . . |") ;
218  mvaddstr(line++, 1, " | . . | . . |") ;
219  mvaddstr(line++, 1, " 40 | . . | . . |") ;
220  mvaddstr(line++, 1, " | . . | . . |") ;
221  mvaddstr(line++, 1, " 30 |............................................|............................................|") ;
222  mvaddstr(line++, 1, " | . . | . . |") ;
223  mvaddstr(line++, 1, " 20 | . . | . . |") ;
224  mvaddstr(line++, 1, " | . . | . . |") ;
225  mvaddstr(line++, 1, " 10 | . . | . . |") ;
226  mvaddstr(line++, 1, " | . . | . . |") ;
227  mvaddstr(line++, 1, " 0 |______________.______________.______________|______________.______________.______________|") ;
228  mvaddstr(line++, 1, " 90 120 150 180 210 240 270") ;
229  mvaddstr(line++, 1, " E AZIMUTH S AZIMUTH W") ;
230 
231 }
232 
233 /**********************************************************************/
235 int Description()
236 {
237  int line(0) ;
238  mvaddstr(line++, 1, "DESCRIPTION:") ;
239  mvaddstr(line++, 3, "This program prints the current positions of the planets to the terminal.") ;
240 // mvaddstr(line++, 3, "NOTE: Az,Alt,Zenith values are relative to Earth-Moon barycenter.") ;
241  return line ;
242 }
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
CEDate::GetTime_UTC
virtual double GetTime_UTC() const
Method for getting the current UTC time.
Definition: CEDate.cpp:621
CEDate
Definition: CEDate.h:43
CEPlanet::Mercury
static CEPlanet Mercury()
Returns an object representing Mercury.
Definition: CEPlanet.cpp:211
CEPlanet::Pluto
static CEPlanet Pluto()
Returns an object representing Pluto.
Definition: CEPlanet.cpp:448
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
CEExecOptions.h
CEDate::GetTime
virtual double GetTime(const double &utc_offset=0.0) const
Method for getting the current time.
Definition: CEDate.cpp:607
SOFA
Use methods included in sofa software.
Definition: CEPlanet.h:48
CECoordinates::GetDMS
static std::vector< double > GetDMS(const double &angle, const CEAngleType &angle_type=CEAngleType::DEGREES)
Convert a given angle into degrees, arcminutes, arcseconds.
Definition: CECoordinates.cpp:1594
CEDate::UTC2TDB
static void UTC2TDB(const double &mjd, double *tdb1, double *tdb2)
Convert the UTC MJD to TDB JD (useful for planet computations)
Definition: CEDate.cpp:429
CppEphem.h
JPL
Use Keplerian algorithm outlined by JPL.
Definition: CEPlanet.h:49
CEAngleType::DEGREES
DefineOptions
CEExecOptions DefineOptions()
Define the command line options for this program.
Definition: planetpositions.cpp:39
CEPlanet::Venus
static CEPlanet Venus()
Returns an object representing Venus.
Definition: CEPlanet.cpp:236
DrawSkyMap
void DrawSkyMap(std::vector< CEObservation > &coords, int line)
Forward Declarations.
Definition: planetpositions.cpp:184
CEPlanet
Definition: CEPlanet.h:35
CEPlanet::Saturn
static CEPlanet Saturn()
Returns an object representing Saturn.
Definition: CEPlanet.cpp:363
CECoordinates::GetHMS
static std::vector< double > GetHMS(const double &angle, const CEAngleType &angle_type=CEAngleType::DEGREES)
Convert a given angle into hours, minutes, seconds.
Definition: CECoordinates.cpp:1613
CETime::SystemUTCOffset_hrs
static double SystemUTCOffset_hrs()
Definition: CETime.h:74
main
int main(int argc, char **argv)
Main.
Definition: planetpositions.cpp:65
CEObserver
Definition: CEObserver.h:28
CEObservation
Definition: CEObservation.h:33
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
Description
int Description()
Definition: planetpositions.cpp:234
CEDate::Gregorian
virtual double Gregorian() const
Get the Gregorian calendar date formatted as a double.
Definition: CEDate.h:170
DrawGrid
void DrawGrid(int line)
Definition: planetpositions.cpp:205
CEPlanet::Jupiter
static CEPlanet Jupiter()
Returns an object representing Jupiter.
Definition: CEPlanet.cpp:335
CEExecOptions
Definition: CEExecOptions.h:9