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