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

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