source: Sophya/trunk/SophyaProg/PMixer/extractRS.cc@ 4061

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

correction suggere par christophe, Reza 07/09/2010

File size: 12.8 KB
Line 
1#include "sopnamsp.h"
2#include "pmixer.h"
3
4/*! \ingroup PMixer
5 * \file extractRS.cc
6 * \brief \b PROGRAM \b extractRS <BR>
7 * Program to create a map (SphereHEALPix) with point source
8 * from small map (matrix) and convolving their radiation spectrum
9 * RadSpectra
10 * with a given filter response SpectralResponse
11 */
12
13// -----------------------------------------------------------------
14// ------------- Function declaration ------------------------------
15int CheckCards(DataCards & dc, string & msg);
16char * BuildFITSFileName(string const & fname);
17SpectralResponse * getSpectralResponse(DataCards & dc);
18float ComputeFrequency(DataCards &,string&);
19
20// -----------------------------------------------------------------
21
22// ----- Global (static) variables ------------
23static bool rdmap = false; // true -> Read map first
24static char mapPath[256]; // Path for input maps
25static int hp_nside = 32; // HealPix NSide
26static int nskycomp = 0; // Number of sky components
27static int debuglev = 0; // Debug Level
28static int printlev = 0; // Print Level
29static POutPersist * so = NULL; // Debug PPFOut file
30
31// -------------------------------------------------------------------------
32// main program
33// -------------------------------------------------------------------------
34int main(int narg, char * arg[])
35{
36 if ((narg < 3) || ((narg > 1) && (strcmp(arg[1], "-h") == 0) )) {
37 cout << " Usage: extractRS parameterFile outputfitsnameformaps [outppfname]" << endl;
38 exit(0);
39 }
40
41 InitTim();
42
43 string msg;
44 int rc = 0;
45 //RadSpectra * es = NULL;
46 //SpectralResponse * sr = NULL;
47 double moy, sig;
48
49 DataCards dc;
50 so = NULL;
51
52 try {
53 string dcard = arg[1];
54 cout << " Decoding parameters from file " << dcard << endl;
55 dc.ReadFile(dcard);
56
57 rc = CheckCards(dc, msg);
58 if (rc) {
59 cerr << " Error condition -> Rc= " << rc << endl;
60 cerr << " Msg= " << msg << endl;
61 return(rc);
62 }
63 }
64 catch (PException exc) {
65 msg = exc.Msg();
66 cerr << " !!!! extractRS/ Readcard - Catched exception - Msg= " << exc.Msg() << endl;
67 return(90);
68 }
69
70
71 cout << " extractRS/Info : NComp = " << nskycomp << " SphereHEALPix_NSide= " << hp_nside << endl;
72 cout << " ... MapPath = " << (string)mapPath << " DebugLev= " << debuglev
73 << " PrintLev= " << printlev << endl;
74
75
76 // We create an output persist file for writing debug objects
77 if (debuglev > 0) so = new POutPersist("extrRSdbg.ppf");
78 SphereHEALPix<float> SourceMap(hp_nside);
79 SphereHEALPix<float> OutputMap(hp_nside);
80 bool okout = true;
81
82 try {
83
84 // FitsIoServer fios; // Our FITS IO Server
85 char * flnm, buff[90];
86 string key;
87 string key2;
88
89 double K = 1.;
90
91 // Loop over sky frequencies components
92 int maxOfSkyComp = nskycomp;
93 int nbOfPix = 0;
94 int sk;
95 Vector freq(maxOfSkyComp);
96 TMatrix<float> spectrum;
97 TMatrix<float> fdenu; //par freq et par pixel
98 int nb_source;
99 TMatrix<float> rareSource; //par freq et par numero de source
100
101 for(sk = 0; sk<maxOfSkyComp; sk++)
102 {
103 TMatrix<float> ings;
104 // Opening the file containing the map
105 cout << "-------------------" << endl;
106 cout << " Processing map's frequency component No " << sk+1 << endl;
107 sprintf(buff, "%d", sk+1);
108 key = (string)"RADSPECMAP" + buff;
109
110 flnm = BuildFITSFileName(dc.SParam(key,0));
111 cout << " Reading Input FITS map " << (string)flnm << endl;
112 FITS_TArray<float> read_input(flnm);
113 ings = (TArray<float>)read_input;
114
115 if (printlev > 2)
116 {
117 double min = 9.e99;
118 double max = -9.e99;
119 for(int x=0;x<ings.NRows(); x++)
120 {
121 for(int y=0;y<ings.NCols(); y++)
122 {
123 if(min>ings(x,y)) min = ings(x,y);
124 if(max<ings(x,y)) max = ings(x,y);
125 }
126 }
127 cout << "min and max values for input map" << min << " " << max << endl;
128 }
129 // Computing the frequency according to the name of the file
130 freq(sk) = ComputeFrequency(dc,key);
131 if (printlev > 2) cout << "filling spectrum for freq = " << freq(sk) << endl;
132
133 // Opening the file containing the point source
134 sprintf(buff, "%d", sk+1);
135 key2 = (string)"SOURCEPTMAP" + buff;
136 flnm = BuildFITSFileName(dc.SParam(key2,0));
137 const char* nameOfFile = flnm ;
138 if (printlev > 2) cout << " Reading Input FITS map " << nameOfFile << endl;
139 ifstream from(nameOfFile);
140 string dum;
141 from >> nb_source >> dum;
142 if(sk==0) rareSource.ReSize(maxOfSkyComp,nb_source);
143 for (int ii=0; ii< nb_source; ii++)
144 {
145 double value = 0;
146 from >> value;
147 rareSource(sk,ii) = value*1.e-26; //for Jansky->W
148 }
149 from.close();
150
151 nbOfPix = ings.NRows()*ings.NCols();
152 spectrum.ReSize(ings.NRows(),ings.NCols());
153 if(sk==0) fdenu.ReSize(maxOfSkyComp,nbOfPix);
154 int xy=0;
155 for(int x=0;x<ings.NRows(); x++)
156 {
157 for(int y=0;y<ings.NCols(); y++)
158 {
159 spectrum(x,y) = ings(x,y)*1.e-26; //for Jansky->W
160 fdenu(sk,xy) = spectrum(x,y);
161 xy += 1;
162 }
163 }
164 if (printlev > 2)
165 {
166 MeanSig(spectrum.DataBlock(), moy, sig );
167 cout << " MeanSig for spectrum map - Mean= " << moy << " Sigma= " << sig << endl;
168 }
169
170
171 K = dc.DParam(key, 1, 1.);
172 if (debuglev > 4) { // Writing the input map to the outppf
173 FIO_TArray<float> fiog;
174 fiog.Write(*so, key);
175 }
176
177 if(printlev>2)
178 {
179 sprintf(buff, "End of Proc. Comp. %d ", sk+1);
180 cout << " --------------------------------------- \n" << endl;
181 PrtTim(buff);
182 }
183 } // End of sky component loop
184
185 // ---------------------------------------------------------------------
186 // Integration in the detector band of the maps
187 // ---------------------------------------------------------------------
188 SpectralResponse* sr = NULL;
189 sr = getSpectralResponse(dc);
190
191 if (printlev > 2) cout << "SpectralResponse!" << *sr << endl;
192 TVector<float> outCoeff(nbOfPix);
193
194 for(int pix=0;pix<nbOfPix;pix++)
195 {
196 Vector smallFDeNu(maxOfSkyComp);
197 for(sk = 0; sk<maxOfSkyComp; sk++)
198 {
199 smallFDeNu(sk) = fdenu(sk,pix);
200 }
201
202 RadSpectraVec radSV(freq,smallFDeNu);
203 if (printlev > 10) cout << "RadiationSpectra" << radSV << endl;
204
205 outCoeff(pix) = radSV.filteredIntegratedFlux(*sr);
206 }
207 int i;
208 for(i=0;i<OutputMap.NbPixels();i++)
209 {
210 double rndval = drand01();
211 //stupid!!!!! if(rndval == 1) rndval = drand01();
212 //unsused!!!!! int rndvalPix = rndval*OutputMap.NbPixels();
213 int outpix = int(rndval*nbOfPix);
214 if(outpix >= nbOfPix) outpix = nbOfPix - 1; // add clean size protection
215 OutputMap(i) = outCoeff(outpix);
216 }
217
218
219 // ---------------------------------------------------------------------
220 // Integration in the detector band of the sources
221 // ---------------------------------------------------------------------
222 TVector<float> sourceCoeff(nb_source);
223 for(int nsource=0;nsource<nb_source;nsource++)
224 {
225 Vector SourceFDeNu(maxOfSkyComp);
226 for(sk = 0; sk<maxOfSkyComp; sk++)
227 {
228 SourceFDeNu(sk) = rareSource(sk,nsource);
229 }
230 RadSpectraVec radSV_Source(freq,SourceFDeNu);
231 sourceCoeff(nsource) = radSV_Source.filteredIntegratedFlux(*sr);
232 }
233
234 for(i=0;i<nb_source;i++)
235 {
236 double rndval = drand01();
237 int outPix = int(rndval*OutputMap.NbPixels());
238 if(outPix >= OutputMap.NbPixels()) outPix = OutputMap.NbPixels() - 1; // add clean size protection
239 SourceMap(outPix) = sourceCoeff(i);
240 OutputMap(outPix) += SourceMap(outPix);
241 }
242 }
243 catch (PException exc) {
244 msg = exc.Msg();
245 cerr << " !!!! extractRS - Catched PException - Msg= " << exc.Msg() << endl;
246 rc = 50;
247 }
248 catch (PThrowable exc) {
249 msg = exc.Msg();
250 cerr << " !!!! extractRS - Catched PThrowable - Msg= " << exc.Msg() << endl;
251 rc = 50;
252 }
253 catch (...) {
254 cerr << " Une exception de type inconnue !!! " << endl;
255 exit(99);
256 }
257
258 // Saving the output map in FITS format
259 if (okout)
260 {
261 // {
262 // FitsIoServer fios;
263 // fios.save(SourceMap, arg[3]);
264 // double moy,sig;
265 // MeanSig(SourceMap.DataBlock(),moy,sig);
266 // cout << " MeanSig for Source Map - Mean= " << moy << " Sigma= " << sig << endl;
267 // cout << "Source Map (SphereHEALPix<float>) written to FITS file "
268 // << (string)(arg[3]) << endl;
269 // }
270 {
271 FITS_SphereHEALPix<float> fios2(OutputMap);
272 fios2.Write(arg[2]);
273 double moy,sig;
274 MeanSig(OutputMap.DataBlock(),moy,sig);
275 cout << " MeanSig for Output Map - Mean= " << moy << " Sigma= " << sig << endl;
276 cout << "Output Map (SphereHEALPix<float>) written to FITS file "
277 << (string)(arg[2]) << endl;
278 }
279 if(printlev>2) PrtTim("End of WriteFITS ");
280 // Saving the output map in PPF format
281 if (narg > 2) {
282 POutPersist s(arg[3]);
283 FIO_SphereHEALPix<float> fiog(OutputMap);
284 fiog.Write(s);
285 cout << "Output Map (SphereHEALPix<float>) written to POutPersist file "
286 << (string)(arg[3]) << endl;
287 if(printlev>2) PrtTim("End of WritePPF ");
288 }
289 }
290 if (so) delete so; // Closing the debug ppf file
291 return(rc);
292}
293
294
295
296/* Nouvelle-Fonction */
297int CheckCards(DataCards & dc, string & msg)
298 // Function to check datacards
299{
300 rdmap = false;
301 mapPath[0] = '\0';
302 nskycomp = 0;
303 debuglev = 0;
304 printlev = 0;
305
306 int rc = 0;
307 string key, key2;
308
309 // Checking datacards
310 if (dc.NbParam("EXT_RS") < 2) {
311 rc = 71;
312 msg = "Invalid parameters - Check @EXT_RS card ";
313 return(rc);
314 }
315 int kc;
316 char buff[256];
317 bool pb = false;
318 string key3;
319 int ncomp = 0;
320 ncomp = dc.IParam("EXT_RS", 0);
321 int nside = 32;
322 nside = dc.IParam("EXT_RS", 1);
323
324 for(kc=0; kc<ncomp; kc++)
325 {
326 key = "RS_EXT_PATH";
327 if (dc.NbParam(key) < 3) strncpy(mapPath, dc.SParam(key, 0).c_str(), 255);
328 mapPath[255] = '\0';
329 sprintf(buff, "RADSPECMAP%d", kc+1);
330 key = buff;
331
332 if (dc.NbParam(key) < 1) {
333 msg = "Missing or invalid card : " + key;
334 pb = true; break;
335 }
336 }
337
338 // Checking detection filter specification
339
340 key = "GAUSSFILTER";
341 key2 = "FILTERFITSFILE";
342 if ( (dc.NbParam(key) < 5) && (dc.NbParam(key2) < 3)) {
343 msg = "Missing card or parameters : Check @GAUSSFILTER or @FILTERFITSFILE";
344 rc = 73; return(rc);
345 }
346
347 if (pb) {
348 rc = 75;
349 return(75);
350 }
351
352
353 // Initialiazing parameters
354 rc = 0;
355 msg = "OK";
356 nskycomp = ncomp;
357 hp_nside = nside;
358 // Checking for PATH definition card
359 key = "RS_EXT_PATH";
360 if (dc.NbParam(key) < 3) strncpy(mapPath, dc.SParam(key, 0).c_str(), 255);
361 mapPath[255] = '\0';
362 key = "DEBUGLEVEL";
363 debuglev = dc.IParam(key, 0, 0);
364 key = "PRINTLEVEL";
365 printlev = dc.IParam(key, 0, 0);
366 return(rc);
367}
368
369static char buff_flnm[1024]; // Mal protege !
370/* Nouvelle-Fonction */
371char* BuildFITSFileName(string const & fname)
372{
373if (mapPath[0] != '\0') sprintf(buff_flnm, "%s/%s", mapPath, fname.c_str());
374else sprintf(buff_flnm, "%s", fname.c_str());
375return(buff_flnm);
376}
377
378
379
380/* Nouvelle-Fonction */
381SpectralResponse * getSpectralResponse(DataCards & dc)
382{
383 SpectralResponse * filt = NULL;
384
385 string key = "FILTERFITSFILE";
386 string key2 = "GAUSSFILTER";
387 string ppfname = "filter";
388
389 if (dc.HasKey(key) ) { // Reading FITS filter file
390 char ifnm[256];
391 strncpy(ifnm, dc.SParam(key, 1).c_str(), 255);
392 ifnm[255] = '\0';
393 TMatrix<float> mtx(2,10);
394 FITS_TArray<float> read_input(ifnm);
395 mtx = (TMatrix<float>)read_input;
396
397 double numin = dc.DParam(key, 2, 1.);
398 double numax = dc.DParam(key, 3, 9999.);
399 Vector nu(mtx.NCols());
400 Vector fnu(mtx.NCols());
401 for(int k=0; k<mtx.NCols(); k++) {
402 nu(k) = mtx(0, k);
403 fnu(k) = mtx(1, k);
404 }
405 filt = new SpecRespVec(nu, fnu, numin, numax);
406 ppfname = key;
407 }
408 else if (dc.HasKey(key2) ) { // creating GaussianFilter
409 double nu0 = dc.DParam(key2, 0, 10.);
410 double s = dc.DParam(key2, 1, 1.);
411 double a = dc.DParam(key2, 2, 1.);
412 double numin = dc.DParam(key2, 3, 0.1);
413 double numax = dc.DParam(key2, 4, 9999);
414 filt = new GaussianFilter(nu0, s, a, numin, numax);
415 ppfname = key2;
416 }
417 if (filt == NULL) throw ParmError("datacard error ! No detection filter");
418 cout << " Filter decoded - Created " << endl;
419 cout << *filt << endl;
420
421// for debug
422 // if (debuglev > 1) SpectralResponse2Nt(*filt, *so, ppfname);
423 return(filt);
424}
425
426
427float ComputeFrequency(DataCards & dc,string& key)
428{
429 ofstream filename("temp_filename");
430 filename << dc.SParam(key,0) << '\n';
431 filename.close();
432
433 ifstream fromtempfile("temp_filename");
434 char chtemp;
435 string temp_str;
436 int count = 0;
437 do {
438 fromtempfile.get(chtemp);
439 if(count> 12 && count < 20)
440 {
441 temp_str+= chtemp;
442 }
443 count+=1;
444 } while(chtemp!='\n');
445 fromtempfile.close();
446
447 ofstream filename2("temp_filename");
448 filename2 << temp_str << '\n';
449 filename2.close();
450
451 int frequency;
452 ifstream fromtempfile2("temp_filename");
453 fromtempfile2 >> frequency;
454 fromtempfile2.close();
455 return ((float)frequency/1000.);
456}
Note: See TracBrowser for help on using the repository browser.