Healpix C++  3.83
map2tga_module.cc
1 /*
2  * This file is part of Healpix_cxx.
3  *
4  * Healpix_cxx is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Healpix_cxx is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Healpix_cxx; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * For more information about HEALPix, see http://healpix.sourceforge.net
19  */
20 
21 /*
22  * Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
23  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
24  * (DLR).
25  */
26 
27 /*
28  * Copyright (C) 2003-2019 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include <sstream>
33 #include <iomanip>
34 #include <cstdlib>
35 #include <memory>
36 #include "healpix_map.h"
37 #include "healpix_map_fitsio.h"
38 #include "rotmatrix.h"
39 #include "pointing.h"
40 #include "ls_image.h"
41 #include "paramfile.h"
42 #include "levels_facilities.h"
43 #include "lsconstants.h"
44 #include "announce.h"
45 #include "string_utils.h"
46 
47 using namespace std;
48 
49 namespace {
50 
51 void histo_eq (arr2<float> &img, float &minv, float &maxv, arr<double> &newpos)
52  {
53  const int nbins=100;
54  arr<int> bincnt (nbins);
55  bincnt.fill(0);
56  int pixels=0;
57 
58  double fact = 1./(maxv-minv);
59  for (tsize i=0; i<img.size1(); ++i)
60  for (tsize j=0; j<img.size2(); ++j)
61  if (img[i][j]>-1e30)
62  {
63  img[i][j] = float((img[i][j]-minv)*fact);
64  int idx = int(img[i][j]*nbins);
65  idx=max(0,min(idx,nbins-1));
66  ++bincnt[idx];
67  ++pixels;
68  }
69 
70  newpos.alloc(nbins+1);
71  int accu=0;
72  for (int m=0; m<nbins; ++m)
73  {
74  newpos[m] = double(accu)/pixels;
75  accu += bincnt[m];
76  }
77  newpos[nbins]=1.;
78 
79  for (tsize i=0; i<img.size1(); ++i)
80  for (tsize j=0; j<img.size2(); ++j)
81  if (img[i][j]>-1e30)
82  {
83  int idx = int(img[i][j]*nbins);
84  idx=max(0,min(idx,nbins-1));
85  double frac = nbins*img[i][j] - idx;
86  img[i][j] = float((1-frac)*newpos[idx] + frac*newpos[idx+1]);
87  img[i][j] = float(minv+(maxv-minv)*img[i][j]);
88  }
89  }
90 
91 void pro_mollw (const Healpix_Map<float> &map, double lon0, double lat0,
92  int xsize, arr2<float> &img, float &minv, float &maxv, bool smooth)
93  {
94  int ysize=xsize/2;
95  img.alloc(xsize,ysize);
96  img.fill(-1e35);
97  double xc=(xsize-1)/2., yc=(ysize-1)/2.;
98 
99  rotmatrix rot;
100  rot.Make_CPAC_Euler_Matrix(lon0,-lat0,0);
101 
102  minv=1e30;
103  maxv=-1e30;
104  for (tsize i=0; i<img.size1(); ++i)
105  for (tsize j=0; j<img.size2(); ++j)
106  {
107  double u = 2*(i-xc)/(xc/1.02);
108  double v = (j-yc)/(yc/1.02);
109  bool mask = ((u*u/4 + v*v) <= 1);
110  if (mask)
111  {
112  pointing ptg (halfpi-(asin(2/pi*(asin(v) + v*sqrt((1-v)*(1+v))))),
113  -halfpi*u/max(sqrt((1-v)*(1+v)),1e-6));
114  vec3 pnt = rot.Transform(ptg.to_vec3());
115  if (smooth)
116  img[i][j] = map.interpolated_value(pnt);
117  else
118  img[i][j] = map[map.ang2pix(pnt)];
119  if (!approx<double>(img[i][j],Healpix_undef))
120  {
121  if (img[i][j]<minv) minv=img[i][j];
122  if (img[i][j]>maxv) maxv=img[i][j];
123  }
124  }
125  }
126  }
127 
128 void pro_gno (const Healpix_Map<float> &map, double lon0, double lat0,
129  int xsize, double delta, arr2<float> &img, float &minv, float &maxv,
130  bool smooth)
131  {
132  rotmatrix rot;
133  rot.Make_CPAC_Euler_Matrix(lon0,-lat0,0);
134 
135  double start=-(xsize/2.)*delta;
136  img.alloc(xsize,xsize);
137  minv=1e30;
138  maxv=-1e30;
139  for (tsize i=0; i<img.size1(); ++i)
140  for (tsize j=0; j<img.size2(); ++j)
141  {
142  vec3 pnt (1,-(start+i*delta), start+j*delta);
143  pnt = rot.Transform(pnt);
144  if (smooth)
145  img[i][j] = map.interpolated_value(pnt);
146  else
147  img[i][j] = map[map.ang2pix(pnt)];
148  if (!approx<double>(img[i][j],Healpix_undef))
149  {
150  if (img[i][j]<minv) minv=img[i][j];
151  if (img[i][j]>maxv) maxv=img[i][j];
152  }
153  }
154  }
155 
156 void colorbar (LS_Image &img, const Palette &pal, int xmin, int xmax,
157  int ymin, int ymax, bool flippal, const arr<double> &newpos)
158  {
159  int nbins = newpos.size()-1;
160  for (int i=xmin; i<=xmax; ++i)
161  {
162  double val = (double(i)-xmin)/(xmax-xmin);
163  if (nbins>0)
164  {
165  int idx = int(val*nbins);
166  idx=max(0,min(idx,nbins-1));
167  double frac = nbins*val - idx;
168  val = (1-frac)*newpos[idx] + frac*newpos[idx+1];
169  }
170  if (flippal) val=1-val;
171  Colour c = pal.Get_Colour(float(val));
172  for (int j=ymin; j<=ymax; ++j)
173  img.put_pixel(i,j,c);
174  }
175  }
176 
177 string conv (double val)
178  {
179  ostringstream os;
180  if (abs(val)>100 || abs(val)<0.01)
181  {
182  os << setw(10) << setprecision(3) << scientific << val;
183  return os.str();
184  }
185  os << setw(10) << setprecision(6) << fixed << val;
186  return trim(os.str());
187  }
188 
189 } // unnamed namespace
190 
191 int map2tga_module (int argc, const char **argv)
192  {
193  module_startup ("map2tga",argc>=2,
194  "\nUsage:\n"
195  " map2tga <init object>\n\n"
196  "or:\n"
197  " map2tga <input file> <output file> [-sig <int>] [-pal <int>]\n"
198  " [-xsz <int>] [-bar] [-log] [-asinh] [-lon <float>] [-lat <float>]\n"
199  " [-mul <float>] [-add <float>] [-min <float>] [-max <float>]\n"
200  " [-res <float>] [-title <string>] [-flippal] [-gnomonic]\n"
201  " [-interpol] [-equalize] [-viewer <viewer>]\n\n");
202 
203  unique_ptr<paramfile> params;
204  if (argc>2)
205  {
206  vector<string> leading;
207  map<string,string> dict;
208  leading.push_back("infile");
209  leading.push_back("outfile");
210  parse_cmdline_classic (argc,argv,leading,dict);
211  if (dict.find("gnomonic")!=dict.end())
212  {
213  dict["pro"]="gno";
214  dict.erase("gnomonic");
215  }
216  params.reset(new paramfile(dict,false));
217  }
218  else
219  params.reset(new paramfile(argv[1]));
220 
221  string infile = params->find<string>("infile");
222  string outfile = params->find<string>("outfile");
223  int colnum = params->find<int>("sig",1);
224  int palnr = params->find<int>("pal",4);
225  bool flippal = params->find<bool>("flippal",false);
226  int xres = params->find<int>("xsz",1024);
227  bool bar = params->find<bool>("bar",false);
228  bool logflag = params->find<bool>("log",false);
229  bool eqflag = params->find<bool>("equalize",false);
230  bool asinhflag = params->find<bool>("asinh",false);
231  double lon0 = degr2rad*params->find<double>("lon",0);
232  double lat0 = degr2rad*params->find<double>("lat",0);
233  double factor = params->find<float>("mul",1);
234  double offset = params->find<float>("add",0);
235  float usermin = params->find<float>("min", -1e30);
236  bool min_supplied = (usermin>-1e29);
237  float usermax = params->find<float>("max", 1e30);
238  bool max_supplied = (usermax<1e29);
239  double res = arcmin2rad*params->find<double>("res",1);
240  string title = params->find<string>("title","");
241  string viewer = params->find<string>("viewer","");
242  bool mollpro = (params->find<string>("pro","")!="gno");
243  bool interpol = params->find<bool>("interpol",false);
244 
245  Healpix_Map<float> map(0,RING);
246  read_Healpix_map_from_fits(infile,map,colnum,2);
247  for (int m=0; m<map.Npix(); ++m)
248  {
249  if (!approx<double>(map[m],Healpix_undef))
250  {
251  map[m] = float((map[m]+offset)*factor);
252  if (logflag)
253  map[m] = (map[m]<=0) ? float(Healpix_undef)
254  : float(log(double(map[m]))/ln10);
255  if (asinhflag)
256  map[m] = (map[m]>=0) ?
257  float( log(double( map[m]+sqrt(map[m]*map[m]+1)))) :
258  float(-log(double(-map[m]+sqrt(map[m]*map[m]+1))));
259  if (min_supplied) if (map[m] < usermin) map[m] = usermin;
260  if (max_supplied) if (map[m] > usermax) map[m] = usermax;
261  }
262  }
263 
264  arr2<float> imgarr;
265  float minv, maxv;
266  mollpro ? pro_mollw (map,lon0,lat0,xres,imgarr,minv,maxv,interpol) :
267  pro_gno (map,lon0,lat0,xres,res,imgarr,minv,maxv,interpol);
268 
269  arr<double> newpos;
270  if (eqflag) histo_eq(imgarr,minv,maxv,newpos);
271 
272  if (min_supplied) minv = usermin;
273  if (max_supplied) maxv = usermax;
274  if (maxv==minv) maxv=minv+1e-10f;
275 
276  int xsz=imgarr.size1();
277  int ysz=imgarr.size2();
278  int yofs=ysz;
279  int scale = max(1,xsz/800);
280  if (bar) ysz+=60*scale;
281  if (title != "") { ysz+=50*scale; yofs+=50*scale; }
282  LS_Image img(xsz,ysz);
283  img.fill(Colour(1,1,1));
284  img.set_font (giant_font);
285  Palette pal;
286  pal.setPredefined(palnr);
287  if (title != "")
288  img.annotate_centered(xsz/2,25*scale,Colour(0,0,0),title,scale);
289  for (tsize i=0; i<imgarr.size1(); ++i)
290  for (tsize j=0; j<imgarr.size2(); ++j)
291  {
292  if (imgarr[i][j]>-1e32)
293  {
294  Colour c(0.5,0.5,0.5);
295  if (!approx<double>(imgarr[i][j],Healpix_undef))
296  {
297  int col = int((imgarr[i][j]-minv)/(maxv-minv)*256);
298  col = min(255,max(col,0));
299  float colfrac = (imgarr[i][j]-minv)/(maxv-minv);
300  if (flippal) colfrac = 1-colfrac;
301  c = pal.Get_Colour(colfrac);
302  }
303  img.put_pixel(i,yofs-j-1,c);
304  }
305  }
306 
307  if (bar)
308  {
309  colorbar (img,pal,xsz/10,(xsz*9)/10,ysz-40*scale,ysz-20*scale,flippal,
310  newpos);
311  img.set_font (medium_bold_font);
312  img.annotate_centered (xsz/20,ysz-30*scale,Colour(0,0,0),conv(minv),scale);
313  img.annotate_centered ((xsz*19)/20,ysz-30*scale,Colour(0,0,0),conv(maxv),
314  scale);
315  }
316  img.write_TGA(outfile);
317 
318  if (viewer!="")
319  {
320  int retcode = system((viewer+" "+outfile).c_str());
321  if (retcode != 0)
322  cout << "Warning: viewer return code was " << retcode << endl;
323  }
324 
325  return 0;
326  }
I ang2pix(const pointing &ang) const
Definition: healpix_base.h:152
void read_Healpix_map_from_fits(fitshandle &inp, Healpix_Map< T > &map, int colnum)
T interpolated_value(const pointing &ptg) const
Definition: healpix_map.h:219
const double Healpix_undef
Healpix value representing "undefined".
Definition: healpix_map.h:39
I Npix() const
Definition: healpix_base.h:429

Generated on Wed Nov 13 2024 12:18:30 for Healpix C++