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