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