/* * This file is part of Healpix_cxx. * * Healpix_cxx is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Healpix_cxx is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Healpix_cxx; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * For more information about HEALPix, see http://healpix.jpl.nasa.gov */ /* * Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt * (DLR). */ /* * Copyright (C) 2003-2010 Max-Planck-Society * Author: Martin Reinecke */ #include #include #include #include "healpix_map.h" #include "healpix_map_fitsio.h" #include "rotmatrix.h" #include "pointing.h" #include "ls_image.h" #include "paramfile.h" #include "levels_facilities.h" #include "lsconstants.h" using namespace std; namespace { void histo_eq (arr2 &img, float &minv, float &maxv, arr &newpos) { const int nbins=100; arr bincnt (nbins); bincnt.fill(0); int pixels=0; double fact = 1./(maxv-minv); for (tsize i=0; i-1e30) { img[i][j] = float((img[i][j]-minv)*fact); int idx = int(img[i][j]*nbins); idx=max(0,min(idx,nbins-1)); ++bincnt[idx]; ++pixels; } newpos.alloc(nbins+1); int accu=0; for (int m=0; m-1e30) { int idx = int(img[i][j]*nbins); idx=max(0,min(idx,nbins-1)); double frac = nbins*img[i][j] - idx; img[i][j] = float((1-frac)*newpos[idx] + frac*newpos[idx+1]); img[i][j] = float(minv+(maxv-minv)*img[i][j]); } } void pro_mollw (const Healpix_Map &map, double lon0, double lat0, int xsize, arr2 &img, float &minv, float &maxv, bool smooth) { int ysize=xsize/2; img.alloc(xsize,ysize); img.fill(-1e35); double xc=(xsize-1)/2., yc=(ysize-1)/2.; rotmatrix rot; rot.Make_CPAC_Euler_Matrix(0,-lat0,-lon0); minv=1e30; maxv=-1e30; for (tsize i=0; i(img[i][j],Healpix_undef)) { if (img[i][j]maxv) maxv=img[i][j]; } } } } void pro_gno (const Healpix_Map &map, double lon0, double lat0, int xsize, double delta, arr2 &img, float &minv, float &maxv, bool smooth) { rotmatrix rot; rot.Make_CPAC_Euler_Matrix(lon0,-lat0,0); double start=-(xsize/2.)*delta; img.alloc(xsize,xsize); minv=1e30; maxv=-1e30; for (tsize i=0; i(img[i][j],Healpix_undef)) { if (img[i][j]maxv) maxv=img[i][j]; } } } void colorbar (LS_Image &img, const Palette &pal, int xmin, int xmax, int ymin, int ymax, bool flippal, const arr &newpos) { int nbins = newpos.size()-1; for (int i=xmin; i<=xmax; ++i) { double val = (double(i)-xmin)/(xmax-xmin); if (nbins>0) { int idx = int(val*nbins); idx=max(0,min(idx,nbins-1)); double frac = nbins*val - idx; val = (1-frac)*newpos[idx] + frac*newpos[idx+1]; } if (flippal) val=1-val; Colour c = pal.Get_Colour(float(val)); for (int j=ymin; j<=ymax; ++j) img.put_pixel(i,j,c); } } string conv (double val) { ostringstream os; if (abs(val)>100 || abs(val)<0.01) { os << setw(10) << setprecision(3) << scientific << val; return os.str(); } os << setw(10) << setprecision(6) << fixed << val; return trim(os.str()); } void usage() { cout << "\nUsage:\n" " map2tga \n\n" "or:\n" " map2tga [-sig ] [-pal ]\n" " [-xsz ] [-bar] [-log] [-asinh] [-lon ] [-lat ]\n" " [-mul ] [-add ] [-min ] [-max ]\n" " [-res ] [-title ] [-flippal] [-gnomonic]\n" " [-interpol] [-equalize] [-viewer ]\n\n"; planck_fail_quietly("Incorrect usage"); } } // unnamed namespace int map2tga_module (int argc, const char **argv) { announce ("map2tga"); if (argc<2) usage(); string infile = ""; string outfile = ""; string title = ""; int xres = 1024; bool bar = false, mollpro=true; double lat0=0, lon0=0, res=1*arcmin2rad; float usermin=-1e30, usermax=1e30, offset=0, factor=1; bool min_supplied=false, max_supplied=false; bool logflag=false, asinhflag=false, eqflag=false; int palnr = 4; int colnum=1; bool flippal = false; bool interpol = false; string viewer = ""; if (argc>2) { infile = argv[1]; outfile = argv[2]; int m=3; while (m("infile"); outfile = params.find("outfile"); colnum = params.find("sig",colnum); palnr = params.find("pal",palnr); flippal = params.find("flippal",flippal); xres = params.find("xsz",xres); bar = params.find("bar",bar); logflag = params.find("log",logflag); eqflag = params.find("equalize",eqflag); asinhflag = params.find("asinh",asinhflag); lon0 = degr2rad*params.find("lon",lon0); lat0 = degr2rad*params.find("lat",lat0); factor = params.find("mul",factor); offset = params.find("add",offset); usermin = params.find("min", usermin); if (usermin>-1e29) min_supplied = true; usermax = params.find("max", usermax); if (usermax<1e29) max_supplied = true; res = arcmin2rad*params.find("res",res); title = params.find("title",""); viewer = params.find("viewer",""); string tmp = params.find("pro",""); if (tmp == "gno") mollpro=false; interpol = params.find("interpol",interpol); } Healpix_Map map(0,RING); read_Healpix_map_from_fits(infile,map,colnum,2); for (int m=0; m(map[m],Healpix_undef)) { map[m] = (map[m]+offset)*factor; if (logflag) { if (map[m]<=0) map[m] = Healpix_undef; else map[m] = float(log(double(map[m]))/ln10); } if (asinhflag) { if (map[m]>=0) map[m] = float(log(double(map[m]+sqrt(map[m]*map[m]+1)))); else map[m] = float(-log(double(-map[m]+sqrt(map[m]*map[m]+1)))); } if (min_supplied) if (map[m] < usermin) map[m] = usermin; if (max_supplied) if (map[m] > usermax) map[m] = usermax; } } arr2 imgarr; float minv, maxv; if (mollpro) pro_mollw (map,lon0,lat0,xres,imgarr,minv,maxv,interpol); else pro_gno (map,lon0,lat0,xres,res,imgarr,minv,maxv,interpol); arr newpos; if (eqflag) histo_eq(imgarr,minv,maxv,newpos); if (min_supplied) minv = usermin; if (max_supplied) maxv = usermax; if (maxv==minv) maxv=minv+1e-10f; int xsz=imgarr.size1(); int ysz=imgarr.size2(); int yofs=ysz; int scale = max(1,xsz/800); if (bar) ysz+=60*scale; if (title != "") { ysz+=50*scale; yofs+=50*scale; } LS_Image img(xsz,ysz); img.fill(Colour(1,1,1)); img.set_font (giant_font); Palette pal; pal.setPredefined(palnr); if (title != "") img.annotate_centered(xsz/2,25*scale,Colour(0,0,0),title,scale); for (tsize i=0; i-1e32) { Colour c(0.5,0.5,0.5); if (!approx(imgarr[i][j],Healpix_undef)) { int col = int((imgarr[i][j]-minv)/(maxv-minv)*256); col = min(255,max(col,0)); float colfrac = (imgarr[i][j]-minv)/(maxv-minv); if (flippal) colfrac = 1-colfrac; c = pal.Get_Colour(colfrac); } img.put_pixel(i,yofs-j-1,c); } } if (bar) { colorbar (img,pal,xsz/10,(xsz*9)/10,ysz-40*scale,ysz-20*scale,flippal, newpos); img.set_font (medium_bold_font); img.annotate_centered (xsz/20,ysz-30*scale,Colour(0,0,0),conv(minv),scale); img.annotate_centered ((xsz*19)/20,ysz-30*scale,Colour(0,0,0),conv(maxv), scale); } img.write_TGA(outfile); if (viewer!="") { int retcode = system((viewer+" "+outfile).c_str()); if (retcode != 0) cout << "Warning: viewer return code was " << retcode << endl; } return 0; }