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

Last change on this file since 8139 was 8139, checked in by Daniela Dorner, 18 years ago
*** empty log message ***
File size: 7.7 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 Int_t statuskey;
92 Int_t objectkey;
93 Int_t fitsfilekey;
94 Double_t ra=0;
95 Double_t dec=0;
96 Double_t zd=0;
97 MTime t;
98 MObservatory obs(MObservatory::kMagic1);
99 TVector3 v;
100
101 while (1)
102 {
103 TString line;
104 line.ReadLine(fin);
105 if (!fin)
106 break;
107
108 if (line=="KVA_Celestron_ST8 KVA_Celestron_R" || line.IsNull())
109 continue;
110
111 TObjArray *arr = line.Tokenize(" ");
112 if (!line(regexp).IsNull())
113 {
114 if (arr->GetEntries()!=4)
115 {
116 cout << "WARNING: Timestampline with less or more than 4 arguments found " << endl;
117 return 2;
118 }
119 numexp+=1;
120 numstars=0;
121 timestamp =Form("%s %s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
122 exposure = (*arr)[2]->GetName();
123 fitsfile = (*arr)[3]->GetName();
124 t.SetSqlDateTime(timestamp.Data());
125 continue;
126 }
127 else
128 {
129 if (arr->GetEntries()!=8)
130 {
131 cout << "WARNING: Objectline with less or more than 8 arguments found " << endl;
132 return 2;
133 }
134
135 if (numstars==0)
136 {
137 select="SELECT fRightAscension, fDeclination FROM Object WHERE ";
138 select+=Form("fObjectName='%s/BL'", (*arr)[0]->GetName());
139
140 TSQLResult *res = serv.Query(select);
141 if (!res)
142 return 2;
143
144 TSQLRow *row=res->Next();
145 ra=(*row)[0]?atof((*row)[0]):0;
146 dec=(*row)[1]?atof((*row)[1]):0;
147 delete res;
148
149 v.SetMagThetaPhi(1, TMath::Pi()/2-(dec*TMath::DegToRad()), ra*TMath::DegToRad()*15);
150 v *= MAstroSky2Local(t, obs);
151
152 zd = v.Theta()*TMath::RadToDeg();
153
154 }
155
156 object= Form("%s/%s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
157 skylevel = (*arr)[2]->GetName();
158 if (skylevel.Contains("-"))
159 skylevel="NULL";
160 fwhm = (*arr)[3]->GetName();
161 if (fwhm.Contains("-") || !fwhm.IsFloat())
162 fwhm="NULL";
163 aperturer = (*arr)[4]->GetName();
164 mag = (*arr)[5]->GetName();
165 if (!mag.IsFloat())
166 mag="NULL";
167 magerr = (*arr)[6]->GetName();
168 if (!magerr.IsFloat())
169 magerr="NULL";
170 status = (*arr)[7]->GetName();
171 numstars+=1;
172 }
173 delete arr;
174
175 if (numstars!=0)
176 {
177 /*
178 cout << numexp << "th exposure: star # " << numstars << endl;
179 cout << " timestamp: " << timestamp << endl;
180 cout << " exposure : " << exposure << endl;
181 cout << " fitsfile : " << fitsfile << endl;
182 cout << " object : " << object << endl;
183 cout << " skylevel : " << skylevel << endl;
184 cout << " fwhm : " << fwhm << endl;
185 cout << " aperturer: " << aperturer << endl;
186 cout << " mag : " << mag << " +/- " << magerr << endl;
187 cout << " status : " << status << endl << endl;
188 */
189 statuskey = serv.QueryKeyOfName("Status", status.Data());
190 objectkey = serv.QueryKeyOfName("Object", object.Data());
191 fitsfilekey = serv.QueryKeyOfName("FitsFile", fitsfile.Data());
192 query=Form("fTimeStamp='%s', fExposure=%s, fFitsFileKEY=%d, "
193 "fObjectKEY=%d, fSkyLevel=%s, fFWHM=%s, "
194 "fApertureRadius=%s, fInstrumentalMag=%s, "
195 "fInstrumentalMagErr=%s, fStatusKEY=%d, fZenithDistance=",
196 timestamp.Data(), exposure.Data(), fitsfilekey,
197 objectkey, skylevel.Data(), fwhm.Data(),
198 aperturer.Data(), mag.Data(), magerr.Data(),
199 statuskey);
200
201 if (ra==0 || dec==0)
202 query+="NULL";
203 else
204 query+=Form("%.1f", zd);
205
206 if (serv.Insert("OpticalData", query)==kFALSE)
207 return 2;
208
209 }
210
211 }
212
213 cout << fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_KVA_C_R.*[.]instr", kFALSE))
214 << ": " << setw(2) << numexp << " exposures, " << setw(2) << numstars << " stars" << endl;
215
216 return 1;
217}
218
219// --------------------------------------------------------------------------
220//
221// loop over all files in this path
222//
223int filloptical(TString path, Bool_t dummy=kTRUE)
224{
225 TEnv env("sql.rc");
226
227 MSQLMagic serv(env);
228 if (!serv.IsConnected())
229 {
230 cout << "ERROR - Connection to database failed." << endl;
231 return 0;
232 }
233
234 serv.SetIsDummy(dummy);
235
236 cout << endl;
237 cout << "filloptical" << endl;
238 cout << "-----------" << endl;
239 cout << endl;
240 cout << "Connected to " << serv.GetName() << endl;
241 cout << "Search Path: " << path << endl;
242 cout << endl;
243
244 //get all runbook files in path
245 if (path.EndsWith(".instr"))
246 return process(serv, path);
247
248 //fill entries for each runbook file
249 MDirIter Next(path, "2*_KVA_C_R*.instr", -1);
250 while (1)
251 {
252 TString name = Next();
253 if (name.IsNull())
254 break;
255
256 if (!process(serv, name))
257 return 0;
258 }
259
260 return 1;
261}
Note: See TracBrowser for help on using the repository browser.