source: Sophya/trunk/AddOn/TAcq/brprocGain.cc@ 3943

Last change on this file since 3943 was 3943, checked in by campagne, 15 years ago

release.txt

File size: 5.6 KB
RevLine 
[3943]1//----------------------------------------------------------------
2// Projet BAORadio - (C) LAL/IRFU 2008-2010
3// Classes de threads de traitement pour BAORadio
4//----------------------------------------------------------------
5
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include <fstream>
10#include <signal.h>
11
12#include "pexceptions.h"
13#include "tvector.h"
14#include "ntuple.h"
15#include "datatable.h"
16#include "histos.h"
17#include "fioarr.h"
18#include "matharr.h"
19#include "timestamp.h"
20#include "ctimer.h"
21#include "fftpserver.h"
22#include "fitsarrhand.h"
23
24#include "FFTW/fftw3.h"
25
26
27#include "pciewrap.h"
28#include "brpaqu.h"
29
30#include "brprocGain.h"
31
32
33
34
35//---------------------------------------------------------------------
36// Classe de traitement de spectres -
37// Calcul de spectres de gain
38//---------------------------------------------------------------------
39/* --Methode-- */
40BRGainCalculator::BRGainCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean,
41 bool fgdatafft, bool fgsinglechan)
42 : BRMeanSpecCalculator(memgr,outpath,nmean,fgdatafft,fgsinglechan), forceMedianFreqFilter_(false),nbwin4mean_(nmean)
43{
44
45 // cout << "(JEC): BRGainCalculator Ctor " << endl;
46
47
48 setNameId("gainCalc",1);
49
50 uint_4 nb_octets_entrop = 0; //this value is valid for Dec. 2010 data at Nancay
51 const char* venvp = NULL;
52 venvp=getenv("BRANA_NBYTECUT");
53 if (venvp!=NULL){
54 nb_octets_entrop = atoi(venvp);
55 cout << "BRGainCalculator : BRANA_NBYTECUT : " << nb_octets_entrop << endl;
56 }
57
58 BRPaquet paq(memgr_.PaqSize()-nb_octets_entrop);
59
60 if (fgsinglechannel_) {
61 medfiltspecmtx_.SetSize(memgr_.NbFibres(), paq.DataSize()/2);
62 }
63 else {
64 medfiltspecmtx_.SetSize(2*memgr_.NbFibres(), paq.DataSize()/4);
65 }
66 //init
67 medfiltspecmtx_ = (r_4)(0.);
68 nummedianfile_ = 0;
69 nbtot_specwin_ = 0;
70
71}
72
73
74/* --Methode-- */
75BRGainCalculator::~BRGainCalculator()
76{
77 cout << "(JEC): BRGainCalculator Dtor: nbtot_specwin = " << nbtot_specwin_
78 << "nbwin4mean = " << nbwin4mean_ << endl;
79 if(nbtot_specwin_>nbwin4mean_*0.5)SaveMedianSpectra();
80}
81
82
83
84/* --Methode-- */
85void BRGainCalculator::ProcSpecWin(uint_8 numpaqstart, uint_8 numpaqend)
86{
87
88 cout << "(JEC): BRGainCalculator ProcSpecWin: "<< nbtot_specwin_
89 << " nbwin4mean = " << nbwin4mean_
90 << endl;
91
92 // cout<< "(Spec_window info) Start" << endl;
93 //spec_window_.Show();
94 //cout<< "(Spec_window info) End" << endl;
95
96 //JEC 13/12/10 save the filtered spectra
97 if ( (nbtot_specwin_>0)&&(nbtot_specwin_%nbwin4mean_==0) ) SaveMedianSpectra();
98
99
100 //DBG cout << "BRGainCalculator::ProcSpecWin()/Debug: numpaqstart=" << numpaqstart
101 //DBG << " numpaqend=" << numpaqend << endl;
102
103
104 //Implement the median filter at a fixed freq. but on all paquets of the window
105 //then apply a median filter on the frequencies
106 for(sa_size_t i=0; i<spec_window_.SizeY(); i++) { //loop on channel (voie)
107 TVector<r_4> medianFilterPaq(spec_window_.SizeX());
108 //we keep freq. = 0 for compatibility
109 for (sa_size_t f=0; f<spec_window_.SizeX(); f++) {
110 std::vector<r_4> val1;
111 TVector<r_4> tval1(spec_window_(Range(f),Range(i),Range::all()).CompactAllDimensions());
112 tval1.FillTo(val1);
113 std::nth_element(val1.begin(),
114 val1.begin()+val1.size()/2,
115 val1.end());
116 medianFilterPaq(f) = *(val1.begin()+val1.size()/2);
117 }//eo loop on frequencies
118
119
120
121 if(forceMedianFreqFilter_) {
122 //perform a median filter on the frequencies
123 TVector<r_4> medianFilterFreq = medfiltspecmtx_.Row(i);
124 sa_size_t hww = 50; //half freq window for filtering
125 sa_size_t fMin = 0.;
126 sa_size_t fMax = spec_window_.SizeX()-1;
127 for (sa_size_t f=0; f<spec_window_.SizeX(); f++) {
128 sa_size_t first = (f-hww >= fMin) ? f-hww : fMin;
129 sa_size_t last = (f+hww <= fMax) ? f+hww : fMax;
130 std::vector<r_4> val2;
131 medianFilterPaq(Range(first,last)).FillTo(val2);
132 std::nth_element(val2.begin(),
133 val2.begin()+val2.size()/2,
134 val2.end());
135 medianFilterFreq(f) += *(val2.begin()+val2.size()/2); //Notice the "+" for later take the mean
136 }//eo loop on frequencies
137 } else {
138 // cout<< "(JEC) ProcSpecWin debug medfiltspecmtx/medianFilterPaq "<< endl;
139 // medfiltspecmtx_.Row(i).Show();
140 // medianFilterPaq.Transpose().Show();
141 medfiltspecmtx_.Row(i) = medianFilterPaq.Transpose();
142 }
143 }//eo loop on channels
144
145 //We always end here with a medfiltspecmtx Matrix Row=[0,NCha-1] x Col=[0,fMax]
146
147
148 if (nbtot_specwin_<nmaxfiles_specw_) SaveSpectraWindow();
149
150
151 nbtot_specwin_++;
152 return;
153}
154
155
156/* --Methode-- */
157//JEC 13/12/10 Start
158void BRGainCalculator::SaveMedianSpectra()
159{
160
161 cout << "(JEC): BRGainCalculator SaveMedianSpectra ("<<nummedianfile_<<") nbwin = "<< nbwin4mean_
162 << " nbtot_specwin = " << nbtot_specwin_
163 << endl;
164
165 for(sa_size_t ir=0; ir< medfiltspecmtx_.NRows(); ir++){
166 char buff[32];
167 sprintf(buff,"NPAQSUM_%d",(int)ir);
168 medfiltspecmtx_.Info()["NPAQSUM"] = nbtot_specwin_;
169 medfiltspecmtx_.Info()[buff] = nbtot_specwin_;
170 if ( nbtot_specwin_ > 0) {
171 medfiltspecmtx_.Row(ir) /= (r_4)nbtot_specwin_;
172 }
173 }//eo loop on channels
174 char nfile[64];
175 string flnm;
176 {
177 sprintf(nfile,"medfiltmtx%d.fits",nummedianfile_);
178 flnm="!"+outpath_+nfile;
179 FitsInOutFile fos(flnm,FitsInOutFile::Fits_Create);
180 fos << medfiltspecmtx_;
181 }
182 cout << nummedianfile_ << "-BRGainCalculator::ProcSpecWin() save filtered spectra matrix in "
183 << flnm << endl;
184
185 //increment the file index
186 nummedianfile_++;
187
188 //reset the matirx
189 medfiltspecmtx_ = (r_4)(0.);
190
191 //reset counter
192 nbtot_specwin_ = 0;
193
194 return;
195}
196//JEC 13/12/10 End
Note: See TracBrowser for help on using the repository browser.