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