| 1 | // Utilisation de SOPHYA pour faciliter les tests ... | 
|---|
| 2 | #include "sopnamsp.h" | 
|---|
| 3 | #include "machdefs.h" | 
|---|
| 4 |  | 
|---|
| 5 | /* ---------------------------------------------------------- | 
|---|
| 6 | Projet BAORadio - (C) LAL/IRFU  2011 | 
|---|
| 7 |  | 
|---|
| 8 | Programme de lecture des fichiers matrices de visibilites | 
|---|
| 9 | au format fits pour ecrire une DataTable de visib_moyenne(temps) | 
|---|
| 10 | Fichier de visibilites produits par le prog vismf | 
|---|
| 11 | R. Ansari, C. Magneville   -  LAL/Irfu | 
|---|
| 12 | V : Mai 2009 | 
|---|
| 13 | ---------------------------------------------------------- */ | 
|---|
| 14 |  | 
|---|
| 15 | // include standard c/c++ | 
|---|
| 16 | #include <math.h> | 
|---|
| 17 | #include <stdio.h> | 
|---|
| 18 | #include <stdlib.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 |  | 
|---|
| 21 | #include <iostream> | 
|---|
| 22 | #include <string> | 
|---|
| 23 |  | 
|---|
| 24 | #include "pexceptions.h" | 
|---|
| 25 | #include "tvector.h" | 
|---|
| 26 | #include "fioarr.h" | 
|---|
| 27 | // #include "tarrinit.h" | 
|---|
| 28 | #include "ntuple.h" | 
|---|
| 29 | #include "datatable.h" | 
|---|
| 30 | #include "histinit.h" | 
|---|
| 31 | #include "matharr.h" | 
|---|
| 32 | #include "timestamp.h" | 
|---|
| 33 | #include <utilarr.h> | 
|---|
| 34 | #include "fitsarrhand.h" | 
|---|
| 35 | #include "fitshdtable.h" | 
|---|
| 36 |  | 
|---|
| 37 | // include sophya mesure ressource CPU/memoire ... | 
|---|
| 38 | #include "resusage.h" | 
|---|
| 39 | #include "ctimer.h" | 
|---|
| 40 | #include "timing.h" | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | //--------------------------- Fonctions de ce fichier   ------------------- | 
|---|
| 44 | int Usage(bool fgshort=true); | 
|---|
| 45 |  | 
|---|
| 46 | int DecodeProc(int narg, char* arg[]); | 
|---|
| 47 | int ProcVisMtxFiles(string& inoutpath, int imin, int imax, int istep, int jf1, int jf2, int nfreq, | 
|---|
| 48 | vector<sa_size_t> tfrlist); | 
|---|
| 49 | int FillVisDTable(BaseDataTable& visdt_, TMatrix< r_4 > vismtx_, TVector< int_4> chanum_, | 
|---|
| 50 | sa_size_t jf1_, sa_size_t jf2_, sa_size_t djf_) ; | 
|---|
| 51 |  | 
|---|
| 52 | //------------------------------------------------------------------------------------------------------------ | 
|---|
| 53 |  | 
|---|
| 54 | /* --Fonction-- */ | 
|---|
| 55 | int Usage(bool fgshort) | 
|---|
| 56 | { | 
|---|
| 57 | cout << " --- visfits2dt : Read fits visibility matrices produced by visfmib to make \n" | 
|---|
| 58 | << "     time-frequency matrices and Visibilites=f(time) datatable " << endl; | 
|---|
| 59 | cout << " Usage: visfits2dt InOutPath Imin,Imax,step NumFreq1,NumFreq2,NBinFreq VisMtxRowList [DT] \n" << endl; | 
|---|
| 60 | if (fgshort) { | 
|---|
| 61 | cout << " svv2mtx -h for detailed instructions" << endl; | 
|---|
| 62 | return 1; | 
|---|
| 63 | } | 
|---|
| 64 | cout << " InOutPath : Input/Output directory name \n" | 
|---|
| 65 | << " Imin,Imax,IStep: Input FITS  files sequence number (vismtxII.fits) \n" | 
|---|
| 66 | << "    FileNames=InOutPath/vismtxII.fits Imin<=II<=Imax II+=IStep \n" | 
|---|
| 67 | << " NumFreq1,NumFreq2,NBinFreq: Freq Zone and number of frequency bins for DataTable\n" | 
|---|
| 68 | << " VisMtxRowList : List of visibiliy matrix rows (0,2,...) -> time-freq map \n" | 
|---|
| 69 | << " DT : Fill datatable if DT arg specified " << endl; | 
|---|
| 70 | return 1; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | //---------------------------------------------------- | 
|---|
| 74 | //---------------------------------------------------- | 
|---|
| 75 | int main(int narg, char* arg[]) | 
|---|
| 76 | { | 
|---|
| 77 | if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage(false); | 
|---|
| 78 | if (narg<5) return Usage(true); | 
|---|
| 79 | HiStatsInitiator _inia; | 
|---|
| 80 | //   TArrayInitiator  _inia; | 
|---|
| 81 |  | 
|---|
| 82 | int rc = 0; | 
|---|
| 83 | try { | 
|---|
| 84 | bool fgvjun09=false; | 
|---|
| 85 | ResourceUsage resu; | 
|---|
| 86 | DecodeProc(narg-1, arg+1); | 
|---|
| 87 | resu.Update(); | 
|---|
| 88 | cout << resu; | 
|---|
| 89 | } | 
|---|
| 90 | catch (PException& exc) { | 
|---|
| 91 | cerr << " visfits2dt.cc catched PException " << exc.Msg() << endl; | 
|---|
| 92 | rc = 77; | 
|---|
| 93 | } | 
|---|
| 94 | catch (std::exception& sex) { | 
|---|
| 95 | cerr << "\n visfits2dt.cc std::exception :" | 
|---|
| 96 | << (string)typeid(sex).name() << "\n msg= " | 
|---|
| 97 | << sex.what() << endl; | 
|---|
| 98 | rc = 78; | 
|---|
| 99 | } | 
|---|
| 100 | catch (...) { | 
|---|
| 101 | cerr << " visfits2dt.cc catched unknown (...) exception  " << endl; | 
|---|
| 102 | rc = 79; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | cout << ">>>> visfits2dt.cc ------- END ----------- RC=" << rc << endl; | 
|---|
| 106 | return rc; | 
|---|
| 107 |  | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | //-------------------------------------------------------------------- | 
|---|
| 111 | //       Traitement fichiers produits par vismfib  (V Nov09) | 
|---|
| 112 | /* --Fonction-- */ | 
|---|
| 113 | static  bool fgfilldt=false; | 
|---|
| 114 |  | 
|---|
| 115 | int DecodeProc(int narg, char* arg[]) | 
|---|
| 116 | { | 
|---|
| 117 | // Decodage des arguments et traitement | 
|---|
| 118 | string inoutpath = arg[0]; | 
|---|
| 119 | int imin=0; | 
|---|
| 120 | int imax=0; | 
|---|
| 121 | int istep=1; | 
|---|
| 122 | sscanf(arg[1],"%d,%d,%d",&imin,&imax,&istep); | 
|---|
| 123 | int jf1=0; | 
|---|
| 124 | int jf2=0; | 
|---|
| 125 | int nfreq=0; | 
|---|
| 126 | sscanf(arg[2],"%d,%d,%d",&jf1,&jf2,&nfreq); | 
|---|
| 127 | int card=1; | 
|---|
| 128 | vector<sa_size_t> rowlist; | 
|---|
| 129 | EnumeratedSequence es; | 
|---|
| 130 | int nbad; | 
|---|
| 131 | es.Append(arg[3], nbad, ","); | 
|---|
| 132 | for(int j=0; j<es.Size(); j++) rowlist.push_back((int)es.Value(j)); | 
|---|
| 133 | fgfilldt=false; | 
|---|
| 134 | if ((narg>4)&&(strcmp(arg[4],"DT")==0))  fgfilldt=true; | 
|---|
| 135 | //  if (rowlist.size()<1) rowlist.push_back(0); | 
|---|
| 136 |  | 
|---|
| 137 | cout << " ----- visfits2dt/DecodeProc - Start - InOutPath= " << inoutpath << " IMin,Max,Step=" | 
|---|
| 138 | << imin << "," << imax << "," << istep << " Card=" << card << endl; | 
|---|
| 139 | cout << "Frequency num range JF=" << jf1 << "," << jf2 << "," << nfreq << "  ------- " << endl; | 
|---|
| 140 | cout << " RowList: " ; | 
|---|
| 141 | for(int j=0; j<rowlist.size(); j++)  cout << rowlist[j] << " , " ; | 
|---|
| 142 | cout << endl; | 
|---|
| 143 | int rc=ProcVisMtxFiles(inoutpath, imin, imax, istep, jf1, jf2, nfreq, rowlist); | 
|---|
| 144 | return rc; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | // Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW) | 
|---|
| 148 | /* --Fonction-- */ | 
|---|
| 149 | int ProcVisMtxFiles(string& inoutpath, int imin, int imax, int istep, int jf1, int jf2, int nfreq, | 
|---|
| 150 | vector<sa_size_t> rowlist) | 
|---|
| 151 | { | 
|---|
| 152 | Timer tm("ProcVisMtxFiles"); | 
|---|
| 153 |  | 
|---|
| 154 | DataTable visdt; | 
|---|
| 155 | visdt.AddDoubleColumn("mfc"); | 
|---|
| 156 | visdt.AddDoubleColumn("mtt"); | 
|---|
| 157 | visdt.AddIntegerColumn("jfreq"); | 
|---|
| 158 | visdt.AddIntegerColumn("numch"); | 
|---|
| 159 | visdt.AddFloatColumn("vre"); | 
|---|
| 160 | visdt.AddFloatColumn("vim"); | 
|---|
| 161 |  | 
|---|
| 162 | vector< TMatrix< r_4 > > vmtf; | 
|---|
| 163 | vector< TMatrix< r_4 > > vmtfa; | 
|---|
| 164 |  | 
|---|
| 165 | if (jf1<1)  jf1=1; | 
|---|
| 166 | if ((jf2<1)||(jf2<jf1))  jf2=jf1; | 
|---|
| 167 | if (nfreq<1) nfreq=1; | 
|---|
| 168 | int djf=(jf2-jf1)/nfreq; | 
|---|
| 169 | if (djf<1) djf=1; | 
|---|
| 170 |  | 
|---|
| 171 | cout << " --- ProcVisMtxFiles Frequency binng Start=" << jf1 << " End= " << jf2 << " DFreq=" << djf << " NBinFreq=" << nfreq << endl; | 
|---|
| 172 | char fname[1024]; | 
|---|
| 173 |  | 
|---|
| 174 | cout << " ---- ProcVisMtxFiles()-Begin - Reading chanum vector" << endl; | 
|---|
| 175 | TVector< int_4 > chanum; | 
|---|
| 176 | { | 
|---|
| 177 | sprintf(fname, "%s/chanum.fits",inoutpath.c_str()); | 
|---|
| 178 | FitsInOutFile fic(fname, FitsInOutFile::Fits_RO); | 
|---|
| 179 | fic.MoveAbsToHDU(3); | 
|---|
| 180 | fic >> chanum; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | cout << " ---- ProcVisMtxFiles()-Read chanum done " << endl; | 
|---|
| 184 | sa_size_t nrows = (imax-imin+1)/istep; | 
|---|
| 185 | sa_size_t ktime=0; | 
|---|
| 186 |  | 
|---|
| 187 | //  sa_size_t mtf_binfreq=25; | 
|---|
| 188 | sa_size_t mtf_bintime=1; | 
|---|
| 189 |  | 
|---|
| 190 | sa_size_t ncols=1; | 
|---|
| 191 |  | 
|---|
| 192 | for(int ifile=imin; ifile<=imax; ifile+=istep) { | 
|---|
| 193 | sprintf(fname, "%s/vismtx%d.fits",inoutpath.c_str(),ifile); | 
|---|
| 194 | cout << " ProcVisMtxFiles[" << ifile << "] opening file " << fname << endl; | 
|---|
| 195 | FitsInOutFile fin(fname, FitsInOutFile::Fits_RO); | 
|---|
| 196 | TMatrix< r_4 > vismtx; | 
|---|
| 197 |  | 
|---|
| 198 | fin >> vismtx; | 
|---|
| 199 | //    cout << vismtx.Info(); | 
|---|
| 200 |  | 
|---|
| 201 | if ((ifile==imin)&&(rowlist.size()>0)) { | 
|---|
| 202 | ncols = vismtx.NCols(); | 
|---|
| 203 | cout << " ProcVisMtxFiles/Info: Output Time-Frequency matrices NRows(time) " | 
|---|
| 204 | << nrows/mtf_bintime+1 << " NCols=2*NFreq) =" << nfreq << endl; | 
|---|
| 205 | for(size_t j=0; j<rowlist.size(); j++) { | 
|---|
| 206 | vmtf.push_back(TMatrix< r_4 >(nrows/mtf_bintime+1, nfreq*2)); | 
|---|
| 207 | vmtfa.push_back(TMatrix< r_4 >(nrows/mtf_bintime+1, nfreq)); | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | if (rowlist.size()>0) { | 
|---|
| 212 | for(size_t j=0; j<rowlist.size(); j++) { | 
|---|
| 213 | sa_size_t jfreb=0; | 
|---|
| 214 | for(sa_size_t jf=jf1; jf<jf2; jf++) { | 
|---|
| 215 | sa_size_t jfreb=(jf-jf1)/djf; | 
|---|
| 216 | if (jfreb>=nfreq)  break; | 
|---|
| 217 | vmtf[j](ktime/mtf_bintime,jfreb*2)+=vismtx(rowlist[j],jf*2); // partie reelle | 
|---|
| 218 | vmtf[j](ktime/mtf_bintime,jfreb*2+1)+=vismtx(rowlist[j],jf*2+1); // partie imaginaire | 
|---|
| 219 | vmtfa[j](ktime/mtf_bintime,jfreb)+=vismtx(rowlist[j],jf*2)*vismtx(rowlist[j],jf*2); // module carre - z.real^2 | 
|---|
| 220 | vmtfa[j](ktime/mtf_bintime,jfreb)+=vismtx(rowlist[j],jf*2+1)*vismtx(rowlist[j],jf*2+1); // module carre z.imag^2 | 
|---|
| 221 | } | 
|---|
| 222 | } | 
|---|
| 223 | } | 
|---|
| 224 | //    cout << " DBG* kr=" << kr << " kr/mtf_bintime=" << kr/mtf_bintime | 
|---|
| 225 | //     << " ncols/2/mtf_binfreq=" << ncols/2/mtf_binfreq << endl; | 
|---|
| 226 | ktime++; | 
|---|
| 227 |  | 
|---|
| 228 |  | 
|---|
| 229 | // Calcul moyenne dans des bandes en frequence | 
|---|
| 230 | if (fgfilldt)   FillVisDTable(visdt, vismtx, chanum, jf1, jf2, djf); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | if (rowlist.size()>0) { | 
|---|
| 234 | sprintf(fname, "!%s/vistfreqmtx.fits",inoutpath.c_str()); | 
|---|
| 235 | cout << "ProcVisMtxFiles: Opening file " << fname << " for writing Visi(Freq,Time) matrices" << endl; | 
|---|
| 236 | FitsInOutFile fo(fname, FitsInOutFile::Fits_Create); | 
|---|
| 237 | for(size_t j=0; j<rowlist.size(); j++)  { | 
|---|
| 238 | cout << " Writing Time-Freq visibility matrix[" << j << "] for VisIJ= " << chanum(rowlist[j]) << endl; | 
|---|
| 239 | fo << vmtf[j]; | 
|---|
| 240 | } | 
|---|
| 241 | sprintf(fname, "!%s/acortfreqmtx.fits",inoutpath.c_str()); | 
|---|
| 242 | cout << "ProcVisMtxFiles: Opening file " << fname << " for writing Visi(Freq,Time) matrices" << endl; | 
|---|
| 243 | FitsInOutFile foa(fname, FitsInOutFile::Fits_Create); | 
|---|
| 244 | for(size_t j=0; j<rowlist.size(); j++)  { | 
|---|
| 245 | cout << " Writing Time-Freq modulesquare matrix[" << j << "] for Mod2IJ= " << chanum(rowlist[j]) << endl; | 
|---|
| 246 | foa << vmtfa[j]; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | } | 
|---|
| 250 | if (fgfilldt)  { | 
|---|
| 251 | cout << visdt; | 
|---|
| 252 | sprintf(fname, "!%s/visidt.fits",inoutpath.c_str()); | 
|---|
| 253 | cout << "ProcVisMtxFiles: writing visibility datatable to file " << fname << endl; | 
|---|
| 254 | FitsInOutFile fod(fname, FitsInOutFile::Fits_Create); | 
|---|
| 255 | fod << visdt; | 
|---|
| 256 | } | 
|---|
| 257 | return 0; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | /* --Fonction-- */ | 
|---|
| 261 | int FillVisDTable(BaseDataTable& visdt_, TMatrix< r_4 > vismtx_, TVector< int_4> chanum_, | 
|---|
| 262 | sa_size_t jf1_, sa_size_t jf2_, sa_size_t djf_) | 
|---|
| 263 | { | 
|---|
| 264 | double xnt_[20]; | 
|---|
| 265 | xnt_[0]=(double)vismtx_.Info()["MEANFC"]; | 
|---|
| 266 | xnt_[1]=(double)vismtx_.Info()["MEANTT"]/1.25e8; | 
|---|
| 267 |  | 
|---|
| 268 | uint_4 nmean_=vismtx_.Info()["NPAQSUM"]; | 
|---|
| 269 | if (djf_<2) { | 
|---|
| 270 | for(sa_size_t rv=0; rv<vismtx_.NRows(); rv++) { | 
|---|
| 271 | for(sa_size_t jf=jf1_; jf<jf2_; jf++) { | 
|---|
| 272 | xnt_[2]=jf; | 
|---|
| 273 | xnt_[3]=chanum_(rv); | 
|---|
| 274 | xnt_[4]=vismtx_(rv,jf*2)/(r_4)(nmean_); | 
|---|
| 275 | xnt_[5]=vismtx_(rv,jf*2+1)/(r_4)(nmean_); | 
|---|
| 276 | visdt_.AddRow(xnt_); | 
|---|
| 277 | } | 
|---|
| 278 | } | 
|---|
| 279 | } | 
|---|
| 280 | else { | 
|---|
| 281 | for(sa_size_t rv=0; rv<vismtx_.NRows(); rv++) { | 
|---|
| 282 | for(sa_size_t jf=jf1_; jf<jf2_; jf+=djf_) { | 
|---|
| 283 | r_4 moyreal=0.; | 
|---|
| 284 | r_4 moyimag=0.; | 
|---|
| 285 | sa_size_t jjfmx=jf+djf_; | 
|---|
| 286 | if (jjfmx > vismtx_.NCols()) jjfmx=vismtx_.NCols(); | 
|---|
| 287 | for(sa_size_t jjf=jf; jjf<jjfmx; jjf++) { | 
|---|
| 288 | moyreal+=vismtx_(rv,jjf*2); | 
|---|
| 289 | moyimag+=vismtx_(rv,jjf*2+1); | 
|---|
| 290 | } | 
|---|
| 291 | xnt_[2]=jf+djf_/2; | 
|---|
| 292 | xnt_[3]=chanum_(rv); | 
|---|
| 293 | xnt_[4]=moyreal/(r_4)(nmean_*djf_); | 
|---|
| 294 | xnt_[5]=moyimag/(r_4)(nmean_*djf_); | 
|---|
| 295 | visdt_.AddRow(xnt_); | 
|---|
| 296 | } | 
|---|
| 297 | } | 
|---|
| 298 | } | 
|---|
| 299 | return 0; | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 |  | 
|---|
| 303 |  | 
|---|
| 304 |  | 
|---|