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

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