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

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

fin fichier

File size: 8.0 KB
Line 
1// toiiter.cc
2// Eric Aubourg CEA/DAPNIA/SPP juillet 1999
3
4#include "ark.h"
5
6#include "toiiter.h"
7#include "toimanager.h"
8#include "toiproducer.h"
9#include "archparam.h"
10#include "asigps.h"
11#include "toiauxgpsproducer.h"
12#include <iostream.h>
13#include <fstream.h>
14
15#define CHK_INITED if (initDone) \
16 throw ArchExc("Trying to modify TOIIter after init done.");
17
18TOIIter::TOIIter() {
19 mjdStart = -999999999;
20 mjdEnd = 999999999;
21 utcStart = -999999999;
22 utcEnd = 999999999;
23 sStart = -999999999;
24 sEnd = 999999999;
25
26 underSample = 1;
27 curSample = -1;
28
29 initDone = false;
30 incDone = false;
31}
32
33#ifdef __MWERKS__
34#pragma mark -
35#endif
36
37void TOIIter::addDirectory(string dir) {
38 CHK_INITED
39 fset.addDirectory(dir);
40}
41
42void TOIIter::addFile(string fn) {
43 CHK_INITED
44 fset.addFile(fn);
45}
46
47void TOIIter::setMJDInterval(double tStart, double tEnd) {
48 CHK_INITED
49 if (tStart>0) mjdStart = tStart;
50 if (tEnd>0) mjdEnd = tEnd;
51}
52
53void TOIIter::setUTCInterval(double tStart, double tEnd) {
54 CHK_INITED
55 if (tStart>0) utcStart = tStart;
56 if (tEnd>0) utcEnd = tEnd;
57}
58
59void TOIIter::setSNInterval(long tStart, long tEnd) {
60 CHK_INITED
61 if (tStart>0) sStart = tStart;
62 if (tEnd>0) sEnd = tEnd;
63}
64
65void TOIIter::setUnderSample(int n) {
66 CHK_INITED
67 if (n<=1) n=1;
68 underSample = n;
69}
70
71int TOIIter::getUnderSample() {
72 return underSample;
73}
74
75void TOIIter::addTOI(TOI& toi, bool trg) {
76 CHK_INITED
77 TOIProducer* prod = TOIManager::findTOIProducer(toi);
78 if (!prod) throw ArchExc("Cannot produce " + toi.fullName());
79 prod->addTOI(toi, this);
80 request.push_back(TOIInfo(toi,prod,trg?1:0));
81}
82
83#ifdef __MWERKS__
84#pragma mark -
85#endif
86
87
88void TOIIter::registerReqHandler(RequestHandler* h) {
89 CHK_INITED
90 handlers.push_back(h);
91}
92
93
94void TOIIter::readReq(istream& str) {
95 CHK_INITED
96 if (!incDone) defaultInclude();
97 string line;
98 while (str) {
99 getline(str,line);
100 if (!str) break;
101 if (line.substr(0,4)=="#END" && (line.length()==4 || line[5] == ' ')) break;
102 if (line[0] != '@' && line[0] != '#') continue;
103 if (!processRequest(line)) {
104 throw ArchExc("Unrecognized directive " + line);
105 }
106 }
107}
108
109
110bool TOIIter::processRequest(string line) {
111 if (line[0] == '#') {
112 int x = line.find(' ');
113 string keyw = line.substr(0, x);
114 string args = (x>0) ? line.substr(x) : string("");
115 bool handled = processOption(keyw,args);
116 for (list<RequestHandler*>::iterator i = handlers.begin();
117 i != handlers.end(); i++) {
118 handled |= (*i)->processOption(keyw,args);
119 }
120 return handled;
121 }
122
123 if (line[0] == '@') {
124 TOI toi(line.substr(1));
125 // if (kind == sampleNum || kind == mjd || kind == mutc) notrig = true;
126 bool handled = processTOIReq(toi,line);
127 for (list<RequestHandler*>::iterator i = handlers.begin();
128 i != handlers.end(); i++) {
129 handled |= (*i)->processTOIReq(toi,line);
130 }
131 return handled;
132 }
133 return false;
134}
135
136
137bool TOIIter::processTOIReq(TOI& toi,string)
138{
139 TOI toi2 = toi;
140 bool trg=true;
141 if (toi.options.find("notrig") != toi.options.end()) {
142 toi2.options.erase("notrig");
143 trg=false;
144 }
145 addTOI(toi2, trg);
146 toi.unit = toi2.unit;
147 return true;
148}
149
150
151bool TOIIter::processOption(string key, string arg)
152{
153 if (arg.length()>0 && arg[0] == ' ') {
154 arg = arg.substr(arg.find_first_not_of(' '));
155 }
156 if (key == "#MJDRANGE") {
157 double tmin, tmax;
158 sscanf(arg.c_str(), "%lg %lg", &tmin, &tmax);
159 setMJDInterval(tmin, tmax);
160 } else if (key == "#UTCRANGE") {
161 double tmin, tmax;
162 sscanf(arg.c_str(), "%lg %lg", &tmin, &tmax);
163 setUTCInterval(tmin, tmax);
164 } else if (key == "#SNRANGE") {
165 long tmin, tmax;
166 sscanf(arg.c_str(), "%ld %ld", &tmin, &tmax);
167 setSNInterval(tmin, tmax);
168 } else if (key == "#PATH") {
169 addDirectory(arg);
170 } else if (key == "#FILE") {
171 addFile(arg);
172 } else if (key == "#UNDERSAMPLE") {
173 setUnderSample(atoi(arg.c_str()));
174 } else if (key == "#MJD0") {
175 double t0;
176 sscanf(arg.c_str(), "%lg", &t0);
177 archParam.acq.tBlock0 = t0;
178 } else if (key == "#UTCORIGIN") {
179 double t0;
180 sscanf(arg.c_str(), "%lg", &t0);
181 archParam.acq.utcOrigin = t0;
182 } else if (key == "#PERECH") {
183 double t0;
184 sscanf(arg.c_str(), "%lg", &t0);
185 archParam.acq.perEch = t0;
186 } else if (key == "#ASIGPS") {
187 ASIGPS* gps = new ASIGPS(arg);
188 TOIManager::registerProducer(new TOIAuxGPSProducer(gps));
189 // gps->FitsDump("GPSDump.fits");
190 } else if (key == "#INCLUDE") {
191 ifstream f(arg.c_str());
192 readReq(f);
193 } else {
194 return false;
195 }
196 return true;
197}
198
199
200void TOIIter::defaultInclude() {
201 incDone = true;
202 processRequest("#REQVERSION V_270999");
203 processRequest("#MJD0 1376.8358818");
204 processRequest("#PERECH 0.005836818076");
205 processRequest("#UTCORIGIN 1376.5");
206 processRequest("#ASIGPS ASI_GPS_archeops1999.ascii");
207 processRequest("#COMMENT Archtoi V3 -- novembre 1999 -- Eric Aubourg CEA/DAPNIA");
208}
209
210#ifdef __MWERKS__
211#pragma mark -
212#endif
213
214void TOIIter::init() {
215 if (initDone) return;
216 initDone = true;
217
218 if (utcStart > 0) {
219 double t = (utcStart/24.) + archParam.acq.utcOrigin;
220 if (t > mjdStart) mjdStart=t;
221 }
222 if (utcEnd > 0) {
223 double t = (utcEnd/24.) + archParam.acq.utcOrigin;
224 if (t < mjdEnd) mjdEnd=t;
225 }
226
227 // Let's add some time on each side, 30 seconds should be ok, at least
228 // for Trapani
229
230 double delT = 30. / 86400;
231 long delSN = long(delT / archParam.acq.perEch);
232
233 fset.setMJDRange(mjdStart-delT, mjdEnd+delT);
234 fset.setSNumRange(sStart-delSN, sEnd+delSN);
235
236 fset.init();
237 //curSample = fset.getSampleIndex();
238}
239
240bool TOIIter::isTrig(int column) {
241 return (request[column].third & triggering) != 0;
242}
243
244TOI TOIIter::getKind(int column) {
245 return request[column].first;
246}
247
248long TOIIter::getSampleNum() {
249 return curSample;
250}
251
252bool TOIIter::next() {
253 if (!initDone) init();
254 for (int ii=0; ii<underSample; ii++)
255 if (!next1()) return false;
256
257 return true;
258}
259
260bool TOIIter::next1() {
261// On tente de produire curSample+1 pour toutes les toi
262// Eventuellement en avancant sur le fichier...
263// Puis on regarde si une TOI triggering a eu une nouvelle
264// valeur.
265// Si on a epuise les fichiers de donnees, on s'arrete des qu'aucune
266// TOI n'a de valeurs apres curSample...
267 if (curSample <= 0) curSample = sStart-1;
268 if (curSample <= 0) {
269 long sn = archParam.acq.MJD2SN(mjdStart) - 1;
270 if (sn > curSample) curSample = sn;
271 }
272
273 if (curSample <= 0) curSample = fset.getSampleIndex()-1;
274
275 if (curSample >= sEnd || archParam.acq.SN2MJD(curSample) >= mjdEnd) return false;
276
277 static long lastClean = 0;
278 if (curSample - lastClean > 100) {
279 for (int i=0; i<request.size(); i++)
280 request[i].second->wontNeedEarlier(request[i].first, this, curSample);
281 lastClean = curSample;
282 }
283
284 bool endFound = false;
285 while(1) {
286 curSample++;
287 for (vector<TOIInfo>::iterator i = request.begin(); i != request.end(); i++) {
288 while ((*i).second->canGetValueLater(curSample, (*i).first)) {
289 if (! fset.next()) {endFound = true; break;} //return false;
290 }
291 }
292 if (endFound && curSample >= fset.getSampleIndex()+71) return false;
293 bool found=false;
294 //bool valuesAhead = false;
295 for (vector<TOIInfo>::iterator i = request.begin(); i != request.end(); i++) {
296 if (((*i).third & triggering) == 0) continue;
297 if ((*i).second->canGetValue(curSample, (*i).first)) {found=true;break;}
298 //if ((*i).second->lastSampleNum((*i).first)>curSample) valuesAhead=true;
299 }
300 if (found) break;
301 }
302 return true;
303}
304
305int TOIIter::getColTOI(TOI const& toi) {
306 for (int i=0; i<request.size(); i++)
307 if (request[i].first == toi) return i;
308 throw ArchExc("getColTOI : no such TOI " + toi.name);
309}
310
311bool TOIIter::canGetValue(int column) {
312 TOIInfo& info = request[column];
313 return(info.second->canGetValue(curSample, info.first));
314}
315
316double TOIIter::getValue(int column) {
317 TOIInfo& info = request[column];
318 return(info.second->getValue(curSample, info.first));
319}
320
321bool TOIIter::canGetValue(TOI const& toi) {
322 return canGetValue(getColTOI(toi));
323}
324
325double TOIIter::getValue(TOI const& toi) {
326 return getValue(getColTOI(toi));
327}
328
Note: See TracBrowser for help on using the repository browser.