00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <string>
00033 #include "alm_fitsio.h"
00034 #include "alm.h"
00035 #include "fitshandle.h"
00036 #include "xcomplex.h"
00037 #include "safe_cast.h"
00038 #include "share_utils.h"
00039
00040 using namespace std;
00041
00042 void get_almsize(fitshandle &inp, int &lmax, int &mmax)
00043 {
00044 if (inp.key_present("MAX-LPOL") && inp.key_present("MAX-MPOL"))
00045 {
00046 inp.get_key ("MAX-LPOL",lmax);
00047 inp.get_key ("MAX-MPOL",mmax);
00048 return;
00049 }
00050
00051 int n_alms = safe_cast<int>(inp.nelems(1));
00052 arr<int> index;
00053 lmax=mmax=-1;
00054 chunkMaker cm(n_alms,inp.efficientChunkSize(1));
00055 uint64 offset,ppix;
00056 while(cm.getNext(offset,ppix))
00057 {
00058 index.alloc(ppix);
00059 inp.read_column(1,index,offset);
00060
00061 for (tsize i=0; i<ppix; ++i)
00062 {
00063 int l = isqrt(index[i]-1);
00064 int m = index[i] - l*l - l - 1;
00065 if (l>lmax) lmax=l;
00066 if (m>mmax) mmax=m;
00067 }
00068 }
00069 }
00070
00071 void get_almsize(const string &filename, int &lmax, int &mmax, int hdunum)
00072 {
00073 fitshandle inp;
00074 inp.open (filename);
00075 inp.goto_hdu(hdunum);
00076 get_almsize (inp, lmax, mmax);
00077 }
00078
00079 void get_almsize_pol(const string &filename, int &lmax, int &mmax)
00080 {
00081 int tlmax, tmmax;
00082 fitshandle inp;
00083 inp.open (filename);
00084 lmax=mmax=0;
00085 for (int hdu=2; hdu<=4; ++hdu)
00086 {
00087 inp.goto_hdu(hdu);
00088 get_almsize (inp,tlmax,tmmax);
00089 if (tlmax>lmax) lmax=tlmax;
00090 if (tmmax>mmax) mmax=tmmax;
00091 }
00092 }
00093
00094 template<typename T> void read_Alm_from_fits
00095 (fitshandle &inp, Alm<xcomplex<T> >&alms, int lmax, int mmax)
00096 {
00097 int n_alms = safe_cast<int>(inp.nelems(1));
00098 arr<int> index;
00099 arr<T> re, im;
00100
00101 alms.Set(lmax, mmax);
00102 alms.SetToZero();
00103 int max_index = lmax*lmax + lmax + mmax + 1;
00104 chunkMaker cm(n_alms,inp.efficientChunkSize(1));
00105 uint64 offset,ppix;
00106 while(cm.getNext(offset,ppix))
00107 {
00108 index.alloc(ppix);
00109 re.alloc(ppix); im.alloc(ppix);
00110 inp.read_column(1,index,offset);
00111 inp.read_column(2,re,offset);
00112 inp.read_column(3,im,offset);
00113
00114 for (tsize i=0; i<ppix; ++i)
00115 {
00116 if (index[i]>max_index) continue;
00117
00118 int l = isqrt(index[i]-1);
00119 int m = index[i] - l*l - l - 1;
00120 planck_assert(m>=0,"negative m encountered");
00121 planck_assert(l>=m, "wrong l,m combination");
00122 if ((l<=lmax) && (m<=mmax))
00123 alms(l,m) = xcomplex<T> (re[i], im[i]);
00124 }
00125 }
00126 }
00127
00128 template void read_Alm_from_fits (fitshandle &inp,
00129 Alm<xcomplex<double> > &alms, int lmax, int mmax);
00130 template void read_Alm_from_fits (fitshandle &inp,
00131 Alm<xcomplex<float> > &alms, int lmax, int mmax);
00132
00133
00134 template<typename T> void read_Alm_from_fits
00135 (const string &filename, Alm<xcomplex<T> >&alms, int lmax, int mmax,
00136 int hdunum)
00137 {
00138 fitshandle inp;
00139 inp.open (filename);
00140 inp.goto_hdu(hdunum);
00141 read_Alm_from_fits(inp,alms,lmax,mmax);
00142 }
00143
00144 template void read_Alm_from_fits (const string &filename,
00145 Alm<xcomplex<double> > &alms, int lmax, int mmax, int hdunum);
00146 template void read_Alm_from_fits (const string &filename,
00147 Alm<xcomplex<float> > &alms, int lmax, int mmax, int hdunum);
00148
00149
00150 template<typename T> void write_Alm_to_fits
00151 (fitshandle &out, const Alm<xcomplex<T> > &alms, int lmax, int mmax,
00152 PDT datatype)
00153 {
00154 vector<fitscolumn> cols;
00155 cols.push_back (fitscolumn("index","l*l+l+m+1",1,PLANCK_INT32));
00156 cols.push_back (fitscolumn("real","unknown",1,datatype));
00157 cols.push_back (fitscolumn("imag","unknown",1,datatype));
00158 out.insert_bintab(cols);
00159 arr<int> index;
00160 arr<double> re, im;
00161
00162 int lm=alms.Lmax(), mm=alms.Mmax();
00163 int n_alms = ((mmax+1)*(mmax+2))/2 + (mmax+1)*(lmax-mmax);
00164
00165 int l=0, m=0;
00166 chunkMaker cm(n_alms,out.efficientChunkSize(1));
00167 uint64 offset,ppix;
00168 while(cm.getNext(offset,ppix))
00169 {
00170 index.alloc(ppix);
00171 re.alloc(ppix); im.alloc(ppix);
00172 for (tsize i=0; i<ppix; ++i)
00173 {
00174 index[i] = l*l + l + m + 1;
00175 if ((l<=lm) && (m<=mm))
00176 { re[i] = alms(l,m).real(); im[i] = alms(l,m).imag(); }
00177 else
00178 { re[i] = 0; im[i] = 0; }
00179 ++m;
00180 if ((m>l) || (m>mmax)) { ++l; m=0; }
00181 }
00182 out.write_column(1,index,offset);
00183 out.write_column(2,re,offset);
00184 out.write_column(3,im,offset);
00185 }
00186 out.set_key("MAX-LPOL",lmax,"highest l in the table");
00187 out.set_key("MAX-MPOL",mmax,"highest m in the table");
00188 }
00189
00190 template void write_Alm_to_fits
00191 (fitshandle &out, const Alm<xcomplex<double> > &alms, int lmax,
00192 int mmax, PDT datatype);
00193 template void write_Alm_to_fits
00194 (fitshandle &out, const Alm<xcomplex<float> > &alms, int lmax,
00195 int mmax, PDT datatype);
00196
00197
00198 template<typename T> void write_compressed_Alm_to_fits
00199 (fitshandle &out, const Alm<xcomplex<T> > &alms, int lmax, int mmax,
00200 PDT datatype)
00201 {
00202 vector<fitscolumn> cols;
00203 cols.push_back (fitscolumn("index","l*l+l+m+1",1,PLANCK_INT32));
00204 cols.push_back (fitscolumn("real","unknown",1,datatype));
00205 cols.push_back (fitscolumn("imag","unknown",1,datatype));
00206 out.insert_bintab(cols);
00207 arr<int> index;
00208 arr<double> re, im;
00209
00210 int n_alms = 0;
00211 for (int m=0; m<=mmax; ++m)
00212 for (int l=m; l<=lmax; ++l)
00213 if (norm(alms(l,m))>0) ++n_alms;
00214
00215 int l=0, m=0;
00216 int real_lmax=0, real_mmax=0;
00217 chunkMaker cm(n_alms,out.efficientChunkSize(1));
00218 uint64 offset,ppix;
00219 while(cm.getNext(offset,ppix))
00220 {
00221 index.alloc(ppix);
00222 re.alloc(ppix); im.alloc(ppix);
00223 for (tsize i=0; i<ppix; ++i)
00224 {
00225 while (norm(alms(l,m))==0)
00226 {
00227 ++m;
00228 if ((m>l) || (m>mmax)) { ++l; m=0; }
00229 }
00230 index[i] = l*l + l + m + 1;
00231 re[i] = alms(l,m).real();
00232 im[i] = alms(l,m).imag();
00233 if (l>real_lmax) real_lmax=l;
00234 if (m>real_mmax) real_mmax=m;
00235 ++m;
00236 if ((m>l) || (m>mmax)) { ++l; m=0; }
00237 }
00238 out.write_column(1,index,offset);
00239 out.write_column(2,re,offset);
00240 out.write_column(3,im,offset);
00241 }
00242 out.set_key("MAX-LPOL",real_lmax,"highest l in the table");
00243 out.set_key("MAX-MPOL",real_mmax,"highest m in the table");
00244 }
00245
00246 template void write_compressed_Alm_to_fits
00247 (fitshandle &out, const Alm<xcomplex<double> > &alms, int lmax,
00248 int mmax, PDT datatype);
00249 template void write_compressed_Alm_to_fits
00250 (fitshandle &out, const Alm<xcomplex<float> > &alms, int lmax,
00251 int mmax, PDT datatype);