source: Sophya/trunk/Cosmo/RadioBeam/calcpk2.cc@ 3787

Last change on this file since 3787 was 3787, checked in by ansari, 15 years ago

Ajout classes / programmes de calcul d'effet de lobe sur les radio-sources, Reza 25/06/2010

File size: 6.3 KB
Line 
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
9Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync InMapRadioSource convFacRsc OutPk [PixNoiseLevel]
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"
24#include "lobe.h"
25#include "cubedef.h"
26
27#include "histinit.h"
28#include "fftwserver.h"
29#include "randr48.h"
30
31#include "ctimer.h"
32
33typedef ThSDR48RandGen RandomGenerator ;
34//--- Declaration des fonctions
35TArray<r_4> CleanForeground(TArray<r_4>& maplss, TArray<r_4>& mapsync, double freq0, double dfreq,
36 TArray<r_8>& synctemp, TArray<r_8>& specidx);
37
38//-------------------------------------------------------------------------
39// ------------------ MAIN PROGRAM ------------------------------
40//-------------------------------------------------------------------------
41/* --Fonction-- */
42int main(int narg, const char* arg[])
43{
44 if (narg<6) {
45 cout << " Usage: calcpk2 InMapLSS convFacLSS InMapSync convFacSync OutPk [PixNoiseLevel] " << endl;
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];
56
57 double pixsignoise = 0.;
58 bool fgaddnoise=false;
59 if (narg>6) {
60 pixsignoise=atof(arg[6]);
61 fgaddnoise=true;
62 }
63
64
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 }
83
84 if (fgaddnoise) {
85 cout << " calcpk2: adding noise to LSS input cube ... " << endl;
86 BeamEffect::AddNoise(inmaplss, pixsignoise);
87 }
88
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);
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-- */
140TArray<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 TracBrowser for help on using the repository browser.