healpix_map.h

Go to the documentation of this file.
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 /*! \file healpix_map.h
00028  *  Copyright (C) 2003-2013 Max-Planck-Society
00029  *  \author Martin Reinecke
00030  */
00031 
00032 #ifndef HEALPIX_MAP_H
00033 #define HEALPIX_MAP_H
00034 
00035 #include "arr.h"
00036 #include "healpix_base.h"
00037 
00038 //! Healpix value representing "undefined"
00039 const double Healpix_undef=-1.6375e30;
00040 
00041 /*! A HEALPix map of a given datatype */
00042 template<typename T> class Healpix_Map: public Healpix_Base
00043   {
00044   private:
00045     arr<T> map;
00046 
00047   public:
00048     /*! Constructs an unallocated map. */
00049     Healpix_Map () {}
00050     /*! Constructs a map with a given \a order and the ordering
00051         scheme \a scheme. */
00052     Healpix_Map (int order, Healpix_Ordering_Scheme scheme)
00053       : Healpix_Base (order, scheme), map(npix_) {}
00054     /*! Constructs a map with a given \a nside and the ordering
00055         scheme \a scheme. */
00056     Healpix_Map (int nside, Healpix_Ordering_Scheme scheme, const nside_dummy)
00057       : Healpix_Base (nside, scheme, SET_NSIDE), map(npix_) {}
00058     /*! Constructs a map from the contents of \a data and sets the ordering
00059         scheme to \a Scheme. The size of \a data must be a valid HEALPix
00060         map size. */
00061     Healpix_Map (const arr<T> &data, Healpix_Ordering_Scheme scheme)
00062       : Healpix_Base (npix2nside(data.size()), scheme, SET_NSIDE), map(data) {}
00063 
00064     /*! Deletes the old map, creates a map from the contents of \a data and
00065         sets the ordering scheme to \a scheme. The size of \a data must be a
00066         valid HEALPix map size.
00067         \note On exit, \a data is zero-sized! */
00068     void Set (arr<T> &data, Healpix_Ordering_Scheme scheme)
00069       {
00070       Healpix_Base::SetNside(npix2nside (data.size()), scheme);
00071       map.transfer(data);
00072       }
00073 
00074     /*! Deletes the old map and creates a new map  with a given \a order
00075         and the ordering scheme \a scheme. */
00076     void Set (int order, Healpix_Ordering_Scheme scheme)
00077       {
00078       Healpix_Base::Set(order, scheme);
00079       map.alloc(npix_);
00080       }
00081     /*! Deletes the old map and creates a new map  with a given \a nside
00082         and the ordering scheme \a scheme. */
00083     void SetNside (int nside, Healpix_Ordering_Scheme scheme)
00084       {
00085       Healpix_Base::SetNside(nside, scheme);
00086       map.alloc(npix_);
00087       }
00088 
00089     /*! Fills the map with \a val. */
00090     void fill (const T &val)
00091       { map.fill(val); }
00092 
00093     /*! Imports the map \a orig into the current map, adjusting the
00094         ordering scheme. \a orig must have the same resolution as the
00095         current map. */
00096     void Import_nograde (const Healpix_Map<T> &orig)
00097       {
00098       planck_assert (nside_==orig.nside_,
00099         "Import_nograde: maps have different nside");
00100       if (orig.scheme_ == scheme_)
00101         for (int m=0; m<npix_; ++m) map[m] = orig.map[m];
00102       else
00103         {
00104         swapfunc swapper = (scheme_ == NEST) ?
00105           &Healpix_Base::ring2nest : &Healpix_Base::nest2ring;
00106 #pragma omp parallel
00107 {
00108         int m;
00109 #pragma omp for schedule (dynamic,5000)
00110         for (m=0; m<npix_; ++m) map[(this->*swapper)(m)] = orig.map[m];
00111 }
00112         }
00113       }
00114 
00115     /*! Imports the map \a orig into the current map, adjusting the
00116         ordering scheme and the map resolution. \a orig must have lower
00117         resolution than the current map, and \a this->Nside() must be an
00118         integer multiple of \a orig.Nside(). */
00119     void Import_upgrade (const Healpix_Map<T> &orig)
00120       {
00121       planck_assert(nside_>orig.nside_,"Import_upgrade: this is no upgrade");
00122       int fact = nside_/orig.nside_;
00123       planck_assert (nside_==orig.nside_*fact,
00124         "the larger Nside must be a multiple of the smaller one");
00125 
00126 #pragma omp parallel
00127 {
00128       int m;
00129 #pragma omp for schedule (dynamic,5000)
00130       for (m=0; m<orig.npix_; ++m)
00131         {
00132         int x,y,f;
00133         orig.pix2xyf(m,x,y,f);
00134         for (int j=fact*y; j<fact*(y+1); ++j)
00135           for (int i=fact*x; i<fact*(x+1); ++i)
00136             {
00137             int mypix = xyf2pix(i,j,f);
00138             map[mypix] = orig.map[m];
00139             }
00140         }
00141 }
00142       }
00143 
00144     /*! Imports the map \a orig into the current map, adjusting the
00145         ordering scheme and the map resolution. \a orig must have higher
00146         resolution than the current map, and \a orig.Nside() must be an
00147         integer multiple of \a this->Nside().
00148         \a pessimistic determines whether or not
00149         pixels are set to \a Healpix_undef when not all of the corresponding
00150         high-resolution pixels are defined.
00151 
00152         This method is instantiated for \a float and \a double only. */
00153     void Import_degrade (const Healpix_Map<T> &orig, bool pessimistic=false);
00154 
00155     /*! Imports the map \a orig into the current map, adjusting the
00156         ordering scheme and the map resolution if necessary.
00157         When downgrading, \a pessimistic determines whether or not
00158         pixels are set to \a Healpix_undef when not all of the corresponding
00159         high-resolution pixels are defined.
00160 
00161         This method is instantiated for \a float and \a double only. */
00162     void Import (const Healpix_Map<T> &orig, bool pessimistic=false)
00163       {
00164       if (orig.nside_ == nside_) // no up/degrading
00165         Import_nograde(orig);
00166       else if (orig.nside_ < nside_) // upgrading
00167         Import_upgrade(orig);
00168       else
00169         Import_degrade(orig,pessimistic);
00170       }
00171 
00172     /*! Returns a constant reference to the pixel with the number \a pix. */
00173     const T &operator[] (int pix) const { return map[pix]; }
00174     /*! Returns a reference to the pixel with the number \a pix. */
00175     T &operator[] (int pix) { return map[pix]; }
00176 
00177     /*! Swaps the map ordering from RING to NEST and vice versa.
00178         This is done in-place (i.e. with negligible space overhead). */
00179     void swap_scheme()
00180       {
00181       swapfunc swapper = (scheme_ == NEST) ?
00182         &Healpix_Base::ring2nest : &Healpix_Base::nest2ring;
00183 
00184       arr<int> cycle=swap_cycles();
00185 
00186       for (tsize m=0; m<cycle.size(); ++m)
00187         {
00188         int istart = cycle[m];
00189 
00190         T pixbuf = map[istart];
00191         int iold = istart, inew = (this->*swapper)(istart);
00192         while (inew != istart)
00193           {
00194           map[iold] = map[inew];
00195           iold = inew;
00196           inew = (this->*swapper)(inew);
00197           }
00198         map[iold] = pixbuf;
00199         }
00200       scheme_ = (scheme_==RING) ? NEST : RING;
00201       }
00202 
00203     /*! performs the actual interpolation using \a pix and \a wgt. */
00204     T interpolation (const fix_arr<int,4> &pix,
00205       const fix_arr<double,4> &wgt) const
00206       {
00207       double wtot=0;
00208       T res=T(0);
00209       for (tsize i=0; i<4; ++i)
00210         {
00211         T val=map[pix[i]];
00212         if (!approx<double>(val,Healpix_undef))
00213           { res+=T(val*wgt[i]); wtot+=wgt[i]; }
00214         }
00215       return (wtot==0.) ? T(Healpix_undef) : T(res/wtot);
00216       }
00217     /*! Returns the interpolated map value at \a ptg */
00218     T interpolated_value (const pointing &ptg) const
00219       {
00220       fix_arr<int,4> pix;
00221       fix_arr<double,4> wgt;
00222       get_interpol (ptg, pix, wgt);
00223       return interpolation (pix, wgt);
00224       }
00225 
00226     /*! Returns a constant reference to the map data. */
00227     const arr<T> &Map() const { return map; }
00228 
00229     /*! Returns the minimum and maximum value of the map in
00230         \a Min and \a Max.
00231 
00232         This method is instantiated for \a float and \a double only. */
00233     void minmax (T &Min, T &Max) const;
00234 
00235     /*! Swaps the contents of two Healpix_Map objects. */
00236     void swap (Healpix_Map &other)
00237       {
00238       Healpix_Base::swap(other);
00239       map.swap(other.map);
00240       }
00241 
00242     /*! Returns the average of all defined map pixels. */
00243     double average() const
00244       {
00245       kahan_adder<double> adder;
00246       int pix=0;
00247       for (int m=0; m<npix_; ++m)
00248         if (!approx<double>(map[m],Healpix_undef))
00249           { ++pix; adder.add(map[m]); }
00250       return (pix>0) ? adder.result()/pix : Healpix_undef;
00251       }
00252 
00253     /*! Adds \a val to all defined map pixels. */
00254     void Add (T val)
00255       {
00256       for (int m=0; m<npix_; ++m)
00257         if (!approx<double>(map[m],Healpix_undef))
00258           { map[m]+=val; }
00259       }
00260 
00261     /*! Multiplies all defined map pixels by \a val. */
00262     void Scale (T val)
00263       {
00264       for (int m=0; m<npix_; ++m)
00265         if (!approx<double>(map[m],Healpix_undef))
00266           { map[m]*=val; }
00267       }
00268 
00269     /*! Returns the root mean square of the map, not counting undefined
00270         pixels. */
00271     double rms() const
00272       {
00273       using namespace std;
00274 
00275       double result=0;
00276       int pix=0;
00277       for (int m=0; m<npix_; ++m)
00278         if (!approx<double>(map[m],Healpix_undef))
00279           { ++pix; result+=map[m]*map[m]; }
00280       return (pix>0) ? sqrt(result/pix) : Healpix_undef;
00281       }
00282     /*! Returns the maximum absolute value in the map, ignoring undefined
00283         pixels. */
00284     T absmax() const
00285       {
00286       using namespace std;
00287 
00288       T result=0;
00289       for (int m=0; m<npix_; ++m)
00290         if (!approx<double>(map[m],Healpix_undef))
00291           { result = max(result,abs(map[m])); }
00292       return result;
00293       }
00294     /*! Returns \a true, if no pixel has the value \a Healpix_undef,
00295         else \a false. */
00296     bool fullyDefined() const
00297       {
00298       for (int m=0; m<npix_; ++m)
00299         if (approx<double>(map[m],Healpix_undef))
00300           return false;
00301       return true;
00302       }
00303     /*! Sets all pixels with the value \a Healpix_undef to 0, and returns
00304         the number of modified pixels. */
00305     tsize replaceUndefWith0()
00306       {
00307       tsize res=0;
00308       for (int m=0; m<npix_; ++m)
00309         if (approx<double>(map[m],Healpix_undef))
00310           { map[m]=0.; ++res; }
00311       return res;
00312       }
00313   };
00314 
00315 #endif

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