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