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

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

oubli ajout de calcpk2.cc Reza 15/06/2010

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