healpix_map.cc 3.81 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 *  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.sourceforge.net
 */

/*
 *  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-2020 Max-Planck-Society
 *  Author: Martin Reinecke
 */

#include "healpix_map.h"

using namespace std;

namespace mr {

namespace healpix {

Martin Reinecke's avatar
Martin Reinecke committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
template<typename T> void Healpix_Map<T>::Import_degrade
  (const Healpix_Map<T> &orig, bool pessimistic)
  {
  MR_assert(nside_<orig.nside_,"Import_degrade: this is no degrade");
  int fact = orig.nside_/nside_;
  MR_assert (orig.nside_==nside_*fact,
    "the larger Nside must be a multiple of the smaller one");

  int minhits = pessimistic ? fact*fact : 1;
#pragma omp parallel
{
  int m;
#pragma omp for schedule (static)
  for (m=0; m<npix_; ++m)
    {
    int x,y,f;
    pix2xyf(m,x,y,f);
    int hits = 0;
    tree_adder<double> adder;
    for (int j=fact*y; j<fact*(y+1); ++j)
      for (int i=fact*x; i<fact*(x+1); ++i)
        {
        int opix = orig.xyf2pix(i,j,f);
        if (!approx<double>(orig.map[opix],Healpix_undef))
          {
          ++hits;
          adder.add(orig.map[opix]);
          }
        }
    map[m] = T((hits<minhits) ? Healpix_undef : adder.result()/hits);
    }
}
  }

template void Healpix_Map<float>::Import_degrade
  (const Healpix_Map<float> &orig, bool pessimistic);
template void Healpix_Map<double>::Import_degrade
  (const Healpix_Map<double> &orig, bool pessimistic);
Martin Reinecke's avatar
Martin Reinecke committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

template<typename T> void Healpix_Map<T>::minmax (T &Min, T &Max) const
  {
  Min = T(1e30); Max = T(-1e30);
  for (int m=0; m<npix_; ++m)
    {
    T val = map[m];
    if (!approx<double>(val,Healpix_undef))
      {
      if (val>Max) Max=val;
      if (val<Min) Min=val;
      }
    }
  }

template void Healpix_Map<float>::minmax (float &Min, float &Max) const;
template void Healpix_Map<double>::minmax (double &Min, double &Max) const;

namespace {

template<typename Iterator> typename iterator_traits<Iterator>::value_type
  mymedian(Iterator first, Iterator last)
  {
  Iterator mid = first+(last-first-1)/2;
  nth_element(first,mid,last);
  if ((last-first)&1) return *mid;
  return typename iterator_traits<Iterator>::value_type
    (0.5*((*mid)+(*min_element(mid+1,last))));
  }

} // unnamed namespace

template<typename T> Healpix_Map<T> Healpix_Map<T>::median (double rad) const
  {
  Healpix_Map<T> out(Nside(), Scheme(), SET_NSIDE);

  rangeset<int> pixset;
  vector<T> list;
  for (int m=0; m<Npix(); ++m)
    {
    query_disc(pix2ang(m),rad,pixset);
    list.resize(pixset.nval());
    size_t cnt=0;
    for (size_t j=0; j<pixset.nranges(); ++j)
      for (int i=pixset.ivbegin(j); i<pixset.ivend(j); ++i)
        if (!approx(map[i], T(Healpix_undef)))
          list[cnt++] = map[i];
    out[m] = (cnt>0) ? mymedian(list.begin(),list.begin()+cnt)
Martin Reinecke's avatar
Martin Reinecke committed
126
                     : T(Healpix_undef);
Martin Reinecke's avatar
Martin Reinecke committed
127 128 129 130 131 132 133 134
    }
  return out;
  }

template Healpix_Map<float> Healpix_Map<float>::median (double rad) const;
template Healpix_Map<double> Healpix_Map<double>::median (double rad) const;

}}