| [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 | //--------------------------------------------------------------------- | 
|---|
| [4016] | 40 | /*! | 
|---|
|  | 41 | \class BRGainCalculator | 
|---|
|  | 42 | \ingroup TAcq | 
|---|
|  | 43 |  | 
|---|
|  | 44 | \brief Gain/normalization computation on spectra (developped for Nancay data) | 
|---|
|  | 45 | */ | 
|---|
| [3943] | 46 | /* --Methode-- */ | 
|---|
|  | 47 | BRGainCalculator::BRGainCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, | 
|---|
|  | 48 | bool fgdatafft, bool fgsinglechan) | 
|---|
| [3992] | 49 | : BRMeanSpecCalculator(memgr,outpath,nmean,fgdatafft,fgsinglechan), forceMedianFreqFilter_(false), medianFilterHalfWidth_(50), nbwin4mean_(nmean) | 
|---|
| [3943] | 50 | { | 
|---|
|  | 51 |  | 
|---|
|  | 52 | //  cout << "(JEC): BRGainCalculator Ctor " << endl; | 
|---|
|  | 53 |  | 
|---|
|  | 54 |  | 
|---|
|  | 55 | setNameId("gainCalc",1); | 
|---|
|  | 56 |  | 
|---|
|  | 57 | uint_4 nb_octets_entrop = 0; //this value is valid for Dec. 2010 data at Nancay | 
|---|
| [3946] | 58 |  | 
|---|
| [3943] | 59 | const char* venvp = NULL; | 
|---|
|  | 60 | venvp=getenv("BRANA_NBYTECUT"); | 
|---|
|  | 61 | if (venvp!=NULL){ | 
|---|
|  | 62 | nb_octets_entrop = atoi(venvp); | 
|---|
|  | 63 | cout << "BRGainCalculator : BRANA_NBYTECUT : " << nb_octets_entrop << endl; | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | BRPaquet paq(memgr_.PaqSize()-nb_octets_entrop); | 
|---|
|  | 67 |  | 
|---|
|  | 68 | if (fgsinglechannel_) { | 
|---|
|  | 69 | medfiltspecmtx_.SetSize(memgr_.NbFibres(), paq.DataSize()/2); | 
|---|
|  | 70 | } | 
|---|
|  | 71 | else { | 
|---|
|  | 72 | medfiltspecmtx_.SetSize(2*memgr_.NbFibres(), paq.DataSize()/4); | 
|---|
|  | 73 | } | 
|---|
|  | 74 | //init | 
|---|
|  | 75 | medfiltspecmtx_ = (r_4)(0.); | 
|---|
|  | 76 | nummedianfile_ = 0; | 
|---|
|  | 77 | nbtot_specwin_ = 0; | 
|---|
| [3992] | 78 | windowTime_ = TimeStamp(); //default | 
|---|
| [3943] | 79 |  | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 |  | 
|---|
|  | 83 | /* --Methode-- */ | 
|---|
|  | 84 | BRGainCalculator::~BRGainCalculator() | 
|---|
|  | 85 | { | 
|---|
|  | 86 | cout << "(JEC): BRGainCalculator Dtor:  nbtot_specwin = " << nbtot_specwin_ | 
|---|
|  | 87 | << "nbwin4mean = " << nbwin4mean_ << endl; | 
|---|
|  | 88 | if(nbtot_specwin_>nbwin4mean_*0.5)SaveMedianSpectra(); | 
|---|
|  | 89 | } | 
|---|
|  | 90 |  | 
|---|
|  | 91 |  | 
|---|
|  | 92 |  | 
|---|
|  | 93 | /* --Methode-- */ | 
|---|
|  | 94 | void BRGainCalculator::ProcSpecWin(uint_8 numpaqstart, uint_8 numpaqend) | 
|---|
|  | 95 | { | 
|---|
|  | 96 |  | 
|---|
| [3992] | 97 | //   cout << "(JEC): BRGainCalculator ProcSpecWin: "<<   nbtot_specwin_ | 
|---|
|  | 98 | //        << endl; | 
|---|
| [3943] | 99 |  | 
|---|
|  | 100 |  | 
|---|
| [3992] | 101 |  | 
|---|
| [3943] | 102 | //JEC 13/12/10 save the filtered spectra | 
|---|
|  | 103 | if ( (nbtot_specwin_>0)&&(nbtot_specwin_%nbwin4mean_==0) ) SaveMedianSpectra(); | 
|---|
|  | 104 |  | 
|---|
| [3992] | 105 | // Get time of window | 
|---|
|  | 106 | windowTime_ = getObsTime(); | 
|---|
| [3943] | 107 |  | 
|---|
| [3992] | 108 | if (prtlev_>1)  { | 
|---|
|  | 109 | cout << " BRGainCalculator::ProcSpecWin() num_win=" << nbtot_specwin_ << " numpaqstart=" << numpaqstart | 
|---|
|  | 110 | << " numpaqend=" << numpaqend  << " nbwin4mean = " << nbwin4mean_ << endl; | 
|---|
|  | 111 | cout << " (1) ObsTime=" << windowTime_ << " TimeTag=" << getCurTimeTagSeconds() << " s. FrameCounter=" | 
|---|
|  | 112 | << getCurFrameCounter() << endl; | 
|---|
|  | 113 | } | 
|---|
| [3943] | 114 | //DBG  cout << "BRGainCalculator::ProcSpecWin()/Debug: numpaqstart=" << numpaqstart | 
|---|
|  | 115 | //DBG     << " numpaqend=" << numpaqend << endl; | 
|---|
|  | 116 |  | 
|---|
|  | 117 |  | 
|---|
|  | 118 | //Implement the median filter at a fixed freq. but on all paquets of the window | 
|---|
|  | 119 | //then apply a median filter on the frequencies | 
|---|
|  | 120 | for(sa_size_t i=0; i<spec_window_.SizeY(); i++)  { //loop on channel (voie) | 
|---|
|  | 121 | TVector<r_4> medianFilterPaq(spec_window_.SizeX()); | 
|---|
|  | 122 | //we keep freq. = 0 for compatibility | 
|---|
|  | 123 | for (sa_size_t f=0; f<spec_window_.SizeX(); f++) { | 
|---|
|  | 124 | std::vector<r_4> val1; | 
|---|
|  | 125 | TVector<r_4> tval1(spec_window_(Range(f),Range(i),Range::all()).CompactAllDimensions()); | 
|---|
|  | 126 | tval1.FillTo(val1); | 
|---|
|  | 127 | std::nth_element(val1.begin(), | 
|---|
|  | 128 | val1.begin()+val1.size()/2, | 
|---|
|  | 129 | val1.end()); | 
|---|
|  | 130 | medianFilterPaq(f) = *(val1.begin()+val1.size()/2); | 
|---|
|  | 131 | }//eo loop on frequencies | 
|---|
|  | 132 |  | 
|---|
|  | 133 |  | 
|---|
|  | 134 |  | 
|---|
|  | 135 | if(forceMedianFreqFilter_) { | 
|---|
|  | 136 | //perform a median filter on the frequencies | 
|---|
|  | 137 | TVector<r_4> medianFilterFreq = medfiltspecmtx_.Row(i); | 
|---|
| [3992] | 138 | sa_size_t hww = medianFilterHalfWidth_; //half freq window for filtering | 
|---|
| [3943] | 139 | sa_size_t fMin = 0.; | 
|---|
|  | 140 | sa_size_t fMax = spec_window_.SizeX()-1; | 
|---|
|  | 141 | for (sa_size_t f=0; f<spec_window_.SizeX(); f++) { | 
|---|
|  | 142 | sa_size_t first = (f-hww >= fMin) ? f-hww : fMin; | 
|---|
|  | 143 | sa_size_t last = (f+hww <= fMax) ? f+hww : fMax; | 
|---|
|  | 144 | std::vector<r_4> val2; | 
|---|
|  | 145 | medianFilterPaq(Range(first,last)).FillTo(val2); | 
|---|
|  | 146 | std::nth_element(val2.begin(), | 
|---|
|  | 147 | val2.begin()+val2.size()/2, | 
|---|
|  | 148 | val2.end()); | 
|---|
| [3946] | 149 | medianFilterFreq(f) = *(val2.begin()+val2.size()/2); //Notice the "+" for later take the mean | 
|---|
| [3943] | 150 | }//eo loop on frequencies | 
|---|
|  | 151 | } else { | 
|---|
|  | 152 | //      cout<< "(JEC) ProcSpecWin debug medfiltspecmtx/medianFilterPaq "<< endl; | 
|---|
|  | 153 | //      medfiltspecmtx_.Row(i).Show(); | 
|---|
|  | 154 | //      medianFilterPaq.Transpose().Show(); | 
|---|
| [3946] | 155 | medfiltspecmtx_.Row(i) += medianFilterPaq.Transpose(); | 
|---|
| [3943] | 156 | } | 
|---|
|  | 157 | }//eo loop on channels | 
|---|
|  | 158 |  | 
|---|
|  | 159 | //We always end here with a medfiltspecmtx Matrix Row=[0,NCha-1] x Col=[0,fMax] | 
|---|
|  | 160 |  | 
|---|
|  | 161 |  | 
|---|
|  | 162 | if (nbtot_specwin_<nmaxfiles_specw_)  SaveSpectraWindow(); | 
|---|
|  | 163 |  | 
|---|
|  | 164 |  | 
|---|
|  | 165 | nbtot_specwin_++; | 
|---|
|  | 166 | return; | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
|  | 169 |  | 
|---|
|  | 170 | /* --Methode-- */ | 
|---|
|  | 171 | //JEC 13/12/10 Start | 
|---|
|  | 172 | void BRGainCalculator::SaveMedianSpectra() | 
|---|
|  | 173 | { | 
|---|
|  | 174 |  | 
|---|
|  | 175 | cout << "(JEC): BRGainCalculator   SaveMedianSpectra ("<<nummedianfile_<<") nbwin = "<< nbwin4mean_ | 
|---|
|  | 176 | << " nbtot_specwin = " <<  nbtot_specwin_ | 
|---|
|  | 177 | << endl; | 
|---|
| [3992] | 178 | if (prtlev_>1)  { | 
|---|
|  | 179 | cout << " BRGainCalculator:SaveMedianSpectra() nbwin4mean = " << nbwin4mean_ << endl | 
|---|
|  | 180 | << " (2) ObsTime=" << windowTime_ << " Total Intensity " << 0.5*medfiltspecmtx_.Sum() << endl; | 
|---|
|  | 181 | } | 
|---|
| [3943] | 182 |  | 
|---|
| [3992] | 183 | //JEC compute power spectrum | 
|---|
|  | 184 |  | 
|---|
|  | 185 |  | 
|---|
|  | 186 |  | 
|---|
|  | 187 |  | 
|---|
|  | 188 | for(sa_size_t ir=0; ir< medfiltspecmtx_.NRows(); ir++){ | 
|---|
|  | 189 | char buff[32]; | 
|---|
|  | 190 | sprintf(buff,"NPAQSUM_%d",(int)ir); | 
|---|
|  | 191 | medfiltspecmtx_.Info()["NPAQSUM"] = nbtot_specwin_; | 
|---|
|  | 192 | medfiltspecmtx_.Info()[buff] = nbtot_specwin_; | 
|---|
|  | 193 | if ( nbtot_specwin_ > 0) { | 
|---|
|  | 194 | medfiltspecmtx_.Row(ir)  /= (r_4)nbtot_specwin_; | 
|---|
| [3943] | 195 | } | 
|---|
| [3992] | 196 | }//eo loop on channels | 
|---|
| [3993] | 197 | //JEC timeStamp in the day @ millisec: faudra gerer le passage a minuit! | 
|---|
|  | 198 | //Reza : getObsTimeSeconds() fournit le temps ecoule en seconde a partir de 0h00 de la date du premier fichier | 
|---|
|  | 199 | medfiltspecmtx_.Info()["TIMEWIN"] = (windowTime_.SecondsPart())*1000.; | 
|---|
|  | 200 | if (nbpmoyttfc_>1)  { | 
|---|
|  | 201 | moyfc_/=(double)nbpmoyttfc_; | 
|---|
|  | 202 | moytt_/=(double)nbpmoyttfc_; | 
|---|
|  | 203 | } | 
|---|
|  | 204 | string ikey,ikdesc; | 
|---|
|  | 205 | ikey="MeanFC";  ikdesc="Mean FrameCounter"; | 
|---|
|  | 206 | medfiltspecmtx_.Info().SetI(ikey,moyfc_); | 
|---|
|  | 207 | medfiltspecmtx_.Info().SetComment(ikey,ikdesc); | 
|---|
|  | 208 | ikey="MeanTT";  ikdesc="Mean TimeTag"; | 
|---|
|  | 209 | medfiltspecmtx_.Info().SetD(ikey,moytt_); | 
|---|
|  | 210 | medfiltspecmtx_.Info().SetComment(ikey,ikdesc); | 
|---|
|  | 211 |  | 
|---|
| [3992] | 212 | char nfile[64]; | 
|---|
|  | 213 | string flnm; | 
|---|
|  | 214 | { | 
|---|
|  | 215 | sprintf(nfile,"medfiltmtx%d.fits",nummedianfile_); | 
|---|
|  | 216 | flnm="!"+outpath_+nfile; | 
|---|
|  | 217 | FitsInOutFile fos(flnm,FitsInOutFile::Fits_Create); | 
|---|
|  | 218 | fos << medfiltspecmtx_; | 
|---|
| [3943] | 219 | cout << nummedianfile_ << "-BRGainCalculator::ProcSpecWin() save filtered spectra matrix in " | 
|---|
|  | 220 | << flnm << endl; | 
|---|
| [3992] | 221 | } | 
|---|
|  | 222 |  | 
|---|
|  | 223 | //increment the file index | 
|---|
|  | 224 | nummedianfile_++; | 
|---|
| [3993] | 225 |  | 
|---|
| [3994] | 226 | if (dtpms_!=NULL)  FillPwrTmDTable(medfiltspecmtx_); | 
|---|
| [3943] | 227 |  | 
|---|
| [3992] | 228 | //reset the matirx | 
|---|
|  | 229 | medfiltspecmtx_ = (r_4)(0.); | 
|---|
|  | 230 | //reset counter | 
|---|
|  | 231 | nbtot_specwin_ = 0; | 
|---|
| [3993] | 232 | moyfc_=moytt_=0.; | 
|---|
|  | 233 | nbpmoyttfc_=0; | 
|---|
| [3943] | 234 |  | 
|---|
| [3992] | 235 | return; | 
|---|
| [3943] | 236 | } | 
|---|
|  | 237 | //JEC 13/12/10 End | 
|---|