source: trunk/FACT++/src/makeplots.cc@ 17957

Last change on this file since 17957 was 17957, checked in by tbretz, 11 years ago
Make use of new central current prediction.
File size: 10.4 KB
Line 
1#include "externals/Prediction.h"
2
3#include "Database.h"
4
5#include "Time.h"
6#include "Configuration.h"
7
8#include <TROOT.h>
9#include <TH1.h>
10#include <TGraph.h>
11#include <TCanvas.h>
12#include <TLegend.h>
13
14using namespace std;
15using namespace Nova;
16
17// ------------------------------------------------------------------------
18
19void CheckForGap(TCanvas &c, TGraph &g, double axis)
20{
21 if (g.GetN()==0 || axis-g.GetX()[g.GetN()-1]<450)
22 return;
23
24 c.cd();
25 ((TGraph*)g.DrawClone("C"))->SetBit(kCanDelete);
26 while (g.GetN())
27 g.RemovePoint(0);
28}
29
30void DrawClone(TCanvas &c, TGraph &g)
31{
32 if (g.GetN()==0)
33 return;
34
35 c.cd();
36 ((TGraph*)g.DrawClone("C"))->SetBit(kCanDelete);
37}
38
39// ========================================================================
40// ========================================================================
41// ========================================================================
42
43void SetupConfiguration(Configuration &conf)
44{
45 po::options_description control("Smart FACT");
46 control.add_options()
47 //("ra", var<double>(), "Source right ascension")
48 //("dec", var<double>(), "Source declination")
49 ("date-time", var<string>(), "SQL time (UTC)")
50 ("source-database", var<string>(""), "Database link as in\n\tuser:password@server[:port]/database.")
51 ("max-current", var<double>(100), "Maximum current to display in other plots.")
52 ("max-zd", var<double>(50), "Maximum zenith distance to display in other plots")
53 ("no-limits", po_switch(), "Switch off limits in plots")
54 ;
55
56 po::positional_options_description p;
57 p.add("date-time", 1); // The first positional options
58
59 conf.AddOptions(control);
60 conf.SetArgumentPositions(p);
61}
62
63void PrintUsage()
64{
65 cout <<
66 "makeplots - The astronomy plotter\n"
67 "\n"
68 "Calculates several plots for the sources in the database\n"
69 "helpful or needed for scheduling. The Plot is always calculated\n"
70 "for the night which starts at the same so. So no matter if\n"
71 "you specify '1974-09-09 00:00:00' or '1974-09-09 21:56:00'\n"
72 "the plots will refer to the night 1974-09-09/1974-09-10.\n"
73 "The advantage is that specification of the date as in\n"
74 "1974-09-09 is enough. Time axis starts and ends at nautical\n"
75 "twilight which is 12deg below horizon.\n"
76 "\n"
77 "Usage: makeplots sql-datetime\n";
78// "Usage: makeplots sql-datetime [--ra={ra} --dec={dec}]\n";
79 cout << endl;
80}
81
82int main(int argc, const char* argv[])
83{
84 gROOT->SetBatch();
85
86 Configuration conf(argv[0]);
87 conf.SetPrintUsage(PrintUsage);
88 SetupConfiguration(conf);
89
90 if (!conf.DoParse(argc, argv))
91 return 127;
92
93// if (conf.Has("ra")^conf.Has("dec"))
94// {
95// cout << "ERROR - Either --ra or --dec missing." << endl;
96// return 1;
97// }
98
99 // ------------------ Eval config ---------------------
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 // Sun set with the same date than th provided date
111 // Sun rise on the following day
112 const RstTime sun_set = GetSolarRst(time.JD()-0.5, -12);
113 const RstTime sun_rise = GetSolarRst(time.JD()+0.5, -12);
114
115 const double jd = floor(time.Mjd())+2400001;
116 const double mjd = floor(time.Mjd())+49718+0.5; // root time
117
118 cout << "Time: " << time << endl;
119 cout << "Base: " << Time(jd-0.5) << endl;
120 cout << "Set: " << Time(sun_set.set) << endl;
121 cout << "Rise: " << Time(sun_rise.rise) << endl;
122
123 const double sunset = sun_set.set;
124 const double sunrise = sun_rise.rise;
125
126 const string fDatabase = conf.Get<string>("source-database");
127
128 // ------------------ Precalc coord ---------------------
129
130 vector<SolarObjects> fCoordinates;
131
132 for (double h=0; h<1; h+=1./(24*12))
133 if (jd+h>sunset && jd+h<sunrise)
134 fCoordinates.emplace_back(jd+h);
135
136 // ------------- Get Sources from databasse ---------------------
137
138 const mysqlpp::StoreQueryResult res =
139 Database(fDatabase).query("SELECT fSourceName, fRightAscension, fDeclination FROM Source WHERE fSourceTypeKEY=1").store();
140
141 // ------------- Create canvases and frames ---------------------
142
143 // It is important to use an offset which is larger than
144 // 1970-01-01 00:00:00. This one will not work if your
145 // local time zone is positive!
146 TH1S hframe("", "", 1, Time(sunset).Mjd()*24*3600, Time(sunrise).Mjd()*24*3600);
147 hframe.SetStats(kFALSE);
148 hframe.GetXaxis()->SetTimeFormat("%Hh%M%F1995-01-01 00:00:00 GMT");
149 hframe.GetXaxis()->SetTitle((Time(jd).GetAsStr("%d/%m/%Y")+" - "+Time(jd+1).GetAsStr("%d/%m/%Y")+" [UTC]").c_str());
150 hframe.GetXaxis()->CenterTitle();
151 hframe.GetYaxis()->CenterTitle();
152 hframe.GetXaxis()->SetTimeDisplay(true);
153 hframe.GetYaxis()->SetTitleSize(0.040);
154 hframe.GetXaxis()->SetTitleSize(0.040);
155 hframe.GetXaxis()->SetTitleOffset(1.1);
156 hframe.GetYaxis()->SetLabelSize(0.040);
157 hframe.GetXaxis()->SetLabelSize(0.040);
158
159 TCanvas c1;
160 c1.SetFillColor(kWhite);
161 c1.SetBorderMode(0);
162 c1.SetFrameBorderMode(0);
163 c1.SetLeftMargin(0.085);
164 c1.SetRightMargin(0.01);
165 c1.SetTopMargin(0.03);
166 c1.SetGrid();
167 hframe.GetYaxis()->SetTitle("Altitude [deg]");
168 hframe.SetMinimum(15);
169 hframe.SetMaximum(90);
170 hframe.DrawCopy();
171
172 TCanvas c2;
173 c2.SetFillColor(kWhite);
174 c2.SetBorderMode(0);
175 c2.SetFrameBorderMode(0);
176 c2.SetLeftMargin(0.085);
177 c2.SetRightMargin(0.01);
178 c2.SetTopMargin(0.03);
179 c2.SetGrid();
180 hframe.GetYaxis()->SetTitle("Predicted Current [#muA]");
181 hframe.SetMinimum(0);
182 hframe.SetMaximum(100);
183 hframe.DrawCopy();
184
185 TCanvas c3;
186 c3.SetFillColor(kWhite);
187 c3.SetBorderMode(0);
188 c3.SetFrameBorderMode(0);
189 c3.SetLeftMargin(0.085);
190 c3.SetRightMargin(0.01);
191 c3.SetTopMargin(0.03);
192 c3.SetGrid();
193 c3.SetLogy();
194 hframe.GetYaxis()->SetTitle("Estimated relative threshold");
195 hframe.GetYaxis()->SetMoreLogLabels();
196 hframe.SetMinimum(0.9);
197 hframe.SetMaximum(11);
198 hframe.DrawCopy();
199
200 TCanvas c4;
201 c4.SetFillColor(kWhite);
202 c4.SetBorderMode(0);
203 c4.SetFrameBorderMode(0);
204 c4.SetLeftMargin(0.085);
205 c4.SetRightMargin(0.01);
206 c4.SetTopMargin(0.03);
207 c4.SetGrid();
208 hframe.GetYaxis()->SetTitle("Distance to moon [deg]");
209 hframe.SetMinimum(0);
210 hframe.SetMaximum(180);
211 hframe.DrawCopy();
212
213 Int_t color[] = { kBlack, kRed, kBlue, kGreen, kCyan, kMagenta };
214 Int_t style[] = { kSolid, kDashed, kDotted };
215
216 TLegend leg(0, 0, 1, 1);
217
218 // ------------- Loop over sources ---------------------
219
220 Int_t cnt=0;
221 for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++, cnt++)
222 {
223 // Eval row
224 const string name = (*v)[0].c_str();
225
226 EquPosn pos;
227 pos.ra = double((*v)[1])*15;
228 pos.dec = double((*v)[2]);
229
230 // Create graphs
231 TGraph g1, g2, g3, g4, gr, gm;
232 g1.SetName(name.data());
233 g2.SetName(name.data());
234 g3.SetName(name.data());
235 g4.SetName(name.data());
236 g1.SetLineWidth(2);
237 g2.SetLineWidth(2);
238 g3.SetLineWidth(2);
239 g4.SetLineWidth(2);
240 gm.SetLineWidth(1);
241 g1.SetLineStyle(style[cnt/6]);
242 g2.SetLineStyle(style[cnt/6]);
243 g3.SetLineStyle(style[cnt/6]);
244 g4.SetLineStyle(style[cnt/6]);
245 g1.SetLineColor(color[cnt%6]);
246 g2.SetLineColor(color[cnt%6]);
247 g3.SetLineColor(color[cnt%6]);
248 g4.SetLineColor(color[cnt%6]);
249 gm.SetLineColor(kYellow);
250
251 if (cnt==0)
252 leg.AddEntry(gm.Clone(), "Moon", "l");
253 leg.AddEntry(g1.Clone(), name.data(), "l");
254
255 // Loop over 24 hours
256 for (auto it=fCoordinates.begin(); it!=fCoordinates.end(); it++)
257 {
258 // get local position of source
259 const HrzPosn hrz = GetHrzFromEqu(pos, it->fJD);
260
261 if (v==res.begin())
262 cout << Time(it->fJD) <<" " << 90-it->fMoonHrz.alt << endl;
263
264 const double cur = FACT::PredictI(*it, pos);
265
266 // Relative energy threshold prediction
267 const double ratio = pow(cos((90-hrz.alt)*M_PI/180), -2.664);
268
269 // Add points to curve
270 const double axis = Time(it->fJD).Mjd()*24*3600;
271
272 // If there is a gap of more than one bin, start a new curve
273 CheckForGap(c1, g1, axis);
274 CheckForGap(c1, gm, axis);
275 CheckForGap(c2, g2, axis);
276 CheckForGap(c3, g3, axis);
277 CheckForGap(c4, g4, axis);
278
279 // Add data
280 if (no_limits || cur<max_current)
281 g1.SetPoint(g1.GetN(), axis, hrz.alt);
282
283 if (no_limits || 90-hrz.alt<max_zd)
284 g2.SetPoint(g2.GetN(), axis, cur);
285
286 if (no_limits || (cur<max_current && 90-hrz.alt<max_zd))
287 g3.SetPoint(g3.GetN(), axis, ratio*pow(cur/6.2, 0.394));
288
289 if (no_limits || (cur<max_current && 90-hrz.alt<max_zd))
290 {
291 const double angle = GetAngularSeparation(it->fMoonEqu, pos);
292 g4.SetPoint(g4.GetN(), axis, angle);
293 }
294
295 if (cnt==0)
296 gm.SetPoint(gm.GetN(), axis, it->fMoonHrz.alt);
297 }
298
299 if (cnt==0)
300 DrawClone(c1, gm);
301
302 DrawClone(c1, g1);
303 DrawClone(c2, g2);
304 DrawClone(c3, g3);
305 DrawClone(c4, g4);
306 }
307
308
309 // Save three plots
310 TCanvas c5;
311 c5.SetFillColor(kWhite);
312 c5.SetBorderMode(0);
313 c5.SetFrameBorderMode(0);
314 leg.Draw();
315
316 const string t = Time(jd).GetAsStr("%Y%m%d");
317
318 c1.SaveAs((t+"-ZenithDistance.eps").c_str());
319 c2.SaveAs((t+"-PredictedCurrent.eps").c_str());
320 c3.SaveAs((t+"-RelativeThreshold.eps").c_str());
321 c4.SaveAs((t+"-MoonDist.eps").c_str());
322 c5.SaveAs((t+"-Legend.eps").c_str());
323
324 c1.SaveAs((t+"-ZenithDistance.root").c_str());
325 c2.SaveAs((t+"-PredictedCurrent.root").c_str());
326 c3.SaveAs((t+"-RelativeThreshold.root").c_str());
327 c4.SaveAs((t+"-MoonDist.root").c_str());
328
329 c1.Print((t+".pdf(").c_str(), "pdf");
330 c2.Print((t+".pdf" ).c_str(), "pdf");
331 c3.Print((t+".pdf" ).c_str(), "pdf");
332 c4.Print((t+".pdf" ).c_str(), "pdf");
333 c5.Print((t+".pdf)").c_str(), "pdf");
334
335 return 0;
336}
Note: See TracBrowser for help on using the repository browser.