Healpix C++  3.83
anafast_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-2017 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include "xcomplex.h"
33 #include "paramfile.h"
34 #include "healpix_data_io.h"
35 #include "alm.h"
36 #include "alm_fitsio.h"
37 #include "healpix_map.h"
38 #include "healpix_map_fitsio.h"
39 #include "powspec.h"
40 #include "powspec_fitsio.h"
41 #include "weight_utils.h"
42 #include "alm_healpix_tools.h"
43 #include "alm_powspec_tools.h"
44 #include "fitshandle.h"
45 #include "levels_facilities.h"
46 #include "lsconstants.h"
47 #include "announce.h"
48 
49 using namespace std;
50 
51 namespace {
52 
53 template<typename T> void anafast_cxx (paramfile &params)
54  {
55  int nlmax = params.template find<int>("nlmax");
56  int nmmax = params.template find<int>("nmmax",nlmax);
57  string infile = params.template find<string>("infile");
58  string outfile = params.template find<string>("outfile","");
59  string outfile_alms = params.template find<string>("outfile_alms","");
60  planck_assert ((outfile!="") || (outfile_alms!=""),
61  "no output specified, nothing done");
62  bool polarisation = params.template find<bool>("polarisation");
63  int num_iter = params.template find<int>("iter_order",0);
64  bool remove_mono = params.template find<bool>("remove_monopole",false);
65  bool do_pwgt = params.param_present("pixelweights");
66  planck_assert(!(params.param_present("ringweights")&&do_pwgt),
67  "both pixel and ring weights specified");
68  planck_assert((num_iter==0)||(!do_pwgt),
69  "iterative analysis cannot be done in combination with pixel weights");
70 
71  if (!polarisation)
72  {
73  Healpix_Map<T> map;
74  read_Healpix_map_from_fits(infile,map,1,2);
75 
76  tsize nmod = map.replaceUndefWith0();
77  if (nmod!=0)
78  cout << "WARNING: replaced " << nmod <<
79  " undefined map pixels with a value of 0" << endl;
80 
81  double avg=0.;
82  if (remove_mono)
83  {
84  avg=map.average();
85  map.Add(T(-avg));
86  }
87 
88  if (do_pwgt)
89  {
90  auto pwgt = read_fullweights_from_fits(
91  params.template find<string>("pixelweights"), map.Nside());
92  apply_fullweights(map,pwgt);
93  }
94 
95  arr<double> weight;
96  get_ring_weights (params,map.Nside(),weight);
97 
98  Alm<xcomplex<T> > alm(nlmax,nmmax);
99  if (map.Scheme()==NEST) map.swap_scheme();
100  map2alm_iter(map,alm,num_iter,weight);
101 
102  alm(0,0) += T(avg*sqrt(fourpi));
103 
104  if (outfile!="")
105  {
106  PowSpec powspec;
107  extract_powspec (alm,powspec);
108  write_powspec_to_fits (outfile,powspec,1);
109  }
110  if (outfile_alms!="")
111  write_Alm_to_fits(outfile_alms,alm,nlmax,nmmax,planckType<T>());
112  }
113  else
114  {
115  Healpix_Map<T> mapT, mapQ, mapU;
116  read_Healpix_map_from_fits(infile,mapT,mapQ,mapU,2);
117 
118  tsize nmod = mapT.replaceUndefWith0()+mapQ.replaceUndefWith0()
119  +mapU.replaceUndefWith0();
120  if (nmod!=0)
121  cout << "WARNING: replaced " << nmod <<
122  " undefined map pixels with a value of 0" << endl;
123 
124  double avg=0.;
125  if (remove_mono)
126  {
127  avg=mapT.average();
128  mapT.Add(T(-avg));
129  }
130 
131  if (do_pwgt)
132  {
133  auto pwgt = read_fullweights_from_fits(
134  params.template find<string>("pixelweights"), mapT.Nside());
135  apply_fullweights(mapT,pwgt);
136  apply_fullweights(mapQ,pwgt);
137  apply_fullweights(mapU,pwgt);
138  }
139 
140  arr<double> weight;
141  get_ring_weights (params,mapT.Nside(),weight);
142 
143  Alm<xcomplex<T> > almT(nlmax,nmmax), almG(nlmax,nmmax), almC(nlmax,nmmax);
144  if (mapT.Scheme()==NEST) mapT.swap_scheme();
145  if (mapQ.Scheme()==NEST) mapQ.swap_scheme();
146  if (mapU.Scheme()==NEST) mapU.swap_scheme();
148  (mapT,mapQ,mapU,almT,almG,almC,num_iter,weight);
149 
150  almT(0,0) += T(avg*sqrt(fourpi));
151 
152  if (outfile!="")
153  {
154  PowSpec powspec;
155  extract_powspec (almT,almG,almC,powspec);
156  params.template find<bool>("full_powerspectrum",false) ?
157  write_powspec_to_fits (outfile,powspec,6) :
158  write_powspec_to_fits (outfile,powspec,4);
159  }
160  if (outfile_alms!="")
161  write_Alm_to_fits (outfile_alms,almT,almG,almC,nlmax,nmmax,planckType<T>());
162  }
163  }
164 
165 } // unamed namespace
166 
167 int anafast_cxx_module (int argc, const char **argv)
168  {
169  module_startup ("anafast_cxx", argc, argv);
170  paramfile params (getParamsFromCmdline(argc,argv));
171 
172  bool dp = params.find<bool> ("double_precision",false);
173  dp ? anafast_cxx<double>(params) : anafast_cxx<float>(params);
174  return 0;
175  }
void write_Alm_to_fits(fitshandle &out, const Alm< xcomplex< T > > &alms, int lmax, int mmax, PDT datatype)
Definition: alm_fitsio.cc:151
I Nside() const
Definition: healpix_base.h:427
double average() const
Definition: healpix_map.h:244
Definition: alm.h:88
void map2alm_pol_iter(const Healpix_Map< T > &mapT, const Healpix_Map< T > &mapQ, const Healpix_Map< T > &mapU, Alm< xcomplex< T > > &almT, Alm< xcomplex< T > > &almG, Alm< xcomplex< T > > &almC, int num_iter, const arr< double > &weight)
void apply_fullweights(Healpix_Map< T > &map, const std::vector< double > &wgt)
void read_Healpix_map_from_fits(fitshandle &inp, Healpix_Map< T > &map, int colnum)
tsize replaceUndefWith0()
Definition: healpix_map.h:306
Healpix_Ordering_Scheme Scheme() const
Definition: healpix_base.h:431
void extract_powspec(const Alm< xcomplex< T > > &alm, PowSpec &powspec)
void map2alm_iter(const Healpix_Map< T > &map, Alm< xcomplex< T > > &alm, int num_iter, const arr< double > &weight)
void swap_scheme()
Definition: healpix_map.h:179
void write_powspec_to_fits(fitshandle &out, const PowSpec &powspec, int nspecs)
void Add(T val)
Definition: healpix_map.h:255

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