source: Sophya/trunk/SophyaLib/SkyT/skymixer.cc@ 663

Last change on this file since 663 was 620, checked in by ansari, 26 years ago

change of strncpy(ifnm, dc.SParam("READMAP", 0) => 0 instead of 1

File size: 13.4 KB
Line 
1#include "machdefs.h"
2#include <stdlib.h>
3#include <stdio.h>
4
5#include <iostream.h>
6#include <math.h>
7
8#include <string>
9#include <vector>
10
11#include "timing.h"
12#include "sambainit.h"
13#include "pexceptions.h"
14#include "datacards.h"
15#include "fitsioserver.h"
16
17#include "spheregorski.h"
18
19#include "radspecvector.h"
20#include "blackbody.h"
21#include "nupower.h"
22
23#include "squarefilt.h"
24#include "trianglefilt.h"
25#include "specrespvector.h"
26#include "gaussfilt.h"
27
28// -----------------------------------------------------------------
29// ------------- Function declaration ------------------------------
30int CheckCards(DataCards & dc, string & msg);
31char * BuildFITSFileName(string const & fname);
32SpectralResponse * getSpectralResponse(DataCards & dc);
33RadSpectra * getEmissionSpectra(DataCards & dc, int nc);
34void RadSpec2Nt(RadSpectra & rs, POutPersist & so, string name);
35void SpectralResponse2Nt(SpectralResponse& sr, POutPersist & so, string name);
36
37template <class T>
38void addComponent(SpectralResponse& sr, PixelMap<T>& finalMap,
39 PixelMap<T>& mapToAdd, RadSpectra& rs, double K=1.);
40template <class T>
41void MeanSig(NDataBlock<T> const & dbl, double& gmoy, double& gsig);
42// -----------------------------------------------------------------
43
44// ----- Global (static) variables ------------
45static bool rdmap = false; // true -> Read map first
46static char mapPath[256]; // Path for input maps
47static int hp_nside = 32; // HealPix NSide
48static int nskycomp = 0; // Number of sky components
49static int debuglev = 0; // Debug Level
50static int printlev = 0; // Print Level
51static POutPersist * so = NULL; // Debug PPFOut file
52
53// -------------------------------------------------------------------------
54// main program
55// -------------------------------------------------------------------------
56int main(int narg, char * arg[])
57{
58 if ((narg < 3) || ((narg > 1) && (strcmp(arg[1], "-h") == 0) )) {
59 cout << " Usage: skymixer parameterFile outputfitsname [outppfname]" << endl;
60 exit(0);
61 }
62
63InitTim();
64
65string msg;
66int rc = 0;
67RadSpectra * es = NULL;
68SpectralResponse * sr = NULL;
69double moy, sig;
70
71DataCards dc;
72so = NULL;
73
74try {
75 string dcard = arg[1];
76 cout << " Decoding parameters from file " << dcard << endl;
77 dc.ReadFile(dcard);
78
79 rc = CheckCards(dc, msg);
80 if (rc) {
81 cerr << " Error condition -> Rc= " << rc << endl;
82 cerr << " Msg= " << msg << endl;
83 return(rc);
84 }
85}
86catch (PException exc) {
87 msg = exc.Msg();
88 cerr << " !!!! skymixer/ Readcard - Catched exception - Msg= " << exc.Msg() << endl;
89 return(90);
90}
91
92
93cout << " skymix/Info : NComp = " << nskycomp << " SphereGorski_NSide= " << hp_nside << endl;
94cout << " ... MapPath = " << (string)mapPath << " DebugLev= " << debuglev
95 << " PrintLev= " << printlev << endl;
96
97// We create an output persist file for writing debug objects
98if (debuglev > 0) so = new POutPersist("skymixdbg.ppf");
99
100SphereGorski<float> outgs(hp_nside);
101bool okout = false;
102
103try {
104 if (rdmap) { // Reading map from FITS file
105 FitsIoServer fios;
106 char ifnm[256];
107 strncpy(ifnm, dc.SParam("READMAP", 0).c_str(), 255);
108 ifnm[255] = '\0';
109 cout << " Reading output HealPix map from FITS file " << (string)ifnm << endl;
110 fios.load(outgs, ifnm, 2);
111 cout << " Output HealPIx Map read - NbPixels= " << outgs.NbPixels() << endl;
112 if (printlev > 0) {
113 MeanSig(outgs.DataBlock(), moy, sig );
114 cout << " MeanSig for outpout map - Mean= " << moy << " Sigma= " << sig << endl;
115 }
116 }
117 else {
118 cout << " Output HealPix Map created - NbPixels= " << outgs.NbPixels() << endl;
119 outgs.SetPixels(0.);
120 }
121
122 // Decoding detection pass-band filter
123 sr = getSpectralResponse(dc);
124 PrtTim(" After FilterCreation ");
125
126 FitsIoServer fios; // Our FITS IO Server
127 char * flnm, buff[90];
128 string key;
129
130 SphereGorski<float> ings(hp_nside); // The input map
131 double K = 1.;
132 // Loop over sky component
133 int sk;
134 for(sk = 0; sk<nskycomp; sk++) {
135 cout << " Processing sky component No " << sk+1 << endl;
136 sprintf(buff, "%d", sk+1);
137 key = (string)"MAPFITSFILE" + buff;
138 flnm = BuildFITSFileName(dc.SParam(key, 0));
139 cout << " Reading Input FITS map " << (string)flnm << endl;
140 fios.load(ings, flnm, 2);
141 K = dc.DParam(key, 1, 1.);
142 if (debuglev > 4) { // Writing tne input map to the outppf
143 FIO_SphereGorski<float> fiog(ings);
144 fiog.Write(*so, key);
145 }
146 // getting Emission spectra
147 if (es) { delete es; es = NULL; }
148 es = getEmissionSpectra(dc, sk);
149
150 if (printlev > 2) {
151 MeanSig(ings.DataBlock(), moy, sig );
152 cout << " MeanSig for input map - Mean= " << moy << " Sigma= " << sig << endl;
153 }
154 addComponent(*sr, outgs, ings, *es, K);
155 okout = true;
156 if (printlev > 1) {
157 MeanSig(outgs.DataBlock(), moy, sig );
158 cout << " MeanSig for Sum map - Mean= " << moy << " Sigma= " << sig << endl;
159 }
160 sprintf(buff, "End of Proc. Comp. %d ", sk+1);
161 cout << " --------------------------------------- \n" << endl;
162 PrtTim(buff);
163 } // End of sky component loop
164} // End of try block
165
166catch (PException exc) {
167 msg = exc.Msg();
168 cerr << " !!!! skymixer - Catched exception - Msg= " << exc.Msg() << endl;
169 rc = 50;
170 }
171
172// Saving the output map in FITS format
173if (okout) {
174 FitsIoServer fios;
175 fios.save(outgs, arg[2]);
176 cout << "Output Map (SphereGorski<float>) written to FITS file "
177 << (string)(arg[2]) << endl;
178 PrtTim("End of WriteFITS ");
179// Saving the output map in PPF format
180 if (narg > 3) {
181 POutPersist s(arg[3]);
182 FIO_SphereGorski<float> fiog(&outgs) ;
183 fiog.Write(s);
184 cout << "Output Map (SphereGorski<float>) written to POutPersist file "
185 << (string)(arg[3]) << endl;
186 PrtTim("End of WritePPF ");
187 }
188 }
189 if (so) delete so; // Closing the debug ppf file
190 return(rc);
191}
192
193/* Nouvelle-Fonction */
194int CheckCards(DataCards & dc, string & msg)
195// Function to check datacards
196{
197rdmap = false;
198mapPath[0] = '\0';
199hp_nside = 32;
200nskycomp = 0;
201debuglev = 0;
202printlev = 0;
203
204int rc = 0;
205string key, key2;
206
207 // Cheking datacards
208 if (dc.NbParam("SKYMIX") < 2) {
209 rc = 71;
210 msg = "Invalid parameters - Check @SKYMIX card ";
211 return(rc);
212 }
213 key = "READMAP";
214 if (dc.HasKey(key)) {
215 if (dc.NbParam(key) < 1) {
216 rc = 72;
217 msg = "Invalid parameters - Check @READMAP card ";
218 return(rc);
219 }
220 else rdmap = true;
221 }
222
223// Checking detection filter specification
224 key = "GAUSSFILTER";
225 key2 = "FILTERFITSFILE";
226 if ( (dc.NbParam(key) < 5) && (dc.NbParam(key2) < 3)) {
227 msg = "Missing card or parameters : Check @GAUSSFILTER or @FILTERFITSFILE";
228 rc = 73; return(rc);
229 }
230
231 // Decoding number of component and pixelisation parameter
232 int mg = 32;
233 int ncomp = 0;
234 ncomp = dc.IParam("SKYMIX", 0, 0);
235 mg = dc.IParam("SKYMIX", 1, 32);
236 if (ncomp < 1) {
237 msg = "Invalid parameters - Check datacards @SKYMIX ";
238 rc = 74;
239 return(rc);
240 }
241
242 // Checking detection filter specification
243// Checking input FITS file specifications
244 int kc;
245 char buff[256];
246 bool pb = false;
247 string key3;
248 for(kc=0; kc<ncomp; kc++) {
249 sprintf(buff, "MAPFITSFILE%d", kc+1);
250 key = buff;
251 if (dc.NbParam(key) < 1) {
252 msg = "Missing or invalid card : " + key;
253 pb = true; break;
254 }
255 sprintf(buff, "SPECTRAFITSFILE%d", kc+1);
256 key = buff;
257 sprintf(buff, "BLACKBODY%d", kc+1);
258 key2 = buff;
259 sprintf(buff, "POWERLAWSPECTRA%d", kc+1);
260 key3 = buff;
261 if ( (dc.NbParam(key) < 3) && (dc.NbParam(key2) < 1) && (dc.NbParam(key3) < 6)) {
262 msg = "Missing card or invalid parameters : " + key + " " + key2 + " " + key3;
263 pb = true; break;
264 }
265
266 }
267
268 if (pb) {
269 rc = 75;
270 return(75);
271 }
272
273
274// Initialiazing parameters
275 rc = 0;
276 msg = "OK";
277 nskycomp = ncomp;
278 hp_nside = mg;
279
280// Checking for PATH definition card
281 key = "MAPPATH";
282 if (dc.NbParam(key) < 3) strncpy(mapPath, dc.SParam(key, 0).c_str(), 255);
283 mapPath[255] = '\0';
284 key = "DEBUGLEVEL";
285 debuglev = dc.IParam(key, 0, 0);
286 key = "PRINTLEVEL";
287 printlev = dc.IParam(key, 0, 0);
288 return(rc);
289}
290
291static char buff_flnm[1024]; // Mal protege !
292/* Nouvelle-Fonction */
293char* BuildFITSFileName(string const & fname)
294{
295if (mapPath[0] != '\0') sprintf(buff_flnm, "%s/%s", mapPath, fname.c_str());
296else sprintf(buff_flnm, "%s", fname.c_str());
297return(buff_flnm);
298}
299
300/* Nouvelle-Fonction */
301SpectralResponse * getSpectralResponse(DataCards & dc)
302{
303 SpectralResponse * filt = NULL;
304
305 string key = "FILTERFITSFILE";
306 string key2 = "GAUSSFILTER";
307 string ppfname = "filter";
308
309 if (dc.HasKey(key) ) { // Reading FITS filter file
310 FitsIoServer fios;
311 char ifnm[256];
312 strncpy(ifnm, dc.SParam(key, 1).c_str(), 255);
313 ifnm[255] = '\0';
314 Matrix mtx(2,10);
315 fios.load(mtx, ifnm);
316 double numin = dc.DParam(key, 2, 1.);
317 double numax = dc.DParam(key, 3, 9999.);
318 Vector nu(mtx.NCols());
319 Vector fnu(mtx.NCols());
320 for(int k=0; k<mtx.NCols(); k++) {
321 nu(k) = mtx(0, k);
322 fnu(k) = mtx(1, k);
323 }
324 filt = new SpecRespVec(nu, fnu, numin, numax);
325 ppfname = key;
326 }
327 else if (dc.HasKey(key2) ) { // creating GaussianFilter
328 double nu0 = dc.DParam(key2, 0, 10.);
329 double s = dc.DParam(key2, 1, 1.);
330 double a = dc.DParam(key2, 2, 1.);
331 double numin = dc.DParam(key2, 3, 0.1);
332 double numax = dc.DParam(key2, 4, 9999);
333 filt = new GaussianFilter(nu0, s, a, numin, numax);
334 ppfname = key2;
335 }
336 if (filt == NULL) throw ParmError("datacard error ! No detection filter");
337 cout << " Filter decoded - Created " << endl;
338 cout << *filt << endl;
339
340// for debug
341 if (debuglev > 1) SpectralResponse2Nt(*filt, *so, ppfname);
342 return(filt);
343}
344
345/* Nouvelle-Fonction */
346RadSpectra * getEmissionSpectra(DataCards & dc, int nc)
347{
348 char numb[16];
349 sprintf(numb, "%d", nc+1);
350
351 string key = (string)"SPECTRAFITSFILE" + numb;
352 string key2 = (string)"BLACKBODY" + numb;
353 string key3 = (string)"POWERLAWSPECTRA" + numb;
354 string ppfname = "espectra";
355
356 RadSpectra * rs = NULL;
357 if (dc.HasKey(key) ) { // Reading emission spectra from file
358 char * ifnm = BuildFITSFileName(dc.SParam(key, 0));
359 cout << " Reading Input FITS spectra file " << (string)ifnm << endl;
360 FitsIoServer fios;
361 Matrix mtx(2,10);
362 fios.load(mtx, ifnm);
363 double numin = dc.DParam(key, 2, 1.);
364 double numax = dc.DParam(key, 3, 9999.);
365 Vector nu(mtx.NCols());
366 Vector tnu(mtx.NCols());
367 for(int k=0; k<mtx.NCols(); k++) {
368 nu(k) = mtx(0, k);
369 tnu(k) = mtx(1, k);
370 }
371 rs = new RadSpectraVec(nu, tnu, numin, numax);
372 ppfname = key;
373 }
374 else if (dc.HasKey(key2) ) { // Creating BlackBody emission spectra
375 rs = new BlackBody(dc.DParam(key2, 0, 2.726));
376 ppfname = key2;
377 }
378 else if (dc.HasKey(key3) ) { // Creating PowerLaw emission spectra
379 double a = dc.DParam(key3, 0, 1.);
380 double nu0 = dc.DParam(key3, 1, 100.);
381 double dnu = dc.DParam(key3, 2, 10.);
382 double b = dc.DParam(key3, 3, 0.);
383 double numin = dc.DParam(key3, 4, 0.1);
384 double numax = dc.DParam(key3, 5, 9999);
385 rs = new PowerLawSpectra(a, b, nu0, dnu, numin, numax);
386 ppfname = key3;
387 }
388 if (rs == NULL) throw ParmError("datacard error ! missing Emission spectra");
389 cout << " Emission spectra decoded - Created (" << ppfname << ")" << endl;
390 cout << *rs << endl;
391// for debug
392 if (debuglev > 2) RadSpec2Nt(*rs, *so, ppfname);
393 return(rs);
394}
395
396
397
398
399/* Nouvelle-Fonction */
400template <class T>
401void addComponent(SpectralResponse& sr, PixelMap<T>& finalMap,
402 PixelMap<T>& mapToAdd, RadSpectra& rs, double K)
403{
404 // finalMap = finalMap + coeff* mapToAdd
405 // coeff = convolution of sr and rs
406 // compute the coefficient corresponding to mapToAdd
407 if (finalMap.NbPixels() != mapToAdd.NbPixels())
408 throw SzMismatchError("addComponent()/Error: Unequal number of Input/Output map pixels");
409 double coeff = rs.filteredIntegratedFlux(sr) * K;
410 if (printlev > 1)
411 cout << " addComponent - Coeff= " << coeff << " (K= " << K << ")" << endl;
412 for(int i=0; i<finalMap.NbPixels(); i++)
413 {
414 finalMap(i) += coeff * mapToAdd(i);
415 }
416}
417
418/* Nouvelle-Fonction */
419template <class T>
420void MeanSig(NDataBlock<T> const & dbl, double& gmoy, double& gsig)
421
422{
423 gmoy=0.;
424 gsig = 0.;
425 double valok;
426 for(int k=0; k<(int)dbl.Size(); k++) {
427 valok = dbl(k);
428 gmoy += valok; gsig += valok*valok;
429 }
430 gmoy /= (double)dbl.Size();
431 gsig = gsig/(double)dbl.Size() - gmoy*gmoy;
432 if (gsig >= 0.) gsig = sqrt(gsig);
433}
434
435/* Nouvelle-Fonction */
436void RadSpec2Nt(RadSpectra & rs, POutPersist & so, string name)
437{
438 char *ntn[2] = {"nu","fnu"};
439 NTuple nt(2,ntn); // Creation NTuple (AVEC new )
440 float xnt[2];
441 double nu;
442 double numin = rs.minFreq();
443 double numax = rs.maxFreq();
444 int nmax = 500;
445 double dnu = (numax-numin)/nmax;
446 for(int k=0; k<nmax; k++) {
447 nu = numin+k*dnu;
448 xnt[0] = nu;
449 xnt[1] = rs.flux(nu);
450 nt.Fill(xnt);
451 }
452 ObjFileIO<NTuple> oiont(nt);
453 oiont.Write(so, name);
454 return;
455}
456
457/* Nouvelle-Fonction */
458void SpectralResponse2Nt(SpectralResponse& sr, POutPersist & so, string name)
459{
460 char *ntn[2] = {"nu","tnu"};
461 NTuple nt(2,ntn); // Creation NTuple (AVEC new )
462 float xnt[2];
463 double nu;
464 double numin = sr.minFreq();
465 double numax = sr.maxFreq();
466 int nmax = 500;
467 double dnu = (numax-numin)/nmax;
468 for(int k=0; k<nmax; k++) {
469 nu = numin+k*dnu;
470 xnt[0] = nu;
471 xnt[1] = sr.transmission(nu);
472 nt.Fill(xnt);
473 }
474 ObjFileIO<NTuple> oiont(nt);
475 oiont.Write(so, name);
476 return;
477}
478
Note: See TracBrowser for help on using the repository browser.