| 1 | // archfileset.cc | 
|---|
| 2 | // Eric Aubourg         CEA/DAPNIA/SPP   septembre 1999 | 
|---|
| 3 |  | 
|---|
| 4 | #include <iostream.h> | 
|---|
| 5 | #include <dirent.h> | 
|---|
| 6 | #include "archfileset.h" | 
|---|
| 7 | #include "archparam.h" | 
|---|
| 8 | #include "toiproducer.h" | 
|---|
| 9 | #include "toimanager.h" | 
|---|
| 10 |  | 
|---|
| 11 | ArchFileSet::ArchFileSet() { | 
|---|
| 12 | file = NULL; | 
|---|
| 13 |  | 
|---|
| 14 | trigMask = block_reglage_mask | block_param_mask | TOIManager::trigMask; | 
|---|
| 15 |  | 
|---|
| 16 | sStart   = -999999999; | 
|---|
| 17 | sEnd     =  999999999; | 
|---|
| 18 | mjdStart = -999999999; | 
|---|
| 19 | mjdEnd   =  999999999; | 
|---|
| 20 |  | 
|---|
| 21 | initDone = false; | 
|---|
| 22 | endFound = false; | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 | ArchFileSet::ArchFileSet(ArchFileSet const& x) { | 
|---|
| 26 | directories = x.directories; | 
|---|
| 27 | filenames = x.filenames; | 
|---|
| 28 | if (x.initDone && x.curFile != x.filenames.end()) curFile = filenames.find(*(x.curFile)); | 
|---|
| 29 |  | 
|---|
| 30 | trigMask = x.trigMask; | 
|---|
| 31 |  | 
|---|
| 32 | sStart   = x.sStart; | 
|---|
| 33 | sEnd     = x.sEnd; | 
|---|
| 34 | mjdStart = x.mjdStart; | 
|---|
| 35 | mjdEnd   = x.mjdEnd; | 
|---|
| 36 |  | 
|---|
| 37 | if (x.file) | 
|---|
| 38 | file = new ArcheopsFile(*x.file); | 
|---|
| 39 | else | 
|---|
| 40 | file = NULL; | 
|---|
| 41 |  | 
|---|
| 42 | initDone = x.initDone; | 
|---|
| 43 | endFound = x.endFound; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | ArchFileSet::~ArchFileSet() { | 
|---|
| 47 | delete file; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | void ArchFileSet::setSNumRange(long start, long end) { | 
|---|
| 51 | sStart = start; | 
|---|
| 52 | sEnd   = end; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void ArchFileSet::setMJDRange(double start, double end) { | 
|---|
| 56 | mjdStart = start; | 
|---|
| 57 | mjdEnd   = end; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void ArchFileSet::addFile(string fn) { | 
|---|
| 61 | filenames.insert(fn); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void ArchFileSet::addDirectory(string fn) { | 
|---|
| 65 | directories.insert(fn); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | #ifdef __MWERKS__ | 
|---|
| 71 | #pragma mark - | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 | #ifdef __MWERKS__ | 
|---|
| 75 | #define filesep ':' | 
|---|
| 76 | #else | 
|---|
| 77 | #define filesep '/' | 
|---|
| 78 | #endif | 
|---|
| 79 |  | 
|---|
| 80 | void ArchFileSet::init() { | 
|---|
| 81 | if (initDone) return; | 
|---|
| 82 | initDone = true; | 
|---|
| 83 | trigMask |= TOIManager::trigMask; | 
|---|
| 84 |  | 
|---|
| 85 | // build files from directories | 
|---|
| 86 | for (set<string>::iterator i = directories.begin(); i != directories.end(); i++) { | 
|---|
| 87 | string directory = *i; | 
|---|
| 88 | if (directory[directory.length()-1] != filesep) | 
|---|
| 89 | directory += filesep; | 
|---|
| 90 |  | 
|---|
| 91 | DIR* dir = opendir(directory.c_str()); | 
|---|
| 92 | struct dirent* ent; | 
|---|
| 93 |  | 
|---|
| 94 | while ((ent = readdir(dir)) != NULL) { | 
|---|
| 95 | // si c'est un repertoire, avec un nom de jour, il faut l'explorer... | 
|---|
| 96 | if (!strncmp(ent->d_name, "arch-", 5)) { | 
|---|
| 97 | double mjd = ArcheopsFile::decodeMJD(ent->d_name+5) - 2./24.; // ENTIER + .5 en temps local! | 
|---|
| 98 | if (mjd >= mjdStart - 1. && mjd <= mjdEnd) { | 
|---|
| 99 | string direc2 = directory + ent->d_name + filesep; | 
|---|
| 100 | DIR* dir2 = opendir(direc2.c_str()); | 
|---|
| 101 | struct dirent* ent2; | 
|---|
| 102 | while ((ent2 = readdir(dir2)) != NULL) { | 
|---|
| 103 | if (*ent2->d_name == 'h') { | 
|---|
| 104 | double mjd2 = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.; | 
|---|
| 105 | if (mjd2 >= mjdStart - 1./24. && mjd2 <= mjdEnd) { | 
|---|
| 106 | filenames.insert(direc2 + ent2->d_name); | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 | } // "arch-" | 
|---|
| 112 | else if (*ent->d_name == 'h') { | 
|---|
| 113 | double mjd = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.; // $CHECK$ UTCOffset | 
|---|
| 114 | if (mjd >= mjdStart - 1./24. && mjd <= mjdEnd) { | 
|---|
| 115 | filenames.insert(directory + ent->d_name); | 
|---|
| 116 | } | 
|---|
| 117 | } // "h*" | 
|---|
| 118 | else  if (!strncmp(ent->d_name, "ARK", 3) || !strncmp(ent->d_name, "ark", 3)) { | 
|---|
| 119 | char * sfx = ent->d_name + strlen(ent->d_name) - 4; | 
|---|
| 120 | if (!strcmp(sfx, ".DAT") || !strcmp(sfx, ".dat")) { | 
|---|
| 121 | filenames.insert(directory + ent->d_name); | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 | } // readdir | 
|---|
| 125 | closedir(dir); | 
|---|
| 126 | } // iterate on dirs | 
|---|
| 127 |  | 
|---|
| 128 | scanFiles(); | 
|---|
| 129 |  | 
|---|
| 130 | if (filenames.empty()) { | 
|---|
| 131 | throw ArchExc("No files in fileset"); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | curFile = filenames.begin(); | 
|---|
| 135 | file = new ArcheopsFile((*curFile).c_str()); | 
|---|
| 136 | cout << "opening file " << (*curFile).c_str() << endl; | 
|---|
| 137 |  | 
|---|
| 138 | extern param_bolo  parametr; | 
|---|
| 139 | block_type_param block; | 
|---|
| 140 | block.param = parametr; | 
|---|
| 141 | valide_block((block_type_modele*)&block, block_param, 0); | 
|---|
| 142 | file->forceBlock((block_type_modele*)&block); | 
|---|
| 143 |  | 
|---|
| 144 | if (!file->lastReglage()) file->nextBlock(block_reglage_mask); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | void ArchFileSet::scanFiles() { | 
|---|
| 148 | file1stSamp.clear(); | 
|---|
| 149 | cout << "Scanning all files" << endl; | 
|---|
| 150 | // Petite astuce pour les STL non conformes comme celles de digital | 
|---|
| 151 | // qui ne supportent pas files.erase(i) suivi de i++.... | 
|---|
| 152 | set<string> copy = filenames; | 
|---|
| 153 | for (set<string>::iterator i = copy.begin(); i != copy.end(); i++) { | 
|---|
| 154 | ArcheopsFile fich((*i).c_str()); | 
|---|
| 155 | if (fich.nextBlock()) { | 
|---|
| 156 | file1stSamp[*i] = fich.blockNum()*72; // premier numsample | 
|---|
| 157 | cout << "File " << *i << " 1st sample = " << fich.blockNum()*72 | 
|---|
| 158 | << " UTC " << archParam.acq.SN2UTC(fich.blockNum()*72) << endl; | 
|---|
| 159 | } else { | 
|---|
| 160 | cout << "File " << *i << " unrecoverable, skipping" << endl; | 
|---|
| 161 | filenames.erase(*i); | 
|---|
| 162 | } | 
|---|
| 163 | } | 
|---|
| 164 | cout << "Scan done" << endl; | 
|---|
| 165 |  | 
|---|
| 166 | // Et maintenant, on ne garde que ceux qui tombent dans l'intervalle... | 
|---|
| 167 | copy = filenames; | 
|---|
| 168 | string prev=""; | 
|---|
| 169 | for (set<string>::iterator i = copy.begin(); i != copy.end(); i++) { | 
|---|
| 170 | double smp = file1stSamp[*i]; | 
|---|
| 171 | double t   = archParam.acq.tBlock0 + smp * archParam.acq.perEch/86400.; | 
|---|
| 172 | if (t>mjdEnd || smp>sEnd) {  // premier echantillon apres tEnd | 
|---|
| 173 | filenames.erase(*i); | 
|---|
| 174 | prev = ""; | 
|---|
| 175 | continue; | 
|---|
| 176 | } | 
|---|
| 177 | if (t<mjdStart || smp<sStart) { // premier echantillon avant tStart -> on vire le precedent si existe | 
|---|
| 178 | if (prev != "") { | 
|---|
| 179 | filenames.erase(prev); | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 | prev = *i; | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | bool ArchFileSet::nextFile() { | 
|---|
| 187 | if (filenames.empty()) { | 
|---|
| 188 | return false; | 
|---|
| 189 | } else { | 
|---|
| 190 | if (curFile == filenames.end()) return false; | 
|---|
| 191 | curFile++; | 
|---|
| 192 | if (curFile == filenames.end()) return false; | 
|---|
| 193 | cout << "opening file " << (*curFile).c_str() << endl; | 
|---|
| 194 | ArcheopsFile* newfile = new ArcheopsFile((*curFile).c_str()); | 
|---|
| 195 | newfile->grabLastBlocs(*file); | 
|---|
| 196 | delete file; | 
|---|
| 197 | file = newfile; | 
|---|
| 198 | return true; | 
|---|
| 199 | } | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | bool ArchFileSet::next() { | 
|---|
| 203 | if (endFound) return false; | 
|---|
| 204 | if (!initDone) init(); | 
|---|
| 205 | while (1) { | 
|---|
| 206 | long got = nextSample(); | 
|---|
| 207 | if (!got) {endFound=true; return false;} // end of files | 
|---|
| 208 | double t = getMJD(); | 
|---|
| 209 | if (t < mjdStart) continue; | 
|---|
| 210 | if (t > mjdEnd) {endFound=true; return false;} | 
|---|
| 211 | long s = getSampleIndex(); | 
|---|
| 212 | if (s < sStart) continue; | 
|---|
| 213 | if (s > sEnd) {endFound=true; return false;} | 
|---|
| 214 |  | 
|---|
| 215 | // On envoie les nouvelles donnees aux producers interesses | 
|---|
| 216 | for (map<TOILowLevProducer*, long>::iterator i = TOIManager::activeLLProducers.begin(); | 
|---|
| 217 | i != TOIManager::activeLLProducers.end(); i++) { | 
|---|
| 218 | if (got & ((*i).second)) (*i).first->handleBlock(this); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | return true; | 
|---|
| 222 | } | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | long ArchFileSet::getSampleIndex() { | 
|---|
| 226 | if (!initDone) init(); | 
|---|
| 227 | return file->blockNum() * nb_per_block*2; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | double ArchFileSet::getMJD() { | 
|---|
| 231 | if (!initDone) init(); | 
|---|
| 232 | int sample = getSampleIndex(); | 
|---|
| 233 | return archParam.acq.SN2MJD(sample); | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | long ArchFileSet::nextSample() { | 
|---|
| 237 | while (1) { | 
|---|
| 238 | long got = 0; | 
|---|
| 239 | if (file->nextBlock(trigMask)) { | 
|---|
| 240 | got |= 1 << type_block(file->currentBlock()); | 
|---|
| 241 | while (file->sameBlockNumAhead()) {  // tant que meme numero de bloc, on lit | 
|---|
| 242 | if (!file->nextBlock()) {          // fin de fichier ? | 
|---|
| 243 | if (nextFile()) file->nextBlock(); // fichier suivant | 
|---|
| 244 | else return 0;    // tout fini | 
|---|
| 245 | } | 
|---|
| 246 | got |= 1 << type_block(file->currentBlock()); | 
|---|
| 247 | } | 
|---|
| 248 | return got; | 
|---|
| 249 | } | 
|---|
| 250 | if (!nextFile()) return 0; | 
|---|
| 251 | } | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | #ifdef __MWERKS__ | 
|---|
| 255 | #pragma mark - | 
|---|
| 256 | #endif | 
|---|
| 257 |  | 
|---|
| 258 | block_type_param*         ArchFileSet::lastParam() { | 
|---|
| 259 | if (!file) return NULL; | 
|---|
| 260 | return file->lastParam(); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | block_type_journal*       ArchFileSet::lastJournal() { | 
|---|
| 264 | if (!file) return NULL; | 
|---|
| 265 | return file->lastJournal(); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | block_type_reglage*       ArchFileSet::lastReglage() { | 
|---|
| 269 | if (!file) return NULL; | 
|---|
| 270 | return file->lastReglage(); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | block_type_dilution*      ArchFileSet::lastDilution() { | 
|---|
| 274 | if (!file) return NULL; | 
|---|
| 275 | return file->lastDilution(); | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | block_type_gps*           ArchFileSet::lastGPS() { | 
|---|
| 279 | if (!file) return NULL; | 
|---|
| 280 | return file->lastGPS(); | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | block_type_une_periode*   ArchFileSet::lastUnePeriode() { | 
|---|
| 284 | if (!file) return NULL; | 
|---|
| 285 | return file->lastUnePeriode(); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | block_type_synchro_sol*   ArchFileSet::lastSynchroSol() { | 
|---|
| 289 | if (!file) return NULL; | 
|---|
| 290 | return file->lastSynchroSol(); | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | block_type_pointage_sol*  ArchFileSet::lastPointageSol() { | 
|---|
| 294 | if (!file) return NULL; | 
|---|
| 295 | return file->lastPointageSol(); | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | block_type_bolo*          ArchFileSet::lastBolo() { | 
|---|
| 299 | if (!file) return NULL; | 
|---|
| 300 | return file->lastBolo(); | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | block_type_gyro*          ArchFileSet::lastGyro() { | 
|---|
| 304 | if (!file) return NULL; | 
|---|
| 305 | return file->lastGyro(); | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | block_type_sst*           ArchFileSet::lastSST() { | 
|---|
| 309 | if (!file) return NULL; | 
|---|
| 310 | return file->lastSST(); | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | block_type_bolo_comprime* ArchFileSet::lastBoloComp() { | 
|---|
| 314 | if (!file) return NULL; | 
|---|
| 315 | return file->lastBoloComp(); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | block_type_gyro_comprime* ArchFileSet::lastGyroComp() { | 
|---|
| 319 | if (!file) return NULL; | 
|---|
| 320 | return file->lastGyroComp(); | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | block_type_sst_comprime*  ArchFileSet::lastSSTComp() { | 
|---|
| 324 | if (!file) return NULL; | 
|---|
| 325 | return file->lastSSTComp(); | 
|---|
| 326 | } | 
|---|