| 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 | \ingroup PrgMap | 
|---|
| 19 | \file prjsmap.cc | 
|---|
| 20 | \brief \b prjsmap: Molleweide and sinus projection of sky maps | 
|---|
| 21 |  | 
|---|
| 22 | \verbatim | 
|---|
| 23 |  | 
|---|
| 24 | csh> prjsmap -h | 
|---|
| 25 | SOPHYA Version  1.1 Revision 0 (V_Fev2001) -- Mar  9 2001 15:45:31 cxx | 
|---|
| 26 |  | 
|---|
| 27 | prjsmap : Molleweide and Sinus projection for sky maps | 
|---|
| 28 | Usage: prjsmap [-float/-r4] [-sinus] [-sx sizeX] [-sy sizeY] [-scale fact] | 
|---|
| 29 | [-off val][-ump val] [-invy] [-fitsin] [-fitsout] InFileName OutFileName | 
|---|
| 30 | -float (-r4): single precision sky map and projection (default = double) | 
|---|
| 31 | -sinus: Selects Sinus projection (default Molleweide projection) | 
|---|
| 32 | -sx sizeX: Projected map size in X | 
|---|
| 33 | -sy sizeY: Projected map size in Y | 
|---|
| 34 | default: sizeX = 2*sizeY ; sizeX*sizeY ~ map.NbPixels()/2 | 
|---|
| 35 | -scale fact: scale factor applied to skymap pixels (def=1.) | 
|---|
| 36 | -off val: offset value for projected map (def=0.) | 
|---|
| 37 | prj(ix, jy) = offset + scale*skymap(theta, phi) | 
|---|
| 38 | -ump val: Unmapped pixel value in projected map (def=0.) | 
|---|
| 39 | -invy: reverses the projection direction along Y (Theta) | 
|---|
| 40 | -fitsout: Select the FITS format for the output map (default PPF format) | 
|---|
| 41 | -fitsin : Select the FITS format for the input vector (default PPF format) | 
|---|
| 42 | InFileName : Input file name (HEALPix map) | 
|---|
| 43 | OutFileName : Output file name (the C_l vector) | 
|---|
| 44 |  | 
|---|
| 45 | \endverbatim | 
|---|
| 46 | */ | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | /* Nouvelle-Fonction */ | 
|---|
| 50 | void Usage(bool fgerr) | 
|---|
| 51 | { | 
|---|
| 52 | cout << endl; | 
|---|
| 53 | if (fgerr) { | 
|---|
| 54 | cout << " prjsmap : Argument Error ! prjsmap -h for usage " << endl; | 
|---|
| 55 | exit(1); | 
|---|
| 56 | } | 
|---|
| 57 | else { | 
|---|
| 58 | cout << " prjsmap : Molleweide and Sinus projection for sky maps \n" | 
|---|
| 59 | << " Usage: prjsmap [-float/-r4] [-sinus] [-sx sizeX] [-sy sizeY] [-scale fact] \n" | 
|---|
| 60 | << "        [-off val][-ump val] [-invy] [-fitsin] [-fitsout] InFileName OutFileName\n" | 
|---|
| 61 | << "   -float (-r4): single precision sky map and projection (default = double) \n" | 
|---|
| 62 | << "   -sinus: Selects Sinus projection (default Molleweide projection) \n" | 
|---|
| 63 | << "   -sx sizeX: Projected map size in X \n" | 
|---|
| 64 | << "   -sy sizeY: Projected map size in Y \n" | 
|---|
| 65 | << "       default: sizeX = 2*sizeY ; sizeX*sizeY ~ map.NbPixels()/2 \n" | 
|---|
| 66 | << "   -scale fact: scale factor applied to skymap pixels (def=1.) \n" | 
|---|
| 67 | << "   -off val: offset value for projected map (def=0.) \n" | 
|---|
| 68 | << "             prj(ix, jy) = offset + scale*skymap(theta, phi) \n" | 
|---|
| 69 | << "   -ump val: Unmapped pixel value in projected map (def=0.) \n" | 
|---|
| 70 | << "   -invy: reverses the projection direction along Y (Theta) \n" | 
|---|
| 71 | << "   -fitsout: Select the FITS format for the output map (default PPF format) \n" | 
|---|
| 72 | << "   -fitsin : Select the FITS format for the input vector (default PPF format) \n" | 
|---|
| 73 | << "   InFileName : Input file name (HEALPix map) \n" | 
|---|
| 74 | << "   OutFileName : Output file name (the C_l vector) \n" << endl; | 
|---|
| 75 | exit(0); | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | /* Nouvelle-Fonction */ | 
|---|
| 80 | template <class T> | 
|---|
| 81 | class _PrjMap { | 
|---|
| 82 | public : | 
|---|
| 83 | /* Methode */ | 
|---|
| 84 | static void PM_MeanSigma(PixelMap<T> const & map, double& gmoy, double& gsig, T& min, T& max) | 
|---|
| 85 | { | 
|---|
| 86 | min = max = map(0); | 
|---|
| 87 | gmoy=0.; | 
|---|
| 88 | gsig = 0.; | 
|---|
| 89 | double valok; | 
|---|
| 90 | for(int k=0; k<map.NbPixels(); k++) { | 
|---|
| 91 | valok = map(k); | 
|---|
| 92 | gmoy += valok;  gsig += valok*valok; | 
|---|
| 93 | if (map(k)<min)  min = map(k); | 
|---|
| 94 | else if (map(k)>max)  max = map(k); | 
|---|
| 95 | } | 
|---|
| 96 | gmoy /= (double)map.NbPixels(); | 
|---|
| 97 | gsig = gsig/(double)map.NbPixels() - gmoy*gmoy; | 
|---|
| 98 | if (gsig >= 0.) gsig = sqrt(gsig); | 
|---|
| 99 | return; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | /* Methode */ | 
|---|
| 103 | static TArray<T> Project_Molleweide(SphericalMap<T> const & map, int sx=0, int sy=0, T ump=0, | 
|---|
| 104 | T offset=0, T scale=1, bool invy=false) | 
|---|
| 105 | { | 
|---|
| 106 | if ( (sx <= 0) && (sy <= 0) ) { | 
|---|
| 107 | sy = sqrt((double)(map.NbPixels()/2)); | 
|---|
| 108 | sx = sy*2; | 
|---|
| 109 | } | 
|---|
| 110 | else { | 
|---|
| 111 | if (sx <= 0) sx = 2*sy; | 
|---|
| 112 | else sy = sx/2; | 
|---|
| 113 | } | 
|---|
| 114 | TArray<T> prj(sx, sy); | 
|---|
| 115 | prj = ump;   // On met tout a ump | 
|---|
| 116 |  | 
|---|
| 117 | cout << " Molleweide projection (sizeX= " << sx << " sizeY= " << sy << " )" | 
|---|
| 118 | << " Scale=" << scale << " Offset=" << offset << endl; | 
|---|
| 119 | r_8 xa, yd, teta,phi, facteur; | 
|---|
| 120 | int_4 ix, jy, k; | 
|---|
| 121 | for(jy=0; jy<sy; jy++) { | 
|---|
| 122 | yd = (r_8)(jy+0.5)/(r_8)sy-0.5; | 
|---|
| 123 | if (invy) teta = (0.5-yd)*M_PI; | 
|---|
| 124 | else teta = (yd+0.5)*M_PI; | 
|---|
| 125 | facteur=2.*M_PI/sin(acos((double)yd*2)); | 
|---|
| 126 | for(ix=0; ix<sx; ix++)  { | 
|---|
| 127 | xa = (r_8)(ix+0.5)/(r_8)sx-0.5; | 
|---|
| 128 | phi = xa*facteur+M_PI; | 
|---|
| 129 | if ( (phi <= 2*M_PI) && (phi >= 0.) ) { | 
|---|
| 130 | k = map.PixIndexSph(teta, phi); | 
|---|
| 131 | prj(ix,jy,0) = offset + scale*map(k); | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | return prj; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | /* Methode */ | 
|---|
| 140 | static TArray<T> Project_Sinus(SphericalMap<T> const & map, int sx=0, int sy=0, T ump=0, | 
|---|
| 141 | T offset=0, T scale=1, bool invy=false) | 
|---|
| 142 | { | 
|---|
| 143 | if ( (sx <= 0) && (sy <= 0) ) { | 
|---|
| 144 | sy = sqrt((double)(map.NbPixels()/2)); | 
|---|
| 145 | sx = sy*2; | 
|---|
| 146 | } | 
|---|
| 147 | else { | 
|---|
| 148 | if (sx <= 0) sx = 2*sy; | 
|---|
| 149 | else sy = sx/2; | 
|---|
| 150 | } | 
|---|
| 151 | TArray<T> prj(sx, sy); | 
|---|
| 152 | prj = ump;   // On met tout a ump | 
|---|
| 153 |  | 
|---|
| 154 | cout << " Sinus projection (sizeX= " << sx << " sizeY= " << sy << " )" | 
|---|
| 155 | << " Scale=" << scale << " Offset=" << offset << endl; | 
|---|
| 156 | r_8 xa, yd, teta,phi, facteur; | 
|---|
| 157 | int_4 ix, jy, k; | 
|---|
| 158 | for(jy=0; jy<sy; jy++) { | 
|---|
| 159 | yd = (r_8)(jy+0.5)/(r_8)sy-0.5; | 
|---|
| 160 | if (invy) teta = (0.5-yd)*M_PI; | 
|---|
| 161 | else teta = (yd+0.5)*M_PI; | 
|---|
| 162 | facteur=1./sin(teta); | 
|---|
| 163 | for(ix=0; ix<sx; ix++)  { | 
|---|
| 164 | xa = (r_8)(ix+0.5)/(r_8)sx-0.5; | 
|---|
| 165 | phi = 2*M_PI*xa*facteur+M_PI; | 
|---|
| 166 | if ( (phi <= 2*M_PI) && (phi >= 0.) ) { | 
|---|
| 167 | k = map.PixIndexSph(teta, phi); | 
|---|
| 168 | prj(ix,jy,0) = offset + scale*map(k); | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 | return prj; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | /* Methode */ | 
|---|
| 176 | static void Project(string & infile, string & outfile, int sx, int sy, T ump, T offset, | 
|---|
| 177 | T scale, bool fgsin, bool invy, bool fgfitsin, bool fgfitsout) | 
|---|
| 178 | { | 
|---|
| 179 |  | 
|---|
| 180 | double deg2rad =  M_PI/180.; | 
|---|
| 181 | double minute2rad =  M_PI/(180.*60.); | 
|---|
| 182 |  | 
|---|
| 183 | SphericalMap<T> * sphp = NULL; | 
|---|
| 184 | SphereHEALPix<T> sphh; | 
|---|
| 185 |  | 
|---|
| 186 | PPersist * pp = NULL; | 
|---|
| 187 |  | 
|---|
| 188 | if (fgfitsin) { | 
|---|
| 189 | cout << "--- Reading Input FITS file " << infile << endl; | 
|---|
| 190 | FitsInFile fii(infile); | 
|---|
| 191 | fii >> sphh; | 
|---|
| 192 | sphp = &sphh; | 
|---|
| 193 | } | 
|---|
| 194 | else { | 
|---|
| 195 | cout << "--- Reading Input PPF file " << infile << endl; | 
|---|
| 196 | PInPersist ppi(infile); | 
|---|
| 197 | pp = ppi.ReadObject(); | 
|---|
| 198 | if (pp) | 
|---|
| 199 | sphp = dynamic_cast< SphericalMap<T> * > (pp->DataObj()); | 
|---|
| 200 | else sphp = NULL; | 
|---|
| 201 | if (sphp == NULL) { | 
|---|
| 202 | cout << " Object in PPF file is not a SphericalMap<T> or wrong data type ! " << endl; | 
|---|
| 203 | throw TypeMismatchExc("_PrjMap::Project(...) Error reading input PPF file !"); | 
|---|
| 204 | } | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | SphericalMap<T> & sph = *sphp; | 
|---|
| 208 | T min, max; | 
|---|
| 209 | double mean, sigma; | 
|---|
| 210 | PM_MeanSigma(sph, mean, sigma, min, max); | 
|---|
| 211 |  | 
|---|
| 212 | cout << " Input map type= " << sph.TypeOfMap() << " PixParm (=NSide for HEALPix)= " | 
|---|
| 213 | << sph.SizeIndex() << endl; | 
|---|
| 214 | cout << " Input map : NbPixels= " << sph.NbPixels() << " Resolution= " | 
|---|
| 215 | << sqrt(sph.PixSolAngle(0))/minute2rad << " Arcminutes " << endl; | 
|---|
| 216 | cout << " map.Min= " << min << " map.Max= " << max | 
|---|
| 217 | << " map.Mean= " << mean << " map.Sigma= " << sigma << endl; | 
|---|
| 218 |  | 
|---|
| 219 | cout << "--- Sky map projection " << endl; | 
|---|
| 220 | TArray<T> prj; | 
|---|
| 221 |  | 
|---|
| 222 | if (fgsin) prj = Project_Sinus(sph, sx, sy, ump, offset, scale, invy); | 
|---|
| 223 | else prj = Project_Molleweide(sph, sx, sy, ump, offset, scale, invy); | 
|---|
| 224 |  | 
|---|
| 225 | cout << "--- Statistics on the projected map :" ; | 
|---|
| 226 | prj.Show(); | 
|---|
| 227 | prj.MinMax(min, max); | 
|---|
| 228 | MeanSigma(prj, mean, sigma); | 
|---|
| 229 | cout << " prj.Min= " << min << " prj.Max= " << max | 
|---|
| 230 | << " prj.Mean= " << mean << " prj.Sigma= " << sigma << endl; | 
|---|
| 231 |  | 
|---|
| 232 | if (fgfitsout) { | 
|---|
| 233 | cout << "--- Writing projection to Output FITS file " << outfile << endl; | 
|---|
| 234 | FitsOutFile fio(outfile, FitsFile::clear); | 
|---|
| 235 | fio << prj; | 
|---|
| 236 | } | 
|---|
| 237 | else { | 
|---|
| 238 | cout << "--- Writing projection to Output PPF file " << outfile << endl; | 
|---|
| 239 | POutPersist ppo(outfile); | 
|---|
| 240 | ppo << prj; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | if (pp) delete pp; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | }; | 
|---|
| 247 |  | 
|---|
| 248 | /* Main-Program */ | 
|---|
| 249 | int main(int narg, char *arg[]) | 
|---|
| 250 | { | 
|---|
| 251 | if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false); | 
|---|
| 252 |  | 
|---|
| 253 | double ump = 0.; | 
|---|
| 254 | double off = 0.; | 
|---|
| 255 | double scale = 1.; | 
|---|
| 256 | int sx = 0; | 
|---|
| 257 | int sy = 0; | 
|---|
| 258 | string infile; | 
|---|
| 259 | string outfile; | 
|---|
| 260 | bool fgfitsin = false; | 
|---|
| 261 | bool fgfitsout = false; | 
|---|
| 262 | bool fgr4 = false; | 
|---|
| 263 | bool fgsinus = false; | 
|---|
| 264 | bool fginvy = false; | 
|---|
| 265 |  | 
|---|
| 266 | cout << " prjsmap : Decoding command line options ... " << endl; | 
|---|
| 267 |  | 
|---|
| 268 | int ko=1; | 
|---|
| 269 | for (int k=1; k<narg; k++)   { | 
|---|
| 270 | if (strcmp(arg[k], "-sx") == 0)  { | 
|---|
| 271 | if (k == narg-1) Usage(true);  // -sx est suivi d'un argument | 
|---|
| 272 | sx = atoi(arg[k+1]);  k++;       // k++ pour sauter au suivant | 
|---|
| 273 | } | 
|---|
| 274 | else if (strcmp(arg[k], "-sy") == 0)  { | 
|---|
| 275 | if (k == narg-1) Usage(true);  // -sy est suivi d'un argument | 
|---|
| 276 | sy = atoi(arg[k+1]);  k++;       // k++ pour sauter au suivant | 
|---|
| 277 | } | 
|---|
| 278 | else if (strcmp(arg[k], "-scale") == 0)  { | 
|---|
| 279 | if (k == narg-1) Usage(true); | 
|---|
| 280 | scale = atof(arg[k+1]);  k++; | 
|---|
| 281 | } | 
|---|
| 282 | else if (strcmp(arg[k], "-off") == 0)  { | 
|---|
| 283 | if (k == narg-1) Usage(true); | 
|---|
| 284 | off = atof(arg[k+1]);  k++; | 
|---|
| 285 | } | 
|---|
| 286 | else if (strcmp(arg[k], "-ump") == 0)  { | 
|---|
| 287 | if (k == narg-1) Usage(true); | 
|---|
| 288 | ump = atof(arg[k+1]);  k++; | 
|---|
| 289 | } | 
|---|
| 290 | else if (strcmp(arg[k], "-sinus") == 0) { | 
|---|
| 291 | fgsinus = true; | 
|---|
| 292 | } | 
|---|
| 293 | else if (strcmp(arg[k], "-invy") == 0) { | 
|---|
| 294 | fginvy = true; | 
|---|
| 295 | } | 
|---|
| 296 | else if (strcmp(arg[k], "-fitsin") == 0) { | 
|---|
| 297 | fgfitsin = true; | 
|---|
| 298 | } | 
|---|
| 299 | else if (strcmp(arg[k], "-fitsout") == 0) { | 
|---|
| 300 | fgfitsout = true; | 
|---|
| 301 | } | 
|---|
| 302 | else if ((strcmp(arg[k], "-float") == 0) || (strcmp(arg[k], "-r4") == 0) ) { | 
|---|
| 303 | fgr4 = true; | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | else { ko = k; break; }  // Debut des noms | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | if ((narg-ko) < 2)  Usage(true); | 
|---|
| 310 | infile = arg[ko]; | 
|---|
| 311 | outfile = arg[ko+1]; | 
|---|
| 312 |  | 
|---|
| 313 | // Bloc try de gestion des exception | 
|---|
| 314 | try { | 
|---|
| 315 | InitTim(); | 
|---|
| 316 | SophyaInit(); | 
|---|
| 317 | if (fgr4) { | 
|---|
| 318 | cout << " Projecting SphericalMap<r_4>  -->  Array<r_4> (float)" << endl; | 
|---|
| 319 | _PrjMap<r_4>::Project(infile, outfile, sx, sy, ump, off, scale, | 
|---|
| 320 | fgsinus, fginvy, fgfitsin, fgfitsout); | 
|---|
| 321 | } | 
|---|
| 322 | else { | 
|---|
| 323 | cout << " Projecting SphericalMap<r_8>  -->  Array<r_8> (double)" << endl; | 
|---|
| 324 | _PrjMap<r_8>::Project(infile, outfile, sx, sy, ump, off, scale, | 
|---|
| 325 | fgsinus, fginvy, fgfitsin, fgfitsout); | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|
| 328 | catch (PThrowable & exc) {   // Exceptions de SOPHYA | 
|---|
| 329 | cerr << " prjsmap: Catched Exception " << (string)typeid(exc).name() | 
|---|
| 330 | << " - Msg= " << exc.Msg() << endl; | 
|---|
| 331 | } | 
|---|
| 332 | catch (...) {    // Autres Exceptions | 
|---|
| 333 | cerr << " prjsmap: some other exception was caught ! " << endl; | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | PrtTim("End of prjsmap "); | 
|---|
| 337 | return(0); | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 |  | 
|---|
| 341 |  | 
|---|