Changeset 3788 in Sophya for trunk/Cosmo/RadioBeam/calcpk2.cc


Ignore:
Timestamp:
Jun 25, 2010, 7:35:17 PM (15 years ago)
Author:
ansari
Message:

Ajout classe de soustraction d'avant plans, Reza 25/06/2010

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cosmo/RadioBeam/calcpk2.cc

    r3787 r3788  
    77    R. Ansari , C. Magneville - Juin 2010
    88
    9 Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPk [PixNoiseLevel]
     9Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPk
     10               [PixNoiseLevel] [TargetBeamArcmin] [NSigSrcThr]
    1011---------------------------------------------------------------  */
    1112
     
    2021#include "specpk.h"
    2122#include "histats.h"
     23#include "vector3d.h"
    2224
    2325#include "qhist.h"
    2426#include "lobe.h"
    2527#include "cubedef.h"
     28#include "fgndsub.h"
     29#include "radutil.h"
    2630
    2731#include "histinit.h"
     
    3236
    3337typedef ThSDR48RandGen RandomGenerator ;
    34 //--- Declaration des fonctions
    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);
    3738
    3839//-------------------------------------------------------------------------
     
    4344{
    4445  if (narg<6) {
    45     cout << " Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync  OutPk [PixNoiseLevel] " << endl;
     46    cout << " Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync  OutPk \n"
     47         << "        [PixNoiseLevel] [TargetBeamArcmin] [NSigSrcThr]" << endl;
    4648    return 1;
    4749  }
     
    6163      fgaddnoise=true;
    6264    }
     65   
     66    bool fgcorrbeam=true;
     67    double tbeamarcmin=30.;
     68    if (narg>7) {
     69      tbeamarcmin=atof(arg[7]);
     70      if (tbeamarcmin<1.e-6)  fgcorrbeam=false;
     71    }
     72    bool fgclnsrc=true;
     73    double nsigsrc=5.;
     74    if (narg>8) {
     75      nsigsrc=atof(arg[8]);
     76      if (nsigsrc<1.e-6)  fgclnsrc=false;
     77    }
    6378
    64 
    65     TArray<r_4> inmaplss, inmapsync;
    66     const char * tits[2]={"LSS", "Sync"};
     79    TArray<r_4> maplss, mapsync;
     80    const char * tits[2]={"LSS", "Sync/RadioSrc"};
    6781    for(int ks=0; ks<2; ks++)  {
    6882      string& ppfname=inppflss;
    6983      r_4 rfac=rfaclss;
    70       TArray<r_4>* inmap=&inmaplss;
    71       if (ks==1) {  ppfname=inppfsync;  rfac=rfacsync;  inmap=&inmapsync; }
     84      TArray<r_4>* inmap=&maplss;
     85      if (ks==1) {  ppfname=inppfsync;  rfac=rfacsync;  inmap=&mapsync; }
    7286      cout << "calcpk2[" << ks+1 << "] : reading 3D map " << tits[ks] << " from file " << ppfname
    7387           << "  RenormFactor=" << rfac << endl;
     
    8296    }
    8397
     98    bool smo;
     99    if (!maplss.CompareSizes(mapsync,smo) ) {
     100      cout << " calcpk2/ERROR sizes " << endl;
     101      maplss.Show();  mapsync.Show();
     102      return 99;
     103    }
     104   
     105    TArray<r_4> skycube(mapsync);
     106    skycube += maplss;
     107
    84108    if (fgaddnoise) {
    85       cout << " calcpk2: adding noise to LSS input cube ... " << endl;
    86       BeamEffect::AddNoise(inmaplss, pixsignoise);
     109      cout << " calcpk2: adding noise to skycube cube ... " << endl;
     110      BeamEffect::AddNoise(skycube, pixsignoise);
    87111    }
    88112
    89     tm.Split(" After read ");
    90     TArray<r_8> synctemp, specidx;
    91     cout << "calcpk2[3] : calling CleanForeground(...) " << endl;
    92     double freq0 = Freq0MHz;
    93     double dfreq = FreqSizeMHz/(double)NFreq;
    94     TArray<r_4> inmap = CleanForeground(inmaplss, inmapsync, freq0, dfreq, synctemp, specidx);
    95113    double mean, sigma;
    96     MeanSigma(inmap, mean, sigma);
    97     cout << " After cleaning: Mean=" << mean << " Sigma=" << sigma << endl;     
     114    MeanSigma(skycube, mean, sigma);
     115    cout << " input sky cube : Mean=" << mean << " Sigma=" << sigma << endl;     
     116    tm.Split(" After input ");
     117
     118    H21Conversions conv;
     119    conv.setFrequency(Freq0MHz);
     120    double lambda = conv.getLambda();
     121    Four2DResponse arep(2, InterfArrayDiametre/lambda, InterfArrayDiametre/lambda);
     122    double DoL = 1.22/ArcminToRadian(tbeamarcmin);
     123    Four2DResponse tbeam(2, DoL, DoL );
     124    ForegroundCleaner  cleaner(arep, tbeam, skycube);
     125    if (fgcorrbeam) {
     126      cout << "calcpk2[3.a] : calling cleaner.BeamCorrections() for target beam (" << tbeamarcmin << " arcmin)"
     127           << " Diam/Lambda=" << DoL << endl;
     128      cleaner.BeamCorrections();
     129    }
     130    if (fgclnsrc) {
     131      cout << "calcpk2[3.b] : calling cleaner.CleanPointSources() with threshold NSigma=" << nsigsrc  << endl;
     132      cleaner.CleanPointSources(nsigsrc);
     133    }
     134
     135    cout << "calcpk2[4] : calling cleaner.extractLSSCube(...) " << endl;
     136    TArray<r_4> synctemp, specidx;
     137    TArray<r_4> exlss = cleaner.extractLSSCube(synctemp, specidx);
     138
     139    MeanSigma(exlss, mean, sigma);
     140    cout << " After cleaning/extractLSS: Mean=" << mean << " Sigma=" << sigma << endl;     
    98141    tm.Split(" After CleanForeground");
    99142
    100     cout << "calcpk2[4] : computing 3D Fourier coefficients ... " << endl;
     143    cout << "calcpk2[5] : computing 3D Fourier coefficients ... " << endl;
    101144    FFTWServer ffts;
    102145    TArray< complex<r_4> > four3d;
    103     ffts.FFTForward(inmap, four3d);
     146    ffts.FFTForward(exlss, four3d);
    104147    tm.Split(" After FFTForward ");
    105148
    106     cout << "calcpk2[5] : computing power spectrum ... " << endl;
     149    cout << "calcpk2[6] : computing power spectrum ... " << endl;
    107150    RandomGenerator rg;
    108151    Four3DPk pkc(four3d, rg);
     
    112155    tm.Split(" Done ComputePk ");   
    113156   
    114     cout << "calcpk2[4] : writing profile P(k) and foreground maps to  " << outname << endl;
     157    cout << "calcpk2[7.a] : writing profile P(k) to  " << outname << endl;
    115158    POutPersist po(outname);
    116     po << PPFNameTag("Pk") << hp;
    117     po << PPFNameTag("Tsync") << synctemp;
    118     po << PPFNameTag("async") << specidx;
     159    po << hp;
     160    outname = "fgm_" + outname;
     161    cout << "calcpk2[7.b] : writing foreground maps to  " << outname << endl;
     162    POutPersist pom(outname);
     163    pom << PPFNameTag("Tsync") << synctemp;
     164    pom << PPFNameTag("async") << specidx;
    119165
    120166  }  // End of try bloc
     
    132178    rc = 97;
    133179  }
    134   cout << " ==== End of calcpk.cc program  Rc= " << rc << endl;
     180  cout << " ==== End of calcpk2.cc program  Rc= " << rc << endl;
    135181  return rc;   
    136182}
    137183
    138184
    139 /* --Fonction-- */
    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
    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;
    158   // double freq0 : Frequence premier index en k (MHz)
    159   // double dfreq :   // largeur en frequence de chaque plan (Mhz) 
    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 }
Note: See TracChangeset for help on using the changeset viewer.