| [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
 | 
|---|
 | 8 |           -parname : -noise , -renmax , -scut , -ngen , -z , -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"
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | typedef DR48RandGen RandomGenerator ;
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | // ---------------------------------------------------------------------
 | 
|---|
| [3930] | 36 | // Test main 
 | 
|---|
 | 37 | // R. Ansari  - Avril-Dec 2010 
 | 
|---|
| [3756] | 38 | // ---------------------------------------------------------------------
 | 
|---|
 | 39 | 
 | 
|---|
| [3931] | 40 | void Usage() 
 | 
|---|
 | 41 | {
 | 
|---|
 | 42 |   cout << " Usage:  pknoise [-parname value] Diameter/Four2DRespTableFile OutPPFName \n" 
 | 
|---|
 | 43 |        << "    -noise NoiseLevel (default=1.) \n"
 | 
|---|
 | 44 |        << "    -renmax MaxValue (default : Do NOT renormalize 2D response value \n"   
 | 
|---|
| [3947] | 45 |        << "    -ngen NGen (default=0) number of noise fourier amp generations \n"
 | 
|---|
 | 46 |        << "       NGen=0 -> Call ComputeNoisePk(), else generate Fourier Amplitudes (random) \n"
 | 
|---|
| [3931] | 47 |        << "    -z redshift (default=0.7) \n"
 | 
|---|
| [3948] | 48 |        << "    -scut SCutValue (default= -100.) \n"
 | 
|---|
 | 49 |        << "       if SCutValue<0. ==> SCut=MinNoisePower*(-SCutValue) \n"
 | 
|---|
| [3931] | 50 |        << "    -prt PrtLev,PrtModulo (default=0,10) " << endl;
 | 
|---|
 | 51 |   return;
 | 
|---|
 | 52 | }
 | 
|---|
| [3756] | 53 | 
 | 
|---|
| [3769] | 54 | //-------------------------------------------------------------------------
 | 
|---|
 | 55 | //      ------------------ MAIN PROGRAM ------------------------------
 | 
|---|
 | 56 | //-------------------------------------------------------------------------
 | 
|---|
| [3756] | 57 | int main(int narg, const char* arg[])
 | 
|---|
 | 58 | {
 | 
|---|
| [3930] | 59 |   if ( (narg<3)||((narg>1)&&(strcmp(arg[1],"-h")==0)) )  {
 | 
|---|
| [3931] | 60 |     Usage();
 | 
|---|
| [3756] | 61 |     return 1;
 | 
|---|
 | 62 |   }
 | 
|---|
| [3930] | 63 |   cout << " ==== pknoise.cc : interferometer noise power spectrum computation ==== " << endl;
 | 
|---|
| [3756] | 64 |   // make sure SOPHYA modules are initialized 
 | 
|---|
 | 65 |   SophyaInit();  
 | 
|---|
 | 66 |   //  FitsIOServerInit();
 | 
|---|
 | 67 |   InitTim();
 | 
|---|
 | 68 |   //--- decoding command line arguments 
 | 
|---|
| [3930] | 69 |   string tits="pknoise";
 | 
|---|
 | 70 |   char cbuff[64];
 | 
|---|
 | 71 |   bool fgresptbl=false;
 | 
|---|
 | 72 |   double DIAMETRE=100.;
 | 
|---|
 | 73 |   string resptblname;
 | 
|---|
| [3931] | 74 |   double NoiseLevel=1.;
 | 
|---|
 | 75 |   
 | 
|---|
 | 76 |   bool fgrenorm=false;
 | 
|---|
 | 77 |   double rmax=1.;
 | 
|---|
| [3947] | 78 |   int NMAX = 0;
 | 
|---|
| [3948] | 79 |   double SCut=-100.;
 | 
|---|
 | 80 |   bool fgautoscut=true;
 | 
|---|
 | 81 |   double FacSCut=-SCut;
 | 
|---|
| [3931] | 82 |   double z_Redshift=0.7 ;  // 21 cm at z=0.7 -> 0.357 m  
 | 
|---|
 | 83 |   int prtlev=0;
 | 
|---|
 | 84 |   int prtmod=10;
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   int ka=1;
 | 
|---|
 | 87 |   while (ka<(narg-1)) {
 | 
|---|
 | 88 |     if (strcmp(arg[ka],"-noise")==0) {
 | 
|---|
 | 89 |       NoiseLevel=atof(arg[ka+1]);
 | 
|---|
 | 90 |       ka+=2;
 | 
|---|
 | 91 |     }
 | 
|---|
 | 92 |     else if (strcmp(arg[ka],"-renmax")==0) {
 | 
|---|
 | 93 |       rmax=atof(arg[ka+1]);  fgrenorm=true;   ka+=2;
 | 
|---|
 | 94 |     }
 | 
|---|
 | 95 |     else if (strcmp(arg[ka],"-scut")==0) {
 | 
|---|
 | 96 |       SCut=atof(arg[ka+1]);    ka+=2; 
 | 
|---|
| [3948] | 97 |       if (SCut<0.) { FacSCut=-SCut;  fgautoscut=true; }
 | 
|---|
| [3931] | 98 |     }
 | 
|---|
 | 99 |     else if (strcmp(arg[ka],"-ngen")==0) {
 | 
|---|
 | 100 |       NMAX=atoi(arg[ka+1]);    ka+=2;
 | 
|---|
 | 101 |     }
 | 
|---|
 | 102 |     else if (strcmp(arg[ka],"-z")==0) {
 | 
|---|
 | 103 |       z_Redshift=atof(arg[ka+1]);  ka+=2;
 | 
|---|
 | 104 |     }
 | 
|---|
 | 105 |     else if (strcmp(arg[ka],"-prt")==0) {
 | 
|---|
 | 106 |       sscanf(arg[ka+1],"%d,%d",&prtlev,&prtmod);   ka+=2;
 | 
|---|
 | 107 |     }
 | 
|---|
 | 108 |     else break; 
 | 
|---|
 | 109 |   }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |   if ((ka+1)>=narg) {
 | 
|---|
 | 112 |     cout << " pknoise / Argument error " << endl;
 | 
|---|
 | 113 |     Usage();
 | 
|---|
 | 114 |     return 2;
 | 
|---|
 | 115 |   }
 | 
|---|
 | 116 |   if (isdigit(*arg[ka])) {
 | 
|---|
| [3930] | 117 |     fgresptbl=false;
 | 
|---|
| [3931] | 118 |     DIAMETRE=atof(arg[ka]);
 | 
|---|
| [3930] | 119 |     sprintf(cbuff,"pknoise_Dish(%g m)", DIAMETRE);
 | 
|---|
 | 120 |   }
 | 
|---|
 | 121 |   else { 
 | 
|---|
| [3931] | 122 |     resptblname=arg[ka];
 | 
|---|
| [3930] | 123 |     fgresptbl=true;
 | 
|---|
| [3931] | 124 |     sprintf(cbuff,"pknoise_RespTblName=%s", arg[ka]);
 | 
|---|
| [3930] | 125 |   }
 | 
|---|
 | 126 |   tits=cbuff;
 | 
|---|
| [3931] | 127 |   string outfile = arg[ka+1];  
 | 
|---|
| [3756] | 128 |   if (outfile==".")  outfile = "pknoise.ppf";
 | 
|---|
| [3931] | 129 |  //-- end command line arguments
 | 
|---|
| [3756] | 130 |   
 | 
|---|
 | 131 |   int rc = 1;  
 | 
|---|
 | 132 |   try {  // exception handling try bloc at top level
 | 
|---|
| [3930] | 133 |     cout << " pknoise[0] : Executing, output file= " << outfile << endl;  
 | 
|---|
| [3756] | 134 |     POutPersist po(outfile);
 | 
|---|
 | 135 | 
 | 
|---|
| [3930] | 136 |     H21Conversions conv;
 | 
|---|
 | 137 |     conv.setRedshift(z_Redshift);
 | 
|---|
 | 138 |     double lambda = conv.getLambda();
 | 
|---|
| [3756] | 139 | 
 | 
|---|
| [3930] | 140 |     double Dol = DIAMETRE/lambda;
 | 
|---|
| [3756] | 141 | 
 | 
|---|
| [3930] | 142 |     Four2DResponse arep(2, DIAMETRE/lambda, DIAMETRE/lambda, lambda);
 | 
|---|
 | 143 |     Four2DResponse* arep_p=&arep;
 | 
|---|
 | 144 |     Four2DRespTable resptbl;
 | 
|---|
 | 145 |     if (fgresptbl) {
 | 
|---|
 | 146 |       cout << "pknoise[1]: initializing Four2DRespTable from file" << resptblname << endl;
 | 
|---|
 | 147 |       resptbl.readFromPPF(resptblname);
 | 
|---|
| [3947] | 148 |       cout << "pknoise[1.b] Four2DRespTable.LambdaRef=" << resptbl.getLambdaRef() << " setLambda(" 
 | 
|---|
 | 149 |            << lambda << ")" << endl;
 | 
|---|
 | 150 |       resptbl.setLambda(lambda);
 | 
|---|
| [3930] | 151 |       arep_p=&resptbl;
 | 
|---|
 | 152 |       if (fgrenorm) {
 | 
|---|
| [3947] | 153 |         cout << "pknoise[1.c] call to resptbl.renormalize(" << rmax << ")"; 
 | 
|---|
| [3930] | 154 |         double omax=resptbl.renormalize(rmax);
 | 
|---|
 | 155 |         cout << " ... Old Max=" << omax << endl;
 | 
|---|
 | 156 |       }
 | 
|---|
| [3769] | 157 |     }
 | 
|---|
| [3930] | 158 |     else cout << " pknoise[1]: Four2DResponse ( Diameter=" << DIAMETRE << " Lambda= " << lambda
 | 
|---|
 | 159 |               << " DoL=" << DIAMETRE/lambda << " ) " << endl;
 | 
|---|
| [3948] | 160 |     Histo2D h2drep = arep_p->GetResponse();
 | 
|---|
 | 161 |     double repmax= h2drep.VMax();
 | 
|---|
 | 162 |     if (fgautoscut) {
 | 
|---|
 | 163 |       SCut = FacSCut/repmax;
 | 
|---|
 | 164 |       cout << " pknoise[1.b]: Four2DResponse.RepMax=" << repmax << " --> SCut=" << FacSCut << "/repmax=" 
 | 
|---|
 | 165 |            << SCut << endl;
 | 
|---|
 | 166 |     }
 | 
|---|
 | 167 |     else cout << " pknoise[1.b]: Four2DResponse.RepMax=" << repmax << " , SCut=" << SCut << endl;
 | 
|---|
| [3756] | 168 |     
 | 
|---|
| [3930] | 169 |     cout << " pknoise[2]: Instanciating object type Four3DPk  " << endl;
 | 
|---|
| [3756] | 170 |     RandomGenerator rg;
 | 
|---|
 | 171 |     Four3DPk m3d(rg);
 | 
|---|
 | 172 |     m3d.SetCellSize(2.*DeuxPI, 2.*DeuxPI, 2.*DeuxPI); 
 | 
|---|
| [3930] | 173 |     m3d.SetPrtLevel(prtlev,prtmod);
 | 
|---|
| [3756] | 174 | 
 | 
|---|
| [3930] | 175 |     cout << " pknoise[3]: Computing Noise P(k) using PkNoiseCalculator ..." << endl;
 | 
|---|
| [3947] | 176 |     if (NMAX>0) {
 | 
|---|
 | 177 |       PkNoiseCalculator pkn(m3d, *(arep_p), SCut, NMAX, tits.c_str());
 | 
|---|
 | 178 |       pkn.SetPrtLevel(prtlev,prtmod);
 | 
|---|
 | 179 |       HProf hpn = pkn.Compute();
 | 
|---|
| [3948] | 180 |       cout << " pknoise[3.b]: writing hpn noise profile to " << outfile << endl;
 | 
|---|
| [3947] | 181 |       po << hpn;
 | 
|---|
 | 182 |     }
 | 
|---|
 | 183 |     else {
 | 
|---|
 | 184 |       Histo fracmodok;
 | 
|---|
 | 185 |       DataTable dtnoise;
 | 
|---|
 | 186 |       HProf hpn = m3d.ComputeNoisePk(*(arep_p),fracmodok,dtnoise,SCut);
 | 
|---|
 | 187 |       HProf h1dnoise=arep_p->GetProjNoiseLevel();
 | 
|---|
 | 188 |       HProf h1drep=arep_p->GetProjResponse();
 | 
|---|
| [3948] | 189 |       cout << " pknoise[3.b]: writing dtnoise,hpn,h2rep... with tags to " << outfile << endl;
 | 
|---|
 | 190 |       po << PPFNameTag("dtnoise") << dtnoise;
 | 
|---|
 | 191 |       po << PPFNameTag("hpnoise") << hpn;
 | 
|---|
 | 192 |       po << PPFNameTag("fracmodok") << fracmodok;
 | 
|---|
 | 193 |       po << PPFNameTag("h1dnoise") << h1dnoise;
 | 
|---|
 | 194 |       po << PPFNameTag("h1drep") << h1drep;
 | 
|---|
 | 195 |       po << PPFNameTag("h2drep") << h2drep;
 | 
|---|
| [3947] | 196 |     }
 | 
|---|
| [3756] | 197 |     rc = 0;
 | 
|---|
 | 198 |   }  // End of try bloc 
 | 
|---|
 | 199 |   catch (PThrowable & exc) {  // catching SOPHYA exceptions
 | 
|---|
 | 200 |     cerr << " pknoise.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name() 
 | 
|---|
 | 201 |          << "\n...exc.Msg= " << exc.Msg() << endl;
 | 
|---|
 | 202 |     rc = 99;
 | 
|---|
 | 203 |   }
 | 
|---|
 | 204 |   catch (std::exception & e) {  // catching standard C++ exceptions
 | 
|---|
 | 205 |     cerr << " pknoise.cc: Catched std::exception "  << " - what()= " << e.what() << endl;
 | 
|---|
 | 206 |     rc = 98;
 | 
|---|
 | 207 |   }
 | 
|---|
 | 208 |   catch (...) {  // catching other exceptions
 | 
|---|
 | 209 |     cerr << " pknoise.cc: some other exception (...) was caught ! " << endl;
 | 
|---|
 | 210 |     rc = 97;
 | 
|---|
 | 211 |   }
 | 
|---|
 | 212 |   PrtTim("End-pknoise");
 | 
|---|
 | 213 |   cout << " ==== End of pknoise.cc program  Rc= " << rc << endl;
 | 
|---|
 | 214 |   return rc;    
 | 
|---|
 | 215 | }
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 | 
 | 
|---|