#include "machdefs.h" #include #include #include #include #include #include #include #include "piacmd.h" #include "nobjmgr.h" #include "pistdimgapp.h" #include "servnobjm.h" #include "tvector.h" #include "pidrawer.h" #include "nomgadapter.h" #include "skymap.h" #include "sphericaltransformserver.h" extern "C" { void skymapmodule_init(); void skymapmodule_end(); } class skymapmoduleExecutor : public CmdExecutor { public: enum {UnKnown=(uint_2) 0, TypeR8=(uint_2) 16, TypeR4=(uint_2) 32, Spherical=(uint_2) 1, HealPix=(uint_2) 2, ThetaPhi=(uint_2) 4, Spherical8=(uint_2) Spherical|TypeR8, Spherical4=(uint_2) Spherical|TypeR4, HealPix8 =(uint_2) Spherical|HealPix|TypeR8, HealPix4 =(uint_2) Spherical|HealPix|TypeR4, ThetaPhi8 =(uint_2) Spherical|ThetaPhi|TypeR8, ThetaPhi4 =(uint_2) Spherical|ThetaPhi|TypeR4}; skymapmoduleExecutor(); virtual ~skymapmoduleExecutor(); virtual int Execute(string& keyw, vector& args, string& toks); void TypeMap(vector& tokens); void Map2Double(vector& tokens); void Map2Map(vector& tokens); void MapMult(vector& tokens); void MapCover(vector& tokens); void Map2Cl(vector& tokens); void Cl2Map(vector& tokens); void Map2Alm(vector& tokens); void Alm2Map(vector& tokens); void CrMapMask(vector& tokens); void CrMaskFrMap(vector& tokens); void MaskMap(vector& tokens); void MapProj(vector& tokens); void Map2Local(vector& tokens); void MapOper(vector& tokens); void MapStat(vector& tokens); protected: void SetTypeMap(vector& tokens); bool IsNSideGood(int_4 nside); void GetNSideGood(int_4 &nside); uint_2 typemap(AnyDataObj* obj); uint_2 DefTypMap; }; /* --Methode-- */ skymapmoduleExecutor::skymapmoduleExecutor() { PIACmd * mpiac; NamedObjMgr omg; mpiac = omg.GetImgApp()->CmdInterpreter(); string hgrp = "SkyMapCmd"; string kw,usage; kw = "settypemap"; usage = "Set le type de map par default"; usage += "\n type= H for Healpix (default)"; usage += "\n T for ThetaPhi"; usage += "\n Usage: settypemap type"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "typemap"; usage = "Imprime le type de map"; usage += "\n Usage: typemap map"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "map2double"; usage = "Convert a float map to a double map"; usage += "\n Usage: map2double map"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "map2map"; usage = "Convert a map into an other map type"; usage += "\n type= H for Healpix"; usage += "\n T for ThetaPhi"; usage += "\n if nside <=1 use nside from map"; usage += "\n Usage: map2map map type [nside]"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "mapmult"; usage = "Multiply a map by a constant"; usage += "\n Usage: mapmult map cste"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "mapcover"; usage = "Print the covering of a map"; usage += "\n Usage: mapcover map v1,v2 [varname]"; usage += "\n v1,v2: good pixels have v1<=val<=v2 (def=0.99,1.01)"; usage += "\n (use default (0.99,1.01): \"!\")"; usage += "\n $varname: percent of sky covering"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "map2cl"; usage = "SkyMap to Cl"; usage += "\n Usage: map2cl map(double) clvec [lmax] [niter]"; usage += "\n lmax: if <=0 or \"!\" use default (3*nside-1)"; usage += "\n niter: number of iterations (<=0 or def=3)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "cl2map"; usage = "Generate SkyMap from Cl"; usage += "\n Usage: cl2map clvec map nside [fwhm]"; usage += "\n (see command settypemap)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "map2alm"; usage = "SkyMap to Alm"; usage += "\n Usage: map2alm map(double) almmat [lmax] [niter]"; usage += "\n lmax: if <=0 or \"!\" use default (3*nside-1)"; usage += "\n niter: number of iterations (<=0 or def=3)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "alm2map"; usage = "Generate SkyMap from Alm"; usage += "\n Usage: alm2map almmat map nside"; usage += "\n (see command settypemap)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "crmapmask"; usage = "Create a map mask (nside) between latitudes lat1,lat2 longitudes lon1,lon2"; usage += "\n Usage: crmapmask mapmsk nside lat1,lat2 lon1,lon2 [valmsk,valnomsk]"; usage += "\n mask pixels between [lat1,lat2] ([-90,90] in degrees)"; usage += "\n and [lon1,lon2] ([0,360[ in degrees)"; usage += "\n (for no mask \"!\")"; usage += "\n valmsk=value to be written into masked pixels (def=0)"; usage += "\n valnomsk=value to be written into unmasked pixels (def=1)"; usage += "\n (see command settypemap)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "crmaskfrmap"; usage = "Create a map mask (nside) relative to an other map pixels values"; usage += "\n Usage: crmaskfrmap mapmsk nside map(double) v1,v2 [valmsk,valnomsk]"; usage += "\n mask if v1RegisterCommand(kw, usage, this, hgrp); kw = "maskmap"; usage = "Mask a map with a mask map"; usage += "\n Usage: maskmap map msk"; usage += "\n operation is map(i) *= msk(theta,phi)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "maproj"; usage = "Project a map into an other one"; usage += "\n Usage: maproj map mapproj [nside]"; usage += "\n Create mapproj(nside) and project map into mapproj"; usage += "\n (use \"maproj map mapproj\" to make a copy)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "map2local"; usage = "Project a map into a local map"; usage += "\n Usage: map2local map localmap nx,ny angleX,angleY phi0,theta0 [x0,y0] [angle]"; usage += "\n nx,ny: number of pixels in x(col),y(row) direction"; usage += "\n X-axis==phi, Y-axis==theta"; usage += "\n angleX,angleY: total angle aperture in x,y direction (degrees)"; usage += "\n phi0,theta0: define origin in phi,theta (degrees)"; usage += "\n x0,y0: map phi0,theta0 to pixel x0,y0 (\"!\" or def=middle of the localmap)"; usage += "\n angle: angle (degrees) of rotation between x-axis of map-coordinate system"; usage += "\n and the tangent to parallel on the sphere (def: 0.)"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "mapop"; usage = "operation on a map"; usage += "\n Usage: mapop map op1 map1 [op2 map2] [op2 map3] [...]"; usage += "\n Do \"map op1= map1\", \"map op2=map2\", ..."; usage += "\n where op = +,-,*,/"; mpiac->RegisterCommand(kw, usage, this, hgrp); kw = "mapstat"; usage = "Statistics from a map"; usage += "\n Usage: mapstat map [msk] [meanvarname] [sigmavarname]"; usage += "\n msk = mask sphere used as weight"; usage += "\n if msk(i)<=0 do not use that pixel"; usage += "\n if msk(i)>0 use that pixel with weight msk(i)"; usage += "\n msk = \"!\" means no mask sphere"; usage += "\n meanvarname = variable name to store mean"; usage += "\n sigmavarname = variable name to store sigma"; usage += "\n (\"!\" means do not store)"; mpiac->RegisterCommand(kw, usage, this, hgrp); DefTypMap = HealPix; } /* --Methode-- */ skymapmoduleExecutor::~skymapmoduleExecutor() { } /* --Methode-- */ int skymapmoduleExecutor::Execute(string& kw, vector& tokens, string& toks) { if(kw == "settypemap") { SetTypeMap(tokens); } else if(kw == "typemap") { if(tokens.size()<1) { cout<<"Usage: typemap map"<& tokens) { NamedObjMgr omg; AnyDataObj* obj=omg.GetObj(tokens[0]); uint_2 t = typemap(obj); if(t==UnKnown) {cout<<"TypeMap: UnKnown"<* map = dynamic_cast*>(obj); nside = map->SizeIndex(); } else if(t&TypeR4) { SphericalMap* map = dynamic_cast*>(obj); nside = map->SizeIndex(); } string dum = ""; if(t==HealPix8) dum = "SphereHEALPix"; else if(t==HealPix4) dum = "SphereHEALPix"; else if(t==ThetaPhi8) dum = "SphereThetaPhi"; else if(t==ThetaPhi4) dum = "SphereThetaPhi"; else if(t==Spherical8) dum = "SphericalMap"; else if(t==Spherical4) dum = "SphericalMap"; cout<<"TypeMap: "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<SizeIndex(); SphericalMap* map8 = NULL; if(t&HealPix) map8 = new SphereHEALPix(nside); else if(t&ThetaPhi) map8 = new SphereThetaPhi(nside); else return; cout<<"Map2Double: converting to double "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<SizeIndex(); uint_2 tomap = UnKnown; if(toupper(tokens[1][0])=='H') tomap = HealPix; else if(toupper(tokens[1][0])=='T') tomap = ThetaPhi; else {cout<<"unkown map type "<<(char)toupper(tokens[1][0])<<" (H or T)!"<2) tonside = atoi(tokens[2].c_str()); else { if(tomap&HealPix) {tonside=int(nside/1.5); GetNSideGood(tonside);} else tonside=int(map->NbThetaSlices()/2.5); } if(tomap&HealPix && !IsNSideGood(tonside)) {cout<<"Bad nside for Healpix "<* map8 = NULL; if(tomap&HealPix) map8 = new SphereHEALPix(tonside); else if(tomap&ThetaPhi) map8 = new SphereThetaPhi(tonside); else return; cout<<"Map2Map: convert "< "<NbThetaSlices()<NbPixels();i++) { double theta,phi; map8->PixThetaPhi(i,theta,phi); int_4 ip = map->PixIndexSph(theta,phi); if(ip<0 || ip>=map->NbPixels()) continue; (*map8)(i)=(*map)(ip); } omg.AddObj(map8,tokens[0]); } /* --Methode-- */ void skymapmoduleExecutor::MapMult(vector& tokens) { NamedObjMgr omg; AnyDataObj* obj=omg.GetObj(tokens[0]); if(obj==NULL) { cout<<"Error: Pas d'objet de nom "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<NbPixels();i++) (*map)(i) *= v; } /* --Methode-- */ void skymapmoduleExecutor::MapCover(vector& tokens) { NamedObjMgr omg; AnyDataObj* obj=omg.GetObj(tokens[0]); if(obj==NULL) { cout<<"Error: Pas d'objet de nom "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<1) if(tokens[1][0]!='!') sscanf(tokens[1].c_str(),"%lf,%lf",&v1,&v2); cout<<"MapCover: "<NbPixels();i++) if(v1<=(*map)(i) && (*map)(i)<=v2) ngood++; double per = (double)ngood/(double)map->NbPixels(); cout<<"NGood="< "<<100.*per<<" %"; if(tokens.size()>2) { if(omg.HasVar(tokens[2])) omg.DeleteVar(tokens[2]); char str[128]; sprintf(str,"%g",per); omg.SetVar(tokens[2],(string)str); cout<<" stored into $"<& tokens) { NamedObjMgr omg; int_4 lmax=0, niter=3; if(tokens.size()>2) if(tokens[2][0]!='!') lmax = atoi(tokens[2].c_str()); if(tokens.size()>3) niter = atoi(tokens[3].c_str()); if(niter<=0) niter = 3; AnyDataObj* obj=omg.GetObj(tokens[0]); if(obj==NULL) { cout<<"Error: Pas d'objet de nom "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"< ylmserver; int_4 nside = map->SizeIndex(); if(lmax<=0) lmax = 3*nside-1; cout<<"Map2Cl: "< "<* cl = dynamic_cast*>(obj); if(cl==NULL) { cout<<"Error: "<"<2) nside = atoi(tokens[2].c_str()); if(DefTypMap&HealPix && !IsNSideGood(nside)) {cout<<"Cl2Map: Bad nside for HealPix "<3) fwhm = atof(tokens[3].c_str()); SphericalMap* map = NULL; if(DefTypMap&HealPix) map = new SphereHEALPix; else if(DefTypMap&ThetaPhi) map = new SphereThetaPhi; else return; SphericalTransformServer ylmserver; cout<<"Cl2Map: Generate map "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"< ylmserver; int_4 nside = map->SizeIndex(); if(lmax<=0) lmax = 3*nside-1; cout<<"Map2Alm: "< "< >* almmat = dynamic_cast >*>(obj); if(almmat==NULL) { cout<<"Error: "< >"<NRows()-1; Alm alm(lmax); alm = (complex)0.; for(int i=0;i2) nside = atoi(tokens[2].c_str()); if(DefTypMap&HealPix && !IsNSideGood(nside)) {cout<<"Alm2Map: Bad nside for HealPix "<* map = NULL; if(DefTypMap&HealPix) map = new SphereHEALPix; else if(DefTypMap&ThetaPhi) map = new SphereThetaPhi; else return; SphericalTransformServer ylmserver; cout<<"Alm2Map: Generate map "<& tokens) { NamedObjMgr omg; int_4 nside = atoi(tokens[1].c_str()); if(DefTypMap&HealPix && !IsNSideGood(nside)) {cout<<"CrMapMask: Bad nside "<* map = NULL; if(DefTypMap&HealPix) map = new SphereHEALPix(nside); else if(DefTypMap&ThetaPhi) map = new SphereThetaPhi(nside); else return; bool lat=false, lon=false; double lat1=-99., lat2=99., lon1=-999., lon2=999.; if(tokens.size()>2) if(tokens[2][0]!='!') {lat=true; sscanf(tokens[2].c_str(),"%lf,%lf",&lat1,&lat2);} if(tokens.size()>3) if(tokens[3][0]!='!') {lon=true; sscanf(tokens[3].c_str(),"%lf,%lf",&lon1,&lon2);} double valmsk=0., unvalmsk=1.; if(tokens.size()>4) sscanf(tokens[4].c_str(),"%lf,%lf",&valmsk,&unvalmsk); cout<<"CrMapMask "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<* msk = NULL; if(t&HealPix) msk = new SphereHEALPix(nside); else if(t&ThetaPhi) msk = new SphereThetaPhi(nside); else return; double v1=-1.e100, v2=1.e100; if(tokens.size()>3) if(tokens[3][0]!='!') sscanf(tokens[3].c_str(),"%lf,%lf",&v1,&v2); double valmsk=0., unvalmsk=1.; if(tokens.size()>4) sscanf(tokens[4].c_str(),"%lf,%lf",&valmsk,&unvalmsk); cout<<"CrMaskFrMap "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<* msk = dynamic_cast*>(objm); if(msk==NULL) { cout<<"Error: "<"<NbPixels();i++) { double theta,phi; map->PixThetaPhi(i,theta,phi); int_4 ip = msk->PixIndexSph(theta,phi); if(ip<0 || ip>=msk->NbPixels()) continue; (*map)(i) *= (*msk)(ip); } } /* --Methode-- */ void skymapmoduleExecutor::MapProj(vector& tokens) { NamedObjMgr omg; AnyDataObj* obj=omg.GetObj(tokens[0]); if(obj==NULL) { cout<<"Error: Pas d'objet de nom "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<SizeIndex(); uint_2 t = typemap(obj); int_4 nside = nsidemap; if(tokens.size()>2) nside = atoi(tokens[2].c_str()); if(t&HealPix && !IsNSideGood(nside)) {cout<<"MapProj: Bad nside for Healpix "<* mapproj = NULL; if(t&HealPix) mapproj = new SphereHEALPix(nside); else if(t&ThetaPhi) mapproj = new SphereThetaPhi(nside); else return; cout<<"MapProj: project "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<2) sscanf(tokens[2].c_str(),"%d,%d",&nx,&ny); if(nx<=0) {cout<<"Error: nx="<0"<0"<3) sscanf(tokens[3].c_str(),"%lf,%lf",&angleX,&angleY); if(angleX<=0. || angleX>180.) {cout<<"Error: bad angleX="<180.) {cout<<"Error: bad angleY="<4) sscanf(tokens[4].c_str(),"%lf,%lf",&phi0,&theta0); if(phi0<0. || phi0>=360.) {cout<<"Error: bad phi0="<90.) {cout<<"Error: bad theta0="<5) if(tokens[5][0]!='!') sscanf(tokens[5].c_str(),"%d,%d",&x0,&y0); double angle=0.; if(tokens.size()>6) sscanf(tokens[6].c_str(),"%lf",&angle); cout<<"Map2Local: proj "<* lmap = new LocalMap(nx,ny,angleX,angleY,theta0,phi0,x0,y0,angle); *lmap = 0.; //lmap->print(cout); int_4 nbad=0; double tmax=-9999., pmax=-9999., tmin=9999., pmin=9999.; for(int_4 i=0;iNbPixels();i++) { double theta,phi; lmap->PixThetaPhi(i,theta,phi); int_4 ip = map->PixIndexSph(theta,phi); if(ip<0 || ip>=map->NbPixels()) {nbad++; continue;} if(thetatmax) tmax=theta; if(phipmax) pmax=phi; (*lmap)(i) = (*map)(ip); } if(nbad) cout<<"Warning: "<& tokens) { NamedObjMgr omg; AnyDataObj* obj=omg.GetObj(tokens[0]); if(obj==NULL) { cout<<"Error: Pas d'objet de nom "<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<* map1 = dynamic_cast*>(obj1); if(map1==NULL) {cout<<"Error: "<"<* map = dynamic_cast*>(obj); if(map==NULL) { cout<<"Error: "<"<* msk = NULL; if(tokens.size()>1) { if(tokens[1][0]!='!') { AnyDataObj* objm=omg.GetObj(tokens[1]); if(objm==NULL) { cout<<"Error: Pas d'objet de nom "<*>(objm); if(msk==NULL) { cout<<"Error: "<"<NbPixels(), npixgood=0; for(int_4 i=0;iPixThetaPhi(i,theta,phi); int_4 ip = msk->PixIndexSph(theta,phi); if(ip<0 || ip>=msk->NbPixels()) w=0.; else w=(*msk)(ip); } if(w<=0.) continue; npixgood++; sumw += w; sum += (*map)(i)*w; sum2 += (*map)(i)*(*map)(i)*w*w; } if(sumw<=0. || npixgood==0) { sum = sum2 = sumw=0.; } else { sum /= sumw; sum2 = sum2/sumw - sum*sum; if(sum2<=0.) sum2=0.; else sum2=sqrt(sum2); } cout<<"Number of good px="<3) if(tokens[3][0]!='!') { if(omg.HasVar(tokens[3])) omg.DeleteVar(tokens[3]); char str[128]; sprintf(str,"%.8f",sum2); omg.SetVar(tokens[3],(string)str); cout<<" sigma stored into $"<2) cout<& tokens) { if(tokens.size()<1) { cout<<"SetTypeMap: Usage: settypemap type"<1) {if(nside%2!=0) return false; else nside/=2;} return true; } /* --Methode-- */ void skymapmoduleExecutor::GetNSideGood(int_4 &nside) // get the nearest good nside for HealPix { double n=1.e50; int_4 ns=nside; for(int_4 i=1;i<66000;i*=2) { double v = fabs((double)(nside-i)); if(v>n) continue; n=v; ns=i; } nside = ns; } /* --Methode-- */ uint_2 skymapmoduleExecutor::typemap(AnyDataObj* obj) { if(obj==NULL) return UnKnown; if(dynamic_cast *>(obj)) return HealPix4; if(dynamic_cast *>(obj)) return HealPix8; if(dynamic_cast*>(obj)) return ThetaPhi4; if(dynamic_cast*>(obj)) return ThetaPhi8; if(dynamic_cast *>(obj)) return Spherical4; if(dynamic_cast *>(obj)) return Spherical8; return UnKnown; } //////////////////////////////////////////////////////////// static skymapmoduleExecutor * piaskmex = NULL; void skymapmodule_init() { if (piaskmex) delete piaskmex; piaskmex = new skymapmoduleExecutor; } void skymapmodule_end() { // Desactivation du module if (piaskmex) delete piaskmex; piaskmex = NULL; }