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

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