source: trunk/MagicSoft/Mars/datacenter/macros/filloptical.C@ 9108

Last change on this file since 9108 was 8996, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 9.9 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//
41///////////////////////////////////////////////////////////////////////////
42#include <iostream>
43#include <iomanip>
44#include <fstream>
45
46#include <TVector3.h>
47
48#include <TRegexp.h>
49
50#include <TSQLRow.h>
51#include <TSQLResult.h>
52
53#include "MDirIter.h"
54#include "MSQLServer.h"
55#include "MSQLMagic.h"
56
57#include "MTime.h"
58#include "MObservatory.h"
59#include "MAstroSky2Local.h"
60
61using namespace std;
62
63//
64// insert the entries from this optical data file into the db
65//
66int process(MSQLMagic &serv, TString fname)
67{
68 ifstream fin(fname);
69 if (!fin)
70 {
71 cout << "Could not open file " << fname << endl;
72 return 0;
73 }
74
75 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);
76
77 Int_t numexp=0;
78 Int_t numstars=0;
79
80 TString timestamp;
81 TString exposure;
82 TString fitsfile;
83 TString object;
84 TString skylevel;
85 TString fwhm;
86 TString aperturer;
87 TString mag;
88 TString magerr;
89 TString status;
90 TString query;
91 TString select;
92 TString telescope;
93 TString telname;
94 TString ccd;
95 TString filterband;
96 TString filter;
97 TString band;
98 Double_t ra=0;
99 Double_t dec=0;
100 Double_t zd=0;
101 MTime t;
102 MObservatory obs(MObservatory::kMagic1);
103 TVector3 v;
104
105 if (fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE)).IsNull())
106 {
107 cout << "Found not valid file: " << fname << endl;
108 return 2;
109 }
110
111 while (1)
112 {
113 TString line;
114 line.ReadLine(fin);
115 if (!fin)
116 {
117 cout << numstars << " objects inserted for this exposure. " << endl;
118 break;
119 }
120
121 if (line.IsNull())
122 continue;
123
124 TObjArray *arr = line.Tokenize(" ");
125
126 if (line=="KVA_Celestron_ST8 KVA_Celestron_R" ||
127 line=="KVA_Celestron_ST8 KVA_Celestron_V" ||
128 line=="Tuorla_ST1001E Tuorla_R")
129 {
130 if (arr->GetEntries()!=2)
131 {
132 cout << "WARNING: Telescopeline with less or more than 2 arguments found " << endl;
133 return 2;
134 }
135 telescope=(*arr)[0]->GetName();
136 telname=telescope(0,telescope.First('_'));
137 ccd=telescope(telescope.Last('_')+1, telescope.Length());
138 filterband=(*arr)[1]->GetName();
139 filter=filterband(0,filterband.Last('_'));
140 band=filterband(filterband.Last('_')+1, filterband.Length());
141 continue;
142 }
143
144 if (!line(regexp).IsNull())
145 {
146 if (arr->GetEntries()!=4)
147 {
148 cout << "WARNING: Timestampline with less or more than 4 arguments found " << endl;
149 return 2;
150 }
151 numexp+=1;
152 if (numstars)
153 cout << numstars << " objects inserted for this exposure. " << endl;
154 numstars=0;
155 timestamp =Form("%s %s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
156
157 exposure = (*arr)[2]->GetName();
158 fitsfile = (*arr)[3]->GetName();
159 t.SetSqlDateTime(timestamp.Data());
160 continue;
161 }
162 else
163 {
164 if (arr->GetEntries()!=8)
165 {
166 cout << "WARNING: Objectline with less or more than 8 arguments found " << endl;
167 cout << line << endl;
168 return 2;
169 }
170
171 //calculation of zd if ra and dec are available
172 if (numstars==0)
173 {
174 select="SELECT fRightAscension, fDeclination FROM Object WHERE ";
175 select+=Form("fObjectName='%s/BL'", (*arr)[0]->GetName());
176
177 TSQLResult *res = serv.Query(select);
178 if (!res)
179 return 2;
180
181 TSQLRow *row=res->Next();
182 if (!row)
183 {
184 cout << "Couldn't get ZD - Query failed: " << select << endl;
185 cout << "Position of the object is missing in the DB." << endl;
186 return 3;
187 }
188 else
189 {
190 ra =atof((*row)[0]);
191 dec=atof((*row)[1]);
192 v.SetMagThetaPhi(1, TMath::Pi()/2-(dec*TMath::DegToRad()), ra*TMath::DegToRad()*15);
193 v *= MAstroSky2Local(t, obs);
194 zd = v.Theta()*TMath::RadToDeg();
195 }
196 delete res;
197 }
198
199 object = Form("%s/%s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
200 skylevel = (*arr)[2]->GetName();
201 fwhm = (*arr)[3]->GetName();
202 aperturer = (*arr)[4]->GetName();
203 mag = (*arr)[5]->GetName();
204 magerr = (*arr)[6]->GetName();
205 status = (*arr)[7]->GetName();
206 //make sure that no nonsense values enter the db
207 if (skylevel.Contains("-"))
208 skylevel = "NULL";
209 if (fwhm.Contains("-") || !fwhm.IsFloat())
210 fwhm = "NULL";
211 if (!mag.IsFloat())
212 mag = "NULL";
213 if (!magerr.IsFloat())
214 magerr = "NULL";
215 numstars+=1;
216 }
217 delete arr;
218
219 if (numstars!=0)
220 {
221 /*
222 cout << numexp << "th exposure: star # " << numstars << endl;
223 cout << " timestamp: " << timestamp << endl;
224 cout << " exposure : " << exposure << endl;
225 cout << " fitsfile : " << fitsfile << endl;
226 cout << " object : " << object << endl;
227 cout << " skylevel : " << skylevel << endl;
228 cout << " fwhm : " << fwhm << endl;
229 cout << " aperturer: " << aperturer << endl;
230 cout << " mag : " << mag << " +/- " << magerr << endl;
231 cout << " status : " << status << endl << endl;
232 */
233 Int_t statuskey = serv.QueryKeyOfName("Status", status.Data());
234 Int_t objectkey = serv.QueryKeyOfName("Object", object.Data());
235 Int_t fitsfilekey = serv.QueryKeyOfName("FitsFile", fitsfile.Data());
236 Int_t telkey = serv.QueryKeyOfName("Telescope", telname.Data());
237 Int_t ccdkey = serv.QueryKeyOfName("CCD", ccd.Data());
238 Int_t bandkey = serv.QueryKeyOfName("Band", band.Data());
239 Int_t filterkey = serv.QueryKeyOfName("Filter", filter.Data());
240
241 query=Form("fTimeStamp='%s', fExposure=%s, fFitsFileKEY=%d, "
242 "fObjectKEY=%d, fSkyLevel=%s, fFWHM=%s, "
243 "fApertureRadius=%s, fInstrumentalMag=%s, "
244 "fInstrumentalMagErr=%s, fStatusKEY=%d, fCCDKEY=%d, "
245 "fFilterKEY=%d, fTelescopeKEY=%d, fBandKEY=%d, "
246 "fZenithDistance=%.1f ",
247 timestamp.Data(), exposure.Data(), fitsfilekey,
248 objectkey, skylevel.Data(), fwhm.Data(),
249 aperturer.Data(), mag.Data(), magerr.Data(),
250 statuskey, ccdkey, filterkey, telkey, bandkey, zd);
251
252 /*
253 if (ra==0 || dec==0 || zd==0)
254 query+="NULL";
255 else
256 query+=Form("%.1f", zd);
257 */
258 if (serv.Insert("OpticalData", query)==kFALSE)
259 return 2;
260 }
261 }
262
263 cout << fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE))
264 << ": " << setw(2) << numexp << " exposures." << endl << endl;
265
266 return 1;
267}
268
269// --------------------------------------------------------------------------
270//
271// loop over all files in this path
272//
273int filloptical(TString path, Bool_t dummy=kTRUE)
274{
275 MSQLMagic serv("sql.rc");
276 if (!serv.IsConnected())
277 {
278 cout << "ERROR - Connection to database failed." << endl;
279 return 0;
280 }
281
282 serv.SetIsDummy(dummy);
283
284 cout << endl;
285 cout << "filloptical" << endl;
286 cout << "-----------" << endl;
287 cout << endl;
288 cout << "Connected to " << serv.GetName() << endl;
289 cout << "Search Path: " << path << endl;
290 cout << endl;
291
292 //get all runbook files in path
293 if (path.EndsWith(".instr"))
294 return process(serv, path);
295
296 //fill entries for each optical data file
297 MDirIter Next(path, "20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_*_R[_]?[12]?[.]instr", -1);
298 while (1)
299 {
300 TString name = Next();
301 if (name.IsNull())
302 break;
303
304 if (!process(serv, name))
305 return 0;
306 }
307
308 return 1;
309}
Note: See TracBrowser for help on using the repository browser.