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 | // ------ Function declaration
|
---|
29 | void addComponent(SpectralResponse& sr, PixelMap<float>& finalMap,
|
---|
30 | PixelMap<float>& mapToAdd, RadSpectra& rs, double K=1.);
|
---|
31 | void addComponent(SpectralResponse& sr, PixelMap<double>& finalMap,
|
---|
32 | PixelMap<double>& mapToAdd, RadSpectra& rs, double K=1.);
|
---|
33 |
|
---|
34 | int CheckCards(DataCards & dc, string & msg);
|
---|
35 | char * BuildFITSFileName(string const & fname);
|
---|
36 | void RadSpec2Nt(RadSpectra & rs, POutPersist & so, string name);
|
---|
37 | void SpectralResponse2Nt(SpectralResponse& sr, POutPersist & so, string name);
|
---|
38 |
|
---|
39 | // ----- Variable globale ------------
|
---|
40 | static char mapPath[256]; // Path for input maps
|
---|
41 | static int hp_nside = 32; // HealPix NSide
|
---|
42 | static int nskycomp = 0; // Number of sky components
|
---|
43 | static int debuglev = 0; // debuglevel
|
---|
44 |
|
---|
45 | // -------------------------------------------------------------------------
|
---|
46 | // main program
|
---|
47 | // -------------------------------------------------------------------------
|
---|
48 | int main(int narg, char * arg[])
|
---|
49 | {
|
---|
50 | if ((narg < 3) || ((narg > 1) && (strcmp(arg[1], "-h") == 0) )) {
|
---|
51 | cout << " Usage: skymixer parameterFile outputfitsname [outppfname]" << endl;
|
---|
52 | exit(0);
|
---|
53 | }
|
---|
54 |
|
---|
55 | InitTim();
|
---|
56 |
|
---|
57 | string msg;
|
---|
58 | int rc = 0;
|
---|
59 | POutPersist * so = NULL;
|
---|
60 |
|
---|
61 | try {
|
---|
62 | string dcard = arg[1];
|
---|
63 | cout << " Decoding parameters from file " << dcard << endl;
|
---|
64 | DataCards dc(dcard);
|
---|
65 |
|
---|
66 | rc = CheckCards(dc, msg);
|
---|
67 | if (rc) goto Fin;
|
---|
68 |
|
---|
69 | cout << " skymix/Info : NComp = " << nskycomp << " SphereGorski_NSide= " << hp_nside << endl;
|
---|
70 | cout << " ... MapPath = " << (string)mapPath << " DbgLev= " << debuglev << endl;
|
---|
71 |
|
---|
72 | // We create an output persist file for writing debug objects
|
---|
73 | if (debuglev > 0) so = new POutPersist("skymixdbg.ppf");
|
---|
74 |
|
---|
75 | SphereGorski<float> outgs(hp_nside);
|
---|
76 | cout << " Output Gorski Map created - NbPixels= " << outgs.NbPixels() << endl;
|
---|
77 | outgs.SetPixels(0.);
|
---|
78 |
|
---|
79 | // Decoding detection pass-band filter
|
---|
80 | double nu0 = dc.DParam("FILTER", 0, 10.);
|
---|
81 | double s = dc.DParam("FILTER", 1, 1.);
|
---|
82 | double a = dc.DParam("FILTER", 2, 1.);
|
---|
83 | double numin = dc.DParam("FILTER", 3, 0.1);
|
---|
84 | double numax = dc.DParam("FILTER", 4, 9999);
|
---|
85 | GaussianFilter filt(nu0, s, a, numin, numax);
|
---|
86 | cout << " Filter decoded - Created " << endl;
|
---|
87 | cout << filt << endl;
|
---|
88 |
|
---|
89 | // FOR debug
|
---|
90 | if (debuglev > 0) SpectralResponse2Nt(filt, *so, "filter");
|
---|
91 |
|
---|
92 | PrtTim(" After FilterCreation ");
|
---|
93 |
|
---|
94 | SphereGorski<float> * ings = NULL; // Our input map
|
---|
95 | FitsIoServer fios; // Our FITS IO Server
|
---|
96 | char * flnm, buff[64];
|
---|
97 | string key;
|
---|
98 |
|
---|
99 | // Loop over sky component
|
---|
100 | int sk;
|
---|
101 | for(sk = 0; sk<nskycomp; sk++) {
|
---|
102 | cout << " Processing sky component No " << sk+1 << endl;
|
---|
103 | if (ings) { delete ings; ings = NULL; }
|
---|
104 | ings = new SphereGorski<float>(hp_nside);
|
---|
105 | sprintf(buff, "MAPFITSFILE%d", sk+1);
|
---|
106 | key = buff;
|
---|
107 | flnm = BuildFITSFileName(dc.SParam(key, 0));
|
---|
108 | cout << " Reading Input FITS map " << (string)flnm << endl;
|
---|
109 | fios.load(*ings, flnm, 1);
|
---|
110 |
|
---|
111 | if (debuglev > 4) { // Writing tne input map to the outppf
|
---|
112 | FIO_SphereGorski<float> fiog(ings);
|
---|
113 | fiog.Write(*so, key);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | } // End of try block
|
---|
117 |
|
---|
118 | catch (PException exc) {
|
---|
119 | msg = exc.Msg();
|
---|
120 | cerr << " !!!! skymixer - Catched exception - Msg= " << exc.Msg() << endl;
|
---|
121 | rc = 90;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | Fin:
|
---|
126 | if (so) delete so; // Closing the debug ppf file
|
---|
127 | if (rc == 0) return(0);
|
---|
128 | cerr << " Error condition -> Rc= " << rc << endl;
|
---|
129 | cerr << " Msg= " << msg << endl;
|
---|
130 | return(rc);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /* Nouvelle-Fonction */
|
---|
134 | int CheckCards(DataCards & dc, string & msg)
|
---|
135 | // Function to check datacards
|
---|
136 | {
|
---|
137 | mapPath[0] = '\0';
|
---|
138 | hp_nside = 32;
|
---|
139 | nskycomp = 0;
|
---|
140 | debuglev = 0;
|
---|
141 |
|
---|
142 | int rc = 0;
|
---|
143 | // Cheking datacards
|
---|
144 | if ( (!dc.HasKey("SKYMIX")) || (!dc.HasKey("FILTER")) ) {
|
---|
145 | rc = 71;
|
---|
146 | msg = "Invalid parameters - NO @SKYMIX or @FILTER card ";
|
---|
147 | return(rc);
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Decoding number of component and pixelisation parameter
|
---|
151 | int mg = 32;
|
---|
152 | int ncomp = 0;
|
---|
153 | ncomp = dc.IParam("SKYMIX", 0, 0);
|
---|
154 | mg = dc.IParam("SKYMIX", 1, 32);
|
---|
155 | if (ncomp < 1) {
|
---|
156 | msg = "Invalid parameters - Check datacards @SKYMIX ";
|
---|
157 | rc = 72;
|
---|
158 | return(rc);
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Checking input FITS file specifications
|
---|
162 | int kc;
|
---|
163 | string key, key2;
|
---|
164 | char buff[256];
|
---|
165 | bool pb = false;
|
---|
166 | for(kc=0; kc<ncomp; kc++) {
|
---|
167 | sprintf(buff, "MAPFITSFILE%d", kc+1);
|
---|
168 | key = buff;
|
---|
169 | if (dc.NbParam(key) < 1) {
|
---|
170 | msg = "Missing or invalid card : " + key;
|
---|
171 | pb = true; break;
|
---|
172 | }
|
---|
173 | sprintf(buff, "SPECTRAFITSFILE%d", kc+1);
|
---|
174 | key = buff;
|
---|
175 | sprintf(buff, "SPECTRAFUNC%d", kc+1);
|
---|
176 | key2 = buff;
|
---|
177 | if ( (dc.NbParam(key) < 1) && (dc.NbParam(key2) < 2) ) {
|
---|
178 | msg = "Missing card or invalid parameters : " + key + " or " + key2;
|
---|
179 | pb = true; break;
|
---|
180 | }
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (pb) {
|
---|
185 | rc = 72;
|
---|
186 | return(72);
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Checking detection filter specification
|
---|
190 | key = "FILTER";
|
---|
191 | if (dc.NbParam(key) < 3) {
|
---|
192 | msg = "Missing card or invalid parameters : " + key;
|
---|
193 | rc = 73; return(rc);
|
---|
194 | }
|
---|
195 |
|
---|
196 | // Initialiazing parameters
|
---|
197 | rc = 0;
|
---|
198 | msg = "OK";
|
---|
199 | nskycomp = ncomp;
|
---|
200 | hp_nside = mg;
|
---|
201 |
|
---|
202 | // Checking for PATH definition card
|
---|
203 | key = "MAPPATH";
|
---|
204 | if (dc.NbParam(key) < 3) strncpy(mapPath, dc.SParam(key, 0).c_str(), 255);
|
---|
205 | mapPath[255] = '\0';
|
---|
206 | key = "DEBUGLEVEL";
|
---|
207 | debuglev = dc.IParam(key, 0, 0);
|
---|
208 | return(rc);
|
---|
209 | }
|
---|
210 |
|
---|
211 | static char buff_flnm[1024]; // Mal protege !
|
---|
212 | /* Nouvelle-Fonction */
|
---|
213 | char* BuildFITSFileName(string const & fname)
|
---|
214 | {
|
---|
215 | if (mapPath[0] != '\0') sprintf(buff_flnm, "%s/%s", mapPath, fname.c_str());
|
---|
216 | else sprintf(buff_flnm, "%s", fname.c_str());
|
---|
217 | return(buff_flnm);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Nouvelle-Fonction */
|
---|
221 | // template <class T>
|
---|
222 | void addComponent(SpectralResponse& sr, PixelMap<float>& finalMap,
|
---|
223 | PixelMap<float>& mapToAdd, RadSpectra& rs, double K)
|
---|
224 | {
|
---|
225 | // finalMap = finalMap + coeff* mapToAdd
|
---|
226 | // coeff = convolution of sr and rs
|
---|
227 | // compute the coefficient corresponding to mapToAdd
|
---|
228 | if (finalMap.NbPixels() != mapToAdd.NbPixels())
|
---|
229 | throw SzMismatchError("addComponent()/Error: Unequal number of Input/Output map pixels");
|
---|
230 | double coeff = rs.filteredIntegratedFlux(sr) * K;
|
---|
231 | for(int i=0; i<finalMap.NbPixels(); i++)
|
---|
232 | {
|
---|
233 | finalMap(i) += coeff * mapToAdd(i);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | /* Nouvelle-Fonction */
|
---|
238 | void addComponent(SpectralResponse& sr, PixelMap<double>& finalMap,
|
---|
239 | PixelMap<double>& mapToAdd, RadSpectra& rs, double K)
|
---|
240 | {
|
---|
241 | // finalMap = finalMap + coeff* mapToAdd
|
---|
242 | // coeff = convolution of sr and rs
|
---|
243 | // compute the coefficient corresponding to mapToAdd
|
---|
244 | if (finalMap.NbPixels() != mapToAdd.NbPixels())
|
---|
245 | throw SzMismatchError("addComponent()/Error: Unequal number of Input/Output map pixels");
|
---|
246 | double coeff = rs.filteredIntegratedFlux(sr) * K;
|
---|
247 | for(int i=0; i<finalMap.NbPixels(); i++)
|
---|
248 | {
|
---|
249 | finalMap(i) += coeff * mapToAdd(i);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | /* Nouvelle-Fonction */
|
---|
255 | void RadSpec2Nt(RadSpectra & rs, POutPersist & so, string name)
|
---|
256 | {
|
---|
257 | char *ntn[2] = {"nu","fnu"};
|
---|
258 | NTuple nt(2,ntn); // Creation NTuple (AVEC new )
|
---|
259 | float xnt[2];
|
---|
260 | double nu;
|
---|
261 | double numin = rs.minFreq();
|
---|
262 | double numax = rs.maxFreq();
|
---|
263 | int nmax = 500;
|
---|
264 | double dnu = (numax-numin)/nmax;
|
---|
265 | for(int k=0; k<nmax; k++) {
|
---|
266 | nu = numin+k*dnu;
|
---|
267 | xnt[0] = nu;
|
---|
268 | xnt[1] = rs.flux(nu);
|
---|
269 | nt.Fill(xnt);
|
---|
270 | }
|
---|
271 | ObjFileIO<NTuple> oiont(nt);
|
---|
272 | oiont.Write(so, name);
|
---|
273 | return;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* Nouvelle-Fonction */
|
---|
277 | void SpectralResponse2Nt(SpectralResponse& sr, POutPersist & so, string name)
|
---|
278 | {
|
---|
279 | char *ntn[2] = {"nu","tnu"};
|
---|
280 | NTuple nt(2,ntn); // Creation NTuple (AVEC new )
|
---|
281 | float xnt[2];
|
---|
282 | double nu;
|
---|
283 | double numin = sr.minFreq();
|
---|
284 | double numax = sr.maxFreq();
|
---|
285 | int nmax = 500;
|
---|
286 | double dnu = (numax-numin)/nmax;
|
---|
287 | for(int k=0; k<nmax; k++) {
|
---|
288 | nu = numin+k*dnu;
|
---|
289 | xnt[0] = nu;
|
---|
290 | xnt[1] = sr.transmission(nu);
|
---|
291 | nt.Fill(xnt);
|
---|
292 | }
|
---|
293 | ObjFileIO<NTuple> oiont(nt);
|
---|
294 | oiont.Write(so, name);
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|