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

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

Archeops 24, gestion byte-swap

File size: 10.3 KB
Line 
1#include "toiiter.h"
2#include "toiinterpolator.h"
3#include <dirent.h>
4
5// Format bloc GPS
6// $GPGGA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,
7
8TOIIter::TOIIter() {
9 // Toutes les valeurs initialisees par defaut. Le TOISvr les positionnera,
10 // puis lancera l'initialisation
11
12 file = NULL;
13 directory = "";
14 fileName = "";
15
16 tStart = -9.e99;
17 tEnd = -9.e99;
18
19 tBlock0 = -1;
20
21 rawIter = NULL;
22 interp = NULL;
23 lastSample = -1;
24 maxLookAhead = 1000;
25
26}
27
28TOIIter::TOIIter(TOIIter const& x) {
29 directory = x.directory;
30 fileName = x.fileName;
31 files = x.files;
32 curFile = x.curFile;
33 imes = x.imes;
34 tStart = x.tStart;
35 tEnd = x.tEnd;
36 tBlock0 = x.tBlock0;
37 trigMask = x.trigMask;
38 infos = x.infos;
39
40 if (x.file)
41 file = new ArcheopsFile(*x.file);
42 else
43 file = NULL;
44
45 if (x.rawIter) {
46 rawIter = new TOIIter(*x.rawIter);
47 interp = new TOIInterpolator[infos.size()];
48 for (int i=0; i<infos.size(); i++)
49 interp[i] = x.interp[i];
50 } else {
51 rawIter = NULL;
52 interp = NULL;
53 }
54
55 lastSample = x.lastSample;
56 maxLookAhead = x.maxLookAhead;
57}
58
59TOIIter::~TOIIter() {
60 delete file;
61 delete rawIter;
62 delete[] interp;
63}
64
65void TOIIter::Init() {
66
67 // $CHECK$ a deplacer, pour gerer repertoires...
68 if (directory == "") {
69 file = new ArcheopsFile(fileName);
70 } else {
71 DIR* dir = opendir(directory.c_str());
72 struct dirent* ent;
73 while ((ent = readdir(dir)) != NULL) {
74 files.insert(ent->d_name);
75 }
76 closedir(dir);
77 curFile = files.begin();
78 file = new ArcheopsFile((*curFile).c_str());
79 }
80
81 if (!file->lastParam()) file->nextBlock(block_param_mask);
82 if (!file->lastReglage()) file->nextBlock(block_reglage_mask);
83
84 // On cherche un bloc GPS pour avoir la correspondance timestamp/UTC.
85 // Pour le moment, on se fonde sur le premier bloc GPS. On pourra faire
86 // mieux en prenant le min de tous les delta_T.
87
88 tBlock0 = file->getStartMJD();
89
90 file->pushMark();
91 if (file->lastGPS() || file->nextBlock(block_gps_mask)) {
92 // le temps du bloc courant, en secondes
93 double dt = file->blockNum() * file->perBlock();
94 tBlock0 = file->getGPSMJD() - dt/86400.;
95 } else { // pas de bloc GPS...
96 tBlock0 = file->getStartMJD();
97 }
98 file->popMark();
99
100 bool hasInterp = false;
101
102 trigMask = 0;
103 for (vector<info>::iterator i = infos.begin(); i != infos.end(); i++) {
104 if ((*i).interpolated) hasInterp = true;
105 if ((*i).triggering) {
106 switch ((*i).kind) {
107 case boloTens:
108 case boloRaw:
109 trigMask |= block_bolo_mask;
110 break;
111 case longitude:
112 case latitude:
113 trigMask |= block_gps_mask;
114 break;
115 case azimut:
116 file->needSSTProcessMask(SSTHandler::findPeriod);
117 trigMask |= block_sst_mask;
118 break;
119 case sstStarZ:
120 case sstStarF:
121 file->needSSTProcessMask(SSTHandler::findStars);
122 trigMask |= block_sst_mask;
123 break;
124 case sstSignal:
125 file->needSSTProcessMask(SSTHandler::rmveOffset);
126 trigMask |= block_sst_mask;
127 break;
128 case alphaAxis:
129 case deltaAxis:
130 case alphaBolo:
131 case deltaBolo:
132 file->needSSTProcessMask(SSTHandler::findAxis);
133 trigMask |= block_sst_mask;
134 break;
135 }
136 }
137 }
138
139 if (trigMask & (block_bolo_mask | block_sst_mask)) {
140 imes = 9999;
141 } else {
142 imes = 0;
143 }
144
145 if (hasInterp) {
146 rawIter = new TOIIter(*this);
147 interp = new TOIInterpolator[infos.size()];
148 for (int i=0; i<infos.size(); i++) {
149 rawIter->infos[i].interpolated = false;
150 }
151 delete file; file = NULL; // on ne travaille plus sur le fichier directement...
152 }
153}
154
155bool TOIIter::NextFile() {
156 if (rawIter)
157 return rawIter->NextFile();
158
159 if (directory == "") {
160 return false;
161 } else {
162 if (curFile == files.end()) return false;
163 curFile++;
164 if (curFile == files.end()) return false;
165 ArcheopsFile* newfile = new ArcheopsFile((*curFile).c_str());
166 newfile->grabLastBlocs(*file);
167 delete file;
168 file = newfile;
169 return true;
170 }
171}
172
173bool TOIIter::Next() {
174 if (rawIter) { // Delegation pour interpolation
175 // Trouve prochain sample disponible
176 for (int k=0; k<2; k++) {
177 long smp = 2147483647L;
178 for (int i=0; i<infos.size(); i++) {
179 long ss = interp[i].nextSample(lastSample+1);
180 if (ss > 0 && ss < smp) smp=ss;
181 }
182 if (smp != 2147483647L) {
183 lastSample = smp;
184 break;
185 }
186 if (!fetchAhead()) // tout le monde etait en bout de course,
187 return false; // on lit un echantillon, ca suffit, d'ou le k<2
188 }
189 // Verifie que tous les interpolateurs ont assez de donnees pour
190 // trouver la valeur correspondante
191 for (int i=0; i<infos.size(); i++) {
192 //rif (infos[i].interpolated)
193 while (interp[i].needMoreDataFor(lastSample) &&
194 rawIter->getSampleIndex() - lastSample < maxLookAhead)
195 if (!fetchAhead()) return false;
196 }
197
198 // On est pret...
199 return true;
200 }
201
202 // trigger sur info indexee dans bloc bolo ou bloc sst ?
203 if (trigMask & (block_bolo_mask | block_sst_mask)) {
204 imes++;
205 if (imes < file->nEchBlock()) return true;
206 imes = 0;
207 }
208
209 // soit pas d'info indexee, soit fin bloc courant...
210 while (1) {
211 if (file->nextBlock(trigMask)) {
212 while (file->sameBlockNumAhead()) { // tant que meme numero de bloc, on lit
213 if (!file->nextBlock()) { // fin de fichier ?
214 if (NextFile()) file->nextBlock();
215 else break;
216 }
217 }
218 return true;
219 }
220 if (!NextFile()) return false;
221 }
222}
223
224/* double TOIIter::getTime() { // MJD
225 // le temps du bloc courant, en secondes
226 double dt = file->blockNum() * file->perBlock();
227 return tBlock0 + dt/86400. + imes*file->perEchant()/86400.;
228 }
229 */
230
231bool TOIIter::canGetValue(int column) {
232 if (column < 0 || column >= infos.size()) return false;
233 if (rawIter) {
234 return interp[column].canGet(lastSample);
235 }
236 TOIKind kind = infos[column].kind;
237 int index = infos[column].index;
238 switch (kind) {
239 case sampleNum:
240 case internalTime:
241 case utc:
242 return true;
243 case boloTens:
244 case boloRaw:
245 return file->lastBolo() != NULL;
246 case sstSignal:
247 return file->lastSST() != NULL;
248 case longitude:
249 case latitude:
250 return file->lastGPS() != NULL;
251 case azimut:
252 case alphaAxis:
253 case deltaAxis:
254 return (file->lastGPS() != NULL && file->lastSST() != NULL);
255 }
256 return false;
257}
258
259double TOIIter::getValue(int column) {
260 if (column < 0 || column >= infos.size()) return -1;
261 if (rawIter) {
262 if (infos[column].interpolated)
263 return interp[column].getIValue(lastSample);
264 else
265 return interp[column].getEValue(lastSample);
266 }
267 TOIKind kind = infos[column].kind;
268 int index = infos[column].index;
269 switch (kind) {
270 case sampleNum:
271 return file->blockNum() * file->nEchBlock() + imes;
272 case internalTime:
273 return (file->blockNum() * file->nEchBlock() + imes) * file->perEchant();
274 case utc:
275 /* printf("utc: %d %d %g %g %g\n",file->blockNum(),
276 (file->blockNum() * file->nEchBlock() + imes),
277 file->perEchant(),
278 (file->blockNum() * file->nEchBlock() + imes) * file->perEchant()/86400.,
279 tBlock0+(file->blockNum() * file->nEchBlock() + imes) * file->perEchant()/86400.); */
280 return tBlock0+(file->blockNum() * file->nEchBlock() + imes) * file->perEchant()/86400.;
281 case boloTens:
282 return file->getMuVBolo(index, imes);
283 case boloRaw:
284 return file->getRawBolo(index, imes);
285 case sstSignal:
286 return file->getSSTSignal(index, imes);
287 case longitude:
288 return file->getGPSLong(); // $CHECK$ TBD gerer interpolation (dans file)
289 case latitude:
290 return file->getGPSLat(); // $CHECK$ TBD gerer interpolation (dans file)
291 case azimut:
292 return file->getAzimut(imes);
293 case alphaAxis:
294 return file->getAlpha(imes);
295 case deltaAxis:
296 return file->getDelta(imes);
297 }
298 return -1;
299}
300
301bool TOIIter::newValue(int column) {
302 if (column < 0 || column >= infos.size()) return false;
303 TOIKind kind = infos[column].kind;
304 switch (kind) {
305 case sampleNum:
306 case internalTime:
307 case utc:
308 return true;
309 case boloTens:
310 return file->blockNum() == file->getBoloBlockNum();
311 case boloRaw:
312 return file->blockNum() == file->getBoloBlockNum();
313 case sstSignal:
314 return file->blockNum() == file->getSSTBlockNum();
315 case longitude:
316 return file->blockNum() == file->getGPSBlockNum() && imes==0;
317 case latitude:
318 return file->blockNum() == file->getGPSBlockNum() && imes==0;
319 case azimut:
320 return true; // $CHECK$ with SSTHandler
321 case alphaAxis:
322 return true; // $CHECK$ with SSTHandler
323 case deltaAxis:
324 return true; // $CHECK$ with SSTHandler
325 }
326 return false;
327}
328
329bool TOIIter::extendValue(int column) {
330 return (!infos[column].interpolated && !newValue(column));
331}
332
333bool TOIIter::interpValue(int column) {
334 return (infos[column].interpolated && !newValue(column));
335}
336
337TOIKind TOIIter::getKind(int column) {
338 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
339 return infos[column].kind;
340}
341
342int TOIIter::getIndex(int column) {
343 if (column < 0 || column >= infos.size()) return (TOIKind)-1;
344 return infos[column].index;
345}
346
347int TOIIter::getColTOI(TOIKind kind, int index) {
348 for (int i=0; i<infos.size(); i++)
349 if (infos[i].kind == kind && infos[i].index == index)
350 return i;
351 return -1;
352}
353
354bool TOIIter::canGetTOI(TOIKind kind, int index) {
355 int col = getColTOI(kind, index);
356 if (col<0) return false;
357 return canGetValue(col);
358}
359
360double TOIIter::getTOI(TOIKind kind, int index) {
361 int col = getColTOI(kind, index);
362 if (col<0) return -9.e99;
363 return getValue(col);
364}
365
366
367int TOIIter::getBlockSampleIndex() {
368 return imes;
369}
370
371int TOIIter::getSampleIndex() {
372 return file->blockNum() * file->nEchBlock() + imes;
373}
374
375bool TOIIter::fetchAhead() { // Seulement si delegation
376 if (!rawIter) return false;
377 if (!rawIter->Next()) return false;
378 long sample = rawIter->getSampleIndex();
379 for (int i=0; i<infos.size(); i++) {
380 if (rawIter->canGetValue(i) && rawIter->newValue(i))
381 interp[i].enterValue(rawIter->getValue(i), sample);
382 }
383 return true;
384}
385
Note: See TracBrowser for help on using the repository browser.