| [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" | 
|---|
| [534] | 11 | #include "toiiter.h" | 
|---|
|  | 12 | #include "toimanager.h" | 
|---|
| [342] | 13 | #include "archtoi.h" | 
|---|
| [394] | 14 | #include "archparam.h" | 
|---|
| [416] | 15 | #include "archvers.h" | 
|---|
| [342] | 16 |  | 
|---|
|  | 17 | using namespace std; | 
|---|
|  | 18 |  | 
|---|
| [406] | 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 | 
|---|
| [342] | 37 |  | 
|---|
| [406] | 38 |  | 
|---|
| [342] | 39 | ArchTOI::ArchTOI(istream& str) | 
|---|
|  | 40 | { | 
|---|
|  | 41 | init(); | 
|---|
| [534] | 42 | iter.readReq(str); | 
|---|
| [342] | 43 | } | 
|---|
|  | 44 |  | 
|---|
|  | 45 |  | 
|---|
|  | 46 | ArchTOI::ArchTOI(string const& filename) | 
|---|
|  | 47 | { | 
|---|
|  | 48 | init(); | 
|---|
|  | 49 | ifstream str(filename.c_str()); | 
|---|
| [534] | 50 | iter.readReq(str); | 
|---|
| [342] | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | void ArchTOI::init() | 
|---|
|  | 54 | { | 
|---|
| [350] | 55 | format = ascii_fmt; | 
|---|
| [342] | 56 | undef  = "#"; | 
|---|
| [432] | 57 | undefV = -99999; | 
|---|
| [342] | 58 | allBolos = false; | 
|---|
| [352] | 59 | fptr = NULL; | 
|---|
|  | 60 | ostr = NULL; | 
|---|
| [407] | 61 | requestVersion = ""; | 
|---|
| [534] | 62 | iter.registerReqHandler(this); | 
|---|
|  | 63 |  | 
|---|
|  | 64 | TOIManager::registerDefaultProducers(); | 
|---|
| [342] | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 |  | 
|---|
| [534] | 68 | bool ArchTOI::processTOIReq(TOI const& toi, string line) | 
|---|
| [342] | 69 | { | 
|---|
|  | 70 | headertoi.push_back(line); | 
|---|
| [534] | 71 | tois.push_back(toi); | 
|---|
| [407] | 72 | return true; | 
|---|
| [342] | 73 | } | 
|---|
|  | 74 |  | 
|---|
| [407] | 75 | bool ArchTOI::processOption(string key, string arg) | 
|---|
| [342] | 76 | { | 
|---|
| [407] | 77 | string line=key; if (arg != "") line = line + " " + arg; | 
|---|
|  | 78 | headeropt.push_back(line); | 
|---|
| [342] | 79 | if (arg.length()>0 && arg[0] == ' ') { | 
|---|
|  | 80 | arg = arg.substr(arg.find_first_not_of(' ')); | 
|---|
|  | 81 | } | 
|---|
|  | 82 | if (key == "#ASCII") { | 
|---|
| [350] | 83 | format = ascii_fmt; | 
|---|
|  | 84 | } else if (key == "#FITS") { | 
|---|
|  | 85 | format = fits_fmt; | 
|---|
| [342] | 86 | } else if (key == "#UNDEF") { | 
|---|
|  | 87 | undef=arg; | 
|---|
| [432] | 88 | if (undef[0] >= '0' && undef[0] <= '9') | 
|---|
|  | 89 | undefV = atoi(undef.c_str()); | 
|---|
| [342] | 90 | } else if (key == "#ALLBOLOS") { | 
|---|
|  | 91 | allBolos=true; | 
|---|
| [407] | 92 | } else if (key == "#REQVERSION") { | 
|---|
| [408] | 93 | if (requestVersion != "") { | 
|---|
|  | 94 | cerr << "Error. Duplicate #REQVERSION option" << endl; | 
|---|
|  | 95 | exit(-1); | 
|---|
|  | 96 | } | 
|---|
| [407] | 97 | requestVersion = arg; | 
|---|
|  | 98 | } else if (key == "#COMMENT") { | 
|---|
|  | 99 | comments.push_back(arg); | 
|---|
|  | 100 | } else { | 
|---|
| [342] | 101 | return false; | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | return true; | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
|  | 107 |  | 
|---|
| [352] | 108 | void ArchTOI::run(string const& outfilename) | 
|---|
| [342] | 109 | { | 
|---|
| [352] | 110 | if (format == ascii_fmt) { | 
|---|
| [634] | 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; | 
|---|
| [352] | 116 | } else { // fits_fmt | 
|---|
| [634] | 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; | 
|---|
| [352] | 122 | } | 
|---|
| [342] | 123 |  | 
|---|
|  | 124 | cout << "starting query" << endl; | 
|---|
| [534] | 125 | iter.init(); | 
|---|
| [352] | 126 | (this->*openFile)(outfilename); | 
|---|
|  | 127 | (this->*outHeader)(iter); | 
|---|
|  | 128 |  | 
|---|
| [342] | 129 | cout << "processing" << endl; | 
|---|
| [534] | 130 | while (iter.next()) { | 
|---|
|  | 131 | int nn = iter.getSampleNum() / iter.getUnderSample(); // Only for tick mark... | 
|---|
| [406] | 132 | #ifdef __MWERKS__ | 
|---|
|  | 133 | yield(); | 
|---|
|  | 134 | #endif | 
|---|
| [350] | 135 | if (nn%200 == 0) { | 
|---|
|  | 136 | cout << "."; cout.flush(); | 
|---|
|  | 137 | } | 
|---|
|  | 138 | if (nn%(200*80) == 0) cout << endl; | 
|---|
| [342] | 139 | // Si rien de dispo parmi les triggering, alors on passe au suivant | 
|---|
| [534] | 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; | 
|---|
| [352] | 147 | int icol=0; | 
|---|
| [534] | 148 | for (int i=0; i<tois.size(); i++) { | 
|---|
| [342] | 149 | double value = iter.getValue(i); | 
|---|
|  | 150 | bool   ok    = iter.canGetValue(i); | 
|---|
| [534] | 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) | 
|---|
| [352] | 159 | (this->*outValue)(icol,  0, true); | 
|---|
| [342] | 160 | else | 
|---|
| [352] | 161 | (this->*outValue)(icol, value); | 
|---|
|  | 162 | icol++; | 
|---|
| [342] | 163 | } | 
|---|
| [352] | 164 | (this->*endLine)(); | 
|---|
| [342] | 165 | } | 
|---|
| [352] | 166 | (this->*closeFile)(); | 
|---|
| [342] | 167 | cout << "\nDone." << endl; | 
|---|
|  | 168 | } | 
|---|
|  | 169 |  | 
|---|
| [352] | 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';; | 
|---|
| [534] | 181 | block_type_param* blk = iter.getFSet().lastParam(); | 
|---|
| [352] | 182 | if (blk) { | 
|---|
| [436] | 183 | int nb = blk->param.n_max_bolo; | 
|---|
| [352] | 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; | 
|---|
| [555] | 216 | *ostr << endl; | 
|---|
|  | 217 | // *ostr << '\n'; | 
|---|
| [352] | 218 | } | 
|---|
|  | 219 |  | 
|---|
|  | 220 | void ArchTOI::closeFile_A() { | 
|---|
|  | 221 | delete ostr; | 
|---|
|  | 222 | ostr = NULL; | 
|---|
|  | 223 | } | 
|---|
|  | 224 |  | 
|---|
| [534] | 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 | } | 
|---|
| [352] | 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 |  | 
|---|
| [534] | 260 |  | 
|---|
| [352] | 261 | void ArchTOI::outHeader_F(TOIIter& iter) { | 
|---|
| [534] | 262 | list<string> toinames = buildNames(); | 
|---|
|  | 263 |  | 
|---|
| [352] | 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; | 
|---|
| [534] | 270 | list<TOI>::iterator kk = tois.begin(); | 
|---|
|  | 271 | for (list<string>::iterator i = toinames.begin(); i != toinames.end(); i++,j++,kk++) { | 
|---|
| [352] | 272 | colnames[j] = const_cast<char*>((*i).c_str()); // should work for most STL implementations... Check... | 
|---|
|  | 273 | coltypes[j] = "1D"; | 
|---|
| [534] | 274 | colunits[j] = const_cast<char*>((*kk).unit.c_str()); | 
|---|
|  | 275 | cout << (*i).c_str() << " " << ((*kk).unit.c_str()) << endl; | 
|---|
| [352] | 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); | 
|---|
| [534] | 289 | list<string>::iterator kkk = toinames.begin(); | 
|---|
|  | 290 | for (list<string>::iterator i = headertoi.begin(); i != headertoi.end(); i++,j++,kkk++) { | 
|---|
| [352] | 291 | char line[80]; | 
|---|
|  | 292 | strcpy(line, (*i).c_str()); | 
|---|
|  | 293 | char* pline = line; | 
|---|
| [534] | 294 | char* cmt   = const_cast<char*>((*kkk).c_str()); | 
|---|
|  | 295 | fits_write_keys_str(fptr, "TOIREQ", j, 1, &pline, &cmt, &fitsStatus); | 
|---|
| [352] | 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 | 
|---|
| [534] | 308 | block_type_param* blk = iter.getFSet().lastParam(); | 
|---|
| [352] | 309 | if (blk) { | 
|---|
| [436] | 310 | int nb = blk->param.n_max_bolo; | 
|---|
| [352] | 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)) { | 
|---|
| [356] | 317 | //j++; | 
|---|
| [352] | 318 | char line[80]; | 
|---|
|  | 319 | strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom); | 
|---|
|  | 320 | char* pline = line; | 
|---|
| [356] | 321 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus); | 
|---|
| [352] | 322 | } | 
|---|
|  | 323 | #else | 
|---|
| [356] | 324 | //j++; | 
|---|
| [352] | 325 | char line[80]; | 
|---|
|  | 326 | strcpy(line, blk->param.bolo[i].bolo_nom); | 
|---|
|  | 327 | char* pline = line; | 
|---|
| [356] | 328 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus); | 
|---|
| [352] | 329 | #endif | 
|---|
|  | 330 | } | 
|---|
| [356] | 331 | //fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus); | 
|---|
| [352] | 332 | } | 
|---|
| [407] | 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); | 
|---|
| [354] | 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); | 
|---|
| [407] | 342 | for (list<string>::iterator i = comments.begin(); i!=comments.end(); i++) | 
|---|
|  | 343 | fits_write_comment(fptr, (char*)((*i).c_str()), &fitsStatus); | 
|---|
| [354] | 344 |  | 
|---|
| [352] | 345 | fitsLine = 1; | 
|---|
|  | 346 | } | 
|---|
|  | 347 |  | 
|---|
|  | 348 | void ArchTOI::outValue_F(int icolumn, double value, bool notdef) { | 
|---|
| [432] | 349 | if (notdef && undef != "#") { | 
|---|
|  | 350 | notdef = false; | 
|---|
|  | 351 | value = undefV; | 
|---|
|  | 352 | } | 
|---|
| [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 */ | 
|---|
| [400] | 367 | } | 
|---|
| [534] | 368 |  | 
|---|
|  | 369 |  | 
|---|