source: Sophya/trunk/Cosmo/RadioBeam/pknoise.cc@ 4027

Last change on this file since 4027 was 4027, checked in by ansari, 14 years ago

Implementatiom prise en compte dA(z) ds la calcul de bruit Pnoise(k) - Reza 17/10/2011

File size: 9.9 KB
Line 
1/* ------------------------ Projet BAORadio --------------------
2 Programme de calcul du spectre de puissance de bruit pour
3 un interferometre (spectre moyenne sur 3D -> P_noise(k) )
4
5 R. Ansari , C. Magneville - Juin 2010
6
7 Usage: pknoise pknoise [-parname value] Diameter/Four2DRespTableFile OutPPFName
8 -parname : -noise , -renmax, -scut, -ngen, -z, -bsize, -cszmpc, -nbin -prt
9--------------------------------------------------------------- */
10
11#include "machdefs.h"
12#include "sopnamsp.h"
13#include <iostream>
14#include <string>
15#include <math.h>
16
17#include <typeinfo>
18
19#include "mdish.h"
20#include "specpk.h"
21#include "interfconfigs.h"
22#include "radutil.h"
23
24#include "histinit.h"
25// #include "fiosinit.h"
26// #include "fitsioserver.h"
27
28#include "randr48.h"
29
30#include "timing.h"
31#include "ctimer.h"
32#include "slininterp.h"
33
34typedef DR48RandGen RandomGenerator ;
35
36// ---------------------------------------------------------------------
37// Test main
38// R. Ansari - Avril-Dec 2010
39// ---------------------------------------------------------------------
40
41void Usage()
42{
43 cout << " Usage: pknoise [-parname value] Diameter/Four2DRespTableFile OutPPFName \n"
44 << " -noise NoiseLevel (default=1.) \n"
45 << " -renmax MaxValue (default : Do NOT renormalize 2D response value \n"
46 << " -ngen NGen (default=0) number of noise fourier amp generations \n"
47 << " NGen=0 -> Call ComputeNoisePk(), else generate Fourier Amplitudes (random) \n"
48 << " NGen=-1 -> Call ComputeNoisePk(), dont change Four3DPk cell size \n"
49 << " -bsize sx,sy,sz : 3D real space box size (default=512x512x256) \n"
50 << " -cszmpc sx,sy,sz : 3D real space box cell size in Mpc (default=3x3x1.5) \n"
51 << " -z redshift : redshift (default=1.0) \n"
52 << " -scut SCutValue (default= -100.) \n"
53 << " if SCutValue<0. ==> SCut=MinNoisePower*(-SCutValue) \n"
54 << " -prt PrtLev,PrtModulo (default=0,10) \n"
55 << " -nbin PkNBin : nomber of histogram bins for Pnoise(k) (default= 256) \n"
56 << " NOTE : this program needs redshift_da.txt andredshift_Hz.txt files" << endl;
57 return;
58}
59
60//-------------------------------------------------------------------------
61// ------------------ MAIN PROGRAM ------------------------------
62//-------------------------------------------------------------------------
63int main(int narg, const char* arg[])
64{
65 if ( (narg<3)||((narg>1)&&(strcmp(arg[1],"-h")==0)) ) {
66 Usage();
67 return 1;
68 }
69 cout << " ==== pknoise.cc : interferometer noise power spectrum computation ==== " << endl;
70 // make sure SOPHYA modules are initialized
71 SophyaInit();
72 // FitsIOServerInit();
73 InitTim();
74 //--- decoding command line arguments
75 string tits="pknoise";
76 char cbuff[64];
77 bool fgresptbl=false;
78 double DIAMETRE=100.;
79 string resptblname;
80 double NoiseLevel=1.;
81
82 bool fgrenorm=false;
83 double rmax=1.;
84 int NMAX = 0;
85 double SCut=-100.;
86 bool fgautoscut=true;
87 double FacSCut=-SCut;
88 double z_Redshift=1.0 ; // z=0.7 : 21 cm at z=0.7 -> 0.357 m
89 double comov_dA_z=3330.; // Comoving radial distance = (1+z)dA
90 double H_z=120.5; // Hubble_param(z)
91 int box3dsz[3]={512,512,256};
92 double cellsz[3]={3.,3.,1.5};
93 int NBINPK=256;
94 int prtlev=0;
95 int prtmod=10;
96
97 int ka=1;
98 while (ka<(narg-1)) {
99 if (strcmp(arg[ka],"-noise")==0) {
100 NoiseLevel=atof(arg[ka+1]);
101 ka+=2;
102 }
103 else if (strcmp(arg[ka],"-renmax")==0) {
104 rmax=atof(arg[ka+1]); fgrenorm=true; ka+=2;
105 }
106 else if (strcmp(arg[ka],"-scut")==0) {
107 SCut=atof(arg[ka+1]); ka+=2;
108 if (SCut<0.) { FacSCut=-SCut; fgautoscut=true; }
109 }
110 else if (strcmp(arg[ka],"-ngen")==0) {
111 NMAX=atoi(arg[ka+1]); ka+=2;
112 }
113 else if (strcmp(arg[ka],"-z")==0) {
114 z_Redshift=atof(arg[ka+1]); ka+=2;
115 }
116 else if (strcmp(arg[ka],"-bsize")==0) {
117 sscanf(arg[ka+1],"%d,%d,%d",box3dsz,box3dsz+1,box3dsz+2); ka+=2;
118 }
119 else if (strcmp(arg[ka],"-cszmpc")==0) {
120 sscanf(arg[ka+1],"%lg,%lg,%lg",cellsz,cellsz+1,cellsz+2); ka+=2;
121 }
122 else if (strcmp(arg[ka],"-nbin")==0) {
123 NBINPK=atoi(arg[ka+1]); ka+=2;
124 }
125 else if (strcmp(arg[ka],"-prt")==0) {
126 sscanf(arg[ka+1],"%d,%d",&prtlev,&prtmod); ka+=2;
127 }
128 else break;
129 }
130
131 if ((ka+1)>=narg) {
132 cout << " pknoise / Argument error " << endl;
133 Usage();
134 return 2;
135 }
136 if (isdigit(*arg[ka])) {
137 fgresptbl=false;
138 DIAMETRE=atof(arg[ka]);
139 sprintf(cbuff,"pknoise_Dish(%g m)", DIAMETRE);
140 }
141 else {
142 resptblname=arg[ka];
143 fgresptbl=true;
144 sprintf(cbuff,"pknoise_RespTblName=%s", arg[ka]);
145 }
146 tits=cbuff;
147 string outfile = arg[ka+1];
148 if (outfile==".") outfile = "pknoise.ppf";
149 //-- end command line arguments
150
151 int rc = 1;
152 try { // exception handling try bloc at top level
153 cout << " pknoise[0] : Executing, output file= " << outfile << endl;
154 POutPersist po(outfile);
155
156 cout << " pknoise[0.b] : Creating interpolating objects from redshift_da.txt and redshift_Hz.txt " << endl;
157 SLinInterp1D redshift2da;
158 string interpfilename="redshift_da.txt";
159 redshift2da.ReadXYFromFile(interpfilename);
160 SLinInterp1D redshift2hz;
161 interpfilename="redshift_hz.txt";
162 redshift2hz.ReadXYFromFile(interpfilename);
163 comov_dA_z=redshift2da(z_Redshift);
164 H_z=redshift2hz(z_Redshift);
165 cout << " Redshift= " << z_Redshift << " -> ComovdA= " << comov_dA_z
166 << " Mpc H(z)= " << H_z << " km/s/Mpc " << endl;
167
168 H21Conversions conv;
169 conv.setRedshift(z_Redshift);
170 double lambda = conv.getLambda();
171
172 double Dol = DIAMETRE/lambda;
173
174 Four2DResponse arep(2, DIAMETRE/lambda, DIAMETRE/lambda, lambda);
175 Four2DResponse* arep_p=&arep;
176 Four2DRespTable resptbl;
177 if (fgresptbl) {
178 cout << "pknoise[1]: initializing Four2DRespTable from file" << resptblname << endl;
179 resptbl.readFromPPF(resptblname);
180 cout << "pknoise[1.b] Four2DRespTable.LambdaRef=" << resptbl.getLambdaRef() << " setLambda("
181 << lambda << ")" << endl;
182 resptbl.setLambda(lambda);
183 arep_p=&resptbl;
184 if (fgrenorm) {
185 cout << "pknoise[1.c] call to resptbl.renormalize(" << rmax << ")";
186 double omax=resptbl.renormalize(rmax);
187 cout << " ... Old Max=" << omax << endl;
188 }
189 }
190 else cout << " pknoise[1]: Four2DResponse ( Diameter=" << DIAMETRE << " Lambda= " << lambda
191 << " DoL=" << DIAMETRE/lambda << " ) " << endl;
192 Histo2D h2drep = arep_p->GetResponse();
193 double repmax= h2drep.VMax();
194 if (fgautoscut) {
195 SCut = FacSCut/repmax;
196 cout << " pknoise[1.b]: Four2DResponse.RepMax=" << repmax << " --> SCut=" << FacSCut << "/repmax="
197 << SCut << endl;
198 }
199 else cout << " pknoise[1.b]: Four2DResponse.RepMax=" << repmax << " , SCut=" << SCut << endl;
200
201 cout << " pknoise[2]: Instanciating object type Four3DPk " << endl;
202 RandomGenerator rg;
203
204 double dfreq=H_z*cellsz[2]/(1+z_Redshift)/lambda/1000.;
205 double freq0=conv.getFrequency()-dfreq*box3dsz[2]/2;
206
207 double dkxmpc=2.*DeuxPI;
208 double dkympc=2.*DeuxPI;
209 double dkzmpc=2.*DeuxPI;
210 double angscale=1.;
211 Vector angscales(box3dsz[2]);
212 angscales=angscale;
213 if (NMAX<0) { box3dsz[0]=256; box3dsz[1]=256; box3dsz[3]=128; }
214 else {
215 angscale=comov_dA_z;
216 for(int kz=0; kz<box3dsz[2]; kz++) angscales(kz)=redshift2da(1420.4/(freq0+(double)kz*dfreq)-1.);
217 dkxmpc = DeuxPI/(double)box3dsz[0]/cellsz[0];
218 dkympc = DeuxPI/(double)box3dsz[1]/cellsz[1];
219 dkzmpc = DeuxPI/(double)box3dsz[2]/cellsz[2];
220 }
221 cout << " pknoise[2.b]: ComovDist=" << comov_dA_z << " Mpc H(z)=" << H_z << " Mpc/km/s -> angscale=" << angscale << endl;
222 cout << " pknoise[2.c]: Freq0=" << freq0 << " dFreq=" << dfreq << " freq(z=" << z_Redshift << ")="
223 << conv.getFrequency() << " zmin=" << 1420.4/freq0-1. << " zmax=" << 1420.4/(freq0+box3dsz[2]*dfreq)-1. << endl;
224 cout << " pknoise[2.d]: Four3DPk m3d(rg," << box3dsz[0]/2 << "," << box3dsz[1] << ","
225 << box3dsz[2] << ")" << endl;
226 Four3DPk m3d(rg,box3dsz[0]/2,box3dsz[1],box3dsz[2]);
227 cout << " pknoise[2.d]: m3d.SetCellSize(" << dkxmpc << "," << dkympc << "," << dkzmpc << endl;
228 m3d.SetCellSize(dkxmpc, dkympc, dkzmpc);
229 m3d.SetPrtLevel(prtlev,prtmod);
230
231 if (NMAX>0) {
232 cout << " pknoise[3]: Computing Noise P(k) using PkNoiseCalculator ..." << endl;
233 PkNoiseCalculator pkn(m3d, *(arep_p), SCut, NMAX, tits.c_str());
234 pkn.SetFreqRange(freq0, dfreq);
235 pkn.SetAngScaleConversion(angscales);
236 pkn.SetPrtLevel(prtlev,prtmod);
237 HProf hpn = pkn.Compute(NBINPK);
238 }
239 else {
240 cout << " pknoise[3]: Computing Noise P(k) m3d.ComputeNoisePk(...) " << endl;
241 HProf hpn = m3d.ComputeNoisePk(*(arep_p),angscale,SCut,NBINPK);
242 }
243
244 HProf hpnoise=m3d.GetPk();
245 DataTable dtnoise;
246 Histo fracmodok=m3d.FillPkDataTable(dtnoise);
247 HProf h1dnoise=arep_p->GetProjNoiseLevel();
248 HProf h1drep=arep_p->GetProjResponse();
249 cout << " pknoise[3.b]: writing dtnoise,hpn,h2rep... with tags to " << outfile << endl;
250 po << PPFNameTag("dtnoise") << dtnoise;
251 po << PPFNameTag("hpnoise") << hpnoise;
252 po << PPFNameTag("fracmodok") << fracmodok;
253 po << PPFNameTag("h1dnoise") << h1dnoise;
254 po << PPFNameTag("h1drep") << h1drep;
255 po << PPFNameTag("h2drep") << h2drep;
256
257 rc = 0;
258 } // End of try bloc
259 catch (PThrowable & exc) { // catching SOPHYA exceptions
260 cerr << " pknoise.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name()
261 << "\n...exc.Msg= " << exc.Msg() << endl;
262 rc = 99;
263 }
264 catch (std::exception & e) { // catching standard C++ exceptions
265 cerr << " pknoise.cc: Catched std::exception " << " - what()= " << e.what() << endl;
266 rc = 98;
267 }
268 catch (...) { // catching other exceptions
269 cerr << " pknoise.cc: some other exception (...) was caught ! " << endl;
270 rc = 97;
271 }
272 PrtTim("End-pknoise");
273 cout << " ==== End of pknoise.cc program Rc= " << rc << endl;
274 return rc;
275}
276
277
Note: See TracBrowser for help on using the repository browser.