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

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

findstar DY qui sort des etoiles

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