Healpix C++  3.50
median_filter_cxx_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) 2005-2012 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include "healpix_map.h"
33 #include "healpix_map_fitsio.h"
34 #include "fitshandle.h"
35 #include "levels_facilities.h"
36 #include "lsconstants.h"
37 #include "announce.h"
38 #include "string_utils.h"
39 
40 using namespace std;
41 
42 namespace {
43 
44 template<typename Iterator> typename iterator_traits<Iterator>::value_type
45  median(Iterator first, Iterator last)
46  {
47  Iterator mid = first+(last-first-1)/2;
48  nth_element(first,mid,last);
49  if ((last-first)&1) return *mid;
50  return typename iterator_traits<Iterator>::value_type
51  (0.5*((*mid)+(*min_element(mid+1,last))));
52  }
53 
54 } // unnamed namespace
55 
56 int median_filter_cxx_module (int argc, const char **argv)
57  {
58  module_startup ("median_filter_cxx", argc, argv, 4,
59  "<input map> <output map> <radius in arcmin>");
60 
61  double radius = stringToData<double>(argv[3])*arcmin2rad;
62 
63  Healpix_Map<float> inmap;
64  read_Healpix_map_from_fits(argv[1],inmap,1,2);
65  Healpix_Map<float> outmap (inmap.Nside(), inmap.Scheme(), SET_NSIDE);
66 
67  rangeset<int> pixset;
68  vector<float> list;
69  for (int m=0; m<inmap.Npix(); ++m)
70  {
71  inmap.query_disc(inmap.pix2ang(m),radius,pixset);
72  list.resize(pixset.nval());
73  tsize cnt=0;
74  for (tsize j=0; j<pixset.nranges(); ++j)
75  for (int i=pixset.ivbegin(j); i<pixset.ivend(j); ++i)
76  if (!approx(inmap[i],float(Healpix_undef)))
77  list[cnt++] = inmap[i];
78  outmap[m] = (cnt>0) ? median(list.begin(),list.begin()+cnt)
79  : Healpix_undef;
80  }
81 
82  write_Healpix_map_to_fits (argv[2],outmap,PLANCK_FLOAT32);
83 
84  return 0;
85  }
void read_Healpix_map_from_fits(fitshandle &inp, Healpix_Map< T > &map, int colnum)
std::size_t tsize
void module_startup(const std::string &name, bool argc_valid, const std::string &usage, bool verbose=true)
void write_Healpix_map_to_fits(fitshandle &out, const Healpix_Map< T > &map, PDT datatype)
bool approx(F a, F b, F epsilon=1e-5)
const double Healpix_undef
Healpix value representing "undefined".
Definition: healpix_map.h:39

Generated on Mon Dec 10 2018 10:24:22 for Healpix C++