| 1 | // Utilisation de SOPHYA pour faciliter les tests ...
 | 
|---|
| 2 | #include "sopnamsp.h"
 | 
|---|
| 3 | #include "machdefs.h"
 | 
|---|
| 4 | 
 | 
|---|
| 5 | /* ---------------------------------------------------------- 
 | 
|---|
| 6 |    Projet BAORadio - (C) LAL/IRFU  2008-2010
 | 
|---|
| 7 | 
 | 
|---|
| 8 |    Programme de lecture des fichiers vecteurs/matrices de 
 | 
|---|
| 9 |    visibilites produits par mcrd / vismfib 
 | 
|---|
| 10 |    R. Ansari, C. Magneville   -  LAL/Irfu
 | 
|---|
| 11 |    V : Mai 2009
 | 
|---|
| 12 |    ---------------------------------------------------------- */
 | 
|---|
| 13 | 
 | 
|---|
| 14 | // include standard c/c++
 | 
|---|
| 15 | #include <math.h>
 | 
|---|
| 16 | #include <stdio.h>
 | 
|---|
| 17 | #include <stdlib.h>
 | 
|---|
| 18 | #include <string.h>
 | 
|---|
| 19 | #include <sys/stat.h>
 | 
|---|
| 20 | #include <iostream>
 | 
|---|
| 21 | #include <string>
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "pexceptions.h"
 | 
|---|
| 24 | #include "tvector.h"
 | 
|---|
| 25 | #include "fioarr.h"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | //--------------------------- Fonctions de ce fichier   ------------------- 
 | 
|---|
| 28 | int Usage(void);
 | 
|---|
| 29 | int Usage(void)
 | 
|---|
| 30 | {
 | 
|---|
| 31 | cout<<" --- svv2mtx2.cc : Read PPF files produced by mcrd/visfmib"<<endl
 | 
|---|
| 32 |     <<"     and make time(row) - mean_frequency(col) matrices"<<endl
 | 
|---|
| 33 |     <<"Usage: svv2mtx2 InOutPath Imin,Imax NumFreq1,NumFreq2,NBinFreq"<<endl 
 | 
|---|
| 34 |     << " svv2mtx -h for detailed instructions"<<endl;
 | 
|---|
| 35 | return 1;
 | 
|---|
| 36 | }
 | 
|---|
| 37 | 
 | 
|---|
| 38 | //----------------------------------------------------
 | 
|---|
| 39 | int main(int narg, char* arg[])
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   // --- Decodage des arguments et traitement 
 | 
|---|
| 42 |   if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage();
 | 
|---|
| 43 |   if (narg<=1) return Usage();
 | 
|---|
| 44 |   string inoutpath = arg[1];
 | 
|---|
| 45 |   int ifilmin=0, ifilmax=-1;
 | 
|---|
| 46 |   if(narg>2) sscanf(arg[2],"%d,%d",&ifilmin,&ifilmax);
 | 
|---|
| 47 |   if(ifilmin<0) ifilmin=0;
 | 
|---|
| 48 |   int jfr1=0, jfr2=0, nbinfreq=0;
 | 
|---|
| 49 |   if(narg>3) sscanf(arg[3],"%d,%d,%d",&jfr1,&jfr2,&nbinfreq);
 | 
|---|
| 50 |   char str[1024];
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   // --- recherche des fichiers de visibilites
 | 
|---|
| 53 |   int nfile = 0;
 | 
|---|
| 54 |   {
 | 
|---|
| 55 |   struct stat buffer;
 | 
|---|
| 56 |   int i1=ifilmin, i2=(ifilmax<ifilmin) ? 999999999: ifilmax;
 | 
|---|
| 57 |   for(int ifile=i1; ifile<=i2; ifile++) {
 | 
|---|
| 58 |     sprintf(str, "%s/vismtx%d.ppf",inoutpath.c_str(),ifile);
 | 
|---|
| 59 |     int status = stat(str,&buffer);
 | 
|---|
| 60 |     if(status) break;
 | 
|---|
| 61 |     ifilmax = ifile;
 | 
|---|
| 62 |   }
 | 
|---|
| 63 |   nfile = ifilmax-ifilmin+1;
 | 
|---|
| 64 |   cout<<"Found "<<nfile<<" files from "<<ifilmin<<" to "<<ifilmax<<endl;
 | 
|---|
| 65 |   if(nfile==0 || ifilmax<ifilmin) return -3;
 | 
|---|
| 66 |   }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   //--------------------------------------------------------------------
 | 
|---|
| 70 |   int rc = 0;
 | 
