| 1 | /*  ------------------------ Projet BAORadio -------------------- 
 | 
|---|
| 2 |   Programme de calcul du spectre de puissance (3D) a partir d'un 
 | 
|---|
| 3 |   cube de  delta T/T LSS, d'un cube  delta T/T LSS synchrotron
 | 
|---|
| 4 |   ou radio-sources, apres ajustement / soustraction d'une loi de 
 | 
|---|
| 5 |   puissance en frequence (l'axe Z du tableau doit etre en frequence)
 | 
|---|
| 6 | 
 | 
|---|
| 7 |     R. Ansari , C. Magneville - Juin 2010 
 | 
|---|
| 8 | 
 | 
|---|
| 9 | Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPkFile 
 | 
|---|
| 10 |                [PixNoiseLevel] [Diameter/Four2DRespTableFile] [TargetBeamArcmin] [NSigSrcThr]
 | 
|---|
| 11 | ---------------------------------------------------------------  */
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "machdefs.h"
 | 
|---|
| 14 | #include "sopnamsp.h"
 | 
|---|
| 15 | #include <iostream>
 | 
|---|
| 16 | #include <string>
 | 
|---|
| 17 | #include <math.h>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <typeinfo>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "specpk.h"
 | 
|---|
| 22 | #include "histats.h"
 | 
|---|
| 23 | #include "vector3d.h"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "qhist.h" 
 | 
|---|
| 26 | #include "lobe.h" 
 | 
|---|
| 27 | #include "cubedef.h" 
 | 
|---|
| 28 | #include "fgndsub.h" 
 | 
|---|
| 29 | #include "radutil.h" 
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include "histinit.h"
 | 
|---|
| 32 | #include "fftwserver.h"
 | 
|---|
| 33 | #include "randr48.h"      
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "ctimer.h"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | typedef ThSDR48RandGen RandomGenerator ;
 | 
|---|
| 38 | 
 | 
|---|
| 39 | //-------------------------------------------------------------------------
 | 
|---|
| 40 | //      ------------------ MAIN PROGRAM ------------------------------
 | 
|---|
| 41 | //-------------------------------------------------------------------------
 | 
|---|
| 42 | /* --Fonction-- */
 | 
|---|
| 43 | int main(int narg, const char* arg[])
 | 
