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

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

gyors

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