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

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

add info, cmv 13/12/2010

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