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

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

nouvelles toi

File size: 8.9 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
15using namespace std;
16
17#ifdef __MWERKS__
18#include "Events.h"
19#include "LowMem.h"
20#include "sioux.h"
21static int macSleepTicks = 0;
22static int macRunTicks = 10;
23static void yield()
24{
25 EventRecord theEvent;
26 static long int macLastTicks;
27 long int ticks = LMGetTicks();
28 if (ticks - macLastTicks >= macRunTicks) {
29 if (WaitNextEvent(everyEvent, &theEvent,macSleepTicks,0))
30 SIOUXHandleOneEvent(&theEvent);
31 macLastTicks = LMGetTicks();
32 }
33}
34#endif
35
36
37ArchTOI::ArchTOI(istream& str)
38{
39 init();
40 svr.readReq(str);
41}
42
43
44ArchTOI::ArchTOI(string const& filename)
45{
46 init();
47 ifstream str(filename.c_str());
48 svr.readReq(str);
49}
50
51void ArchTOI::init()
52{
53 format = ascii_fmt;
54 undef = "#";
55 allBolos = false;
56 fptr = NULL;
57 ostr = NULL;
58 requestVersion = "";
59 svr.registerReqHandler(this);
60}
61
62
63bool ArchTOI::processTOIReq(string line, string toiname, TOIKind /*kind*/, int index,
64 bool interp, bool repet, bool flag, bool /*notrig*/)
65{
66 headertoi.push_back(line);
67 if (index>=0) {
68 char idx[10];
69 sprintf(idx,"_%d",index);
70 toiname += idx;
71 }
72 if (flag) {
73 toinames.push_back("flg_"+toiname);
74 }
75 toinames.push_back(toiname);
76 toiflags.push_back(flg((flag?hasflag:0)+((!repet&&!interp)?useNA:0)));
77 return true;
78}
79
80bool ArchTOI::processOption(string key, string arg)
81{
82 string line=key; if (arg != "") line = line + " " + arg;
83 headeropt.push_back(line);
84 if (arg.length()>0 && arg[0] == ' ') {
85 arg = arg.substr(arg.find_first_not_of(' '));
86 }
87 if (key == "#ASCII") {
88 format = ascii_fmt;
89 } else if (key == "#FITS") {
90 format = fits_fmt;
91 } else if (key == "#UNDEF") {
92 undef=arg;
93 } else if (key == "#ALLBOLOS") {
94 allBolos=true;
95 } else if (key == "#REQVERSION") {
96 if (requestVersion != "") {
97 cerr << "Error. Duplicate #REQVERSION option" << endl;
98 exit(-1);
99 }
100 requestVersion = arg;
101 } else if (key == "#COMMENT") {
102 comments.push_back(arg);
103 } else {
104 return false;
105 }
106
107 return true;
108}
109
110
111void ArchTOI::run(string const& outfilename)
112{
113 if (format == ascii_fmt) {
114 openFile = &openFile_A;
115 outHeader = &outHeader_A;
116 outValue = &outValue_A;
117 endLine = &endLine_A;
118 closeFile = &closeFile_A;
119 } else { // fits_fmt
120 openFile = &openFile_F;
121 outHeader = &outHeader_F;
122 outValue = &outValue_F;
123 endLine = &endLine_F;
124 closeFile = &closeFile_F;
125 }
126
127 cout << "starting query" << endl;
128 TOIIter iter = svr.doQuery();
129 (this->*openFile)(outfilename);
130 (this->*outHeader)(iter);
131
132 cout << "processing" << endl;
133 while (iter.Next()) {
134 int nn = iter.getSampleIndex();
135 #ifdef __MWERKS__
136 yield();
137 #endif
138 if (nn%200 == 0) {
139 cout << "."; cout.flush();
140 }
141 if (nn%(200*80) == 0) cout << endl;
142 // Si rien de dispo parmi les triggering, alors on passe au suivant
143 bool hasValue = false;
144 for (int i=0; i<toiflags.size(); i++) {
145 if (!iter.isTrig(i)) continue;
146 if (iter.canGetValue(i)) {hasValue=true; break;}
147 }
148 if (!hasValue) continue;
149 int icol=0;
150 for (int i=0; i<toiflags.size(); i++) {
151 double value = iter.getValue(i);
152 bool ok = iter.canGetValue(i);
153 bool isnew = iter.newValue(i);
154 flg flag = toiflags[i];
155 if (flag & hasflag) {
156 (this->*outValue)(icol, (ok && isnew ? 1 : 0));
157 icol++;
158 }
159 if (((flag & useNA)!=0 && !isnew) || !ok)
160 (this->*outValue)(icol, 0, true);
161 else
162 (this->*outValue)(icol, value);
163 icol++;
164 }
165 (this->*endLine)();
166 }
167 (this->*closeFile)();
168 cout << "\nDone." << endl;
169}
170
171
172void ArchTOI::openFile_A(string const& filename) {
173 ostr = new ofstream(filename.c_str());
174}
175
176void ArchTOI::outHeader_A(TOIIter& iter) {
177 if (!ostr) return;
178 for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++)
179 *ostr << (*i) << '\n';
180 for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++)
181 *ostr << (*i) << '\n';;
182 block_type_param* blk = iter.lastParam();
183 if (blk) {
184 int nb = blk->param.nb_bolo;
185 for (int i=0; i<nb; i++) {
186#if version_num > 25
187 if (allBolos) {
188 *ostr << "$BOLO " << i << " "
189 << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << " "
190 << blk->param.bolo[i].bolo_code_util << '\n';
191 } else if (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
192 blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis) {
193 *ostr << "$BOLO " << i << " "
194 << blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom << "\n";
195 }
196#else
197 *ostr << "$BOLO " << i << " " <<
198 blk->param.bolo[i].bolo_nom << '\n';
199#endif
200 }
201 }
202 *ostr << "#END" << endl;
203}
204
205void ArchTOI::outValue_A(int icolumn, double value, bool notdef) {
206 if (!ostr) return;
207 if (icolumn > 0) *ostr << '\t';
208 if (notdef) {
209 *ostr << undef ;
210 } else {
211 *ostr << setprecision(11) << value ;
212 }
213}
214
215void ArchTOI::endLine_A() {
216 if (!ostr) return;
217 *ostr << '\n';
218}
219
220void ArchTOI::closeFile_A() {
221 delete ostr;
222 ostr = NULL;
223}
224
225
226void ArchTOI::openFile_F(string const& filename) {
227 fitsStatus=0;
228 remove(filename.c_str());
229 if (fits_create_file(&fptr, filename.c_str(), &fitsStatus)) {
230 fits_report_error(stderr, fitsStatus);
231 exit(-1);
232 }
233}
234
235void ArchTOI::outHeader_F(TOIIter& iter) {
236 int ncols=toinames.size();
237
238 char** colnames = new (char*[ncols]);
239 char** coltypes = new (char*[ncols]);
240 char** colunits = new (char*[ncols]);
241 int j=0;
242 for (list<string>::iterator i = toinames.begin(); i != toinames.end(); i++,j++) {
243 colnames[j] = const_cast<char*>((*i).c_str()); // should work for most STL implementations... Check...
244 coltypes[j] = "1D";
245 colunits[j] = " ";
246 }
247
248 fits_create_tbl(fptr, BINARY_TBL, 0, ncols, colnames, coltypes, colunits, NULL, &fitsStatus);
249 fits_write_date(fptr, &fitsStatus);
250
251 delete[] colunits;
252 delete[] coltypes;
253 delete[] colnames;
254
255 j=1;
256 // Rappel dans le header des requetes...
257 int ntoireq = headertoi.size();
258 fits_write_key(fptr, TINT, "TOIREQ", &ntoireq, NULL, &fitsStatus);
259 for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++,j++) {
260 char line[80];
261 strcpy(line, (*i).c_str());
262 char* pline = line;
263 fits_write_keys_str(fptr, "TOIREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
264 }
265 j=1;
266 int noptreq = headeropt.size();
267 fits_write_key(fptr, TINT, "OPTREQ", &noptreq, NULL, &fitsStatus);
268 for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++,j++) {
269 char line[80];
270 strcpy(line, (*i).c_str());
271 char* pline = line;
272 fits_write_keys_str(fptr, "OPTREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
273 }
274
275 // Noms des bolos
276 block_type_param* blk = iter.lastParam();
277 if (blk) {
278 int nb = blk->param.nb_bolo;
279 j=0;
280 for (int i=0; i<nb; i++) {
281#if version_num > 25
282 if (allBolos ||
283 (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
284 blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis)) {
285 //j++;
286 char line[80];
287 strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom);
288 char* pline = line;
289 fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
290 }
291#else
292 //j++;
293 char line[80];
294 strcpy(line, blk->param.bolo[i].bolo_nom);
295 char* pline = line;
296 fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
297#endif
298 }
299 //fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus);
300 }
301 fits_write_key_str(fptr, "TOIVERS", ARCHTOI_VERS, "Archtoi version",&fitsStatus);
302 fits_write_key_str(fptr, "TOITAG", ARCHTOI_TAG, "Archtoi cvs tag",&fitsStatus);
303 if (requestVersion != "")
304 fits_write_key_str(fptr, "REQVERS", (char*) requestVersion.c_str(),
305 "Request options version",&fitsStatus);
306 fits_write_comment(fptr, "Generated with archtoi " ARCHTOI_VERS, &fitsStatus);
307 char line[80];
308 sprintf(line, "using archeops.h %d", version_num);
309 fits_write_comment(fptr, line, &fitsStatus);
310 for (list<string>::iterator i = comments.begin(); i!=comments.end(); i++)
311 fits_write_comment(fptr, (char*)((*i).c_str()), &fitsStatus);
312
313 fitsLine = 1;
314}
315
316void ArchTOI::outValue_F(int icolumn, double value, bool notdef) {
317 if (notdef) {
318 fits_write_col_null(fptr, icolumn+1, fitsLine, 1, 1, &fitsStatus);
319 } else {
320 fits_write_col_dbl(fptr, icolumn+1, fitsLine, 1, 1, &value, &fitsStatus);
321 }
322}
323
324void ArchTOI::endLine_F() {
325 fitsLine++;
326}
327
328void ArchTOI::closeFile_F() {
329 fits_close_file(fptr, &fitsStatus);
330 fits_report_error(stderr, fitsStatus); /* print out any error messages */
331}
Note: See TracBrowser for help on using the repository browser.