source: Sophya/trunk/Poubelle/archTOI.old/toiiter.cc@ 407

Last change on this file since 407 was 407, checked in by ansari, 26 years ago

apres grenoble

File size: 18.8 KB
RevLine 
[350]1// toiiter.cc
2// Eric Aubourg CEA/DAPNIA/SPP juillet 1999
3
[407]4#ifndef nb_max_bolo
5#define _archeops // Acquisition Archeops (avec transputer)
6#define programme
7extern "C" {
8#include "archeops.h"
9}
10#endif
[350]11
[407]12
[310]13#include "toiiter.h"
[315]14#include "toiinterpolator.h"
[394]15#include "archparam.h"
[310]16#include <dirent.h>
[342]17#include <iostream.h>
[310]18
19// Format bloc GPS
20// $GPGGA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,
21
22TOIIter::TOIIter() {
23 // Toutes les valeurs initialisees par defaut. Le TOISvr les positionnera,
24 // puis lancera l'initialisation
25
26 file = NULL;
27 directory = "";
28
[342]29 files.clear();
30
31 isOnBoardRecorder = false;
32
33 imes=0;
34
[310]35 tStart = -9.e99;
[342]36 tEnd = 9.e99;
[310]37
[394]38 //tBlock0 = -1;
39 // perEch = -1;
[350]40 // Values for Trapani 99 = default values
[394]41 //tBlock0 = 1376.8358818;
42 //perEch = 0.005836818076;
[315]43
[342]44 trigMask = 0;
[315]45 rawIter = NULL;
46 interp = NULL;
47 lastSample = -1;
[342]48 maxLookAhead = 10000;
[358]49
50 auxGPS = NULL;
[393]51
52 initDone = false;
[310]53}
54
55TOIIter::TOIIter(TOIIter const& x) {
56 directory = x.directory;
57 files = x.files;
[401]58 if (x.initDone && x.curFile != x.files.end()) curFile = files.find(*(x.curFile));
[342]59 isOnBoardRecorder = x.isOnBoardRecorder;
[310]60 imes = x.imes;
61 tStart = x.tStart;
62 tEnd = x.tEnd;
63 trigMask = x.trigMask;
64 infos = x.infos;
65
[315]66 if (x.file)
67 file = new ArcheopsFile(*x.file);
68 else
69 file = NULL;
70
71 if (x.rawIter) {
72 rawIter = new TOIIter(*x.rawIter);
73 interp = new TOIInterpolator[infos.size()];
74 for (int i=0; i<infos.size(); i++)
75 interp[i] = x.interp[i];
76 } else {
77 rawIter = NULL;
78 interp = NULL;
79 }
80
81 lastSample = x.lastSample;
82 maxLookAhead = x.maxLookAhead;
[358]83
84 auxGPS = x.auxGPS;
85 if (auxGPS) auxGPS = auxGPS->clone();
[393]86
87 initDone = x.initDone;
[310]88}
89
90TOIIter::~TOIIter() {
91 delete file;
[315]92 delete rawIter;
93 delete[] interp;
[358]94 delete auxGPS;
[310]95}
96
[342]97#ifdef __MWERKS__
98#define filesep ':'
99#else
100#define filesep '/'
101#endif
102
[310]103void TOIIter::Init() {
[393]104 if (initDone) return;
105 initDone = true;
[350]106 // On a soit un repertoire, soit une liste de fichiers....
[310]107 if (directory == "") {
[350]108 if (files.empty()) { // Ni repertoire, ni fichiers
109 cerr << "toiiter : pas de repertoire, pas de fichiers" << endl;
110 exit(-1);
[342]111 } else {
[350]112 // On a deja une liste de fichiers
[342]113 }
114 } else { // On a un repertoire a explorer
115 // On cherche soit les fichiers dans le repertoire donne, soit des fichiers
116 // dans un sous-repertoire "arch-YY_MM_DD". Les fichiers ont un nom en
117 // "hYY_MM_DD-hh_mm_ss".
118 // Pour l'enregistreur de vol, les fichiers ont un nom en ARKxxxxxx.DAT
119 if (directory[directory.length()-1] != filesep)
120 directory += filesep;
[310]121 DIR* dir = opendir(directory.c_str());
122 struct dirent* ent;
123 while ((ent = readdir(dir)) != NULL) {
[342]124 // si c'est un repertoire, avec un nom de jour, il faut l'explorer...
125 if (!strncmp(ent->d_name, "arch-", 5)) {
126 double mjd = ArcheopsFile::decodeMJD(ent->d_name+5) - 2./24.; // ENTIER + .5 en temps local!
127 if (mjd >= tStart - 1. && mjd <= tEnd) {
128 string direc2 = directory + ent->d_name + filesep;
129 DIR* dir2 = opendir(direc2.c_str());
130 struct dirent* ent2;
131 while ((ent2 = readdir(dir2)) != NULL) {
132 if (*ent2->d_name == 'h') {
133 double mjd2 = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.;
134 if (mjd2 >= tStart - 1./24. && mjd2 <= tEnd) {
135 files.insert(direc2 + ent2->d_name);
136 }
137 }
138 }
139 }
140 } else {
141 if (!isOnBoardRecorder && *ent->d_name == 'h') {
142 double mjd = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.; // $CHECK$ UTCOffset
143 if (mjd >= tStart - 1./24. && mjd <= tEnd) {
144 files.insert(directory + ent->d_name);
145 }
146 } else if (isOnBoardRecorder) {
[350]147 if (strncmp(ent->d_name, "ARK", 3) && strncmp(ent->d_name, "ark", 3)) continue;
[342]148 char * sfx = ent->d_name + strlen(ent->d_name) - 4;
[350]149 if (strcmp(sfx, ".DAT") && strcmp(sfx, ".dat")) continue;
[342]150 files.insert(directory + ent->d_name);
151 }
152 }
[310]153 }
154 closedir(dir);
155 }
[350]156
157 ScanFiles();
158
159 curFile = files.begin();
160 file = new ArcheopsFile((*curFile).c_str());
161 cout << "opening file " << (*curFile).c_str() << endl;
[310]162
[342]163 // On avance jusqu'a avoir au moins un bloc param et un bloc reglage,
164 // car on ne peut rien faire sans...
165 // Si on a des donnees de l'enregistreur de vol, pas de bloc param, et
166 // on en simule un
[350]167 double oldTStart = tStart;
168 tStart = -9.e99; // pour init, on accepte des blocs avant tstart....
169
[342]170 if (!file->lastParam()) {
171 if (isOnBoardRecorder) {
172 extern param_bolo parametr;
173 block_type_param block;
174 block.param = parametr;
175 valide_block((block_type_modele*)&block, block_param, 0);
176 file->forceBlock((block_type_modele*)&block);
177 } else {
178 file->nextBlock(block_param_mask);
179 }
180 }
[310]181 if (!file->lastReglage()) file->nextBlock(block_reglage_mask);
182
183 // On cherche un bloc GPS pour avoir la correspondance timestamp/UTC.
184 // Pour le moment, on se fonde sur le premier bloc GPS. On pourra faire
[342]185 // mieux en prenant le min de tous les delta_T, a condition d'avoir un
186 // peu plus de details sur la facon dont le GPS est lu.
[310]187
[394]188 if (archParam.acq.tBlock0 < 0) {
189 archParam.acq.tBlock0 = file->getStartMJD();
[315]190
[342]191 file->pushMark();
192 if (file->lastGPS() || file->nextBlock(block_gps_mask)) {
193 // le temps du bloc courant, en secondes
194 double dt = file->blockNum() * file->perBlock();
[394]195 archParam.acq.tBlock0 = file->getGPSMJD() - dt/86400.;
[342]196 } else { // pas de bloc GPS...
[394]197 archParam.acq.tBlock0 = file->getStartMJD();
[342]198 }
199 file->popMark();
[310]200 }
[350]201 tStart = oldTStart; // on restaure
[342]202
[394]203 if (archParam.acq.perEch < 0)
204 archParam.acq.perEch = file->perEchant();
[315]205
206 bool hasInterp = false;
[310]207
208 trigMask = 0;
209 for (vector<info>::iterator i = infos.begin(); i != infos.end(); i++) {
[315]210 if ((*i).interpolated) hasInterp = true;
[310]211 if ((*i).triggering) {
212 switch ((*i).kind) {
[400]213 case boloTens:
[407]214 case boloTens2:
[400]215 case boloRaw:
[407]216 case boloRes:
217 case boloTemp:
218 case boloRawCN:
219 case boloTens2T:
220 trigMask |= block_bolo_mask;
221 break;
222 case boloGainAmpli:
223 trigMask |= block_reglage_mask;
224 break;
[400]225 case gpsTime:
226 case longitude:
227 case latitude:
228 case altitude:
[407]229 trigMask |= block_gps_mask;
230 break;
[400]231 case azimut:
[407]232 file->needSSTProcessMask(SSTHandler::findPeriod);
233 trigMask |= block_sst_mask;
234 break;
[400]235 case sstStarCnt:
236 case sstStarZ:
237 case sstStarF:
238 case sstStarT:
[407]239 file->needSSTProcessMask(SSTHandler::findStars);
240 trigMask |= block_sst_mask;
241 break;
[400]242 case sstDiode:
[407]243 case sstDiodeCN:
244 file->needSSTProcessMask(SSTHandler::permDiode);
245 trigMask |= block_sst_mask;
246 break;
[400]247 case sstChannel:
[407]248 case sstChannelCN:
249 trigMask |= block_sst_mask;
250 break;
[400]251 case gyroRaw:
[407]252 case gyroTens:
253 trigMask |= block_gyro_mask;
254 break;
255 case gyroSpeed:
256 trigMask |= block_gyro_mask; // $CHECK$ + info to calibrate gyros
257 break;
[400]258 case alphaAxis:
259 case deltaAxis:
[407]260 case alphaSst:
261 case deltaSst:
[400]262 case alphaBolo:
263 case deltaBolo:
[407]264 file->needSSTProcessMask(SSTHandler::findAxis);
265 trigMask |= block_sst_mask;
266 break;
[310]267 }
268 }
269 }
270
271 if (trigMask & (block_bolo_mask | block_sst_mask)) {
272 imes = 9999;
273 } else {
274 imes = 0;
275 }
[315]276
277 if (hasInterp) {
278 rawIter = new TOIIter(*this);
279 interp = new TOIInterpolator[infos.size()];
280 for (int i=0; i<infos.size(); i++) {
281 rawIter->infos[i].interpolated = false;
282 }
283 delete file; file = NULL; // on ne travaille plus sur le fichier directement...
284 }
[310]285}
286
[350]287void TOIIter::ScanFiles() {
288 file1stSamp.clear();
289 cout << "Scanning all files" << endl;
290 // Petite astuce pour les STL non conformes comme celles de digital
291 // qui ne supportent pas files.erase(i) suivi de i++....
292 set<string> copy = files;
293 for (set<string>::iterator i = copy.begin(); i != copy.end(); i++) {
294 ArcheopsFile fich((*i).c_str());
295 if (fich.nextBlock()) {
296 file1stSamp[*i] = fich.blockNum()*72; // premier numsample
297 cout << "File " << *i << " 1st sample = " << fich.blockNum()*72 << endl;
298 } else {
299 cout << "File " << *i << " unrecoverable, skipping" << endl;
300 files.erase(*i);
301 }
302 }
303 cout << "Scan done" << endl;
304
305 // Et maintenant, on ne garde que ceux qui tombent dans l'intervalle...
306 copy = files;
307 string prev="";
308 for (set<string>::iterator i = copy.begin(); i != copy.end(); i++) {
309 double smp = file1stSamp[*i];
[394]310 double t = archParam.acq.tBlock0 + smp * archParam.acq.perEch/86400.;
[350]311 if (t>tEnd) { // premier echantillon apres tEnd
312 files.erase(*i);
313 prev = "";
314 continue;
315 }
316 if (t<tStart) { // premier echantillon avant tStart -> on vire le precedent si existe
317 if (prev != "") {
318 files.erase(prev);
319 }
320 }
321 prev = *i;
322 }
323}
324
[310]325bool TOIIter::NextFile() {
[315]326 if (rawIter)
327 return rawIter->NextFile();
328
[342]329 if (files.empty()) {
[310]330 return false;
331 } else {
332 if (curFile == files.end()) return false;
333 curFile++;
334 if (curFile == files.end()) return false;
[342]335 cout << "opening file " << (*curFile).c_str() << endl;
[310]336 ArcheopsFile* newfile = new ArcheopsFile((*curFile).c_str());
337 newfile->grabLastBlocs(*file);
338 delete file;
339 file = newfile;
340 return true;
341 }
342}
343
[350]344
[310]345bool TOIIter::Next() {
[393]346 if (!initDone) Init();
[350]347 while (1) {
348 if (!NextSample()) return false; // end of files
[358]349 double t = getMJD();
[350]350 if (t < tStart) continue;
351 if (t > tEnd) return false;
352 return true;
353 }
354}
355
356bool TOIIter::NextSample() {
[315]357 if (rawIter) { // Delegation pour interpolation
358 // Trouve prochain sample disponible
359 for (int k=0; k<2; k++) {
360 long smp = 2147483647L;
361 for (int i=0; i<infos.size(); i++) {
362 long ss = interp[i].nextSample(lastSample+1);
363 if (ss > 0 && ss < smp) smp=ss;
364 }
365 if (smp != 2147483647L) {
366 lastSample = smp;
367 break;
368 }
369 if (!fetchAhead()) // tout le monde etait en bout de course,
370 return false; // on lit un echantillon, ca suffit, d'ou le k<2
371 }
372 // Verifie que tous les interpolateurs ont assez de donnees pour
373 // trouver la valeur correspondante
374 for (int i=0; i<infos.size(); i++) {
375 //rif (infos[i].interpolated)
376 while (interp[i].needMoreDataFor(lastSample) &&
377 rawIter->getSampleIndex() - lastSample < maxLookAhead)
378 if (!fetchAhead()) return false;
379 }
380
381 // On est pret...
382 return true;
383 }
[310]384
[406]385 // trigger sur info indexee dans bloc bolo, bloc gyro ou bloc sst ?
386 if (trigMask & (block_bolo_mask | block_sst_mask | block_gyro_mask )) {
[310]387 imes++;
388 if (imes < file->nEchBlock()) return true;
389 imes = 0;
390 }
391
392 // soit pas d'info indexee, soit fin bloc courant...
[315]393 while (1) {
394 if (file->nextBlock(trigMask)) {
395 while (file->sameBlockNumAhead()) { // tant que meme numero de bloc, on lit
396 if (!file->nextBlock()) { // fin de fichier ?
[393]397 if (NextFile()) file->nextBlock(); // fichier suivant
398 else return false; // tout fini
[315]399 }
400 }
401 return true;
402 }
403 if (!NextFile()) return false;
[310]404 }
405}
406
[315]407/* double TOIIter::getTime() { // MJD
[310]408 // le temps du bloc courant, en secondes
409 double dt = file->blockNum() * file->perBlock();
410 return tBlock0 + dt/86400. + imes*file->perEchant()/86400.;
411 }
[315]412 */
[310]413
[315]414bool TOIIter::canGetValue(int column) {
[393]415 if (!initDone) Init();
[315]416 if (column < 0 || column >= infos.size()) return false;
[358]417 TOIKind kind = infos[column].kind;
418 if (auxGPS &&
[363]419 (kind == longitude || kind == latitude || kind == altitude || kind == tsid)) {
[358]420 double dummy;
421 return auxGPS->getLocation(getMJD(), dummy, dummy, dummy) == 0;
422 }
[315]423 if (rawIter) {
424 return interp[column].canGet(lastSample);
425 }
426 int index = infos[column].index;
427 switch (kind) {
[400]428 case sampleNum:
429 case internalTime:
430 case mjd:
431 return true;
432 case boloTens:
[407]433 case boloTens2:
434 case boloTens2T:
435 case boloRes:
436 case boloRawCN:
[400]437 if (imes==0 && file->llastBolo()==NULL) return false;
438 return file->lastBolo() != NULL;
439 case boloRaw:
440 return file->lastBolo() != NULL;
[407]441 case boloGainAmpli:
442 return file->lastReglage() != NULL;
[400]443 case sstDiode:
444 case sstChannel:
[407]445 case sstDiodeCN:
446 case sstChannelCN:
[400]447 return file->lastSST() != NULL;
448 case sstStarCnt:
449 case sstStarZ:
450 case sstStarF:
451 case sstStarT:{
452 if (file->lastSST() == NULL) return false;
453 int n = file->getNumbStar(imes);
454 return (n > 0 && index < n);
[315]455 }
[400]456 case gyroRaw:
[407]457 case gyroTens:
458 case gyroSpeed:
[400]459 return (file->lastGyro() != NULL);
460 case gpsTime:
461 return file->hasGPSTime();
462 case longitude:
463 case latitude:
464 return file->hasGPSPos();
465 case altitude:
466 return file->hasGPSAlt();
467 case tsid:
468 return file->hasGPSPos();
469 case azimut:
470 case alphaAxis:
471 case deltaAxis:
472 return false;
473 //return (file->lastGPS() != NULL && file->lastSST() != NULL);
474 case alphaSst:
475 case deltaSst:
476 case alphaBolo:
477 case deltaBolo:
478 return false;
479
480 case boloTemp:
481 return false;
482 }
[315]483 return false;
484}
[400]485
[407]486
[315]487double TOIIter::getValue(int column) {
[393]488 if (!initDone) Init();
[310]489 if (column < 0 || column >= infos.size()) return -1;
[358]490 TOIKind kind = infos[column].kind;
491 if (auxGPS &&
[363]492 (kind == longitude || kind == latitude || kind == altitude || kind == tsid)) {
[358]493 double lat,lon,alt;
494 if (auxGPS->getLocation(getMJD(), lat, lon, alt)) return -99999;
495 if (kind == longitude) return lon;
496 if (kind == latitude) return lat;
[363]497 if (kind == altitude) return alt;
498 if (kind == tsid) {
499 tSid.setLongitude(lon);
500 return tSid.getLST(getMJD());
501 }
[358]502 }
[315]503 if (rawIter) {
504 if (infos[column].interpolated)
505 return interp[column].getIValue(lastSample);
506 else
507 return interp[column].getEValue(lastSample);
508 }
[310]509 int index = infos[column].index;
510 switch (kind) {
[400]511 case sampleNum:
512 return getSampleIndex();
513 case internalTime:
514 return getSampleIndex() * archParam.acq.perEch;
515 case mjd:
516 return getMJD();
517 case boloTens:
518 return file->getMuVBolo(index, imes);
519 case boloRaw:
520 return file->getRawBolo(index, imes);
[407]521 case boloGainAmpli:
522 return file->getGainAmpli(index);
[400]523 case sstDiode:
524 return file->getSSTSignal(index, imes);
525 case sstChannel:
526 return file->getSSTRawSignal(index, imes);
527 case sstStarCnt:
528 return file->getNumbStar(imes);
529 case sstStarZ:
530 return file->getSSTStarZ(index, imes);
531 case sstStarF:
532 return file->getSSTStarF(index, imes);
533 case sstStarT:
534 return file->getSSTStarT(index, imes);
535 case gyroRaw:
[407]536 return file->getGyroRaw(index, imes);
537 case gyroTens:
538 return file->getGyroTens(index, imes);
539 case gyroSpeed:
540 return file->getGyroSpeed(index, imes);
[400]541 case gpsTime:
542 return file->getGPSUTC();
543 case longitude:
544 return file->getGPSLong();
545 case latitude:
546 return file->getGPSLat();
547 case altitude:
548 return file->getGPSAlt();
549 case tsid:
550 tSid.setLongitude(file->getGPSLong());
551 return tSid.getLST(getMJD());
552 case azimut:
553 return file->getAzimut(imes);
554 case alphaAxis:
555 return file->getAlpha(imes);
556 case deltaAxis:
557 return file->getDelta(imes);
[310]558 }
559 return -1;
[315]560}
[310]561
[315]562bool TOIIter::newValue(int column) {
[393]563 if (!initDone) Init();
[310]564 if (column < 0 || column >= infos.size()) return false;
[358]565 TOIKind kind = infos[column].kind;
566 if (auxGPS &&
567 (kind == longitude || kind == latitude || kind == altitude)) {
568 return true;
569 }
[342]570 if (rawIter) {
571 return interp[column].isNewValue(lastSample);
572 }
[310]573 switch (kind) {
[315]574 case sampleNum:
575 case internalTime:
[350]576 case mjd:
[363]577 case tsid:
[315]578 return true;
[310]579 case boloTens:
[315]580 return file->blockNum() == file->getBoloBlockNum();
[310]581 case boloRaw:
[315]582 return file->blockNum() == file->getBoloBlockNum();
[407]583 case boloGainAmpli:
584 return file->blockNum() == file->getReglageBlockNum() && imes==0;
[350]585 case sstChannel:
586 case sstDiode:
[400]587 case sstStarCnt:
[342]588 case sstStarZ:
589 case sstStarF:
[394]590 case sstStarT:
[315]591 return file->blockNum() == file->getSSTBlockNum();
[342]592 case gyroRaw:
[407]593 case gyroTens:
594 case gyroSpeed:
[342]595 return file->blockNum() == file->getGyroBlockNum();
596 case gpsTime:
597 return file->blockNum() == file->getGPSBlockNum() && imes==0;
[310]598 case longitude:
[315]599 return file->blockNum() == file->getGPSBlockNum() && imes==0;
[310]600 case latitude:
[315]601 return file->blockNum() == file->getGPSBlockNum() && imes==0;
[342]602 case altitude:
603 return file->blockNum() == file->getGPSBlockNum() && imes==0;
[315]604 case azimut:
605 return true; // $CHECK$ with SSTHandler
606 case alphaAxis:
607 return true; // $CHECK$ with SSTHandler
608 case deltaAxis:
609 return true; // $CHECK$ with SSTHandler
[310]610 }
611 return false;
[315]612}
[310]613
[315]614bool TOIIter::extendValue(int column) {
615 return (!infos[column].interpolated && !newValue(column));
616}
[310]617
[315]618bool TOIIter::interpValue(int column) {
619 return (infos[column].interpolated && !newValue(column));
620}
[342]621
622bool TOIIter::isTrig(int column) {
623 if (column < 0 || column >= infos.size()) return false;
624 return infos[column].triggering;
625}
626
[310]627
[315]628TOIKind TOIIter::getKind(int column) {
[310]629 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
630 return infos[column].kind;
[315]631}
[310]632
[315]633int TOIIter::getIndex(int column) {
[310]634 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
635 return infos[column].index;
[315]636}
637
638int TOIIter::getColTOI(TOIKind kind, int index) {
639 for (int i=0; i<infos.size(); i++)
640 if (infos[i].kind == kind && infos[i].index == index)
641 return i;
642 return -1;
643}
644
645bool TOIIter::canGetTOI(TOIKind kind, int index) {
646 int col = getColTOI(kind, index);
647 if (col<0) return false;
648 return canGetValue(col);
649}
650
651double TOIIter::getTOI(TOIKind kind, int index) {
652 int col = getColTOI(kind, index);
653 if (col<0) return -9.e99;
654 return getValue(col);
655}
656
[310]657
[315]658int TOIIter::getBlockSampleIndex() {
[310]659 return imes;
[315]660}
661
662int TOIIter::getSampleIndex() {
[393]663 if (!initDone) Init();
[342]664 if (file) {
665 return file->blockNum() * file->nEchBlock() + imes;
666 } else {
667 return lastSample;
668 }
[315]669}
[358]670
671double TOIIter::getMJD() {
[393]672 if (!initDone) Init();
[358]673 int sample = getSampleIndex();
[394]674 return archParam.acq.tBlock0 + sample*archParam.acq.perEch/86400.;
[358]675}
[315]676
677bool TOIIter::fetchAhead() { // Seulement si delegation
678 if (!rawIter) return false;
679 if (!rawIter->Next()) return false;
680 long sample = rawIter->getSampleIndex();
681 for (int i=0; i<infos.size(); i++) {
[342]682 if (rawIter->canGetValue(i) && rawIter->newValue(i)) {
[315]683 interp[i].enterValue(rawIter->getValue(i), sample);
[342]684 }
[315]685 }
686 return true;
687}
688
[342]689block_type_param* TOIIter::lastParam() {
[393]690 if (!initDone) Init();
[342]691 if (file) return file->lastParam();
692 else return rawIter->lastParam();
693}
694
695
Note: See TracBrowser for help on using the repository browser.