|---|
| 44 | {
 | 
|---|
| 45 |   if (narg<6) {
 | 
|---|
| 46 |     cout << " Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync  OutPkFile \n" 
 | 
|---|
| 47 |          << "        [PixNoiseLevel] [Diameter/Four2DRespTableFile] [TargetBeamArcmin] [NSigSrcThr]" << endl;
 | 
|---|
| 48 |     return 1;
 | 
|---|
| 49 |   }
 | 
|---|
| 50 |   Timer tm("calcpk2");
 | 
|---|
| 51 |   int rc = 0;
 | 
|---|
| 52 |   try {
 | 
|---|
| 53 |     string inppflss = arg[1];
 | 
|---|
| 54 |     r_4 rfaclss = atof(arg[2]);
 | 
|---|
| 55 |     string inppfsync = arg[3];
 | 
|---|
| 56 |     r_4 rfacsync = atof(arg[4]);
 | 
|---|
| 57 |     string outname = arg[5];
 | 
|---|
| 58 | 
 | 
|---|
| 59 |     double pixsignoise = 0.;
 | 
|---|
| 60 |     bool fgaddnoise=false;
 | 
|---|
| 61 |     if (narg>6) {
 | 
|---|
| 62 |       pixsignoise=atof(arg[6]);
 | 
|---|
| 63 |       if (pixsignoise>1.e-6)  fgaddnoise=true;
 | 
|---|
| 64 |     }
 | 
|---|
| 65 |     
 | 
|---|
| 66 |     bool fgcorrbeam=true;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |     bool fgresptbl=false;
 | 
|---|
| 69 |     double DIAMETRE=100.;
 | 
|---|
| 70 |     string resptblname;
 | 
|---|
| 71 |     if (narg>7) {
 | 
|---|
| 72 |       if (isdigit(*arg[7])) {
 | 
|---|
| 73 |         fgresptbl=false;
 | 
|---|
| 74 |         DIAMETRE=atof(arg[7]);
 | 
|---|
| 75 |       }
 | 
|---|
| 76 |       else { 
 | 
|---|
| 77 |         resptblname=arg[7];
 | 
|---|
| 78 |         fgresptbl=true;
 | 
|---|
| 79 |       }
 | 
|---|
| 80 |     }
 | 
|---|
| 81 |     double tbeamDoL=135;
 | 
|---|
| 82 |     if (narg>8) {
 | 
|---|
| 83 |       tbeamDoL=atof(arg[8]);
 | 
|---|
| 84 |       if (tbeamDoL<1.)  fgcorrbeam=false;
 | 
|---|
| 85 |     }
 | 
|---|
| 86 |     bool fgclnsrc=true;
 | 
|---|
| 87 |     double nsigsrc=5.;
 | 
|---|
| 88 |     if (narg>9) {
 | 
|---|
| 89 |       nsigsrc=atof(arg[9]);
 | 
|---|
| 90 |       if (nsigsrc<1.e-6)  fgclnsrc=false;
 | 
|---|
| 91 |     }
 | 
|---|
| 92 | 
 | 
|---|
| 93 |     TArray<r_4> maplss, mapsync;
 | 
|---|
| 94 |     const char * tits[2]={"LSS", "Sync/RadioSrc"};
 | 
|---|
| 95 |     for(int ks=0; ks<2; ks++)  {
 | 
|---|
| 96 |       string& ppfname=inppflss;
 | 
|---|
| 97 |       r_4 rfac=rfaclss;
 | 
|---|
| 98 |       TArray<r_4>* inmap=&maplss; 
 | 
|---|
| 99 |       if (ks==1) {  ppfname=inppfsync;  rfac=rfacsync;  inmap=&mapsync; }
 | 
|---|
| 100 |       cout << "calcpk2[" << ks+1 << "] : reading 3D map " << tits[ks] << " from file " << ppfname 
 | 
|---|
| 101 |            << "  RenormFactor=" << rfac << endl;
 | 
|---|
| 102 |       PInPersist pin(ppfname);
 | 
|---|
| 103 |       pin >> (*inmap);
 | 
|---|
| 104 |       (*inmap) *= rfac;
 | 
|---|
| 105 |       double mean, sigma;
 | 
|---|
| 106 |       MeanSigma(*inmap, mean, sigma);
 | 
|---|
| 107 |       cout << " ...InMap sizes " << inmap->InfoString() << endl;
 | 
|---|
| 108 |       inmap->Show(); 
 | 
|---|
| 109 |       cout << " ... Mean=" << mean << " Sigma=" << sigma << endl;
 | 
|---|
| 110 |     }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |     bool smo; 
 | 
|---|
| 113 |     if (!maplss.CompareSizes(mapsync,smo) ) {
 | 
|---|
| 114 |       cout << " calcpk2/ERROR sizes " << endl;
 | 
|---|
| 115 |       maplss.Show();  mapsync.Show(); 
 | 
|---|
| 116 |       return 99;
 | 
|---|
| 117 |     }
 | 
|---|
| 118 |     
 | 
|---|
| 119 |     TArray<r_4> skycube(mapsync);
 | 
|---|
| 120 |     skycube += maplss;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |     if (fgaddnoise) { 
 | 
|---|
| 123 |       cout << " calcpk2: adding noise to skycube cube ... " << endl;
 | 
|---|
| 124 |       BeamEffect::AddNoise(skycube, pixsignoise);
 | 
|---|
| 125 |     }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     double mean, sigma;
 | 
|---|
| 128 |     MeanSigma(skycube, mean, sigma);
 | 
|---|
| 129 |     cout << " input sky cube : Mean=" << mean << " Sigma=" << sigma << endl;      
 | 
|---|
| 130 |     tm.Split(" After input ");
 | 
|---|
| 131 | 
 | 
|---|
| 132 |     H21Conversions conv;
 | 
|---|
| 133 |     conv.setFrequency(Freq0MHz);
 | 
|---|
| 134 |     double lambda = conv.getLambda();
 | 
|---|
| 135 |     Four2DResponse arep(2, DIAMETRE/lambda, DIAMETRE/lambda, lambda);
 | 
|---|
| 136 |     Four2DResponse* arep_p=&arep;
 | 
|---|
| 137 |     Four2DRespTable resptbl;
 | 
|---|
| 138 |     if (fgresptbl) {
 | 
|---|
| 139 |       cout << "calcpk2[3.a]: initializing Four2DRespTable from file" << resptblname << endl;
 | 
|---|
| 140 |       resptbl.readFromPPF(resptblname);
 | 
|---|
| 141 |       arep_p=&resptbl;
 | 
|---|
| 142 |     }
 | 
|---|
| 143 |     else cout << " calcpk2[3.a]: Four2DResponse ( Diameter=" << DIAMETRE << " Lambda= " << lambda
 | 
|---|
| 144 |               << " DoL=" << DIAMETRE/lambda << " ) " << endl;
 | 
|---|
| 145 | 
 | 
|---|
| 146 |     double DoL = tbeamDoL;
 | 
|---|
| 147 |     double tbeamarcmin = RadianToDegree(1.22/DoL)*60.;
 | 
|---|
| 148 |     int typcb = 2;
 | 
|---|
| 149 |     //    if (fgresptbl) typcb=22;
 | 
|---|
| 150 |     Four2DResponse tbeam(typcb, DoL, DoL );
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     ForegroundCleaner  cleaner(*arep_p, tbeam, skycube); 
 | 
|---|
| 153 |     if (fgcorrbeam) {
 | 
|---|
| 154 |       cout << "calcpk2[3.b] : calling cleaner.BeamCorrections() for target beam D/Lambda=" << DoL 
 | 
|---|
| 155 |            << "  -> arcmin " << tbeamarcmin << " TypDishResp=" << typcb << endl;
 | 
|---|
| 156 |       cleaner.BeamCorrections();
 | 
|---|
| 157 |     }
 | 
|---|
| 158 |     cout << " calcpk2[3.c] : calling cleaner.CleanNegatives() ... " << endl;
 | 
|---|
| 159 |     cleaner.CleanNegatives();
 | 
|---|
| 160 |     if (fgclnsrc) {
 | 
|---|
| 161 |       cout << "calcpk2[3.d] : calling cleaner.CleanPointSources() with threshold NSigma=" << nsigsrc  << endl;
 | 
|---|
| 162 |       cleaner.CleanPointSources(nsigsrc);
 | 
|---|
| 163 |     }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |     cout << "calcpk2[4] : calling cleaner.extractLSSCube(...) " << endl;
 | 
|---|
| 166 |     TArray<r_4> synctemp, specidx; 
 | 
|---|
| 167 |     TArray<r_4> exlss = cleaner.extractLSSCube(synctemp, specidx);
 | 
|---|
| 168 | 
 | 
|---|
| 169 |     MeanSigma(exlss, mean, sigma);
 | 
|---|
| 170 |     cout << " After cleaning/extractLSS: Mean=" << mean << " Sigma=" << sigma << endl;      
 | 
|---|
| 171 |     tm.Split(" After CleanForeground");
 | 
|---|
| 172 | 
 | 
|---|
| 173 |     cout << "calcpk2[5] : computing 3D Fourier coefficients ... " << endl;
 | 
|---|
| 174 |     FFTWServer ffts;
 | 
|---|
| 175 |     TArray< complex<r_4> > four3d;
 | 
|---|
| 176 |     ffts.FFTForward(exlss, four3d);
 | 
|---|
| 177 |     tm.Split(" After FFTForward ");
 | 
|---|
| 178 | 
 | 
|---|
| 179 |     cout << "calcpk2[6] : computing power spectrum ... " << endl;
 | 
|---|
| 180 |     RandomGenerator rg;
 | 
|---|
| 181 |     Four3DPk pkc(four3d, rg);
 | 
|---|
| 182 |     double dkxmpc = DeuxPI/(double)exlss.SizeX()/XCellSizeMpc;
 | 
|---|
| 183 |     double dkympc = DeuxPI/(double)exlss.SizeY()/YCellSizeMpc;
 | 
|---|
| 184 |     double dkzmpc = DeuxPI/(double)exlss.SizeZ()/ZCellSizeMpc;
 | 
|---|
| 185 |     pkc.SetCellSize(dkxmpc, dkympc, dkzmpc);
 | 
|---|
| 186 |     
 | 
|---|
| 187 |     HProf hp = pkc.ComputePk(0.,256);
 | 
|---|
| 188 | 
 | 
|---|
| 189 |     tm.Split(" Done ComputePk ");    
 | 
|---|
| 190 |     
 | 
|---|
| 191 |     cout << "calcpk2[7.a] : writing profile P(k)  to  " << outname << endl;
 | 
|---|
| 192 |     POutPersist po(outname);
 | 
|---|
| 193 |     po << hp;
 | 
|---|
| 194 |     outname = "fgm_" + outname;
 | 
|---|
| 195 |     cout << "calcpk2[7.b] : writing foreground maps to  " << outname << endl;
 | 
|---|
| 196 |     POutPersist pom(outname);
 | 
|---|
| 197 |     pom << PPFNameTag("Tsync") << synctemp;
 | 
|---|
| 198 |     pom << PPFNameTag("async") << specidx;
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   }  // End of try bloc 
 | 
|---|
| 201 |   catch (PThrowable & exc) {  // catching SOPHYA exceptions
 | 
|---|
| 202 |     cerr << " calcpk2.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name() 
 | 
|---|
| 203 |          << "\n...exc.Msg= " << exc.Msg() << endl;
 | 
|---|
| 204 |     rc = 99;
 | 
|---|
| 205 |   }
 | 
|---|
| 206 |   catch (std::exception & e) {  // catching standard C++ exceptions
 | 
|---|
| 207 |     cerr << " calcpk2.cc: Catched std::exception "  << " - what()= " << e.what() << endl;
 | 
|---|
| 208 |     rc = 98;
 | 
|---|
| 209 |   }
 | 
|---|
| 210 |   catch (...) {  // catching other exceptions
 | 
|---|
| 211 |     cerr << " calcpk2.cc: some other exception (...) was caught ! " << endl;
 | 
|---|
| 212 |     rc = 97;
 | 
|---|
| 213 |   }
 | 
|---|
| 214 |   cout << " ==== End of calcpk2.cc program  Rc= " << rc << endl;
 | 
|---|
| 215 |   return rc;    
 | 
|---|
| 216 | }
 | 
|---|
| 217 | 
 | 
|---|
| 218 | 
 | 
|---|