Healpix C++  3.82
healpix_map_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-2017 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include "healpix_map_fitsio.h"
33 #include "healpix_map.h"
34 #include "fitshandle.h"
35 #include "share_utils.h"
36 #include "string_utils.h"
37 
38 using namespace std;
39 
40 namespace {
41 
42 unsigned int healpix_repcount (tsize npix)
43  {
44  if (npix<1024) return 1;
45  if ((npix%1024)==0) return 1024;
46  return isqrt (npix/12);
47  }
48 
49 bool is_iau (const fitshandle &inp)
50  {
51  if (!inp.key_present("POLCCONV")) return false; // no keyword implies COSMO
52  string polcconv=inp.get_key<string>("POLCCONV");
53  planck_assert((polcconv=="COSMO")||(polcconv=="IAU"), "bad POLCCONV keyword");
54  return (polcconv=="IAU");
55  }
56 
57 } // unnamed namespace
58 
59 template<typename T> void read_Healpix_map_from_fits
60  (fitshandle &inp, Healpix_Map<T> &map, int colnum)
61  {
62  arr<T> myarr;
63  inp.read_entire_column (colnum, myarr);
64  int64 nside = inp.get_key<int>("NSIDE");
65  planck_assert (int64(myarr.size())==12*nside*nside,
66  string("mismatch between number of map pixels ("
67  +dataToString(myarr.size())+") and Nside ("+dataToString(nside)+")"));
68  map.Set (myarr, string2HealpixScheme(inp.get_key<string>("ORDERING")));
69  }
70 
71 template void read_Healpix_map_from_fits (fitshandle &inp,
72  Healpix_Map<float> &map, int colnum);
73 template void read_Healpix_map_from_fits (fitshandle &inp,
74  Healpix_Map<double> &map, int colnum);
75 template void read_Healpix_map_from_fits (fitshandle &inp,
76  Healpix_Map<int> &map, int colnum);
77 
78 template<typename T> Healpix_Map<T> read_Healpix_map_from_fits
79  (fitshandle &inp, int colnum)
80  {
81  Healpix_Map<T> res;
82  read_Healpix_map_from_fits (inp, res, colnum);
83  return res;
84  }
85 
87  (fitshandle &inp, int colnum);
89  (fitshandle &inp, int colnum);
91  (fitshandle &inp, int colnum);
92 
93 template<typename T> void read_Healpix_map_from_fits
94  (const string &filename, Healpix_Map<T> &map, int colnum, int hdunum)
95  {
96  fitshandle inp;
97  inp.open (filename);
98  inp.goto_hdu (hdunum);
99  read_Healpix_map_from_fits (inp,map,colnum);
100  }
101 
102 template void read_Healpix_map_from_fits (const string &filename,
103  Healpix_Map<float> &map, int colnum, int hdunum);
104 template void read_Healpix_map_from_fits (const string &filename,
105  Healpix_Map<double> &map, int colnum, int hdunum);
106 template void read_Healpix_map_from_fits (const string &filename,
107  Healpix_Map<int> &map, int colnum, int hdunum);
108 
109 template<typename T> Healpix_Map<T> read_Healpix_map_from_fits
110  (const string &filename, int colnum, int hdunum)
111  {
112  Healpix_Map<T> res;
113  read_Healpix_map_from_fits (filename, res, colnum, hdunum);
114  return res;
115  }
116 
118  (const string &filename, int colnum, int hdunum);
120  (const string &filename, int colnum, int hdunum);
122  (const string &filename, int colnum, int hdunum);
123 
124 template<typename T> void read_Healpix_map_from_fits
125  (fitshandle &inp, Healpix_Map<T> &mapT, Healpix_Map<T> &mapQ,
126  Healpix_Map<T> &mapU)
127  {
128  int64 nside = inp.get_key<int>("NSIDE");
130  = string2HealpixScheme(inp.get_key<string>("ORDERING"));
131  mapT.SetNside(nside,scheme);
132  mapQ.SetNside(nside,scheme);
133  mapU.SetNside(nside,scheme);
134  planck_assert (multiequal(int64(mapT.Npix()),inp.nelems(1),inp.nelems(2),
135  inp.nelems(3)), "mismatch between number of map pixels and Nside");
136  chunkMaker cm(mapT.Npix(),inp.efficientChunkSize(1));
137  uint64 offset,ppix;
138  while(cm.getNext(offset,ppix))
139  {
140  inp.read_column_raw(1,&mapT[offset],ppix,offset);
141  inp.read_column_raw(2,&mapQ[offset],ppix,offset);
142  inp.read_column_raw(3,&mapU[offset],ppix,offset);
143  }
144  if (is_iau(inp))
145  for (int i=0; i<mapU.Npix(); ++i)
146  mapU[i]=-mapU[i];
147  }
148 
149 template void read_Healpix_map_from_fits (fitshandle &inp,
151  Healpix_Map<float> &mapU);
152 template void read_Healpix_map_from_fits (fitshandle &inp,
154  Healpix_Map<double> &mapU);
155 
156 template<typename T> void read_Healpix_map_from_fits
157  (const string &filename, Healpix_Map<T> &mapT, Healpix_Map<T> &mapQ,
158  Healpix_Map<T> &mapU, int hdunum)
159  {
160  fitshandle inp;
161  inp.open(filename);
162  inp.goto_hdu(hdunum);
163  read_Healpix_map_from_fits (inp,mapT,mapQ,mapU);
164  }
165 
166 template void read_Healpix_map_from_fits (const string &filename,
168  Healpix_Map<float> &mapU, int hdunum);
169 template void read_Healpix_map_from_fits (const string &filename,
171  Healpix_Map<double> &mapU, int hdunum);
172 
174  (fitshandle &out, const Healpix_Base &base, PDT datatype,
175  const arr<string> &colname)
176  {
177  vector<fitscolumn> cols;
178  int repcount = healpix_repcount (base.Npix());
179  for (tsize m=0; m<colname.size(); ++m)
180  cols.push_back (fitscolumn (colname[m],"unknown",repcount, datatype));
181  out.insert_bintab(cols);
182  out.set_key ("PIXTYPE",string("HEALPIX"),"HEALPIX pixelisation");
183  string ordering = (base.Scheme()==RING) ? "RING" : "NESTED";
184  out.set_key ("ORDERING",ordering,
185  "Pixel ordering scheme, either RING or NESTED");
186  out.set_key ("NSIDE",base.Nside(),"Resolution parameter for HEALPIX");
187  out.set_key ("FIRSTPIX",0,"First pixel # (0 based)");
188  out.set_key ("LASTPIX",base.Npix()-1,"Last pixel # (0 based)");
189  out.set_key ("INDXSCHM",string("IMPLICIT"),
190  "Indexing: IMPLICIT or EXPLICIT");
191  }
192 
193 template<typename T> void write_Healpix_map_to_fits
194  (fitshandle &out, const Healpix_Map<T> &map, PDT datatype)
195  {
196  arr<string> colname(1);
197  colname[0] = "TEMPERATURE";
198  prepare_Healpix_fitsmap (out, map, datatype, colname);
199  out.write_column(1,map.Map());
200  }
201 
202 template void write_Healpix_map_to_fits
203  (fitshandle &out, const Healpix_Map<float> &map, PDT datatype);
204 template void write_Healpix_map_to_fits
205  (fitshandle &out, const Healpix_Map<double> &map, PDT datatype);
206 template void write_Healpix_map_to_fits
207  (fitshandle &out, const Healpix_Map<int> &map, PDT datatype);
208 
209 
210 template<typename T> void write_Healpix_map_to_fits
211  (fitshandle &out, const Healpix_Map<T> &mapT,
212  const Healpix_Map<T> &mapQ, const Healpix_Map<T> &mapU, PDT datatype)
213  {
214  arr<string> colname(3);
215  colname[0] = "TEMPERATURE";
216  colname[1] = "Q_POLARISATION";
217  colname[2] = "U_POLARISATION";
218  prepare_Healpix_fitsmap (out, mapT, datatype, colname);
219  out.set_key ("POLCCONV", string("COSMO"));
220  chunkMaker cm(mapT.Npix(),out.efficientChunkSize(1));
221  uint64 offset,ppix;
222  while(cm.getNext(offset,ppix))
223  {
224  out.write_column_raw(1,&mapT[offset],ppix,offset);
225  out.write_column_raw(2,&mapQ[offset],ppix,offset);
226  out.write_column_raw(3,&mapU[offset],ppix,offset);
227  }
228  }
229 
230 template void write_Healpix_map_to_fits
231  (fitshandle &out, const Healpix_Map<float> &mapT,
232  const Healpix_Map<float> &mapQ, const Healpix_Map<float> &mapU,
233  PDT datatype);
234 template void write_Healpix_map_to_fits
235  (fitshandle &out, const Healpix_Map<double> &mapT,
236  const Healpix_Map<double> &mapQ, const Healpix_Map<double> &mapU,
237  PDT datatype);
void SetNside(int nside, Healpix_Ordering_Scheme scheme)
Definition: healpix_map.h:83
I Nside() const
Definition: healpix_base.h:427
void Set(arr< T > &data, Healpix_Ordering_Scheme scheme)
Definition: healpix_map.h:68
Healpix_Ordering_Scheme
void read_Healpix_map_from_fits(fitshandle &inp, Healpix_Map< T > &map, int colnum)
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)
void prepare_Healpix_fitsmap(fitshandle &out, const Healpix_Base &base, PDT datatype, const arr< std::string > &colname)
const arr< T > & Map() const
Definition: healpix_map.h:228
I Npix() const
Definition: healpix_base.h:429

Generated on Thu Jul 28 2022 17:32:07 for Healpix C++