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

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