source: Sophya/trunk/AddOn/TAcq/corrcrtadc.cc@ 4007

Last change on this file since 4007 was 4007, checked in by ansari, 14 years ago

Debug (1) amelioration print numero canaux, Reza 16/6/2011

File size: 12.0 KB
RevLine 
[4005]1// Utilisation de SOPHYA ...
[3996]2#include "sopnamsp.h"
3#include "machdefs.h"
4
[4005]5/* ---------------------------------------------------------------
6 Projet BAORadio-21cm intensity mapping - (C) LAL/IRFU 2008-2011
[3996]7
[4005]8 CRT (Pittsburgh-CMU) DUMP ADC read/processing program to compute
9 spectra and correlations du CRT
[3996]10 R. Ansari, C. Magneville - LAL/Irfu - Juin 2011
[4005]11 --------------------------------------------------------------- */
[3996]12
13// include standard c/c++
14#include <math.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <iostream>
20#include <string>
21
[4005]22// SOPHYA include files
[3996]23#include "pexceptions.h"
24#include "tvector.h"
25#include "fioarr.h"
26// #include "tarrinit.h"
27#include "ntuple.h"
28#include "datatable.h"
29#include "histinit.h"
30#include "matharr.h"
31#include "timestamp.h"
32#include "fftwserver.h"
33#include "fftpserver.h"
34#include "utilarr.h"
[3999]35#include "histats.h"
[3996]36
37// include sophya mesure ressource CPU/memoire ...
38#include "resusage.h"
39#include "ctimer.h"
40#include "timing.h"
41
42
[4005]43//--------------------------- Functions in this file -------------------
[3996]44int Usage(bool fgshort=true);
45int DecodeProc(int narg, char* arg[]);
[4005]46int ProcessADCFiles(string& inoutpath, vector<int>& adcids, string& outname, bool fgcombiq,
47 int imin, int imax, int istep, int jf1, int jf2, int nfreq);
48int ComputeCorrel(TMatrix< complex<r_4> >& cfour, bool fgcombiq,
49 TMatrix< complex<r_8> >& correl, TMatrix< complex<r_8> >& autocorrel);
[3999]50int ADCFiles2Histo(string& inoutpath, vector<int>& adcids, string& outname, int imin, int imax, int istep);
[4005]51int CombineIQPairs(TMatrix< complex<r_4> >& cfour);
[3996]52//------------------------------------------------------------------------------------------------------------
53
54/* --Fonction-- */
55int Usage(bool fgshort)
56{
[3999]57 cout << " --- corrcrtadc.cc : Read CRT-ADC dump files and process to produce correlation matrices \n"
[4005]58 << " Usage: corrcrtadc -corr/-corciq/-hval InOutPath OutFile [AdcIds] [Imin,Imax,step]\n" << endl;
[3996]59 if (fgshort) {
60 cout << " corrcrtadc -h for detailed instructions" << endl;
61 return 1;
62 }
[4005]63 cout << " -corr/-corciq/-hval : Compute correlations or Fill histograms for ADC sample values \n"
64 << " -corciq : combine IQ pairs to separate side-bands ... \n"
[3999]65 << " InOutPath : Input/Output directory name \n"
[4000]66 << " ADCIds : ADC Id's to be processed (Example: 1,2,3,4, default=0,1) \n"
[3996]67 << " OutFile : Output PPF file name \n"
68 << " Imin,Imax,IStep: Input ADC dump files sequence number \n"
[3999]69 << " FileNames=InOutPath/adcdumpNJFII.fits Imin<=II<=Imax II+=IStep , J=ADCIds \n" << endl;
70 // << " NumFreq1,NumFreq2,NBinFreq: Freq Zone and number of frequency bins for ntuple" << endl;
[3996]71 return 1;
72}
73
74//----------------------------------------------------
75//----------------------------------------------------
76int main(int narg, char* arg[])
77{
78 if ((narg>1)&&(strcmp(arg[1],"-h")==0)) return Usage(false);
79 if (narg<4) return Usage(true);
80 HiStatsInitiator _inia;
81 // TArrayInitiator _inia;
82
83 int rc = 0;
84 try {
85 ResourceUsage resu;
86 DecodeProc(narg-1, arg+1);
87 resu.Update();
88 cout << resu;
89 }
90 catch (PException& exc) {
91 cerr << " corrcrtadc.cc catched PException " << exc.Msg() << endl;
92 rc = 77;
93 }
94 catch (std::exception& sex) {
95 cerr << "\n corrcrtadc.cc std::exception :"
96 << (string)typeid(sex).name() << "\n msg= "
97 << sex.what() << endl;
98 rc = 78;
99 }
100 catch (...) {
101 cerr << " corrcrtadc.cc catched unknown (...) exception " << endl;
102 rc = 79;
103 }
104
105 cout << ">>>> corrcrtadc.cc ------- END ----------- RC=" << rc << endl;
106 return rc;
107
108}
109
[3999]110
[3996]111//--------------------------------------------------------------------
112// Traitement fichiers produits par vismfib (V Nov09)
113/* --Fonction-- */
114int DecodeProc(int narg, char* arg[])
115{
116 // Decodage des arguments et traitement
[3999]117 string popt = arg[0];
118 bool fillhis=false;
[4005]119 bool fgcomiq=false;
[3999]120 if (popt=="-hval") fillhis=true;
[4005]121 else if (popt=="-corciq") fgcomiq=true;
[3999]122 string inoutpath = arg[1];
123 string outname = arg[2];
[4005]124 int aids[8]={0,1,-1,-1,-1,-1,-1,-1};
125 if (narg>3) sscanf(arg[3],"%d,%d,%d,%d,%d,%d,%d,%d",aids,aids+1,aids+2,aids+3,aids+4,aids+5,aids+6,aids+7);
[3999]126 vector<int> adcids;
[4005]127 for(int ii=0;ii<8;ii++)
[4001]128 if((aids[ii]>=0)&&(aids[ii]<16)) adcids.push_back(aids[ii]);
[3996]129 int imin=0;
130 int imax=0;
131 int istep=1;
[3999]132 if (narg>4) sscanf(arg[4],"%d,%d,%d",&imin,&imax,&istep);
[3996]133 int jf1=0;
134 int jf2=0;
135 int nfreq=0;
[3999]136 if (narg>5)
137 sscanf(arg[5],"%d,%d,%d",&jf1,&jf2,&nfreq);
[3996]138
[3999]139 cout << " ----- corrcrtadc/DecodeProc - Start " << ((fillhis)? " Fill ADC-val histogram" : " Compute Correlation") << endl;
140 cout << " InOutPath= " << inoutpath << " ADCIds: " ;
141 for(size_t ii=0; ii<adcids.size(); ii++) cout << adcids[ii] << " , ";
[4001]142 cout << " (NbADC=" << adcids.size() << ") OutFileName= " << outname
143 << " IMin,Max,Step=" << imin << "," << imax << "," << istep << endl;
144 // << "Frequency num range JF=" << jf1 << "," << jf2 << "," << nfreq << " ------- " << endl;
[3999]145 int rc=0;
146 if (fillhis) rc = ADCFiles2Histo(inoutpath, adcids, outname, imin, imax, istep);
[4005]147 else rc=ProcessADCFiles(inoutpath, adcids, outname, fgcomiq, imin, imax, istep, jf1, jf2, nfreq);
[3999]148
[3996]149 return rc;
150}
151
[3999]152// Taille des fichiers 4 voies = 1024*1024*128 ===> 32 x 1024 x 1024 samples / voie
153// On lit par paquet de 4096 = 4 x 1024 / voie --> ADCRDBLKSZ=16x1024=16384
154#define ADCFLEN 4096
155#define ADCRDBLKSZ 16384
156#define ADCNREAD 8192
157
[4001]158static int PRTMODULO = 1000;
[3999]159
[3996]160// Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
161/* --Fonction-- */
[4005]162int ProcessADCFiles(string& inoutpath, vector<int>& adcids, string& outname, bool fgcombiq, int imin, int imax, int istep, int jf1, int jf2, int nfreq)
[3996]163{
164 Timer tm("ProcessADCFiles");
165
[3999]166#define NFREQUSED 2040
167#define NFREQUSESTART 5
168#define NFREBIN 4
[3996]169
[3999]170 sa_size_t NFREQREBIN = NFREQUSED/NFREBIN;
171 sa_size_t NBADC = adcids.size();
172
[3996]173 sa_size_t NCHAN = 4*NBADC;
174 TMatrix< complex<r_4> > cfour(NCHAN, NFREQUSED);
175 sa_size_t NCORREL = NCHAN*(NCHAN+1)/2;
176 TMatrix< complex<r_8> > correl(NCORREL,NFREQUSED);
[3997]177 TMatrix< complex<r_8> > autocorrel(NCHAN,NFREQUSED);
[3999]178 double nsumcor=0.;
[3996]179
[3999]180 vector<FILE*> fip;
181 vector<int> adcchans;
182 for(size_t ka=0; ka<NBADC; ka++) {
183 fip.push_back(NULL);
[4003]184 for(size_t ica=0; ica<4; ica++)
185 adcchans.push_back(adcids[ka]*4+ica);
[3999]186 }
187
[3996]188 FFTPackServer ffts(false);
189
190 TVector<r_4> sigadc(ADCFLEN);
191 TVector< complex<r_4> > foursig;
192
193 char fname[512];
194
[3997]195 signed char rdbuff[ADCRDBLKSZ];
[3996]196 for(int ifile=imin; ifile<=imax; ifile+=istep) {
197 for(int ka=0; ka<NBADC; ka++) {
[3999]198 sprintf(fname, "%s/adcdumpN%dF%d.bd",inoutpath.c_str(),adcids[ka],ifile);
[3996]199 fip[ka]=fopen(fname,"rb");
[4002]200 if (fip[ka]==NULL) {
[3999]201 cout << "ProcessADCFiles/ERROR opening file " << fname << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
[4002]202 throw IOExc(" ADC dump file open error!");
203 }
[3999]204 else
205 cout << "ProcessADCFiles/Info - file " << fname << " opened ... " << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
[3996]206 }
207
208 for(int m=0; m<ADCNREAD; m++) {
209 sa_size_t irc=0;
210 for(int ka=0; ka<NBADC; ka++) {
211 fread(rdbuff,1,ADCRDBLKSZ,fip[ka]);
212 for(int jac=0; jac<4; jac++) {
[3997]213 for(sa_size_t ls=0; ls<ADCFLEN; ls++) sigadc(ls)=(float)rdbuff[ls*4+jac];
[3996]214 ffts.FFTForward(sigadc, foursig);
[3999]215 cfour.Row(irc)=foursig(Range(NFREQUSESTART,NFREQUSESTART+NFREQUSED-1)).Transpose(); irc++;
[3996]216 }
217 }
[4005]218 ComputeCorrel(cfour, fgcombiq, correl, autocorrel);
[3999]219 nsumcor++;
220 if (m%PRTMODULO==0) cout << " ProcessADCFiles/Info Done read+FFT+correl m=" << m << " / Max=" << ADCNREAD << endl;
[3996]221 }
222 for(int ka=0; ka<NBADC; ka++) fclose(fip[ka]);
223 }
224
225
[3999]226 correl /= nsumcor;
227 autocorrel /= nsumcor;
228
229 TMatrix< complex<r_8> > correbin(NCORREL,NFREQREBIN);
230 sa_size_t icc=0;
231 for(sa_size_t i=0; i<correbin.NCols(); i++) {
232 for(sa_size_t j=0; j<NFREBIN; j++) {
233 correbin.Column(i) += correl.Column(icc);
234 icc++;
235 }
236 }
237
[3996]238 sprintf(fname, "%s/%s",inoutpath.c_str(),outname.c_str());
239 cout << "ProcessADCFiles: Opening file " << fname << " for writing correlation matrix" << endl;
240 POutPersist po(fname);
[3997]241 po << PPFNameTag("correlmtx") << correl;
242 po << PPFNameTag("acorrmtx") << autocorrel;
[3999]243 po << PPFNameTag("rebincormtx") << correbin;
[3997]244
[3996]245 cout << "ProcessADCFiles: finished FFT + correlation computing " << endl;
[3999]246
247 cout << " --------------- Channel/Correlation number association --------------- " << endl;
[4006]248 cout << " CorreNumber- axb (MtxRow) : (Ca, Cb) -- ADCChan(c,d) -- I/Q(e,f)" << endl;
[3999]249 cout << " ----------------------------------------------------------------------- " << endl;
250 sa_size_t jcr=0;
[4006]251 char IQid[2];
252 char EWid[2];
253 int EWnum[2];
254 int Bid[2];
255 int Inpid[2];
256
[3999]257 for(sa_size_t j1=0; j1<cfour.NRows(); j1++) {
258 for(sa_size_t j2=j1; j2<cfour.NRows(); j2++) {
[4006]259 cout << jcr << " : (" << j1 << "," << j2 << ") -> ADCChans (" << adcchans[j1] << "," << adcchans[j2] << ") -- (";
260 Bid[0]=adcchans[j1]/4;
261 Bid[1]=adcchans[j2]/4;
262 Inpid[0]=adcchans[j1]%4;
263 Inpid[1]=adcchans[j2]%4;
264
265 if ((adcchans[j1]/4)%2==0) IQid[0]='I'; else IQid[0]='Q';
266 if ((adcchans[j2]/4)%2==0) IQid[1]='I'; else IQid[1]='Q';
267 if (adcchans[j1]%2==0) EWid[0]='E'; else EWid[0]='W';
268 if (adcchans[j2]%2==0) EWid[1]='E'; else EWid[1]='W';
[4007]269 cout << IQid[0] << '-' << EWid[0] << (Bid[0]/2)*2+Inpid[0]/2 << ","
270 << IQid[1] << '-' << EWid[1] << (Bid[1]/2)*2+Inpid[1]/2 << ")" << endl;
[3999]271 jcr++;
272 }
273 }
274 cout << " ----------------------------------------------------------------------- " << endl;
275
[3996]276 return 0;
277}
278
279/* --Fonction-- */
[4005]280int ComputeCorrel(TMatrix< complex<r_4> >& cfour, bool fgcombiq,
281 TMatrix< complex<r_8> >& correl, TMatrix< complex<r_8> >& autocorrel)
[3996]282{
[4005]283 if (fgcombiq) CombineIQPairs(cfour);
[3996]284 sa_size_t jcr=0;
285 for(sa_size_t j1=0; j1<cfour.NRows(); j1++) {
286 for(sa_size_t j2=j1; j2<cfour.NRows(); j2++) {
287 for(sa_size_t i=0; i<cfour.NCols(); i++)
288 correl(jcr,i) += complex<r_8> ( cfour(j1,i)*conj(cfour(j2,i)) );
[3997]289 if (j1==j2) autocorrel.Row(j1)=correl.Row(jcr);
[3996]290 jcr++;
291 }
292 }
293 return 0;
294}
295
[4005]296int CombineIQPairs(TMatrix< complex<r_4> >& cfour)
297{
298 // to be coded ...
299 return 0;
300}
301
[3999]302/* --Fonction-- */
303int ADCFiles2Histo(string& inoutpath, vector<int>& adcids, string& outname, int imin, int imax, int istep)
304{
305 Timer tm("ADCFiles2Histo");
306
307 sa_size_t NBADC = adcids.size();
308
309 sa_size_t NCHAN = 4*NBADC;
310 vector<Histo *> vhist;
311 for(int ka=0; ka<NCHAN; ka++)
312 vhist.push_back(new Histo(-128.5,128.5,257) );
313
314 vector<FILE*> fip;
315 for(size_t ka=0; ka<NBADC; ka++) fip.push_back(NULL);
316
317 char fname[512];
318
319 signed char rdbuff[ADCRDBLKSZ];
320 for(int ifile=imin; ifile<=imax; ifile+=istep) {
321 for(int ka=0; ka<NBADC; ka++) {
322 sprintf(fname, "%s/adcdumpN%dF%d.bd",inoutpath.c_str(),adcids[ka],ifile);
323 fip[ka]=fopen(fname,"rb");
[4002]324 if (fip[ka]==NULL) {
[3999]325 cout << "ADCFiles2Histo/ERROR opening file " << fname << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
[4002]326 throw IOExc(" ADC dump file open error!");
327 }
[3999]328 else
329 cout << "ADCFiles2Histo/Info - file " << fname << " opened ... " << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
330 }
331
332 for(int m=0; m<ADCNREAD; m++) {
333 sa_size_t irc=0;
334 for(int ka=0; ka<NBADC; ka++) {
335 fread(rdbuff,1,ADCRDBLKSZ,fip[ka]);
336 for(int jac=0; jac<4; jac++) {
337 for(sa_size_t ls=0; ls<ADCFLEN; ls++) vhist[irc]->Add((r_8)rdbuff[ls*4+jac]);
338 irc++;
339 }
340 }
341 if (m%PRTMODULO==0) cout << " ADCFiles2Histo/Info Done FillHist m=" << m << " / Max=" << ADCNREAD << endl;
342 }
343 for(int ka=0; ka<NBADC; ka++) fclose(fip[ka]);
344 }
345
346 sprintf(fname, "%s/%s",inoutpath.c_str(),outname.c_str());
347 cout << "ADCFiles2Histo: Opening file " << fname << " for writing ADC value histograms " << endl;
348 POutPersist po(fname);
349 char nametag[64];
350 for(size_t ka=0; ka<NCHAN; ka++) {
351 sprintf(nametag,"adcvalC%d",ka);
[4000]352 po << PPFNameTag(nametag) << *(vhist[ka]);
[3999]353 }
354 for(size_t ka=0; ka<NCHAN; ka++) delete vhist[ka];
355
356 cout << "ADCFiles2Histo: finished ADC value histogram fill " << endl;
357 return 0;
358}
Note: See TracBrowser for help on using the repository browser.