| 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 | /*!
 | 
|---|
| 17 |   \ingroup PrgUtil
 | 
|---|
| 18 |   \file cl2map.cc
 | 
|---|
| 19 |   \brief \b cl2map: generates spherical maps from power spectra (C(l))
 | 
|---|
| 20 | 
 | 
|---|
| 21 |   \verbatim
 | 
|---|
| 22 | 
 | 
|---|
| 23 |   csh> cl2map -h
 | 
|---|
| 24 |   SOPHYA Version  1.1 Revision 0 (V_Fev2001) -- Mar  9 2001 15:45:31 cxx
 | 
|---|
| 25 | 
 | 
|---|
| 26 |   cl2map : Spherical harmonics synthesis - Power spectrum C_l -> HEALPix map
 | 
|---|
| 27 |   Usage: cl2map [-float/-r4] [-msph pixp] [-beam fwhm]
 | 
|---|
| 28 |          [-fitsin] [-fitsout] [-autoinirand] InFileName OutFileName
 | 
|---|
| 29 |    -float (-r4): single precision C_l and map (default = double)
 | 
|---|
| 30 |    -msph pix: Spherical map pixelization parameter
 | 
|---|
| 31 |               nside=2^n for HEALPix - default=128
 | 
|---|
| 32 |                 lmax = 2^n  default=128
 | 
|---|
| 33 |    -beam fwhm : Beam moothing (in l-space) FWHM in arcmin, default=0
 | 
|---|
| 34 |    -fitsout: Select the FITS format for the output map (default PPF format)
 | 
|---|
| 35 |    -fitsin : Select the FITS format for the input vector (default PPF format)
 | 
|---|
| 36 |    -autoinirand : Automtatic random number generator initialization
 | 
|---|
| 37 |    InFileName : Input file name containing the C_l vector
 | 
|---|
| 38 |    OutFileName : Output file name (HEALPix map)
 | 
|---|
| 39 | 
 | 
|---|
| 40 |   \endverbatim
 | 
|---|
| 41 | */
 | 
|---|
| 42 | 
 | 
|---|
| 43 | 
 | 
|---|
| 44 | /* Nouvelle-Fonction */
 | 
|---|
| 45 | void Usage(bool fgerr)
 | 
