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

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

archtoi 2 aout 99

File size: 14.8 KB
Line 
1#include "toiiter.h"
2#include "toiinterpolator.h"
3#include <dirent.h>
4#include <iostream.h>
5
6// Format bloc GPS
7// $GPGGA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,
8
9TOIIter::TOIIter() {
10 // Toutes les valeurs initialisees par defaut. Le TOISvr les positionnera,
11 // puis lancera l'initialisation
12
13 file = NULL;
14 directory = "";
15 fileName = "";
16
17 files.clear();
18
19 isOnBoardRecorder = false;
20
21 imes=0;
22
23 tStart = -9.e99;
24 tEnd = 9.e99;
25
26 tBlock0 = -1;
27 perEch = -1;
28
29 trigMask = 0;
30 rawIter = NULL;
31 interp = NULL;
32 lastSample = -1;
33 maxLookAhead = 10000;
34
35}
36
37TOIIter::TOIIter(TOIIter const& x) {
38 directory = x.directory;
39 fileName = x.fileName;
40 files = x.files;
41 // curFile = x.curFile; // $CHECK$ DANGER !!
42 curFile = files.find(*(x.curFile));
43 isOnBoardRecorder = x.isOnBoardRecorder;
44 imes = x.imes;
45 tStart = x.tStart;
46 tEnd = x.tEnd;
47 tBlock0 = x.tBlock0;
48 perEch = x.perEch;
49 trigMask = x.trigMask;
50 infos = x.infos;
51
52 if (x.file)
53 file = new ArcheopsFile(*x.file);
54 else
55 file = NULL;
56
57 if (x.rawIter) {
58 rawIter = new TOIIter(*x.rawIter);
59 interp = new TOIInterpolator[infos.size()];
60 for (int i=0; i<infos.size(); i++)
61 interp[i] = x.interp[i];
62 } else {
63 rawIter = NULL;
64 interp = NULL;
65 }
66
67 lastSample = x.lastSample;
68 maxLookAhead = x.maxLookAhead;
69}
70
71TOIIter::~TOIIter() {
72 delete file;
73 delete rawIter;
74 delete[] interp;
75}
76
77#ifdef __MWERKS__
78#define filesep ':'
79#else
80#define filesep '/'
81#endif
82
83void TOIIter::Init() {
84 // On a soit un repertoire, soit un fichier, soit une liste de fichiers....
85 if (directory == "") {
86 if (files.empty()) { // un seul fichier
87 file = new ArcheopsFile(fileName);
88 } else {
89 curFile = files.begin();
90 file = new ArcheopsFile((*curFile).c_str());
91 }
92 } else { // On a un repertoire a explorer
93 // On cherche soit les fichiers dans le repertoire donne, soit des fichiers
94 // dans un sous-repertoire "arch-YY_MM_DD". Les fichiers ont un nom en
95 // "hYY_MM_DD-hh_mm_ss".
96 // Pour l'enregistreur de vol, les fichiers ont un nom en ARKxxxxxx.DAT
97 if (directory[directory.length()-1] != filesep)
98 directory += filesep;
99 DIR* dir = opendir(directory.c_str());
100 struct dirent* ent;
101 while ((ent = readdir(dir)) != NULL) {
102 // si c'est un repertoire, avec un nom de jour, il faut l'explorer...
103 if (!strncmp(ent->d_name, "arch-", 5)) {
104 double mjd = ArcheopsFile::decodeMJD(ent->d_name+5) - 2./24.; // ENTIER + .5 en temps local!
105 if (mjd >= tStart - 1. && mjd <= tEnd) {
106 string direc2 = directory + ent->d_name + filesep;
107 DIR* dir2 = opendir(direc2.c_str());
108 struct dirent* ent2;
109 while ((ent2 = readdir(dir2)) != NULL) {
110 if (*ent2->d_name == 'h') {
111 double mjd2 = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.;
112 if (mjd2 >= tStart - 1./24. && mjd2 <= tEnd) {
113 files.insert(direc2 + ent2->d_name);
114 }
115 }
116 }
117 }
118 } else {
119 if (!isOnBoardRecorder && *ent->d_name == 'h') {
120 double mjd = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.; // $CHECK$ UTCOffset
121 if (mjd >= tStart - 1./24. && mjd <= tEnd) {
122 files.insert(directory + ent->d_name);
123 }
124 } else if (isOnBoardRecorder) {
125 if (strncmp(ent->d_name, "ARK", 3)) continue;
126 char * sfx = ent->d_name + strlen(ent->d_name) - 4;
127 if (strcmp(sfx, ".DAT")) continue;
128 files.insert(directory + ent->d_name);
129 }
130 }
131 }
132 closedir(dir);
133 curFile = files.begin();
134 file = new ArcheopsFile((*curFile).c_str());
135 }
136
137 // On avance jusqu'a avoir au moins un bloc param et un bloc reglage,
138 // car on ne peut rien faire sans...
139 // Si on a des donnees de l'enregistreur de vol, pas de bloc param, et
140 // on en simule un
141 if (!file->lastParam()) {
142 if (isOnBoardRecorder) {
143 extern param_bolo parametr;
144 block_type_param block;
145 block.param = parametr;
146 valide_block((block_type_modele*)&block, block_param, 0);
147 file->forceBlock((block_type_modele*)&block);
148 } else {
149 file->nextBlock(block_param_mask);
150 }
151 }
152 if (!file->lastReglage()) file->nextBlock(block_reglage_mask);
153
154 // On cherche un bloc GPS pour avoir la correspondance timestamp/UTC.
155 // Pour le moment, on se fonde sur le premier bloc GPS. On pourra faire
156 // mieux en prenant le min de tous les delta_T, a condition d'avoir un
157 // peu plus de details sur la facon dont le GPS est lu.
158
159 if (tBlock0 < 0) {
160 tBlock0 = file->getStartMJD();
161
162 file->pushMark();
163 if (file->lastGPS() || file->nextBlock(block_gps_mask)) {
164 // le temps du bloc courant, en secondes
165 double dt = file->blockNum() * file->perBlock();
166 tBlock0 = file->getGPSMJD() - dt/86400.;
167 } else { // pas de bloc GPS...
168 tBlock0 = file->getStartMJD();
169 }
170 file->popMark();
171 }
172
173 if (perEch < 0)
174 perEch = file->perEchant();
175
176 bool hasInterp = false;
177
178 trigMask = 0;
179 for (vector<info>::iterator i = infos.begin(); i != infos.end(); i++) {
180 if ((*i).interpolated) hasInterp = true;
181 if ((*i).triggering) {
182 switch ((*i).kind) {
183 case boloTens:
184 case boloRaw:
185 trigMask |= block_bolo_mask;
186 break;
187 case gpsTime:
188 case longitude:
189 case latitude:
190 case altitude:
191 trigMask |= block_gps_mask;
192 break;
193 case azimut:
194 file->needSSTProcessMask(SSTHandler::findPeriod);
195 trigMask |= block_sst_mask;
196 break;
197 case sstStarZ:
198 case sstStarF:
199 file->needSSTProcessMask(SSTHandler::findStars);
200 trigMask |= block_sst_mask;
201 break;
202 case sstSignal:
203 file->needSSTProcessMask(SSTHandler::rmveOffset);
204 trigMask |= block_sst_mask;
205 break;
206 case sstRaw:
207 trigMask |= block_sst_mask;
208 break;
209 case gyroRaw:
210 trigMask |= block_gyro_mask;
211 break;
212 case alphaAxis:
213 case deltaAxis:
214 case alphaBolo:
215 case deltaBolo:
216 file->needSSTProcessMask(SSTHandler::findAxis);
217 trigMask |= block_sst_mask;
218 break;
219 }
220 }
221 }
222
223 if (trigMask & (block_bolo_mask | block_sst_mask)) {
224 imes = 9999;
225 } else {
226 imes = 0;
227 }
228
229 if (hasInterp) {
230 rawIter = new TOIIter(*this);
231 interp = new TOIInterpolator[infos.size()];
232 for (int i=0; i<infos.size(); i++) {
233 rawIter->infos[i].interpolated = false;
234 }
235 delete file; file = NULL; // on ne travaille plus sur le fichier directement...
236 }
237}
238
239bool TOIIter::NextFile() {
240 if (rawIter)
241 return rawIter->NextFile();
242
243 if (files.empty()) {
244 return false;
245 } else {
246 if (curFile == files.end()) return false;
247 curFile++;
248 if (curFile == files.end()) return false;
249 cout << "opening file " << (*curFile).c_str() << endl;
250 ArcheopsFile* newfile = new ArcheopsFile((*curFile).c_str());
251 newfile->grabLastBlocs(*file);
252 delete file;
253 file = newfile;
254 return true;
255 }
256}
257
258bool TOIIter::Next() {
259 if (rawIter) { // Delegation pour interpolation
260 // Trouve prochain sample disponible
261 for (int k=0; k<2; k++) {
262 long smp = 2147483647L;
263 for (int i=0; i<infos.size(); i++) {
264 long ss = interp[i].nextSample(lastSample+1);
265 if (ss > 0 && ss < smp) smp=ss;
266 }
267 if (smp != 2147483647L) {
268 lastSample = smp;
269 break;
270 }
271 if (!fetchAhead()) // tout le monde etait en bout de course,
272 return false; // on lit un echantillon, ca suffit, d'ou le k<2
273 }
274 // Verifie que tous les interpolateurs ont assez de donnees pour
275 // trouver la valeur correspondante
276 for (int i=0; i<infos.size(); i++) {
277 //rif (infos[i].interpolated)
278 while (interp[i].needMoreDataFor(lastSample) &&
279 rawIter->getSampleIndex() - lastSample < maxLookAhead)
280 if (!fetchAhead()) return false;
281 }
282
283 // On est pret...
284 return true;
285 }
286
287 // trigger sur info indexee dans bloc bolo ou bloc sst ?
288 if (trigMask & (block_bolo_mask | block_sst_mask)) {
289 imes++;
290 if (imes < file->nEchBlock()) return true;
291 imes = 0;
292 }
293
294 // soit pas d'info indexee, soit fin bloc courant...
295 while (1) {
296 if (file->nextBlock(trigMask)) {
297 while (file->sameBlockNumAhead()) { // tant que meme numero de bloc, on lit
298 if (!file->nextBlock()) { // fin de fichier ?
299 if (NextFile()) file->nextBlock();
300 else break;
301 }
302 }
303 return true;
304 }
305 if (!NextFile()) return false;
306 }
307}
308
309/* double TOIIter::getTime() { // MJD
310 // le temps du bloc courant, en secondes
311 double dt = file->blockNum() * file->perBlock();
312 return tBlock0 + dt/86400. + imes*file->perEchant()/86400.;
313 }
314 */
315
316bool TOIIter::canGetValue(int column) {
317 if (column < 0 || column >= infos.size()) return false;
318 if (rawIter) {
319 return interp[column].canGet(lastSample);
320 }
321 TOIKind kind = infos[column].kind;
322 int index = infos[column].index;
323 switch (kind) {
324 case sampleNum:
325 case internalTime:
326 case utc:
327 return true;
328 case boloTens:
329 if (imes==0 && file->llastBolo()==NULL) return false;
330 return file->lastBolo() != NULL;
331 case boloRaw:
332 return file->lastBolo() != NULL;
333 case sstSignal:
334 case sstRaw:
335 return file->lastSST() != NULL;
336 case sstStarZ:
337 case sstStarF:
338 return (file->lastSST() != NULL) && (file->getSSTStarZ(index, imes) >= 0);
339 case gyroRaw:
340 return (file->lastGyro() != NULL);
341 case gpsTime:
342 return file->hasGPSTime();
343 case longitude:
344 case latitude:
345 return file->hasGPSPos();
346 case altitude:
347 return file->hasGPSAlt();
348 case azimut:
349 case alphaAxis:
350 case deltaAxis:
351 return (file->lastGPS() != NULL && file->lastSST() != NULL);
352 }
353 return false;
354}
355
356double TOIIter::getValue(int column) {
357 if (column < 0 || column >= infos.size()) return -1;
358 if (rawIter) {
359 if (infos[column].interpolated)
360 return interp[column].getIValue(lastSample);
361 else
362 return interp[column].getEValue(lastSample);
363 }
364 TOIKind kind = infos[column].kind;
365 int index = infos[column].index;
366 switch (kind) {
367 case sampleNum:
368 return file->blockNum() * file->nEchBlock() + imes;
369 case internalTime:
370 //return (file->blockNum() * file->nEchBlock() + imes) * file->perEchant();
371 return (file->blockNum() * file->nEchBlock() + imes) * perEch;
372 case utc:
373 /* printf("utc: %d %d %g %g %g\n",file->blockNum(),
374 (file->blockNum() * file->nEchBlock() + imes),
375 file->perEchant(),
376 (file->blockNum() * file->nEchBlock() + imes) * file->perEchant()/86400.,
377 tBlock0+(file->blockNum() * file->nEchBlock() + imes) * file->perEchant()/86400.); */
378 //return tBlock0+(file->blockNum() * file->nEchBlock() + imes) * file->perEchant()/86400.;
379 return tBlock0+(file->blockNum() * file->nEchBlock() + imes) * perEch/86400.;
380 case boloTens:
381 return file->getMuVBolo(index, imes);
382 case boloRaw:
383 return file->getRawBolo(index, imes);
384 case sstSignal:
385 return file->getSSTSignal(index, imes);
386 case sstRaw:
387 return file->getSSTRawSignal(index, imes);
388 case sstStarZ:
389 return file->getSSTStarZ(index, imes);
390 case sstStarF:
391 return file->getSSTStarF(index, imes);
392 case gyroRaw:
393 return file->getGyro(index, imes);
394 case gpsTime:
395 return file->getGPSUTC();
396 case longitude:
397 return file->getGPSLong();
398 case latitude:
399 return file->getGPSLat();
400 case altitude:
401 return file->getGPSAlt();
402 case azimut:
403 return file->getAzimut(imes);
404 case alphaAxis:
405 return file->getAlpha(imes);
406 case deltaAxis:
407 return file->getDelta(imes);
408 }
409 return -1;
410}
411
412bool TOIIter::newValue(int column) {
413 if (column < 0 || column >= infos.size()) return false;
414 if (rawIter) {
415 return interp[column].isNewValue(lastSample);
416 }
417 TOIKind kind = infos[column].kind;
418 switch (kind) {
419 case sampleNum:
420 case internalTime:
421 case utc:
422 return true;
423 case boloTens:
424 return file->blockNum() == file->getBoloBlockNum();
425 case boloRaw:
426 return file->blockNum() == file->getBoloBlockNum();
427 case sstRaw:
428 case sstSignal:
429 case sstStarZ:
430 case sstStarF:
431 return file->blockNum() == file->getSSTBlockNum();
432 case gyroRaw:
433 return file->blockNum() == file->getGyroBlockNum();
434 case gpsTime:
435 return file->blockNum() == file->getGPSBlockNum() && imes==0;
436 case longitude:
437 return file->blockNum() == file->getGPSBlockNum() && imes==0;
438 case latitude:
439 return file->blockNum() == file->getGPSBlockNum() && imes==0;
440 case altitude:
441 return file->blockNum() == file->getGPSBlockNum() && imes==0;
442 case azimut:
443 return true; // $CHECK$ with SSTHandler
444 case alphaAxis:
445 return true; // $CHECK$ with SSTHandler
446 case deltaAxis:
447 return true; // $CHECK$ with SSTHandler
448 }
449 return false;
450}
451
452bool TOIIter::extendValue(int column) {
453 return (!infos[column].interpolated && !newValue(column));
454}
455
456bool TOIIter::interpValue(int column) {
457 return (infos[column].interpolated && !newValue(column));
458}
459
460bool TOIIter::isTrig(int column) {
461 if (column < 0 || column >= infos.size()) return false;
462 return infos[column].triggering;
463}
464
465
466TOIKind TOIIter::getKind(int column) {
467 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
468 return infos[column].kind;
469}
470
471int TOIIter::getIndex(int column) {
472 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
473 return infos[column].index;
474}
475
476int TOIIter::getColTOI(TOIKind kind, int index) {
477 for (int i=0; i<infos.size(); i++)
478 if (infos[i].kind == kind && infos[i].index == index)
479 return i;
480 return -1;
481}
482
483bool TOIIter::canGetTOI(TOIKind kind, int index) {
484 int col = getColTOI(kind, index);
485 if (col<0) return false;
486 return canGetValue(col);
487}
488
489double TOIIter::getTOI(TOIKind kind, int index) {
490 int col = getColTOI(kind, index);
491 if (col<0) return -9.e99;
492 return getValue(col);
493}
494
495
496int TOIIter::getBlockSampleIndex() {
497 return imes;
498}
499
500int TOIIter::getSampleIndex() {
501 if (file) {
502 return file->blockNum() * file->nEchBlock() + imes;
503 } else {
504 return lastSample;
505 }
506}
507
508bool TOIIter::fetchAhead() { // Seulement si delegation
509 if (!rawIter) return false;
510 if (!rawIter->Next()) return false;
511 long sample = rawIter->getSampleIndex();
512 for (int i=0; i<infos.size(); i++) {
513 if (rawIter->canGetValue(i) && rawIter->newValue(i)) {
514 interp[i].enterValue(rawIter->getValue(i), sample);
515 }
516 }
517 return true;
518}
519
520block_type_param* TOIIter::lastParam() {
521 if (file) return file->lastParam();
522 else return rawIter->lastParam();
523}
524
525
Note: See TracBrowser for help on using the repository browser.