source: Sophya/trunk/Cosmo/SimLSS/cmvrvloscor.cc@ 3790

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

etude des cubes generes par le GSM, cmv 28/06/2010

File size: 10.7 KB
RevLine 
[3770]1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <iostream>
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <math.h>
8#include <unistd.h>
9
10#include "sophyainit.h"
11#include "timing.h"
12#include "dvlist.h"
[3773]13#include "histos.h"
[3770]14#include "fabtcolread.h"
[3781]15#include "fftwserver.h"
[3770]16
17#include "constcosmo.h"
18#include "geneutils.h"
19#include "genefluct3d.h"
[3782]20// set simul = 6_0p0_780
21// cmvrvloscorf -K 75 -S ginit3d_${simul}_r.fits ginit3d_${simul}_rv.fits
[3790]22// cmvrvloscorf -n 1,30 -K 75 -S -2 ginit3d_${simul}_r.fits ginit3d_${simul}_rv.fits
[3770]23
24void usage(void);
25void usage(void)
26{
[3781]27cout
28<<"cmvrvloscor [options] rho.fits vlos.fits"<<endl
29<<"-n nplany,nhfill: process one Y plane every \"nplany\" (def:1(all))"<<endl
30<<" fill histos with \"nhfill\" los (def:25)"<<endl
31<<"-K npix: compute correlation R*V at +/- npix pixels (def: no)"<<endl
32<<" (very time comsuming!!!)"<<endl
33<<"-S: compute cross-power spectrum of V*conj(R) (def: no)"<<endl
34<<"-N: do not create 3D cube and recompute 1D and 2D spectra (def: no do-it !)"<<endl
[3790]35<<"-2: compute 2D projection fpr dRho and dRho(corrected) (def=no)"<<endl
[3781]36<<endl;
[3770]37}
38
39int main(int narg,char *arg[])
40{
[3781]41 int nthread = 1, nplany=1, nhfilllos = 25, npixcor = 0;
[3790]42 bool docube=true, dopk = false, do2d = false;
[3781]43
44 // --- Decodage des arguments
45 char c;
[3790]46 while((c = getopt(narg,arg,"hn:K:SN2")) != -1) {
[3781]47 switch (c) {
48 case 'n' :
49 sscanf(optarg,"%d,%d",&nplany,&nhfilllos);
50 if(nplany<=0) nplany = 1;
51 if(nhfilllos<=0) nhfilllos = 0;
52 break;
53 case 'K' :
54 npixcor = atoi(optarg);
55 break;
56 case 'S' :
57 dopk = true;
58 break;
59 case 'N' :
60 docube = false;
61 break;
[3790]62 case '2' :
63 do2d = true;
64 break;
[3781]65 case 'h' :
66 default :
67 usage(); return -1;
68 }
69 }
70 if(optind>=narg-1) {usage(); return -1;}
[3770]71
72 //----TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH
73 try {
74 //----TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH
75
[3773]76 SophyaInit();
77 InitTim();
78
[3781]79 // --- open FITS files (dRho/Rho and Vlos)
80 cout<<"> read rho: "<<arg[optind]<<endl;
81 FitsImg3DRead f3dr(arg[optind],0,5);
82 cout<<"> read vlos: "<<arg[optind+1]<<endl;
83 FitsImg3DRead f3dv(arg[optind+1],0,5);
[3771]84 long Nx = f3dr.ReadKeyL("Nx");
85 long Ny = f3dr.ReadKeyL("Ny");;
86 long Nz = f3dr.ReadKeyL("Nz");;
87 cout<<"N: x="<<Nx<<" y="<<Ny<<" z="<<Nz<<endl;
88 double Dx = f3dr.ReadKey("Dx");
89 double Dy = f3dr.ReadKey("Dy");
90 double Dz = f3dr.ReadKey("Dz");
91 cout<<"D: x="<<Dx<<" y="<<Dy<<" z="<<Dz<<endl;
92 double Zref = f3dr.ReadKey("ZREF");
93 double Href = f3dr.ReadKey("HREF");
94 cout<<"Zref="<<Zref<<" Href="<<Href<<endl;
[3770]95
[3773]96 double dmin = min(Dx,min(Dy,Dz));
97 double nmax = max(Nx,max(Ny,Nz));
98 cout<<"dmin="<<dmin<<" nmax="<<nmax<<endl;
[3790]99 Histo hmpc(-dmin*nmax,dmin*nmax,4*nmax);
[3773]100
[3770]101 POutPersist pos("cmvrvloscor.ppf");
[3781]102 DVList dvlcor;
[3770]103
[3781]104 // --- Create a Cube for analysis
105 GeneFluct3D *fluct3d = NULL;
106 TArray<GEN3D_TYPE>* rgen = NULL;
107 if(docube) {
108 cout<<"...Create and fill 3D cube"<<endl;
109 fluct3d = new GeneFluct3D(Nx,Ny,Nz,Dx,Dy,Dz,nthread,2);
110 fluct3d->Print();
111 rgen = &(fluct3d->GetRealArray());
112 *rgen = 0.;
[3790]113 cout<<"rgen: size [1]="<<rgen->SizeX()
114 <<" [2]="<<rgen->SizeY()
115 <<" [3]="<<rgen->SizeZ()<<endl;
116 cout<<"pkgen: size [1]="<<fluct3d->GetComplexArray().SizeX()
117 <<" [2]="<<fluct3d->GetComplexArray().SizeY()
118 <<" [3]="<<fluct3d->GetComplexArray().SizeZ()<<endl;
[3781]119 }
120
121 // --- Vector for real-space correlation computation
122 int imil = Nz-1;
123 dvlcor("imil") = (int_4)imil;
124 TVector<int_4> nKsi;
125 TVector<r_8> Ksirv, Ksirvc, Ksirr, Ksirrc;
126 if(npixcor>0) {
127 Ksirv.ReSize(2*Nz-1); Ksirv = 0.;
128 Ksirvc.ReSize(2*Nz-1); Ksirvc = 0.;
129 Ksirr.ReSize(2*Nz-1); Ksirr = 0.;
130 Ksirrc.ReSize(2*Nz-1); Ksirrc = 0.;
131 nKsi.ReSize(2*Nz-1); nKsi = 0;
132 cout<<"...Compute R*V correlation on +/-"<<npixcor<<" px"<<endl;
133 }
134
135 // --- Vector for PK cross-correlation computation
136 int npk = 0;
137 TVector< complex<r_4> > FR, FV;
138 TVector< complex<r_8> > pkvr, FRdis;
139 TVector<r_8> pkr, pkrc;
140 FFTWServer fftserv;
141 if(dopk) cout<<"...compute V*conj(R) cross-power spectrum"<<endl;
142
143 // --- Read and process data
144 TVector<r_4> R(Nz), V(Nz);
[3773]145 TVector<r_8> Rdis(Nz);
[3781]146 if(nplany>Ny) nplany = Ny;
147 cout<<"...Will read one Y plane every "<<nplany<<endl;
148 if(nhfilllos) {
149 cout<<"...Fill Mpc displacement histo with "<<nhfilllos<<" los"<<endl;
150 nhfilllos = int((double)Nx*Ny/nplany/nhfilllos + 0.5);
151 if(nhfilllos<=0) nhfilllos = 1;
152 cout<<" -> fill one los every "<<nhfilllos<<endl;
153 }
[3770]154
[3781]155 cout<<">>> filling redshift distorted cube"<<endl;
156 int nlosread = 0;
[3771]157 for(int i=0;i<Nx;i++) {
[3781]158 if(i%(Nx/10)==0) cout<<" i="<<i<<endl;
[3790]159 TMatrix<r_4> M2d, M2dc;
160 if(do2d && (i==0 || i==Nx/2 || i==Nx-1)) {
161 M2d.ReSize(Ny,Nz); M2d = 0.;
162 M2dc.ReSize(Ny,Nz); M2dc = 0.;
163 }
[3781]164 for(int j=0;j<Ny;j+=nplany) {
165 bool fhis = false;
166 if(nhfilllos) if(nlosread%nhfilllos==0) fhis = true;
[3773]167 //for(int l=0;l<Nz;l++) R(l) = f3dr.Read(l,j,i);
168 //for(int l=0;l<Nz;l++) V(l) = f3dv.Read(l,j,i);
169 f3dr.Read(j,i,R);
170 f3dv.Read(j,i,V);
[3770]171 Rdis = 0.;
[3781]172 // Calcul du champ R redshift distordu
[3771]173 for(int l=0;l<Nz;l++) {
[3773]174 double d = (1.+Zref) / Href * V(l);
[3781]175 if(fhis) hmpc.Add(d);
[3771]176 double lpd = (double)l + d/Dz; // valeur du deplacee
[3781]177 // on repartit proportionellement au recouvrement sur 2 pixels
[3790]178 long l1 = long(lpd); // pixel de gauche
179 long l2 = l1 + 1; // pixel de droite
[3771]180 lpd -= (double)l1; // recouvrement du pixel du dessus
181 if(l1>=0 && l1<Nz) Rdis(l1) += R(l) * (1.-lpd);
182 if(l2>=0 && l2<Nz) Rdis(l2) += R(l) * lpd;
[3770]183 }
[3790]184 // On remplit eventuellement les matrices 2D
185 if(do2d && M2d.Size()>0)
186 for(int l=0;l<Nz;l++) {M2d(j,l) = R(l); M2dc(j,l) = Rdis(l);}
[3781]187 // On remplit le cube avec le champ R redshift distordu
188 if(fluct3d) for(int l=0;l<Nz;l++) (*rgen)(l,j,i) += Rdis(l);
189 // Calcul eventuel de la fonction de correlation R*V
190 if(npixcor>0) {
191 for(long l1=0;l1<Nz;l1++) {
192 for(long l2=max(0L,l1-npixcor);l2<min(Nz,l1+npixcor);l2++) {
193 int lc = imil+(l2-l1);
194 Ksirr(lc) += R(l1)*R(l2);
195 Ksirrc(lc) += Rdis(l1)*R(l2);
196 Ksirv(lc) += R(l1)*V(l2);
197 Ksirvc(lc) += Rdis(l1)*V(l2);
198 nKsi(lc)++;
199 }
200 }
201 }
202 // Cross-power spectrum computation
203 if(dopk) {
204 fftserv.FFTForward(V,FV);
205 int nf = FV.Size();
206 if(pkvr.Size()<=0) {
207 cout<<"...Create vector for cross-power spectrum computation"<<endl;
208 pkvr.ReSize(nf); pkvr = complex<r_8>(0.);
209 pkr.ReSize(nf); pkr = 0.;
210 pkrc.ReSize(nf); pkrc = 0.;
211 }
212 fftserv.FFTForward(R,FR);
213 for(int l=0;l<nf;l++) {
214 pkvr(l) += FV(l)*conj(FR(l));
215 pkr(l) += norm(FR(l));
216 }
217 fftserv.FFTForward(Rdis,FRdis);
218 for(int l=0;l<nf;l++) pkrc(l) += norm(FRdis(l));
219 npk++;
220 }
221 nlosread++;
[3770]222 }
[3790]223 if(do2d && M2d.Size()>0) {
224 char str[64];
225 sprintf(str,"mx_%d",i);
226 pos.PutObject(M2d,str);
227 sprintf(str,"mxc_%d",i);
228 pos.PutObject(M2dc,str);
229 }
[3770]230 }
[3781]231
232 cout<<"Number of processed los: "<<nlosread<<" / "<<Nx*Ny<<endl;
233 dvlcor("nlosread") = (int_4)nlosread;
234 if(hmpc.NEntries()>0) {
235 hmpc.Show();
236 pos.PutObject(hmpc,"hmpc");
237 }
238 if(Ksirr.Size()>0) {
239 for(int l=0;l<Ksirr.Size();l++) if(nKsi(l)>0) {
240 Ksirr(l) /= nKsi(l);
241 Ksirrc(l) /= nKsi(l);
242 Ksirv(l) /= nKsi(l);
243 Ksirvc(l) /= nKsi(l);
244 }
245 pos.PutObject(Ksirr,"ksirr");
246 pos.PutObject(Ksirrc,"ksirrc");
247 pos.PutObject(Ksirv,"ksirv");
248 pos.PutObject(Ksirvc,"ksirvc");
249 pos.PutObject(nKsi,"nksi");
250 }
251 if(npk>0) {
252 pkvr /= (double)npk;
253 pkr /= (double)npk;
254 pkrc /= (double)npk;
255 pos.PutObject(pkvr,"pkvr");
256 pos.PutObject(pkr,"pkr");
257 pos.PutObject(pkrc,"pkrc");
258 }
[3770]259 PrtTim(">>>> End filling redshift distorted cube");
260
[3781]261 // --- Fourier transform 3D cube and compute 1D and 2D power spectra
262 if(fluct3d) {
263 cout<<">>> Fourier transform 3D cube and compute 1D and 2D power spectra"<<endl;
264 // do the FFT for spectrum analysis
265 fluct3d->ReComputeFourier();
266 PrtTim(">>>> End ReComputing spectrum");
[3770]267
[3781]268 // Compute 1D spectrum
269 cout<<endl<<"\n--- Computing final 1D spectrum"<<endl;
270 double dkmin = fluct3d->GetKincMin(), knyqmax = fluct3d->GetKmax();
271 long nherr = long(knyqmax/dkmin+0.5);
272 cout<<"\nFor HistoErr: d="<<dkmin<<" max="<<knyqmax<<" n="<<nherr<<endl;
273 HistoErr hpkrec(0.,knyqmax,nherr); hpkrec.Zero();
274 hpkrec.ReCenterBin(); hpkrec.Show();
275 fluct3d->ComputeSpectrum(hpkrec);
276 pos.PutObject(hpkrec,"hpkrec");
277 PrtTim(">>>> End Computing final spectrum");
[3770]278
[3781]279 // Compute 2D spectrum
280 cout<<"\n--- Computing final 2D spectrum"<<endl;
281 double dktmin = fluct3d->GetKTincMin(), ktnyqmax = fluct3d->GetKTmax();
282 double dkzmin = fluct3d->GetKinc()[2], kznyqmax = fluct3d->GetKnyq()[2];
283 long nherrt = long(ktnyqmax/dktmin+0.5), nherrz = long(kznyqmax/dkzmin+0.5);
284 cout<<"For Histo2DErr: d="<<dktmin<<","<<dkzmin
285 <<" max="<<ktnyqmax<<","<<kznyqmax<<" n="<<nherrt<<","<<nherrz<<endl;
286 Histo2DErr hpkrec2(0.,ktnyqmax,nherrt,0.,kznyqmax,nherrz);
287 hpkrec2.ReCenterBin(); hpkrec2.Zero(); hpkrec2.Show();
288 fluct3d->ComputeSpectrum2D(hpkrec2);
289 pos.PutObject(hpkrec2,"hpkrec2");
290 PrtTim(">>>> End Computing final 2D spectrum");
291 }
[3770]292
[3781]293 // --- end of job, write objects in ppf
294 pos.PutObject(dvlcor,"dvlcor");
295 if(fluct3d) delete fluct3d;
296
[3770]297 //----TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH
298 } catch (PException& exc) {
299 cerr<<"cmvrvloscor.cc catched PException"<<exc.Msg()<<endl;
300 return 77;
301 } catch (std::exception& sex) {
302 cerr << "cmvrvloscor.cc std::exception :"
303 << (string)typeid(sex).name() << "\n msg= "
304 << sex.what() << endl;
305 return 78;
306 } catch (...) {
307 cerr << "cmvrvloscor.cc catched unknown (...) exception " << endl;
308 return 79;
309 }
310 //----TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH-TRY-CATCH
311
312 return 0;
313}
314
315/*
316openppf cmvrvloscor.ppf
317
[3773]318disp hmpc
319
[3781]320# cross-correlation
321disp nksi
322set imil ${dvlcor.imil}
323n/plot ksirv.val%n-${imil} ! ! "nsta cpts"
324n/plot ksirvc.val%n-${imil} ! ! "nsta cpts same red"
325
326n/plot ksirr.val%n-${imil} ! ! "nsta cpts"
327n/plot ksirrc.val%n-${imil} ! ! "nsta cpts same red"
328
329# cross-power spectrum
330n/plot pkr.val%n ! ! "nsta cpts logx"
331n/plot pkrc.val%n ! ! "nsta cpts logx same red"
332
333n/plot pkvr.val%n ! ! "nsta cpts logx"
334
335# reconstructed 1D power spectrum
[3770]336n/plot hpkrec.val%x x>0 ! "nsta cpts logx"
337
[3782]338# reconstructed 2D power spectrum
[3770]339imag hpkrec2
[3771]340addoval 0 0 0.05 0.05 "green" false
341addoval 0 0 0.1 0.1 "green" false
342addoval 0 0 0.25 0.25 "green" false
343addoval 0 0 0.5 0.5 "green" false
344x = ${hpkrec2.xmax} / 2.
345addoval 0 0 $x $x "green" false
346x = ${hpkrec2.ymax} / 2.
347addoval 0 0 $x $x "green" false
[3770]348
349# proj selon kT (black), selon kZ (red)
[3773]350n/plot hpkrec2.val%sqrt(x*x+y*y) ! ! "nsta crossmarker3 logx"
[3790]351
352# les matrices 2D
353set n 0
354disp mx_$n
355newwin
356disp mxc_$n
[3770]357 */
Note: See TracBrowser for help on using the repository browser.