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

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

fin de fichier, start

File size: 8.0 KB
RevLine 
[350]1// toiiter.cc
2// Eric Aubourg CEA/DAPNIA/SPP juillet 1999
3
[534]4#include "ark.h"
[350]5
[310]6#include "toiiter.h"
[534]7#include "toimanager.h"
8#include "toiproducer.h"
[394]9#include "archparam.h"
[534]10#include "asigps.h"
11#include "toiauxgpsproducer.h"
[342]12#include <iostream.h>
[534]13#include <fstream.h>
[310]14
[534]15#define CHK_INITED if (initDone) \
16 throw ArchExc("Trying to modify TOIIter after init done.");
[310]17
18TOIIter::TOIIter() {
[534]19 mjdStart = -999999999;
20 mjdEnd = 999999999;
21 utcStart = -999999999;
22 utcEnd = 999999999;
23 sStart = -999999999;
24 sEnd = 999999999;
[342]25
[534]26 underSample = 1;
27 curSample = -1;
28
29 initDone = false;
30 incDone = false;
31}
[342]32
[534]33#ifdef __MWERKS__
34#pragma mark -
35#endif
36
37void TOIIter::addDirectory(string dir) {
38 CHK_INITED
39 fset.addDirectory(dir);
[310]40}
41
[534]42void TOIIter::addFile(string fn) {
43 CHK_INITED
44 fset.addFile(fn);
45}
[310]46
[534]47void TOIIter::setMJDInterval(double tStart, double tEnd) {
48 CHK_INITED
49 if (tStart>0) mjdStart = tStart;
50 if (tEnd>0) mjdEnd = tEnd;
[310]51}
52
[534]53void TOIIter::setUTCInterval(double tStart, double tEnd) {
54 CHK_INITED
55 if (tStart>0) utcStart = tStart;
56 if (tEnd>0) utcEnd = tEnd;
[310]57}
58
[534]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
[342]83#ifdef __MWERKS__
[534]84#pragma mark -
[342]85#endif
86
[310]87
[534]88void TOIIter::registerReqHandler(RequestHandler* h) {
89 CHK_INITED
90 handlers.push_back(h);
91}
[315]92
93
[534]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 }
[310]107}
108
[534]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);
[350]119 }
[534]120 return handled;
[350]121 }
[534]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);
[350]130 }
[534]131 return handled;
[350]132 }
[534]133 return false;
[350]134}
135
[534]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 {
[310]194 return false;
195 }
[426]196 return true;
197}
[350]198
[534]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");
[612]207 processRequest("#COMMENT Archtoi V3 -- novembre 1999 -- Eric Aubourg CEA/DAPNIA");
[350]208}
209
[534]210#ifdef __MWERKS__
211#pragma mark -
212#endif
[310]213
[534]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;
[310]221 }
[534]222 if (utcEnd > 0) {
223 double t = (utcEnd/24.) + archParam.acq.utcOrigin;
224 if (t < mjdEnd) mjdEnd=t;
[310]225 }
226
[612]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);
[400]232
[612]233 fset.setMJDRange(mjdStart-delT, mjdEnd+delT);
234 fset.setSNumRange(sStart-delSN, sEnd+delSN);
235
[534]236 fset.init();
237 //curSample = fset.getSampleIndex();
[315]238}
[342]239
240bool TOIIter::isTrig(int column) {
[534]241 return (request[column].third & triggering) != 0;
[342]242}
[310]243
[534]244TOI TOIIter::getKind(int column) {
245 return request[column].first;
[315]246}
[534]247
248long TOIIter::getSampleNum() {
249 return curSample;
[315]250}
251
[534]252bool TOIIter::next() {
253 if (!initDone) init();
254 for (int ii=0; ii<underSample; ii++)
255 if (!next1()) return false;
256
257 return true;
[315]258}
259
[534]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...
[612]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
[534]273 if (curSample <= 0) curSample = fset.getSampleIndex()-1;
274
275 if (curSample >= sEnd || archParam.acq.SN2MJD(curSample) >= mjdEnd) return false;
276
[555]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 }
[534]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 bool found=false;
[612]293 //bool valuesAhead = false;
[534]294 for (vector<TOIInfo>::iterator i = request.begin(); i != request.end(); i++) {
295 if (((*i).third & triggering) == 0) continue;
296 if ((*i).second->canGetValue(curSample, (*i).first)) {found=true;break;}
[612]297 //if ((*i).second->lastSampleNum((*i).first)>curSample) valuesAhead=true;
[534]298 }
299 if (found) break;
[612]300 if (endFound && curSample >= fset.getSampleIndex()+71) return false;
[534]301 }
302 return true;
[315]303}
304
[534]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);
[315]309}
310
[534]311bool TOIIter::canGetValue(int column) {
312 TOIInfo& info = request[column];
313 return(info.second->canGetValue(curSample, info.first));
[315]314}
[426]315
[534]316double TOIIter::getValue(int column) {
317 TOIInfo& info = request[column];
318 return(info.second->getValue(curSample, info.first));
[315]319}
[358]320
[534]321bool TOIIter::canGetValue(TOI const& toi) {
322 return canGetValue(getColTOI(toi));
[358]323}
[315]324
[534]325double TOIIter::getValue(TOI const& toi) {
326 return getValue(getColTOI(toi));
[342]327}
328
Note: See TracBrowser for help on using the repository browser.