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

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

vendredi soir...

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