| 1 | // lecture des fichiers acq de Pittsburgh Dec 2010 | 
|---|
| 2 | // et ecriture des matrices de visi temps-frequence | 
|---|
| 3 | // > make OBJ=$CMVPROG/obj/ EXE=$CMVPROG/exe/ chanum_1210 svv2mtx2_1210 | 
|---|
| 4 |  | 
|---|
| 5 | #include "sopnamsp.h" | 
|---|
| 6 | #include "machdefs.h" | 
|---|
| 7 | #include <math.h> | 
|---|
| 8 | #include <stdio.h> | 
|---|
| 9 | #include <stdlib.h> | 
|---|
| 10 | #include <string.h> | 
|---|
| 11 | #include <unistd.h> | 
|---|
| 12 | #include <sys/stat.h> | 
|---|
| 13 | #include <fstream> | 
|---|
| 14 | #include <iostream> | 
|---|
| 15 | #include <string> | 
|---|
| 16 |  | 
|---|
| 17 | #include "pexceptions.h" | 
|---|
| 18 | #include "tvector.h" | 
|---|
| 19 | #include "fioarr.h" | 
|---|
| 20 | #include "timing.h" | 
|---|
| 21 |  | 
|---|
| 22 | /* | 
|---|
| 23 | export TACQEXE=~/Reza/TAcq/Objs | 
|---|
| 24 | ./CasA03Dec.csh > CasA03Dec.log 2>&1 | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 | //--------------------------- Fonctions de ce fichier   ------------------- | 
|---|
| 28 | void usage(void); | 
|---|
| 29 | void usage(void) | 
|---|
| 30 | { | 
|---|
| 31 | cout<<"svv2mtx2_1012 [options] dir : lecture des fichiers acq de Pittsburgh Dec 2010"<<endl | 
|---|
| 32 | <<" dir : repertoire ou se trouvent les fichiers d'acq"<<endl | 
|---|
| 33 | <<" -D : visi is a duplicated one"<<endl | 
|---|
| 34 | <<" -o visi.ppf : nom du ficher ppf pour ecrire la visi temps-frequence"<<endl | 
|---|
| 35 | <<" -v v1,v2 : numero de voie acq pour cette visi [1-32]"<<endl | 
|---|
| 36 | <<"            on calcule <v1.conj(v2)>"<<endl | 
|---|
| 37 | <<" -C : compute and store the complexe conjugated visi"<<endl | 
|---|
| 38 | <<"      dans ce cas on calcule <v2.conj(v1)>, le but est d'avoir toujours <E.conj(W)>"<<endl | 
|---|
| 39 | <<" -t thr : numero de la thread ayant traite cette visi [0-N]"<<endl | 
|---|
| 40 | <<" -r row : numero de la ligne de la matrice acq pour cette visi [0-Nrow["<<endl | 
|---|
| 41 | <<" -f freq0 : 1ere frequence en MHz"<<endl | 
|---|
| 42 | <<" -T it1,it2 : numero (temps) des fichiers a traiter [it1,it2]"<<endl | 
|---|
| 43 | <<" -F if1,if2,ngrpfreq : numero des frequences [if1,if2] de [0,N[ a traiter et regroupement par ngrpfreq"<<endl | 
|---|
| 44 | <<"ATTENTION: lancer ce prog depuis le repertoire ou doivent etre ecrits les fichiers de visi"<<endl; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | //---------------------------------------------------- | 
|---|
| 48 | int main(int narg, char* arg[]) | 
|---|
| 49 | { | 
|---|
| 50 | // --- Decodage des arguments et traitement | 
|---|
| 51 | int nmiss_stop = 1000;  // on s'arrete si on a "nmiss_stop" fichiers consecutifs manquants | 
|---|
| 52 | string outname = ""; | 
|---|
| 53 | int numthread = -1, numrow = -1; | 
|---|
| 54 | bool dupli = false, doconj = false; | 
|---|
| 55 | double freq0 = 0.; | 
|---|
| 56 | int vacq1=-1, vacq2=-1; | 
|---|
| 57 | int ifilmin=0, ifilmax=99999; | 
|---|
| 58 | int jfr1=0, jfr2=-1, ngrpfreq=1; | 
|---|
| 59 |  | 
|---|
| 60 | char c; | 
|---|
| 61 | while((c = getopt(narg,arg,"hDCo:t:r:f:T:F:v:")) != -1) { | 
|---|
| 62 | switch (c) { | 
|---|
| 63 | case 'v' : | 
|---|
| 64 | sscanf(optarg,"%d,%d",&vacq1,&vacq2); | 
|---|
| 65 | break; | 
|---|
| 66 | case 'D' : | 
|---|
| 67 | dupli = true; | 
|---|
| 68 | break; | 
|---|
| 69 | case 'C' : | 
|---|
| 70 | doconj = true; | 
|---|
| 71 | break; | 
|---|
| 72 | case 'o' : | 
|---|
| 73 | outname = optarg; | 
|---|
| 74 | break; | 
|---|
| 75 | case 't' : | 
|---|
| 76 | numthread = atoi(optarg); | 
|---|
| 77 | break; | 
|---|
| 78 | case 'r' : | 
|---|
| 79 | numrow = atoi(optarg); | 
|---|
| 80 | break; | 
|---|
| 81 | case 'f' : | 
|---|
| 82 | freq0 = atof(optarg); | 
|---|
| 83 | break; | 
|---|
| 84 | case 'T' : | 
|---|
| 85 | sscanf(optarg,"%d,%d",&ifilmin,&ifilmax); | 
|---|
| 86 | if(ifilmin<0) ifilmin=0; | 
|---|
| 87 | break; | 
|---|
| 88 | case 'F' : | 
|---|
| 89 | sscanf(optarg,"%d,%d,%d",&jfr1,&jfr2,&ngrpfreq); | 
|---|
| 90 | if(ngrpfreq<=0) ngrpfreq=1; | 
|---|
| 91 | break; | 
|---|
| 92 | case 'h' : | 
|---|
| 93 | default : | 
|---|
| 94 | usage(); return -1; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 | if(optind>=narg) {usage(); return -1;} | 
|---|
| 98 | string indir = arg[optind]; | 
|---|
| 99 |  | 
|---|
| 100 | cout<<"v1="<<vacq1<<" v2="<<vacq2<<" doconj="<<(int)doconj<<endl; | 
|---|
| 101 | cout<<"thread="<<numthread<<endl; if(numthread<0) return -2; | 
|---|
| 102 | cout<<"numrow="<<numrow<<endl;  if(numrow<0) return -2; | 
|---|
| 103 | cout<<"dupli="<<(int)dupli<<endl; | 
|---|
| 104 | cout<<"outname="<<outname<<endl; if(outname.size()<=0) return -2; | 
|---|
| 105 | cout<<"indir="<<indir<<endl; | 
|---|
| 106 | cout<<"freq0="<<freq0<<" MHz"<<endl; | 
|---|
| 107 | cout<<"request: file "<<ifilmin<<" to "<<ifilmax<<endl; if(ifilmax<ifilmin) return -3; | 
|---|
| 108 | cout<<"request: freq "<<jfr1<<" to "<<jfr2<<" grouped by "<<ngrpfreq<<endl; | 
|---|
| 109 |  | 
|---|
| 110 | InitTim(); | 
|---|
| 111 |  | 
|---|
| 112 | // --- recherche et comptage des fichiers de visibilites | 
|---|
| 113 | // ATTENTION: il peut manquer des fichiers au debut ou dans la sequence | 
|---|
| 114 | string flistname = outname; flistname += ".filelist"; | 
|---|
| 115 | int nfile = 0; | 
|---|
| 116 | { | 
|---|
| 117 | ofstream flistw(flistname.c_str(), ofstream::out); | 
|---|
| 118 | cout<<"writing in file list in: "<<flistname<<" (is_open="<<flistw.is_open()<<")"<<endl; | 
|---|
| 119 | if(!flistw) {cout<<"!!!!OPEN failed "<<flistname<<endl; return -4;} | 
|---|
| 120 |  | 
|---|
| 121 | char str[4096]; | 
|---|
| 122 | struct stat buffer; | 
|---|
| 123 | int ifmin2 = ifilmin, ifmax2 = ifilmax; ifilmax = -1; | 
|---|
| 124 | bool foundfirst = false; | 
|---|
| 125 | int nmiss = 0; | 
|---|
| 126 | for(int ifile=ifmin2; ifile<=ifmax2; ifile++) { | 
|---|
| 127 | sprintf(str, "%s/vismtx_%d_%d.ppf",indir.c_str(),numthread,ifile); | 
|---|
| 128 | int status = stat(str,&buffer); | 
|---|
| 129 | if(!foundfirst && status==0) { | 
|---|
| 130 | cout<<"first file found: "<<str<<endl; | 
|---|
| 131 | foundfirst = true; | 
|---|
| 132 | if(ifile>ifilmin) ifilmin = ifile; | 
|---|
| 133 | } | 
|---|
| 134 | if(!foundfirst) continue; | 
|---|
| 135 | if(ifile<ifilmin) continue; | 
|---|
| 136 | if(foundfirst && status!=0) { | 
|---|
| 137 | // On fait ca car "stst()" est extremement long si le fichier n'existe pas! | 
|---|
| 138 | if(nmiss>nmiss_stop) break; | 
|---|
| 139 | nmiss++; | 
|---|
| 140 | continue; | 
|---|
| 141 | } | 
|---|
| 142 | nfile++; | 
|---|
| 143 | ifilmax = ifile; | 
|---|
| 144 | nmiss = 0; | 
|---|
| 145 | flistw << string(str) <<endl; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | flistw.close(); | 
|---|
| 149 | cout<<"Found "<<nfile<<" files from "<<ifilmin<<" to "<<ifilmax<<endl; | 
|---|
| 150 | if(nfile==0 || ifilmax<ifilmin) return -5; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | PrtTim("--- End of file number search"); | 
|---|
| 154 |  | 
|---|
| 155 | //-------------------------------------------------------------------- | 
|---|
| 156 | int rc = 0; | 
|---|
| 157 | try { | 
|---|
| 158 |  | 
|---|
| 159 | ifstream flistr(flistname.c_str(), ofstream::in); | 
|---|
| 160 | if(!flistr) {cout<<"!!!!OPEN failed "<<flistname<<endl; return -6;} | 
|---|
| 161 |  | 
|---|
| 162 | // --- read visibility files | 
|---|
| 163 | TMatrix< complex<r_4> > MVisi; | 
|---|
| 164 | TVector<r_8> MeanTT(nfile); | 
|---|
| 165 | TVector<r_4> Freq; | 
|---|
| 166 | TVector<int_4> Npaqsum(nfile); | 
|---|
| 167 | string tudeb, tufin; | 
|---|
| 168 | double tudeb_day, tufin_day; | 
|---|
| 169 | int nfreq = 0; | 
|---|
| 170 | int lpmod = nfile/25; if(lpmod<=0) lpmod=1; | 
|---|
| 171 |  | 
|---|
| 172 | int_4 ntimefill = 0, ntimebad = 0; | 
|---|
| 173 | bool foundfirst = true; | 
|---|
| 174 | while(!flistr.eof()) { | 
|---|
| 175 | // --- Lecture d'une visi elementaire (fichier acq) | 
|---|
| 176 | string fname; | 
|---|
| 177 | getline(flistr,fname); | 
|---|
| 178 | if(fname.size()==0) continue; | 
|---|
| 179 | if(ntimefill%lpmod==0) cout<<ntimefill<<" "<<" : "<<fname<<endl; | 
|---|
| 180 | TMatrix< complex<r_4> > vismtx; | 
|---|
| 181 | try { | 
|---|
| 182 | PInPersist pin(fname); | 
|---|
| 183 | pin >> vismtx; | 
|---|
| 184 | } catch(...) { | 
|---|
| 185 | cout<<"ERROR: bad file "<<fname<<endl; | 
|---|
| 186 | ntimebad++; | 
|---|
| 187 | continue; | 
|---|
| 188 | } | 
|---|
| 189 | tufin = (string)vismtx.Info()["DATEOBS"]; | 
|---|
| 190 |  | 
|---|
| 191 | // --- Time keeping and number of summed elementary visibilities | 
|---|
| 192 | MeanTT(ntimefill) = (double)vismtx.Info()["MeanTT"]/125.e6; | 
|---|
| 193 | uint_4 npaqsum = vismtx.Info()["NPAQSUM"]; | 
|---|
| 194 |  | 
|---|
| 195 | // --- Initialisation purposes for the first read file | 
|---|
| 196 | if(foundfirst) { | 
|---|
| 197 | foundfirst = false; | 
|---|
| 198 | tudeb = tufin; | 
|---|
| 199 | printf("Reference Time: tt = %.5f sec, TU = %s\n",MeanTT(0),tudeb.c_str()); | 
|---|
| 200 | if(numrow>=vismtx.NRows()) { | 
|---|
| 201 | cout<<"ERROR: requested numrow="<<numrow<<" => "<<vismtx.NRows()<<endl; | 
|---|
| 202 | return -5; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | // frequency grouping | 
|---|
| 206 | int nfreq0 = vismtx.NCols(); | 
|---|
| 207 | cout<<"vismtx: number of frequencies = "<<nfreq0<<endl; | 
|---|
| 208 | if(jfr1<=0) jfr1=0; if(jfr1>=nfreq0) jfr1=nfreq0-1; | 
|---|
| 209 | if(jfr2<jfr1 || jfr2>=nfreq0) jfr2=nfreq0-1; | 
|---|
| 210 | cout<<"frequency from jfr1="<<jfr1<<" to jfr2="<<jfr2<<" grouped by "<<ngrpfreq<<endl; | 
|---|
| 211 | nfreq = 0; for(int i=jfr1;i<=jfr2;i+=ngrpfreq) nfreq++; | 
|---|
| 212 | cout<<"frequency from [jfr1="<<jfr1<<" , jfr2="<<jfr2<<"] grouped by "<<ngrpfreq | 
|---|
| 213 | <<": total of "<<nfreq<<" freq"<<endl; | 
|---|
| 214 | Freq.ReSize(nfreq); Freq = 0.; | 
|---|
| 215 | for(int i=0;i<nfreq;i++) { | 
|---|
| 216 | int nf=0; | 
|---|
| 217 | for(int j=0; j<ngrpfreq; j++) { | 
|---|
| 218 | int f = jfr1 + i*ngrpfreq + j; | 
|---|
| 219 | if(f>jfr2) break; | 
|---|
| 220 | Freq(i) += f; | 
|---|
| 221 | nf++; | 
|---|
| 222 | } | 
|---|
| 223 | if(nf>0) Freq(i) /= (double)nf; | 
|---|
| 224 | else {cout<<"ERROR: last freq bin with 0 freq in it"<<endl; return -6;} | 
|---|
| 225 | if(i<4 || i>nfreq-4) cout<<"  F("<<i<<") = "<<Freq(i)<<" nf="<<nf<<endl; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | // allocate visib matrice <f> vs t | 
|---|
| 229 | cout<<"allocating visibility matrice ("<<nfile<<","<<nfreq<<") " | 
|---|
| 230 | <<nfile*nfreq*sizeof(complex<r_4>)/1.e6<<" Mo"<<endl; | 
|---|
| 231 | MVisi.ReSize(nfile,nfreq); | 
|---|
| 232 | MVisi = complex<r_4>(0.,0.); | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | // Fill time-freq visibility matrix | 
|---|
| 236 | for(int i=0;i<nfreq;i++) { | 
|---|
| 237 | int nf=0; | 
|---|
| 238 | for(int j=0; j<ngrpfreq; j++) { | 
|---|
| 239 | int f = jfr1 + i*ngrpfreq + j; | 
|---|
| 240 | if(f>jfr2) break; | 
|---|
| 241 | MVisi(ntimefill,i) += vismtx(numrow,f); | 
|---|
| 242 | nf++; | 
|---|
| 243 | } | 
|---|
| 244 | Npaqsum(ntimefill) = nf * npaqsum; | 
|---|
| 245 | MVisi(ntimefill,i) /= double(Npaqsum(ntimefill)); | 
|---|
| 246 | if(doconj) MVisi(ntimefill,i) = conj(MVisi(ntimefill,i)); | 
|---|
| 247 | } | 
|---|
| 248 | ntimefill++; | 
|---|
| 249 |  | 
|---|
| 250 | }  // fin boucle sur le fichiers d'acq | 
|---|
| 251 | cout<<"ntimefill = "<<ntimefill<<" / "<<nfile<<" , ntimebad="<<ntimebad<<" bad files"<<endl; | 
|---|
| 252 | flistr.close(); | 
|---|
| 253 | string order = "rm -f "; order += flistname; | 
|---|
| 254 | system(order.c_str()); | 
|---|
| 255 |  | 
|---|
| 256 | // --- keeping info with visi | 
|---|
| 257 | { | 
|---|
| 258 | // bricolo en unite de jours depuis le 0/12/2010 a 0h | 
|---|
| 259 | int y,m,d,h,mn; double s; | 
|---|
| 260 | sscanf(tudeb.c_str(),"%d-%d-%dT%d:%d:%lf",&y,&m,&d,&h,&mn,&s); | 
|---|
| 261 | tudeb_day = (double)d + ((double)h + mn/60. + s/3600.)/24.; | 
|---|
| 262 | sscanf(tufin.c_str(),"%d-%d-%dT%d:%d:%lf",&y,&m,&d,&h,&mn,&s); | 
|---|
| 263 | tufin_day = (double)d + ((double)h + mn/60. + s/3600.)/24.; | 
|---|
| 264 | } | 
|---|
| 265 | DVList dvl; | 
|---|
| 266 | dvl["vacq1"] = vacq1; | 
|---|
| 267 | dvl["vacq2"] = vacq2; | 
|---|
| 268 | dvl["nth"] = numthread; | 
|---|
| 269 | dvl["row"] = numrow; | 
|---|
| 270 | dvl["dir"] = indir; | 
|---|
| 271 | dvl["dupli"] = (dupli) ? 1: 0; | 
|---|
| 272 | dvl["isconj"] = (doconj) ? 1: 0; | 
|---|
| 273 | dvl["TUobs_0"] = tudeb; | 
|---|
| 274 | dvl["TUobs_N"] = tufin; | 
|---|
| 275 | dvl["TUday_0"] = tudeb_day; | 
|---|
| 276 | dvl["TUday_N"] = tufin_day; | 
|---|
| 277 | dvl["TTag_0"] = MeanTT(0); | 
|---|
| 278 | if(ntimefill>0) dvl["TTag_N"] = MeanTT(ntimefill-1); | 
|---|
| 279 | dvl["freq0"] = freq0; | 
|---|
| 280 | dvl["dfreq0"] = 500./8192.; | 
|---|
| 281 | dvl["jfr1"] = jfr1; | 
|---|
| 282 | dvl["jfr2"] = jfr2; | 
|---|
| 283 | dvl["ngrpfreq"] = ngrpfreq; | 
|---|
| 284 | dvl["ifilmin"] = ifilmin; | 
|---|
| 285 | dvl["ifilmax"] = ifilmax; | 
|---|
| 286 | dvl["ntimefill"] = ntimefill; | 
|---|
| 287 | dvl["ntimebad"] = ntimebad; | 
|---|
| 288 |  | 
|---|
| 289 | // --- writing visibility matrix to file | 
|---|
| 290 | cout<<"writing visibility matrix to file "<<outname<<endl; | 
|---|
| 291 | POutPersist pos(outname); | 
|---|
| 292 | if(ntimefill>0) { | 
|---|
| 293 | TVector<r_8> dum1(MeanTT(Range(0,ntimefill-1))); | 
|---|
| 294 | pos.PutObject(dum1,"meantt"); | 
|---|
| 295 | TVector<int_4> dum2(Npaqsum(Range(0,ntimefill-1))); | 
|---|
| 296 | pos.PutObject(dum2,"npaqsum"); | 
|---|
| 297 | TMatrix< complex<r_4> > dum3(MVisi(Range(0,ntimefill-1),Range(0,MVisi.NCols()-1))); | 
|---|
| 298 | dum3.Info() = dvl; | 
|---|
| 299 | pos.PutObject(dum3,"visi"); | 
|---|
| 300 | } | 
|---|
| 301 | pos.PutObject(Freq,"ifreq"); | 
|---|
| 302 |  | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | //-------------------------------------------------------------------- | 
|---|
| 306 | catch(PException& exc) { | 
|---|
| 307 | cerr<<" svv2mtx2_1210.cc catched PException "<<exc.Msg()<<endl; | 
|---|
| 308 | rc = 77; | 
|---|
| 309 | } | 
|---|
| 310 | catch (std::exception& sex) { | 
|---|
| 311 | cerr<<"\n svv2mtx2_1210.cc std::exception :" | 
|---|
| 312 | <<(string)typeid(sex).name() << "\n msg= "<<sex.what()<<endl; | 
|---|
| 313 | rc = 78; | 
|---|
| 314 | } | 
|---|
| 315 | catch(...) { | 
|---|
| 316 | cerr<<" svv2mtx2_1210.cc catched unknown (...) exception  "<<endl; | 
|---|
| 317 | rc = 79; | 
|---|
| 318 | } | 
|---|
| 319 | cout<<">>>> svv2mtx2_1210.cc ------- END ----------- RC="<<rc<<endl; | 
|---|
| 320 | PrtTim("--- End of job"); | 
|---|
| 321 | return rc; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 |  | 
|---|
| 325 | /* | 
|---|
| 326 | openppf visi1_01_05.ppf | 
|---|
| 327 |  | 
|---|
| 328 | print visi 5 | 
|---|
| 329 |  | 
|---|
| 330 | n/plot ifreq.val%n ! ! "cpts" | 
|---|
| 331 |  | 
|---|
| 332 | n/plot npaqsum.val%n ! ! "cpts" | 
|---|
| 333 |  | 
|---|
| 334 | n/plot meantt.val%n ! ! "cpts" | 
|---|
| 335 | cp meantt dmeantt | 
|---|
| 336 | c++exec dmeantt=0.; for(int i=1;i<dmeantt.Size();i++) dmeantt(i)=meantt(i)-meantt(i-1); | 
|---|
| 337 | n/plot dmeantt.val%n n>0 ! "cpts nsta" | 
|---|
| 338 |  | 
|---|
| 339 | imag visi "cdmod" | 
|---|
| 340 | imag visi "cdreal" | 
|---|
| 341 | imag visi "cdimag" | 
|---|
| 342 | imag visi "cdphas" | 
|---|
| 343 |  | 
|---|
| 344 | */ | 
|---|