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

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

more methods cmv 6/8/02

File size: 25.6 KB
Line 
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
22static bool IsNSideGood(int_4 nside) {
23 if(nside<=1) return false;
24 while(nside>1) {if(nside%2!=0) return false; else nside/=2;}
25 return true;
26}
27
28extern "C" {
29 void skymapmodule_init();
30 void skymapmodule_end();
31}
32
33class skymapmoduleExecutor : public CmdExecutor {
34public:
35 skymapmoduleExecutor();
36 virtual ~skymapmoduleExecutor();
37 virtual int Execute(string& keyw, vector<string>& args, string& toks);
38 void Map2Double(vector<string>& tokens);
39 void MapMult(vector<string>& tokens);
40 void MapCover(vector<string>& tokens);
41 void Map2Cl(vector<string>& tokens);
42 void Cl2Map(vector<string>& tokens);
43 void Map2Alm(vector<string>& tokens);
44 void Alm2Map(vector<string>& tokens);
45 void CrMapMask(vector<string>& tokens);
46 void CrMaskFrMap(vector<string>& tokens);
47 void MaskMap(vector<string>& tokens);
48 void MapProj(vector<string>& tokens);
49 void Map2Local(vector<string>& tokens);
50 void MapOper(vector<string>& tokens);
51 void MapStat(vector<string>& tokens);
52};
53
54/* --Methode-- */
55skymapmoduleExecutor::skymapmoduleExecutor()
56{
57PIACmd * mpiac;
58NamedObjMgr omg;
59mpiac = omg.GetImgApp()->CmdInterpreter();
60
61string hgrp = "SkyMapCmd";
62
63string kw = "map2double";
64string usage = "Convert a float map to a double map";
65usage += "\n Usage: map2double map";
66mpiac->RegisterCommand(kw, usage, this, hgrp);
67
68kw = "mapmult";
69usage = "Multiply a map by a constant";
70usage += "\n Usage: mapmult map cste";
71mpiac->RegisterCommand(kw, usage, this, hgrp);
72
73kw = "mapcover";
74usage = "Print the covering of a map";
75usage += "\n Usage: mapcover map v1,v2 [varname]";
76usage += "\n v1,v2: good pixels have v1<=val<=v2 (def=0.99,1.01)";
77usage += "\n (use default (0.99,1.01): \"!\")";
78usage += "\n $varname: percent of sky covering";
79mpiac->RegisterCommand(kw, usage, this, hgrp);
80
81kw = "map2cl";
82usage = "SkyMap to Cl";
83usage += "\n Usage: map2cl map(double) clvec [lmax] [niter]";
84usage += "\n lmax: if <=0 or \"!\" use default (3*nside-1)";
85usage += "\n niter: number of iterations (<=0 or def=3)";
86mpiac->RegisterCommand(kw, usage, this, hgrp);
87
88kw = "cl2map";
89usage = "Generate SkyMap from Cl";
90usage += "\n Usage: cl2map clvec map nside [fwhm]";
91mpiac->RegisterCommand(kw, usage, this, hgrp);
92
93kw = "map2alm";
94usage = "SkyMap to Alm";
95usage += "\n Usage: map2alm map(double) almmat [lmax] [niter]";
96usage += "\n lmax: if <=0 or \"!\" use default (3*nside-1)";
97usage += "\n niter: number of iterations (<=0 or def=3)";
98mpiac->RegisterCommand(kw, usage, this, hgrp);
99
100kw = "alm2map";
101usage = "Generate SkyMap from Alm";
102usage += "\n Usage: alm2map almmat map nside";
103mpiac->RegisterCommand(kw, usage, this, hgrp);
104
105kw = "crmapmask";
106usage = "Create a map mask (nside) between latitudes lat1,lat2 longitudes lon1,lon2";
107usage += "\n Usage: crmapmask mapmsk nside lat1,lat2 lon1,lon2 [valmsk,valnomsk]";
108usage += "\n mask pixels between [lat1,lat2] ([-90,90] in degrees)";
109usage += "\n and [lon1,lon2] ([0,360[ in degrees)";
110usage += "\n (for no mask \"!\")";
111usage += "\n valmsk=value to be written into masked pixels (def=0)";
112usage += "\n valnomsk=value to be written into unmasked pixels (def=1)";
113mpiac->RegisterCommand(kw, usage, this, hgrp);
114
115kw = "crmaskfrmap";
116usage = "Create a map mask (nside) relative to an other map pixels values";
117usage += "\n Usage: crmaskfrmap mapmsk nside map(double) v1,v2 [valmsk,valnomsk]";
118usage += "\n mask if v1<mapmsk(i)<v2";
119usage += "\n valmsk=value to be written into masked pixels (def=0)";
120usage += "\n valnomsk=value to be written into unmasked pixels (def=1)";
121mpiac->RegisterCommand(kw, usage, this, hgrp);
122
123kw = "maskmap";
124usage = "Mask a map with a mask map";
125usage += "\n Usage: maskmap map msk";
126usage += "\n operation is map(i) *= msk(theta,phi)";
127mpiac->RegisterCommand(kw, usage, this, hgrp);
128
129kw = "maproj";
130usage = "Project a map into an other one";
131usage += "\n Usage: maproj map mapproj [nside]";
132usage += "\n Create mapproj(nside) and project map into mapproj";
133usage += "\n (use \"maproj map mapproj\" to make a copy)";
134mpiac->RegisterCommand(kw, usage, this, hgrp);
135
136kw = "map2local";
137usage = "Project a map into a local map";
138usage += "\n Usage: map2local map localmap nx,ny angleX,angleY phi0,theta0 [x0,y0] [angle]";
139usage += "\n nx,ny: number of pixels in x(col),y(row) direction";
140usage += "\n X-axis==phi, Y-axis==theta";
141usage += "\n angleX,angleY: total angle aperture in x,y direction (degrees)";
142usage += "\n phi0,theta0: define origin in phi,theta (degrees)";
143usage += "\n x0,y0: map phi0,theta0 to pixel x0,y0 (\"!\" or def=middle of the localmap)";
144usage += "\n angle: angle (degrees) of rotation between x-axis of map-coordinate system";
145usage += "\n and the tangent to parallel on the sphere (def: 0.)";
146mpiac->RegisterCommand(kw, usage, this, hgrp);
147
148kw = "mapop";
149usage = "operation on a map";
150usage += "\n Usage: mapop map op1 map1 [op2 map2] [op2 map3] [...]";
151usage += "\n Do \"map op1= map1\", \"map op2=map2\", ...";
152usage += "\n where op = +,-,*,/";
153mpiac->RegisterCommand(kw, usage, this, hgrp);
154
155kw = "mapstat";
156usage = "Statistics from a map";
157usage += "\n Usage: mapstat map [msk] [meanvarname] [sigmavarname]";
158usage += "\n msk = mask sphere used as weight";
159usage += "\n if msk(i)<=0 do not use that pixel";
160usage += "\n if msk(i)>0 use that pixel with weight msk(i)";
161usage += "\n msk = \"!\" means no mask sphere";
162usage += "\n meanvarname = variable name to store mean";
163usage += "\n sigmavarname = variable name to store sigma";
164usage += "\n (\"!\" means do not store)";
165mpiac->RegisterCommand(kw, usage, this, hgrp);
166
167}
168
169/* --Methode-- */
170skymapmoduleExecutor::~skymapmoduleExecutor()
171{
172}
173
174/* --Methode-- */
175int skymapmoduleExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
176{
177
178if(kw == "map2double") {
179 if(tokens.size()<1) {
180 cout<<"Usage: map2double map"<<endl;
181 return(0);
182 }
183 Map2Double(tokens);
184} else if(kw == "mapmult") {
185 if(tokens.size()<2) {
186 cout<<"Usage: mapmult map cste"<<endl;
187 return(0);
188 }
189 MapMult(tokens);
190} else if(kw == "mapcover") {
191 if(tokens.size()<1) {
192 cout<<"Usage: mapcover map v1,v2 [varname]"<<endl;
193 return(0);
194 }
195 MapCover(tokens);
196} else if(kw == "map2cl") {
197 if(tokens.size()<2) {
198 cout<<"Usage: map2cl map clvec [lmax] [niter]"<<endl;
199 return(0);
200 }
201 Map2Cl(tokens);
202} else if(kw == "cl2map") {
203 if(tokens.size()<2) {
204 cout<<"Usage: cl2map clvec map nside [fwhm]"<<endl;
205 return(0);
206 }
207 Cl2Map(tokens);
208} else if(kw == "map2alm") {
209 if(tokens.size()<2) {
210 cout<<"Usage: map2alm map(double) almmat [lmax] [niter]"<<endl;
211 return(0);
212 }
213 Map2Alm(tokens);
214} else if(kw == "alm2map") {
215 if(tokens.size()<3) {
216 cout<<"Usage: alm2map almmat map nside"<<endl;
217 return(0);
218 }
219 Alm2Map(tokens);
220} else if(kw == "crmapmask") {
221 if(tokens.size()<2) {
222 cout<<"Usage: crmapmask mapmsk nside lat1,lat2 lon1,lon2 [valmsk,valnomsk]"<<endl;
223 return(0);
224 }
225 CrMapMask(tokens);
226} else if(kw == "crmaskfrmap") {
227 if(tokens.size()<3) {
228 cout<<"Usage: crmaskfrmap mapmsk nside map(double) v1,v2 [valmsk,valnomsk]"<<endl;
229 return(0);
230 }
231 CrMaskFrMap(tokens);
232} else if(kw == "maskmap") {
233 if(tokens.size()<2) {
234 cout<<"Usage: maskmap map msk"<<endl;
235 return(0);
236 }
237 MaskMap(tokens);
238} else if(kw == "maproj") {
239 if(tokens.size()<2) {
240 cout<<"Usage: maproj map mapproj [nside]"<<endl;
241 return(0);
242 }
243 MapProj(tokens);
244} else if(kw == "map2local") {
245 if(tokens.size()<5) {
246 cout<<"Usage: map2local map localmap nx,ny angleX,angleY phi0,theta0 [x0,y0] [angle]"<<endl;
247 return(0);
248 }
249 Map2Local(tokens);
250} else if(kw == "mapop") {
251 if(tokens.size()<3) {
252 cout<<"Usage: mapop map op map1 [op map2] [op map3] [...]"<<endl;
253 return(0);
254 }
255 MapOper(tokens);
256} else if(kw == "mapstat") {
257 if(tokens.size()<1) {
258 cout<<"Usage: Usage: mapstat map [msk] [meanvarname] [sigmavarname]"<<endl;
259 return(0);
260 }
261 MapStat(tokens);
262}
263
264return(0);
265}
266
267/* --Methode-- */
268void skymapmoduleExecutor::Map2Double(vector<string>& tokens)
269{
270 NamedObjMgr omg;
271 AnyDataObj* obj=omg.GetObj(tokens[0]);
272 if(obj==NULL) {
273 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
274 return;
275 }
276 SphereHEALPix<r_4>* map = dynamic_cast<SphereHEALPix<r_4>*>(obj);
277 if(map==NULL) {
278 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_4>"<<endl;
279 return;
280 }
281 int_4 nside = map->SizeIndex();
282 SphereHEALPix<r_8>* map8 = new SphereHEALPix<r_8>(nside);
283 cout<<"Map2Double: converting to double "<<tokens[0]<<endl;
284 for(int_4 i=0;i<map8->NbPixels();i++) (*map8)(i) = (r_8) (*map)(i);
285 omg.AddObj(map8,tokens[0]);
286}
287
288/* --Methode-- */
289void skymapmoduleExecutor::MapMult(vector<string>& tokens)
290{
291 NamedObjMgr omg;
292 AnyDataObj* obj=omg.GetObj(tokens[0]);
293 if(obj==NULL) {
294 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
295 return;
296 }
297 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
298 if(map==NULL) {
299 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
300 return;
301 }
302 double v = atof(tokens[1].c_str());
303 cout<<"MapMult: Multiply "<<tokens[0]<<" by "<<v<<endl;
304 for(int_4 i=0;i<map->NbPixels();i++) (*map)(i) *= v;
305}
306
307/* --Methode-- */
308void skymapmoduleExecutor::MapCover(vector<string>& tokens)
309{
310 NamedObjMgr omg;
311 AnyDataObj* obj=omg.GetObj(tokens[0]);
312 if(obj==NULL) {
313 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
314 return;
315 }
316 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
317 if(map==NULL) {
318 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
319 return;
320 }
321
322 double v1=0.99, v2=1.01;
323 if(tokens.size()>1) if(tokens[1][0]!='!')
324 sscanf(tokens[1].c_str(),"%lf,%lf",&v1,&v2);
325 cout<<"MapCover: "<<tokens[0]<<" good px=["<<v1<<","<<v2<<"]"<<endl;
326 int_4 ngood=0;
327 for(int_4 i=0;i<map->NbPixels();i++)
328 if(v1<=(*map)(i) && (*map)(i)<=v2) ngood++;
329 double per = (double)ngood/(double)map->NbPixels();
330 cout<<"NGood="<<ngood<<" NTot="<<map->NbPixels()
331 <<" -> "<<100.*per<<" %";
332 if(tokens.size()>2) {
333 if(omg.HasVar(tokens[2])) omg.DeleteVar(tokens[2]);
334 char str[128]; sprintf(str,"%g",per);
335 omg.SetVar(tokens[2],(string)str);
336 cout<<" stored into $"<<tokens[2];
337 }
338 cout<<endl;
339}
340
341/* --Methode-- */
342void skymapmoduleExecutor::Map2Cl(vector<string>& tokens)
343{
344 NamedObjMgr omg;
345
346 int_4 lmax=0, niter=3;
347 if(tokens.size()>2) if(tokens[2][0]!='!')
348 lmax = atoi(tokens[2].c_str());
349 if(tokens.size()>3) niter = atoi(tokens[3].c_str());
350 if(niter<=0) niter = 3;
351
352 AnyDataObj* obj=omg.GetObj(tokens[0]);
353 if(obj==NULL) {
354 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
355 return;
356 }
357 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
358 if(map==NULL) {
359 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
360 return;
361 }
362
363 SphericalTransformServer<r_8> ylmserver;
364
365 int_4 nside = map->SizeIndex();
366 if(lmax<=0) lmax = 3*nside-1;
367
368 cout<<"Map2Cl: "<<tokens[0]<<" -> "<<tokens[1]
369 <<" lmax="<<lmax<<" niter="<<niter<<endl;
370
371 SphereHEALPix<r_8> tmpmap(*map,false);
372 TVector<r_8> cltmp = ylmserver.DecomposeToCl(tmpmap,lmax,0.,niter);
373
374 TVector<r_8>* cl = new TVector<r_8>(cltmp,false);
375 omg.AddObj(cl,tokens[1]);
376}
377
378/* --Methode-- */
379void skymapmoduleExecutor::Cl2Map(vector<string>& tokens)
380{
381 NamedObjMgr omg;
382
383 AnyDataObj* obj=omg.GetObj(tokens[0]);
384 if(obj==NULL) {
385 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
386 return;
387 }
388 TVector<r_8>* cl = dynamic_cast<TVector<r_8>*>(obj);
389 if(cl==NULL) {
390 cout<<"Error: "<<tokens[0]<<" is not a TVector<r_8>"<<endl;
391 return;
392 }
393
394 int nside = 128;
395 if(tokens.size()>2) nside = atoi(tokens[2].c_str());
396 double fwhm = 0.;
397 if(tokens.size()>3) fwhm = atof(tokens[3].c_str());
398
399 SphereHEALPix<r_8>* map = new SphereHEALPix<r_8>;
400
401 SphericalTransformServer<r_8> ylmserver;
402 cout<<"Cl2Map: Generate map "<<tokens[1]<<" with nside="<<nside
403 <<" from cl "<<tokens[0]<<" with fwhm="<<fwhm<<endl;
404 ylmserver.GenerateFromCl(*map,nside,*cl,fwhm);
405
406 omg.AddObj(map,tokens[1]);
407}
408
409/* --Methode-- */
410void skymapmoduleExecutor::Map2Alm(vector<string>& tokens)
411{
412 NamedObjMgr omg;
413
414 int_4 lmax=0, niter=3;
415 if(tokens.size()>2) if(tokens[2][0]!='!')
416 lmax = atoi(tokens[2].c_str());
417 if(tokens.size()>3) niter = atoi(tokens[3].c_str());
418 if(niter<=0) niter = 3;
419
420 AnyDataObj* obj=omg.GetObj(tokens[0]);
421 if(obj==NULL) {
422 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
423 return;
424 }
425 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
426 if(map==NULL) {
427 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
428 return;
429 }
430
431 SphericalTransformServer<r_8> ylmserver;
432
433 int_4 nside = map->SizeIndex();
434 if(lmax<=0) lmax = 3*nside-1;
435
436 cout<<"Map2Alm: "<<tokens[0]<<" -> "<<tokens[1]
437 <<" lmax="<<lmax<<" niter="<<niter<<endl;
438
439 SphereHEALPix<r_8> tmpmap(*map,false);
440 Alm<r_8> almtmp;
441 ylmserver.DecomposeToAlm(tmpmap,almtmp,lmax,0.,niter);
442
443 int n = almtmp.rowNumber();
444 TMatrix< complex<r_8> >* alm = new TMatrix< complex<r_8> >(n,n);
445 *alm = (complex<r_8>)0.;
446 for(int i=0;i<n;i++) for(int j=0;j<=i;j++) (*alm)(i,j)=almtmp(i,j);
447 omg.AddObj(alm,tokens[1]);
448}
449
450/* --Methode-- */
451void skymapmoduleExecutor::Alm2Map(vector<string>& tokens)
452{
453 NamedObjMgr omg;
454
455 AnyDataObj* obj=omg.GetObj(tokens[0]);
456 if(obj==NULL) {
457 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
458 return;
459 }
460 TMatrix< complex<r_8> >* almmat = dynamic_cast<TMatrix< complex<r_8> >*>(obj);
461 if(almmat==NULL) {
462 cout<<"Error: "<<tokens[0]<<" is not a TMatrix< complex<r_8> >"<<endl;
463 return;
464 }
465
466 int lmax = almmat->NRows()-1;
467 Alm<r_8> alm(lmax);
468 alm = (complex<r_8>)0.;
469 for(int i=0;i<lmax+1;i++) for(int j=0;j<=i;j++) alm(i,j)=(*almmat)(i,j);
470
471 int nside = 128;
472 if(tokens.size()>2) nside = atoi(tokens[2].c_str());
473 SphereHEALPix<r_8>* map = new SphereHEALPix<r_8>;
474
475 SphericalTransformServer<r_8> ylmserver;
476 cout<<"Alm2Map: Generate map "<<tokens[1]<<" with nside="<<nside
477 <<" from alm "<<tokens[0]<<endl;
478 ylmserver.GenerateFromAlm(*map,nside,alm);
479
480 omg.AddObj(map,tokens[1]);
481}
482
483/* --Methode-- */
484void skymapmoduleExecutor::CrMapMask(vector<string>& tokens)
485{
486 NamedObjMgr omg;
487
488 int_4 nside = atoi(tokens[1].c_str());
489 if(!IsNSideGood(nside)) {cout<<"CrMapMask: Bad nside "<<nside<<endl; return;}
490
491 SphereHEALPix<r_8>* map = new SphereHEALPix<r_8>(nside);
492
493 bool lat=false, lon=false;
494 double lat1=-99., lat2=99., lon1=-999., lon2=999.;
495 if(tokens.size()>2) if(tokens[2][0]!='!')
496 {lat=true; sscanf(tokens[2].c_str(),"%lf,%lf",&lat1,&lat2);}
497 if(tokens.size()>3) if(tokens[3][0]!='!')
498 {lon=true; sscanf(tokens[3].c_str(),"%lf,%lf",&lon1,&lon2);}
499
500 double valmsk=0., unvalmsk=1.;
501 if(tokens.size()>4) sscanf(tokens[4].c_str(),"%lf,%lf",&valmsk,&unvalmsk);
502
503 cout<<"CrMapMask "<<tokens[0]<<" nside="<<nside;
504 if(lat) cout<<" for latitude in ["<<lat1<<","<<lat2<<"]";
505 if(lon) cout<<" for longitude in ["<<lon1<<","<<lon2<<"]";
506 cout<<" valmsk="<<valmsk<<" unvalmsk="<<unvalmsk<<endl;
507
508 //Attention: conversion en co-latitude ==> inversion: [lat2,lat1]
509 lat1 = (90.-lat1)*M_PI/180.; lat2 = (90.-lat2)*M_PI/180.;
510 lon1 *= M_PI/180.; lon2 *= M_PI/180.;
511 for(int_4 i=0;i<map->NbPixels();i++) {
512 (*map)(i)=unvalmsk;
513 if(!lat && !lon) continue;
514 double theta,phi; map->PixThetaPhi(i,theta,phi);
515 if(lat && (theta<lat2 || lat1<theta)) continue;
516 if(lon && ( phi<lon1 || lon2<phi )) continue;
517 (*map)(i)=valmsk;
518 }
519
520 omg.AddObj(map,tokens[0]);
521}
522
523/* --Methode-- */
524void skymapmoduleExecutor::CrMaskFrMap(vector<string>& tokens)
525{
526 NamedObjMgr omg;
527
528 int_4 nside = atoi(tokens[1].c_str());
529 if(nside<=1) return;
530 if(!IsNSideGood(nside)) {cout<<"CrMapMask: Bad nside "<<nside<<endl; return;}
531
532 AnyDataObj* obj=omg.GetObj(tokens[2]);
533 if(obj==NULL) {
534 cout<<"Error: Pas d'objet de nom "<<tokens[2]<<endl;
535 return;
536 }
537 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
538 if(map==NULL) {
539 cout<<"Error: "<<tokens[2]<<" is not a SphereHEALPix<r_8>"<<endl;
540 return;
541 }
542
543 SphereHEALPix<r_8>* msk = new SphereHEALPix<r_8>(nside);
544
545 double v1=-1.e100, v2=1.e100;
546 if(tokens.size()>3) if(tokens[3][0]!='!')
547 sscanf(tokens[3].c_str(),"%lf,%lf",&v1,&v2);
548
549 double valmsk=0., unvalmsk=1.;
550 if(tokens.size()>4) sscanf(tokens[4].c_str(),"%lf,%lf",&valmsk,&unvalmsk);
551
552 cout<<"CrMaskFrMap "<<tokens[0]<<" nside"<<nside<<" with "<<tokens[2]<<endl
553 <<" for value in ["<<v1<<","<<v2<<"]"
554 <<" valmsk="<<valmsk<<" unvalmsk="<<unvalmsk<<endl;
555
556 for(int_4 i=0;i<msk->NbPixels();i++) {
557 double theta,phi; msk->PixThetaPhi(i,theta,phi);
558 int_4 ip = map->PixIndexSph(theta,phi);
559 if(ip<0 || ip>=map->NbPixels()) continue;
560 double v = (*map)(ip);
561 if(v>v1 && v<v2) (*msk)(i)=valmsk;
562 else (*msk)(i)=unvalmsk;
563 }
564
565 omg.AddObj(msk,tokens[0]);
566}
567
568/* --Methode-- */
569void skymapmoduleExecutor::MaskMap(vector<string>& tokens)
570{
571 NamedObjMgr omg;
572
573 AnyDataObj* obj=omg.GetObj(tokens[0]);
574 if(obj==NULL) {
575 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
576 return;
577 }
578 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
579 if(map==NULL) {
580 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
581 return;
582 }
583
584 AnyDataObj* objm=omg.GetObj(tokens[1]);
585 if(obj==NULL) {
586 cout<<"Error: Pas d'objet de nom "<<tokens[1]<<endl;
587 return;
588 }
589 SphereHEALPix<r_8>* msk = dynamic_cast<SphereHEALPix<r_8>*>(objm);
590 if(msk==NULL) {
591 cout<<"Error: "<<tokens[1]<<" is not a SphereHEALPix<r_8>"<<endl;
592 return;
593 }
594
595 cout<<"MskMap: "<<tokens[0]<<" with mask "<<tokens[1]<<endl;
596
597 for(int_4 i=0;i<map->NbPixels();i++) {
598 double theta,phi; map->PixThetaPhi(i,theta,phi);
599 int_4 ip = msk->PixIndexSph(theta,phi);
600 if(ip<0 || ip>=msk->NbPixels()) continue;
601 (*map)(i) *= (*msk)(ip);
602 }
603
604}
605
606/* --Methode-- */
607void skymapmoduleExecutor::MapProj(vector<string>& tokens)
608{
609 NamedObjMgr omg;
610
611 AnyDataObj* obj=omg.GetObj(tokens[0]);
612 if(obj==NULL) {
613 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
614 return;
615 }
616 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
617 if(map==NULL) {
618 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
619 return;
620 }
621 int_4 nsidemap = map->SizeIndex();
622
623 int_4 nside = nsidemap;
624 if(tokens.size()>2) nside = atoi(tokens[2].c_str());
625 if(!IsNSideGood(nside)) {cout<<"MapProj: Bad nside "<<nside<<endl; return;}
626
627 SphereHEALPix<r_8>* mapproj = new SphereHEALPix<r_8>(nside);
628
629 if(nsidemap==nside) { // tailles egales
630 for(int_4 i=0;i<mapproj->NbPixels();i++)
631 (*mapproj)(i) = (*map)(i);
632 } else if(nsidemap<nside) { // mapproj>map
633 for(int_4 i=0;i<mapproj->NbPixels();i++) {
634 double theta,phi; mapproj->PixThetaPhi(i,theta,phi);
635 int_4 ip = map->PixIndexSph(theta,phi);
636 if(ip<0 || ip>=map->NbPixels()) continue;
637 (*mapproj)(i) = (*map)(ip);
638 }
639 } else { // mapproj<map
640 SphereHEALPix<int_4> nproj(nside);
641 nproj = 0; *mapproj = 0.;
642 for(int_4 i=0;i<map->NbPixels();i++) {
643 double theta,phi; map->PixThetaPhi(i,theta,phi);
644 int_4 ip = mapproj->PixIndexSph(theta,phi);
645 if(ip<0 || ip>=mapproj->NbPixels()) continue;
646 (*mapproj)(ip) += (*map)(i);
647 nproj(ip)++;
648 }
649 for(int_4 i=0;i<mapproj->NbPixels();i++)
650 (*mapproj)(i) /= (r_8) nproj(i);
651 }
652
653 omg.AddObj(mapproj,tokens[1]);
654}
655
656/* --Methode-- */
657void skymapmoduleExecutor::Map2Local(vector<string>& tokens)
658{
659 NamedObjMgr omg;
660
661 AnyDataObj* obj=omg.GetObj(tokens[0]);
662 if(obj==NULL) {
663 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
664 return;
665 }
666 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
667 if(map==NULL) {
668 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
669 return;
670 }
671
672 int_4 nx=10,ny=10;
673 if(tokens.size()>2) sscanf(tokens[2].c_str(),"%d,%d",&nx,&ny);
674 if(nx<=0)
675 {cout<<"Error: nx="<<nx<<" value >0"<<endl; return;}
676 if(ny<=0)
677 {cout<<"Error: ny="<<ny<<" value >0"<<endl; return;}
678
679 double angleX=1., angleY=1.;
680 if(tokens.size()>3) sscanf(tokens[3].c_str(),"%lf,%lf",&angleX,&angleY);
681 if(angleX<=0. || angleX>180.)
682 {cout<<"Error: bad angleX="<<angleX<<" value #]0,180]"<<endl; return;}
683 if(angleY<=0. || angleY>180.)
684 {cout<<"Error: bad angleY="<<angleY<<" value #]0,180]"<<endl; return;}
685
686 double phi0=0., theta0=0.;
687 if(tokens.size()>4) sscanf(tokens[4].c_str(),"%lf,%lf",&phi0,&theta0);
688 if(phi0<0. || phi0>=360.)
689 {cout<<"Error: bad phi0="<<phi0<<" value #[0,360["<<endl; return;}
690 if(theta0<-90. || theta0>90.)
691 {cout<<"Error: bad theta0="<<theta0<<" value #[-90,90]"<<endl; return;}
692
693 int_4 x0=nx/2, y0=ny/2;
694 if(tokens.size()>5) if(tokens[5][0]!='!')
695 sscanf(tokens[5].c_str(),"%d,%d",&x0,&y0);
696
697 double angle=0.;
698 if(tokens.size()>6) sscanf(tokens[6].c_str(),"%lf",&angle);
699
700 cout<<"Map2Local: proj "<<tokens[0]<<" to local "<<tokens[1]<<endl
701 <<" nx="<<nx<<" ny="<<ny<<" angleX="<<angleX<<" angleY="<<angleY
702 <<" phi0="<<phi0<<" theta0="<<theta0<<" x0="<<x0<<" y0="<<y0
703 <<" angle="<<angle<<endl;
704 if(angle<-90. || angle>90.)
705 {cout<<"Error: bad angle="<<angle<<" value #[-90,90]"<<endl; return;}
706
707 theta0 = (90.-theta0);
708 LocalMap<r_8>* lmap = new LocalMap<r_8>(nx,ny,angleX,angleY,theta0,phi0,x0,y0,angle);
709 *lmap = 0.; //lmap->print(cout);
710
711 int_4 nbad=0;
712 double tmax=-9999., pmax=-9999., tmin=9999., pmin=9999.;
713 for(int_4 i=0;i<lmap->NbPixels();i++) {
714 double theta,phi;
715 lmap->PixThetaPhi(i,theta,phi);
716 int_4 ip = map->PixIndexSph(theta,phi);
717 if(ip<0 || ip>=map->NbPixels()) {nbad++; continue;}
718 if(theta<tmin) tmin=theta; if(theta>tmax) tmax=theta;
719 if(phi<pmin) pmin=phi; if(phi>pmax) pmax=phi;
720 (*lmap)(i) = (*map)(ip);
721 }
722 if(nbad) cout<<"Warning: "<<nbad<<" bad pixels numbers"<<endl;
723 cout<<" phi=["<<pmin/M_PI*180.<<","<<pmax/M_PI*180.<<"]="
724 <<(pmax-pmin)/M_PI*180.
725 <<" theta=["<<tmin/M_PI*180.<<","<<tmax/M_PI*180.<<"]="
726 <<(tmax-tmin)/M_PI*180.<<endl;
727
728 omg.AddObj(lmap,tokens[1]);
729}
730
731/* --Methode-- */
732void skymapmoduleExecutor::MapOper(vector<string>& tokens)
733{
734 NamedObjMgr omg;
735
736 AnyDataObj* obj=omg.GetObj(tokens[0]);
737 if(obj==NULL) {
738 cout<<"Error: Pas d'objet de nom "<<tokens[0]<<endl;
739 return;
740 }
741 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
742 if(map==NULL) {
743 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
744 return;
745 }
746
747 cout<<"MapOper: "<<tokens[0];
748 for(unsigned short iop=1;iop<tokens.size()-1;iop+=2) {
749 char op = tokens[iop][0];
750 if(op!='+' && op!='-' && op!='*' && op!='/') continue;
751 AnyDataObj* obj1=omg.GetObj(tokens[iop+1]);
752 if(obj1==NULL)
753 {cout<<"Error: Pas d'objet de nom "<<tokens[iop+1]<<endl;
754 continue;}
755 SphereHEALPix<r_8>* map1 = dynamic_cast<SphereHEALPix<r_8>*>(obj1);
756 if(map1==NULL)
757 {cout<<"Error: "<<tokens[iop+1]<<" is not a SphereHEALPix<r_8>"<<endl;
758 continue;}
759 cout<<" "<<op<<"= "<<tokens[iop+1];
760 for(int_4 i=0;i<map->NbPixels();i++) {
761 double theta,phi; map->PixThetaPhi(i,theta,phi);
762 int_4 ip = map1->PixIndexSph(theta,phi);
763 if(ip<0 || ip>=map1->NbPixels()) continue;
764 if(op=='+') (*map)(i) += (*map1)(ip);
765 else if(op=='-') (*map)(i) -= (*map1)(ip);
766 else if(op=='*') (*map)(i) *= (*map1)(ip);
767 else if(op=='/')
768 {if((*map1)(ip)!=0.) (*map)(i) /= (*map1)(ip);
769 else (*map)(i) = 0.;}
770 }
771 }
772 cout<<endl;
773}
774
775/* --Methode-- */
776void skymapmoduleExecutor::MapStat(vector<string>& tokens)
777{
778 NamedObjMgr omg;
779
780 AnyDataObj* obj=omg.GetObj(tokens[0]);
781 if(obj==NULL) {
782 cout<<"Error: Pas d'objet de nom "<<tokens[1]<<endl;
783 return;
784 }
785 SphereHEALPix<r_8>* map = dynamic_cast<SphereHEALPix<r_8>*>(obj);
786 if(map==NULL) {
787 cout<<"Error: "<<tokens[0]<<" is not a SphereHEALPix<r_8>"<<endl;
788 return;
789 }
790
791 SphereHEALPix<r_8>* msk = NULL;
792 if(tokens.size()>1) {
793 if(tokens[1][0]!='!') {
794 AnyDataObj* objm=omg.GetObj(tokens[1]);
795 if(objm==NULL) {
796 cout<<"Error: Pas d'objet de nom "<<tokens[1]<<endl;
797 return;
798 }
799 msk = dynamic_cast<SphereHEALPix<r_8>*>(objm);
800 if(msk==NULL) {
801 cout<<"Error: "<<tokens[1]<<" is not a SphereHEALPix<r_8>"<<endl;
802 return;
803 }
804 }
805 }
806
807 cout<<"MapStat for map "<<tokens[0];
808 if(msk) cout<<" using mask "<<tokens[1];
809 cout<<endl;
810
811 double sum=0., sum2=0., sumw=0.;
812 int npix = map->NbPixels(), npixgood=0;
813 for(int_4 i=0;i<npix;i++) {
814 double w = 1.;
815 if(msk) {
816 double theta,phi; map->PixThetaPhi(i,theta,phi);
817 int_4 ip = msk->PixIndexSph(theta,phi);
818 if(ip<0 || ip>=msk->NbPixels()) w=0.;
819 else w=(*msk)(ip);
820 }
821 if(w<=0.) continue;
822 npixgood++; sumw += w;
823 sum += (*map)(i)*w;
824 sum2 += (*map)(i)*(*map)(i)*w*w;
825 }
826 if(sumw<=0. || npixgood==0) {
827 sum = sum2 = sumw=0.;
828 } else {
829 sum /= sumw;
830 sum2 = sum2/sumw - sum*sum;
831 if(sum2<=0.) sum2=0.; else sum2=sqrt(sum2);
832 }
833 cout<<"Number of good px="<<npixgood<<" / "<<npix
834 <<" ("<<100.*npixgood/npix<<" %)"<<endl;
835 cout<<" mean="<<sum<<" sigma="<<sum2<<endl;
836
837 if(tokens.size()>2) if(tokens[2][0]!='!') {
838 if(omg.HasVar(tokens[2])) omg.DeleteVar(tokens[2]);
839 char str[128]; sprintf(str,"%.8f",sum);
840 omg.SetVar(tokens[2],(string)str);
841 cout<<" mean stored into $"<<tokens[2];
842 }
843 if(tokens.size()>3) if(tokens[3][0]!='!') {
844 if(omg.HasVar(tokens[3])) omg.DeleteVar(tokens[3]);
845 char str[128]; sprintf(str,"%.8f",sum2);
846 omg.SetVar(tokens[3],(string)str);
847 cout<<" sigma stored into $"<<tokens[3];
848 }
849 if(tokens.size()>2) cout<<endl;
850
851}
852
853////////////////////////////////////////////////////////////
854static skymapmoduleExecutor * piaskmex = NULL;
855
856void skymapmodule_init()
857{
858if (piaskmex) delete piaskmex;
859piaskmex = new skymapmoduleExecutor;
860}
861
862void skymapmodule_end()
863{
864// Desactivation du module
865if (piaskmex) delete piaskmex;
866piaskmex = NULL;
867}
868
869////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.