1 | // archfileset.cc
|
---|
2 | // Eric Aubourg CEA/DAPNIA/SPP septembre 1999
|
---|
3 |
|
---|
4 | #include <iostream.h>
|
---|
5 | #include <dirent.h>
|
---|
6 | #include "archfileset.h"
|
---|
7 | #include "archparam.h"
|
---|
8 | #include "toiproducer.h"
|
---|
9 | #include "toimanager.h"
|
---|
10 |
|
---|
11 | ArchFileSet::ArchFileSet() {
|
---|
12 | file = NULL;
|
---|
13 |
|
---|
14 | trigMask = block_reglage_mask | block_param_mask | TOIManager::trigMask;
|
---|
15 |
|
---|
16 | sStart = -999999999;
|
---|
17 | sEnd = 999999999;
|
---|
18 | mjdStart = -999999999;
|
---|
19 | mjdEnd = 999999999;
|
---|
20 |
|
---|
21 | initDone = false;
|
---|
22 | endFound = false;
|
---|
23 | }
|
---|
24 |
|
---|
25 | ArchFileSet::ArchFileSet(ArchFileSet const& x) {
|
---|
26 | directories = x.directories;
|
---|
27 | filenames = x.filenames;
|
---|
28 | if (x.initDone && x.curFile != x.filenames.end()) curFile = filenames.find(*(x.curFile));
|
---|
29 |
|
---|
30 | trigMask = x.trigMask;
|
---|
31 |
|
---|
32 | sStart = x.sStart;
|
---|
33 | sEnd = x.sEnd;
|
---|
34 | mjdStart = x.mjdStart;
|
---|
35 | mjdEnd = x.mjdEnd;
|
---|
36 |
|
---|
37 | if (x.file)
|
---|
38 | file = new ArcheopsFile(*x.file);
|
---|
39 | else
|
---|
40 | file = NULL;
|
---|
41 |
|
---|
42 | initDone = x.initDone;
|
---|
43 | endFound = x.endFound;
|
---|
44 | }
|
---|
45 |
|
---|
46 | ArchFileSet::~ArchFileSet() {
|
---|
47 | delete file;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void ArchFileSet::setSNumRange(long start, long end) {
|
---|
51 | sStart = start;
|
---|
52 | sEnd = end;
|
---|
53 | }
|
---|
54 |
|
---|
55 | void ArchFileSet::setMJDRange(double start, double end) {
|
---|
56 | mjdStart = start;
|
---|
57 | mjdEnd = end;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void ArchFileSet::addFile(string fn) {
|
---|
61 | filenames.insert(fn);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void ArchFileSet::addDirectory(string fn) {
|
---|
65 | directories.insert(fn);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | #ifdef __MWERKS__
|
---|
71 | #pragma mark -
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | #ifdef __MWERKS__
|
---|
75 | #define filesep ':'
|
---|
76 | #else
|
---|
77 | #define filesep '/'
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | void ArchFileSet::init() {
|
---|
81 | if (initDone) return;
|
---|
82 | initDone = true;
|
---|
83 | trigMask |= TOIManager::trigMask;
|
---|
84 |
|
---|
85 | // build files from directories
|
---|
86 | for (set<string>::iterator i = directories.begin(); i != directories.end(); i++) {
|
---|
87 | string directory = *i;
|
---|
88 | if (directory[directory.length()-1] != filesep)
|
---|
89 | directory += filesep;
|
---|
90 |
|
---|
91 | DIR* dir = opendir(directory.c_str());
|
---|
92 | struct dirent* ent;
|
---|
93 |
|
---|
94 | while ((ent = readdir(dir)) != NULL) {
|
---|
95 | // si c'est un repertoire, avec un nom de jour, il faut l'explorer...
|
---|
96 | if (!strncmp(ent->d_name, "arch-", 5)) {
|
---|
97 | double mjd = ArcheopsFile::decodeMJD(ent->d_name+5) - 2./24.; // ENTIER + .5 en temps local!
|
---|
98 | if (mjd >= mjdStart - 1. && mjd <= mjdEnd) {
|
---|
99 | string direc2 = directory + ent->d_name + filesep;
|
---|
100 | DIR* dir2 = opendir(direc2.c_str());
|
---|
101 | struct dirent* ent2;
|
---|
102 | while ((ent2 = readdir(dir2)) != NULL) {
|
---|
103 | if (*ent2->d_name == 'h') {
|
---|
104 | double mjd2 = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.;
|
---|
105 | if (mjd2 >= mjdStart - 1./24. && mjd2 <= mjdEnd) {
|
---|
106 | filenames.insert(direc2 + ent2->d_name);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | } // "arch-"
|
---|
112 | else if (*ent->d_name == 'h') {
|
---|
113 | double mjd = ArcheopsFile::decodeMJD(ent->d_name+1) - 2./24.; // $CHECK$ UTCOffset
|
---|
114 | if (mjd >= mjdStart - 1./24. && mjd <= mjdEnd) {
|
---|
115 | filenames.insert(directory + ent->d_name);
|
---|
116 | }
|
---|
117 | } // "h*"
|
---|
118 | else if (!strncmp(ent->d_name, "ARK", 3) || !strncmp(ent->d_name, "ark", 3)) {
|
---|
119 | char * sfx = ent->d_name + strlen(ent->d_name) - 4;
|
---|
120 | if (!strcmp(sfx, ".DAT") || !strcmp(sfx, ".dat")) {
|
---|
121 | filenames.insert(directory + ent->d_name);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | } // readdir
|
---|
125 | closedir(dir);
|
---|
126 | } // iterate on dirs
|
---|
127 |
|
---|
128 | scanFiles();
|
---|
129 |
|
---|
130 | if (filenames.empty()) {
|
---|
131 | throw ArchExc("No files in fileset");
|
---|
132 | }
|
---|
133 |
|
---|
134 | curFile = filenames.begin();
|
---|
135 | file = new ArcheopsFile((*curFile).c_str());
|
---|
136 | cout << "opening file " << (*curFile).c_str() << endl;
|
---|
137 |
|
---|
138 | extern param_bolo parametr;
|
---|
139 | block_type_param block;
|
---|
140 | block.param = parametr;
|
---|
141 | valide_block((block_type_modele*)&block, block_param, 0);
|
---|
142 | file->forceBlock((block_type_modele*)&block);
|
---|
143 |
|
---|
144 | if (!file->lastReglage()) file->nextBlock(block_reglage_mask);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void ArchFileSet::scanFiles() {
|
---|
148 | file1stSamp.clear();
|
---|
149 | cout << "Scanning all files" << endl;
|
---|
150 | // Petite astuce pour les STL non conformes comme celles de digital
|
---|
151 | // qui ne supportent pas files.erase(i) suivi de i++....
|
---|
152 | set<string> copy = filenames;
|
---|
153 | for (set<string>::iterator i = copy.begin(); i != copy.end(); i++) {
|
---|
154 | ArcheopsFile fich((*i).c_str());
|
---|
155 | if (fich.nextBlock()) {
|
---|
156 | file1stSamp[*i] = fich.blockNum()*72; // premier numsample
|
---|
157 | cout << "File " << *i << " 1st sample = " << fich.blockNum()*72 << endl;
|
---|
158 | } else {
|
---|
159 | cout << "File " << *i << " unrecoverable, skipping" << endl;
|
---|
160 | filenames.erase(*i);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | cout << "Scan done" << endl;
|
---|
164 |
|
---|
165 | // Et maintenant, on ne garde que ceux qui tombent dans l'intervalle...
|
---|
166 | copy = filenames;
|
---|
167 | string prev="";
|
---|
168 | for (set<string>::iterator i = copy.begin(); i != copy.end(); i++) {
|
---|
169 | double smp = file1stSamp[*i];
|
---|
170 | double t = archParam.acq.tBlock0 + smp * archParam.acq.perEch/86400.;
|
---|
171 | if (t>mjdEnd || smp>sEnd) { // premier echantillon apres tEnd
|
---|
172 | filenames.erase(*i);
|
---|
173 | prev = "";
|
---|
174 | continue;
|
---|
175 | }
|
---|
176 | if (t<mjdStart || smp<sStart) { // premier echantillon avant tStart -> on vire le precedent si existe
|
---|
177 | if (prev != "") {
|
---|
178 | filenames.erase(prev);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | prev = *i;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | bool ArchFileSet::nextFile() {
|
---|
186 | if (filenames.empty()) {
|
---|
187 | return false;
|
---|
188 | } else {
|
---|
189 | if (curFile == filenames.end()) return false;
|
---|
190 | curFile++;
|
---|
191 | if (curFile == filenames.end()) return false;
|
---|
192 | cout << "opening file " << (*curFile).c_str() << endl;
|
---|
193 | ArcheopsFile* newfile = new ArcheopsFile((*curFile).c_str());
|
---|
194 | newfile->grabLastBlocs(*file);
|
---|
195 | delete file;
|
---|
196 | file = newfile;
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | bool ArchFileSet::next() {
|
---|
202 | if (endFound) return false;
|
---|
203 | if (!initDone) init();
|
---|
204 | while (1) {
|
---|
205 | long got = nextSample();
|
---|
206 | if (!got) {endFound=true; return false;} // end of files
|
---|
207 | double t = getMJD();
|
---|
208 | if (t < mjdStart) continue;
|
---|
209 | if (t > mjdEnd) {endFound=true; return false;}
|
---|
210 | long s = getSampleIndex();
|
---|
211 | if (s < sStart) continue;
|
---|
212 | if (s > sEnd) {endFound=true; return false;}
|
---|
213 |
|
---|
214 | // On envoie les nouvelles donnees aux producers interesses
|
---|
215 | for (map<TOILowLevProducer*, long>::iterator i = TOIManager::activeLLProducers.begin();
|
---|
216 | i != TOIManager::activeLLProducers.end(); i++) {
|
---|
217 | if (got & ((*i).second)) (*i).first->handleBlock(this);
|
---|
218 | }
|
---|
219 |
|
---|
220 | return true;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | long ArchFileSet::getSampleIndex() {
|
---|
225 | if (!initDone) init();
|
---|
226 | return file->blockNum() * nb_per_block*2;
|
---|
227 | }
|
---|
228 |
|
---|
229 | double ArchFileSet::getMJD() {
|
---|
230 | if (!initDone) init();
|
---|
231 | int sample = getSampleIndex();
|
---|
232 | return archParam.acq.SN2MJD(sample);
|
---|
233 | }
|
---|
234 |
|
---|
235 | long ArchFileSet::nextSample() {
|
---|
236 | while (1) {
|
---|
237 | long got = 0;
|
---|
238 | if (file->nextBlock(trigMask)) {
|
---|
239 | got |= 1 << type_block(file->currentBlock());
|
---|
240 | while (file->sameBlockNumAhead()) { // tant que meme numero de bloc, on lit
|
---|
241 | if (!file->nextBlock()) { // fin de fichier ?
|
---|
242 | if (nextFile()) file->nextBlock(); // fichier suivant
|
---|
243 | else return 0; // tout fini
|
---|
244 | }
|
---|
245 | got |= 1 << type_block(file->currentBlock());
|
---|
246 | }
|
---|
247 | return got;
|
---|
248 | }
|
---|
249 | if (!nextFile()) return 0;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | #ifdef __MWERKS__
|
---|
254 | #pragma mark -
|
---|
255 | #endif
|
---|
256 |
|
---|
257 | block_type_param* ArchFileSet::lastParam() {
|
---|
258 | if (!file) return NULL;
|
---|
259 | return file->lastParam();
|
---|
260 | }
|
---|
261 |
|
---|
262 | block_type_journal* ArchFileSet::lastJournal() {
|
---|
263 | if (!file) return NULL;
|
---|
264 | return file->lastJournal();
|
---|
265 | }
|
---|
266 |
|
---|
267 | block_type_reglage* ArchFileSet::lastReglage() {
|
---|
268 | if (!file) return NULL;
|
---|
269 | return file->lastReglage();
|
---|
270 | }
|
---|
271 |
|
---|
272 | block_type_dilution* ArchFileSet::lastDilution() {
|
---|
273 | if (!file) return NULL;
|
---|
274 | return file->lastDilution();
|
---|
275 | }
|
---|
276 |
|
---|
277 | block_type_gps* ArchFileSet::lastGPS() {
|
---|
278 | if (!file) return NULL;
|
---|
279 | return file->lastGPS();
|
---|
280 | }
|
---|
281 |
|
---|
282 | block_type_une_periode* ArchFileSet::lastUnePeriode() {
|
---|
283 | if (!file) return NULL;
|
---|
284 | return file->lastUnePeriode();
|
---|
285 | }
|
---|
286 |
|
---|
287 | block_type_synchro_sol* ArchFileSet::lastSynchroSol() {
|
---|
288 | if (!file) return NULL;
|
---|
289 | return file->lastSynchroSol();
|
---|
290 | }
|
---|
291 |
|
---|
292 | block_type_pointage_sol* ArchFileSet::lastPointageSol() {
|
---|
293 | if (!file) return NULL;
|
---|
294 | return file->lastPointageSol();
|
---|
295 | }
|
---|
296 |
|
---|
297 | block_type_bolo* ArchFileSet::lastBolo() {
|
---|
298 | if (!file) return NULL;
|
---|
299 | return file->lastBolo();
|
---|
300 | }
|
---|
301 |
|
---|
302 | block_type_gyro* ArchFileSet::lastGyro() {
|
---|
303 | if (!file) return NULL;
|
---|
304 | return file->lastGyro();
|
---|
305 | }
|
---|
306 |
|
---|
307 | block_type_sst* ArchFileSet::lastSST() {
|
---|
308 | if (!file) return NULL;
|
---|
309 | return file->lastSST();
|
---|
310 | }
|
---|
311 |
|
---|
312 | block_type_bolo_comprime* ArchFileSet::lastBoloComp() {
|
---|
313 | if (!file) return NULL;
|
---|
314 | return file->lastBoloComp();
|
---|
315 | }
|
---|
316 |
|
---|
317 | block_type_gyro_comprime* ArchFileSet::lastGyroComp() {
|
---|
318 | if (!file) return NULL;
|
---|
319 | return file->lastGyroComp();
|
---|
320 | }
|
---|
321 |
|
---|
322 | block_type_sst_comprime* ArchFileSet::lastSSTComp() {
|
---|
323 | if (!file) return NULL;
|
---|
324 | return file->lastSSTComp();
|
---|
325 | }
|
---|