|---|
| 46 | {
 | 
|---|
| 47 |   cout << endl;
 | 
|---|
| 48 |   if (fgerr) {
 | 
|---|
| 49 |     cout << " cl2map : Argument Error ! cl2map -h for usage " << endl; 
 | 
|---|
| 50 |     exit(1);
 | 
|---|
| 51 |   }
 | 
|---|
| 52 |   else {
 | 
|---|
| 53 |     cout << " cl2map : Spherical harmonics synthesis - Power spectrum C_l -> HEALPix map \n" 
 | 
|---|
| 54 |          << " Usage: cl2map [-float/-r4] [-msph pixp] [-beam fwhm] \n" 
 | 
|---|
| 55 |          << "        [-fitsin] [-fitsout] [-autoinirand] InFileName OutFileName \n" 
 | 
|---|
| 56 |          << "   -float (-r4): single precision C_l and map (default = double) \n"
 | 
|---|
| 57 |          << "   -msph pix: Spherical map pixelization parameter \n" 
 | 
|---|
| 58 |          << "              nside=2^n for HEALPix - default=128 \n"
 | 
|---|
| 59 |          << "                lmax = 2^n  default=128 \n"
 | 
|---|
| 60 |          << "   -beam fwhm : Beam moothing (in l-space) FWHM in arcmin, default=0 \n"
 | 
|---|
| 61 |          << "   -fitsout: Select the FITS format for the output map (default PPF format) \n"
 | 
|---|
| 62 |          << "   -fitsin : Select the FITS format for the input vector (default PPF format) \n"
 | 
|---|
| 63 |          << "   -autoinirand : Automtatic random number generator initialization \n" 
 | 
|---|
| 64 |          << "   InFileName : Input file name containing the C_l vector \n" 
 | 
|---|
| 65 |          << "   OutFileName : Output file name (HEALPix map) \n" << endl;
 | 
|---|
| 66 |     exit(0);
 | 
|---|
| 67 |   }  
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | /* Nouvelle-Classe */
 | 
|---|
| 71 | template <class T> 
 | 
|---|
| 72 | class _Cl2Map {
 | 
|---|
| 73 | public :
 | 
|---|
| 74 | static void BuildMap(string & infile, string & outfile, int msph, double beam, 
 | 
|---|
| 75 |                      bool fgfitsin, bool fgfitsout)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   double deg2rad =  M_PI/180.;
 | 
|---|
| 78 |   double minute2rad =  M_PI/(180.*60.);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   TArray<T> cla;
 | 
|---|
| 81 |   SphereHEALPix<T> sph;
 | 
|---|
| 82 |   if (fgfitsin) {
 | 
|---|
| 83 |     cout << "--- Reading Input FITS file " << infile << endl; 
 | 
|---|
| 84 |     FitsInFile fii(infile);
 | 
|---|
| 85 |     fii >> cla;
 | 
|---|
| 86 |   }
 | 
|---|
| 87 |   else {
 | 
|---|
| 88 |     cout << "--- Reading Input PPF file " << infile << endl; 
 | 
|---|
| 89 |     PInPersist ppi(infile);
 | 
|---|
| 90 |     ppi >> cla;
 | 
|---|
| 91 |   }
 | 
|---|
| 92 |   TVector<T> clvec(cla);
 | 
|---|
| 93 |   cout << " Input vector : " ; 
 | 
|---|
| 94 |   clvec.Show(); 
 | 
|---|
| 95 |   T min, max;
 | 
|---|
| 96 |   double mean, sigma;
 | 
|---|
| 97 |   clvec.MinMax(min, max);
 | 
|---|
| 98 |   MeanSigma(clvec, mean, sigma);
 | 
|---|
| 99 |   cout << " C_l.Min= " << min << " C_l.Max= " << max 
 | 
|---|
| 100 |        << " C_l.Mean= " << mean << " C_l.Sigma= " << sigma << endl;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   cout << "--- Calling  GenerateFromCl() (beam= " << beam 
 | 
|---|
| 103 |        << "' msph= " << msph << ") beam=" << beam*minute2rad << " rad" << endl;
 | 
|---|
| 104 |   // on cree la carte 
 | 
|---|
| 105 |   SphericalTransformServer<T> sphtr;
 | 
|---|
| 106 |   sphtr.GenerateFromCl(sph, msph, clvec, beam*minute2rad);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   cout << " Output map : NbPixels= " <<  sph.NbPixels() << " NSide= " 
 | 
|---|
| 109 |        << sph.SizeIndex() << " Resolution= "
 | 
|---|
| 110 |        << sqrt(sph.PixSolAngle(0))/minute2rad << " Arcminutes " << endl;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   if (fgfitsout) {
 | 
|---|
| 113 |     cout << "--- Writing map to Output FITS file " << outfile << endl;
 | 
|---|
| 114 |     string dum = "rm -f "; dum += outfile; system(dum.c_str()); 
 | 
|---|
| 115 |     FitsOutFile fio(outfile);
 | 
|---|
| 116 |     fio << sph;
 | 
|---|
| 117 |   }
 | 
|---|
| 118 |   else {
 | 
|---|
| 119 |     cout << "--- Writing map to Output PPF file " << outfile << endl; 
 | 
|---|
| 120 |     POutPersist ppo(outfile);
 | 
|---|
| 121 |     ppo << sph;
 | 
|---|
| 122 |   }
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | };
 | 
|---|
| 126 | 
 | 
|---|
| 127 | /* Main-Program */
 | 
|---|
| 128 | int main(int narg, char *arg[])
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false);
 | 
|---|
| 131 |   
 | 
|---|
| 132 |   int msph = 128;
 | 
|---|
| 133 |   double beam = 0.;
 | 
|---|
| 134 |   string infile;
 | 
|---|
| 135 |   string outfile;
 | 
|---|
| 136 |   bool fgfitsin = false;
 | 
|---|
| 137 |   bool fgfitsout = false;
 | 
|---|
| 138 |   bool fginirand = false;
 | 
|---|
| 139 |   bool fgr4 = false;
 | 
|---|
| 140 |   cout << " cl2map : Decoding command line options ... " << endl;
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   int ko=1;
 | 
