source: Sophya/trunk/AddOn/TAcq/svv2mtx2_1210.cc@ 3940

Last change on this file since 3940 was 3940, checked in by cmv, 15 years ago

acceleration prog + petits ajouts, cmv 24/01/2011

File size: 10.6 KB
RevLine 
[3925]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>
[3940]13#include <fstream>
[3925]14#include <iostream>
15#include <string>
16
17#include "pexceptions.h"
18#include "tvector.h"
19#include "fioarr.h"
[3940]20#include "timing.h"
[3925]21
[3937]22/*
23export TACQEXE=~/Reza/TAcq/Objs
24./CasA03Dec.csh > CasA03Dec.log 2>&1
25*/
26
[3925]27//--------------------------- Fonctions de ce fichier -------------------
28void usage(void);
29void usage(void)
30{
31cout<<"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
[3940]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
[3925]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
[3927]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;
[3925]45}
46
47//----------------------------------------------------
48int main(int narg, char* arg[])
49{
50 // --- Decodage des arguments et traitement
[3940]51 int nmiss_stop = 1000; // on s'arrete si on a "nmiss_stop" fichiers consecutifs manquants
[3925]52 string outname = "";
53 int numthread = -1, numrow = -1;
[3928]54 bool dupli = false, doconj = false;
[3925]55 double freq0 = 0.;
[3940]56 int vacq1=-1, vacq2=-1;
[3926]57 int ifilmin=0, ifilmax=99999;
[3925]58 int jfr1=0, jfr2=-1, ngrpfreq=1;
59
60 char c;
[3940]61 while((c = getopt(narg,arg,"hDCo:t:r:f:T:F:v:")) != -1) {
[3925]62 switch (c) {
[3940]63 case 'v' :
64 sscanf(optarg,"%d,%d",&vacq1,&vacq2);
65 break;
[3925]66 case 'D' :
67 dupli = true;
68 break;
[3928]69 case 'C' :
70 doconj = true;
71 break;
[3925]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
[3940]100 cout<<"v1="<<vacq1<<" v2="<<vacq2<<" doconj="<<(int)doconj<<endl;
[3925]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
[3940]110 InitTim();
111
[3925]112 // --- recherche et comptage des fichiers de visibilites
[3926]113 // ATTENTION: il peut manquer des fichiers au debut ou dans la sequence
[3940]114 string flistname = outname; flistname += ".filelist";
[3925]115 int nfile = 0;
116 {
[3940]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;
[3926]123 int ifmin2 = ifilmin, ifmax2 = ifilmax; ifilmax = -1;
[3925]124 bool foundfirst = false;
[3940]125 int nmiss = 0;
[3926]126 for(int ifile=ifmin2; ifile<=ifmax2; ifile++) {
[3925]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;
[3940]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 }
[3925]142 nfile++;
143 ifilmax = ifile;
[3940]144 nmiss = 0;
145 flistw << string(str) <<endl;
[3925]146 }
[3940]147
148 flistw.close();
[3925]149 cout<<"Found "<<nfile<<" files from "<<ifilmin<<" to "<<ifilmax<<endl;
[3940]150 if(nfile==0 || ifilmax<ifilmin) return -5;
[3925]151 }
152
[3940]153 PrtTim("--- End of file number search");
[3925]154
155 //--------------------------------------------------------------------
156 int rc = 0;
157 try {
158
[3940]159 ifstream flistr(flistname.c_str(), ofstream::in);
160 if(!flistr) {cout<<"!!!!OPEN failed "<<flistname<<endl; return -6;}
161
[3925]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;
[3940]170 int lpmod = nfile/25; if(lpmod<=0) lpmod=1;
[3925]171
[3927]172 int_4 ntimefill = 0, ntimebad = 0;
[3940]173 bool foundfirst = true;
174 while(!flistr.eof()) {
[3925]175 // --- Lecture d'une visi elementaire (fichier acq)
[3940]176 string fname;
177 getline(flistr,fname);
178 if(fname.size()==0) continue;
179 if(ntimefill%lpmod==0) cout<<ntimefill<<" "<<" : "<<fname<<endl;
[3925]180 TMatrix< complex<r_4> > vismtx;
[3926]181 try {
[3940]182 PInPersist pin(fname);
[3926]183 pin >> vismtx;
184 } catch(...) {
[3940]185 cout<<"ERROR: bad file "<<fname<<endl;
[3926]186 ntimebad++;
187 continue;
188 }
[3925]189 tufin = (string)vismtx.Info()["DATEOBS"];
190
191 // --- Time keeping and number of summed elementary visibilities
[3926]192 MeanTT(ntimefill) = (double)vismtx.Info()["MeanTT"]/125.e6;
[3925]193 uint_4 npaqsum = vismtx.Info()["NPAQSUM"];
194
195 // --- Initialisation purposes for the first read file
[3940]196 if(foundfirst) {
197 foundfirst = false;
[3925]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;
[3926]241 MVisi(ntimefill,i) += vismtx(numrow,f);
[3925]242 nf++;
243 }
[3926]244 Npaqsum(ntimefill) = nf * npaqsum;
245 MVisi(ntimefill,i) /= double(Npaqsum(ntimefill));
[3928]246 if(doconj) MVisi(ntimefill,i) = conj(MVisi(ntimefill,i));
[3925]247 }
248 ntimefill++;
249
250 } // fin boucle sur le fichiers d'acq
[3926]251 cout<<"ntimefill = "<<ntimefill<<" / "<<nfile<<" , ntimebad="<<ntimebad<<" bad files"<<endl;
[3940]252 flistr.close();
253 string order = "rm -f "; order += flistname;
254 system(order.c_str());
[3925]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 }
[3940]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;
[3925]288
289 // --- writing visibility matrix to file
[3940]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 }
[3925]301 pos.PutObject(Freq,"ifreq");
[3940]302
[3925]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;
[3940]320 PrtTim("--- End of job");
[3925]321 return rc;
322}
323
324
325/*
326openppf visi1_01_05.ppf
327
328print visi 5
329
330n/plot ifreq.val%n ! ! "cpts"
331
332n/plot npaqsum.val%n ! ! "cpts"
333
334n/plot meantt.val%n ! ! "cpts"
335cp meantt dmeantt
336c++exec dmeantt=0.; for(int i=1;i<dmeantt.Size();i++) dmeantt(i)=meantt(i)-meantt(i-1);
337n/plot dmeantt.val%n n>0 ! "cpts nsta"
338
339imag visi "cdmod"
340imag visi "cdreal"
341imag visi "cdimag"
342imag visi "cdphas"
343
344 */
Note: See TracBrowser for help on using the repository browser.