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

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

pb ecriture de TTag_N, cmv 07/01/2011

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