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

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

auxilliary GPS

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