00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef HEALPIX_MAP_H
00033 #define HEALPIX_MAP_H
00034
00035 #include "arr.h"
00036 #include "healpix_base.h"
00037
00038
00039 const double Healpix_undef=-1.6375e30;
00040
00041
00042 template<typename T> class Healpix_Map: public Healpix_Base
00043 {
00044 private:
00045 arr<T> map;
00046
00047 public:
00048
00049 Healpix_Map () {}
00050
00051
00052 Healpix_Map (int order, Healpix_Ordering_Scheme scheme)
00053 : Healpix_Base (order, scheme), map(npix_) {}
00054
00055
00056 Healpix_Map (int nside, Healpix_Ordering_Scheme scheme, const nside_dummy)
00057 : Healpix_Base (nside, scheme, SET_NSIDE), map(npix_) {}
00058
00059
00060
00061 Healpix_Map (const arr<T> &data, Healpix_Ordering_Scheme scheme)
00062 : Healpix_Base (npix2nside(data.size()), scheme, SET_NSIDE), map(data) {}
00063
00064
00065
00066
00067
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
00075
00076 void Set (int order, Healpix_Ordering_Scheme scheme)
00077 {
00078 Healpix_Base::Set(order, scheme);
00079 map.alloc(npix_);
00080 }
00081
00082
00083 void SetNside (int nside, Healpix_Ordering_Scheme scheme)
00084 {
00085 Healpix_Base::SetNside(nside, scheme);
00086 map.alloc(npix_);
00087 }
00088
00089
00090 void fill (const T &val)
00091 { map.fill(val); }
00092
00093
00094
00095
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
00116
00117
00118
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
00145
00146
00147
00148
00149
00150
00151
00152
00153 void Import_degrade (const Healpix_Map<T> &orig, bool pessimistic=false);
00154
00155
00156
00157
00158
00159
00160
00161
00162 void Import (const Healpix_Map<T> &orig, bool pessimistic=false)
00163 {
00164 if (orig.nside_ == nside_)
00165 Import_nograde(orig);
00166 else if (orig.nside_ < nside_)
00167 Import_upgrade(orig);
00168 else
00169 Import_degrade(orig,pessimistic);
00170 }
00171
00172
00173 const T &operator[] (int pix) const { return map[pix]; }
00174
00175 T &operator[] (int pix) { return map[pix]; }
00176
00177
00178
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
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
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
00227 const arr<T> &Map() const { return map; }
00228
00229
00230
00231
00232
00233 void minmax (T &Min, T &Max) const;
00234
00235
00236 void swap (Healpix_Map &other)
00237 {
00238 Healpix_Base::swap(other);
00239 map.swap(other.map);
00240 }
00241
00242
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
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
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
00270
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
00283
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
00295
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
00304
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