Healpix C++  3.83
alm_fitsio.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-2015 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include <string>
33 #include "alm_fitsio.h"
34 #include "alm.h"
35 #include "fitshandle.h"
36 #include "xcomplex.h"
37 #include "safe_cast.h"
38 #include "share_utils.h"
39 
40 using namespace std;
41 
42 void get_almsize(fitshandle &inp, int &lmax, int &mmax)
43  {
44  if (inp.key_present("MAX-LPOL") && inp.key_present("MAX-MPOL"))
45  {
46  inp.get_key ("MAX-LPOL",lmax);
47  inp.get_key ("MAX-MPOL",mmax);
48  return;
49  }
50 
51  int n_alms = safe_cast<int>(inp.nelems(1));
52  arr<int> index;
53  lmax=mmax=-1;
54  chunkMaker cm(n_alms,inp.efficientChunkSize(1));
55  uint64 offset,ppix;
56  while(cm.getNext(offset,ppix))
57  {
58  index.alloc(ppix);
59  inp.read_column(1,index,offset);
60 
61  for (tsize i=0; i<ppix; ++i)
62  {
63  int l = isqrt(index[i]-1);
64  int m = index[i] - l*l - l - 1;
65  if (l>lmax) lmax=l;
66  if (m>mmax) mmax=m;
67  }
68  }
69  }
70 
71 void get_almsize(const string &filename, int &lmax, int &mmax, int hdunum)
72  {
73  fitshandle inp;
74  inp.open (filename);
75  inp.goto_hdu(hdunum);
76  get_almsize (inp, lmax, mmax);
77  }
78 
79 void get_almsize_pol(const string &filename, int &lmax, int &mmax)
80  {
81  int tlmax, tmmax;
82  fitshandle inp;
83  inp.open (filename);
84  lmax=mmax=0;
85  for (int hdu=2; hdu<=4; ++hdu)
86  {
87  inp.goto_hdu(hdu);
88  get_almsize (inp,tlmax,tmmax);
89  if (tlmax>lmax) lmax=tlmax;
90  if (tmmax>mmax) mmax=tmmax;
91  }
92  }
93 
94 template<typename T> void read_Alm_from_fits
95  (fitshandle &inp, Alm<xcomplex<T> >&alms, int lmax, int mmax)
96  {
97  int n_alms = safe_cast<int>(inp.nelems(1));
98  arr<int> index;
99  arr<T> re, im;
100 
101  alms.Set(lmax, mmax);
102  alms.SetToZero();
103  int max_index = lmax*lmax + lmax + mmax + 1;
104  chunkMaker cm(n_alms,inp.efficientChunkSize(1));
105  uint64 offset,ppix;
106  while(cm.getNext(offset,ppix))
107  {
108  index.alloc(ppix);
109  re.alloc(ppix); im.alloc(ppix);
110  inp.read_column(1,index,offset);
111  inp.read_column(2,re,offset);
112  inp.read_column(3,im,offset);
113 
114  for (tsize i=0; i<ppix; ++i)
115  {
116  if (index[i]>max_index) continue;
117 
118  int l = isqrt(index[i]-1);
119  int m = index[i] - l*l - l - 1;
120  planck_assert(m>=0,"negative m encountered");
121  planck_assert(l>=m, "wrong l,m combination");
122  if ((l<=lmax) && (m<=mmax))
123  alms(l,m) = xcomplex<T> (re[i], im[i]);
124  }
125  }
126  }
127 
128 template void read_Alm_from_fits (fitshandle &inp,
129  Alm<xcomplex<double> > &alms, int lmax, int mmax);
130 template void read_Alm_from_fits (fitshandle &inp,
131  Alm<xcomplex<float> > &alms, int lmax, int mmax);
132 
133 
134 template<typename T> void read_Alm_from_fits
135  (const string &filename, Alm<xcomplex<T> >&alms, int lmax, int mmax,
136  int hdunum)
137  {
138  fitshandle inp;
139  inp.open (filename);
140  inp.goto_hdu(hdunum);
141  read_Alm_from_fits(inp,alms,lmax,mmax);
142  }
143 
144 template void read_Alm_from_fits (const string &filename,
145  Alm<xcomplex<double> > &alms, int lmax, int mmax, int hdunum);
146 template void read_Alm_from_fits (const string &filename,
147  Alm<xcomplex<float> > &alms, int lmax, int mmax, int hdunum);
148 
149 
150 template<typename T> void write_Alm_to_fits
151  (fitshandle &out, const Alm<xcomplex<T> > &alms, int lmax, int mmax,
152  PDT datatype)
153  {
154  vector<fitscolumn> cols;
155  cols.push_back (fitscolumn("index","l*l+l+m+1",1,PLANCK_INT32));
156  cols.push_back (fitscolumn("real","unknown",1,datatype));
157  cols.push_back (fitscolumn("imag","unknown",1,datatype));
158  out.insert_bintab(cols);
159  arr<int> index;
160  arr<double> re, im;
161 
162  int lm=alms.Lmax(), mm=alms.Mmax();
163  int n_alms = ((mmax+1)*(mmax+2))/2 + (mmax+1)*(lmax-mmax);
164 
165  int l=0, m=0;
166  chunkMaker cm(n_alms,out.efficientChunkSize(1));
167  uint64 offset,ppix;
168  while(cm.getNext(offset,ppix))
169  {
170  index.alloc(ppix);
171  re.alloc(ppix); im.alloc(ppix);
172  for (tsize i=0; i<ppix; ++i)
173  {
174  index[i] = l*l + l + m + 1;
175  if ((l<=lm) && (m<=mm))
176  { re[i] = alms(l,m).real(); im[i] = alms(l,m).imag(); }
177  else
178  { re[i] = 0; im[i] = 0; }
179  ++m;
180  if ((m>l) || (m>mmax)) { ++l; m=0; }
181  }
182  out.write_column(1,index,offset);
183  out.write_column(2,re,offset);
184  out.write_column(3,im,offset);
185  }
186  out.set_key("MAX-LPOL",lmax,"highest l in the table");
187  out.set_key("MAX-MPOL",mmax,"highest m in the table");
188  }
189 
190 template void write_Alm_to_fits
191  (fitshandle &out, const Alm<xcomplex<double> > &alms, int lmax,
192  int mmax, PDT datatype);
193 template void write_Alm_to_fits
194  (fitshandle &out, const Alm<xcomplex<float> > &alms, int lmax,
195  int mmax, PDT datatype);
196 
197 
198 template<typename T> void write_compressed_Alm_to_fits
199  (fitshandle &out, const Alm<xcomplex<T> > &alms, int lmax, int mmax,
200  PDT datatype)
201  {
202  vector<fitscolumn> cols;
203  cols.push_back (fitscolumn("index","l*l+l+m+1",1,PLANCK_INT32));
204  cols.push_back (fitscolumn("real","unknown",1,datatype));
205  cols.push_back (fitscolumn("imag","unknown",1,datatype));
206  out.insert_bintab(cols);
207  arr<int> index;
208  arr<double> re, im;
209 
210  int n_alms = 0;
211  for (int m=0; m<=mmax; ++m)
212  for (int l=m; l<=lmax; ++l)
213  if (norm(alms(l,m))>0) ++n_alms;
214 
215  int l=0, m=0;
216  int real_lmax=0, real_mmax=0;
217  chunkMaker cm(n_alms,out.efficientChunkSize(1));
218  uint64 offset,ppix;
219  while(cm.getNext(offset,ppix))
220  {
221  index.alloc(ppix);
222  re.alloc(ppix); im.alloc(ppix);
223  for (tsize i=0; i<ppix; ++i)
224  {
225  while (norm(alms(l,m))==0)
226  {
227  ++m;
228  if ((m>l) || (m>mmax)) { ++l; m=0; }
229  }
230  index[i] = l*l + l + m + 1;
231  re[i] = alms(l,m).real();
232  im[i] = alms(l,m).imag();
233  if (l>real_lmax) real_lmax=l;
234  if (m>real_mmax) real_mmax=m;
235  ++m;
236  if ((m>l) || (m>mmax)) { ++l; m=0; }
237  }
238  out.write_column(1,index,offset);
239  out.write_column(2,re,offset);
240  out.write_column(3,im,offset);
241  }
242  out.set_key("MAX-LPOL",real_lmax,"highest l in the table");
243  out.set_key("MAX-MPOL",real_mmax,"highest m in the table");
244  }
245 
246 template void write_compressed_Alm_to_fits
247  (fitshandle &out, const Alm<xcomplex<double> > &alms, int lmax,
248  int mmax, PDT datatype);
249 template void write_compressed_Alm_to_fits
250  (fitshandle &out, const Alm<xcomplex<float> > &alms, int lmax,
251  int mmax, PDT datatype);
void write_Alm_to_fits(fitshandle &out, const Alm< xcomplex< T > > &alms, int lmax, int mmax, PDT datatype)
Definition: alm_fitsio.cc:151
void read_Alm_from_fits(fitshandle &inp, Alm< xcomplex< T > > &alms, int lmax, int mmax)
Definition: alm_fitsio.cc:95
Definition: alm.h:88
void get_almsize_pol(const std::string &filename, int &lmax, int &mmax)
void get_almsize(fitshandle &inp, int &lmax, int &mmax)
Definition: alm_fitsio.cc:42
void write_compressed_Alm_to_fits(fitshandle &out, const Alm< xcomplex< T > > &alms, int lmax, int mmax, PDT datatype)
Definition: alm_fitsio.cc:199

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