source: Sophya/trunk/Poubelle/archTOI.old/archtoi.cc@ 396

Last change on this file since 396 was 394, checked in by ansari, 26 years ago

Integration detecteur d'etoiles DY

File size: 10.1 KB
Line 
1// archtoi.cc
2// Eric Aubourg CEA/DAPNIA/SPP juillet 1999
3
4
5#include <string>
6#include <iostream.h>
7#include <fstream.h>
8#include <iomanip.h>
9
10#include "archeopsfile.h"
11#include "toisvr.h"
12#include "archtoi.h"
13#include "archparam.h"
14#include "asigps.h"
15
16using namespace std;
17
18
19ArchTOI::ArchTOI(istream& str)
20{
21 init();
22 readReq(str);
23}
24
25
26ArchTOI::ArchTOI(string const& filename)
27{
28 init();
29 ifstream str(filename.c_str());
30 readReq(str);
31}
32
33void ArchTOI::init()
34{
35 format = ascii_fmt;
36 undef = "#";
37 allBolos = false;
38 fptr = NULL;
39 ostr = NULL;
40}
41
42void ArchTOI::readReq(istream& str)
43{
44 string line;
45 while (str) {
46 getline(str,line);
47 if (!str) break;
48 if (line[0] == '@') processTOIReq(line);
49 else if (line[0] == '#')
50 if (!processOption(line)) break;
51 }
52}
53
54#define tsttoi(toi) if (key == "@"#toi) kind = toi;
55
56void ArchTOI::processTOIReq(string line)
57{
58 // find TOI kind, index and options
59 TOIKind kind= (TOIKind)-1;
60 int index=-1;
61 bool interp=false;
62 bool repet =false;
63 bool flag =false;
64 bool notrig=false;
65 int x = line.find(' ');
66 string key = line.substr(0, x);
67 string opts = (x>0) ? line.substr(x) : string("");
68 tsttoi(sampleNum)
69 else tsttoi(internalTime)
70 else tsttoi(mjd)
71 else tsttoi(boloTens)
72 else tsttoi(boloRaw)
73 else tsttoi(sstDiode)
74 else tsttoi(sstChannel)
75 else tsttoi(sstStarZ)
76 else tsttoi(sstStarF)
77 else tsttoi(sstStarT)
78 else tsttoi(gyroRaw)
79 else tsttoi(gpsTime)
80 else tsttoi(longitude)
81 else tsttoi(latitude)
82 else tsttoi(altitude)
83 else tsttoi(tsid)
84 else tsttoi(azimut)
85 else tsttoi(alphaAxis)
86 else tsttoi(deltaAxis)
87 else tsttoi(alphaBolo)
88 else tsttoi(deltaBolo)
89 else {
90 cerr << "*Warning, unrecognized TOI " << line << endl;
91 return;
92 }
93 if (kind == sampleNum) notrig = true;
94 while (opts != "") {
95 if (opts[0] == ' ') {
96 opts = opts.substr(opts.find_first_not_of(' '));
97 if (opts == "") break;
98 }
99 x = opts.find(' ');
100 string opt = opts.substr(0, x);
101 opts = (x>0) ? opts.substr(x) : string("");
102 if (opt[0]>='0' && opt[0]<='9') {
103 index = atoi(opt.c_str());
104 } else if (opt == "notrig") {
105 notrig = true;
106 } else if (opt == "repet") {
107 repet = true; interp = false;
108 } else if (opt == "interp") {
109 interp = true; repet = false;
110 } else if (opt == "flag") {
111 flag = true;
112 }
113 }
114 headertoi.push_back(line);
115 string toiname = key.substr(1);
116 if (index>=0) {
117 char idx[10];
118 sprintf(idx,"_%d",index);
119 toiname += idx;
120 }
121 if (flag) {
122 toinames.push_back("flg_"+toiname);
123 }
124 toinames.push_back(toiname);
125 toiflags.push_back(flg((flag?hasflag:0)+((!repet&&!interp)?useNA:0)));
126 if (index<0) index=0;
127 svr.AddInfo(kind, index, !notrig, interp);
128}
129
130bool ArchTOI::processOption(string line)
131{
132 int x = line.find(' ');
133 string key = line.substr(0, x);
134 string arg = (x>0) ? line.substr(x) : string("");
135 if (arg.length()>0 && arg[0] == ' ') {
136 arg = arg.substr(arg.find_first_not_of(' '));
137 }
138 if (key == "#ASCII") {
139 format = ascii_fmt;
140 } else if (key == "#FITS") {
141 format = fits_fmt;
142 } else if (key == "#TRANGE") {
143 double tmin, tmax;
144 sscanf(arg.c_str(), "%lg %lg", &tmin, &tmax);
145 svr.SetTimeInterval(tmin, tmax);
146 } else if (key == "#PATH") {
147 svr.SetDirectory(arg);
148 } else if (key == "#FILE") {
149 svr.AddFile(arg);
150 } else if (key == "#UNDEF") {
151 undef=arg;
152 } else if (key == "#ALLBOLOS") {
153 allBolos=true;
154 } else if (key == "#RECORDER") {
155 svr.OnBoardRecorderFiles(true);
156 } else if (key == "#MJD0") {
157 double t0;
158 sscanf(arg.c_str(), "%lg", &t0);
159 archParam.acq.tBlock0 = t0;
160 } else if (key == "#PERECH") {
161 double t0;
162 sscanf(arg.c_str(), "%lg", &t0);
163 archParam.acq.perEch = t0;
164 } else if (key == "#ASIGPS") {
165 ASIGPS* gps = new ASIGPS(arg);
166 gps->FitsDump("GPSDump.fits");
167 svr.UseAuxGPS(gps);
168 } else if (key == "#END") {
169 return false;
170 } else {
171 cerr << "*Warning, unrecognized option " << line << endl;
172 return true;
173 }
174
175 headeropt.push_back(line);
176 return true;
177}
178
179
180void ArchTOI::run(string const& outfilename)
181{
182 if (format == ascii_fmt) {
183 openFile = &openFile_A;
184 outHeader = &outHeader_A;
185 outValue = &outValue_A;
186 endLine = &endLine_A;
187 closeFile = &closeFile_A;
188 } else { // fits_fmt
189 openFile = &openFile_F;
190 outHeader = &outHeader_F;
191 outValue = &outValue_F;
192 endLine = &endLine_F;
193 closeFile = &closeFile_F;
194 }
195
196 cout << "starting query" << endl;
197 TOIIter iter = svr.DoQuery();
198 (this->*openFile)(outfilename);
199 (this->*outHeader)(iter);
200
201 cout << "processing" << endl;
202 while (iter.Next()) {
203 int nn = iter.getSampleIndex();
204 if (nn%200 == 0) {
205 cout << "."; cout.flush();
206 }
207 if (nn%(200*80) == 0) cout << endl;
208 // Si rien de dispo parmi les triggering, alors on passe au suivant
209 bool hasValue = false;
210 for (int i=0; i<toiflags.size(); i++) {
211 if (!iter.isTrig(i)) continue;
212 if (iter.canGetValue(i)) {hasValue=true; break;}
213 }
214 if (!hasValue) continue;
215 int icol=0;
216 for (int i=0; i<toiflags.size(); i++) {
217 double value = iter.getValue(i);
218 bool ok = iter.canGetValue(i);
219 bool isnew = iter.newValue(i);
220 flg flag = toiflags[i];
221 if (flag & hasflag) {
222 (this->*outValue)(icol, (ok && isnew ? 1 : 0));
223 icol++;
224 }
225 if (((flag & useNA)!=0 && !isnew) || !ok)
226 (this->*outValue)(icol, 0, true);
227 else
228 (this->*outValue)(icol, value);
229 icol++;
230 }
231 (this->*endLine)();
232 }
233 (this->*closeFile)();
234 cout << "\nDone." << endl;
235}
236
237
238void ArchTOI::openFile_A(string const& filename) {
239 ostr = new ofstream(filename.c_str());
240}
241
242void ArchTOI::outHeader_A(TOIIter& iter) {
243 if (!ostr) return;
244 for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++)
245 *ostr << (*i) << '\n';
246 for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++)
247 *ostr << (*i) << '\n';;
248 block_type_param* blk = iter.lastParam();
249 if (blk) {
250 int nb = blk->param.nb_bolo;
251 for (int i=0; i<nb; i++) {
252#if version_num > 25
253 if (allBolos) {
254 *ostr << "$BOLO " << i << " "
255 << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << " "
256 << blk->param.bolo[i].bolo_code_util << '\n';
257 } else if (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
258 blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis) {
259 *ostr << "$BOLO " << i << " "
260 << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << "\n";
261 }
262#else
263 *ostr << "$BOLO " << i << " " <<
264 blk->param.bolo[i].bolo_nom << '\n';
265#endif
266 }
267 }
268 *ostr << "#END" << endl;
269}
270
271void ArchTOI::outValue_A(int icolumn, double value, bool notdef) {
272 if (!ostr) return;
273 if (icolumn > 0) *ostr << '\t';
274 if (notdef) {
275 *ostr << undef ;
276 } else {
277 *ostr << setprecision(11) << value ;
278 }
279}
280
281void ArchTOI::endLine_A() {
282 if (!ostr) return;
283 *ostr << '\n';
284}
285
286void ArchTOI::closeFile_A() {
287 delete ostr;
288 ostr = NULL;
289}
290
291
292void ArchTOI::openFile_F(string const& filename) {
293 fitsStatus=0;
294 remove(filename.c_str());
295 if (fits_create_file(&fptr, filename.c_str(), &fitsStatus)) {
296 fits_report_error(stderr, fitsStatus);
297 exit(-1);
298 }
299}
300
301void ArchTOI::outHeader_F(TOIIter& iter) {
302 int ncols=toinames.size();
303
304 char** colnames = new (char*[ncols]);
305 char** coltypes = new (char*[ncols]);
306 char** colunits = new (char*[ncols]);
307 int j=0;
308 for (list<string>::iterator i = toinames.begin(); i != toinames.end(); i++,j++) {
309 colnames[j] = const_cast<char*>((*i).c_str()); // should work for most STL implementations... Check...
310 coltypes[j] = "1D";
311 colunits[j] = " ";
312 }
313
314 fits_create_tbl(fptr, BINARY_TBL, 0, ncols, colnames, coltypes, colunits, NULL, &fitsStatus);
315 fits_write_date(fptr, &fitsStatus);
316
317 delete[] colunits;
318 delete[] coltypes;
319 delete[] colnames;
320
321 j=1;
322 // Rappel dans le header des requetes...
323 int ntoireq = headertoi.size();
324 fits_write_key(fptr, TINT, "TOIREQ", &ntoireq, NULL, &fitsStatus);
325 for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++,j++) {
326 char line[80];
327 strcpy(line, (*i).c_str());
328 char* pline = line;
329 fits_write_keys_str(fptr, "TOIREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
330 }
331 j=1;
332 int noptreq = headeropt.size();
333 fits_write_key(fptr, TINT, "OPTREQ", &noptreq, NULL, &fitsStatus);
334 for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++,j++) {
335 char line[80];
336 strcpy(line, (*i).c_str());
337 char* pline = line;
338 fits_write_keys_str(fptr, "OPTREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
339 }
340
341 // Noms des bolos
342 block_type_param* blk = iter.lastParam();
343 if (blk) {
344 int nb = blk->param.nb_bolo;
345 j=0;
346 for (int i=0; i<nb; i++) {
347#if version_num > 25
348 if (allBolos ||
349 (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
350 blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis)) {
351 //j++;
352 char line[80];
353 strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom);
354 char* pline = line;
355 fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
356 }
357#else
358 //j++;
359 char line[80];
360 strcpy(line, blk->param.bolo[i].bolo_nom);
361 char* pline = line;
362 fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
363#endif
364 }
365 //fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus);
366 }
367 fits_write_comment(fptr, "Generated with archtoi " ARCHTOI_VERS, &fitsStatus);
368 char line[80];
369 sprintf(line, "using archeops.h %d", version_num);
370 fits_write_comment(fptr, line, &fitsStatus);
371
372 fitsLine = 1;
373}
374
375void ArchTOI::outValue_F(int icolumn, double value, bool notdef) {
376 if (notdef) {
377 fits_write_col_null(fptr, icolumn+1, fitsLine, 1, 1, &fitsStatus);
378 } else {
379 fits_write_col_dbl(fptr, icolumn+1, fitsLine, 1, 1, &value, &fitsStatus);
380 }
381}
382
383void ArchTOI::endLine_F() {
384 fitsLine++;
385}
386
387void ArchTOI::closeFile_F() {
388 fits_close_file(fptr, &fitsStatus);
389 fits_report_error(stderr, fitsStatus); /* print out any error messages */
390}
Note: See TracBrowser for help on using the repository browser.