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
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 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;
225 case gpsTime:
226 case longitude:
227 case latitude:
228 case altitude:
229 trigMask |= block_gps_mask;
230 break;
231 case azimut:
232 file->needSSTProcessMask(SSTHandler::findPeriod);
233 trigMask |= block_sst_mask;
234 break;
235 case sstStarCnt:
236 case sstStarZ:
237 case sstStarF:
238 case sstStarT:
239 file->needSSTProcessMask(SSTHandler::findStars);
240 trigMask |= block_sst_mask;
241 break;
242 case sstDiode:
243 case sstDiodeCN:
244 file->needSSTProcessMask(SSTHandler::permDiode);
245 trigMask |= block_sst_mask;
246 break;
247 case sstChannel:
248 case sstChannelCN:
249 trigMask |= block_sst_mask;
250 break;
251 case gyroRaw:
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;
258 case alphaAxis:
259 case deltaAxis:
260 case alphaSst:
261 case deltaSst:
262 case alphaBolo:
263 case deltaBolo:
264 file->needSSTProcessMask(SSTHandler::findAxis);
265 trigMask |= block_sst_mask;
266 break;
267 }
268 }
269 }
270
271 if (trigMask & (block_bolo_mask | block_sst_mask)) {
272 imes = 9999;
273 } else {
274 imes = 0;
275 }
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 }
285}
286
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];
310 double t = archParam.acq.tBlock0 + smp * archParam.acq.perEch/86400.;
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
325bool TOIIter::NextFile() {
326 if (rawIter)
327 return rawIter->NextFile();
328
329 if (files.empty()) {
330 return false;
331 } else {
332 if (curFile == files.end()) return false;
333 curFile++;
334 if (curFile == files.end()) return false;
335 cout << "opening file " << (*curFile).c_str() << endl;
336 ArcheopsFile* newfile = new ArcheopsFile((*curFile).c_str());
337 newfile->grabLastBlocs(*file);
338 delete file;
339 file = newfile;
340 return true;
341 }
342}
343
344
345bool TOIIter::Next() {
346 if (!initDone) Init();
347 while (1) {
348 if (!NextSample()) return false; // end of files
349 double t = getMJD();
350 if (t < tStart) continue;
351 if (t > tEnd) return false;
352 return true;
353 }
354}
355
356bool TOIIter::NextSample() {
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 }
384
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 )) {
387 imes++;
388 if (imes < file->nEchBlock()) return true;
389 imes = 0;
390 }
391
392 // soit pas d'info indexee, soit fin bloc courant...
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 ?
397 if (NextFile()) file->nextBlock(); // fichier suivant
398 else return false; // tout fini
399 }
400 }
401 return true;
402 }
403 if (!NextFile()) return false;
404 }
405}
406
407/* double TOIIter::getTime() { // MJD
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 }
412 */
413
414bool TOIIter::canGetValue(int column) {
415 if (!initDone) Init();
416 if (column < 0 || column >= infos.size()) return false;
417 TOIKind kind = infos[column].kind;
418 if (auxGPS &&
419 (kind == longitude || kind == latitude || kind == altitude || kind == tsid)) {
420 double dummy;
421 return auxGPS->getLocation(getMJD(), dummy, dummy, dummy) == 0;
422 }
423 if (rawIter) {
424 return interp[column].canGet(lastSample);
425 }
426 int index = infos[column].index;
427 switch (kind) {
428 case sampleNum:
429 case internalTime:
430 case mjd:
431 return true;
432 case boloTens:
433 case boloTens2:
434 case boloTens2T:
435 case boloRes:
436 case boloRawCN:
437 if (imes==0 && file->llastBolo()==NULL) return false;
438 return file->lastBolo() != NULL;
439 case boloRaw:
440 return file->lastBolo() != NULL;
441 case boloGainAmpli:
442 return file->lastReglage() != NULL;
443 case sstDiode:
444 case sstChannel:
445 case sstDiodeCN:
446 case sstChannelCN:
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);
455 }
456 case gyroRaw:
457 case gyroTens:
458 case gyroSpeed:
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 }
483 return false;
484}
485
486
487double TOIIter::getValue(int column) {
488 if (!initDone) Init();
489 if (column < 0 || column >= infos.size()) return -1;
490 TOIKind kind = infos[column].kind;
491 if (auxGPS &&
492 (kind == longitude || kind == latitude || kind == altitude || kind == tsid)) {
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;
497 if (kind == altitude) return alt;
498 if (kind == tsid) {
499 tSid.setLongitude(lon);
500 return tSid.getLST(getMJD());
501 }
502 }
503 if (rawIter) {
504 if (infos[column].interpolated)
505 return interp[column].getIValue(lastSample);
506 else
507 return interp[column].getEValue(lastSample);
508 }
509 int index = infos[column].index;
510 switch (kind) {
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);
521 case boloGainAmpli:
522 return file->getGainAmpli(index);
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:
536 return file->getGyroRaw(index, imes);
537 case gyroTens:
538 return file->getGyroTens(index, imes);
539 case gyroSpeed:
540 return file->getGyroSpeed(index, imes);
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);
558 }
559 return -1;
560}
561
562bool TOIIter::newValue(int column) {
563 if (!initDone) Init();
564 if (column < 0 || column >= infos.size()) return false;
565 TOIKind kind = infos[column].kind;
566 if (auxGPS &&
567 (kind == longitude || kind == latitude || kind == altitude)) {
568 return true;
569 }
570 if (rawIter) {
571 return interp[column].isNewValue(lastSample);
572 }
573 switch (kind) {
574 case sampleNum:
575 case internalTime:
576 case mjd:
577 case tsid:
578 return true;
579 case boloTens:
580 return file->blockNum() == file->getBoloBlockNum();
581 case boloRaw:
582 return file->blockNum() == file->getBoloBlockNum();
583 case boloGainAmpli:
584 return file->blockNum() == file->getReglageBlockNum() && imes==0;
585 case sstChannel:
586 case sstDiode:
587 case sstStarCnt:
588 case sstStarZ:
589 case sstStarF:
590 case sstStarT:
591 return file->blockNum() == file->getSSTBlockNum();
592 case gyroRaw:
593 case gyroTens:
594 case gyroSpeed:
595 return file->blockNum() == file->getGyroBlockNum();
596 case gpsTime:
597 return file->blockNum() == file->getGPSBlockNum() && imes==0;
598 case longitude:
599 return file->blockNum() == file->getGPSBlockNum() && imes==0;
600 case latitude:
601 return file->blockNum() == file->getGPSBlockNum() && imes==0;
602 case altitude:
603 return file->blockNum() == file->getGPSBlockNum() && imes==0;
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
610 }
611 return false;
612}
613
614bool TOIIter::extendValue(int column) {
615 return (!infos[column].interpolated && !newValue(column));
616}
617
618bool TOIIter::interpValue(int column) {
619 return (infos[column].interpolated && !newValue(column));
620}
621
622bool TOIIter::isTrig(int column) {
623 if (column < 0 || column >= infos.size()) return false;
624 return infos[column].triggering;
625}
626
627
628TOIKind TOIIter::getKind(int column) {
629 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
630 return infos[column].kind;
631}
632
633int TOIIter::getIndex(int column) {
634 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
635 return infos[column].index;
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
657
658int TOIIter::getBlockSampleIndex() {
659 return imes;
660}
661
662int TOIIter::getSampleIndex() {
663 if (!initDone) Init();
664 if (file) {
665 return file->blockNum() * file->nEchBlock() + imes;
666 } else {
667 return lastSample;
668 }
669}
670
671double TOIIter::getMJD() {
672 if (!initDone) Init();
673 int sample = getSampleIndex();
674 return archParam.acq.tBlock0 + sample*archParam.acq.perEch/86400.;
675}
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++) {
682 if (rawIter->canGetValue(i) && rawIter->newValue(i)) {
683 interp[i].enterValue(rawIter->getValue(i), sample);
684 }
685 }
686 return true;
687}
688
689block_type_param* TOIIter::lastParam() {
690 if (!initDone) Init();
691 if (file) return file->lastParam();
692 else return rawIter->lastParam();
693}
694
695
Note: See TracBrowser for help on using the repository browser.