source: trunk/FACT++/src/makedata.cc@ 15470

Last change on this file since 15470 was 15366, checked in by tbretz, 12 years ago
Fixed a crash when the source was not found.
File size: 6.8 KB
Line 
1#include "externals/nova.h"
2
3#include "Database.h"
4
5#include "Time.h"
6#include "Configuration.h"
7
8using namespace std;
9
10// ------------------------------------------------------------------------
11
12double Angle(double ra, double dec, double r, double d)
13{
14 const double theta0 = M_PI/2-d*M_PI/180;
15 const double phi0 = r*M_PI/12;
16
17 const double theta1 = M_PI/2-dec*M_PI/180;
18 const double phi1 = ra*M_PI/12;
19
20 const double x0 = sin(theta0) * cos(phi0);
21 const double y0 = sin(theta0) * sin(phi0);
22 const double z0 = cos(theta0);
23
24 const double x1 = sin(theta1) * cos(phi1);
25 const double y1 = sin(theta1) * sin(phi1);
26 const double z1 = cos(theta1);
27
28 double arg = x0*x1 + y0*y1 + z0*z1;
29 if(arg > 1.0) arg = 1.0;
30 if(arg < -1.0) arg = -1.0;
31
32 return acos(arg) * 180/M_PI;
33}
34
35// ========================================================================
36// ========================================================================
37// ========================================================================
38
39void SetupConfiguration(Configuration &conf)
40{
41 po::options_description control("Smart FACT");
42 control.add_options()
43 ("source-name", var<string>(), "Source name")
44 ("date-time", var<string>(), "SQL time (UTC)")
45 ("source-database", var<string>(""), "Database link as in\n\tuser:password@server[:port]/database.")
46 ("max-current", var<double>(75), "Maximum current to display in other plots.")
47 ("max-zd", var<double>(75), "Maximum zenith distance to display in other plots")
48 ("no-limits", po_switch(), "Switch off limits in plots")
49 ;
50
51 po::positional_options_description p;
52 p.add("source-name", 1); // The 1st positional options
53 p.add("date-time", 2); // The 2nd positional options
54
55 conf.AddOptions(control);
56 conf.SetArgumentPositions(p);
57}
58
59void PrintUsage()
60{
61 cout <<
62 "makedata - The astronomy data listing\n"
63 "\n"
64// "Calculates several plots for the sources in the database\n"
65// "helpful or needed for scheduling. The Plot is always calculated\n"
66// "for the night which starts at the same so. So no matter if\n"
67// "you specify '1974-09-09 00:00:00' or '1974-09-09 21:56:00'\n"
68// "the plots will refer to the night 1974-09-09/1974-09-10.\n"
69// "The advantage is that specification of the date as in\n"
70// "1974-09-09 is enough. Time axis starts and ends at nautical\n"
71// "twilight which is 12deg below horizon.\n"
72// "\n"
73 "Usage: makedata sql-datetime [--ra={ra} --dec={dec}]\n";
74 cout << endl;
75}
76
77int main(int argc, const char* argv[])
78{
79 Configuration conf(argv[0]);
80 conf.SetPrintUsage(PrintUsage);
81 SetupConfiguration(conf);
82
83 if (!conf.DoParse(argc, argv))
84 return 127;
85/*
86 if (!conf.Has("source-name"))
87 {
88 cout << "ERROR - --source-name missing." << endl;
89 return 1;
90 }
91*/
92 // ------------------ Eval config ---------------------
93
94 const double lon = -(17.+53./60+26.525/3600);
95 const double lat = 28.+45./60+42.462/3600;
96
97 ln_lnlat_posn observer;
98 observer.lng = lon;
99 observer.lat = lat;
100
101 Time time;
102 if (conf.Has("date-time"))
103 time.SetFromStr(conf.Get<string>("date-time"));
104
105 const double max_current = conf.Get<double>("max-current");
106 const double max_zd = conf.Get<double>("max-zd");
107 const double no_limits = conf.Get<bool>("no-limits");
108
109 // -12: nautical
110 ln_rst_time sun_set; // Sun set with the same date than th provided date
111 ln_rst_time sun_rise; // Sun rise on the following day
112 ln_get_solar_rst_horizon(time.JD()-0.5, &observer, -12, &sun_set);
113 ln_get_solar_rst_horizon(time.JD()+0.5, &observer, -12, &sun_rise);
114
115 const double jd = floor(time.Mjd())+2400001;
116 const double mjd = floor(time.Mjd())+49718+0.5;
117
118
119 const double jd0 = fmod(sun_set.set, 1); // ~0.3
120 const double jd1 = fmod(sun_rise.rise, 1); // ~0.8
121
122 cout << Time::iso << time << ", " << mjd-49718 << ", ";
123 cout << jd0 << ", ";
124 cout << jd1 << "\n";
125
126 if (!conf.Has("source-name"))
127 return 1;
128
129 const string source_name = conf.Get<string>("source-name");
130 const string fDatabase = conf.Get<string>("source-database");
131
132 // ------------------ Precalc moon coord ---------------------
133
134 vector<pair<ln_equ_posn, double>> fMoonCoords;
135
136 for (double h=0; h<1; h+=1./(24*12))
137 {
138 ln_equ_posn moon;
139 ln_get_lunar_equ_coords_prec(jd+h, &moon, 0.01);
140
141 const double disk = ln_get_lunar_disk(jd+h);
142
143 fMoonCoords.push_back(make_pair(moon, disk));
144 }
145
146 // ------------- Get Sources from databasse ---------------------
147
148 const mysqlpp::StoreQueryResult res =
149 Database(fDatabase).query("SELECT fRightAscension, fDeclination FROM source WHERE fSourceName='"+source_name+"'").store();
150
151 // ------------- Create canvases and frames ---------------------
152
153 vector<mysqlpp::Row>::const_iterator row=res.begin();
154 if (row==res.end())
155 return 1;
156
157 Nova::EquPosn pos;
158 pos.ra = double((*row)[0])*15;
159 pos.dec = double((*row)[1]);
160
161 // Loop over 24 hours
162 for (int i=0; i<24*12; i++)
163 {
164 const double h = double(i)/(24*12);
165
166 // check if it is betwene sun-rise and sun-set
167 if (h<jd0 || h>jd1)
168 continue;
169
170 // get local position of source
171 const Nova::HrzPosn hrz = Nova::GetHrzFromEqu(pos, jd+h);
172
173 // Get moon properties and
174 const Nova::EquPosn moon = fMoonCoords[i].first;
175 const double disk = fMoonCoords[i].second;
176
177 const Nova::HrzPosn hrzm = Nova::GetHrzFromEqu(moon, jd+h);
178
179 // Distance between source and moon
180 const double angle = Angle(moon.ra, moon.dec, pos.ra, pos.dec);
181
182 // Current prediction
183 const double cang = sin(angle *M_PI/180);
184 const double calt = sin(hrzm.alt*M_PI/180);
185
186 const double lc = cang==0 ? -1 : calt*pow(disk, 3)/sqrt(cang);
187 const double cur = lc>0 ? 8.1+94.6*lc : 8.1;
188
189 // Relative energy threshold prediction
190 const double cs = cos((90+hrz.alt)*M_PI/180);
191 const double ratio = (10.*sqrt(409600.*cs*cs+9009.) + 6400.*cs - 60.)/10.;
192
193 // Add points to curve
194 // const double axis = (mjd+h)*24*3600;
195
196 Time t(mjd-49718);
197 t += boost::posix_time::minutes(i*5);
198
199 cout << t << ", " << h << ", ";
200
201 if (no_limits || cur<max_current)
202 cout << hrz.alt;
203 cout << ", ";
204
205 if (no_limits || 90-hrz.alt<max_zd)
206 cout << cur;
207 cout << ", ";
208
209 if (no_limits || (cur<max_current && 90-hrz.alt<max_zd))
210 cout << ratio*cur/8.1;
211 cout << ", ";
212
213 if (no_limits || (cur<max_current && 90-hrz.alt<max_zd))
214 cout << angle;
215 cout << "\n";
216 }
217
218 cout << flush;
219
220 return 0;
221}
Note: See TracBrowser for help on using the repository browser.