|---|
| 71 |   try {
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   // --- read visibilities coding vector
 | 
|---|
| 74 |   cout<<"reading chanum vector"<<endl;
 | 
|---|
| 75 |   TVector<uint_4> chanum;
 | 
|---|
| 76 |   {
 | 
|---|
| 77 |   sprintf(str,"%s/chanum.ppf",inoutpath.c_str());
 | 
|---|
| 78 |   PInPersist pic(str);
 | 
|---|
| 79 |   pic >> chanum;
 | 
|---|
| 80 |   cout<<"number of visib is "<<chanum.Size()<<endl;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 |   vector< TMatrix< complex<r_4> >* > vMVis;
 | 
|---|
| 83 |   for(int i=0;i<chanum.Size();i++) vMVis.push_back(new TMatrix< complex<r_4> >);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   // --- read visibility files
 | 
|---|
| 86 |   int nfreq0, nvisi, nmoyfreq = 0;
 | 
|---|
| 87 |   TVector<r_8> Ttag(nfile), Freq;
 | 
|---|
| 88 |   TVector<int_4> Npaqsum(nfile), NFreq;
 | 
|---|
| 89 |   for(int ifile=ifilmin; ifile<=ifilmax; ifile++) {
 | 
|---|
| 90 |     sprintf(str, "%s/vismtx%d.ppf",inoutpath.c_str(),ifile);
 | 
|---|
| 91 |     cout<<ifile<<" opening: "<<str<<endl;
 | 
|---|
| 92 |     PInPersist pin(str);
 | 
|---|
| 93 |     TMatrix< complex<r_4> > vismtx;
 | 
|---|
| 94 |     pin >> vismtx;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |     Ttag(ifile-ifilmin) = (double)vismtx.Info()["MeanTT"]/125.e6;
 | 
|---|
| 97 |     if(ifile==ifilmin) cout<<"Reference TimeTag is "<<vismtx.Info()["MeanTT"]<<" = "<< Ttag(0)<<" sec"<<endl;
 | 
|---|
| 98 |     uint_4 nmean = vismtx.Info()["NPAQSUM"];
 | 
|---|
| 99 |     Npaqsum(ifile-ifilmin) = (int_4)nmean;
 | 
|---|
| 100 |     nfreq0 = vismtx.NCols();
 | 
|---|
| 101 |     nvisi = vismtx.NRows();
 | 
|---|
| 102 | 
 | 
|---|
| 103 |     // --- For initialisation purposes
 | 
|---|
| 104 |     if (ifile==ifilmin) {
 | 
|---|
| 105 |       cout<<"vismtx: number of frequencies = "<<nfreq0
 | 
|---|
| 106 |           <<" , visib = "<<nvisi<<" , nmean = "<<nmean<<endl;
 | 
|---|
| 107 |       if(chanum.Size()!=nvisi)
 | 
|---|
| 108 |         throw ParmError("ERROR: nvisi != chanum.Size() !!!");
 | 
|---|
| 109 |       // how many average frequencies ?
 | 
|---|
| 110 |       if(jfr1<=0) jfr1=1;
 | 
|---|
| 111 |       if(jfr1>=nfreq0) jfr1=nfreq0-1;
 | 
|---|
| 112 |       if(jfr2<jfr1 || jfr2>=nfreq0) jfr2=nfreq0-1;
 | 
|---|
| 113 |       if(nbinfreq<=0) nbinfreq=1;
 | 
|---|
| 114 |       if(nbinfreq>jfr2-jfr1+1) nbinfreq=jfr2-jfr1+1;
 | 
|---|
| 115 |       nmoyfreq = (jfr2-jfr1+1)/nbinfreq;
 | 
|---|
| 116 |       if(nmoyfreq*nbinfreq<jfr2-jfr1+1) nmoyfreq++;
 | 
|---|
| 117 |       cout<<"frequency averaging: jfr1="<<jfr1<<" jfr2="<<jfr2
 | 
|---|
| 118 |           <<" nbinfreq="<<nbinfreq<<" (nmoyfreq="<<nmoyfreq<<")"<<endl;
 | 
|---|
| 119 |       // average frequencies value
 | 
|---|
| 120 |       Freq.ReSize(nbinfreq); Freq = 0.;
 | 
|---|
| 121 |       NFreq.ReSize(nbinfreq); NFreq = 0;
 | 
|---|
| 122 |       cout<<"Frequency average:"<<endl;
 | 
|---|
| 123 |       for(int i=0;i<nbinfreq;i++) {
 | 
|---|
| 124 |         Freq(i) = 0.;
 | 
|---|
| 125 |         int nj = 0;
 | 
|---|
| 126 |         for(int j=0;j<nmoyfreq;j++) {
 | 
|---|
| 127 |           int ip = jfr1 + i*nmoyfreq + j;
 | 
|---|
| 128 |           if(ip>=nfreq0) break;
 | 
|---|
| 129 |           Freq(i) += ip;
 | 
|---|
| 130 |           nj++;
 | 
|---|
| 131 |         }
 | 
|---|
| 132 |         if(nj>0) Freq(i) /= (double)nj;
 | 
|---|
| 133 |         NFreq(i) = nj;
 | 
|---|
| 134 |         cout<<"  F("<<i<<") = "<<Freq(i)<<" (nj="<<NFreq(i)
 | 
|---|
| 135 |             <<" ["<<jfr1+i*nmoyfreq<<","<<jfr1+i*nmoyfreq+nj-1<<"])"<<endl;
 | 
|---|
| 136 |       }
 | 
|---|
| 137 |       // allocate visib matrice <f> vs t
 | 
|---|
| 138 |       cout<<"allocating "<<nvisi<<" visibility matrices ("<<nfile<<","<<nbinfreq<<") "
 | 
|---|
| 139 |           <<nvisi*nfile*nbinfreq*sizeof(complex<r_4>)/1.e6<<" Mo"<<endl;
 | 
|---|
| 140 |       for(int ivi=0;ivi<nvisi;ivi++) {
 | 
|---|
| 141 |         vMVis[ivi]->ReSize(nfile,nbinfreq);
 | 
|---|
| 142 |         (*vMVis[ivi]) = complex<r_4>(0.,0.);
 | 
|---|
| 143 |       }
 | 
|---|
| 144 |     }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |     // --- Fill visibility matrix
 | 
|---|
| 147 |     for(int ivi=0;ivi<nvisi;ivi++) {
 | 
|---|
| 148 |       for(int ifr=0;ifr<nbinfreq;ifr++) {
 | 
|---|
| 149 |         int nj = 0;
 | 
|---|
| 150 |         complex<r_8> v(0.,0.);
 | 
|---|
| 151 |         for(int j=0;j<nmoyfreq;j++) {
 | 
|---|
| 152 |           int ifrp = jfr1 + ifr*nmoyfreq + j;
 | 
|---|
| 153 |           if(ifrp>=nfreq0) break;
 | 
|---|
| 154 |           v += vismtx(ivi,ifrp);
 | 
|---|
| 155 |           nj++;
 | 
|---|
| 156 |         }
 | 
|---|
| 157 |         if(nj>0) v /= (double)nj;
 | 
|---|
| 158 |         (*vMVis[ivi])(ifile-ifilmin,ifr) = v / (double)nmean;
 | 
|---|
| 159 |       }
 | 
|---|
| 160 |     }
 | 
|---|
| 161 |   }
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   sprintf(str, "%s/svvdt2.ppf",inoutpath.c_str());
 | 
|---|
| 164 |   cout<<"writing visibility matrix to file "<<str<<endl;
 | 
|---|
| 165 |   POutPersist pos(str);
 | 
|---|
| 166 |   pos.PutObject(chanum,"chanum");
 | 
|---|
| 167 |   pos.PutObject(Ttag,"ttsec");
 | 
|---|
| 168 |   pos.PutObject(Npaqsum,"npaqsum");
 | 
|---|
| 169 |   pos.PutObject(Freq,"ifreq");
 | 
|---|
| 170 |   pos.PutObject(NFreq,"nifreq");
 | 
|---|
| 171 |   for(unsigned int i=0;i<vMVis.size();i++) {
 | 
|---|
| 172 |     sprintf(str,"visft%d",chanum(i));
 | 
|---|
| 173 |     pos.PutObject(*vMVis[i],str);
 | 
|---|
| 174 |     delete vMVis[i];
 | 
|---|
| 175 |   }
 | 
|---|
| 176 |   cout<<" -------- End of Job --------"<<endl;
 | 
|---|
| 177 |   }
 | 
|---|
| 178 |   //--------------------------------------------------------------------
 | 
|---|
| 179 |   catch(PException& exc) {
 | 
|---|
| 180 |     cerr<<" svv2mtx2.cc catched PException "<<exc.Msg()<<endl;
 | 
|---|
| 181 |     rc = 77;
 | 
|---|
| 182 |   }  
 | 
|---|
| 183 |   catch (std::exception& sex) {
 | 
|---|
| 184 |     cerr<<"\n svv2mtx2.cc std::exception :" 
 | 
|---|
| 185 |         <<(string)typeid(sex).name() << "\n msg= "<<sex.what()<<endl;
 | 
|---|
| 186 |     rc = 78;
 | 
|---|
| 187 |   }
 | 
|---|
| 188 |   catch(...) {
 | 
|---|
| 189 |     cerr<<" svv2mtx2.cc catched unknown (...) exception  "<<endl; 
 | 
|---|
| 190 |     rc = 79; 
 | 
|---|
| 191 |   } 
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   cout<<">>>> svv2mtx2.cc ------- END ----------- RC="<<rc<<endl;
 | 
|---|
| 194 |   return rc;
 | 
|---|
| 195 | }
 | 
|---|