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

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

gestion de la conjugaison complexe pour visi # de <E.conj(W)>, cmv 17/12/2010

File size: 9.3 KB
Line 
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 <iostream>
14#include <string>
15
16#include "pexceptions.h"
17#include "tvector.h"
18#include "fioarr.h"
19
20//--------------------------- Fonctions de ce fichier -------------------
21void usage(void);
22void usage(void)
23{
24cout<<"svv2mtx2_1012 [options] dir : lecture des fichiers acq de Pittsburgh Dec 2010"<<endl
25 <<" dir : repertoire ou se trouvent les fichiers d'acq"<<endl
26 <<" -C : compute and store the complexe conjugated visi"<<endl
27 <<" -D : visi is a duplicated one"<<endl
28 <<" -o visi.ppf : nom du ficher ppf pour ecrire la visi temps-frequence"<<endl
29 <<" -t thr : numero de la thread ayant traite cette visi [0-N]"<<endl
30 <<" -r row : numero de la ligne de la matrice acq pour cette visi [0-Nrow["<<endl
31 <<" -f freq0 : 1ere frequence en MHz"<<endl
32 <<" -T it1,it2 : numero (temps) des fichiers a traiter [it1,it2]"<<endl
33 <<" -F if1,if2,ngrpfreq : numero des frequences [if1,if2] de [0,N[ a traiter et regroupement par ngrpfreq"<<endl
34 <<"ATTENTION: lancer ce prog depuis le repertoire ou doivent etre ecrits les fichiers de visi"<<endl;
35}
36
37//----------------------------------------------------
38int main(int narg, char* arg[])
39{
40 // --- Decodage des arguments et traitement
41 string outname = "";
42 int numthread = -1, numrow = -1;
43 bool dupli = false, doconj = false;
44 double freq0 = 0.;
45 int ifilmin=0, ifilmax=99999;
46 int jfr1=0, jfr2=-1, ngrpfreq=1;
47 char str[2048];
48
49 char c;
50 while((c = getopt(narg,arg,"hDCo:t:r:f:T:F:")) != -1) {
51 switch (c) {
52 case 'D' :
53 dupli = true;
54 break;
55 case 'C' :
56 doconj = true;
57 break;
58 case 'o' :
59 outname = optarg;
60 break;
61 case 't' :
62 numthread = atoi(optarg);
63 break;
64 case 'r' :
65 numrow = atoi(optarg);
66 break;
67 case 'f' :
68 freq0 = atof(optarg);
69 break;
70 case 'T' :
71 sscanf(optarg,"%d,%d",&ifilmin,&ifilmax);
72 if(ifilmin<0) ifilmin=0;
73 break;
74 case 'F' :
75 sscanf(optarg,"%d,%d,%d",&jfr1,&jfr2,&ngrpfreq);
76 if(ngrpfreq<=0) ngrpfreq=1;
77 break;
78 case 'h' :
79 default :
80 usage(); return -1;
81 }
82 }
83 if(optind>=narg) {usage(); return -1;}
84 string indir = arg[optind];
85
86 cout<<"thread="<<numthread<<endl; if(numthread<0) return -2;
87 cout<<"numrow="<<numrow<<endl; if(numrow<0) return -2;
88 cout<<"dupli="<<(int)dupli<<endl;
89 cout<<"doconj="<<(int)doconj<<endl;
90 cout<<"outname="<<outname<<endl; if(outname.size()<=0) return -2;
91 cout<<"indir="<<indir<<endl;
92 cout<<"freq0="<<freq0<<" MHz"<<endl;
93 cout<<"request: file "<<ifilmin<<" to "<<ifilmax<<endl; if(ifilmax<ifilmin) return -3;
94 cout<<"request: freq "<<jfr1<<" to "<<jfr2<<" grouped by "<<ngrpfreq<<endl;
95
96 // --- recherche et comptage des fichiers de visibilites
97 // ATTENTION: il peut manquer des fichiers au debut ou dans la sequence
98 struct stat buffer;
99 int nfile = 0;
100 {
101 int ifmin2 = ifilmin, ifmax2 = ifilmax; ifilmax = -1;
102 bool foundfirst = false;
103 for(int ifile=ifmin2; ifile<=ifmax2; ifile++) {
104 sprintf(str, "%s/vismtx_%d_%d.ppf",indir.c_str(),numthread,ifile);
105 int status = stat(str,&buffer);
106 if(!foundfirst && status==0) {
107 cout<<"first file found: "<<str<<endl;
108 foundfirst = true;
109 if(ifile>ifilmin) ifilmin = ifile;
110 }
111 if(!foundfirst) continue;
112 if(ifile<ifilmin) continue;
113 if(foundfirst && status!=0) continue;
114 nfile++;
115 ifilmax = ifile;
116 }
117 cout<<"Found "<<nfile<<" files from "<<ifilmin<<" to "<<ifilmax<<endl;
118 if(nfile==0 || ifilmax<ifilmin) return -4;
119 }
120
121
122 //--------------------------------------------------------------------
123 int rc = 0;
124 try {
125
126 // --- read visibility files
127 TMatrix< complex<r_4> > MVisi;
128 TVector<r_8> MeanTT(nfile);
129 TVector<r_4> Freq;
130 TVector<int_4> Npaqsum(nfile);
131 string tudeb, tufin;
132 double tudeb_day, tufin_day;
133 int nfreq = 0;
134 int lpmod = nfile/10; if(lpmod<=0) lpmod=1;
135
136 int_4 ntimefill = 0, ntimebad = 0;
137 for(int ifile=ifilmin; ifile<=ifilmax; ifile++) {
138
139 // --- Lecture d'une visi elementaire (fichier acq)
140 sprintf(str, "%s/vismtx_%d_%d.ppf",indir.c_str(),numthread,ifile);
141 int status = stat(str,&buffer);
142 if(status) continue; // fichier inexistant
143 if(ifile==ifilmin || ifile==ifilmax || (ifile-ifilmin)%lpmod==0)
144 cout<<ntimefill<<" "<<ifile<<" opening: "<<str<<endl;
145 TMatrix< complex<r_4> > vismtx;
146 try {
147 PInPersist pin(str);
148 pin >> vismtx;
149 } catch(...) {
150 cout<<"ERROR: bad file "<<str<<endl;
151 ntimebad++;
152 continue;
153 }
154 tufin = (string)vismtx.Info()["DATEOBS"];
155
156 // --- Time keeping and number of summed elementary visibilities
157 MeanTT(ntimefill) = (double)vismtx.Info()["MeanTT"]/125.e6;
158 uint_4 npaqsum = vismtx.Info()["NPAQSUM"];
159
160 // --- Initialisation purposes for the first read file
161 if (ifile==ifilmin) {
162
163 tudeb = tufin;
164 printf("Reference Time: tt = %.5f sec, TU = %s\n",MeanTT(0),tudeb.c_str());
165 if(numrow>=vismtx.NRows()) {
166 cout<<"ERROR: requested numrow="<<numrow<<" => "<<vismtx.NRows()<<endl;
167 return -5;
168 }
169
170 // frequency grouping
171 int nfreq0 = vismtx.NCols();
172 cout<<"vismtx: number of frequencies = "<<nfreq0<<endl;
173 if(jfr1<=0) jfr1=0; if(jfr1>=nfreq0) jfr1=nfreq0-1;
174 if(jfr2<jfr1 || jfr2>=nfreq0) jfr2=nfreq0-1;
175 cout<<"frequency from jfr1="<<jfr1<<" to jfr2="<<jfr2<<" grouped by "<<ngrpfreq<<endl;
176 nfreq = 0; for(int i=jfr1;i<=jfr2;i+=ngrpfreq) nfreq++;
177 cout<<"frequency from [jfr1="<<jfr1<<" , jfr2="<<jfr2<<"] grouped by "<<ngrpfreq
178 <<": total of "<<nfreq<<" freq"<<endl;
179 Freq.ReSize(nfreq); Freq = 0.;
180 for(int i=0;i<nfreq;i++) {
181 int nf=0;
182 for(int j=0; j<ngrpfreq; j++) {
183 int f = jfr1 + i*ngrpfreq + j;
184 if(f>jfr2) break;
185 Freq(i) += f;
186 nf++;
187 }
188 if(nf>0) Freq(i) /= (double)nf;
189 else {cout<<"ERROR: last freq bin with 0 freq in it"<<endl; return -6;}
190 if(i<4 || i>nfreq-4) cout<<" F("<<i<<") = "<<Freq(i)<<" nf="<<nf<<endl;
191 }
192
193 // allocate visib matrice <f> vs t
194 cout<<"allocating visibility matrice ("<<nfile<<","<<nfreq<<") "
195 <<nfile*nfreq*sizeof(complex<r_4>)/1.e6<<" Mo"<<endl;
196 MVisi.ReSize(nfile,nfreq);
197 MVisi = complex<r_4>(0.,0.);
198 }
199
200 // Fill time-freq visibility matrix
201 for(int i=0;i<nfreq;i++) {
202 int nf=0;
203 for(int j=0; j<ngrpfreq; j++) {
204 int f = jfr1 + i*ngrpfreq + j;
205 if(f>jfr2) break;
206 MVisi(ntimefill,i) += vismtx(numrow,f);
207 nf++;
208 }
209 Npaqsum(ntimefill) = nf * npaqsum;
210 MVisi(ntimefill,i) /= double(Npaqsum(ntimefill));
211 if(doconj) MVisi(ntimefill,i) = conj(MVisi(ntimefill,i));
212 }
213 ntimefill++;
214
215 } // fin boucle sur le fichiers d'acq
216 cout<<"ntimefill = "<<ntimefill<<" / "<<nfile<<" , ntimebad="<<ntimebad<<" bad files"<<endl;
217
218 // --- keeping info with visi
219 {
220 // bricolo en unite de jours depuis le 0/12/2010 a 0h
221 int y,m,d,h,mn; double s;
222 sscanf(tudeb.c_str(),"%d-%d-%dT%d:%d:%lf",&y,&m,&d,&h,&mn,&s);
223 tudeb_day = (double)d + ((double)h + mn/60. + s/3600.)/24.;
224 sscanf(tufin.c_str(),"%d-%d-%dT%d:%d:%lf",&y,&m,&d,&h,&mn,&s);
225 tufin_day = (double)d + ((double)h + mn/60. + s/3600.)/24.;
226 }
227 MVisi.Info()["nth"] = numthread;
228 MVisi.Info()["row"] = numrow;
229 MVisi.Info()["dir"] = indir;
230 MVisi.Info()["dupli"] = (dupli) ? 1: 0;
231 MVisi.Info()["doconj"] = (doconj) ? 1: 0;
232 MVisi.Info()["TUobs_0"] = tudeb;
233 MVisi.Info()["TUobs_N"] = tufin;
234 MVisi.Info()["TUday_0"] = tudeb_day;
235 MVisi.Info()["TUday_N"] = tufin_day;
236 MVisi.Info()["TTag_0"] = MeanTT(0);
237 MVisi.Info()["TTag_N"] = MeanTT(MeanTT.Size()-1);
238 MVisi.Info()["freq0"] = freq0;
239 MVisi.Info()["dfreq0"] = 500./8192.;
240 MVisi.Info()["jfr1"] = jfr1;
241 MVisi.Info()["jfr2"] = jfr2;
242 MVisi.Info()["ngrpfreq"] = ngrpfreq;
243 MVisi.Info()["ifilmin"] = ifilmin;
244 MVisi.Info()["ifilmax"] = ifilmax;
245 MVisi.Info()["ntimefill"] = ntimefill;
246 MVisi.Info()["ntimebad"] = ntimebad;
247
248 // --- writing visibility matrix to file
249 sprintf(str,"%s",outname.c_str());
250 cout<<"writing visibility matrix to file "<<str<<endl;
251 POutPersist pos(str);
252 pos.PutObject(MeanTT,"meantt");
253 pos.PutObject(Npaqsum,"npaqsum");
254 pos.PutObject(Freq,"ifreq");
255 pos.PutObject(MVisi,"visi");
256 }
257
258 //--------------------------------------------------------------------
259 catch(PException& exc) {
260 cerr<<" svv2mtx2_1210.cc catched PException "<<exc.Msg()<<endl;
261 rc = 77;
262 }
263 catch (std::exception& sex) {
264 cerr<<"\n svv2mtx2_1210.cc std::exception :"
265 <<(string)typeid(sex).name() << "\n msg= "<<sex.what()<<endl;
266 rc = 78;
267 }
268 catch(...) {
269 cerr<<" svv2mtx2_1210.cc catched unknown (...) exception "<<endl;
270 rc = 79;
271 }
272 cout<<">>>> svv2mtx2_1210.cc ------- END ----------- RC="<<rc<<endl;
273 return rc;
274}
275
276
277/*
278openppf visi1_01_05.ppf
279
280print visi 5
281
282n/plot ifreq.val%n ! ! "cpts"
283
284n/plot npaqsum.val%n ! ! "cpts"
285
286n/plot meantt.val%n ! ! "cpts"
287cp meantt dmeantt
288c++exec dmeantt=0.; for(int i=1;i<dmeantt.Size();i++) dmeantt(i)=meantt(i)-meantt(i-1);
289n/plot dmeantt.val%n n>0 ! "cpts nsta"
290
291imag visi "cdmod"
292imag visi "cdreal"
293imag visi "cdimag"
294imag visi "cdphas"
295
296 */
Note: See TracBrowser for help on using the repository browser.