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

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

nouvelles toi

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