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

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

Amelioration programme mfits2pec.cc - Reza 06/04/2009

File size: 10.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 (non 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//---- Declaration des fonctions de calcul ----
40// Fonction d'analyse 1ere version, pas d'entete ds le fichier, 1 voie
41int ana_data_0(vector<string>& infiles, string& outfile);
42// Fonction d'analyse 2eme version , 1 voie / paquet
43int ana_data_1(vector<string>& infiles, string& oufile);
44// Fonction d'analyse 2eme version , 2 voies / paquet
45int ana_data_2(vector<string>& infiles, string& oufile);
46
47//----------------------------------------------------
48//----------------------------------------------------
49int main(int narg, char* arg[])
50{
51 if (narg < 4) {
52 cout << " ---Calcul spectres moyennes a partir de fits BAORadio " << endl;
53 cout << " Usage: mfits2spec ACT OutPPF file1 [file2 file3 ...] " << endl;
54 cout << " ACT=-0,-1,-2 ==> 0: Nancay-Juil2008, - 1,2 : 1/2 voies / paquet " << endl;
55 cout << " OutPPF : Output PPF file name, file1,file2 ... Input FITS files " << endl;
56 return 1;
57 }
58
59 TArrayInitiator _inia;
60
61 int rc = 0;
62 try {
63 string act = arg[1];
64 string outppf = arg[2];
65 vector<string> infiles;
66 for(int i=3; i<narg; i++) infiles.push_back(arg[i]);
67 cout << " ---------- mfits2spec.cc Start - ACT= " << act << " ------------- " << endl;
68 ResourceUsage resu;
69 if (act == "-0") ana_data_0(infiles, outppf);
70 else if (act == "-1") ana_data_1(infiles, outppf);
71 else if (act == "-2") ana_data_2(infiles, outppf);
72 else cout << " mfits2spec.cc / Bad argument ACT=" << act << " -> exit" << endl;
73 cout << resu ;
74 }
75 catch (MiniFITSException& exc) {
76 cerr << " mfits2spec.cc catched MiniFITSException " << exc.Msg() << endl;
77 rc = 77;
78 }
79 catch (std::exception& sex) {
80 cerr << "\n mfits2spec.cc std::exception :"
81 << (string)typeid(sex).name() << "\n msg= "
82 << sex.what() << endl;
83 rc = 78;
84 }
85 catch (...) {
86 cerr << " mfits2spec.cc catched unknown (...) exception " << endl;
87 rc = 79;
88 }
89
90 cout << ">>>> mfits2spec.cc ------- FIN ----------- RC=" << rc << endl;
91 return rc;
92
93}
94
95inline r_4 Zmod2(complex<r_4> z)
96{ return (z.real()*z.real()+z.imag()*z.imag()); }
97
98/*--Nouvelle-Fonction--*/
99int ana_data_0(vector<string>& infiles, string& outfile)
100{
101 TVector<float> spectre;
102 sa_size_t nzm = 0; // Nb de spectres moyennes
103 for(int ifile=0; ifile<infiles.size(); ifile++) {
104 string ffname = infiles[ifile];
105// -------------- Lecture de bytes
106 cout << ifile <<"-Ouverture/lecture fichier " << ffname << endl;
107 MiniFITSFile mff(ffname, MF_Read);
108 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
109 << " NAxis2=" << mff.NAxis2() << endl;
110 if (mff.DataType() != MF_Byte) {
111 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
112 }
113 size_t sx = mff.NAxis1();
114 size_t sy = mff.NAxis2();
115
116 Byte* data = new Byte[sx];
117 TVector<r_4> vx(sx);
118 TVector< complex<r_4> > cfour;
119 FFTPackServer ffts;
120
121 for(int j=0; j<sy; j++) {
122 mff.ReadB(data, sx, j*sx);
123 // On convertit en float
124 for(sa_size_t ix=0; ix<vx.Size(); ix++) vx(ix) = (r_4)(data[ix]);
125 // On fait le FFT
126 ffts.FFTForward(vx, cfour);
127 if (!spectre.IsAllocated()) spectre.SetSize(cfour.Size());
128
129 // On cumule les modules carres
130 for(sa_size_t jf=1; jf<spectre.Size(); jf++)
131 spectre(jf) += Zmod2(cfour(jf));
132 nzm++;
133
134// cout << " j=" << j << " data[0...3]=" << (int)data[0] << " , "
135// << (int)data[1] << " , " << (int)data[2] << endl;
136 }
137 cout << "---- FIN lecture " << ffname << endl;
138 delete[] data;
139 }
140 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
141 spectre /= (r_4)(nzm);
142 POutPersist po(outfile);
143 po << spectre;
144 return 0;
145}
146
147/*--Nouvelle-Fonction--*/
148int ana_data_1(vector<string>& infiles, string& outfile)
149{
150 TVector<float> spectre;
151 float freq0 = 0;
152 int paqsz = 0;
153 int nfileok;
154 sa_size_t nzm = 0; // Nb de spectres moyennes
155 Byte* data = NULL;
156 FFTPackServer ffts;
157 for(int ifile=0; ifile<infiles.size(); ifile++) {
158 string ffname = infiles[ifile];
159// -------------- Lecture de bytes
160 cout << "ana_data_1[" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
161 MiniFITSFile mff(ffname, MF_Read);
162 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
163 << " NAxis2=" << mff.NAxis2() << endl;
164 if (mff.DataType() != MF_Byte) {
165 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
166 continue;
167 }
168// Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) ...
169 if (paqsz == 0) { // premier passage, on fixe la taille de paquet et on alloue le buffer
170 paqsz = mff.NAxis1()+16;
171 data = new Byte[paqsz];
172 for(int ib=0; ib<paqsz; ib++) data[ib]=0;
173 }
174 else {
175 if (paqsz != mff.NAxis1()+16) {
176 cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+16 --> skipping " << endl;
177 continue;
178 }
179 }
180 size_t sx = mff.NAxis1();
181 size_t sy = mff.NAxis2();
182 BRPaquet paq(NULL, data, paqsz);
183 TVector<r_4> vx(paq.DataSize());
184 TVector< complex<r_4> > cfour;
185
186 for(int j=0; j<sy; j++) {
187 mff.ReadB(data, sx, j*sx);
188 // On convertit en float
189 for(sa_size_t ix=0; ix<vx.Size(); ix++) vx(ix) = (r_4)(paq.Data1()[ix])-127.5;
190 // On fait le FFT
191 ffts.FFTForward(vx, cfour);
192 if (!spectre.IsAllocated()) spectre.SetSize(cfour.Size());
193
194 // On cumule les modules carres - en sautant la frequence zero
195 for(sa_size_t jf=1; jf<spectre.Size(); jf++)
196 spectre(jf) += Zmod2(cfour(jf));
197 nzm++; freq0 += cfour(0).real();
198 }
199 nfileok++;
200 cout << "---- FIN lecture " << ffname << " NFileOK=" << nfileok << endl;
201 }
202 if (data) delete[] data;
203 if (nzm <= 0) {
204 cout << " ana_data_1/ERROR : nzm=" << nzm << " spectres moyennes !" << endl;
205 return nzm;
206 }
207 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
208 spectre /= (r_4)(nzm);
209 spectre.Info().Comment() = " SpectreMoyenne (Moyenne module^2) ";
210 spectre.Info()["NMOY"] = nzm; // Nombre de spectres moyennes
211 spectre.Info()["Freq0"] = freq0;
212 POutPersist po(outfile);
213 po << PPFNameTag("specV1") << spectre;
214 return nfileok;
215}
216
217/*--Nouvelle-Fonction--*/
218int ana_data_2(vector<string>& infiles, string& outfile)
219{
220 TVector<float> specV1, specV2;
221 TVector< complex<float> > cxspecV12;
222 float freq0v1 = 0;
223 float freq0v2 = 0;
224 int paqsz = 0;
225 int nfileok;
226 sa_size_t nzm = 0; // Nb de spectres moyennes
227 Byte* data = NULL;
228 FFTPackServer ffts;
229 for(int ifile=0; ifile<infiles.size(); ifile++) {
230 string ffname = infiles[ifile];
231// -------------- Lecture de bytes
232 cout << "ana_data_2[" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
233 MiniFITSFile mff(ffname, MF_Read);
234 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
235 << " NAxis2=" << mff.NAxis2() << endl;
236 if (mff.DataType() != MF_Byte) {
237 cout << " PB : DataType!=MF_Byte --> skipping " << endl;
238 continue;
239 }
240// Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) ...
241 if (paqsz == 0) { // premier passage, on fixe la taille de paquet et on alloue le buffer
242 paqsz = mff.NAxis1()+16;
243 cout << " ana_data_2/ Allocating data , PaqSz=" << paqsz << endl;
244 data = new Byte[paqsz];
245 for(int ib=0; ib<paqsz; ib++) data[ib]=0;
246 }
247 else {
248 if (paqsz != mff.NAxis1()+16) {
249 cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+16 --> skipping " << endl;
250 continue;
251 }
252 }
253 size_t sx = mff.NAxis1();
254 size_t sy = mff.NAxis2();
255 BRPaquet paq(NULL, data, paqsz);
256 TVector<r_4> vx1(paq.DataSize()/2);
257 TVector<r_4> vx2(paq.DataSize()/2);
258 TVector< complex<r_4> > cfour1,cfour2;
259
260 for(int j=0; j<sy; j++) {
261 mff.ReadB(data, sx, j*sx);
262 // if (j%50 == 0) cout << " *DBG* mff.ReadB() , j=" << j << endl;
263 // On convertit en float
264 for(sa_size_t ix=0; ix<vx1.Size(); ix++) vx1(ix) = (r_4)(paq.Data1()[ix])-127.5;
265 for(sa_size_t ix=0; ix<vx2.Size(); ix++) vx2(ix) = (r_4)(paq.Data2()[ix])-127.5;
266 // On fait le FFT
267 ffts.FFTForward(vx1, cfour1);
268 ffts.FFTForward(vx2, cfour2);
269 if (!specV1.IsAllocated()) specV1.SetSize(cfour1.Size());
270 if (!specV2.IsAllocated()) specV2.SetSize(cfour2.Size());
271 if (!cxspecV12.IsAllocated()) cxspecV12.SetSize(cfour1.Size());
272
273 // On cumule les modules carres - en sautant la frequence zero
274 for(sa_size_t jf=1; jf<specV1.Size(); jf++) {
275 specV1(jf) += Zmod2(cfour1(jf));
276 specV2(jf) += Zmod2(cfour2(jf));
277 cxspecV12(jf) += cfour1(jf)*cfour2(jf);
278 }
279 nzm++; freq0v1 += cfour1(0).real(); freq0v2 += cfour2(0).real();
280 }
281 nfileok++;
282 cout << "---- FIN lecture " << ffname << " NFileOK=" << nfileok << endl;
283 }
284 if (data) delete[] data;
285 if (nzm <= 0) {
286 cout << " ana_data_2/ERROR : nzm=" << nzm << " spectres moyennes !" << endl;
287 return nzm;
288 }
289 cout << "---- Ecriture spectre moyenne ds " << outfile << endl;
290 specV1 /= (r_4)(nzm);
291 specV2 /= (r_4)(nzm);
292 cxspecV12 /= complex<r_4>((r_4)(nzm),0.);
293 specV1.Info().Comment() = " SpectreMoyenne (Moyenne module^2) - Voie 1 (/2)";
294 specV2.Info().Comment() = " SpectreMoyenne (Moyenne module^2) - Voie 2 (/2)";
295 specV1.Info()["NMOY"] = specV2.Info()["NMOY"] = nzm; // Nombre de spectres moyennes
296 specV1.Info()["Freq0"] = freq0v1;
297 specV2.Info()["Freq0"] = freq0v2;
298 POutPersist po(outfile);
299 po << PPFNameTag("specV1") << specV1;
300 po << PPFNameTag("specV2") << specV2;
301 po << PPFNameTag("cxspecV12") << cxspecV12;
302 return 0;
303}
Note: See TracBrowser for help on using the repository browser.