| 1 | #include "sopnamsp.h"
 | 
|---|
| 2 | #include "machdefs.h"    // Definitions specifiques SOPHYA
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include <math.h>
 | 
|---|
| 5 | #include <iostream>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "nbmath.h"
 | 
|---|
| 8 | #include "timing.h"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "array.h"
 | 
|---|
| 11 | #include "skymap.h"
 | 
|---|
| 12 | #include "samba.h"
 | 
|---|
| 13 | #include "sambainit.h"
 | 
|---|
| 14 | #include "fitsspherehealpix.h"
 | 
|---|
| 15 | #include "fitstarray.h"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /*!
 | 
|---|
| 19 |   \ingroup PrgMap
 | 
|---|
| 20 |   \file map2cl.cc
 | 
|---|
| 21 |   \brief \b map2cl: Computes power spectra (C(l)) on spherical maps.
 | 
|---|
| 22 | 
 | 
|---|
| 23 |   \verbatim
 | 
|---|
| 24 | 
 | 
|---|
| 25 |   csh> map2cl -h
 | 
|---|
| 26 |   SOPHYA Version  1.1 Revision 0 (V_Fev2001) -- Mar  9 2001 15:45:31 cxx
 | 
|---|
| 27 | 
 | 
|---|
| 28 |   map2cl : Spherical harmonics analysis - HEALPix map -> Power spectrum C_l
 | 
|---|
| 29 |   Usage: map2cl [-float/-r4] [-lmax lval] [-thetacut dtdeg]
 | 
|---|
| 30 |          [-fitsin] [-fitsout] InFileName OutFileName
 | 
|---|
| 31 |    -float (-r4): single precision C_l and map (default = double)
 | 
|---|
| 32 |    -lmax lval: Maximum value for l index (default=255)
 | 
|---|
| 33 |    -thetacut dtdeg : Symmetric delta-theta cut (in degree) along equator
 | 
|---|
| 34 |                     (default=0 -> no cut)
 | 
|---|
| 35 |    -iter_order lval : 1,2,3,4... order of an iterative analysis,
 | 
|---|
| 36 |               3rd order is usually optimal (default=0 -> standard analysis)
 | 
|---|
| 37 |    -keepalm : Save Alm's as a 2-D array (Matrix) 
 | 
|---|
| 38 |    -fitsout: Select the FITS format for the output vector/alm's (default PPF format)
 | 
|---|
| 39 |    -fitsin : Select the FITS format for the input map (default PPF format)
 | 
|---|
| 40 |    InFileName : Input file name (HEALPix map)
 | 
