| [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 | 
 | 
|---|
| [3986] | 9 | Usage: calcpk2 [-t -g -mxr val] InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPkFile 
 | 
|---|
| [3796] | 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 | {
 | 
|---|
| [3830] | 45 |   if ( (narg<6)||((narg>1)&&(strcmp(arg[1],"-h")==0)) ) {
 | 
|---|
| [4022] | 46 |     cout << " Usage: [-t -g -mxr val -fmt T0,alpha] calcpk2 InMapLSS convFacLSS InMapFgnd convFacFgnd OutPkFile \n" 
 | 
|---|
| [3973] | 47 |          << "        [PixNoiseLevel] [D_Dish/Four2DRespTableFile CorBeamDiam] \n"
 | 
|---|
| [3830] | 48 |          << "        [NSigSrcThr] [P2/P1] [RecMapFile] " << endl;
 | 
|---|
 | 49 |     if ((narg>1)&&(strcmp(arg[1],"-h")==0)) {
 | 
|---|
| [3973] | 50 |       cout << "-t -g : Triangular / gaussian beam shape (def=gaussian) \n" 
 | 
|---|
| [4022] | 51 |            << "-fmt T0,alpha: fix mean slice temp. according to power law T0,alpha \n "
 | 
|---|
| [3986] | 52 |            << "-mxr val: Max beam correction factor (default=10.) \n "
 | 
|---|
| [3973] | 53 |            << "- InMapLSS: Input 3D LSS cube (PPF file name) \n "
 | 
|---|
| [3830] | 54 |            << "- convFacLSS: LSS cube conversion factor to mK (milliKelvin) \n"
 | 
|---|
 | 55 |            << "- InMapFgnd: Input 3D foreground cube (PPF file name) \n"
 | 
|---|
 | 56 |            << "- convFacFgnd: Foreground cube conversion factor to mK (milliKelvin) \n"
 | 
|---|
 | 57 |            << "- PixNoiseLevel: White noise level per pixel (mK) (default=0.) \n" 
 | 
|---|
 | 58 |            << "- D_Dish/Four2DRespTableFile: Dish diameter or 2D (u,v) plane response (PPF file name) \n"
 | 
|---|
| [3973] | 59 |            << "- CorBeamDiam: Beam correction target dish diameter  \n" 
 | 
|---|
| [3830] | 60 |            << "    These two parameters are used to correct for beam effect for a \n"
 | 
|---|
 | 61 |            << "    target beam (independent of frequency) defined by D/Lambda \n"
 | 
|---|
| [3973] | 62 |            << "    DoL = 100 --> beam ~ 35 arcmin (D=30m @ z~0.5 Lambda~30cm) \n" 
 | 
|---|
| [3830] | 63 |            << "    default : no beam correction applied \n " 
 | 
|---|
 | 64 |            << " - NSigSrcThr: Point source cleaning, Nb_Sigmas on stacked 2D temperature \n" 
 | 
|---|
| [3973] | 65 |            << "    default (0.) : no point source cleaning, use NSigSrcThr ~ 3..5 \n"
 | 
|---|
| [3830] | 66 |            << " - P2/P1: 2nd/first degree polynomial fit on ln(Temp) = f(ln(freq)) \n "
 | 
|---|
 | 67 |            << "    foreground subtraction. default is P2 \n" 
 | 
|---|
 | 68 |            << "- RecMapFile: output PPF file for reconstructed foreground template \n" 
 | 
|---|
 | 69 |            << "    (Temperature,SpectralIndex) and extracted LSS cube \n"
 | 
|---|
 | 70 |          << endl;
 | 
|---|
 | 71 |       return 1;
 | 
|---|
 | 72 |     }
 | 
|---|
 | 73 |     else cout << "   calcpk2 -h for detailed usage " << endl;
 | 
|---|
 | 74 |     return 2;
 | 
|---|
| [3784] | 75 |   }
 | 
|---|
 | 76 |   Timer tm("calcpk2");
 | 
|---|
 | 77 |   int rc = 0;
 | 
|---|
 | 78 |   try {
 | 
|---|
| [3973] | 79 |     bool fggaussian=true;  // true -> gaussian beam 
 | 
|---|
| [3989] | 80 |     double maxratio=10.;   // valeur max du rapport des lobes lors de la correction de lobe
 | 
|---|
 | 81 | 
 | 
|---|
| [4022] | 82 |     bool fgfix_mXYtemp=false;
 | 
|---|
 | 83 |     double T0_pl=1.; 
 | 
|---|
 | 84 |     double alpha_pl=0.;
 | 
|---|
| [3973] | 85 |     // decodage argument optionnel 
 | 
|---|
 | 86 |     bool fgoptarg=true;
 | 
|---|
 | 87 |     while (fgoptarg) {
 | 
|---|
 | 88 |       string fbo = arg[1];
 | 
|---|
 | 89 |       if (fbo=="-t")  { fggaussian=false; arg++; narg--; }
 | 
|---|
 | 90 |       else if (fbo=="-g")  { fggaussian=true; arg++; narg--; }
 | 
|---|
| [3986] | 91 |       else if (fbo=="-mxr")  { arg++; maxratio=atof(arg[1]); arg++; narg-=2; }
 | 
|---|
| [4022] | 92 |       else if (fbo=="-fmt")  
 | 
|---|
 | 93 |         { fgfix_mXYtemp=true; arg++; sscanf(arg[1],"%lg,%lg",&T0_pl,&alpha_pl); arg++; narg-=2; }
 | 
|---|
| [3973] | 94 |       else fgoptarg=false;
 | 
|---|
 | 95 |     }
 | 
|---|
 | 96 |     if (narg < 6) {
 | 
|---|
 | 97 |       cout << " calcpk2/error arguments , applobe -h for help " << endl;
 | 
|---|
 | 98 |       return 2;
 | 
|---|
 | 99 |     }
 | 
|---|
 | 100 | 
 | 
|---|
| [3784] | 101 |     string inppflss = arg[1];
 | 
|---|
 | 102 |     r_4 rfaclss = atof(arg[2]);
 | 
|---|
 | 103 |     string inppfsync = arg[3];
 | 
|---|
 | 104 |     r_4 rfacsync = atof(arg[4]);
 | 
|---|
 | 105 |     string outname = arg[5];
 | 
|---|
| [3787] | 106 | 
 | 
|---|
 | 107 |     double pixsignoise = 0.;
 | 
|---|
 | 108 |     bool fgaddnoise=false;
 | 
|---|
 | 109 |     if (narg>6) {
 | 
|---|
 | 110 |       pixsignoise=atof(arg[6]);
 | 
|---|
| [3789] | 111 |       if (pixsignoise>1.e-6)  fgaddnoise=true;
 | 
|---|
| [3787] | 112 |     }
 | 
|---|
| [3788] | 113 |     
 | 
|---|
 | 114 |     bool fgcorrbeam=true;
 | 
|---|
| [3796] | 115 | 
 | 
|---|
 | 116 |     bool fgresptbl=false;
 | 
|---|
 | 117 |     double DIAMETRE=100.;
 | 
|---|
 | 118 |     string resptblname;
 | 
|---|
 | 119 |     if (narg>7) {
 | 
|---|
 | 120 |       if (isdigit(*arg[7])) {
 | 
|---|
 | 121 |         fgresptbl=false;
 | 
|---|
 | 122 |         DIAMETRE=atof(arg[7]);
 | 
|---|
 | 123 |       }
 | 
|---|
 | 124 |       else { 
 | 
|---|
 | 125 |         resptblname=arg[7];
 | 
|---|
 | 126 |         fgresptbl=true;
 | 
|---|
 | 127 |       }
 | 
|---|
 | 128 |     }
 | 
|---|
| [3973] | 129 |     double tbeamDiam=0.;
 | 
|---|
| [3796] | 130 |     if (narg>8) {
 | 
|---|
| [3973] | 131 |       tbeamDiam=atof(arg[8]);
 | 
|---|
 | 132 |       if (tbeamDiam<1.)  fgcorrbeam=false;
 | 
|---|
| [3788] | 133 |     }
 | 
|---|
 | 134 |     bool fgclnsrc=true;
 | 
|---|
 | 135 |     double nsigsrc=5.;
 | 
|---|
| [3796] | 136 |     if (narg>9) {
 | 
|---|
 | 137 |       nsigsrc=atof(arg[9]);
 | 
|---|
| [3788] | 138 |       if (nsigsrc<1.e-6)  fgclnsrc=false;
 | 
|---|
 | 139 |     }
 | 
|---|
| [3830] | 140 |     bool fgpoly2=true;  // true -> soustraction polynome degre 2
 | 
|---|
 | 141 |     if ((narg>0)&&(strcmp(arg[10],"P1")==0)) fgpoly2=false;      
 | 
|---|
 | 142 |     bool fgsavemaps=false;
 | 
|---|
 | 143 |     string outmap_ppfname="extlss.ppf";
 | 
|---|
 | 144 |     if (narg>11) {
 | 
|---|
 | 145 |       outmap_ppfname=arg[11];
 | 
|---|
 | 146 |       fgsavemaps=true;
 | 
|---|
 | 147 |     }
 | 
|---|
 | 148 |     
 | 
|---|
| [3788] | 149 |     TArray<r_4> maplss, mapsync;
 | 
|---|
 | 150 |     const char * tits[2]={"LSS", "Sync/RadioSrc"};
 | 
|---|
| [3784] | 151 |     for(int ks=0; ks<2; ks++)  {
 | 
|---|
 | 152 |       string& ppfname=inppflss;
 | 
|---|
 | 153 |       r_4 rfac=rfaclss;
 | 
|---|
| [3788] | 154 |       TArray<r_4>* inmap=&maplss; 
 | 
|---|
 | 155 |       if (ks==1) {  ppfname=inppfsync;  rfac=rfacsync;  inmap=&mapsync; }
 | 
|---|
| [3784] | 156 |       cout << "calcpk2[" << ks+1 << "] : reading 3D map " << tits[ks] << " from file " << ppfname 
 | 
|---|
 | 157 |            << "  RenormFactor=" << rfac << endl;
 | 
|---|
 | 158 |       PInPersist pin(ppfname);
 | 
|---|
 | 159 |       pin >> (*inmap);
 | 
|---|
 | 160 |       (*inmap) *= rfac;
 | 
|---|
 | 161 |       double mean, sigma;
 | 
|---|
 | 162 |       MeanSigma(*inmap, mean, sigma);
 | 
|---|
 | 163 |       cout << " ...InMap sizes " << inmap->InfoString() << endl;
 | 
|---|
 | 164 |       inmap->Show(); 
 | 
|---|
 | 165 |       cout << " ... Mean=" << mean << " Sigma=" << sigma << endl;
 | 
|---|
 | 166 |     }
 | 
|---|
| [3787] | 167 | 
 | 
|---|
| [3788] | 168 |     bool smo; 
 | 
|---|
 | 169 |     if (!maplss.CompareSizes(mapsync,smo) ) {
 | 
|---|
 | 170 |       cout << " calcpk2/ERROR sizes " << endl;
 | 
|---|
 | 171 |       maplss.Show();  mapsync.Show(); 
 | 
|---|
 | 172 |       return 99;
 | 
|---|
 | 173 |     }
 | 
|---|
 | 174 |     
 | 
|---|
 | 175 |     TArray<r_4> skycube(mapsync);
 | 
|---|
 | 176 |     skycube += maplss;
 | 
|---|
 | 177 | 
 | 
|---|
| [3787] | 178 |     if (fgaddnoise) { 
 | 
|---|
| [3788] | 179 |       cout << " calcpk2: adding noise to skycube cube ... " << endl;
 | 
|---|
 | 180 |       BeamEffect::AddNoise(skycube, pixsignoise);
 | 
|---|
| [3787] | 181 |     }
 | 
|---|
 | 182 | 
 | 
|---|
| [3784] | 183 |     double mean, sigma;
 | 
|---|
| [3788] | 184 |     MeanSigma(skycube, mean, sigma);
 | 
|---|
 | 185 |     cout << " input sky cube : Mean=" << mean << " Sigma=" << sigma << endl;      
 | 
|---|
 | 186 |     tm.Split(" After input ");
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 |     H21Conversions conv;
 | 
|---|
 | 189 |     conv.setFrequency(Freq0MHz);
 | 
|---|
 | 190 |     double lambda = conv.getLambda();
 | 
|---|
| [3796] | 191 |     Four2DResponse arep(2, DIAMETRE/lambda, DIAMETRE/lambda, lambda);
 | 
|---|
 | 192 |     Four2DResponse* arep_p=&arep;
 | 
|---|
 | 193 |     Four2DRespTable resptbl;
 | 
|---|
 | 194 |     if (fgresptbl) {
 | 
|---|
 | 195 |       cout << "calcpk2[3.a]: initializing Four2DRespTable from file" << resptblname << endl;
 | 
|---|
 | 196 |       resptbl.readFromPPF(resptblname);
 | 
|---|
| [3973] | 197 |       resptbl.renormalize(1.);
 | 
|---|
| [3796] | 198 |       arep_p=&resptbl;
 | 
|---|
 | 199 |     }
 | 
|---|
 | 200 |     else cout << " calcpk2[3.a]: Four2DResponse ( Diameter=" << DIAMETRE << " Lambda= " << lambda
 | 
|---|
 | 201 |               << " DoL=" << DIAMETRE/lambda << " ) " << endl;
 | 
|---|
 | 202 | 
 | 
|---|
| [3973] | 203 |     double DoL = tbeamDiam/lambda;
 | 
|---|
| [3792] | 204 |     double tbeamarcmin = RadianToDegree(1.22/DoL)*60.;
 | 
|---|
| [3973] | 205 |     int typcb = (fggaussian)?1:2;
 | 
|---|
| [3796] | 206 |     //    if (fgresptbl) typcb=22;
 | 
|---|
 | 207 |     Four2DResponse tbeam(typcb, DoL, DoL );
 | 
|---|
 | 208 | 
 | 
|---|
| [3986] | 209 |     ForegroundCleaner  cleaner(*arep_p, tbeam, skycube, maxratio); 
 | 
|---|
| [4022] | 210 |     if (fgfix_mXYtemp) {
 | 
|---|
 | 211 |       cout << "calcpk2[3.b] : calling cleaner.FixMeanXYTemp(T0=" << T0_pl << ",alpha=" << alpha_pl << ")" << endl;
 | 
|---|
 | 212 |       cleaner.FixMeanXYTemp(T0_pl,alpha_pl);
 | 
|---|
 | 213 |     }
 | 
|---|
| [3788] | 214 |     if (fgcorrbeam) {
 | 
|---|
| [4022] | 215 |       cout << "calcpk2[3.c] : calling cleaner.BeamCorrections() for target beam Diameter=" << tbeamDiam  
 | 
|---|
| [3973] | 216 |            << " D/Lambda=" << DoL  << "  -> arcmin " << tbeamarcmin << " TypDishResp=" << typcb << endl;
 | 
|---|
| [3788] | 217 |       cleaner.BeamCorrections();
 | 
|---|
 | 218 |     }
 | 
|---|
| [4022] | 219 |     cout << " calcpk2[3.d] : calling cleaner.CleanNegatives() ... " << endl;
 | 
|---|
| [3789] | 220 |     cleaner.CleanNegatives();
 | 
|---|
| [3788] | 221 |     if (fgclnsrc) {
 | 
|---|
| [4022] | 222 |       cout << "calcpk2[3.e] : calling cleaner.CleanPointSources() with threshold NSigma=" << nsigsrc  << endl;
 | 
|---|
| [3788] | 223 |       cleaner.CleanPointSources(nsigsrc);
 | 
|---|
 | 224 |     }
 | 
|---|
 | 225 | 
 | 
|---|
 | 226 |     cout << "calcpk2[4] : calling cleaner.extractLSSCube(...) " << endl;
 | 
|---|
 | 227 |     TArray<r_4> synctemp, specidx; 
 | 
|---|
| [3830] | 228 |     TArray<r_4> exlss; 
 | 
|---|
 | 229 |     if (fgpoly2) exlss = cleaner.extractLSSCubeP2(synctemp, specidx);
 | 
|---|
 | 230 |     else  exlss = cleaner.extractLSSCubeP1(synctemp, specidx);
 | 
|---|
| [3788] | 231 | 
 | 
|---|
 | 232 |     MeanSigma(exlss, mean, sigma);
 | 
|---|
 | 233 |     cout << " After cleaning/extractLSS: Mean=" << mean << " Sigma=" << sigma << endl;      
 | 
|---|
| [3784] | 234 |     tm.Split(" After CleanForeground");
 | 
|---|
 | 235 | 
 | 
|---|
| [3788] | 236 |     cout << "calcpk2[5] : computing 3D Fourier coefficients ... " << endl;
 | 
|---|
| [3784] | 237 |     FFTWServer ffts;
 | 
|---|
 | 238 |     TArray< complex<r_4> > four3d;
 | 
|---|
| [3788] | 239 |     ffts.FFTForward(exlss, four3d);
 | 
|---|
| [3784] | 240 |     tm.Split(" After FFTForward ");
 | 
|---|
 | 241 | 
 | 
|---|
| [3788] | 242 |     cout << "calcpk2[6] : computing power spectrum ... " << endl;
 | 
|---|
| [3784] | 243 |     RandomGenerator rg;
 | 
|---|
 | 244 |     Four3DPk pkc(four3d, rg);
 | 
|---|
| [3789] | 245 |     double dkxmpc = DeuxPI/(double)exlss.SizeX()/XCellSizeMpc;
 | 
|---|
 | 246 |     double dkympc = DeuxPI/(double)exlss.SizeY()/YCellSizeMpc;
 | 
|---|
 | 247 |     double dkzmpc = DeuxPI/(double)exlss.SizeZ()/ZCellSizeMpc;
 | 
|---|
 | 248 |     pkc.SetCellSize(dkxmpc, dkympc, dkzmpc);
 | 
|---|
| [3784] | 249 |     
 | 
|---|
| [3973] | 250 |     HProf hp = pkc.ComputePk(0.,HPk_NBin);
 | 
|---|
| [3784] | 251 | 
 | 
|---|
 | 252 |     tm.Split(" Done ComputePk ");    
 | 
|---|
| [3830] | 253 |     { 
 | 
|---|
| [3788] | 254 |     cout << "calcpk2[7.a] : writing profile P(k)  to  " << outname << endl;
 | 
|---|
| [3784] | 255 |     POutPersist po(outname);
 | 
|---|
| [3788] | 256 |     po << hp;
 | 
|---|
| [3830] | 257 |     }
 | 
|---|
 | 258 |     if (fgsavemaps) {
 | 
|---|
 | 259 |       cout << "calcpk2[7.b] : writing foreground maps and extracted LSS to  " << outmap_ppfname << endl;
 | 
|---|
 | 260 |       POutPersist pom(outmap_ppfname);
 | 
|---|
 | 261 |       pom << PPFNameTag("Tsync") << synctemp;
 | 
|---|
 | 262 |       pom << PPFNameTag("async") << specidx;
 | 
|---|
 | 263 |       pom << PPFNameTag("extlss") << exlss;
 | 
|---|
 | 264 |     }
 | 
|---|
| [3784] | 265 | 
 | 
|---|
 | 266 |   }  // End of try bloc 
 | 
|---|
 | 267 |   catch (PThrowable & exc) {  // catching SOPHYA exceptions
 | 
|---|
 | 268 |     cerr << " calcpk2.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name() 
 | 
|---|
 | 269 |          << "\n...exc.Msg= " << exc.Msg() << endl;
 | 
|---|
 | 270 |     rc = 99;
 | 
|---|
 | 271 |   }
 | 
|---|
 | 272 |   catch (std::exception & e) {  // catching standard C++ exceptions
 | 
|---|
 | 273 |     cerr << " calcpk2.cc: Catched std::exception "  << " - what()= " << e.what() << endl;
 | 
|---|
 | 274 |     rc = 98;
 | 
|---|
 | 275 |   }
 | 
|---|
 | 276 |   catch (...) {  // catching other exceptions
 | 
|---|
 | 277 |     cerr << " calcpk2.cc: some other exception (...) was caught ! " << endl;
 | 
|---|
 | 278 |     rc = 97;
 | 
|---|
 | 279 |   }
 | 
|---|
| [3788] | 280 |   cout << " ==== End of calcpk2.cc program  Rc= " << rc << endl;
 | 
|---|
| [3784] | 281 |   return rc;    
 | 
|---|
 | 282 | }
 | 
|---|
 | 283 | 
 | 
|---|
 | 284 | 
 | 
|---|