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

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

ameliorations mineures, cmv 20/05/2010

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