|---|
| 143 |   for (int k=1; k<narg; k++)   {
 | 
|---|
| 144 |     if (strcmp(arg[k], "-msph") == 0)  {
 | 
|---|
| 145 |         if (k == narg-1) Usage(true);  // -msph est suivi d'un argument 
 | 
|---|
| 146 |       msph = atoi(arg[k+1]);  k++;       // k++ pour sauter au suivant
 | 
|---|
| 147 |     }
 | 
|---|
| 148 |     else if (strcmp(arg[k], "-beam") == 0)  {
 | 
|---|
| 149 |         if (k == narg-1) Usage(true);  // -beam est suivi d'un argument 
 | 
|---|
| 150 |       beam = atof(arg[k+1]);  k++;       // k++ pour sauter au suivant
 | 
|---|
| 151 |     }
 | 
|---|
| 152 |     else if (strcmp(arg[k], "-fitsin") == 0) {
 | 
|---|
| 153 |       fgfitsin = true;  
 | 
|---|
| 154 |     }
 | 
|---|
| 155 |     else if (strcmp(arg[k], "-fitsout") == 0) {
 | 
|---|
| 156 |       fgfitsout = true;  
 | 
|---|
| 157 |     }
 | 
|---|
| 158 |     else if (strcmp(arg[k], "-autoinirand") == 0) {
 | 
|---|
| 159 |       fginirand = true;  
 | 
|---|
| 160 |     }
 | 
|---|
| 161 |     else if ((strcmp(arg[k], "-float") == 0) || (strcmp(arg[k], "-r4") == 0) ) {
 | 
|---|
| 162 |       fgr4 = true;  
 | 
|---|
| 163 |     }
 | 
|---|
| 164 |    
 | 
|---|
| 165 |     else { ko = k; break; }  // Debut des noms
 | 
|---|
| 166 |   }
 | 
|---|
| 167 | 
 | 
|---|
| 168 |   if ((narg-ko) < 2)  Usage(true); 
 | 
|---|
| 169 |   infile = arg[ko];
 | 
|---|
| 170 |   outfile = arg[ko+1];
 | 
|---|
| 171 |   
 | 
|---|
| 172 |   // Bloc try de gestion des exception 
 | 
|---|
| 173 |   try {
 | 
|---|
| 174 |     InitTim();
 | 
|---|
| 175 |     SophyaInit();
 | 
|---|
| 176 |     if (fginirand) {
 | 
|---|
| 177 |       cout << " Setting random number generator seed (using time) " << endl; 
 | 
|---|
| 178 |       Auto_Ini_Ranf(1);
 | 
|---|
| 179 |     }
 | 
|---|
| 180 |     if (fgr4) {
 | 
|---|
| 181 |       cout << " Power spectrum C_l (r_4) --> SphereHEALPix<r_4> (float) " << endl;
 | 
|---|
| 182 |       _Cl2Map<r_4>::BuildMap(infile, outfile, msph, beam, fgfitsin, fgfitsout);
 | 
|---|
| 183 |     }
 | 
|---|
| 184 |     else {
 | 
|---|
| 185 |       cout << " Power spectrum C_l (r_8) --> SphereHEALPix<r_8> (double) " << endl;
 | 
|---|
| 186 |       _Cl2Map<r_8>::BuildMap(infile, outfile, msph, beam, fgfitsin, fgfitsout);
 | 
|---|
| 187 |     }
 | 
|---|
| 188 |   }
 | 
|---|
| 189 |   catch (PThrowable & exc) {   // Exceptions de SOPHYA
 | 
|---|
| 190 |     cerr << " cl2map: Catched Exception " << (string)typeid(exc).name()
 | 
|---|
| 191 |          << " - Msg= " << exc.Msg() << endl;
 | 
|---|
| 192 |   }
 | 
|---|
| 193 |   catch (...) {    // Autres Exceptions
 | 
|---|
| 194 |     cerr << " cl2map: some other exception was caught ! " << endl;
 | 
|---|
| 195 |   }
 | 
|---|
| 196 |   
 | 
|---|
| 197 |   PrtTim("End of cl2map ");
 | 
|---|
| 198 |   return(0);
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   
 | 
|---|