source: Sophya/trunk/ArchTOIPipe/TestPipes/quickmap.cc@ 1803

Last change on this file since 1803 was 1800, checked in by aubourg, 24 years ago

GPH 424.1

File size: 10.9 KB
Line 
1// GPH 424.1 Planck HFI-L2 Simple Map Making
2// Eric Aubourg CEA/DAPNIA/SPP
3// $Id: quickmap.cc,v 1.1 2001-11-29 18:19:32 aubourg Exp $
4
5#include <stdlib.h>
6#include <stdio.h>
7#include "toi.h"
8#include "toiprocessor.h"
9#include "fitstoirdr.h"
10#include "fitstoiwtr.h"
11#include "toimanager.h"
12#include "toisegment.h"
13
14#include "sambainit.h"
15#include "toi2gmap.h"
16#include "fitsspherehealpix.h"
17#include "localmap.h"
18#include "fitslocalmap.h"
19
20#include <util/PlatformUtils.hpp>
21#include <sax/SAXException.hpp>
22#include <sax/SAXParseException.hpp>
23#include <parsers/DOMParser.hpp>
24#include <dom/DOM_DOMException.hpp>
25
26
27void usage(void);
28
29void usage(void) {
30 cerr << "usage: gph424_1g xml_options_file" << endl;
31 exit(-1);
32}
33
34// Prefs structure
35
36struct Gph424_1_Prefs {
37 struct {
38 struct {
39 string fname;
40 string coord1; // TOI name
41 string coord2; // TOI name
42 unsigned long typcoord;
43 } pointing;
44 struct {
45 string fname;
46 string toiname;
47 } signal;
48 } input;
49
50 struct {
51 string fname;
52 string wfname; // weight
53 unsigned long typcoord;
54 string typmap; // local, healpix...
55 struct {
56 int nlat;
57 } healpix_opt;
58 struct {
59 int npx, npy;
60 double org1,org2;
61 double anglex, angley;
62 } local_opt;
63 } output;
64
65 int nproc;
66 long snb, sne;
67
68 void dump() {
69 cout << "*** GPH424-1 settings\n";
70 cout << " input\n";
71 cout << " | pointing\n";
72 cout << " | | fname " << input.pointing.fname << "\n";
73 cout << " | | coord1 " << input.pointing.coord1 << "\n";
74 cout << " | | coord2 " << input.pointing.coord2 << "\n";
75 cout << " | | typcoord "
76 << ((input.pointing.typcoord & TypCoordGal) ? "G" : "E")
77 << ((input.pointing.typcoord & TypCoordHD) ? "H" :
78 (input.pointing.typcoord & TypCoordDD) ? "D" : "R") << "\n";
79 cout << " | signal\n";
80 cout << " | | fname " << input.signal.fname << "\n";
81 cout << " | | toiname " << input.signal.toiname << "\n";
82 cout << " output\n";
83 cout << " | fname " << output.fname << "\n";
84 cout << " | wfname " << output.wfname << "\n";
85 cout << " | typcoord "
86 << ((output.typcoord & TypCoordGal) ? "G" : "E")
87 << ((output.typcoord & TypCoordHD) ? "H" :
88 (input.pointing.typcoord & TypCoordDD) ? "D" : "R") << "\n";
89 cout << " | typmap " << output.typmap << "\n";
90 if (output.typmap == "healpix") {
91 cout << " | | nlat " << output.healpix_opt.nlat << "\n";
92 } else if (output.typmap == "local") {
93 cout << " | | npix " << output.local_opt.npx << ", " << output.local_opt.npy << "\n";
94 cout << " | | origin " << output.local_opt.org1 << ", " << output.local_opt.org2 << "\n";
95 cout << " | | extension " << output.local_opt.anglex << ", " << output.local_opt.angley << "\n";
96 }
97 cout << " nproc " << nproc << "\n";
98 if (sne >= 0 && snb >= 0) {
99 cout << " samples " << snb << " " << sne << "\n";
100 }
101 cout << "***" << endl;
102 }
103};
104
105DOM_Node DOMGetNode(DOM_Document& doc, string path) {
106 DOM_Element el = doc.getDocumentElement();
107
108 while (path != "") {
109 if (path[0] == '/') {
110 path = path.substr(1);
111 }
112 if (path == "") break;
113 int x = path.find('/');
114 string pathElem;
115 if (x == -1) {
116 pathElem = path;
117 path = "";
118 } else {
119 pathElem = path.substr(0, x);
120 path = path.substr(x);
121 }
122 DOM_Node n = el.getElementsByTagName(pathElem.c_str()).item(0);
123 if (n==0) return n;
124 el = (DOM_Element&) n;
125 }
126 return el;
127}
128
129string DOMGetString(DOM_Document& doc, string path, string def="") {
130 DOM_Node n = DOMGetNode(doc, path);
131 if (n==0) return def;
132 char* s = n.getFirstChild().getNodeValue().transcode();
133 char* p=s; while ((*p)==' ') p++;
134 char* q=p+strlen(p)-1;
135 while (q>p && *q==' ') {
136 *q = '\0';
137 q--;
138 }
139 string ss = p;
140 free(s);
141 return ss;
142}
143
144bool DOMHasOption(DOM_Document& doc, string path) {
145 DOM_Node n = DOMGetNode(doc, path);
146 return n != 0;
147}
148
149void parsePrefs(string prefFile, Gph424_1_Prefs& prefs) {
150 try {
151 XMLPlatformUtils::Initialize();
152 } catch (const XMLException& toCatch) {
153 cerr << "Error during XML initialization! :\n"
154 << toCatch.getMessage() << endl;
155 exit(-1);
156 }
157
158 DOMParser* parser = new DOMParser;
159
160 try {
161 parser->parse(prefFile.c_str());
162 } catch (const XMLException& toCatch) {
163 cerr << "\nError during parsing: '" << prefFile << "'\n"
164 << "Exception message is: \n"
165 << toCatch.getMessage() << "\n" << endl;
166 exit(-1);
167 } catch (const DOM_DOMException& toCatch) {
168 cerr << "\nDOM Error during parsing: '" << prefFile << "'\n"
169 << "DOMException code is: \n"
170 << toCatch.code << "\n" << endl;
171 exit(-1);
172 } catch (...) {
173 cerr << "\nUnexpected exception during parsing: '" << prefFile << "'\n";
174 exit(-1);
175 }
176
177 DOM_Document doc = parser->getDocument();
178
179 prefs.input.pointing.fname = DOMGetString(doc, "/input/pointing/fname");
180 prefs.input.pointing.coord1 = DOMGetString(doc, "/input/pointing/coord1","phi");
181 prefs.input.pointing.coord2 = DOMGetString(doc, "/input/pointing/coord2","theta");
182 bool inequ = DOMHasOption(doc, "/input/pointing/equatorial");
183 bool ingal = DOMHasOption(doc, "/input/pointing/galactic");
184 prefs.input.pointing.typcoord = ingal ? TypCoordGal : TypCoordEq;
185 bool inhd = DOMHasOption(doc, "/input/pointing/hourdeg");
186 bool indd = DOMHasOption(doc, "/input/pointing/degdeg");
187 bool inrr = DOMHasOption(doc, "/input/pointing/radian");
188 if (inhd) {
189 prefs.input.pointing.typcoord |= TypCoordHD;
190 } else if (indd) {
191 prefs.input.pointing.typcoord |= TypCoordDD;
192 } else if (inrr) {
193 prefs.input.pointing.typcoord |= TypCoordRR;
194 } else if (prefs.input.pointing.typcoord & TypCoordEq) {
195 prefs.input.pointing.typcoord |= TypCoordHD;
196 } else {
197 prefs.input.pointing.typcoord |= TypCoordDD;
198 }
199
200 prefs.input.signal.fname = DOMGetString(doc, "/input/signal/fname");
201 prefs.input.signal.toiname = DOMGetString(doc, "/input/signal/toiname");
202
203 prefs.output.fname = DOMGetString(doc, "/output/fname");
204 prefs.output.wfname = DOMGetString(doc, "/output/wfname");
205 bool outequ = DOMHasOption(doc, "/output/equatorial");
206 bool outgal = DOMHasOption(doc, "/output/galactic");
207 prefs.output.typcoord = outgal ? TypCoordGal : TypCoordEq;
208 bool outhd = DOMHasOption(doc, "/output/hourdeg");
209 bool outdd = DOMHasOption(doc, "/output/degdeg");
210 bool outrr = DOMHasOption(doc, "/output/radian");
211 if (outhd) {
212 prefs.output.typcoord |= TypCoordHD;
213 } else if (outdd) {
214 prefs.output.typcoord |= TypCoordDD;
215 } else if (outrr) {
216 prefs.output.typcoord |= TypCoordRR;
217 } else if (prefs.output.typcoord & TypCoordEq) {
218 prefs.output.typcoord |= TypCoordHD;
219 } else {
220 prefs.output.typcoord |= TypCoordDD;
221 }
222
223 prefs.output.typmap = DOMHasOption(doc, "/output/local") ? "local" :
224 (DOMHasOption(doc, "/output/healpix") ? "healpix" : "healpix");
225 prefs.output.healpix_opt.nlat = atoi(DOMGetString(doc, "/output/healpix/nlat", "128").c_str());
226
227 string s = DOMGetString(doc, "/output/local/npx");
228 prefs.output.local_opt.npx = prefs.output.local_opt.npy = -1;
229 int nc = sscanf(s.c_str(), "%d %d", &prefs.output.local_opt.npx, &prefs.output.local_opt.npy);
230 if (nc == 1) prefs.output.local_opt.npy = prefs.output.local_opt.npx;
231
232 s = DOMGetString(doc, "/output/local/center");
233 prefs.output.local_opt.org1 = prefs.output.local_opt.org2 = 0;
234 sscanf(s.c_str(), "%lg %lg", &prefs.output.local_opt.org1, &prefs.output.local_opt.org2);
235
236 s = DOMGetString(doc, "/output/local/angle");
237 prefs.output.local_opt.anglex = prefs.output.local_opt.angley = 0;
238 nc = sscanf(s.c_str(), "%d %d", &prefs.output.local_opt.anglex, &prefs.output.local_opt.angley);
239 if (nc == 1) prefs.output.local_opt.angley = prefs.output.local_opt.anglex;
240
241 prefs.nproc = atoi(DOMGetString(doc, "/nproc", "1").c_str());
242
243 s = DOMGetString(doc, "/samples", "-1 -1");
244 sscanf(s.c_str(), "%ld %ld", &prefs.snb, &prefs.sne);
245}
246
247int main(int argc, char** argv) {
248
249 // Read prefs
250 Gph424_1_Prefs prefs;
251 parsePrefs(argv[1], prefs);
252 prefs.dump();
253
254 // Assemble pipeline
255
256 TOIManager* mgr = TOIManager::getManager();
257
258 // Two input files
259
260 FITSTOIReader inPoint(prefs.input.pointing.fname);
261 FITSTOIReader inSig (prefs.input.signal.fname);
262
263 inPoint.setImplicitSN();
264 inSig.setImplicitSN();
265
266 // Prepare maps for output
267
268 PixelMap<r_8>* map;
269 PixelMap<r_8>* wmap;
270
271 if (prefs.output.typmap == "local") {
272 LocalMap<r_8>* lmap = new LocalMap<r_8>(prefs.output.local_opt.npx, prefs.output.local_opt.npy);
273 lmap->SetOrigin(prefs.output.local_opt.org1, prefs.output.local_opt.org2);
274 lmap->SetSize(prefs.output.local_opt.anglex, prefs.output.local_opt.angley);
275 map = lmap;
276 wmap = new LocalMap<r_8>(*lmap);
277 } else if (prefs.output.typmap == "healpix") {
278 map = new SphereHEALPix<r_8>(prefs.output.healpix_opt.nlat);
279 wmap = new SphereHEALPix<r_8>(prefs.output.healpix_opt.nlat);
280 } else {
281 cout << "typmap " << prefs.output.typmap << " unknown" << endl;
282 exit(-1);
283 }
284
285 TOI2GMap toi2m(map,wmap); toi2m.SetIsColat();
286
287 toi2m.SetEquinox(2000.);
288 toi2m.SetCoorIn((TypAstroCoord) prefs.input.pointing.typcoord);
289 toi2m.SetCoorOut((TypAstroCoord) prefs.output.typcoord);
290
291 TOISegmented * toicoord1in = new TOISegmented(prefs.input.pointing.coord1);
292 inPoint.addOutput(prefs.input.pointing.coord1,toicoord1in);
293 toi2m.addInput("Coord1In",toicoord1in);
294
295 TOISegmented * toicoord2in = new TOISegmented(prefs.input.pointing.coord2);
296 inPoint.addOutput(prefs.input.pointing.coord2,toicoord2in);
297 toi2m.addInput("Coord2In",toicoord2in);
298
299 TOISegmented * toibolin = new TOISegmented("toi_bolo_in");
300 inSig.addOutput(prefs.input.signal.toiname,toibolin);
301 toi2m.addInput("BoloIn",toibolin);
302
303 // Start processing
304
305 if (prefs.snb >= 0 && prefs.sne >= 0) {
306 mgr->setRequestedSample(prefs.snb, prefs.sne);
307 }
308
309 inPoint.start();
310 inSig.start();
311 toi2m.start();
312 mgr->joinAll();
313
314 // Save maps
315
316 {
317 FitsOutFile sfits(prefs.output.fname,FitsFile::clear);
318 cout<<"Creating map fits file "<<prefs.output.fname<<endl;
319 // Would need some generic mechanism here $CHECK$ $$TBD$$
320 if (map->TypeOfMap() == "RING") {
321 sfits << * (SphereHEALPix<r_8>*)map;
322 } else if (map->TypeOfMap() == "LOCAL") {
323 sfits << * (LocalMap<r_8>*)map;
324 } else {
325 cout << "error, unknown type " << map->TypeOfMap() << endl;
326 }
327 }
328
329 {
330 FitsOutFile swfits(prefs.output.wfname,FitsFile::clear);
331 cout<<"Creating map weight fits file "<<prefs.output.wfname<<endl;
332 // swfits << *wmap;
333 if (wmap->TypeOfMap() == "RING") {
334 swfits << * (SphereHEALPix<r_8>*)wmap;
335 } else if (wmap->TypeOfMap() == "LOCAL") {
336 swfits << * (LocalMap<r_8>*)wmap;
337 } else {
338 cout << "error, unknown type " << wmap->TypeOfMap() << endl;
339 }
340 }
341
342 delete map;
343 delete wmap;
344
345 return 0;
346}
Note: See TracBrowser for help on using the repository browser.