| [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  = "#"; | 
|---|
| [432] | 56 | undefV = -99999; | 
|---|
| [342] | 57 | allBolos = false; | 
|---|
| [352] | 58 | fptr = NULL; | 
|---|
|  | 59 | ostr = NULL; | 
|---|
| [407] | 60 | requestVersion = ""; | 
|---|
|  | 61 | svr.registerReqHandler(this); | 
|---|
| [342] | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 |  | 
|---|
| [407] | 65 | bool ArchTOI::processTOIReq(string line, string toiname, TOIKind /*kind*/, int index, | 
|---|
|  | 66 | bool interp, bool repet, bool flag, bool /*notrig*/) | 
|---|
| [342] | 67 | { | 
|---|
|  | 68 | headertoi.push_back(line); | 
|---|
| [352] | 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); | 
|---|
| [342] | 78 | toiflags.push_back(flg((flag?hasflag:0)+((!repet&&!interp)?useNA:0))); | 
|---|
| [407] | 79 | return true; | 
|---|
| [342] | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [407] | 82 | bool ArchTOI::processOption(string key, string arg) | 
|---|
| [342] | 83 | { | 
|---|
| [407] | 84 | string line=key; if (arg != "") line = line + " " + arg; | 
|---|
|  | 85 | headeropt.push_back(line); | 
|---|
| [342] | 86 | if (arg.length()>0 && arg[0] == ' ') { | 
|---|
|  | 87 | arg = arg.substr(arg.find_first_not_of(' ')); | 
|---|
|  | 88 | } | 
|---|
|  | 89 | if (key == "#ASCII") { | 
|---|
| [350] | 90 | format = ascii_fmt; | 
|---|
|  | 91 | } else if (key == "#FITS") { | 
|---|
|  | 92 | format = fits_fmt; | 
|---|
| [342] | 93 | } else if (key == "#UNDEF") { | 
|---|
|  | 94 | undef=arg; | 
|---|
| [432] | 95 | if (undef[0] >= '0' && undef[0] <= '9') | 
|---|
|  | 96 | undefV = atoi(undef.c_str()); | 
|---|
| [342] | 97 | } else if (key == "#ALLBOLOS") { | 
|---|
|  | 98 | allBolos=true; | 
|---|
| [407] | 99 | } else if (key == "#REQVERSION") { | 
|---|
| [408] | 100 | if (requestVersion != "") { | 
|---|
|  | 101 | cerr << "Error. Duplicate #REQVERSION option" << endl; | 
|---|
|  | 102 | exit(-1); | 
|---|
|  | 103 | } | 
|---|
| [407] | 104 | requestVersion = arg; | 
|---|
|  | 105 | } else if (key == "#COMMENT") { | 
|---|
|  | 106 | comments.push_back(arg); | 
|---|
|  | 107 | } else { | 
|---|
| [342] | 108 | return false; | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | return true; | 
|---|
|  | 112 | } | 
|---|
|  | 113 |  | 
|---|
|  | 114 |  | 
|---|
| [352] | 115 | void ArchTOI::run(string const& outfilename) | 
|---|
| [342] | 116 | { | 
|---|
| [352] | 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 | } | 
|---|
| [342] | 130 |  | 
|---|
|  | 131 | cout << "starting query" << endl; | 
|---|
| [407] | 132 | TOIIter iter = svr.doQuery(); | 
|---|
| [352] | 133 | (this->*openFile)(outfilename); | 
|---|
|  | 134 | (this->*outHeader)(iter); | 
|---|
|  | 135 |  | 
|---|
| [342] | 136 | cout << "processing" << endl; | 
|---|
|  | 137 | while (iter.Next()) { | 
|---|
| [426] | 138 | int nn = iter.getSampleIndex() / iter.getUnderSampling(); // Only for tick mark... | 
|---|
| [406] | 139 | #ifdef __MWERKS__ | 
|---|
|  | 140 | yield(); | 
|---|
|  | 141 | #endif | 
|---|
| [350] | 142 | if (nn%200 == 0) { | 
|---|
|  | 143 | cout << "."; cout.flush(); | 
|---|
|  | 144 | } | 
|---|
|  | 145 | if (nn%(200*80) == 0) cout << endl; | 
|---|
| [342] | 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; | 
|---|
| [352] | 153 | int icol=0; | 
|---|
| [342] | 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]; | 
|---|
| [352] | 159 | if (flag & hasflag) { | 
|---|
|  | 160 | (this->*outValue)(icol,  (ok && isnew ? 1 : 0)); | 
|---|
|  | 161 | icol++; | 
|---|
|  | 162 | } | 
|---|
| [342] | 163 | if (((flag & useNA)!=0 && !isnew) || !ok) | 
|---|
| [352] | 164 | (this->*outValue)(icol,  0, true); | 
|---|
| [342] | 165 | else | 
|---|
| [352] | 166 | (this->*outValue)(icol, value); | 
|---|
|  | 167 | icol++; | 
|---|
| [342] | 168 | } | 
|---|
| [352] | 169 | (this->*endLine)(); | 
|---|
| [342] | 170 | } | 
|---|
| [352] | 171 | (this->*closeFile)(); | 
|---|
| [342] | 172 | cout << "\nDone." << endl; | 
|---|
|  | 173 | } | 
|---|
|  | 174 |  | 
|---|
| [352] | 175 |  | 
|---|
|  | 176 | void ArchTOI::openFile_A(string const& filename) { | 
|---|
|  | 177 | ostr = new ofstream(filename.c_str()); | 
|---|
|  | 178 | } | 
|---|
|  | 179 |  | 
|---|
|  | 180 | void 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) { | 
|---|
| [436] | 188 | int nb = blk->param.n_max_bolo; | 
|---|
| [352] | 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 |  | 
|---|
|  | 209 | void 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 |  | 
|---|
|  | 219 | void ArchTOI::endLine_A() { | 
|---|
|  | 220 | if (!ostr) return; | 
|---|
|  | 221 | *ostr << '\n'; | 
|---|
|  | 222 | } | 
|---|
|  | 223 |  | 
|---|
|  | 224 | void ArchTOI::closeFile_A() { | 
|---|
|  | 225 | delete ostr; | 
|---|
|  | 226 | ostr = NULL; | 
|---|
|  | 227 | } | 
|---|
|  | 228 |  | 
|---|
|  | 229 |  | 
|---|
|  | 230 | void 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 |  | 
|---|
|  | 239 | void 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) { | 
|---|
| [436] | 282 | int nb = blk->param.n_max_bolo; | 
|---|
| [352] | 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)) { | 
|---|
| [356] | 289 | //j++; | 
|---|
| [352] | 290 | char line[80]; | 
|---|
|  | 291 | strcpy(line, blk->param.nom_coef[blk->param.bolo[i].numero_nom_coef].bolo_nom); | 
|---|
|  | 292 | char* pline = line; | 
|---|
| [356] | 293 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus); | 
|---|
| [352] | 294 | } | 
|---|
|  | 295 | #else | 
|---|
| [356] | 296 | //j++; | 
|---|
| [352] | 297 | char line[80]; | 
|---|
|  | 298 | strcpy(line, blk->param.bolo[i].bolo_nom); | 
|---|
|  | 299 | char* pline = line; | 
|---|
| [356] | 300 | fits_write_keys_str(fptr, "BOLO", i, 1, &pline, (char**) NULL, &fitsStatus); | 
|---|
| [352] | 301 | #endif | 
|---|
|  | 302 | } | 
|---|
| [356] | 303 | //fits_write_key(fptr, TINT, "BOLO", &j, NULL, &fitsStatus); | 
|---|
| [352] | 304 | } | 
|---|
| [407] | 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); | 
|---|
| [354] | 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); | 
|---|
| [407] | 314 | for (list<string>::iterator i = comments.begin(); i!=comments.end(); i++) | 
|---|
|  | 315 | fits_write_comment(fptr, (char*)((*i).c_str()), &fitsStatus); | 
|---|
| [354] | 316 |  | 
|---|
| [352] | 317 | fitsLine = 1; | 
|---|
|  | 318 | } | 
|---|
|  | 319 |  | 
|---|
|  | 320 | void ArchTOI::outValue_F(int icolumn, double value, bool notdef) { | 
|---|
| [432] | 321 | if (notdef && undef != "#") { | 
|---|
|  | 322 | notdef = false; | 
|---|
|  | 323 | value = undefV; | 
|---|
|  | 324 | } | 
|---|
| [352] | 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 |  | 
|---|
|  | 332 | void ArchTOI::endLine_F() { | 
|---|
|  | 333 | fitsLine++; | 
|---|
|  | 334 | } | 
|---|
|  | 335 |  | 
|---|
|  | 336 | void ArchTOI::closeFile_F() { | 
|---|
|  | 337 | fits_close_file(fptr, &fitsStatus); | 
|---|
|  | 338 | fits_report_error(stderr, fitsStatus);  /* print out any error messages */ | 
|---|
| [400] | 339 | } | 
|---|