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

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

added support for FITS output

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