[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 |
|
---|
[3787] | 9 | Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPk [PixNoiseLevel]
|
---|
[3784] | 10 | --------------------------------------------------------------- */
|
---|
| 11 |
|
---|
| 12 | #include "machdefs.h"
|
---|
| 13 | #include "sopnamsp.h"
|
---|
| 14 | #include <iostream>
|
---|
| 15 | #include <string>
|
---|
| 16 | #include <math.h>
|
---|
| 17 |
|
---|
| 18 | #include <typeinfo>
|
---|
| 19 |
|
---|
| 20 | #include "specpk.h"
|
---|
| 21 | #include "histats.h"
|
---|
| 22 |
|
---|
| 23 | #include "qhist.h"
|
---|
[3787] | 24 | #include "lobe.h"
|
---|
| 25 | #include "cubedef.h"
|
---|
| 26 |
|
---|
[3784] | 27 | #include "histinit.h"
|
---|
| 28 | #include "fftwserver.h"
|
---|
| 29 | #include "randr48.h"
|
---|
| 30 |
|
---|
| 31 | #include "ctimer.h"
|
---|
| 32 |
|
---|
| 33 | typedef ThSDR48RandGen RandomGenerator ;
|
---|
| 34 | //--- Declaration des fonctions
|
---|
[3787] | 35 | TArray<r_4> CleanForeground(TArray<r_4>& maplss, TArray<r_4>& mapsync, double freq0, double dfreq,
|
---|
| 36 | TArray<r_8>& synctemp, TArray<r_8>& specidx);
|
---|
[3784] | 37 |
|
---|
| 38 | //-------------------------------------------------------------------------
|
---|
| 39 | // ------------------ MAIN PROGRAM ------------------------------
|
---|
| 40 | //-------------------------------------------------------------------------
|
---|
| 41 | /* --Fonction-- */
|
---|
| 42 | int main(int narg, const char* arg[])
|
---|
| 43 | {
|
---|
| 44 | if (narg<6) {
|
---|
[3787] | 45 | cout << " Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync OutPk [PixNoiseLevel] " << endl;
|
---|
[3784] | 46 | return 1;
|
---|
| 47 | }
|
---|
| 48 | Timer tm("calcpk2");
|
---|
| 49 | int rc = 0;
|
---|
| 50 | try {
|
---|
| 51 | string inppflss = arg[1];
|
---|
| 52 | r_4 rfaclss = atof(arg[2]);
|
---|
| 53 | string inppfsync = arg[3];
|
---|
| 54 | r_4 rfacsync = atof(arg[4]);
|
---|
| 55 | string outname = arg[5];
|
---|
[3787] | 56 |
|
---|
| 57 | double pixsignoise = 0.;
|
---|
| 58 | bool fgaddnoise=false;
|
---|
| 59 | if (narg>6) {
|
---|
| 60 | pixsignoise=atof(arg[6]);
|
---|
| 61 | fgaddnoise=true;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
[3784] | 65 | TArray<r_4> inmaplss, inmapsync;
|
---|
| 66 | const char * tits[2]={"LSS", "Sync"};
|
---|
| 67 | for(int ks=0; ks<2; ks++) {
|
---|
| 68 | string& ppfname=inppflss;
|
---|
| 69 | r_4 rfac=rfaclss;
|
---|
| 70 | TArray<r_4>* inmap=&inmaplss;
|
---|
| 71 | if (ks==1) { ppfname=inppfsync; rfac=rfacsync; inmap=&inmapsync; }
|
---|
| 72 | cout << "calcpk2[" << ks+1 << "] : reading 3D map " << tits[ks] << " from file " << ppfname
|
---|
| 73 | << " RenormFactor=" << rfac << endl;
|
---|
| 74 | PInPersist pin(ppfname);
|
---|
| 75 | pin >> (*inmap);
|
---|
| 76 | (*inmap) *= rfac;
|
---|
| 77 | double mean, sigma;
|
---|
| 78 | MeanSigma(*inmap, mean, sigma);
|
---|
| 79 | cout << " ...InMap sizes " << inmap->InfoString() << endl;
|
---|
| 80 | inmap->Show();
|
---|
| 81 | cout << " ... Mean=" << mean << " Sigma=" << sigma << endl;
|
---|
| 82 | }
|
---|
[3787] | 83 |
|
---|
| 84 | if (fgaddnoise) {
|
---|
| 85 | cout << " calcpk2: adding noise to LSS input cube ... " << endl;
|
---|
| 86 | BeamEffect::AddNoise(inmaplss, pixsignoise);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[3784] | 89 | tm.Split(" After read ");
|
---|
| 90 | TArray<r_8> synctemp, specidx;
|
---|
| 91 | cout << "calcpk2[3] : calling CleanForeground(...) " << endl;
|
---|
[3787] | 92 | double freq0 = Freq0MHz;
|
---|
| 93 | double dfreq = FreqSizeMHz/(double)NFreq;
|
---|
| 94 | TArray<r_4> inmap = CleanForeground(inmaplss, inmapsync, freq0, dfreq, synctemp, specidx);
|
---|
[3784] | 95 | double mean, sigma;
|
---|
| 96 | MeanSigma(inmap, mean, sigma);
|
---|
| 97 | cout << " After cleaning: Mean=" << mean << " Sigma=" << sigma << endl;
|
---|
| 98 | tm.Split(" After CleanForeground");
|
---|
| 99 |
|
---|
| 100 | cout << "calcpk2[4] : computing 3D Fourier coefficients ... " << endl;
|
---|
| 101 | FFTWServer ffts;
|
---|
| 102 | TArray< complex<r_4> > four3d;
|
---|
| 103 | ffts.FFTForward(inmap, four3d);
|
---|
| 104 | tm.Split(" After FFTForward ");
|
---|
| 105 |
|
---|
| 106 | cout << "calcpk2[5] : computing power spectrum ... " << endl;
|
---|
| 107 | RandomGenerator rg;
|
---|
| 108 | Four3DPk pkc(four3d, rg);
|
---|
| 109 |
|
---|
| 110 | HProf hp = pkc.ComputePk(0.,256);
|
---|
| 111 |
|
---|
| 112 | tm.Split(" Done ComputePk ");
|
---|
| 113 |
|
---|
| 114 | cout << "calcpk2[4] : writing profile P(k) and foreground maps to " << outname << endl;
|
---|
| 115 | POutPersist po(outname);
|
---|
| 116 | po << PPFNameTag("Pk") << hp;
|
---|
| 117 | po << PPFNameTag("Tsync") << synctemp;
|
---|
| 118 | po << PPFNameTag("async") << specidx;
|
---|
| 119 |
|
---|
| 120 | } // End of try bloc
|
---|
| 121 | catch (PThrowable & exc) { // catching SOPHYA exceptions
|
---|
| 122 | cerr << " calcpk2.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name()
|
---|
| 123 | << "\n...exc.Msg= " << exc.Msg() << endl;
|
---|
| 124 | rc = 99;
|
---|
| 125 | }
|
---|
| 126 | catch (std::exception & e) { // catching standard C++ exceptions
|
---|
| 127 | cerr << " calcpk2.cc: Catched std::exception " << " - what()= " << e.what() << endl;
|
---|
| 128 | rc = 98;
|
---|
| 129 | }
|
---|
| 130 | catch (...) { // catching other exceptions
|
---|
| 131 | cerr << " calcpk2.cc: some other exception (...) was caught ! " << endl;
|
---|
| 132 | rc = 97;
|
---|
| 133 | }
|
---|
| 134 | cout << " ==== End of calcpk.cc program Rc= " << rc << endl;
|
---|
| 135 | return rc;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | /* --Fonction-- */
|
---|
[3787] | 140 | TArray<r_4> CleanForeground(TArray<r_4>& maplss, TArray<r_4>& mapsync, double freq0, double dfreq,
|
---|
| 141 | TArray<r_8>& synctemp, TArray<r_8>& specidx)
|
---|
| 142 | // Inputs : maplss, mapsyc, freq0, dfreq
|
---|
| 143 | // Outputs : synctemp, specidx (reconstructed foreground temperature and spectral index
|
---|
| 144 | // Return_Array : foreground subtracted LSS signal
|
---|
[3784] | 145 | {
|
---|
| 146 | bool smo;
|
---|
| 147 | if (!maplss.CompareSizes(mapsync,smo) ) {
|
---|
| 148 | cout << " CleanForeground/ERROR sizes " << endl;
|
---|
| 149 | maplss.Show(); mapsync.Show();
|
---|
| 150 | throw ParmError("CleanForeground- maplss , mapsync not the same size !");
|
---|
| 151 | }
|
---|
| 152 | sa_size_t sz[5]; sz[0]=maplss.SizeX(); sz[1]=maplss.SizeY();
|
---|
| 153 | synctemp.SetSize(2, sz);
|
---|
| 154 | specidx.SetSize(2, sz);
|
---|
| 155 | TArray<r_4>& omap=maplss;
|
---|
| 156 | Vector vlnf(maplss.SizeZ());
|
---|
| 157 | int nprt = 0;
|
---|
[3787] | 158 | // double freq0 : Frequence premier index en k (MHz)
|
---|
| 159 | // double dfreq : // largeur en frequence de chaque plan (Mhz)
|
---|
[3784] | 160 | for(sa_size_t i=0; i<maplss.SizeX(); i++)
|
---|
| 161 | for(sa_size_t j=0; j<maplss.SizeY(); j++) {
|
---|
| 162 | r_8 s1, sx, sx2, sy, sxy;
|
---|
| 163 | s1=sx=sx2=sy=sxy=0.;
|
---|
| 164 | for(sa_size_t k=0; k<maplss.SizeZ(); k++) {
|
---|
| 165 | double lnf=log((double)k*dfreq+freq0);
|
---|
| 166 | vlnf(k)=lnf;
|
---|
| 167 | double ttot=(r_8)(mapsync(i,j,k)+maplss(i,j,k));
|
---|
| 168 | if (ttot < 1.e-5) continue;
|
---|
| 169 | double lntt=log(ttot);
|
---|
| 170 | s1+=1.; sx+=lnf; sx2+=(lnf*lnf);
|
---|
| 171 | sy+=lntt; sxy+=(lnf*lntt);
|
---|
| 172 | }
|
---|
| 173 | double beta = (sx*sxy-sx2*sy)/(sx*sx-s1*sx2);
|
---|
| 174 | double alpha = (s1*sxy-sx*sy)/(s1*sx2-sx*sx);
|
---|
| 175 | double T0 = exp(beta+alpha*vlnf(0));
|
---|
| 176 | if ((i%6==0)&&(j%7==0))
|
---|
| 177 | cout << "CleanForeground[" << i << "," << j << "]: T0=" << T0 << " alpha=" << alpha
|
---|
| 178 | << " (mapsync=" << mapsync(i,j,0) << " ... " << mapsync(i,j,125) << ")" << endl;
|
---|
| 179 | synctemp(i,j) = T0;
|
---|
| 180 | specidx(i,j) = alpha;
|
---|
| 181 | for(sa_size_t k=0; k<maplss.SizeZ(); k++) {
|
---|
| 182 | r_4 fittedtemp = (r_4)(exp(beta+alpha*vlnf(k)));
|
---|
| 183 | omap(i,j,k) = mapsync(i,j,k)+maplss(i,j,k)-fittedtemp;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | return omap;
|
---|
| 187 | }
|
---|