source: Sophya/trunk/AddOn/TAcq/racquproc.cc@ 3640

Last change on this file since 3640 was 3635, checked in by ansari, 16 years ago

Amelioration/ correction diverses, introduction du programme de lecture / traitement multi-thread mcrd.cc - Reza 26/05/2009

File size: 11.1 KB
Line 
1#include "racquproc.h"
2
3#include <stdlib.h>
4#include <unistd.h>
5#include <fstream>
6#include <signal.h>
7
8#include "pexceptions.h"
9#include "tvector.h"
10#include "fioarr.h"
11#include "timestamp.h"
12#include "fftpserver.h"
13#include "fftwserver.h"
14
15#include "FFTW/fftw3.h"
16
17
18#include "pciewrap.h"
19#include "brpaqu.h"
20#include "minifits.h"
21struct sigaction act;
22
23//-------------------------------------------------------
24// Classe thread de traitement avec 1 voie par frame
25//-------------------------------------------------------
26
27void DataProcSignal(int s)
28{
29 cout <<"............................................... receive CtrlC" << endl;
30
31}
32
33DataProc::DataProc(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
34 : memgr(mem)
35{
36 nmax_ = nmax;
37 nmean_ = nmean;
38 step_ = step;
39 stop_ = false;
40 path_ = path;
41 act.sa_handler=DataProcSignal;
42}
43
44static inline r_4 Zmod2(complex<r_4> z)
45{ return (z.real()*z.real()+z.imag()*z.imag()); }
46
47void DataProc::Stop()
48{
49 stop_=true;
50 // cout <<" DataProc::Stop ... > STOP " << endl;
51}
52
53void DataProc::run()
54{
55
56 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
57 // sigaction(SIGINT,&act,NULL);
58 setRC(1);
59 try {
60 TimeStamp ts;
61 cout << " DataProc::run() - Starting " << ts << " NMaxMemZones=" << nmax_
62 << " NMean=" << nmean_ << " Step=" << step_ << endl;
63 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
64 char fname[512];
65 sprintf(fname,"%s/proc.log",path_.c_str());
66 ofstream filog(fname);
67 filog << " DataProc::run() - starting log file " << ts << endl;
68 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
69
70// Initialisation pour clcul FFT
71 TVector< complex<r_4> > cfour; // composant TF
72 uint_4 paqsz = memgr.PaqSize();
73 BRPaquet pq(NULL, NULL, paqsz);
74 TVector<r_4> vx(pq.DataSize());
75 vx = (r_4)(0.);
76 FFTPackServer ffts;
77 ffts.FFTForward(vx, cfour);
78 TVector<r_4> spectre;
79 spectre.ReSize(cfour.Size());
80
81 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
82 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
83
84 uint_4 ifile = 0;
85 uint_4 nzm = 0;
86 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
87 if (stop_) break;
88 int mid = memgr.FindMemZoneId(MemZA_Proc);
89 Byte* buff = memgr.GetMemZone(mid);
90 if (buff == NULL) {
91 cout << " DataProc::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
92 setRC(2);
93 return;
94 }
95 BRPaquet paq0(NULL, buff, paqsz);
96 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
97 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
98 Byte min = 255;
99 Byte max = 0;
100
101 for(sa_size_t j=0; j<vx.Size(); j++)
102 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
103 fftwf_execute(plan);
104 // ffts_.FFTForward(vx, cfour_);
105 for(sa_size_t j=0; j<spectre.Size(); j++)
106 spectre(j) += Zmod2(cfour(j));
107 nzm++;
108 }
109 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
110 spectre /= (r_4)(nzm);
111 spectre.Info()["NPaqMoy"] = nzm;
112 sprintf(fname,"%s/spectre%d.ppf",path_.c_str(),(int)ifile);
113 POutPersist po(fname);
114 po << spectre;
115 spectre = (r_4)(0.);
116 nzm = 0; ifile++;
117 ts.SetNow();
118 filog << ts << " : proc file " << fname << endl;
119 cout << " DataProc::run() " << ts << " : created file " << fname << endl;
120 }
121
122 memgr.FreeMemZone(mid, MemZS_Proc);
123 }
124 }
125 catch (PException& exc) {
126 cout << " DataProc::run()/catched PException " << exc.Msg() << endl;
127 setRC(3);
128 return;
129 }
130 catch(...) {
131 cout << " DataProc::run()/catched unknown ... exception " << endl;
132 setRC(4);
133 return;
134 }
135 setRC(0);
136 return;
137}
138
139//---------------------------------------------------------------
140// Classe thread de traitement donnees ADC avec 2 voies par frame
141//---------------------------------------------------------------
142
143DataProc2C::DataProc2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
144 : memgr(mem)
145{
146 nmax_ = nmax;
147 nmean_ = nmean;
148 step_ = step;
149 stop_ = false;
150 path_ = path;
151 act.sa_handler=DataProcSignal;
152}
153void DataProc2C::Stop()
154{
155 stop_=true;
156 // cout <<" DataProc2C::Stop ... > STOP " << endl;
157}
158
159void DataProc2C::run()
160{
161 // sigaction(SIGINT,&act,NULL);
162 setRC(1);
163 try {
164 TimeStamp ts;
165 cout << " DataProc2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
166 << " NMean=" << nmean_ << " Step=" << step_ << endl;
167 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
168 char fname[512];
169 sprintf(fname,"%s/proc.log",path_.c_str());
170 ofstream filog(fname);
171 filog << " DataProc2C::run() - starting log file " << ts << endl;
172 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
173
174// Initialisation pour clcul FFT
175 TVector< complex<r_4> > cfour; // composant TF
176 uint_4 paqsz = memgr.PaqSize();
177 BRPaquet pq(NULL, NULL, paqsz);
178 TVector<r_4> vx(pq.DataSize()/2);
179 vx = (r_4)(0.);
180 FFTPackServer ffts;
181 ffts.FFTForward(vx, cfour);
182 TVector<r_4> spectreV1, spectreV2;
183 spectreV1.ReSize(cfour.Size());
184 spectreV2.ReSize(cfour.Size());
185
186 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
187 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
188
189 uint_4 ifile = 0;
190 uint_4 nzm = 0;
191 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
192 if (stop_) break;
193 int mid = memgr.FindMemZoneId(MemZA_Proc);
194 Byte* buff = memgr.GetMemZone(mid);
195 if (buff == NULL) {
196 cout << " DataProc2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
197 setRC(2);
198 return;
199 }
200 BRPaquet paq0(NULL, buff, paqsz);
201 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
202 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
203 Byte min = 255;
204 Byte max = 0;
205
206 for(sa_size_t j=0; j<vx.Size(); j++)
207 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
208 fftwf_execute(plan);
209 // ffts_.FFTForward(vx, cfour_);
210 for(sa_size_t j=0; j<spectreV1.Size(); j++)
211 spectreV1(j) += Zmod2(cfour(j));
212
213 for(sa_size_t j=0; j<vx.Size(); j++)
214 vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
215 fftwf_execute(plan);
216 // ffts_.FFTForward(vx, cfour_);
217 for(sa_size_t j=0; j<spectreV2.Size(); j++)
218 spectreV2(j) += Zmod2(cfour(j));
219
220 nzm++;
221 }
222 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
223 spectreV1 /= (r_4)(nzm);
224 spectreV2 /= (r_4)(nzm);
225 spectreV1.Info()["NPaqMoy"] = nzm;
226 spectreV2.Info()["NPaqMoy"] = nzm;
227 {
228 sprintf(fname,"%s/spectre2C_%d.ppf",path_.c_str(),(int)ifile);
229 POutPersist po(fname);
230 po << PPFNameTag("specV1") << spectreV1;
231 po << PPFNameTag("specV2") << spectreV2;
232 }
233 spectreV1 = (r_4)(0.);
234 spectreV2 = (r_4)(0.);
235 nzm = 0; ifile++;
236 ts.SetNow();
237 filog << ts << " : proc file " << fname << endl;
238 cout << " DataProc2C::run() " << ts << " : created file " << fname << endl;
239 }
240
241 memgr.FreeMemZone(mid, MemZS_Proc);
242 }
243 }
244 catch (PException& exc) {
245 cout << " DataProc2C::run()/catched PException " << exc.Msg() << endl;
246 setRC(3);
247 return;
248 }
249 catch(...) {
250 cout << " DataProc2C::run()/catched unknown ... exception " << endl;
251 setRC(4);
252 return;
253 }
254 setRC(0);
255 return;
256}
257
258
259
260
261//---------------------------------------------------------------
262// Classe thread de traitement donnees FFT avec 2 voies par frame
263//---------------------------------------------------------------
264
265inline r_4 Zmod2TwoByte(TwoByteComplex z)
266{ return (z.realD()*z.realD()+z.imagD()*z.imagD()); }
267
268DataProcFFT2C::DataProcFFT2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
269 : memgr(mem)
270{
271 nmax_ = nmax;
272 nmean_ = nmean;
273 step_ = step;
274 stop_ = false;
275 path_ = path;
276 act.sa_handler=DataProcSignal;
277}
278void DataProcFFT2C::Stop()
279{
280 stop_=true;
281 // cout <<" DataProcFFT2C::Stop ... > STOP " << endl;
282}
283
284void DataProcFFT2C::run()
285{
286 // sigaction(SIGINT,&act,NULL);
287 setRC(1);
288 try {
289 TimeStamp ts;
290 cout << " DataProcFFT2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
291 << " NMean=" << nmean_ << " Step=" << step_ << endl;
292 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
293 char fname[512];
294 sprintf(fname,"%s/proc.log",path_.c_str());
295 ofstream filog(fname);
296 filog << " DataProcFFT2C::run() - starting log file " << ts << endl;
297 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
298
299// Initialisation pour clcul FFT
300 TVector< complex<r_4> > cfour; // composant TF
301 uint_4 paqsz = memgr.PaqSize();
302 BRPaquet pq(NULL, NULL, paqsz);
303
304 TVector<r_4> spectreV1(pq.DataSize()/4+1), spectreV2(pq.DataSize()/4+1);
305
306 uint_4 ifile = 0;
307 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
308 if (stop_ ) break;
309 int mid = memgr.FindMemZoneId(MemZA_Proc);
310 Byte* buff = memgr.GetMemZone(mid);
311 if (buff == NULL) {
312 cout << " DataProcFFT2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
313 setRC(2);
314 return;
315 }
316 BRPaquet paq0(NULL, buff, paqsz);
317 uint_4 nzm = 0;
318 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
319 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
320 Byte min = 255;
321 Byte max = 0;
322
323 TwoByteComplex* zz;
324 // Traitement Voie 1
325 zz = (TwoByteComplex*)paq.Data1();
326 spectreV1(0) += zz[0].realD()*zz[0].realD(); // Composante continue, partie reelle uniquement
327 for(sa_size_t j=1; j<spectreV1.Size()-1; j++) spectreV1(j) += Zmod2TwoByte(zz[j]);
328
329 spectreV1(spectreV1.Size()-1) += zz[0].imagD()*zz[0].imagD(); // Freq. Nyquist a N/2
330
331 // Traitement Voie 2
332 zz = (TwoByteComplex*)paq.Data2();
333 spectreV2(0) += zz[0].realD()*zz[0].realD(); // Composante continue, partie reelle uniquement
334 for(sa_size_t j=1; j<spectreV2.Size()-1; j++) spectreV2(j) += Zmod2TwoByte(zz[j]);
335
336 spectreV2(spectreV2.Size()-1) += zz[0].imagD()*zz[0].imagD(); // Freq. Nyquist a N/2
337
338 nzm++;
339 }
340 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
341 spectreV1 /= (r_4)(nzm);
342 spectreV2 /= (r_4)(nzm);
343 spectreV1.Info()["NPaqMoy"] = nzm;
344 spectreV2.Info()["NPaqMoy"] = nzm;
345 {
346 sprintf(fname,"%s/spectreFFT2C_%d.ppf",path_.c_str(),(int)ifile);
347 POutPersist po(fname);
348 po << PPFNameTag("specV1") << spectreV1;
349 po << PPFNameTag("specV2") << spectreV2;
350 }
351 spectreV1 = (r_4)(0.);
352 spectreV2 = (r_4)(0.);
353 nzm = 0; ifile++;
354 ts.SetNow();
355 filog << ts << " : proc file " << fname << endl;
356 cout << " DataProcFFT2C::run() " << ts << " : created file " << fname << endl;
357 }
358
359 memgr.FreeMemZone(mid, MemZS_Proc);
360 }
361 }
362 catch (PException& exc) {
363 cout << " DataProcFFT2C::run()/catched PException " << exc.Msg() << endl;
364 setRC(3);
365 return;
366 }
367 catch(...) {
368 cout << " DataProcFFT2C::run()/catched unknown ... exception " << endl;
369 setRC(4);
370 return;
371 }
372 setRC(0);
373 return;
374}
Note: See TracBrowser for help on using the repository browser.