source: trunk/Mars/datacenter/macros/filloptical.C@ 18066

Last change on this file since 18066 was 9131, checked in by Daniela Dorner, 16 years ago
*** empty log message ***
File size: 11.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2006
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// filloptical.C
28// =============
29//
30// This macro is used to read the files from KVA containing the results from
31// the optical observations.
32// Remark: Running it from the commandline looks like this:
33// root -q -l -b filloptical.C+\(\"filename\"\,kFALSE\)
34//
35// Make sure, that database and password are corretly set.
36//
37// Returns 1 in case of success.
38// Returns 2 in case of invalid file or line in file.
39// Returns 3 in case of a missing object name in the database.
40// Returns 4 in case of a telescope line with more or less than 2 arguments
41// Returns 5 in case of a timestamp line with more or less than 4 arguments
42// Returns 6 in case of a object line with more or less than 10 arguments
43//
44///////////////////////////////////////////////////////////////////////////
45#include <iostream>
46#include <iomanip>
47#include <fstream>
48
49#include <TVector3.h>
50
51#include <TRegexp.h>
52
53#include <TSQLRow.h>
54#include <TSQLResult.h>
55
56#include "MDirIter.h"
57#include "MSQLServer.h"
58#include "MSQLMagic.h"
59
60#include "MTime.h"
61#include "MObservatory.h"
62#include "MAstroSky2Local.h"
63
64using namespace std;
65
66//
67// insert the entries from this optical data file into the db
68//
69int process(MSQLMagic &serv, TString fname)
70{
71 ifstream fin(fname);
72 if (!fin)
73 {
74 cout << "Could not open file " << fname << endl;
75 return 0;
76 }
77
78 TRegexp regexp("^20[0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9].*$", kFALSE);
79
80 Int_t numexp=0;
81 Int_t numstars=0;
82
83 TString timestamp;
84 TString exposure;
85 TString fitsfile;
86 TString object;
87 TString skylevel;
88 TString fwhm;
89 TString ellipticity;
90 TString posangle;
91 TString aperturer;
92 TString mag;
93 TString magerr;
94 TString status;
95 TString query;
96 TString select;
97 TString telescope;
98 TString telname;
99 TString ccd;
100 TString filterband;
101 TString filter;
102 TString band;
103 Double_t ra=0;
104 Double_t dec=0;
105 Double_t zd=0;
106 MTime t;
107 MObservatory obs(MObservatory::kMagic1);
108 TVector3 v;
109
110 if (fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE)).IsNull())
111 {
112 cout << "Found not valid file: " << fname << endl;
113 return 2;
114 }
115
116 while (1)
117 {
118 TString line;
119 line.ReadLine(fin);
120 if (!fin)
121 {
122 cout << numstars << " objects inserted for this exposure. " << endl;
123 break;
124 }
125
126 if (line.IsNull())
127 continue;
128
129 TObjArray *arr = line.Tokenize(" ");
130
131 if (line=="KVA_Celestron_ST8 KVA_Celestron_R" ||
132 line=="KVA_Celestron_ST8 KVA_Celestron_V" ||
133 line=="Tuorla_ST1001E Tuorla_R")
134 {
135 if (arr->GetEntries()!=2)
136 {
137 cout << "WARNING: Telescopeline with less or more than 2 arguments found in file " <<
138 fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE)) << endl;
139 cout << "Line: " << line << endl;
140 return 4;
141 }
142 telescope=(*arr)[0]->GetName();
143 telname=telescope(0,telescope.First('_'));
144 ccd=telescope(telescope.Last('_')+1, telescope.Length());
145 filterband=(*arr)[1]->GetName();
146 filter=filterband(0,filterband.Last('_'));
147 band=filterband(filterband.Last('_')+1, filterband.Length());
148 continue;
149 }
150
151 if (!line(regexp).IsNull())
152 {
153 if (arr->GetEntries()!=4)
154 {
155 cout << "WARNING: Timestampline with less or more than 4 arguments found in file " <<
156 fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE)) << endl;
157 cout << "Line: " << line << endl;
158 return 5;
159 }
160 numexp+=1;
161 if (numstars)
162 cout << numstars << " objects inserted for this exposure. " << endl;
163 numstars=0;
164 timestamp =Form("%s %s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
165
166 exposure = (*arr)[2]->GetName();
167 fitsfile = (*arr)[3]->GetName();
168 t.SetSqlDateTime(timestamp.Data());
169 continue;
170 }
171 else
172 {
173 if (arr->GetEntries()!=10)
174 {
175 cout << "WARNING: Objectline with less or more than 10 arguments found in file " <<
176 fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE)) << endl;
177 cout << "Line: " << line << endl;
178 return 6;
179 }
180
181 //calculation of zd if ra and dec are available
182 if (numstars==0)
183 {
184 select="SELECT fRightAscension, fDeclination FROM Object WHERE ";
185 select+=Form("fObjectName='%s/BL'", (*arr)[0]->GetName());
186
187 TSQLResult *res = serv.Query(select);
188 if (!res)
189 return 2;
190
191 TSQLRow *row=res->Next();
192 if (!row)
193 {
194 cout << "Couldn't get ZD - Query failed: " << select << endl;
195 cout << "Position of the object is missing in the DB." << endl;
196 return 3;
197 }
198 else
199 {
200 ra =atof((*row)[0]);
201 dec=atof((*row)[1]);
202 v.SetMagThetaPhi(1, TMath::Pi()/2-(dec*TMath::DegToRad()), ra*TMath::DegToRad()*15);
203 v *= MAstroSky2Local(t, obs);
204 zd = v.Theta()*TMath::RadToDeg();
205 }
206 delete res;
207 }
208
209 object = Form("%s/%s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
210 skylevel = (*arr)[2]->GetName();
211 fwhm = (*arr)[3]->GetName();
212 ellipticity = (*arr)[4]->GetName();
213 posangle = (*arr)[5]->GetName();
214 aperturer = (*arr)[6]->GetName();
215 mag = (*arr)[7]->GetName();
216 magerr = (*arr)[8]->GetName();
217 status = (*arr)[9]->GetName();
218 //make sure that no nonsense values enter the db
219 if (skylevel.Contains("-"))
220 skylevel = "NULL";
221 if (ellipticity.Contains("-"))
222 ellipticity = "NULL";
223 if (fwhm.Contains("-") || !fwhm.IsFloat())
224 fwhm = "NULL";
225 if (posangle.EndsWith("-"))
226 posangle = "NULL";
227 if (!mag.IsFloat())
228 mag = "NULL";
229 if (!magerr.IsFloat())
230 magerr = "NULL";
231 numstars+=1;
232 }
233 delete arr;
234
235 if (numstars!=0)
236 {
237 /*
238 cout << numexp << "th exposure: star # " << numstars << endl;
239 cout << " timestamp : " << timestamp << endl;
240 cout << " exposure : " << exposure << endl;
241 cout << " fitsfile : " << fitsfile << endl;
242 cout << " object : " << object << endl;
243 cout << " skylevel : " << skylevel << endl;
244 cout << " fwhm : " << fwhm << endl;
245 cout << " ellipticity: " << ellipticity << endl;
246 cout << " posangle : " << posangle << endl;
247 cout << " aperturer : " << aperturer << endl;
248 cout << " mag : " << mag << " +/- " << magerr << endl;
249 cout << " status : " << status << endl << endl;
250 */
251
252 Int_t statuskey = serv.QueryKeyOfName("Status", status.Data());
253 Int_t objectkey = serv.QueryKeyOfName("Object", object.Data());
254 Int_t fitsfilekey = serv.QueryKeyOfName("FitsFile", fitsfile.Data());
255 Int_t telkey = serv.QueryKeyOfName("Telescope", telname.Data());
256 Int_t ccdkey = serv.QueryKeyOfName("CCD", ccd.Data());
257 Int_t bandkey = serv.QueryKeyOfName("Band", band.Data());
258 Int_t filterkey = serv.QueryKeyOfName("Filter", filter.Data());
259
260 query=Form("fTimeStamp='%s', fExposure=%s, fFitsFileKEY=%d, "
261 "fObjectKEY=%d, fSkyLevel=%s, fFWHM=%s, "
262 "fEllipticity=%s, fPosAngle=%s, fApertureRadius=%s, "
263 "fInstrumentalMag=%s, fInstrumentalMagErr=%s, "
264 "fStatusKEY=%d, fCCDKEY=%d, fFilterKEY=%d, "
265 "fTelescopeKEY=%d, fBandKEY=%d, fZenithDistance=%.1f ",
266 timestamp.Data(), exposure.Data(), fitsfilekey,
267 objectkey, skylevel.Data(), fwhm.Data(),
268 ellipticity.Data(), posangle.Data(), aperturer.Data(),
269 mag.Data(), magerr.Data(), statuskey, ccdkey,
270 filterkey, telkey, bandkey, zd);
271
272 /*
273 if (ra==0 || dec==0 || zd==0)
274 query+="NULL";
275 else
276 query+=Form("%.1f", zd);
277 */
278 if (serv.Insert("OpticalData", query)==kFALSE)
279 return 2;
280 }
281 }
282
283 cout << fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE))
284 << ": " << setw(2) << numexp << " exposures." << endl << endl;
285
286 return 1;
287}
288
289// --------------------------------------------------------------------------
290//
291// loop over all files in this path
292//
293int filloptical(TString path, Bool_t dummy=kTRUE)
294{
295 MSQLMagic serv("sql.rc");
296 if (!serv.IsConnected())
297 {
298 cout << "ERROR - Connection to database failed." << endl;
299 return 0;
300 }
301
302 serv.SetIsDummy(dummy);
303
304 cout << endl;
305 cout << "filloptical" << endl;
306 cout << "-----------" << endl;
307 cout << endl;
308 cout << "Connected to " << serv.GetName() << endl;
309 cout << "Search Path: " << path << endl;
310 cout << endl;
311
312 //get all runbook files in path
313 if (path.EndsWith(".instr"))
314 return process(serv, path);
315
316 //fill entries for each optical data file
317 MDirIter Next(path, "20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_*_R[_]?[12]?[.]instr", -1);
318 while (1)
319 {
320 TString name = Next();
321 if (name.IsNull())
322 break;
323
324 if (!process(serv, name))
325 return 0;
326 }
327
328 return 1;
329}
Note: See TracBrowser for help on using the repository browser.