[350] | 1 | // archtoi.cc
|
---|
| 2 | // Eric Aubourg CEA/DAPNIA/SPP juillet 1999
|
---|
| 3 |
|
---|
| 4 |
|
---|
[342] | 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"
|
---|
[394] | 13 | #include "archparam.h"
|
---|
[416] | 14 | #include "archvers.h"
|
---|
[342] | 15 |
|
---|
| 16 | using namespace std;
|
---|
| 17 |
|
---|
[406] | 18 | #ifdef __MWERKS__
|
---|
| 19 | #include "Events.h"
|
---|
| 20 | #include "LowMem.h"
|
---|
| 21 | #include "sioux.h"
|
---|
| 22 | static int macSleepTicks = 0;
|
---|
| 23 | static int macRunTicks = 10;
|
---|
| 24 | static 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
|
---|
[342] | 36 |
|
---|
[406] | 37 |
|
---|
[342] | 38 | ArchTOI::ArchTOI(istream& str)
|
---|
| 39 | {
|
---|
| 40 | init();
|
---|
[407] | 41 | svr.readReq(str);
|
---|
[342] | 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | ArchTOI::ArchTOI(string const& filename)
|
---|
| 46 | {
|
---|
| 47 | init();
|
---|
| 48 | ifstream str(filename.c_str());
|
---|
[407] | 49 | svr.readReq(str);
|
---|
[342] | 50 | }
|
---|
| 51 |
|
---|
| 52 | void ArchTOI::init()
|
---|
| 53 | {
|
---|
[350] | 54 | format = ascii_fmt;
|
---|
[342] | 55 | undef = "#";
|
---|
| 56 | allBolos = false;
|
---|
[352] | 57 | fptr = NULL;
|
---|
| 58 | ostr = NULL;
|
---|
[407] | 59 | requestVersion = "";
|
---|
| 60 | svr.registerReqHandler(this);
|
---|
[342] | 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[407] | 64 | bool ArchTOI::processTOIReq(string line, string toiname, TOIKind /*kind*/, int index,
|
---|
| 65 | bool interp, bool repet, bool flag, bool /*notrig*/)
|
---|
[342] | 66 | {
|
---|
| 67 | headertoi.push_back(line);
|
---|
[352] | 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);
|
---|
[342] | 77 | toiflags.push_back(flg((flag?hasflag:0)+((!repet&&!interp)?useNA:0)));
|
---|
[407] | 78 | return true;
|
---|
[342] | 79 | }
|
---|
| 80 |
|
---|
[407] | 81 | bool ArchTOI::processOption(string key, string arg)
|
---|
[342] | 82 | {
|
---|
[407] | 83 | string line=key; if (arg != "") line = line + " " + arg;
|
---|
| 84 | headeropt.push_back(line);
|
---|
[342] | 85 | if (arg.length()>0 && arg[0] == ' ') {
|
---|
| 86 | arg = arg.substr(arg.find_first_not_of(' '));
|
---|
| 87 | }
|
---|
| 88 | if (key == "#ASCII") {
|
---|
[350] | 89 | format = ascii_fmt;
|
---|
| 90 | } else if (key == "#FITS") {
|
---|
| 91 | format = fits_fmt;
|
---|
[342] | 92 | } else if (key == "#UNDEF") {
|
---|
| 93 | undef=arg;
|
---|
| 94 | } else if (key == "#ALLBOLOS") {
|
---|
| 95 | allBolos=true;
|
---|
[407] | 96 | } else if (key == "#REQVERSION") {
|
---|
[408] | 97 | if (requestVersion != "") {
|
---|
| 98 | cerr << "Error. Duplicate #REQVERSION option" << endl;
|
---|
| 99 | exit(-1);
|
---|
| 100 | }
|
---|
[407] | 101 | requestVersion = arg;
|
---|
| 102 | } else if (key == "#COMMENT") {
|
---|
| 103 | comments.push_back(arg);
|
---|
| 104 | } else {
|
---|
[342] | 105 | return false;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return true;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
[352] | 112 | void ArchTOI::run(string const& outfilename)
|
---|
[342] | 113 | {
|
---|
[352] | 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 | }
|
---|
[342] | 127 |
|
---|
| 128 | cout << "starting query" << endl;
|
---|
[407] | 129 | TOIIter iter = svr.doQuery();
|
---|
[352] | 130 | (this->*openFile)(outfilename);
|
---|
| 131 | (this->*outHeader)(iter);
|
---|
| 132 |
|
---|
[342] | 133 | cout << "processing" << endl;
|
---|
| 134 | while (iter.Next()) {
|
---|
| 135 | int nn = iter.getSampleIndex();
|
---|
[406] | 136 | #ifdef __MWERKS__
|
---|
| 137 | yield();
|
---|
| 138 | #endif
|
---|
[350] | 139 | if (nn%200 == 0) {
|
---|
| 140 | cout << "."; cout.flush();
|
---|
| 141 | }
|
---|
| 142 | if (nn%(200*80) == 0) cout << endl;
|
---|
[342] | 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;
|
---|
[352] | 150 | int icol=0;
|
---|
[342] | 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];
|
---|
[352] | 156 | if (flag & hasflag) {
|
---|
| 157 | (this->*outValue)(icol, (ok && isnew ? 1 : 0));
|
---|
| 158 | icol++;
|
---|
| 159 | }
|
---|
[342] | 160 | if (((flag & useNA)!=0 && !isnew) || !ok)
|
---|
[352] | 161 | (this->*outValue)(icol, 0, true);
|
---|
[342] | 162 | else
|
---|
[352] | 163 | (this->*outValue)(icol, value);
|
---|
| 164 | icol++;
|
---|
[342] | 165 | }
|
---|
[352] | 166 | (this->*endLine)();
|
---|
[342] | 167 | }
|
---|
[352] | 168 | (this->*closeFile)();
|
---|
[342] | 169 | cout << "\nDone." << endl;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[352] | 172 |
|
---|
| 173 | void ArchTOI::openFile_A(string const& filename) {
|
---|
| 174 | ostr = new ofstream(filename.c_str());
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | void 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 |
|
---|
| 206 | void 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 |
|
---|
| 216 | void ArchTOI::endLine_A() {
|
---|
| 217 | if (!ostr) return;
|
---|
| 218 | *ostr << '\n';
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | void ArchTOI::closeFile_A() {
|
---|
| 222 | delete ostr;
|
---|
| 223 | ostr = NULL;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | void 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 |
|
---|
| 236 | void 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)) {
|
---|
[356] | 286 | //j++;
|
---|
[352] | 287 | char line[80];
|
---|
| 288 | strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom);
|
---|
| 289 | char* pline = line;
|
---|
[356] | 290 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
|
---|
[352] | 291 | }
|
---|
| 292 | #else
|
---|
[356] | 293 | //j++;
|
---|
[352] | 294 | char line[80];
|
---|
| 295 | strcpy(line, blk->param.bolo[i].bolo_nom);
|
---|
| 296 | char* pline = line;
|
---|
[356] | 297 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
|
---|
[352] | 298 | #endif
|
---|
| 299 | }
|
---|
[356] | 300 | //fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus);
|
---|
[352] | 301 | }
|
---|
[407] | 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);
|
---|
[354] | 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);
|
---|
[407] | 311 | for (list<string>::iterator i = comments.begin(); i!=comments.end(); i++)
|
---|
| 312 | fits_write_comment(fptr, (char*)((*i).c_str()), &fitsStatus);
|
---|
[354] | 313 |
|
---|
[352] | 314 | fitsLine = 1;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | void 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 |
|
---|
| 325 | void ArchTOI::endLine_F() {
|
---|
| 326 | fitsLine++;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | void ArchTOI::closeFile_F() {
|
---|
| 330 | fits_close_file(fptr, &fitsStatus);
|
---|
| 331 | fits_report_error(stderr, fitsStatus); /* print out any error messages */
|
---|
[400] | 332 | }
|
---|