healpix_map_fitsio.cc

00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  Healpix_cxx is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.sourceforge.net
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*
00028  *  Copyright (C) 2003-2011 Max-Planck-Society
00029  *  Author: Martin Reinecke
00030  */
00031 
00032 #include "healpix_map_fitsio.h"
00033 #include "healpix_map.h"
00034 #include "fitshandle.h"
00035 #include "share_utils.h"
00036 #include "string_utils.h"
00037 
00038 using namespace std;
00039 
00040 namespace {
00041 
00042 unsigned int healpix_repcount (tsize npix)
00043   {
00044   if (npix<1024) return 1;
00045   if ((npix%1024)==0) return 1024;
00046   return isqrt (npix/12);
00047   }
00048 
00049 } // unnamed namespace
00050 
00051 template<typename T> void read_Healpix_map_from_fits
00052   (fitshandle &inp, Healpix_Map<T> &map, int colnum)
00053   {
00054   arr<T> myarr;
00055   inp.read_entire_column (colnum, myarr);
00056   int64 nside = inp.get_key<int>("NSIDE");
00057   planck_assert (int64(myarr.size())==12*nside*nside,
00058     string("mismatch between number of map pixels ("
00059     +dataToString(myarr.size())+") and Nside ("+dataToString(nside)+")"));
00060   map.Set (myarr, string2HealpixScheme(inp.get_key<string>("ORDERING")));
00061   }
00062 
00063 template void read_Healpix_map_from_fits (fitshandle &inp,
00064   Healpix_Map<float> &map, int colnum);
00065 template void read_Healpix_map_from_fits (fitshandle &inp,
00066   Healpix_Map<double> &map, int colnum);
00067 template void read_Healpix_map_from_fits (fitshandle &inp,
00068   Healpix_Map<int> &map, int colnum);
00069 
00070 
00071 template<typename T> void read_Healpix_map_from_fits
00072   (const string &filename, Healpix_Map<T> &map, int colnum, int hdunum)
00073   {
00074   fitshandle inp;
00075   inp.open (filename);
00076   inp.goto_hdu (hdunum);
00077   read_Healpix_map_from_fits (inp,map,colnum);
00078   }
00079 
00080 template void read_Healpix_map_from_fits (const string &filename,
00081   Healpix_Map<float> &map, int colnum, int hdunum);
00082 template void read_Healpix_map_from_fits (const string &filename,
00083   Healpix_Map<double> &map, int colnum, int hdunum);
00084 template void read_Healpix_map_from_fits (const string &filename,
00085   Healpix_Map<int> &map, int colnum, int hdunum);
00086 
00087 template<typename T> void read_Healpix_map_from_fits
00088   (fitshandle &inp, Healpix_Map<T> &mapT, Healpix_Map<T> &mapQ,
00089   Healpix_Map<T> &mapU)
00090   {
00091   int64 nside = inp.get_key<int>("NSIDE");
00092   Healpix_Ordering_Scheme scheme
00093     = string2HealpixScheme(inp.get_key<string>("ORDERING"));
00094   mapT.SetNside(nside,scheme);
00095   mapQ.SetNside(nside,scheme);
00096   mapU.SetNside(nside,scheme);
00097   planck_assert (multiequal(int64(mapT.Npix()),inp.nelems(1),inp.nelems(2),
00098     inp.nelems(3)), "mismatch between number of map pixels and Nside");
00099   chunkMaker cm(mapT.Npix(),inp.efficientChunkSize(1));
00100   uint64 offset,ppix;
00101   while(cm.getNext(offset,ppix))
00102     {
00103     inp.read_column_raw(1,&mapT[offset],ppix,offset);
00104     inp.read_column_raw(2,&mapQ[offset],ppix,offset);
00105     inp.read_column_raw(3,&mapU[offset],ppix,offset);
00106     }
00107   }
00108 
00109 template void read_Healpix_map_from_fits (fitshandle &inp,
00110   Healpix_Map<float> &mapT, Healpix_Map<float> &mapQ,
00111   Healpix_Map<float> &mapU);
00112 template void read_Healpix_map_from_fits (fitshandle &inp,
00113   Healpix_Map<double> &mapT, Healpix_Map<double> &mapQ,
00114   Healpix_Map<double> &mapU);
00115 
00116 template<typename T> void read_Healpix_map_from_fits
00117   (const string &filename, Healpix_Map<T> &mapT, Healpix_Map<T> &mapQ,
00118   Healpix_Map<T> &mapU, int hdunum)
00119   {
00120   fitshandle inp;
00121   inp.open(filename);
00122   inp.goto_hdu(hdunum);
00123   read_Healpix_map_from_fits (inp,mapT,mapQ,mapU);
00124   }
00125 
00126 template void read_Healpix_map_from_fits (const string &filename,
00127   Healpix_Map<float> &mapT, Healpix_Map<float> &mapQ,
00128   Healpix_Map<float> &mapU, int hdunum);
00129 template void read_Healpix_map_from_fits (const string &filename,
00130   Healpix_Map<double> &mapT, Healpix_Map<double> &mapQ,
00131   Healpix_Map<double> &mapU, int hdunum);
00132 
00133 void prepare_Healpix_fitsmap
00134   (fitshandle &out, const Healpix_Base &base, PDT datatype,
00135   const arr<string> &colname)
00136   {
00137   vector<fitscolumn> cols;
00138   int repcount = healpix_repcount (base.Npix());
00139   for (tsize m=0; m<colname.size(); ++m)
00140     cols.push_back (fitscolumn (colname[m],"unknown",repcount, datatype));
00141   out.insert_bintab(cols);
00142   out.set_key ("PIXTYPE",string("HEALPIX"),"HEALPIX pixelisation");
00143   string ordering = (base.Scheme()==RING) ? "RING" : "NESTED";
00144   out.set_key ("ORDERING",ordering,
00145                   "Pixel ordering scheme, either RING or NESTED");
00146   out.set_key ("NSIDE",base.Nside(),"Resolution parameter for HEALPIX");
00147   out.set_key ("FIRSTPIX",0,"First pixel # (0 based)");
00148   out.set_key ("LASTPIX",base.Npix()-1,"Last pixel # (0 based)");
00149   out.set_key ("INDXSCHM",string("IMPLICIT"),
00150                   "Indexing: IMPLICIT or EXPLICIT");
00151   }
00152 
00153 template<typename T> void write_Healpix_map_to_fits
00154   (fitshandle &out, const Healpix_Map<T> &map, PDT datatype)
00155   {
00156   arr<string> colname(1);
00157   colname[0] = "signal";
00158   prepare_Healpix_fitsmap (out, map, datatype, colname);
00159   out.write_column(1,map.Map());
00160   }
00161 
00162 template void write_Healpix_map_to_fits
00163   (fitshandle &out, const Healpix_Map<float> &map, PDT datatype);
00164 template void write_Healpix_map_to_fits
00165   (fitshandle &out, const Healpix_Map<double> &map, PDT datatype);
00166 template void write_Healpix_map_to_fits
00167   (fitshandle &out, const Healpix_Map<int> &map, PDT datatype);
00168 
00169 
00170 template<typename T> void write_Healpix_map_to_fits
00171   (fitshandle &out, const Healpix_Map<T> &mapT,
00172    const Healpix_Map<T> &mapQ, const Healpix_Map<T> &mapU, PDT datatype)
00173   {
00174   arr<string> colname(3);
00175   colname[0] = "signal";
00176   colname[1] = "Q-pol";
00177   colname[2] = "U-pol";
00178   prepare_Healpix_fitsmap (out, mapT, datatype, colname);
00179   chunkMaker cm(mapT.Npix(),out.efficientChunkSize(1));
00180   uint64 offset,ppix;
00181   while(cm.getNext(offset,ppix))
00182     {
00183     out.write_column_raw(1,&mapT[offset],ppix,offset);
00184     out.write_column_raw(2,&mapQ[offset],ppix,offset);
00185     out.write_column_raw(3,&mapU[offset],ppix,offset);
00186     }
00187   }
00188 
00189 template void write_Healpix_map_to_fits
00190   (fitshandle &out, const Healpix_Map<float> &mapT,
00191    const Healpix_Map<float> &mapQ, const Healpix_Map<float> &mapU,
00192    PDT datatype);
00193 template void write_Healpix_map_to_fits
00194   (fitshandle &out, const Healpix_Map<double> &mapT,
00195    const Healpix_Map<double> &mapQ, const Healpix_Map<double> &mapU,
00196    PDT datatype);

Generated on Thu Oct 8 14:48:52 2015 for Healpix C++