median_filter_cxx_module.cc

00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  Healpix_cxx is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.sourceforge.net
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*
00028  *  Copyright (C) 2005-2012 Max-Planck-Society
00029  *  Author: Martin Reinecke
00030  */
00031 
00032 #include "healpix_map.h"
00033 #include "healpix_map_fitsio.h"
00034 #include "fitshandle.h"
00035 #include "levels_facilities.h"
00036 #include "lsconstants.h"
00037 #include "announce.h"
00038 #include "string_utils.h"
00039 
00040 using namespace std;
00041 
00042 namespace {
00043 
00044 template<typename Iterator> typename iterator_traits<Iterator>::value_type
00045   median(Iterator first, Iterator last)
00046   {
00047   Iterator mid = first+(last-first-1)/2;
00048   nth_element(first,mid,last);
00049   if ((last-first)&1) return *mid;
00050   return typename iterator_traits<Iterator>::value_type
00051     (0.5*((*mid)+(*min_element(mid+1,last))));
00052   }
00053 
00054 } // unnamed namespace
00055 
00056 int median_filter_cxx_module (int argc, const char **argv)
00057   {
00058   module_startup ("median_filter_cxx", argc, argv, 4,
00059                   "<input map> <output map> <radius in arcmin>");
00060 
00061   double radius = stringToData<double>(argv[3])*arcmin2rad;
00062 
00063   Healpix_Map<float> inmap;
00064   read_Healpix_map_from_fits(argv[1],inmap,1,2);
00065   Healpix_Map<float> outmap (inmap.Nside(), inmap.Scheme(), SET_NSIDE);
00066 
00067   rangeset<int> pixset;
00068   vector<float> list;
00069   for (int m=0; m<inmap.Npix(); ++m)
00070     {
00071     inmap.query_disc(inmap.pix2ang(m),radius,pixset);
00072     list.resize(pixset.nval());
00073     tsize cnt=0;
00074     for (tsize j=0; j<pixset.nranges(); ++j)
00075       for (int i=pixset.ivbegin(j); i<pixset.ivend(j); ++i)
00076         if (!approx(inmap[i],float(Healpix_undef)))
00077           list[cnt++] = inmap[i];
00078     outmap[m] = (cnt>0) ? median(list.begin(),list.begin()+cnt)
00079                         : Healpix_undef;
00080     }
00081 
00082   write_Healpix_map_to_fits (argv[2],outmap,PLANCK_FLOAT32);
00083 
00084   return 0;
00085   }

Generated on Thu Oct 8 14:48:52 2015 for Healpix C++