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

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