source: Sophya/trunk/SophyaPI/ProgPI/skymapmodule.cc@ 2155

Last change on this file since 2155 was 2155, checked in by cmv, 23 years ago

module skymap cmv 4/8/2002

File size: 13.3 KB
RevLine 
[2155]1#include "machdefs.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <iostream.h>
5#include <math.h>
6
7#include <typeinfo>
8#include <vector>
9#include <string>
10
11#include "piacmd.h"
12#include "nobjmgr.h"
13#include "pistdimgapp.h"
14#include "servnobjm.h"
15#include "tvector.h"
16#include "pidrawer.h"
17#include "nomgadapter.h"
18
19#include "skymap.h"
20#include "sphericaltransformserver.h"
21
22extern "C" {
23 void skymapmodule_init();
24 void skymapmodule_end();
25}
26
27class skymapmoduleExecutor : public CmdExecutor {
28public:
29 skymapmoduleExecutor();
30 virtual ~skymapmoduleExecutor();
31 virtual int Execute(string& keyw, vector<string>& args, string& toks);
32 void Map2Double(vector<string>& tokens);
33 void MapMult(vector<string>& tokens);
34 void Map2Cl(vector<string>& tokens);
35 void CrMapMask(vector<string>& tokens);
36 void CrMaskFrMap(vector<string>& tokens);
37 void MaskMap(vector<string>& tokens);
38 void MapProj(vector<string>& tokens);
39 void MapOper(vector<string>& tokens);
40};
41
42/* --Methode-- */
43skymapmoduleExecutor::skymapmoduleExecutor()
44{
45PIACmd * mpiac;
46NamedObjMgr omg;
47mpiac = omg.GetImgApp()->CmdInterpreter();
48
49string hgrp = "SkyMapCmd";
50
51string kw = "map2double";
52string usage = "Convert a float map to a double map";
53usage += "\n Usage: map2double map";
54mpiac->RegisterCommand(kw, usage, this, hgrp);
55
56kw = "mapmult";
57usage = "Multiply a map by a constant";
58usage += "\n Usage: mapmult map cste";
59mpiac->RegisterCommand(kw, usage, this, hgrp);
60
61kw = "map2cl";
62usage = "SkyMap to Cl";
63usage += "\n Usage: map2cl map(double) clvec [lmax] [niter]";
64mpiac->RegisterCommand(kw, usage, this, hgrp);
65
66kw = "crmapmask";
67usage = "Create a map mask (nside) between latitudes lat1 lat2";
68usage += "\n Usage: crmapmask mapmsk nside lat1 lat2 [valmsk=0] [valnomsk=1]";
69usage += "\n mask pixels between [lat1,lat2] ([-90,90] in degrees)";
70usage += "\n valmsk=value to be written into masked pixels";
71usage += "\n valnomsk=value to be written into unmasked pixels";
72mpiac->RegisterCommand(kw, usage, this, hgrp);
73
74kw = "crmaskfrmap";
75usage = "Create a map mask (nside) relative to an other map pixels values";
76usage += "\n Usage: crmaskfrmap mapmsk nside map(double) v1 v2 [valmsk=0] [valnomsk=1]";
77usage += "\n mask if v1<mapmsk(i)<v2";
78usage += "\n valmsk=value to be written into masked pixels";
79usage += "\n valnomsk=value to be written into unmasked pixels";
80mpiac->RegisterCommand(kw, usage, this, hgrp);
81
82kw = "maskmap";
83usage = "Mask a map with a mask map";
84usage += "\n Usage: maskmap map msk";
85usage += "\n operation is map(i) *= msk(theta,phi)";
86mpiac->RegisterCommand(kw, usage, this, hgrp);
87
88kw = "maproj";
89usage = "Project a map into an other one";
90usage += "\n Usage: maproj map mapproj [nside]";
91usage += "\n Create mapproj(nside) and project map into mapproj";
92usage += "\n (use \"maproj map mapproj\" to make a copy)";
93mpiac->RegisterCommand(kw, usage, this, hgrp);
94
95kw = "mapop";
96usage = "operation on a map";
97usage += "\n Usage: mapop map op1 map1 [op2 map2] [op2 map3] [...]";
98usage += "\n Do \"map op1= map1\", \"map op2=map2\", ...";
99usage += "\n where op = +,-,*,/";
100mpiac->RegisterCommand(kw, usage, this, hgrp);
101
102}
103
104/* --Methode-- */
105skymapmoduleExecutor::~skymapmoduleExecutor()
106{
107}
108
109/* --Methode-- */
110int skymapmoduleExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
111{
112
113if(kw == "map2double") {
114 if(tokens.size()<1) {
115 cout<<"Usage: map2double map"<<endl;
116 return(0);
117 }
118 Map2Double(tokens);
119} else if(kw == "mapmult") {
120 if(tokens.size()<2) {
121 cout<<"Usage: mapmult map cste"<<endl;
122 return(0);
123 }
124 MapMult(tokens);
125} else if(kw == "map2cl") {
126 if(tokens.size()<2) {
127 cout<<"Usage: map2cl map clvec [lmax] [niter]"<<endl;
128 return(0);
129 }
130 Map2Cl(tokens);
131} else if(kw == "crmapmask") {
132 if(tokens.size()<2) {
133 cout<<"Usage: crmapmask mapmsk nside lat1 lat2 [valmsk=0] [valnomsk=1]"<<endl;
134 return(0);
135 }
136 CrMapMask(tokens);
137} else if(kw == "crmaskfrmap") {
138 if(tokens.size()<3) {
139 cout<<"Usage: crmaskfrmap mapmsk nside map(double) v1 v2 [valmsk=0] [valnomsk=1]"<<endl;
140 return(0);
141 }
142 CrMaskFrMap(tokens);
143} else if(kw == "maskmap") {
144 if(tokens.size()<2) {
145 cout<<"Usage: maskmap map msk"<<endl;
146 return(0);
147 }
148 MaskMap(tokens);
149} else if(kw == "maproj") {
150 if(tokens.size()<2) {
151 cout<<"Usage: maproj map mapproj [nside]"<<endl;
152 return(0);
153 }
154 MapProj(tokens);
155} else if(kw == "mapop") {
156 if(tokens.size()<3) {
157 cout<<"Usage: mapop map op map1 [op map2] [op map3] [...]"<<endl;
158 return(0);
159 }
160 MapOper(tokens);
161}
162
163return(0);
164}
165
166/* --Methode-- */
167void skymapmoduleExecutor::Map2Double(vector<string>& tokens)
168{
169 NamedObjMgr omg;
170 AnyDataObj* obj=omg.GetObj(tokens[0]);
171 if(obj==NULL) {
172 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
173 return;
174 }
175 SphereHEALPix<r_4>* map = dynamic_cast<SphereHEALPix<r_4>*>(obj);
176 if(map==NULL) {
177 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_4>"<<endl;
178 return;
179 }
180 int_4 nside = map->SizeIndex();
181 SphereHEALPix<r_8>* map8 = new SphereHEALPix<r_8>(nside);
182 cout<<"Map2Double: converting to double "<<tokens[0]<<endl;
183 for(int_4 i=0;i<map8->NbPixels();i++) (*map8)(i) = (r_8) (*map)(i);
184 omg.AddObj(map8,tokens[0]);
185}
186
187/* --Methode-- */
188void skymapmoduleExecutor::MapMult(vector<string>& tokens)
189{
190 NamedObjMgr omg;
191 AnyDataObj* obj=omg.GetObj(tokens[0]);
192 if(obj==NULL) {
193 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
194 return;
195 }
196 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
197 if(map==NULL) {
198 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
199 return;
200 }
201 double v = atof(tokens[1].c_str());
202 cout<<"MapMult: Multiply "<<tokens[0]<<" by "<<v<<endl;
203 for(int_4 i=0;i<map->NbPixels();i++) (*map)(i) *= v;
204}
205
206/* --Methode-- */
207void skymapmoduleExecutor::Map2Cl(vector<string>& tokens)
208{
209 NamedObjMgr omg;
210
211 int_4 lmax=0, niter=3;
212 if(tokens.size()>2) lmax = atoi(tokens[2].c_str());
213 if(tokens.size()>3) niter = atoi(tokens[3].c_str());
214 if(niter<=0) niter = 3;
215
216 AnyDataObj* obj=omg.GetObj(tokens[0]);
217 if(obj==NULL) {
218 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
219 return;
220 }
221 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
222 if(map==NULL) {
223 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
224 return;
225 }
226
227 SphericalTransformServer<r_8> ylmserver;
228
229 int_4 nside = map->SizeIndex();
230 if(lmax<=0) lmax = 3*nside-1;
231
232 cout<<"Map2Cl: "<<tokens[0]<<" -> "<<tokens[1]
233 <<" lmax="<<lmax<<" niter="<<niter<<endl;
234
235 SphereHEALPix<r_8> tmpmap(*map,false);
236 TVector<r_8> cltmp = ylmserver.DecomposeToCl(tmpmap,lmax,0.,niter);
237
238 TVector<r_8>* cl = new TVector<r_8>(cltmp,false);
239 omg.AddObj(cl,tokens[1]);
240}
241
242/* --Methode-- */
243void skymapmoduleExecutor::CrMapMask(vector<string>& tokens)
244{
245 NamedObjMgr omg;
246
247 int_4 nside = atoi(tokens[1].c_str());
248 if(nside<=1) return;
249 int_4 n=nside; bool ok=true;
250 while(n>1) {if(n%2!=0) {ok=false; break;} else n/=2;}
251 if(!ok) {cout<<"CrMapMask: Bad nside "<<nside<<endl; return;}
252
253 SphereHEALPix<r_8>* map = new SphereHEALPix<r_8>(nside);
254
255 double lat1=-1.e30, lat2=1.e30;
256 if(tokens.size()>2) lat1=atof(tokens[2].c_str());
257 if(tokens.size()>3) lat2=atof(tokens[3].c_str());
258
259 double valmsk=0., unvalmsk=1.;
260 if(tokens.size()>4) valmsk=atof(tokens[4].c_str());
261 if(tokens.size()>5) unvalmsk=atof(tokens[5].c_str());
262
263 cout<<"CrMapMask "<<tokens[0]<<" nside="<<nside
264 <<" for latitude in ["<<lat1<<","<<lat2<<"]"
265 <<" valmsk="<<valmsk<<" unvalmsk="<<unvalmsk<<endl;
266
267 lat1 = (90.-lat1)*M_PI/180.; lat2 = (90.-lat2)*M_PI/180.;
268 for(int_4 i=0;i<map->NbPixels();i++) {
269 double theta,phi;
270 map->PixThetaPhi(i,theta,phi);
271 if(theta<lat1 && theta>lat2) (*map)(i)=valmsk;
272 else (*map)(i)=unvalmsk;
273 }
274
275 omg.AddObj(map,tokens[0]);
276}
277
278/* --Methode-- */
279void skymapmoduleExecutor::CrMaskFrMap(vector<string>& tokens)
280{
281 NamedObjMgr omg;
282
283 int_4 nside = atoi(tokens[1].c_str());
284 if(nside<=1) return;
285 int_4 n=nside; bool ok=true;
286 while(n>1) {if(n%2!=0) {ok=false; break;} else n/=2;}
287 if(!ok) {cout<<"CrMapMask: Bad nside "<<nside<<endl; return;}
288
289 AnyDataObj* obj=omg.GetObj(tokens[2]);
290 if(obj==NULL) {
291 cout<<"Error: Pas d'objet de nom "<<tokens[1]<<endl;
292 return;
293 }
294 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
295 if(map==NULL) {
296 cout<<"Error: "<<tokens[1]<<" is not a SphereHEALPix<r_8>"<<endl;
297 return;
298 }
299
300 SphereHEALPix<r_8>* msk = new SphereHEALPix<r_8>(nside);
301
302 double v1=-1.e50, v2=1.e50;
303 if(tokens.size()>3) v1 = atof(tokens[3].c_str());
304 if(tokens.size()>4) v2 = atof(tokens[4].c_str());
305
306 double valmsk=0., unvalmsk=1.;
307 if(tokens.size()>5) valmsk=atof(tokens[5].c_str());
308 if(tokens.size()>6) unvalmsk=atof(tokens[6].c_str());
309
310 cout<<"CrMaskFrMap "<<tokens[0]<<" nside"<<nside<<" with "<<tokens[2]<<endl
311 <<" for value in ["<<v1<<","<<v2<<"]"
312 <<" valmsk="<<valmsk<<" unvalmsk="<<unvalmsk<<endl;
313
314 for(int_4 i=0;i<msk->NbPixels();i++) {
315 double theta,phi;
316 msk->PixThetaPhi(i,theta,phi);
317 int_4 ip = map->PixIndexSph(theta,phi);
318 if(ip<0 || ip>=map->NbPixels()) continue;
319 double v = (*map)(ip);
320 if(v>v1 && v<v2) (*msk)(i)=valmsk;
321 else (*msk)(i)=unvalmsk;
322 }
323
324 omg.AddObj(msk,tokens[0]);
325}
326
327/* --Methode-- */
328void skymapmoduleExecutor::MaskMap(vector<string>& tokens)
329{
330 NamedObjMgr omg;
331
332 AnyDataObj* obj=omg.GetObj(tokens[0]);
333 if(obj==NULL) {
334 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
335 return;
336 }
337 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
338 if(map==NULL) {
339 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
340 return;
341 }
342
343 AnyDataObj* objm=omg.GetObj(tokens[1]);
344 if(obj==NULL) {
345 cout<<"Error: Pas d'objet de nom "<<tokens[1]<<endl;
346 return;
347 }
348 SphereHEALPix<r_8>* msk = dynamic_cast<SphereHEALPix<r_8>*>(objm);
349 if(msk==NULL) {
350 cout<<"Error: "<<tokens[1]<<" is not a SphereHEALPix<r_8>"<<endl;
351 return;
352 }
353
354 cout<<"MskMap: "<<tokens[0]<<" with mask "<<tokens[1]<<endl;
355
356 for(int_4 i=0;i<map->NbPixels();i++) {
357 double theta,phi;
358 map->PixThetaPhi(i,theta,phi);
359 int_4 ip = msk->PixIndexSph(theta,phi);
360 if(ip<0 || ip>=msk->NbPixels()) continue;
361 (*map)(i) *= (*msk)(ip);
362 }
363
364}
365
366/* --Methode-- */
367void skymapmoduleExecutor::MapProj(vector<string>& tokens)
368{
369 NamedObjMgr omg;
370
371 AnyDataObj* obj=omg.GetObj(tokens[0]);
372 if(obj==NULL) {
373 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
374 return;
375 }
376 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
377 if(map==NULL) {
378 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
379 return;
380 }
381 int_4 nsidemap = map->SizeIndex();
382
383 int_4 nside = nsidemap;
384 if(tokens.size()>2) nside = atoi(tokens[2].c_str());
385 if(nside<=1) return;
386 int_4 n=nside; bool ok=true;
387 while(n>1) {if(n%2!=0) {ok=false; break;} else n/=2;}
388 if(!ok) {cout<<"MapProj: Bad nside "<<nside<<endl; return;}
389
390 SphereHEALPix<r_8>* mapproj = new SphereHEALPix<r_8>(nside);
391
392 if(nsidemap==nside) { // tailles egales
393 for(int_4 i=0;i<mapproj->NbPixels();i++)
394 (*mapproj)(i) = (*map)(i);
395 } else if(nsidemap<nside) { // mapproj>map
396 for(int_4 i=0;i<mapproj->NbPixels();i++) {
397 double theta,phi;
398 mapproj->PixThetaPhi(i,theta,phi);
399 int_4 ip = map->PixIndexSph(theta,phi);
400 if(ip<0 || ip>=map->NbPixels()) continue;
401 (*mapproj)(i) = (*map)(ip);
402 }
403 } else { // mapproj<map
404 SphereHEALPix<int_4> nproj(nside);
405 nproj = 0; *mapproj = 0.;
406 for(int_4 i=0;i<map->NbPixels();i++) {
407 double theta,phi;
408 map->PixThetaPhi(i,theta,phi);
409 int_4 ip = mapproj->PixIndexSph(theta,phi);
410 if(ip<0 || ip>=mapproj->NbPixels()) continue;
411 (*mapproj)(ip) += (*map)(i);
412 nproj(ip)++;
413 }
414 for(int_4 i=0;i<mapproj->NbPixels();i++)
415 (*mapproj)(i) /= (r_8) nproj(i);
416 }
417
418 omg.AddObj(mapproj,tokens[1]);
419}
420
421/* --Methode-- */
422void skymapmoduleExecutor::MapOper(vector<string>& tokens)
423{
424 NamedObjMgr omg;
425
426 AnyDataObj* obj=omg.GetObj(tokens[0]);
427 if(obj==NULL) {
428 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
429 return;
430 }
431 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
432 if(map==NULL) {
433 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
434 return;
435 }
436
437 cout<<"MapOper: "<<tokens[0];
438 for(unsigned short iop=1;iop<tokens.size()-1;iop+=2) {
439 char op = tokens[iop][0];
440 if(op!='+' && op!='-' && op!='*' && op!='/') continue;
441 AnyDataObj* obj1=omg.GetObj(tokens[iop+1]);
442 if(obj1==NULL)
443 {cout<<"Error: Pas d'objet de nom "<<tokens[iop+1]<<endl;
444 continue;}
445 SphereHEALPix<r_8>* map1 = dynamic_cast<SphereHEALPix<r_8>*>(obj1);
446 if(map1==NULL)
447 {cout<<"Error: "<<tokens[iop+1]<<" is not a SphereHEALPix<r_8>"<<endl;
448 continue;}
449 cout<<" "<<op<<"= "<<tokens[iop+1];
450 for(int_4 i=0;i<map->NbPixels();i++) {
451 double theta,phi;
452 map->PixThetaPhi(i,theta,phi);
453 int_4 ip = map1->PixIndexSph(theta,phi);
454 if(ip<0 || ip>=map1->NbPixels()) continue;
455 if(op=='+') (*map)(i) += (*map1)(ip);
456 else if(op=='-') (*map)(i) -= (*map1)(ip);
457 else if(op=='*') (*map)(i) *= (*map1)(ip);
458 else if(op=='/')
459 {if((*map1)(ip)!=0.) (*map)(i) /= (*map1)(ip);
460 else (*map)(i) = 0.;}
461 }
462 }
463 cout<<endl;
464}
465
466////////////////////////////////////////////////////////////
467static skymapmoduleExecutor * piaskmex = NULL;
468
469void skymapmodule_init()
470{
471if (piaskmex) delete piaskmex;
472piaskmex = new skymapmoduleExecutor;
473}
474
475void skymapmodule_end()
476{
477// Desactivation du module
478if (piaskmex) delete piaskmex;
479piaskmex = NULL;
480}
481
482////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.