|---|
| 41 |    OutFileName : Output file name (the C_l vector / and Alm's)
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   \endverbatim
 | 
|---|
| 44 | */
 | 
|---|
| 45 | 
 | 
|---|
| 46 | /* Nouvelle-Fonction */
 | 
|---|
| 47 | void Usage(bool fgerr)
 | 
|---|
| 48 | {
 | 
|---|
| 49 |   cout << endl;
 | 
|---|
| 50 |   if (fgerr) {
 | 
|---|
| 51 |     cout << " map2cl : Argument Error ! map2cl -h for usage " << endl; 
 | 
|---|
| 52 |     exit(1);
 | 
|---|
| 53 |   }
 | 
|---|
| 54 |   else {
 | 
|---|
| 55 |     cout << " map2cl : Spherical harmonics analysis - HEALPix map -> Power spectrum C_l \n" 
 | 
|---|
| 56 |          << " Usage: map2cl [-float/-r4] [-lmax lval] [-thetacut dtdeg] [-iter_order lval]\n" 
 | 
|---|
| 57 |          << "        [-keepalm AlmFileName] [-fitsin] [-fitsout] InFileName OutFileName \n" 
 | 
|---|
| 58 |          << "   -float (-r4): single precision C_l and map (default = double) \n"
 | 
|---|
| 59 |          << "   -lmax lval: Maximum value for l index (default=255)\n" 
 | 
|---|
| 60 |          << "   -thetacut dtdeg : Symmetric delta-theta cut (in degree) along equator \n" 
 | 
|---|
| 61 |          << "                    (default=0 -> no cut)\n"
 | 
|---|
| 62 |          << "   -iter_order lval : 1,2,3,4... order of an iterative analysis\n" 
 | 
|---|
| 63 |          << "       3rd order is usually optimal (default=0 -> standard analysis)\n"
 | 
|---|
| 64 |          << "   -keepalm : Save Alm's as a 2-D complex array (Matrix<complex>) - only with PPF format  \n"
 | 
|---|
| 65 |          << "   -fitsout: Select the FITS format for the output files (Map/Alm) (default PPF format) \n"
 | 
|---|
| 66 |          << "   -fitsin : Select the FITS format for the input map (default PPF format) \n"
 | 
|---|
| 67 |          << "   InFileName : Input file name (HEALPix map) \n" 
 | 
|---|
| 68 |          << "   OutFileName : Output file name (the C_l vector) \n" << endl; 
 | 
|---|
| 69 |     exit(0);
 | 
|---|
| 70 |   }  
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /* Nouvelle-Fonction */
 | 
|---|
| 74 | template <class T> 
 | 
|---|
| 75 | class _Map2Cl {
 | 
|---|
| 76 | public :
 | 
|---|
| 77 | static void ComputeCl(string & infile, string & outfile, int lmax, double tcut, 
 | 
|---|
| 78 |                       int iterationOrder, bool fgfitsin, bool fgfitsout, bool keepalm)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   double deg2rad =  M_PI/180.;
 | 
|---|
| 81 |   double minute2rad =  M_PI/(180.*60.);
 | 
|---|
| 82 |  
 | 
|---|
| 83 |   SphereHEALPix<T> sph;
 | 
|---|
| 84 |   if (fgfitsin) {
 | 
|---|
| 85 |     cout << "--- Reading Input FITS file " << infile << endl; 
 | 
|---|
| 86 |     FitsInFile fii(infile);
 | 
|---|
| 87 |     fii >> sph;
 | 
|---|
| 88 |   }
 | 
|---|
| 89 |   else {
 | 
|---|
| 90 |     cout << "--- Reading Input PPF file " << infile << endl; 
 | 
|---|
| 91 |     PInPersist ppi(infile);
 | 
|---|
| 92 |     ppi >> sph;
 | 
|---|
| 93 |   } 
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   cout << " Input map : NbPixels= " <<  sph.NbPixels() << " NSide= " 
 | 
|---|
| 96 |        << sph.SizeIndex() << " Resolution= "
 | 
|---|
| 97 |        << sqrt(sph.PixSolAngle(0))/minute2rad << " Arcminutes " << endl;
 | 
|---|
| 98 |   
 | 
|---|
| 99 |   double ctcut = (tcut < 1.e-19) ? 0. : cos((90.-tcut)*deg2rad);
 | 
|---|
| 100 |   cout << "--- Calling  DecomposeToCl() (lmax= " << lmax 
 | 
|---|
| 101 |        << " cos_theta_cut= " << ctcut << " iter_order= " << iterationOrder << ") theta_cut=" << tcut << " deg"  << endl;
 | 
|---|
| 102 |   // Decomposition de la carte en C_l
 | 
|---|
| 103 |   SphericalTransformServer<T> sphtr;
 | 
|---|
| 104 |   TVector<T> clvec;
 | 
|---|
| 105 |   TMatrix< complex<T> > almmtx;
 | 
|---|
| 106 |   if (keepalm) {
 | 
|---|
| 107 |     Alm<T> alm(lmax);
 | 
|---|
| 108 |     sphtr.DecomposeToAlm(sph, alm, lmax, ctcut, iterationOrder);
 | 
|---|
| 109 |     almmtx.SetSize(alm.Lmax()+1, alm.Lmax()+1);
 | 
|---|
| 110 |     for(int il=0; il<alm.Lmax()+1; il++)
 | 
|---|
| 111 |       for(int im=0; im<il; im++)
 | 
|---|
| 112 |         almmtx(il, im) = alm(il, im);
 | 
|---|
| 113 |     clvec = alm.powerSpectrum();
 | 
|---|
| 114 |   }
 | 
|---|
| 115 |   else {
 | 
|---|
| 116 |     clvec = sphtr.DecomposeToCl(sph, lmax, ctcut, iterationOrder);
 | 
|---|
| 117 |   }
 | 
|---|
| 118 |   T min, max;
 | 
|---|
| 119 |   double mean, sigma;
 | 
|---|
| 120 |   clvec.MinMax(min, max);
 | 
|---|
| 121 |   MeanSigma(clvec, mean, sigma);
 | 
|---|
| 122 |   cout << "--- Statistics on the computed C_l vector: Size=" << clvec.Size() << endl; 
 | 
|---|
| 123 |   cout << " C_l.Min= " << min << " C_l.Max= " << max 
 | 
|---|
| 124 |        << " C_l.Mean= " << mean << " C_l.Sigma= " << sigma << endl;
 | 
|---|
| 125 |   
 | 
|---|
| 126 |   if (fgfitsout) {
 | 
|---|
| 127 |     FitsOutFile fio(outfile, FitsFile::clear);
 | 
|---|
| 128 |     cout << "--- Writing C_l vector to Output FITS file " << outfile << endl; 
 | 
|---|
| 129 |     fio << clvec;
 | 
|---|
| 130 |   }
 | 
|---|
| 131 |   else {
 | 
|---|
| 132 |     POutPersist ppo(outfile);
 | 
|---|
| 133 |     if (keepalm) {
 | 
|---|
| 134 |       cout << "--- Writing C_l vector to Output PPF file " << outfile << endl; 
 | 
|---|
| 135 |       ppo.PutObject(clvec, "Cl");
 | 
|---|
| 136 |       cout << "--- Writing Alm Matrix to Output PPF file " << outfile << endl; 
 | 
|---|
| 137 |       ppo.PutObject(almmtx, "Alm");      
 | 
|---|
| 138 |     }
 | 
|---|
| 139 |     else {
 | 
|---|
| 140 |       cout << "--- Writing C_l vector to Output PPF file " << outfile << endl; 
 | 
|---|
| 141 |       ppo << clvec;
 | 
|---|
| 142 |     }
 | 
|---|
| 143 |   }
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | };
 | 
|---|
| 147 | 
 | 
|---|
| 148 | /* Main-Program */
 | 
|---|
| 149 | int main(int narg, char *arg[])
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false);
 | 
|---|
| 152 |   
 | 
|---|
| 153 |   int lmax = 255;
 | 
|---|
| 154 |   int iterationOrder = 0;
 | 
|---|
| 155 |   double tcut = 0.;
 | 
|---|
| 156 |   string infile;
 | 
|---|
| 157 |   string outfile;
 | 
|---|
| 158 |   bool fgfitsin = false;
 | 
|---|
| 159 |   bool fgfitsout = false;
 | 
|---|
| 160 |   bool fgr4 = false;
 | 
|---|
| 161 |   bool fgkeepalm = false;
 | 
|---|
| 162 |   cout << " map2cl : Decoding command line options ... " << endl;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   int ko=1;
 | 
|---|
| 165 |   for (int k=1; k<narg; k++)   {
 | 
|---|
| 166 |     if (strcmp(arg[k], "-lmax") == 0)  {
 | 
|---|
| 167 |       if (k == narg-1) Usage(true);  // -lmax est suivi d'un argument 
 | 
|---|
| 168 |       lmax = atoi(arg[k+1]);  k++;       // k++ pour sauter au suivant
 | 
|---|
| 169 |     }
 | 
|---|
| 170 |     else if (strcmp(arg[k], "-thetacut") == 0)  {
 | 
|---|
| 171 |       if (k == narg-1) Usage(true);  // -thetacut est suivi d'un argument 
 | 
|---|
| 172 |       tcut = atof(arg[k+1]);  k++;       // k++ pour sauter au suivant
 | 
|---|
| 173 |     }
 | 
|---|
| 174 |     else if (strcmp(arg[k], "-iter_order") == 0)  {
 | 
|---|
| 175 |       if (k == narg-1) Usage(true);  // -iter_order est suivi d'un argument 
 | 
|---|
| 176 |       iterationOrder = atof(arg[k+1]);  k++;   // k++ pour sauter au suivant
 | 
|---|
| 177 |     }
 | 
|---|
| 178 |     else if (strcmp(arg[k], "-fitsin") == 0) {
 | 
|---|
| 179 |       fgfitsin = true;  
 | 
|---|
| 180 |     }
 | 
|---|
| 181 |     else if (strcmp(arg[k], "-fitsout") == 0) {
 | 
|---|
| 182 |       fgfitsout = true;  
 | 
|---|
| 183 |     }
 | 
|---|
| 184 |     else if (strcmp(arg[k], "-keepalm") == 0) {
 | 
|---|
| 185 |       fgkeepalm = true;  
 | 
|---|
| 186 |     }
 | 
|---|
| 187 |     else if ((strcmp(arg[k], "-float") == 0) || (strcmp(arg[k], "-r4") == 0) ) {
 | 
|---|
| 188 |       fgr4 = true;  
 | 
|---|
| 189 |     }
 | 
|---|
| 190 |    
 | 
|---|
| 191 |     else { ko = k; break; }  // Debut des noms
 | 
|---|
| 192 |   }
 | 
|---|
| 193 | 
 | 
|---|
| 194 |   if ((narg-ko) < 2)  Usage(true); 
 | 
|---|
| 195 |   infile = arg[ko];
 | 
|---|
| 196 |   outfile = arg[ko+1];
 | 
|---|
| 197 |   
 | 
|---|
| 198 |   // Bloc try de gestion des exception 
 | 
|---|
| 199 |   try {
 | 
|---|
| 200 |     InitTim();
 | 
|---|
| 201 |     SophyaInit();
 | 
|---|
| 202 |     if (fgr4) {
 | 
|---|
| 203 |       cout << " SphereHEALPix<r_4>  --> Power spectrum C_l<r_4> (float)" << endl;
 | 
|---|
| 204 |       _Map2Cl<r_4>::ComputeCl(infile, outfile, lmax, tcut, iterationOrder, 
 | 
|---|
| 205 |                               fgfitsin, fgfitsout, fgkeepalm);
 | 
|---|
| 206 |     }
 | 
|---|
| 207 |     else {
 | 
|---|
| 208 |       cout << " SphereHEALPix<r_8>  --> Power spectrum C_l<r_8> (double)" << endl;
 | 
|---|
| 209 |       _Map2Cl<r_8>::ComputeCl(infile, outfile, lmax, tcut, iterationOrder,
 | 
|---|
| 210 |                               fgfitsin, fgfitsout, fgkeepalm);
 | 
|---|
| 211 |     }
 | 
|---|
| 212 |   }
 | 
|---|
| 213 |   catch (PThrowable & exc) {   // Exceptions de SOPHYA
 | 
|---|
| 214 |     cerr << " map2cl: Catched Exception " << (string)typeid(exc).name()
 | 
|---|
| 215 |          << " - Msg= " << exc.Msg() << endl;
 | 
|---|
| 216 |   }
 | 
|---|
| 217 |   catch (...) {    // Autres Exceptions
 | 
|---|
| 218 |     cerr << " map2cl: some other exception was caught ! " << endl;
 | 
|---|
| 219 |   }
 | 
|---|
| 220 |   
 | 
|---|
| 221 |   PrtTim("End of map2cl ");
 | 
|---|
| 222 |   return(0);
 | 
|---|
| 223 | }
 | 
|---|
| 224 | 
 | 
|---|
| 225 | 
 | 
|---|
| 226 |   
 | 
|---|