Healpix C++  3.83
compute_weights_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) 2016-2019 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include <chrono>
33 #include "paramfile.h"
34 #include "fitshandle.h"
35 #include "announce.h"
36 #include "weight_utils.h"
37 
38 using namespace std;
39 
40 class SimpleTimer
41  {
42  private:
43  using clock = std::chrono::steady_clock;
44  clock::time_point starttime;
45 
46  public:
47  SimpleTimer()
48  : starttime(clock::now()) {}
49  double operator()() const
50  {
51  return std::chrono::duration<double>(clock::now() - starttime).count();
52  }
53  };
54 
55 void write_fullfile(const string &name, int nside, int nlmax, double epsilon,
56  const vector<double> &wgt)
57  {
58  fitshandle out;
59  out.create (name);
60  vector<fitscolumn> cols;
61  int repcount = 1;
62  cols.push_back (fitscolumn ("COMPRESSED PIXEL WEIGHTS","1",repcount,
63  PLANCK_FLOAT64));
64  out.insert_bintab(cols);
65  out.set_key ("CREATOR",string("compute_weights"),
66  "Software creating the FITS file");
67  out.set_key ("NSIDE",nside,"Resolution parameter for HEALPix");
68  out.set_key ("MAX_LPOL",nlmax,"Maximum l multipole");
69  double val=*max_element(wgt.begin(),wgt.end());
70  out.set_key("MAXVAL1",val,"maximum value of pixel weights");
71  val=*min_element(wgt.begin(),wgt.end());
72  out.set_key("MINVAL1",val,"minimum value of pixel weights");
73  out.set_key("EPSILON",epsilon,"epsilon reached after minimization");
74  out.write_column(1,wgt);
75  }
76 
77 int compute_weights_module (int argc, const char **argv)
78  {
79  module_startup ("compute_weights", argc, argv);
80  paramfile params (getParamsFromCmdline(argc,argv));
81 
82  planck_assert (params.param_present("outfile_ring")
83  || params.param_present("outfile_full"), "no job requested");
84  int nside = params.find<int>("nside");
85  int nlmax = params.find<int>("nlmax");
86  if (nlmax&1)
87  {
88  cout << "Warning: specified nlmax is odd. Reducing by 1" << endl;
89  --nlmax;
90  }
91  double epsilon = params.find<double>("epsilon",1e-7);
92  int itmax = params.find<int>("max_iter",10000);
93 
94  if (params.param_present("outfile_ring"))
95  {
96  double epsilon_out;
97  vector<double> wgt=get_ringweights(nside,nlmax,epsilon,itmax,epsilon_out);
98  fitshandle out;
99  out.create (params.find<string>("outfile_ring"));
100  vector<fitscolumn> cols;
101  int repcount = 1;
102  cols.push_back (fitscolumn ("TEMPERATURE WEIGHTS","1",repcount,
103  PLANCK_FLOAT64));
104  cols.push_back (fitscolumn ("Q-POLARISATION WEIGHTS","1",repcount,
105  PLANCK_FLOAT64));
106  cols.push_back (fitscolumn ("U-POLARISATION WEIGHTS","1",repcount,
107  PLANCK_FLOAT64));
108  out.insert_bintab(cols);
109  out.set_key ("CREATOR",string("compute_weights"),
110  "Software creating the FITS file");
111  out.set_key ("NSIDE",nside,"Resolution parameter for HEALPIX");
112  out.set_key ("MAX_LPOL",nlmax,"Maximum multipole l used in map synthesis");
113  double val=*max_element(wgt.begin(),wgt.end());
114  out.set_key("MAXVAL1",val,"maximum value of T weights");
115  out.set_key("MAXVAL2",val,"maximum value of Q weights");
116  out.set_key("MAXVAL3",val,"maximum value of U weights");
117  val=*min_element(wgt.begin(),wgt.end());
118  out.set_key("MINVAL1",val,"minimum value of T weights");
119  out.set_key("MINVAL2",val,"minimum value of Q weights");
120  out.set_key("MINVAL3",val,"minimum value of U weights");
121  out.set_key("EPSILON",epsilon_out,"epsilon reached after minimization");
122  out.write_column(1,wgt);
123  out.write_column(2,wgt);
124  out.write_column(3,wgt);
125  }
126  if (params.param_present("outfile_full"))
127  {
128  FullWeightComputer comp(nside,nlmax);
129  auto name = params.find<string>("outfile_full");
130  double lasteps=2, dumpeps=2;
131  SimpleTimer t;
132  auto t_old = t();
133  vector<double> best;
134  while((comp.current_iter()<itmax) && (comp.current_epsilon()>epsilon))
135  {
136  comp.iterate(1);
137  double eps=comp.current_epsilon();
138  if (eps<lasteps)
139  {
140  best = comp.current_alm();
141  lasteps=eps;
142  }
143  if ((t()-t_old>120) && (dumpeps>lasteps))
144  {
145  cout << "\nwriting output file. eps=" << lasteps << endl;
146  write_fullfile(name, nside, nlmax, lasteps, comp.alm2wgt(best));
147  t_old=t();
148  dumpeps=lasteps;
149  }
150  }
151  if (dumpeps>lasteps)
152  {
153  cout << "\nwriting output file. eps=" << lasteps << endl;
154  write_fullfile(name, nside, nlmax, lasteps, comp.alm2wgt(best));
155  }
156  }
157  return 0;
158  }
vector< double > get_ringweights(int nside, int lmax, double epsilon, int itmax, double &epsilon_out)

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