| [3784] | 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 | 
 | 
|---|
| [3796] | 9 | Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPkFile 
 | 
|---|
 | 10 |                [PixNoiseLevel] [Diameter/Four2DRespTableFile] [TargetBeamArcmin] [NSigSrcThr]
 | 
|---|
| [3784] | 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"
 | 
|---|
| [3788] | 23 | #include "vector3d.h"
 | 
|---|
| [3784] | 24 | 
 | 
|---|
 | 25 | #include "qhist.h" 
 | 
|---|
| [3787] | 26 | #include "lobe.h" 
 | 
|---|
 | 27 | #include "cubedef.h" 
 | 
|---|
| [3788] | 28 | #include "fgndsub.h" 
 | 
|---|
 | 29 | #include "radutil.h" 
 | 
|---|
| [3787] | 30 | 
 | 
|---|
| [3784] | 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) {
 | 
|---|
| [3796] | 46 |     cout << " Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync  OutPkFile \n" 
 | 
|---|
 | 47 |          << "        [PixNoiseLevel] [Diameter/Four2DRespTableFile] [TargetBeamArcmin] [NSigSrcThr]" << endl;
 | 
|---|
| [3784] | 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];
 | 
|---|
| [3787] | 58 | 
 | 
|---|
 | 59 |     double pixsignoise = 0.;
 | 
|---|
 | 60 |     bool fgaddnoise=false;
 | 
|---|
 | 61 |     if (narg>6) {
 | 
|---|
 | 62 |       pixsignoise=atof(arg[6]);
 | 
|---|
| [3789] | 63 |       if (pixsignoise>1.e-6)  fgaddnoise=true;
 | 
|---|
| [3787] | 64 |     }
 | 
|---|
| [3788] | 65 |     
 | 
|---|
 | 66 |     bool fgcorrbeam=true;
 | 
|---|
| [3796] | 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 |     }
 | 
|---|
| [3792] | 81 |     double tbeamDoL=135;
 | 
|---|
| [3796] | 82 |     if (narg>8) {
 | 
|---|
 | 83 |       tbeamDoL=atof(arg[8]);
 | 
|---|
| [3792] | 84 |       if (tbeamDoL<1.)  fgcorrbeam=false;
 | 
|---|
| [3788] | 85 |     }
 | 
|---|
 | 86 |     bool fgclnsrc=true;
 | 
|---|
 | 87 |     double nsigsrc=5.;
 | 
|---|
| [3796] | 88 |     if (narg>9) {
 | 
|---|
 | 89 |       nsigsrc=atof(arg[9]);
 | 
|---|
| [3788] | 90 |       if (nsigsrc<1.e-6)  fgclnsrc=false;
 | 
|---|
 | 91 |     }
 | 
|---|
| [3787] | 92 | 
 | 
|---|
| [3788] | 93 |     TArray<r_4> maplss, mapsync;
 | 
|---|
 | 94 |     const char * tits[2]={"LSS", "Sync/RadioSrc"};
 | 
|---|
| [3784] | 95 |     for(int ks=0; ks<2; ks++)  {
 | 
|---|
 | 96 |       string& ppfname=inppflss;
 | 
|---|
 | 97 |       r_4 rfac=rfaclss;
 | 
|---|
| [3788] | 98 |       TArray<r_4>* inmap=&maplss; 
 | 
|---|
 | 99 |       if (ks==1) {  ppfname=inppfsync;  rfac=rfacsync;  inmap=&mapsync; }
 | 
|---|
| [3784] | 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 |     }
 | 
|---|
| [3787] | 111 | 
 | 
|---|
| [3788] | 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 | 
 | 
|---|
| [3787] | 122 |     if (fgaddnoise) { 
 | 
|---|
| [3788] | 123 |       cout << " calcpk2: adding noise to skycube cube ... " << endl;
 | 
|---|
 | 124 |       BeamEffect::AddNoise(skycube, pixsignoise);
 | 
|---|
| [3787] | 125 |     }
 | 
|---|
 | 126 | 
 | 
|---|
| [3784] | 127 |     double mean, sigma;
 | 
|---|
| [3788] | 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();
 | 
|---|
| [3796] | 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 | 
 | 
|---|
| [3792] | 146 |     double DoL = tbeamDoL;
 | 
|---|
 | 147 |     double tbeamarcmin = RadianToDegree(1.22/DoL)*60.;
 | 
|---|
| [3796] | 148 |     int typcb = 2;
 | 
|---|
 | 149 |     //    if (fgresptbl) typcb=22;
 | 
|---|
 | 150 |     Four2DResponse tbeam(typcb, DoL, DoL );
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |     ForegroundCleaner  cleaner(*arep_p, tbeam, skycube); 
 | 
|---|
| [3788] | 153 |     if (fgcorrbeam) {
 | 
|---|
| [3796] | 154 |       cout << "calcpk2[3.b] : calling cleaner.BeamCorrections() for target beam D/Lambda=" << DoL 
 | 
|---|
 | 155 |            << "  -> arcmin " << tbeamarcmin << " TypDishResp=" << typcb << endl;
 | 
|---|
| [3788] | 156 |       cleaner.BeamCorrections();
 | 
|---|
 | 157 |     }
 | 
|---|
| [3796] | 158 |     cout << " calcpk2[3.c] : calling cleaner.CleanNegatives() ... " << endl;
 | 
|---|
| [3789] | 159 |     cleaner.CleanNegatives();
 | 
|---|
| [3788] | 160 |     if (fgclnsrc) {
 | 
|---|
| [3796] | 161 |       cout << "calcpk2[3.d] : calling cleaner.CleanPointSources() with threshold NSigma=" << nsigsrc  << endl;
 | 
|---|
| [3788] | 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;      
 | 
|---|
| [3784] | 171 |     tm.Split(" After CleanForeground");
 | 
|---|
 | 172 | 
 | 
|---|
| [3788] | 173 |     cout << "calcpk2[5] : computing 3D Fourier coefficients ... " << endl;
 | 
|---|
| [3784] | 174 |     FFTWServer ffts;
 | 
|---|
 | 175 |     TArray< complex<r_4> > four3d;
 | 
|---|
| [3788] | 176 |     ffts.FFTForward(exlss, four3d);
 | 
|---|
| [3784] | 177 |     tm.Split(" After FFTForward ");
 | 
|---|
 | 178 | 
 | 
|---|
| [3788] | 179 |     cout << "calcpk2[6] : computing power spectrum ... " << endl;
 | 
|---|
| [3784] | 180 |     RandomGenerator rg;
 | 
|---|
 | 181 |     Four3DPk pkc(four3d, rg);
 | 
|---|
| [3789] | 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);
 | 
|---|
| [3784] | 186 |     
 | 
|---|
 | 187 |     HProf hp = pkc.ComputePk(0.,256);
 | 
|---|
 | 188 | 
 | 
|---|
 | 189 |     tm.Split(" Done ComputePk ");    
 | 
|---|
 | 190 |     
 | 
|---|
| [3788] | 191 |     cout << "calcpk2[7.a] : writing profile P(k)  to  " << outname << endl;
 | 
|---|
| [3784] | 192 |     POutPersist po(outname);
 | 
|---|
| [3788] | 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;
 | 
|---|
| [3784] | 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 |   }
 | 
|---|
| [3788] | 214 |   cout << " ==== End of calcpk2.cc program  Rc= " << rc << endl;
 | 
|---|
| [3784] | 215 |   return rc;    
 | 
|---|
 | 216 | }
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 | 
 | 
|---|