Healpix C++  3.83
udgrade_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) 2003-2012 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include "paramfile.h"
33 #include "healpix_map.h"
34 #include "healpix_map_fitsio.h"
35 #include "fitshandle.h"
36 #include "levels_facilities.h"
37 #include "geom_utils.h"
38 #include "xcomplex.h"
39 #include "announce.h"
40 
41 using namespace std;
42 
43 namespace {
44 
45 double alpha (const pointing &p1, const pointing &p2)
46  {
47  vec3 v1(p1), v2(p2);
48  vec3 dir(v2-v1);
49  return orientation (v2, dir) - orientation (v1,dir);
50  }
51 
52 template<typename T> void degrade_pol (const Healpix_Map<T> &q1,
53  const Healpix_Map<T> &u1, Healpix_Map<T> &q2, Healpix_Map<T> &u2,
54  bool pessimistic)
55  {
56  planck_assert(q1.conformable(u1) && q2.conformable(u2), "map mismatch");
57  planck_assert(q2.Nside()<q1.Nside(),"this is no degrade");
58  int fact = q1.Nside()/q2.Nside();
59  planck_assert (q1.Nside()==q2.Nside()*fact,
60  "the larger Nside must be a multiple of the smaller one");
61 
62  int minhits = pessimistic ? fact*fact : 1;
63 #pragma omp parallel
64 {
65  int m, npix=q2.Npix();
66 #pragma omp for schedule (static)
67  for (m=0; m<npix; ++m)
68  {
69  int x,y,f;
70  q2.pix2xyf(m,x,y,f);
71  int hits = 0;
72  xcomplex<double> sum = 0;
73  for (int j=fact*y; j<fact*(y+1); ++j)
74  for (int i=fact*x; i<fact*(x+1); ++i)
75  {
76  int opix = q1.xyf2pix(i,j,f);
77  if (!(approx<double>(q1[opix],Healpix_undef)
78  ||approx<double>(u1[opix],Healpix_undef)))
79  {
80  ++hits;
81  xcomplex<double> val(q1[opix],u1[opix]);
82  double ang=alpha(q2.pix2ang(m),q1.pix2ang(opix));
83  xcomplex<double> mul(cos(2*ang),sin(2*ang));
84  sum += val*mul;
85  }
86  }
87  q2[m] = T((hits<minhits) ? Healpix_undef : sum.real()/hits);
88  u2[m] = T((hits<minhits) ? Healpix_undef : sum.imag()/hits);
89  }
90 }
91  }
92 
93 template<typename T> void udgrade_cxx (paramfile &params)
94  {
95  string infile = params.template find<string>("infile");
96  string outfile = params.template find<string>("outfile");
97  int nside = params.template find<int>("nside");
98  bool polarisation = params.template find<bool>("polarisation",false);
99  bool pessimistic = params.template find<bool>("pessimistic",false);
100 
101  if (!polarisation)
102  {
103  Healpix_Map<T> inmap;
104  read_Healpix_map_from_fits(infile,inmap,1,2);
105  Healpix_Map<T> outmap (nside,inmap.Scheme(),SET_NSIDE);
106 
107  outmap.Import(inmap,pessimistic);
108  write_Healpix_map_to_fits (outfile,outmap,planckType<T>());
109  }
110  else
111  {
112  Healpix_Map<T> inmap;
113  read_Healpix_map_from_fits(infile,inmap,1,2);
114  Healpix_Map<T> outmapT (nside,inmap.Scheme(),SET_NSIDE),
115  outmapQ (nside,inmap.Scheme(),SET_NSIDE),
116  outmapU (nside,inmap.Scheme(),SET_NSIDE);
117 // planck_assert(inmap.Nside()<=outmapT.Nside(),
118 // "degrading not supported for polarised maps");
119 
120  outmapT.Import(inmap,pessimistic);
121  if ((outmapQ.Nside()<inmap.Nside())
122  && params.template find<bool>("parallel_transport",true))
123  {
124 cout << "Experimental: polarised degrade with parallel transport" << endl;
125  read_Healpix_map_from_fits(infile,inmap,2,2);
126  Healpix_Map<T> inmap2;
127  read_Healpix_map_from_fits(infile,inmap2,3,2);
128  degrade_pol (inmap, inmap2, outmapQ, outmapU, pessimistic);
129  }
130  else
131  {
132 cout << "WARNING: polarised degrade without parallel transport" << endl;
133  read_Healpix_map_from_fits(infile,inmap,2,2);
134  outmapQ.Import(inmap,pessimistic);
135  read_Healpix_map_from_fits(infile,inmap,3,2);
136  outmapU.Import(inmap,pessimistic);
137  }
138  write_Healpix_map_to_fits (outfile,outmapT,outmapQ,outmapU,planckType<T>());
139  }
140  }
141 
142 } // unnamed namespace
143 
144 int udgrade_cxx_module (int argc, const char **argv)
145  {
146  module_startup ("udgrade_cxx", argc, argv);
147  paramfile params (getParamsFromCmdline(argc,argv));
148 
149  bool dp = params.find<bool> ("double_precision",false);
150  dp ? udgrade_cxx<double>(params) : udgrade_cxx<float>(params);
151 
152  return 0;
153  }
I Nside() const
Definition: healpix_base.h:427
void read_Healpix_map_from_fits(fitshandle &inp, Healpix_Map< T > &map, int colnum)
void Import(const Healpix_Map< T > &orig, bool pessimistic=false)
Definition: healpix_map.h:162
Healpix_Ordering_Scheme Scheme() const
Definition: healpix_base.h:431
void write_Healpix_map_to_fits(fitshandle &out, const Healpix_Map< T > &map, PDT datatype)
const double Healpix_undef
Healpix value representing "undefined".
Definition: healpix_map.h:39
bool conformable(const T_Healpix_Base &other) const
Definition: healpix_base.h:435
pointing pix2ang(I pix) const
Definition: healpix_base.h:185
I Npix() const
Definition: healpix_base.h:429

Generated on Wed Nov 13 2024 12:18:30 for Healpix C++