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

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