| 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 = &ArchTOI::openFile_A;
|
|---|
| 112 | outHeader = &ArchTOI::outHeader_A;
|
|---|
| 113 | outValue = &ArchTOI::outValue_A;
|
|---|
| 114 | endLine = &ArchTOI::endLine_A;
|
|---|
| 115 | closeFile = &ArchTOI::closeFile_A;
|
|---|
| 116 | } else { // fits_fmt
|
|---|
| 117 | openFile = &ArchTOI::openFile_F;
|
|---|
| 118 | outHeader = &ArchTOI::outHeader_F;
|
|---|
| 119 | outValue = &ArchTOI::outValue_F;
|
|---|
| 120 | endLine = &ArchTOI::endLine_F;
|
|---|
| 121 | closeFile = &ArchTOI::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 << endl;
|
|---|
| 217 | // *ostr << '\n';
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | void ArchTOI::closeFile_A() {
|
|---|
| 221 | delete ostr;
|
|---|
| 222 | ostr = NULL;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | list<string> ArchTOI::buildNames() {
|
|---|
| 226 | list<string> toinames;
|
|---|
| 227 | map<string,int> namecount;
|
|---|
| 228 |
|
|---|
| 229 | for (list<TOI>::iterator i = tois.begin(); i != tois.end(); i++) {
|
|---|
| 230 | namecount[(*i).name]++;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | map<string,int> curcount;
|
|---|
| 234 | for (list<TOI>::iterator i = tois.begin(); i != tois.end(); i++) {
|
|---|
| 235 | string toiname = (*i).name;
|
|---|
| 236 | if (namecount[(*i).name]>1) {
|
|---|
| 237 | char s[10];
|
|---|
| 238 | sprintf(s,"_%d",curcount[(*i).name]++);
|
|---|
| 239 | toiname += s;
|
|---|
| 240 | }
|
|---|
| 241 | if ((*i).index != TOI::unspec) {
|
|---|
| 242 | char s[10];
|
|---|
| 243 | sprintf(s,"_%d",(*i).index);
|
|---|
| 244 | toiname += s;
|
|---|
| 245 | }
|
|---|
| 246 | toinames.push_back(toiname);
|
|---|
| 247 | }
|
|---|
| 248 | return toinames;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | void ArchTOI::openFile_F(string const& filename) {
|
|---|
| 252 | fitsStatus=0;
|
|---|
| 253 | remove(filename.c_str());
|
|---|
| 254 | if (fits_create_file(&fptr, filename.c_str(), &fitsStatus)) {
|
|---|
| 255 | fits_report_error(stderr, fitsStatus);
|
|---|
| 256 | exit(-1);
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | void ArchTOI::outHeader_F(TOIIter& iter) {
|
|---|
| 262 | list<string> toinames = buildNames();
|
|---|
| 263 |
|
|---|
| 264 | int ncols=toinames.size();
|
|---|
| 265 |
|
|---|
| 266 | char** colnames = new (char*[ncols]);
|
|---|
| 267 | char** coltypes = new (char*[ncols]);
|
|---|
| 268 | char** colunits = new (char*[ncols]);
|
|---|
| 269 | int j=0;
|
|---|
| 270 | list<TOI>::iterator kk = tois.begin();
|
|---|
| 271 | for (list<string>::iterator i = toinames.begin(); i != toinames.end(); i++,j++,kk++) {
|
|---|
| 272 | colnames[j] = const_cast<char*>((*i).c_str()); // should work for most STL implementations... Check...
|
|---|
| 273 | coltypes[j] = "1D";
|
|---|
| 274 | colunits[j] = const_cast<char*>((*kk).unit.c_str());
|
|---|
| 275 | cout << (*i).c_str() << " " << ((*kk).unit.c_str()) << endl;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | fits_create_tbl(fptr, BINARY_TBL, 0, ncols, colnames, coltypes, colunits, NULL, &fitsStatus);
|
|---|
| 279 | fits_write_date(fptr, &fitsStatus);
|
|---|
| 280 |
|
|---|
| 281 | delete[] colunits;
|
|---|
| 282 | delete[] coltypes;
|
|---|
| 283 | delete[] colnames;
|
|---|
| 284 |
|
|---|
| 285 | j=1;
|
|---|
| 286 | // Rappel dans le header des requetes...
|
|---|
| 287 | int ntoireq = headertoi.size();
|
|---|
| 288 | fits_write_key(fptr, TINT, "TOIREQ", &ntoireq, NULL, &fitsStatus);
|
|---|
| 289 | list<string>::iterator kkk = toinames.begin();
|
|---|
| 290 | for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++,j++,kkk++) {
|
|---|
| 291 | char line[80];
|
|---|
| 292 | strcpy(line, (*i).c_str());
|
|---|
| 293 | char* pline = line;
|
|---|
| 294 | char* cmt = const_cast<char*>((*kkk).c_str());
|
|---|
| 295 | fits_write_keys_str(fptr, "TOIREQ", j, 1, &pline, &cmt, &fitsStatus);
|
|---|
| 296 | }
|
|---|
| 297 | j=1;
|
|---|
| 298 | int noptreq = headeropt.size();
|
|---|
| 299 | fits_write_key(fptr, TINT, "OPTREQ", &noptreq, NULL, &fitsStatus);
|
|---|
| 300 | for (list<string>::iterator i = headeropt.begin(); i != headeropt.end(); i++,j++) {
|
|---|
| 301 | char line[80];
|
|---|
| 302 | strcpy(line, (*i).c_str());
|
|---|
| 303 | char* pline = line;
|
|---|
| 304 | fits_write_keys_str(fptr, "OPTREQ", j, 1, &pline, (char**) NULL, &fitsStatus);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | // Noms des bolos
|
|---|
| 308 | block_type_param* blk = iter.getFSet().lastParam();
|
|---|
| 309 | if (blk) {
|
|---|
| 310 | int nb = blk->param.n_max_bolo;
|
|---|
| 311 | j=0;
|
|---|
| 312 | for (int i=0; i<nb; i++) {
|
|---|
| 313 | #if version_num > 25
|
|---|
| 314 | if (allBolos ||
|
|---|
| 315 | (blk->param.bolo[i].bolo_code_util != bolo_hors_service &&
|
|---|
| 316 | blk->param.bolo[i].bolo_code_util != bolo_normal_non_transmis)) {
|
|---|
| 317 | //j++;
|
|---|
| 318 | char line[80];
|
|---|
| 319 | strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom);
|
|---|
| 320 | char* pline = line;
|
|---|
| 321 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
|
|---|
| 322 | }
|
|---|
| 323 | #else
|
|---|
| 324 | //j++;
|
|---|
| 325 | char line[80];
|
|---|
| 326 | strcpy(line, blk->param.bolo[i].bolo_nom);
|
|---|
| 327 | char* pline = line;
|
|---|
| 328 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus);
|
|---|
| 329 | #endif
|
|---|
| 330 | }
|
|---|
| 331 | //fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus);
|
|---|
| 332 | }
|
|---|
| 333 | fits_write_key_str(fptr, "TOIVERS", ARCHTOI_VERS, "Archtoi version",&fitsStatus);
|
|---|
| 334 | fits_write_key_str(fptr, "TOITAG", ARCHTOI_TAG, "Archtoi cvs tag",&fitsStatus);
|
|---|
| 335 | if (requestVersion != "")
|
|---|
| 336 | fits_write_key_str(fptr, "REQVERS", (char*) requestVersion.c_str(),
|
|---|
| 337 | "Request options version",&fitsStatus);
|
|---|
| 338 | fits_write_comment(fptr, "Generated with archtoi " ARCHTOI_VERS, &fitsStatus);
|
|---|
| 339 | char line[80];
|
|---|
| 340 | sprintf(line, "using archeops.h %d", version_num);
|
|---|
| 341 | fits_write_comment(fptr, line, &fitsStatus);
|
|---|
| 342 | for (list<string>::iterator i = comments.begin(); i!=comments.end(); i++)
|
|---|
| 343 | fits_write_comment(fptr, (char*)((*i).c_str()), &fitsStatus);
|
|---|
| 344 |
|
|---|
| 345 | fitsLine = 1;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | void ArchTOI::outValue_F(int icolumn, double value, bool notdef) {
|
|---|
| 349 | if (notdef && undef != "#") {
|
|---|
| 350 | notdef = false;
|
|---|
| 351 | value = undefV;
|
|---|
| 352 | }
|
|---|
| 353 | if (notdef) {
|
|---|
| 354 | fits_write_col_null(fptr, icolumn+1, fitsLine, 1, 1, &fitsStatus);
|
|---|
| 355 | } else {
|
|---|
| 356 | fits_write_col_dbl(fptr, icolumn+1, fitsLine, 1, 1, &value, &fitsStatus);
|
|---|
| 357 | }
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | void ArchTOI::endLine_F() {
|
|---|
| 361 | fitsLine++;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | void ArchTOI::closeFile_F() {
|
|---|
| 365 | fits_close_file(fptr, &fitsStatus);
|
|---|
| 366 | fits_report_error(stderr, fitsStatus); /* print out any error messages */
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|