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

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