[1195] | 1 | // utilitaires de pixelisation HEALPix
|
---|
| 2 | #include "HEALPixUtils.h"
|
---|
| 3 | #include <iostream.h>
|
---|
| 4 | #include <math.h>
|
---|
| 5 | //#include <complex>
|
---|
| 6 | #include "tvector.h"
|
---|
| 7 | #include "smathconst.h"
|
---|
| 8 | extern "C"
|
---|
| 9 | {
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include <stdlib.h>
|
---|
| 12 | #include <unistd.h>
|
---|
| 13 | }
|
---|
| 14 |
|
---|
[1196] | 15 | using namespace SOPHYA;
|
---|
| 16 |
|
---|
| 17 | //////////////////////////////////////////////////////////////////////////
|
---|
| 18 | //
|
---|
| 19 | // ------------- Classe PIXELS_XY -----------------------
|
---|
| 20 | //
|
---|
| 21 | class PIXELS_XY
|
---|
[1195] | 22 | {
|
---|
| 23 |
|
---|
[1196] | 24 | public :
|
---|
[1195] | 25 |
|
---|
[1196] | 26 | static PIXELS_XY& instance();
|
---|
[1195] | 27 |
|
---|
[1196] | 28 | NDataBlock<int_4> pix2x_;
|
---|
| 29 | NDataBlock<int_4> pix2y_;
|
---|
| 30 | NDataBlock<int_4> x2pix_;
|
---|
| 31 | NDataBlock<int_4> y2pix_;
|
---|
| 32 |
|
---|
| 33 | private :
|
---|
| 34 |
|
---|
| 35 | PIXELS_XY();
|
---|
| 36 | void mk_pix2xy();
|
---|
| 37 | void mk_xy2pix();
|
---|
| 38 | };
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 |
|
---|
[1195] | 42 | //*******************************************************************
|
---|
| 43 | // Class PIXELS_XY
|
---|
| 44 | // Construction des tableaux necessaires a la traduction des indices RING en
|
---|
| 45 | // indices NESTED (ou l'inverse)
|
---|
| 46 | //*******************************************************************
|
---|
| 47 |
|
---|
| 48 | PIXELS_XY::PIXELS_XY()
|
---|
| 49 | {
|
---|
| 50 | pix2x_.ReSize(1024);
|
---|
| 51 | pix2x_.Reset();
|
---|
| 52 | pix2y_.ReSize(1024);
|
---|
| 53 | pix2y_.Reset();
|
---|
| 54 | x2pix_.ReSize(128);
|
---|
| 55 | x2pix_.Reset();
|
---|
| 56 | y2pix_.ReSize(128);
|
---|
| 57 | y2pix_.Reset();
|
---|
| 58 | mk_pix2xy();
|
---|
| 59 | mk_xy2pix();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[1196] | 62 | // Instance unique de la classe PIXELS_XY
|
---|
[1392] | 63 | static PIXELS_XY * _singleton = NULL;
|
---|
[1196] | 64 |
|
---|
[1195] | 65 | PIXELS_XY& PIXELS_XY::instance()
|
---|
| 66 | {
|
---|
[1392] | 67 | if (_singleton == NULL) _singleton = new PIXELS_XY ;
|
---|
| 68 | return (*_singleton);
|
---|
[1195] | 69 | }
|
---|
| 70 |
|
---|
| 71 | void PIXELS_XY::mk_pix2xy()
|
---|
| 72 | {
|
---|
| 73 | /*
|
---|
| 74 | ==================================================
|
---|
| 75 | subroutine mk_pix2xy
|
---|
| 76 | ==================================================
|
---|
| 77 | c constructs the array giving x and y in the face from pixel number
|
---|
| 78 | c for the nested (quad-cube like) ordering of pixels
|
---|
| 79 | c
|
---|
| 80 | c the bits corresponding to x and y are interleaved in the pixel number
|
---|
| 81 | c one breaks up the pixel number by even and odd bits
|
---|
| 82 | ==================================================
|
---|
| 83 | */
|
---|
| 84 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 85 | // (16/12/98)
|
---|
| 86 |
|
---|
| 87 | int kpix, jpix, IX, IY, IP, ID;
|
---|
| 88 |
|
---|
| 89 | for(kpix = 0; kpix < 1024; kpix++)
|
---|
| 90 | {
|
---|
| 91 | jpix = kpix;
|
---|
| 92 | IX = 0;
|
---|
| 93 | IY = 0;
|
---|
| 94 | IP = 1 ;// ! bit position (in x and y)
|
---|
| 95 | while( jpix!=0 )
|
---|
| 96 | { // ! go through all the bits
|
---|
| 97 | ID=jpix%2;// ! bit value (in kpix), goes in ix
|
---|
| 98 | jpix = jpix/2;
|
---|
| 99 | IX = ID*IP+IX;
|
---|
| 100 |
|
---|
| 101 | ID=jpix%2;// ! bit value (in kpix), goes in iy
|
---|
| 102 | jpix = jpix/2;
|
---|
| 103 | IY = ID*IP+IY;
|
---|
| 104 |
|
---|
| 105 | IP = 2*IP;// ! next bit (in x and y)
|
---|
| 106 | }
|
---|
| 107 | pix2x_(kpix) = IX;// ! in 0,31
|
---|
| 108 | pix2y_(kpix) = IY;// ! in 0,31
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void PIXELS_XY::mk_xy2pix()
|
---|
| 113 | {
|
---|
| 114 | /*
|
---|
| 115 | =================================================
|
---|
| 116 | subroutine mk_xy2pix
|
---|
| 117 | =================================================
|
---|
| 118 | c sets the array giving the number of the pixel lying in (x,y)
|
---|
| 119 | c x and y are in {1,128}
|
---|
| 120 | c the pixel number is in {0,128**2-1}
|
---|
| 121 | c
|
---|
| 122 | c if i-1 = sum_p=0 b_p * 2^p
|
---|
| 123 | c then ix = sum_p=0 b_p * 4^p
|
---|
| 124 | c iy = 2*ix
|
---|
| 125 | c ix + iy in {0, 128**2 -1}
|
---|
| 126 | =================================================
|
---|
| 127 | */
|
---|
| 128 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 129 | // (16/12/98)
|
---|
| 130 |
|
---|
| 131 | int K,IP,I,J,ID;
|
---|
| 132 | for(I = 1; I <= 128; I++)
|
---|
| 133 | {
|
---|
| 134 | J = I-1;// !pixel numbers
|
---|
| 135 | K = 0;//
|
---|
| 136 | IP = 1;//
|
---|
| 137 | truc : if( J==0 )
|
---|
| 138 | {
|
---|
| 139 | x2pix_(I-1) = K;
|
---|
| 140 | y2pix_(I-1) = 2*K;
|
---|
| 141 | }
|
---|
| 142 | else
|
---|
| 143 | {
|
---|
| 144 | ID = (int)fmod(J,2);
|
---|
| 145 | J = J/2;
|
---|
| 146 | K = IP*ID+K;
|
---|
| 147 | IP = IP*4;
|
---|
| 148 | goto truc;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 |
|
---|
[1196] | 155 | int_4 HEALPix::nest2ring(int_4 nside, int_4 ipnest)
|
---|
[1195] | 156 | {
|
---|
| 157 | /*
|
---|
| 158 | ====================================================
|
---|
| 159 | subroutine nest2ring(nside, ipnest, ipring)
|
---|
| 160 | ====================================================
|
---|
| 161 | c conversion from NESTED to RING pixel number
|
---|
| 162 | ====================================================
|
---|
| 163 | */
|
---|
| 164 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 165 | // (16/12/98)
|
---|
| 166 |
|
---|
| 167 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 168 |
|
---|
| 169 | int npix, npface, face_num, ncap, n_before;
|
---|
| 170 | int ipf, ip_low, ip_trunc, ip_med, ip_hi;
|
---|
| 171 | int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
|
---|
| 172 | int ns_max=8192;
|
---|
| 173 | int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
|
---|
| 174 | int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
|
---|
| 175 |
|
---|
| 176 | if( nside<1 || nside>ns_max ) {
|
---|
| 177 | cout << "nside out of range" << endl;
|
---|
| 178 | exit(0);
|
---|
| 179 | }
|
---|
| 180 | npix = 12 * nside* nside;
|
---|
| 181 | if( ipnest<0 || ipnest>npix-1 ) {
|
---|
| 182 | cout << "ipnest out of range" << endl;
|
---|
| 183 | exit(0);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | ncap = 2* nside*( nside-1);// ! number of points in the North Polar cap
|
---|
| 187 | nl4 = 4* nside;
|
---|
| 188 |
|
---|
| 189 | //c finds the face, and the number in the face
|
---|
| 190 | npface = nside* nside;
|
---|
| 191 | //cccccc ip = ipnest - 1 ! in {0,npix-1}
|
---|
| 192 |
|
---|
| 193 | face_num = ipnest/npface;// ! face number in {0,11}
|
---|
| 194 | ipf =ipnest%npface;// ! pixel number in the face {0,npface-1}
|
---|
| 195 | //c finds the x,y on the face (starting from the lowest corner)
|
---|
| 196 | //c from the pixel number
|
---|
| 197 | ip_low=ipf%1024; // ! content of the last 10 bits
|
---|
| 198 | ip_trunc = ipf/1024; // ! truncation of the last 10 bits
|
---|
| 199 | ip_med=ip_trunc%1024; // ! content of the next 10 bits
|
---|
| 200 | ip_hi = ip_trunc/1024;// ! content of the high weight 10 bits
|
---|
| 201 |
|
---|
| 202 | ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
|
---|
| 203 | iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
|
---|
| 204 |
|
---|
| 205 | //c transforms this in (horizontal, vertical) coordinates
|
---|
| 206 | jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
|
---|
| 207 | jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
|
---|
| 208 |
|
---|
| 209 | //c computes the z coordinate on the sphere
|
---|
| 210 | // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
|
---|
| 211 | jr = jrll[face_num]*nside - jrt - 1;
|
---|
| 212 | nr = nside;// ! equatorial region (the most frequent)
|
---|
| 213 | n_before = ncap + nl4 * (jr - nside);
|
---|
| 214 | kshift=(jr - nside)%2;
|
---|
| 215 | if( jr<nside ) {//then ! north pole region
|
---|
| 216 | nr = jr;
|
---|
| 217 | n_before = 2 * nr * (nr - 1);
|
---|
| 218 | kshift = 0;
|
---|
| 219 | }
|
---|
| 220 | else if( jr>3*nside ) {//then ! south pole region
|
---|
| 221 | nr = nl4 - jr;
|
---|
| 222 | n_before = npix - 2 * (nr + 1) * nr;
|
---|
| 223 | kshift = 0;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | //c computes the phi coordinate on the sphere, in [0,2Pi]
|
---|
| 227 | jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
|
---|
| 228 |
|
---|
| 229 | if( jp>nl4 ) jp = jp - nl4;
|
---|
| 230 | if( jp<1 ) jp = jp + nl4;
|
---|
| 231 |
|
---|
| 232 | int aux=n_before + jp - 1;
|
---|
| 233 | return (n_before + jp - 1);// ! in {0, npix-1}
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
[1196] | 237 | int_4 HEALPix::ring2nest(int_4 nside, int_4 ipring)
|
---|
[1195] | 238 | {
|
---|
| 239 | /*
|
---|
| 240 | ==================================================
|
---|
| 241 | subroutine ring2nest(nside, ipring, ipnest)
|
---|
| 242 | ==================================================
|
---|
| 243 | c conversion from RING to NESTED pixel number
|
---|
| 244 | ==================================================
|
---|
| 245 | */
|
---|
| 246 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 247 | // (16/12/98)
|
---|
| 248 |
|
---|
| 249 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 250 |
|
---|
| 251 | double fihip, hip;
|
---|
| 252 | int npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1;
|
---|
| 253 | int kshift, face_num, nr;
|
---|
| 254 | int irn, ire, irm, irs, irt, ifm , ifp;
|
---|
| 255 | int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf;
|
---|
| 256 | int ns_max(8192);
|
---|
| 257 |
|
---|
| 258 | // coordinate of the lowest corner of each face
|
---|
| 259 | int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};// ! in unit of nside
|
---|
| 260 | int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};//! in unit of nside/2
|
---|
| 261 |
|
---|
| 262 | if( nside<1 || nside>ns_max ) {
|
---|
| 263 | cout << "nside out of range" << endl;
|
---|
| 264 | exit(0);
|
---|
| 265 | }
|
---|
| 266 | npix = 12 * nside*nside;
|
---|
| 267 | if( ipring<0 || ipring>npix-1 ) {
|
---|
| 268 | cout << "ipring out of range" << endl;
|
---|
| 269 | exit(0);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | nl2 = 2*nside;
|
---|
| 273 | nl4 = 4*nside;
|
---|
| 274 | npix = 12*nside*nside;// ! total number of points
|
---|
| 275 | ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
|
---|
| 276 | ipring1 = ipring + 1;
|
---|
| 277 |
|
---|
| 278 | //c finds the ring number, the position of the ring and the face number
|
---|
| 279 | if( ipring1<=ncap ) {//then
|
---|
| 280 |
|
---|
| 281 | hip = ipring1/2.;
|
---|
| 282 | fihip = floor ( hip );
|
---|
| 283 | irn = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
|
---|
| 284 | iphi = ipring1 - 2*irn*(irn - 1);
|
---|
| 285 |
|
---|
| 286 | kshift = 0;
|
---|
| 287 | nr = irn ;// ! 1/4 of the number of points on the current ring
|
---|
| 288 | face_num = (iphi-1) / irn;// ! in {0,3}
|
---|
| 289 | }
|
---|
| 290 | else if( ipring1<=nl2*(5*nside+1) ) {//then
|
---|
| 291 |
|
---|
| 292 | ip = ipring1 - ncap - 1;
|
---|
| 293 | irn = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
|
---|
| 294 | iphi = (int)fmod(ip,nl4) + 1;
|
---|
| 295 |
|
---|
| 296 | kshift = (int)fmod(irn+nside,2);// ! 1 if irn+nside is odd, 0 otherwise
|
---|
| 297 | nr = nside;
|
---|
| 298 | ire = irn - nside + 1;// ! in {1, 2*nside +1}
|
---|
| 299 | irm = nl2 + 2 - ire;
|
---|
| 300 | ifm = (iphi - ire/2 + nside -1) / nside;// ! face boundary
|
---|
| 301 | ifp = (iphi - irm/2 + nside -1) / nside;
|
---|
| 302 | if( ifp==ifm ) {//then ! faces 4 to 7
|
---|
| 303 | face_num = (int)fmod(ifp,4) + 4;
|
---|
| 304 | }
|
---|
| 305 | else if( ifp + 1==ifm ) {//then ! (half-)faces 0 to 3
|
---|
| 306 | face_num = ifp;
|
---|
| 307 | }
|
---|
| 308 | else if( ifp - 1==ifm ) {//then ! (half-)faces 8 to 11
|
---|
| 309 | face_num = ifp + 7;
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | else {
|
---|
| 313 |
|
---|
| 314 | ip = npix - ipring1 + 1;
|
---|
| 315 | hip = ip/2.;
|
---|
| 316 | fihip = floor ( hip );
|
---|
| 317 | irs = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
|
---|
| 318 | iphi = 4*irs + 1 - (ip - 2*irs*(irs-1));
|
---|
| 319 |
|
---|
| 320 | kshift = 0;
|
---|
| 321 | nr = irs;
|
---|
| 322 | irn = nl4 - irs;
|
---|
| 323 | face_num = (iphi-1) / irs + 8;// ! in {8,11}
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | //c finds the (x,y) on the face
|
---|
| 327 | irt = irn - jrll[face_num]*nside + 1;// ! in {-nside+1,0}
|
---|
| 328 | ipt = 2*iphi - jpll[face_num]*nr - kshift - 1;// ! in {-nside+1,nside-1}
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | if( ipt>=nl2 ) ipt = ipt - 8*nside;// ! for the face #4
|
---|
| 332 |
|
---|
| 333 | ix = (ipt - irt ) / 2;
|
---|
| 334 | iy = -(ipt + irt ) / 2;
|
---|
| 335 |
|
---|
| 336 | ix_low = (int)fmod(ix,128);
|
---|
| 337 | ix_hi = ix/128;
|
---|
| 338 | iy_low = (int)fmod(iy,128);
|
---|
| 339 | iy_hi = iy/128;
|
---|
| 340 | ipf=(PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
|
---|
| 341 |
|
---|
| 342 | return (ipf + face_num* nside *nside);// ! in {0, 12*nside**2 - 1}
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[1196] | 345 | int_4 HEALPix::ang2pix_ring(int_4 nside, double theta, double phi)
|
---|
[1195] | 346 | {
|
---|
| 347 | /*
|
---|
| 348 | ==================================================
|
---|
| 349 | c gives the pixel number ipix (RING)
|
---|
| 350 | c corresponding to angles theta and phi
|
---|
| 351 | c==================================================
|
---|
| 352 | */
|
---|
| 353 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 354 | // (16/12/98)
|
---|
| 355 |
|
---|
| 356 | int nl2, nl4, ncap, npix, jp, jm, ipix1;
|
---|
| 357 | double z, za, tt, tp, tmp;
|
---|
| 358 | int ir, ip, kshift;
|
---|
| 359 |
|
---|
| 360 | double piover2(Pi/2.);
|
---|
| 361 | double twopi(2.*Pi);
|
---|
| 362 | double z0(2./3.);
|
---|
| 363 | int ns_max(8192);
|
---|
| 364 |
|
---|
| 365 | if( nside<1 || nside>ns_max ) {
|
---|
| 366 | cout << "nside out of range" << endl;
|
---|
| 367 | exit(0);
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | if( theta<0. || theta>Pi) {
|
---|
| 371 | cout << "theta out of range" << endl;
|
---|
| 372 | exit(0);
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | z = cos(theta);
|
---|
| 376 | za = fabs(z);
|
---|
| 377 | if( phi >= twopi) phi = phi - twopi;
|
---|
| 378 | if (phi < 0.) phi = phi + twopi;
|
---|
| 379 | tt = phi / piover2;// ! in [0,4)
|
---|
| 380 |
|
---|
| 381 | nl2 = 2*nside;
|
---|
| 382 | nl4 = 4*nside;
|
---|
| 383 | ncap = nl2*(nside-1);// ! number of pixels in the north polar cap
|
---|
| 384 | npix = 12*nside*nside;
|
---|
| 385 |
|
---|
| 386 | if( za <= z0 ) {
|
---|
| 387 |
|
---|
| 388 | jp = (int)floor(nside*(0.5 + tt - z*0.75));// ! index of ascending edge line
|
---|
| 389 | jm = (int)floor(nside*(0.5 + tt + z*0.75));// ! index of descending edge line
|
---|
| 390 |
|
---|
| 391 | ir = nside + 1 + jp - jm;// ! in {1,2n+1} (ring number counted from z=2/3)
|
---|
| 392 | kshift = 0;
|
---|
| 393 | if (fmod(ir,2)==0.) kshift = 1;// ! kshift=1 if ir even, 0 otherwise
|
---|
| 394 |
|
---|
| 395 | ip = (int)floor( ( jp+jm - nside + kshift + 1 ) / 2 ) + 1;// ! in {1,4n}
|
---|
| 396 | if( ip>nl4 ) ip = ip - nl4;
|
---|
| 397 |
|
---|
| 398 | ipix1 = ncap + nl4*(ir-1) + ip ;
|
---|
| 399 | }
|
---|
| 400 | else {
|
---|
| 401 |
|
---|
| 402 | tp = tt - floor(tt);// !MOD(tt,1.d0)
|
---|
| 403 | tmp = sqrt( 3.*(1. - za) );
|
---|
| 404 |
|
---|
| 405 | jp = (int)floor( nside * tp * tmp );// ! increasing edge line index
|
---|
| 406 | jm = (int)floor( nside * (1. - tp) * tmp );// ! decreasing edge line index
|
---|
| 407 |
|
---|
| 408 | ir = jp + jm + 1;// ! ring number counted from the closest pole
|
---|
| 409 | ip = (int)floor( tt * ir ) + 1;// ! in {1,4*ir}
|
---|
| 410 | if( ip>4*ir ) ip = ip - 4*ir;
|
---|
| 411 |
|
---|
| 412 | ipix1 = 2*ir*(ir-1) + ip;
|
---|
| 413 | if( z<=0. ) {
|
---|
| 414 | ipix1 = npix - 2*ir*(ir+1) + ip;
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | return (ipix1 - 1);// ! in {0, npix-1}
|
---|
| 418 | }
|
---|
| 419 |
|
---|
[1196] | 420 | int_4 HEALPix::ang2pix_nest(int_4 nside, double theta, double phi)
|
---|
[1195] | 421 | {
|
---|
| 422 | /*
|
---|
| 423 | ==================================================
|
---|
| 424 | subroutine ang2pix_nest(nside, theta, phi, ipix)
|
---|
| 425 | ==================================================
|
---|
| 426 | c gives the pixel number ipix (NESTED)
|
---|
| 427 | c corresponding to angles theta and phi
|
---|
| 428 | c
|
---|
| 429 | c the computation is made to the highest resolution available (nside=8192)
|
---|
| 430 | c and then degraded to that required (by integer division)
|
---|
| 431 | c this doesn't cost more, and it makes sure
|
---|
| 432 | c that the treatement of round-off will be consistent
|
---|
| 433 | c for every resolution
|
---|
| 434 | ==================================================
|
---|
| 435 | */
|
---|
| 436 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 437 | // (16/12/98)
|
---|
| 438 |
|
---|
| 439 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 440 |
|
---|
| 441 | double z, za, z0, tt, tp, tmp;
|
---|
| 442 | int face_num,jp,jm;
|
---|
| 443 | int ifp, ifm;
|
---|
| 444 | int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf, ntt;
|
---|
| 445 | double piover2(Pi/2.), twopi(2.*Pi);
|
---|
| 446 | int ns_max(8192);
|
---|
| 447 |
|
---|
| 448 | if( nside<1 || nside>ns_max ) {
|
---|
| 449 | cout << "nside out of range" << endl;
|
---|
| 450 | exit(0);
|
---|
| 451 | }
|
---|
| 452 | if( theta<0 || theta>Pi ) {
|
---|
| 453 | cout << "theta out of range" << endl;
|
---|
| 454 | exit(0);
|
---|
| 455 | }
|
---|
| 456 | z = cos(theta);
|
---|
| 457 | za = fabs(z);
|
---|
| 458 | z0 = 2./3.;
|
---|
| 459 | if( phi>=twopi ) phi = phi - twopi;
|
---|
| 460 | if( phi<0. ) phi = phi + twopi;
|
---|
| 461 | tt = phi / piover2;// ! in [0,4[
|
---|
| 462 | if( za<=z0 ) { // then ! equatorial region
|
---|
| 463 |
|
---|
| 464 | //(the index of edge lines increase when the longitude=phi goes up)
|
---|
| 465 | jp = (int)floor(ns_max*(0.5 + tt - z*0.75));// ! ascending edge line index
|
---|
| 466 | jm = (int)floor(ns_max*(0.5 + tt + z*0.75));// ! descending edge line index
|
---|
| 467 |
|
---|
| 468 | //c finds the face
|
---|
| 469 | ifp = jp / ns_max;// ! in {0,4}
|
---|
| 470 | ifm = jm / ns_max;
|
---|
| 471 | if( ifp==ifm ) face_num = (int)fmod(ifp,4) + 4; //then ! faces 4 to 7
|
---|
| 472 | else if( ifp<ifm ) face_num = (int)fmod(ifp,4); // (half-)faces 0 to 3
|
---|
| 473 | else face_num = (int)fmod(ifm,4) + 8;//! (half-)faces 8 to 11
|
---|
| 474 |
|
---|
| 475 | ix = (int)fmod(jm, ns_max);
|
---|
| 476 | iy = ns_max - (int)fmod(jp, ns_max) - 1;
|
---|
| 477 | }
|
---|
| 478 | else { //! polar region, za > 2/3
|
---|
| 479 |
|
---|
| 480 | ntt = (int)floor(tt);
|
---|
| 481 | if( ntt>=4 ) ntt = 3;
|
---|
| 482 | tp = tt - ntt;
|
---|
| 483 | tmp = sqrt( 3.*(1. - za) );// ! in ]0,1]
|
---|
| 484 |
|
---|
| 485 | //(the index of edge lines increase when distance from the closest pole goes up)
|
---|
| 486 | jp = (int)floor(ns_max*tp*tmp); // ! line going toward the pole as phi increases
|
---|
| 487 | jm = (int)floor(ns_max*(1.-tp)*tmp); // ! that one goes away of the closest pole
|
---|
| 488 | jp = (int)min(ns_max-1, jp);// ! for points too close to the boundary
|
---|
| 489 | jm = (int)min(ns_max-1, jm);
|
---|
| 490 |
|
---|
| 491 | // finds the face and pixel's (x,y)
|
---|
| 492 | if( z>=0 ) {
|
---|
| 493 | face_num = ntt;// ! in {0,3}
|
---|
| 494 | ix = ns_max - jm - 1;
|
---|
| 495 | iy = ns_max - jp - 1;
|
---|
| 496 | }
|
---|
| 497 | else {
|
---|
| 498 | face_num = ntt + 8;// ! in {8,11}
|
---|
| 499 | ix = jp;
|
---|
| 500 | iy = jm;
|
---|
| 501 | }
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | ix_low = (int)fmod(ix,128);
|
---|
| 505 | ix_hi = ix/128;
|
---|
| 506 | iy_low = (int)fmod(iy,128);
|
---|
| 507 | iy_hi = iy/128;
|
---|
| 508 | ipf= (PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
|
---|
| 509 | // ipf = ipf / pow(ns_max/nside,2.);// ! in {0, nside**2 - 1}
|
---|
| 510 | // return ( ipf + face_num*pow(nside,2));// ! in {0, 12*nside**2 - 1}
|
---|
| 511 | // $CHECK$ Reza 25/10/99 , pow remplace par *
|
---|
| 512 | ipf = ipf / ((ns_max/nside)*(ns_max/nside));
|
---|
| 513 | return (ipf + face_num*nside*nside);
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[1196] | 516 | void HEALPix::pix2ang_ring(int_4 nside,int_4 ipix,double& theta,double& phi)
|
---|
| 517 | {
|
---|
[1195] | 518 | /*
|
---|
| 519 | ===================================================
|
---|
| 520 | c gives theta and phi corresponding to pixel ipix (RING)
|
---|
| 521 | c for a parameter nside
|
---|
| 522 | ===================================================
|
---|
| 523 | */
|
---|
| 524 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 525 | // (16/12/98)
|
---|
| 526 |
|
---|
| 527 | int nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
|
---|
| 528 | double fact1, fact2, fodd, hip, fihip;
|
---|
| 529 |
|
---|
| 530 | int ns_max(8192);
|
---|
| 531 |
|
---|
| 532 | if( nside<1 || nside>ns_max ) {
|
---|
| 533 | cout << "nside out of range" << endl;
|
---|
| 534 | exit(0);
|
---|
| 535 | }
|
---|
| 536 | npix = 12*nside*nside; // ! total number of points
|
---|
| 537 | if( ipix<0 || ipix>npix-1 ) {
|
---|
| 538 | cout << "ipix out of range" << endl;
|
---|
| 539 | exit(0);
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | ipix1 = ipix + 1; // in {1, npix}
|
---|
| 543 | nl2 = 2*nside;
|
---|
| 544 | nl4 = 4*nside;
|
---|
| 545 | ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
|
---|
| 546 | fact1 = 1.5*nside;
|
---|
| 547 | fact2 = 3.0*nside*nside;
|
---|
| 548 |
|
---|
| 549 | if( ipix1 <= ncap ) { //! North Polar cap -------------
|
---|
| 550 |
|
---|
| 551 | hip = ipix1/2.;
|
---|
| 552 | fihip = floor(hip);
|
---|
| 553 | iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
|
---|
| 554 | iphi = ipix1 - 2*iring*(iring - 1);
|
---|
| 555 |
|
---|
| 556 | theta = acos( 1. - iring*iring / fact2 );
|
---|
| 557 | phi = ((double)iphi - 0.5) * Pi/(2.*iring);
|
---|
| 558 | // cout << theta << " " << phi << endl;
|
---|
| 559 | }
|
---|
| 560 | else if( ipix1 <= nl2*(5*nside+1) ) {//then ! Equatorial region ------
|
---|
| 561 |
|
---|
| 562 | ip = ipix1 - ncap - 1;
|
---|
| 563 | iring = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
|
---|
| 564 | iphi = ip%nl4 + 1;
|
---|
| 565 |
|
---|
| 566 | fodd = 0.5 * (1 + (iring+nside)%2 );// ! 1 if iring+nside is odd, 1/2 otherwise
|
---|
| 567 | theta = acos( (nl2 - iring) / fact1 );
|
---|
| 568 | phi = ((double)iphi - fodd) * Pi /(2.*nside);
|
---|
| 569 | }
|
---|
| 570 | else {//! South Polar cap -----------------------------------
|
---|
| 571 |
|
---|
| 572 | ip = npix - ipix1 + 1;
|
---|
| 573 | hip = ip/2.;
|
---|
| 574 | fihip = floor(hip);
|
---|
| 575 | iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
|
---|
| 576 | iphi = (int)(4.*iring + 1 - (ip - 2.*iring*(iring-1)));
|
---|
| 577 |
|
---|
| 578 | theta = acos( -1. + iring*iring / fact2 );
|
---|
| 579 | phi = ((double)iphi - 0.5) * Pi/(2.*iring);
|
---|
| 580 | // cout << theta << " " << phi << endl;
|
---|
| 581 | }
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[1196] | 584 | void HEALPix::pix2ang_nest(int_4 nside,int_4 ipix,double& theta,double& phi)
|
---|
| 585 | {
|
---|
[1195] | 586 | /*
|
---|
| 587 | ==================================================
|
---|
| 588 | subroutine pix2ang_nest(nside, ipix, theta, phi)
|
---|
| 589 | ==================================================
|
---|
| 590 | c gives theta and phi corresponding to pixel ipix (NESTED)
|
---|
| 591 | c for a parameter nside
|
---|
| 592 | ==================================================
|
---|
| 593 | */
|
---|
| 594 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 595 | // (16/12/98)
|
---|
| 596 |
|
---|
| 597 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 598 |
|
---|
| 599 | int npix, npface, face_num;
|
---|
| 600 | int ipf, ip_low, ip_trunc, ip_med, ip_hi;
|
---|
| 601 | int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
|
---|
| 602 | double z, fn, fact1, fact2;
|
---|
| 603 | double piover2(Pi/2.);
|
---|
| 604 | int ns_max(8192);
|
---|
| 605 |
|
---|
| 606 | // ! coordinate of the lowest corner of each face
|
---|
| 607 | int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};//! in unit of nside
|
---|
| 608 | int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};// ! in unit of nside/2
|
---|
| 609 |
|
---|
| 610 | if( nside<1 || nside>ns_max ) {
|
---|
| 611 | cout << "nside out of range" << endl;
|
---|
| 612 | exit(0);
|
---|
| 613 | }
|
---|
| 614 | npix = 12 * nside*nside;
|
---|
| 615 | if( ipix<0 || ipix>npix-1 ) {
|
---|
| 616 | cout << "ipix out of range" << endl;
|
---|
| 617 | exit(0);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | fn = 1.*nside;
|
---|
| 621 | fact1 = 1./(3.*fn*fn);
|
---|
| 622 | fact2 = 2./(3.*fn);
|
---|
| 623 | nl4 = 4*nside;
|
---|
| 624 |
|
---|
| 625 | //c finds the face, and the number in the face
|
---|
| 626 | npface = nside*nside;
|
---|
| 627 |
|
---|
| 628 | face_num = ipix/npface;// ! face number in {0,11}
|
---|
| 629 | ipf = (int)fmod(ipix,npface);// ! pixel number in the face {0,npface-1}
|
---|
| 630 |
|
---|
| 631 | //c finds the x,y on the face (starting from the lowest corner)
|
---|
| 632 | //c from the pixel number
|
---|
| 633 | ip_low = (int)fmod(ipf,1024);// ! content of the last 10 bits
|
---|
| 634 | ip_trunc = ipf/1024 ;// ! truncation of the last 10 bits
|
---|
| 635 | ip_med = (int)fmod(ip_trunc,1024);// ! content of the next 10 bits
|
---|
| 636 | ip_hi = ip_trunc/1024 ;//! content of the high weight 10 bits
|
---|
| 637 |
|
---|
| 638 | ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
|
---|
| 639 | iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
|
---|
| 640 |
|
---|
| 641 | //c transforms this in (horizontal, vertical) coordinates
|
---|
| 642 | jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
|
---|
| 643 | jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
|
---|
| 644 |
|
---|
| 645 | //c computes the z coordinate on the sphere
|
---|
| 646 | // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
|
---|
| 647 | jr = jrll[face_num]*nside - jrt - 1;
|
---|
| 648 | nr = nside;// ! equatorial region (the most frequent)
|
---|
| 649 | z = (2*nside-jr)*fact2;
|
---|
| 650 | kshift = (int)fmod(jr - nside, 2);
|
---|
| 651 | if( jr<nside ) { //then ! north pole region
|
---|
| 652 | nr = jr;
|
---|
| 653 | z = 1. - nr*nr*fact1;
|
---|
| 654 | kshift = 0;
|
---|
| 655 | }
|
---|
| 656 | else {
|
---|
| 657 | if( jr>3*nside ) {// then ! south pole region
|
---|
| 658 | nr = nl4 - jr;
|
---|
| 659 | z = - 1. + nr*nr*fact1;
|
---|
| 660 | kshift = 0;
|
---|
| 661 | }
|
---|
| 662 | }
|
---|
| 663 | theta = acos(z);
|
---|
| 664 |
|
---|
| 665 | //c computes the phi coordinate on the sphere, in [0,2Pi]
|
---|
| 666 | // jp = (jpll[face_num+1]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
|
---|
| 667 | jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;
|
---|
| 668 | if( jp>nl4 ) jp = jp - nl4;
|
---|
| 669 | if( jp<1 ) jp = jp + nl4;
|
---|
| 670 | phi = (jp - (kshift+1)*0.5) * (piover2 / nr);
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[1196] | 673 |
|
---|