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