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

Last change on this file since 885 was 884, checked in by ansari, 25 years ago

Adaptation SphereGorski -> SphereHEALPix apres modifs Sophie (suite) - Reza 11/4/2000

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