source: Sophya/trunk/AddOn/TAcq/mfits2spec.cc@ 3631

Last change on this file since 3631 was 3610, checked in by ansari, 16 years ago

Amelioration script analyse paquet de donnees FFT - Reza, 30/04/2009

File size: 16.4 KB
Line 
1// Utilisation de SOPHYA pour faciliter les tests ...
2#include "sopnamsp.h"
3#include "machdefs.h"
4
5/* ------------------------------------------------------------------
6 Programme de calcul de spectre moyenne a partir des fichiers fits
7 d'acquisition de BAORadio - donnees brutes ou fft
8 R. Ansari, C. Magneville
9 V1 : Juillet 2008 , V2 : Avril 2009
10 ------------------------------------------------------------------ */
11
12// include standard c/c++
13#include <math.h>
14#include <stdio.h>
15
16#include <iostream>
17#include <string>
18
19#include "pexceptions.h"
20#include "tvector.h"
21#include "fioarr.h"
22#include "tarrinit.h"
23#include "timestamp.h"
24#include "fftpserver.h"
25#include "fftwserver.h"
26
27#include "FFTW/fftw3.h"
28
29// include sophya mesure ressource CPU/memoire ...
30#include "resusage.h"
31#include "ctimer.h"
32#include "timing.h"
33
34
35// include mini-fits lib , et structure paquet BAORadio
36#include "minifits.h"
37#include "brpaqu.h"
38
39
40// Definition de type pour les tableaux de float
41#define DTF r_4
42
43//---- Declaration des fonctions de calcul ----
44// Fonction d'analyse 1ere version, pas d'entete ds le fichier, 1 voie
45int ana_data_0(vector<string>& infiles, string& outfile);
46// Fonction d'analyse 2eme version , 1 voie / paquet
47int ana_data_1(vector<string>& infiles, string& oufile);
48// Fonction d'analyse 2eme version , 2 voies / paquet
49int ana_data_2(vector<string>& infiles, string& oufile);
50// Fonction d'analyse 2eme version , 2 voies / paquet
51int ana_data_fft_2(vector<string>& infiles, string& oufile);
52
53//----------------------------------------------------
54//----------------------------------------------------
55int main(int narg, char* arg[])
56{
57 if (narg < 4) {
58 cout << " ---Mean spectrum computation from BAORadio FITS files" << endl;
59 cout << " Usage: mfits2spec ACT OutPPF file1 [file2 file3 ...] " << endl;
60 cout << " Or : mfits2spec ACT OutPPF -infits DirName Imin Imax " << endl;
61 cout << " ACT=-0,-1,-2,-fft2 \n"
62 << " -0: Nancay-July2008 \n"
63 << " -1,-2 : Raw data 1 ou 2 channels / packet(or frame) \n"
64 << " -fft2 : FFT data 2 channels / packet " << endl;
65 cout << " OutPPF : Output PPF file name " << endl;
66 cout << " file1,file2 ... : Input FITS files " << endl;
67 cout << " -infiles DirName Imin Imax : Input fits directory and sequence numbers \n"
68 << " FileNames=DirName/signalII.fits Imin<=II<=Imax " << endl;
69 return 1;
70 }
71
72 TArrayInitiator _inia;
73
74 int rc = 0;
75 try {
76 string act = arg[1];
77 string outppf = arg[2];
78 vector<string> infiles;
79 if (strcmp(arg[3],"-infits")==0) {
80 if (narg<7) {
81 cout << " mfits2spec/Error arguments - type mfits2spec for help" << endl;
82 return 2;
83 }
84 char nbuff[1024];
85 char* dirname = arg[4];
86 int imin = atoi(arg[5]);
87 int imax = atoi(arg[6]);
88 for(int ii=imin; ii<=imax; ii++) {
89 sprintf(nbuff,"%s/signal%d.fits",dirname,ii);
90 infiles.push_back(nbuff);
91 }
92 }
93 else // Liste de noms de fichiers fournis directement
94 for(int i=3; i<narg; i++) infiles.push_back(arg[i]);
95
96 cout << " ---------- mfits2spec.cc Start - ACT= " << act << " ------------- " << endl;
97 ResourceUsage resu;
98 if (act == "-0") ana_data_0(infiles, outppf);
99 else if (act == "-1") ana_data_1(infiles, outppf);
100 else if (act == "-2") ana_data_2(infiles, outppf);
101 else if (act == "-fft2") ana_data_fft_2(infiles, outppf);
102 else cout << " mfits2spec.cc / Bad argument ACT=" << act << " -> exit" << endl;
103 cout << resu ;
104 }
105 catch (MiniFITSException& exc) {
106 cerr << " mfits2spec.cc catched MiniFITSException " << exc.Msg() << endl;
107 rc = 77;
108 }
109 catch (std::exception& sex) {
110 cerr << "\n mfits2spec.cc std::exception :"
111 << (string)typeid(sex).name() << "\n msg= "
112 << sex.what() << endl;
113 rc = 78;
114 }
115 catch (...) {
116 cerr << " mfits2spec.cc catched unknown (...) exception " << endl;
117 rc = 79;
118 }
119
120 cout << ">>>> mfits2spec.cc ------- FIN ----------- RC=" << rc << endl;
121 return rc;
122
123}
124
125inline DTF Zmod2(complex<DTF> z)
126{ return (z.real()*z.real()+z.imag()*z.imag()); }
127
128/*--Nouvelle-Fonction--*/
129int ana_data_0(vector<string>& infiles, string& outfile)
130// Calcul spectre moyen 1 voie, donnees brutes, Donnees Juillet 2008
131{
132 TVector<float> spectre;
133 sa_size_t nzm = 0; // Nb de spectres moyennes
134 for(int ifile=0; ifile<infiles.size(); ifile++) {
135 string ffname = infiles[ifile];
136// -------------- Lecture de bytes
137 cout << ifile <<"-Ouverture/lecture fichier " << ffname << endl;
138 MiniFITSFile mff(ffname, MF_Read);
139 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
140 << " NAxis2=" << mff.NAxis2() << endl;
141 if (mff.DataType() != MF_Byte) {
142 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
143 }
144 size_t sx = mff.NAxis1();
145 size_t sy = mff.NAxis2();
146
147 Byte* data = new Byte[sx];
148 TVector<DTF> vx(sx);
149 TVector< complex<DTF> > cfour;
150 FFTPackServer ffts;
151
152 for(int j=0; j<sy; j++) {
153 mff.ReadB(data, sx, j*sx);
154 // On convertit en float
155 for(sa_size_t ix=0; ix<vx.Size(); ix++) vx(ix) = (DTF)(data[ix]);
156 // On fait le FFT
157 ffts.FFTForward(vx, cfour);
158 if (!spectre.IsAllocated()) spectre.SetSize(cfour.Size());
159
160 // On cumule les modules carres
161 for(sa_size_t jf=1; jf<spectre.Size(); jf++)
162 spectre(jf) += Zmod2(cfour(jf));
163 nzm++;
164
165// cout << " j=" << j << " data[0...3]=" << (int)data[0] << " , "
166// << (int)data[1] << " , " << (int)data[2] << endl;
167 }
168 cout << "---- FIN lecture " << ffname << endl;
169 delete[] data;
170 }
171 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
172 spectre /= (DTF)(nzm);
173 POutPersist po(outfile);
174 po << spectre;
175 return 0;
176}
177
178/*--Nouvelle-Fonction--*/
179int ana_data_1(vector<string>& infiles, string& outfile)
180// Calcul spectre moyen 1 voie, donnees brutes
181{
182 TVector<float> spectre;
183 float freq0 = 0;
184 int paqsz = 0;
185 int nfileok=0;
186 sa_size_t nzm = 0; // Nb de spectres moyennes
187 Byte* data = NULL;
188 FFTPackServer ffts;
189
190 Timer tm("ana_data_1");
191 size_t totnbytesrd = 0;
192
193 for(int ifile=0; ifile<infiles.size(); ifile++) {
194 string ffname = infiles[ifile];
195// -------------- Lecture de bytes
196 cout << "ana_data_1[" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
197 MiniFITSFile mff(ffname, MF_Read);
198 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
199 << " NAxis2=" << mff.NAxis2() << endl;
200 if (mff.DataType() != MF_Byte) {
201 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
202 continue;
203 }
204// Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) ...
205 if (paqsz == 0) { // premier passage, on fixe la taille de paquet et on alloue le buffer
206 paqsz = mff.NAxis1()+16;
207 data = new Byte[paqsz];
208 for(int ib=0; ib<paqsz; ib++) data[ib]=0;
209 }
210 else {
211 if (paqsz != mff.NAxis1()+16) {
212 cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+16 --> skipping " << endl;
213 continue;
214 }
215 }
216 size_t sx = mff.NAxis1();
217 size_t sy = mff.NAxis2();
218 BRPaquet paq(NULL, data, paqsz);
219 TVector<DTF> vx(paq.DataSize());
220 TVector< complex<DTF> > cfour;
221
222 for(int j=0; j<sy; j++) {
223 mff.ReadB(data, sx, j*sx);
224 // On convertit en float
225 for(sa_size_t ix=0; ix<vx.Size(); ix++) vx(ix) = (DTF)(paq.Data1()[ix])-127.5;
226 // On fait le FFT
227 ffts.FFTForward(vx, cfour);
228 if (!spectre.IsAllocated()) spectre.SetSize(cfour.Size());
229
230 // On cumule les modules carres - en sautant la frequence zero
231 for(sa_size_t jf=1; jf<spectre.Size(); jf++)
232 spectre(jf) += Zmod2(cfour(jf));
233 nzm++; freq0 += cfour(0).real();
234 }
235 nfileok++;
236 size_t nbytesrd = sx*sy;
237 totnbytesrd += nbytesrd;
238 tm.Split();
239 cout << "---- FIN lecture " << ffname << " NFileOK=" << nfileok << endl;
240 cout << " TotalDiskRead= " << totnbytesrd/(1024*1024) << " MBytes Disk-Read rate= "
241 << (double)(nbytesrd)/(1024.*1024.)/tm.PartialCPUTime() << " MB/s" << endl;
242 }
243 if (data) delete[] data;
244 if (nzm <= 0) {
245 cout << " ana_data_1/ERROR : nzm=" << nzm << " spectres moyennes !" << endl;
246 return nzm;
247 }
248 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
249 spectre /= (DTF)(nzm);
250 spectre.Info().Comment() = " SpectreMoyenne (Moyenne module^2) ";
251 spectre.Info()["NMOY"] = nzm; // Nombre de spectres moyennes
252 spectre.Info()["Freq0"] = freq0;
253 POutPersist po(outfile);
254 po << PPFNameTag("specV1") << spectre;
255 return nfileok;
256}
257
258/*--Nouvelle-Fonction--*/
259int ana_data_2(vector<string>& infiles, string& outfile)
260// Calcul spectres moyens 2 voies, donnees brutes
261{
262 TVector<float> specV1, specV2;
263 TVector< complex<float> > cxspecV12;
264 float freq0v1 = 0;
265 float freq0v2 = 0;
266 int paqsz = 0;
267 int nfileok=0;
268 sa_size_t nzm = 0; // Nb de spectres moyennes
269 Byte* data = NULL;
270 FFTPackServer ffts;
271
272 Timer tm("ana_data_2");
273 size_t totnbytesrd = 0;
274
275 for(int ifile=0; ifile<infiles.size(); ifile++) {
276 string ffname = infiles[ifile];
277// -------------- Lecture de bytes
278 cout << "ana_data_2[" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
279 MiniFITSFile mff(ffname, MF_Read);
280 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
281 << " NAxis2=" << mff.NAxis2() << endl;
282 if (mff.DataType() != MF_Byte) {
283 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
284 continue;
285 }
286// Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) ...
287 if (paqsz == 0) { // premier passage, on fixe la taille de paquet et on alloue le buffer
288 paqsz = mff.NAxis1()+16;
289 cout << " ana_data_2/ Allocating data , PaqSz=" << paqsz << endl;
290 data = new Byte[paqsz];
291 for(int ib=0; ib<paqsz; ib++) data[ib]=0;
292 }
293 else {
294 if (paqsz != mff.NAxis1()+16) {
295 cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+16 --> skipping " << endl;
296 continue;
297 }
298 }
299 size_t sx = mff.NAxis1();
300 size_t sy = mff.NAxis2();
301 BRPaquet paq(NULL, data, paqsz);
302 TVector<DTF> vx1(paq.DataSize()/2);
303 TVector<DTF> vx2(paq.DataSize()/2);
304 TVector< complex<DTF> > cfour1,cfour2;
305
306 for(int j=0; j<sy; j++) {
307 mff.ReadB(data, sx, j*sx);
308 // if (j%50 == 0) cout << " *DBG* mff.ReadB() , j=" << j << endl;
309 // On convertit en float
310 for(sa_size_t ix=0; ix<vx1.Size(); ix++) vx1(ix) = (DTF)(paq.Data1()[ix])-127.5;
311 for(sa_size_t ix=0; ix<vx2.Size(); ix++) vx2(ix) = (DTF)(paq.Data2()[ix])-127.5;
312 // On fait le FFT
313 ffts.FFTForward(vx1, cfour1);
314 ffts.FFTForward(vx2, cfour2);
315 if (!specV1.IsAllocated()) specV1.SetSize(cfour1.Size());
316 if (!specV2.IsAllocated()) specV2.SetSize(cfour2.Size());
317 if (!cxspecV12.IsAllocated()) cxspecV12.SetSize(cfour1.Size());
318
319 // On cumule les modules carres - en sautant la frequence zero
320 for(sa_size_t jf=1; jf<specV1.Size(); jf++) {
321 specV1(jf) += Zmod2(cfour1(jf));
322 specV2(jf) += Zmod2(cfour2(jf));
323 cxspecV12(jf) += cfour1(jf)*cfour2(jf);
324 }
325 nzm++; freq0v1 += cfour1(0).real(); freq0v2 += cfour2(0).real();
326 }
327 nfileok++;
328 size_t nbytesrd = sx*sy;
329 totnbytesrd += nbytesrd;
330 tm.Split();
331 cout << "---- FIN lecture " << ffname << " NFileOK=" << nfileok << endl;
332 cout << " TotalDiskRead= " << totnbytesrd/(1024*1024) << " MBytes Disk-Read rate= "
333 << (double)(nbytesrd)/(1024.*1024.)/tm.PartialCPUTime() << " MB/s" << endl;
334 }
335 if (data) delete[] data;
336 if (nzm <= 0) {
337 cout << " ana_data_2/ERROR : nzm=" << nzm << " spectres moyennes !" << endl;
338 return nzm;
339 }
340 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
341 specV1 /= (DTF)(nzm);
342 specV2 /= (DTF)(nzm);
343 cxspecV12 /= complex<DTF>((DTF)(nzm),0.);
344 specV1.Info().Comment() = " SpectreMoyenne (Moyenne module^2) - Raw-Voie 1 (/2)";
345 specV2.Info().Comment() = " SpectreMoyenne (Moyenne module^2) - Raw-Voie 2 (/2)";
346 specV1.Info()["NMOY"] = specV2.Info()["NMOY"] = nzm; // Nombre de spectres moyennes
347 specV1.Info()["Freq0"] = freq0v1;
348 specV2.Info()["Freq0"] = freq0v2;
349 POutPersist po(outfile);
350 po << PPFNameTag("specV1") << specV1;
351 po << PPFNameTag("specV2") << specV2;
352 po << PPFNameTag("cxspecV12") << cxspecV12;
353 return 0;
354}
355
356
357inline DTF Zmod2TwoByte(TwoByteComplex z)
358{ return (z.realD()*z.realD()+z.imagD()*z.imagD()); }
359
360/*--Nouvelle-Fonction--*/
361int ana_data_fft_2(vector<string>& infiles, string& outfile)
362// Calcul spectres moyens 2 voies, donnees brutes
363{
364 TVector<float> specV1, specV2;
365 TVector< complex<float> > cxspecV12;
366 float freq0v1 = 0;
367 float freq0v2 = 0;
368 int paqsz = 0;
369 int nfileok=0;
370 sa_size_t nzm = 0; // Nb de spectres moyennes
371 Byte* data = NULL;
372
373 Timer tm("ana_data_fft_2");
374 size_t totnbytesrd = 0;
375
376 for(int ifile=0; ifile<infiles.size(); ifile++) {
377 string ffname = infiles[ifile];
378// -------------- Lecture de bytes
379 cout << "ana_data_fft_2[" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
380 MiniFITSFile mff(ffname, MF_Read);
381 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
382 << " NAxis2=" << mff.NAxis2() << endl;
383 if (mff.DataType() != MF_Byte) {
384 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
385 continue;
386 }
387// Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) ...
388 if (paqsz == 0) { // premier passage, on fixe la taille de paquet et on alloue le buffer
389 paqsz = mff.NAxis1()+16;
390 cout << " ana_data_fft_2/ Allocating data , PaqSz=" << paqsz << endl;
391 data = new Byte[paqsz];
392 for(int ib=0; ib<paqsz; ib++) data[ib]=0;
393 }
394 else {
395 if (paqsz != mff.NAxis1()+16) {
396 cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+16 --> skipping " << endl;
397 continue;
398 }
399 }
400 size_t sx = mff.NAxis1();
401 size_t sy = mff.NAxis2();
402 BRPaquet paq(NULL, data, paqsz);
403 TVector<DTF> vx1(paq.DataSize()/2);
404 TVector<DTF> vx2(paq.DataSize()/2);
405 TVector< complex<DTF> > cfour1,cfour2;
406
407 if (!specV1.IsAllocated()) specV1.SetSize(paq.DataSize()/4+1);
408 if (!specV2.IsAllocated()) specV2.SetSize(paq.DataSize()/4+1);
409 if (!cxspecV12.IsAllocated()) cxspecV12.SetSize(paq.DataSize()/4+1);
410
411 for(int j=0; j<sy; j++) {
412 mff.ReadB(data, sx, j*sx);
413
414 TwoByteComplex* zz1 = (TwoByteComplex*)paq.Data1();
415 TwoByteComplex* zz2 = (TwoByteComplex*)paq.Data2();
416 // Composante continue, partie reelle uniquement
417 specV1(0) += zz1[0].realD()*zz1[0].realD();
418 specV2(0) += zz2[0].realD()*zz2[0].realD();
419 cxspecV12(0) += zz1[0].realD()*zz2[0].realD();
420
421 for(sa_size_t jf=1; jf<specV1.Size()-1; jf++) {
422 specV1(jf) += Zmod2TwoByte(zz1[jf]);
423 specV2(jf) += Zmod2TwoByte(zz2[jf]);
424 cxspecV12(jf) += (DTF)(zz1[jf].realD()*zz2[jf].realD()+zz1[jf].imagD()*zz2[jf].imagD());
425 }
426 // Freq. Nyquist a N/2
427 specV1(specV1.Size()-1) += zz1[0].imagD()*zz1[0].imagD(); // Freq. Nyquist a N/2
428 specV2(specV2.Size()-1) += zz2[0].imagD()*zz2[0].imagD(); // Freq. Nyquist a N/2
429 cxspecV12(cxspecV12.Size()-1) += zz1[0].imagD()*zz2[0].imagD();
430
431 nzm++; freq0v1 += zz1[0].realD(); freq0v2 += zz2[0].realD();
432 }
433 nfileok++;
434 size_t nbytesrd = sx*sy;
435 totnbytesrd += nbytesrd;
436 tm.Split();
437 cout << "---- FIN lecture " << ffname << " NFileOK=" << nfileok << endl;
438 cout << " TotalDiskRead= " << totnbytesrd/(1024*1024) << " MBytes Disk-Read rate= "
439 << (double)(nbytesrd)/(1024.*1024.)/tm.PartialCPUTime() << " MB/s" << endl;
440 }
441 if (data) delete[] data;
442 if (nzm <= 0) {
443 cout << " ana_data_fft_2/ERROR : nzm=" << nzm << " spectres moyennes !" << endl;
444 return nzm;
445 }
446 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
447 specV1 /= (DTF)(nzm);
448 specV2 /= (DTF)(nzm);
449 cxspecV12 /= complex<DTF>((DTF)(nzm),0.);
450 specV1.Info().Comment() = " SpectreMoyenne (Moyenne module^2) - FFT-Voie 1 (/2)";
451 specV2.Info().Comment() = " SpectreMoyenne (Moyenne module^2) - FFT-Voie 2 (/2)";
452 specV1.Info()["NMOY"] = specV2.Info()["NMOY"] = nzm; // Nombre de spectres moyennes
453 specV1.Info()["Freq0"] = freq0v1;
454 specV2.Info()["Freq0"] = freq0v2;
455 POutPersist po(outfile);
456 po << PPFNameTag("specV1") << specV1;
457 po << PPFNameTag("specV2") << specV2;
458 po << PPFNameTag("cxspecV12") << cxspecV12;
459 return 0;
460}
Note: See TracBrowser for help on using the repository browser.