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

Last change on this file since 3881 was 3639, checked in by cmv, 16 years ago

ajout des #include C pour compil sur dernieres versions gcc/g++, cmv 27/05/2009

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