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

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

secure the code from non initialisation in certain options (JEC)

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
[3946]51
[3943]52 const char* venvp = NULL;
53 venvp=getenv("BRANA_NBYTECUT");
54 if (venvp!=NULL){
55 nb_octets_entrop = atoi(venvp);
56 cout << "BRGainCalculator : BRANA_NBYTECUT : " << nb_octets_entrop << endl;
57 }
58
59 BRPaquet paq(memgr_.PaqSize()-nb_octets_entrop);
60
61 if (fgsinglechannel_) {
62 medfiltspecmtx_.SetSize(memgr_.NbFibres(), paq.DataSize()/2);
63 }
64 else {
65 medfiltspecmtx_.SetSize(2*memgr_.NbFibres(), paq.DataSize()/4);
66 }
67 //init
68 medfiltspecmtx_ = (r_4)(0.);
69 nummedianfile_ = 0;
70 nbtot_specwin_ = 0;
71
72}
73
74
75/* --Methode-- */
76BRGainCalculator::~BRGainCalculator()
77{
78 cout << "(JEC): BRGainCalculator Dtor: nbtot_specwin = " << nbtot_specwin_
79 << "nbwin4mean = " << nbwin4mean_ << endl;
80 if(nbtot_specwin_>nbwin4mean_*0.5)SaveMedianSpectra();
81}
82
83
84
85/* --Methode-- */
86void BRGainCalculator::ProcSpecWin(uint_8 numpaqstart, uint_8 numpaqend)
87{
88
[3946]89// cout << "(JEC): BRGainCalculator ProcSpecWin: "<< nbtot_specwin_
90// << " nbwin4mean = " << nbwin4mean_
91// << endl;
[3943]92
93 // cout<< "(Spec_window info) Start" << endl;
94 //spec_window_.Show();
95 //cout<< "(Spec_window info) End" << endl;
96
97 //JEC 13/12/10 save the filtered spectra
98 if ( (nbtot_specwin_>0)&&(nbtot_specwin_%nbwin4mean_==0) ) SaveMedianSpectra();
99
100
101 //DBG cout << "BRGainCalculator::ProcSpecWin()/Debug: numpaqstart=" << numpaqstart
102 //DBG << " numpaqend=" << numpaqend << endl;
103
104
105 //Implement the median filter at a fixed freq. but on all paquets of the window
106 //then apply a median filter on the frequencies
107 for(sa_size_t i=0; i<spec_window_.SizeY(); i++) { //loop on channel (voie)
108 TVector<r_4> medianFilterPaq(spec_window_.SizeX());
109 //we keep freq. = 0 for compatibility
110 for (sa_size_t f=0; f<spec_window_.SizeX(); f++) {
111 std::vector<r_4> val1;
112 TVector<r_4> tval1(spec_window_(Range(f),Range(i),Range::all()).CompactAllDimensions());
113 tval1.FillTo(val1);
114 std::nth_element(val1.begin(),
115 val1.begin()+val1.size()/2,
116 val1.end());
117 medianFilterPaq(f) = *(val1.begin()+val1.size()/2);
118 }//eo loop on frequencies
119
120
121
122 if(forceMedianFreqFilter_) {
123 //perform a median filter on the frequencies
124 TVector<r_4> medianFilterFreq = medfiltspecmtx_.Row(i);
125 sa_size_t hww = 50; //half freq window for filtering
126 sa_size_t fMin = 0.;
127 sa_size_t fMax = spec_window_.SizeX()-1;
128 for (sa_size_t f=0; f<spec_window_.SizeX(); f++) {
129 sa_size_t first = (f-hww >= fMin) ? f-hww : fMin;
130 sa_size_t last = (f+hww <= fMax) ? f+hww : fMax;
131 std::vector<r_4> val2;
132 medianFilterPaq(Range(first,last)).FillTo(val2);
133 std::nth_element(val2.begin(),
134 val2.begin()+val2.size()/2,
135 val2.end());
[3946]136 medianFilterFreq(f) = *(val2.begin()+val2.size()/2); //Notice the "+" for later take the mean
[3943]137 }//eo loop on frequencies
138 } else {
139 // cout<< "(JEC) ProcSpecWin debug medfiltspecmtx/medianFilterPaq "<< endl;
140 // medfiltspecmtx_.Row(i).Show();
141 // medianFilterPaq.Transpose().Show();
[3946]142 medfiltspecmtx_.Row(i) += medianFilterPaq.Transpose();
[3943]143 }
144 }//eo loop on channels
145
146 //We always end here with a medfiltspecmtx Matrix Row=[0,NCha-1] x Col=[0,fMax]
147
148
149 if (nbtot_specwin_<nmaxfiles_specw_) SaveSpectraWindow();
150
151
152 nbtot_specwin_++;
153 return;
154}
155
156
157/* --Methode-- */
158//JEC 13/12/10 Start
159void BRGainCalculator::SaveMedianSpectra()
160{
161
162 cout << "(JEC): BRGainCalculator SaveMedianSpectra ("<<nummedianfile_<<") nbwin = "<< nbwin4mean_
163 << " nbtot_specwin = " << nbtot_specwin_
164 << endl;
165
166 for(sa_size_t ir=0; ir< medfiltspecmtx_.NRows(); ir++){
167 char buff[32];
168 sprintf(buff,"NPAQSUM_%d",(int)ir);
169 medfiltspecmtx_.Info()["NPAQSUM"] = nbtot_specwin_;
170 medfiltspecmtx_.Info()[buff] = nbtot_specwin_;
171 if ( nbtot_specwin_ > 0) {
172 medfiltspecmtx_.Row(ir) /= (r_4)nbtot_specwin_;
173 }
174 }//eo loop on channels
175 char nfile[64];
176 string flnm;
177 {
178 sprintf(nfile,"medfiltmtx%d.fits",nummedianfile_);
179 flnm="!"+outpath_+nfile;
180 FitsInOutFile fos(flnm,FitsInOutFile::Fits_Create);
181 fos << medfiltspecmtx_;
182 }
183 cout << nummedianfile_ << "-BRGainCalculator::ProcSpecWin() save filtered spectra matrix in "
184 << flnm << endl;
185
186 //increment the file index
187 nummedianfile_++;
188
189 //reset the matirx
190 medfiltspecmtx_ = (r_4)(0.);
191
192 //reset counter
193 nbtot_specwin_ = 0;
194
195 return;
196}
197//JEC 13/12/10 End
Note: See TracBrowser for help on using the repository browser.