source: trunk/FACT++/src/moon.cc@ 14227

Last change on this file since 14227 was 14188, checked in by tbretz, 12 years ago
Fixed a problem with the optionality of ra and dec
File size: 5.1 KB
Line 
1#include <libnova/solar.h>
2#include <libnova/lunar.h>
3#include <libnova/rise_set.h>
4#include <libnova/transform.h>
5
6#include "Time.h"
7#include "Configuration.h"
8
9using namespace std;
10
11// ------------------------------------------------------------------------
12
13class Moon
14{
15public:
16 Time time;
17
18 double ra;
19 double dec;
20
21 double zd;
22 double az;
23
24 double disk;
25
26 bool visible;
27
28 Time fRise;
29 Time fTransit;
30 Time fSet;
31
32 int state;
33
34 Moon() : time(Time::none)
35 {
36 }
37
38 // Could be done more efficient: Only recalcuate if
39 // the current time exceeds at least on of the stored times
40 Moon(double lon, double lat, const Time &t=Time()) : time(t)
41 {
42 const double JD = time.JD();
43
44 ln_lnlat_posn observer;
45 observer.lng = lon;
46 observer.lat = lat;
47
48 //observer.lng.degrees = -5;
49 //observer.lng.minutes = 36;
50 //observer.lng.seconds = 30;
51 //observer.lat.degrees = 42;
52 //observer.lat.minutes = 35;
53 //observer.lat.seconds = 40;
54
55 ln_rst_time moon;
56 ln_get_lunar_rst(JD-0.5, &observer, &moon);
57
58 fRise = Time(moon.rise);
59 fTransit = Time(moon.transit);
60 fSet = Time(moon.set);
61
62 //visible =
63 // ((JD>moon.rise && JD<moon.set ) && moon.rise<moon.set) ||
64 // ((JD<moon.set || JD>moon.rise) && moon.rise>moon.set);
65
66 const bool is_up = JD>moon.rise;
67 const bool is_sinking = JD>moon.transit;
68 const bool is_dn = JD>moon.set;
69
70 ln_get_lunar_rst(JD+0.5, &observer, &moon);
71 if (is_up)
72 fRise = Time(moon.rise);
73 if (is_sinking)
74 fTransit = Time(moon.transit);
75 if (is_dn)
76 fSet = Time(moon.set);
77
78 ln_equ_posn pos;
79 ln_get_lunar_equ_coords(JD, &pos);
80
81 ln_hrz_posn hrz;
82 ln_get_hrz_from_equ (&pos, &observer, JD, &hrz);
83 az = hrz.az;
84 zd = 90-hrz.alt;
85
86 ra = pos.ra/15;
87 dec = pos.dec;
88
89 disk = ln_get_lunar_disk(JD)*100;
90 state = 0;
91 if (fRise <fTransit && fRise <fSet) state = 0; // not visible
92 if (fTransit<fSet && fTransit<fRise) state = 1; // before culm
93 if (fSet <fRise && fSet <fTransit) state = 2; // after culm
94
95 visible = state!=0;
96
97 // 0: not visible
98 // 1: visible before cul
99 // 2: visible after cul
100 }
101
102 double Angle(double r, double d) const
103 {
104 const double theta0 = M_PI/2-d*M_PI/180;
105 const double phi0 = r*M_PI/12;
106
107 const double theta1 = M_PI/2-dec*M_PI/180;
108 const double phi1 = ra*M_PI/12;
109
110 const double x0 = sin(theta0) * cos(phi0);
111 const double y0 = sin(theta0) * sin(phi0);
112 const double z0 = cos(theta0);
113
114 const double x1 = sin(theta1) * cos(phi1);
115 const double y1 = sin(theta1) * sin(phi1);
116 const double z1 = cos(theta1);
117
118 double arg = x0*x1 + y0*y1 + z0*z1;
119 if(arg > 1.0) arg = 1.0;
120 if(arg < -1.0) arg = -1.0;
121
122 return acos(arg) * 180/M_PI;
123 }
124};
125
126// ========================================================================
127// ========================================================================
128// ========================================================================
129
130void SetupConfiguration(Configuration &conf)
131{
132 po::options_description control("Smart FACT");
133 control.add_options()
134 ("ra", var<double>(), "Source right ascension")
135 ("dec", var<double>(), "Source declination")
136 ("date-time", var<string>()
137#if BOOST_VERSION >= 104200
138 ->required()
139#endif
140 , "SQL time (UTC)")
141 ;
142
143 po::positional_options_description p;
144 p.add("date-time", 1); // The first positional options
145
146 conf.AddOptions(control);
147 conf.SetArgumentPositions(p);
148}
149
150void PrintUsage()
151{
152 cout <<
153 "moon - The moon calculator\n"
154 "\n"
155 "Usage: moon sql-datetime [--ra={ra} --dec={dec}]\n";
156 cout << endl;
157}
158
159int main(int argc, const char* argv[])
160{
161 Configuration conf(argv[0]);
162 conf.SetPrintUsage(PrintUsage);
163 SetupConfiguration(conf);
164
165 if (!conf.DoParse(argc, argv))
166 return 127;
167
168 if (conf.Has("ra")^conf.Has("dec"))
169 {
170 cout << "ERROR - Either --ra or --dec missing." << endl;
171 return 1;
172 }
173
174 Time time;
175 time.SetFromStr(conf.Get<string>("date-time"));
176
177 ln_lnlat_posn observer;
178 observer.lng = -(17.+53./60+26.525/3600);
179 observer.lat = 28.+45./60+42.462/3600;
180
181 const Moon moon(observer.lng, observer.lat, time);
182
183 cout << setprecision(15);
184 cout << time.GetAsStr() << '\n';
185 cout << moon.visible << '\n';
186 cout << moon.disk << '\n';
187 cout << moon.zd << '\n';
188
189 if (conf.Has("ra") && conf.Has("dec"))
190 {
191 ln_equ_posn pos;
192 pos.ra = conf.Get<double>("ra")*15;
193 pos.dec = conf.Get<double>("dec");
194
195 ln_hrz_posn hrz;
196 ln_get_hrz_from_equ(&pos, &observer, time.JD(), &hrz);
197 cout << moon.Angle(pos.ra/15, pos.dec) << '\n';
198 }
199
200 cout << endl;
201
202 return 0;
203}
Note: See TracBrowser for help on using the repository browser.