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

Last change on this file since 4011 was 3989, checked in by ansari, 14 years ago

Propagation modif MaxRatio dds rapports deux lobes ds applobe.cc, Reza 06/05/2011

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