[1195] | 1 | // utilitaires de pixelisation HEALPix
|
---|
| 2 | #include "HEALPixUtils.h"
|
---|
[2322] | 3 | #include <iostream>
|
---|
[1195] | 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 | {
|
---|
[2082] | 144 | ID = J%2; //RzModFloor (int)fmod(J,2);
|
---|
[1195] | 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 ) {
|
---|
[1954] | 177 | char buff[64];
|
---|
| 178 | sprintf(buff,"HEALPix::nest2ring nside(=%d) out of range ", nside);
|
---|
| 179 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 180 | }
|
---|
| 181 | npix = 12 * nside* nside;
|
---|
| 182 | if( ipnest<0 || ipnest>npix-1 ) {
|
---|
[1954] | 183 | char buff[64];
|
---|
| 184 | sprintf(buff,"HEALPix::nest2ring ipnest(=%d) out of range ", ipnest);
|
---|
| 185 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 186 | }
|
---|
| 187 |
|
---|
| 188 | ncap = 2* nside*( nside-1);// ! number of points in the North Polar cap
|
---|
| 189 | nl4 = 4* nside;
|
---|
| 190 |
|
---|
| 191 | //c finds the face, and the number in the face
|
---|
| 192 | npface = nside* nside;
|
---|
| 193 | //cccccc ip = ipnest - 1 ! in {0,npix-1}
|
---|
| 194 |
|
---|
| 195 | face_num = ipnest/npface;// ! face number in {0,11}
|
---|
| 196 | ipf =ipnest%npface;// ! pixel number in the face {0,npface-1}
|
---|
| 197 | //c finds the x,y on the face (starting from the lowest corner)
|
---|
| 198 | //c from the pixel number
|
---|
| 199 | ip_low=ipf%1024; // ! content of the last 10 bits
|
---|
| 200 | ip_trunc = ipf/1024; // ! truncation of the last 10 bits
|
---|
| 201 | ip_med=ip_trunc%1024; // ! content of the next 10 bits
|
---|
| 202 | ip_hi = ip_trunc/1024;// ! content of the high weight 10 bits
|
---|
| 203 |
|
---|
| 204 | ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
|
---|
| 205 | iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
|
---|
| 206 |
|
---|
| 207 | //c transforms this in (horizontal, vertical) coordinates
|
---|
| 208 | jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
|
---|
| 209 | jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
|
---|
| 210 |
|
---|
| 211 | //c computes the z coordinate on the sphere
|
---|
| 212 | // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
|
---|
| 213 | jr = jrll[face_num]*nside - jrt - 1;
|
---|
| 214 | nr = nside;// ! equatorial region (the most frequent)
|
---|
| 215 | n_before = ncap + nl4 * (jr - nside);
|
---|
| 216 | kshift=(jr - nside)%2;
|
---|
| 217 | if( jr<nside ) {//then ! north pole region
|
---|
| 218 | nr = jr;
|
---|
| 219 | n_before = 2 * nr * (nr - 1);
|
---|
| 220 | kshift = 0;
|
---|
| 221 | }
|
---|
| 222 | else if( jr>3*nside ) {//then ! south pole region
|
---|
| 223 | nr = nl4 - jr;
|
---|
| 224 | n_before = npix - 2 * (nr + 1) * nr;
|
---|
| 225 | kshift = 0;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | //c computes the phi coordinate on the sphere, in [0,2Pi]
|
---|
| 229 | jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
|
---|
| 230 |
|
---|
| 231 | if( jp>nl4 ) jp = jp - nl4;
|
---|
| 232 | if( jp<1 ) jp = jp + nl4;
|
---|
| 233 |
|
---|
[2082] | 234 | //RzDel int aux=n_before + jp - 1;
|
---|
[1195] | 235 | return (n_before + jp - 1);// ! in {0, npix-1}
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 |
|
---|
[1196] | 239 | int_4 HEALPix::ring2nest(int_4 nside, int_4 ipring)
|
---|
[1195] | 240 | {
|
---|
| 241 | /*
|
---|
| 242 | ==================================================
|
---|
| 243 | subroutine ring2nest(nside, ipring, ipnest)
|
---|
| 244 | ==================================================
|
---|
| 245 | c conversion from RING to NESTED pixel number
|
---|
| 246 | ==================================================
|
---|
| 247 | */
|
---|
| 248 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 249 | // (16/12/98)
|
---|
| 250 |
|
---|
| 251 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 252 |
|
---|
| 253 | double fihip, hip;
|
---|
| 254 | int npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1;
|
---|
| 255 | int kshift, face_num, nr;
|
---|
| 256 | int irn, ire, irm, irs, irt, ifm , ifp;
|
---|
| 257 | int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf;
|
---|
| 258 | int ns_max(8192);
|
---|
| 259 |
|
---|
| 260 | // coordinate of the lowest corner of each face
|
---|
| 261 | int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};// ! in unit of nside
|
---|
| 262 | int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};//! in unit of nside/2
|
---|
| 263 |
|
---|
| 264 | if( nside<1 || nside>ns_max ) {
|
---|
[1954] | 265 | char buff[64];
|
---|
| 266 | sprintf(buff,"HEALPix::ring2nest nside(=%d) out of range ", nside);
|
---|
| 267 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 268 | }
|
---|
| 269 | npix = 12 * nside*nside;
|
---|
| 270 | if( ipring<0 || ipring>npix-1 ) {
|
---|
[1954] | 271 | char buff[64];
|
---|
| 272 | sprintf(buff,"HEALPix::ring2nest ipring(=%d) out of range ", ipring);
|
---|
| 273 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 274 | }
|
---|
| 275 |
|
---|
| 276 | nl2 = 2*nside;
|
---|
| 277 | nl4 = 4*nside;
|
---|
| 278 | npix = 12*nside*nside;// ! total number of points
|
---|
| 279 | ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
|
---|
| 280 | ipring1 = ipring + 1;
|
---|
| 281 |
|
---|
| 282 | //c finds the ring number, the position of the ring and the face number
|
---|
| 283 | if( ipring1<=ncap ) {//then
|
---|
| 284 |
|
---|
| 285 | hip = ipring1/2.;
|
---|
| 286 | fihip = floor ( hip );
|
---|
| 287 | irn = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
|
---|
| 288 | iphi = ipring1 - 2*irn*(irn - 1);
|
---|
| 289 |
|
---|
| 290 | kshift = 0;
|
---|
| 291 | nr = irn ;// ! 1/4 of the number of points on the current ring
|
---|
| 292 | face_num = (iphi-1) / irn;// ! in {0,3}
|
---|
| 293 | }
|
---|
| 294 | else if( ipring1<=nl2*(5*nside+1) ) {//then
|
---|
| 295 |
|
---|
| 296 | ip = ipring1 - ncap - 1;
|
---|
[2082] | 297 | irn = ip / nl4 + nside; //RzModFloor (int)floor( ip / nl4 ) + nside; ! counted from North pole
|
---|
| 298 | iphi = ip%nl4 + 1; //RzModFloor (int)fmod(ip,nl4) + 1;
|
---|
[1195] | 299 |
|
---|
[2082] | 300 | kshift = (irn+nside)%2; //RzModFloor (int)fmod(irn+nside,2); ! 1 if irn+nside is odd, 0 otherwise
|
---|
[1195] | 301 | nr = nside;
|
---|
| 302 | ire = irn - nside + 1;// ! in {1, 2*nside +1}
|
---|
| 303 | irm = nl2 + 2 - ire;
|
---|
| 304 | ifm = (iphi - ire/2 + nside -1) / nside;// ! face boundary
|
---|
| 305 | ifp = (iphi - irm/2 + nside -1) / nside;
|
---|
| 306 | if( ifp==ifm ) {//then ! faces 4 to 7
|
---|
[2082] | 307 | face_num = ifp%4+4; // RzModFloor (int)fmod(ifp,4) + 4;
|
---|
[1195] | 308 | }
|
---|
| 309 | else if( ifp + 1==ifm ) {//then ! (half-)faces 0 to 3
|
---|
| 310 | face_num = ifp;
|
---|
| 311 | }
|
---|
| 312 | else if( ifp - 1==ifm ) {//then ! (half-)faces 8 to 11
|
---|
| 313 | face_num = ifp + 7;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | else {
|
---|
| 317 |
|
---|
| 318 | ip = npix - ipring1 + 1;
|
---|
| 319 | hip = ip/2.;
|
---|
| 320 | fihip = floor ( hip );
|
---|
| 321 | irs = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
|
---|
| 322 | iphi = 4*irs + 1 - (ip - 2*irs*(irs-1));
|
---|
| 323 |
|
---|
| 324 | kshift = 0;
|
---|
| 325 | nr = irs;
|
---|
| 326 | irn = nl4 - irs;
|
---|
| 327 | face_num = (iphi-1) / irs + 8;// ! in {8,11}
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | //c finds the (x,y) on the face
|
---|
| 331 | irt = irn - jrll[face_num]*nside + 1;// ! in {-nside+1,0}
|
---|
| 332 | ipt = 2*iphi - jpll[face_num]*nr - kshift - 1;// ! in {-nside+1,nside-1}
|
---|
| 333 |
|
---|
| 334 |
|
---|
| 335 | if( ipt>=nl2 ) ipt = ipt - 8*nside;// ! for the face #4
|
---|
| 336 |
|
---|
| 337 | ix = (ipt - irt ) / 2;
|
---|
| 338 | iy = -(ipt + irt ) / 2;
|
---|
| 339 |
|
---|
[2082] | 340 | ix_low = ix%128; //RzModFloor (int)fmod(ix,128);
|
---|
[1195] | 341 | ix_hi = ix/128;
|
---|
[2082] | 342 | iy_low = iy%128; //RzModFloor (int)fmod(iy,128);
|
---|
[1195] | 343 | iy_hi = iy/128;
|
---|
| 344 | ipf=(PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
|
---|
| 345 |
|
---|
| 346 | return (ipf + face_num* nside *nside);// ! in {0, 12*nside**2 - 1}
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[1196] | 349 | int_4 HEALPix::ang2pix_ring(int_4 nside, double theta, double phi)
|
---|
[1195] | 350 | {
|
---|
| 351 | /*
|
---|
| 352 | ==================================================
|
---|
| 353 | c gives the pixel number ipix (RING)
|
---|
| 354 | c corresponding to angles theta and phi
|
---|
| 355 | c==================================================
|
---|
| 356 | */
|
---|
| 357 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 358 | // (16/12/98)
|
---|
| 359 |
|
---|
| 360 | int nl2, nl4, ncap, npix, jp, jm, ipix1;
|
---|
| 361 | double z, za, tt, tp, tmp;
|
---|
| 362 | int ir, ip, kshift;
|
---|
| 363 |
|
---|
| 364 | double piover2(Pi/2.);
|
---|
| 365 | double twopi(2.*Pi);
|
---|
| 366 | double z0(2./3.);
|
---|
| 367 | int ns_max(8192);
|
---|
| 368 |
|
---|
| 369 | if( nside<1 || nside>ns_max ) {
|
---|
[1954] | 370 | char buff[64];
|
---|
| 371 | sprintf(buff,"HEALPix::ang2pix_ring nside(=%d) out of range ", nside);
|
---|
| 372 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 373 | }
|
---|
| 374 |
|
---|
| 375 | if( theta<0. || theta>Pi) {
|
---|
[1954] | 376 | char buff[64];
|
---|
| 377 | sprintf(buff,"HEALPix::ang2pix_ring theta(=%g) out of range ", theta);
|
---|
| 378 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 379 | }
|
---|
| 380 |
|
---|
| 381 | z = cos(theta);
|
---|
| 382 | za = fabs(z);
|
---|
| 383 | if( phi >= twopi) phi = phi - twopi;
|
---|
| 384 | if (phi < 0.) phi = phi + twopi;
|
---|
| 385 | tt = phi / piover2;// ! in [0,4)
|
---|
| 386 |
|
---|
| 387 | nl2 = 2*nside;
|
---|
| 388 | nl4 = 4*nside;
|
---|
| 389 | ncap = nl2*(nside-1);// ! number of pixels in the north polar cap
|
---|
| 390 | npix = 12*nside*nside;
|
---|
| 391 |
|
---|
| 392 | if( za <= z0 ) {
|
---|
| 393 |
|
---|
| 394 | jp = (int)floor(nside*(0.5 + tt - z*0.75));// ! index of ascending edge line
|
---|
| 395 | jm = (int)floor(nside*(0.5 + tt + z*0.75));// ! index of descending edge line
|
---|
| 396 |
|
---|
| 397 | ir = nside + 1 + jp - jm;// ! in {1,2n+1} (ring number counted from z=2/3)
|
---|
| 398 | kshift = 0;
|
---|
[2082] | 399 | //RzModFloor if (fmod(ir,2)==0.) kshift = 1; ! kshift=1 if ir even, 0 otherwise
|
---|
| 400 | if ((ir%2)==0) kshift = 1;// ! kshift=1 if ir even, 0 otherwise
|
---|
[1195] | 401 |
|
---|
[2082] | 402 | //RzModFloor ip = (int)floor( ( jp+jm - nside + kshift + 1 ) / 2 ) + 1; ! in {1,4n}
|
---|
| 403 | ip = ( jp+jm - nside + kshift + 1 )/2 + 1;
|
---|
[1195] | 404 | if( ip>nl4 ) ip = ip - nl4;
|
---|
| 405 |
|
---|
| 406 | ipix1 = ncap + nl4*(ir-1) + ip ;
|
---|
| 407 | }
|
---|
| 408 | else {
|
---|
| 409 |
|
---|
| 410 | tp = tt - floor(tt);// !MOD(tt,1.d0)
|
---|
| 411 | tmp = sqrt( 3.*(1. - za) );
|
---|
| 412 |
|
---|
| 413 | jp = (int)floor( nside * tp * tmp );// ! increasing edge line index
|
---|
| 414 | jm = (int)floor( nside * (1. - tp) * tmp );// ! decreasing edge line index
|
---|
| 415 |
|
---|
| 416 | ir = jp + jm + 1;// ! ring number counted from the closest pole
|
---|
| 417 | ip = (int)floor( tt * ir ) + 1;// ! in {1,4*ir}
|
---|
| 418 | if( ip>4*ir ) ip = ip - 4*ir;
|
---|
| 419 |
|
---|
| 420 | ipix1 = 2*ir*(ir-1) + ip;
|
---|
| 421 | if( z<=0. ) {
|
---|
| 422 | ipix1 = npix - 2*ir*(ir+1) + ip;
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | return (ipix1 - 1);// ! in {0, npix-1}
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[1196] | 428 | int_4 HEALPix::ang2pix_nest(int_4 nside, double theta, double phi)
|
---|
[1195] | 429 | {
|
---|
| 430 | /*
|
---|
| 431 | ==================================================
|
---|
| 432 | subroutine ang2pix_nest(nside, theta, phi, ipix)
|
---|
| 433 | ==================================================
|
---|
| 434 | c gives the pixel number ipix (NESTED)
|
---|
| 435 | c corresponding to angles theta and phi
|
---|
| 436 | c
|
---|
| 437 | c the computation is made to the highest resolution available (nside=8192)
|
---|
| 438 | c and then degraded to that required (by integer division)
|
---|
| 439 | c this doesn't cost more, and it makes sure
|
---|
| 440 | c that the treatement of round-off will be consistent
|
---|
| 441 | c for every resolution
|
---|
| 442 | ==================================================
|
---|
| 443 | */
|
---|
| 444 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 445 | // (16/12/98)
|
---|
| 446 |
|
---|
| 447 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 448 |
|
---|
| 449 | double z, za, z0, tt, tp, tmp;
|
---|
| 450 | int face_num,jp,jm;
|
---|
| 451 | int ifp, ifm;
|
---|
| 452 | int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf, ntt;
|
---|
| 453 | double piover2(Pi/2.), twopi(2.*Pi);
|
---|
| 454 | int ns_max(8192);
|
---|
| 455 |
|
---|
| 456 | if( nside<1 || nside>ns_max ) {
|
---|
[1954] | 457 | char buff[64];
|
---|
| 458 | sprintf(buff,"HEALPix:::ang2pix_nest nside(=%d) out of range ", nside);
|
---|
| 459 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 460 | }
|
---|
| 461 | if( theta<0 || theta>Pi ) {
|
---|
[1954] | 462 | char buff[64];
|
---|
| 463 | sprintf(buff,"HEALPix:::ang2pix_nest theta(=%g) out of range ", theta);
|
---|
| 464 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 465 | }
|
---|
| 466 | z = cos(theta);
|
---|
| 467 | za = fabs(z);
|
---|
| 468 | z0 = 2./3.;
|
---|
| 469 | if( phi>=twopi ) phi = phi - twopi;
|
---|
| 470 | if( phi<0. ) phi = phi + twopi;
|
---|
| 471 | tt = phi / piover2;// ! in [0,4[
|
---|
| 472 | if( za<=z0 ) { // then ! equatorial region
|
---|
| 473 |
|
---|
| 474 | //(the index of edge lines increase when the longitude=phi goes up)
|
---|
| 475 | jp = (int)floor(ns_max*(0.5 + tt - z*0.75));// ! ascending edge line index
|
---|
| 476 | jm = (int)floor(ns_max*(0.5 + tt + z*0.75));// ! descending edge line index
|
---|
| 477 |
|
---|
| 478 | //c finds the face
|
---|
| 479 | ifp = jp / ns_max;// ! in {0,4}
|
---|
| 480 | ifm = jm / ns_max;
|
---|
[2082] | 481 | if( ifp==ifm ) face_num = (ifp%4)+4; //RzModFloor (int)fmod(ifp,4) + 4; then ! faces 4 to 7
|
---|
| 482 | else if( ifp<ifm ) face_num = ifp%4; //RzModFloor (int)fmod(ifp,4); (half-)faces 0 to 3
|
---|
| 483 | else face_num = (ifm%4) + 8; //RzModFloor (int)fmod(ifm,4) + 8;! (half-)faces 8 to 11
|
---|
[1195] | 484 |
|
---|
[2082] | 485 | ix = jm%ns_max; //RzModFloor (int)fmod(jm, ns_max);
|
---|
| 486 | iy = ns_max - (jp%ns_max) - 1;//RzModFloor ns_max - (int)fmod(jp, ns_max) - 1;
|
---|
[1195] | 487 | }
|
---|
| 488 | else { //! polar region, za > 2/3
|
---|
| 489 |
|
---|
| 490 | ntt = (int)floor(tt);
|
---|
| 491 | if( ntt>=4 ) ntt = 3;
|
---|
| 492 | tp = tt - ntt;
|
---|
| 493 | tmp = sqrt( 3.*(1. - za) );// ! in ]0,1]
|
---|
| 494 |
|
---|
| 495 | //(the index of edge lines increase when distance from the closest pole goes up)
|
---|
| 496 | jp = (int)floor(ns_max*tp*tmp); // ! line going toward the pole as phi increases
|
---|
| 497 | jm = (int)floor(ns_max*(1.-tp)*tmp); // ! that one goes away of the closest pole
|
---|
| 498 | jp = (int)min(ns_max-1, jp);// ! for points too close to the boundary
|
---|
| 499 | jm = (int)min(ns_max-1, jm);
|
---|
| 500 |
|
---|
| 501 | // finds the face and pixel's (x,y)
|
---|
| 502 | if( z>=0 ) {
|
---|
| 503 | face_num = ntt;// ! in {0,3}
|
---|
| 504 | ix = ns_max - jm - 1;
|
---|
| 505 | iy = ns_max - jp - 1;
|
---|
| 506 | }
|
---|
| 507 | else {
|
---|
| 508 | face_num = ntt + 8;// ! in {8,11}
|
---|
| 509 | ix = jp;
|
---|
| 510 | iy = jm;
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[2082] | 514 | ix_low = (ix%128); //RzModFloor (int)fmod(ix,128);
|
---|
[1195] | 515 | ix_hi = ix/128;
|
---|
[2082] | 516 | iy_low = (iy%128); //RzModFloor ((int)fmod(iy,128);
|
---|
[1195] | 517 | iy_hi = iy/128;
|
---|
| 518 | ipf= (PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
|
---|
| 519 | // ipf = ipf / pow(ns_max/nside,2.);// ! in {0, nside**2 - 1}
|
---|
| 520 | // return ( ipf + face_num*pow(nside,2));// ! in {0, 12*nside**2 - 1}
|
---|
| 521 | // $CHECK$ Reza 25/10/99 , pow remplace par *
|
---|
| 522 | ipf = ipf / ((ns_max/nside)*(ns_max/nside));
|
---|
| 523 | return (ipf + face_num*nside*nside);
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[1196] | 526 | void HEALPix::pix2ang_ring(int_4 nside,int_4 ipix,double& theta,double& phi)
|
---|
| 527 | {
|
---|
[1195] | 528 | /*
|
---|
| 529 | ===================================================
|
---|
| 530 | c gives theta and phi corresponding to pixel ipix (RING)
|
---|
| 531 | c for a parameter nside
|
---|
| 532 | ===================================================
|
---|
| 533 | */
|
---|
| 534 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 535 | // (16/12/98)
|
---|
| 536 |
|
---|
| 537 | int nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
|
---|
| 538 | double fact1, fact2, fodd, hip, fihip;
|
---|
| 539 |
|
---|
| 540 | int ns_max(8192);
|
---|
| 541 |
|
---|
| 542 | if( nside<1 || nside>ns_max ) {
|
---|
[1954] | 543 | char buff[64];
|
---|
| 544 | sprintf(buff,"HEALPix::pix2ang_ring nside(=%d) out of range ", nside);
|
---|
| 545 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 546 | }
|
---|
| 547 | npix = 12*nside*nside; // ! total number of points
|
---|
| 548 | if( ipix<0 || ipix>npix-1 ) {
|
---|
[1954] | 549 | char buff[64];
|
---|
| 550 | sprintf(buff,"HEALPix::pix2ang_ring ipix(=%d) out of range ", ipix);
|
---|
| 551 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 552 | }
|
---|
| 553 |
|
---|
| 554 | ipix1 = ipix + 1; // in {1, npix}
|
---|
| 555 | nl2 = 2*nside;
|
---|
| 556 | nl4 = 4*nside;
|
---|
| 557 | ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
|
---|
| 558 | fact1 = 1.5*nside;
|
---|
| 559 | fact2 = 3.0*nside*nside;
|
---|
| 560 |
|
---|
| 561 | if( ipix1 <= ncap ) { //! North Polar cap -------------
|
---|
| 562 |
|
---|
| 563 | hip = ipix1/2.;
|
---|
| 564 | fihip = floor(hip);
|
---|
| 565 | iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
|
---|
| 566 | iphi = ipix1 - 2*iring*(iring - 1);
|
---|
| 567 |
|
---|
| 568 | theta = acos( 1. - iring*iring / fact2 );
|
---|
| 569 | phi = ((double)iphi - 0.5) * Pi/(2.*iring);
|
---|
| 570 | // cout << theta << " " << phi << endl;
|
---|
| 571 | }
|
---|
| 572 | else if( ipix1 <= nl2*(5*nside+1) ) {//then ! Equatorial region ------
|
---|
| 573 |
|
---|
| 574 | ip = ipix1 - ncap - 1;
|
---|
[2082] | 575 | iring = ip/nl4 + nside; //RzModFloor (int)floor( ip / nl4 ) + nside; ! counted from North pole
|
---|
[1195] | 576 | iphi = ip%nl4 + 1;
|
---|
| 577 |
|
---|
| 578 | fodd = 0.5 * (1 + (iring+nside)%2 );// ! 1 if iring+nside is odd, 1/2 otherwise
|
---|
| 579 | theta = acos( (nl2 - iring) / fact1 );
|
---|
| 580 | phi = ((double)iphi - fodd) * Pi /(2.*nside);
|
---|
| 581 | }
|
---|
| 582 | else {//! South Polar cap -----------------------------------
|
---|
| 583 |
|
---|
| 584 | ip = npix - ipix1 + 1;
|
---|
| 585 | hip = ip/2.;
|
---|
| 586 | fihip = floor(hip);
|
---|
| 587 | iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
|
---|
| 588 | iphi = (int)(4.*iring + 1 - (ip - 2.*iring*(iring-1)));
|
---|
| 589 |
|
---|
| 590 | theta = acos( -1. + iring*iring / fact2 );
|
---|
| 591 | phi = ((double)iphi - 0.5) * Pi/(2.*iring);
|
---|
| 592 | // cout << theta << " " << phi << endl;
|
---|
| 593 | }
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[1196] | 596 | void HEALPix::pix2ang_nest(int_4 nside,int_4 ipix,double& theta,double& phi)
|
---|
| 597 | {
|
---|
[1195] | 598 | /*
|
---|
| 599 | ==================================================
|
---|
| 600 | subroutine pix2ang_nest(nside, ipix, theta, phi)
|
---|
| 601 | ==================================================
|
---|
| 602 | c gives theta and phi corresponding to pixel ipix (NESTED)
|
---|
| 603 | c for a parameter nside
|
---|
| 604 | ==================================================
|
---|
| 605 | */
|
---|
| 606 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
|
---|
| 607 | // (16/12/98)
|
---|
| 608 |
|
---|
| 609 | const PIXELS_XY& PXY= PIXELS_XY::instance();
|
---|
| 610 |
|
---|
| 611 | int npix, npface, face_num;
|
---|
| 612 | int ipf, ip_low, ip_trunc, ip_med, ip_hi;
|
---|
| 613 | int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
|
---|
| 614 | double z, fn, fact1, fact2;
|
---|
| 615 | double piover2(Pi/2.);
|
---|
| 616 | int ns_max(8192);
|
---|
| 617 |
|
---|
| 618 | // ! coordinate of the lowest corner of each face
|
---|
| 619 | int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};//! in unit of nside
|
---|
| 620 | int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};// ! in unit of nside/2
|
---|
| 621 |
|
---|
| 622 | if( nside<1 || nside>ns_max ) {
|
---|
[1954] | 623 | char buff[64];
|
---|
| 624 | sprintf(buff,"HEALPix::pix2ang_nest nside(=%d) out of range ", nside);
|
---|
| 625 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 626 | }
|
---|
| 627 | npix = 12 * nside*nside;
|
---|
| 628 | if( ipix<0 || ipix>npix-1 ) {
|
---|
[1954] | 629 | char buff[64];
|
---|
| 630 | sprintf(buff,"HEALPix::pix2ang_nest ipix(=%d) out of range ", ipix);
|
---|
| 631 | throw RangeCheckError(PExcLongMessage(buff));
|
---|
[1195] | 632 | }
|
---|
| 633 |
|
---|
| 634 | fn = 1.*nside;
|
---|
| 635 | fact1 = 1./(3.*fn*fn);
|
---|
| 636 | fact2 = 2./(3.*fn);
|
---|
| 637 | nl4 = 4*nside;
|
---|
| 638 |
|
---|
| 639 | //c finds the face, and the number in the face
|
---|
| 640 | npface = nside*nside;
|
---|
| 641 |
|
---|
| 642 | face_num = ipix/npface;// ! face number in {0,11}
|
---|
[2082] | 643 | ipf = ipix%npface; //RzModFloor (int)fmod(ipix,npface); ! pixel number in the face {0,npface-1}
|
---|
[1195] | 644 |
|
---|
| 645 | //c finds the x,y on the face (starting from the lowest corner)
|
---|
| 646 | //c from the pixel number
|
---|
[2082] | 647 | ip_low = ipf%1024; //RzModFloor (int)fmod(ipf,1024); ! content of the last 10 bits
|
---|
[1195] | 648 | ip_trunc = ipf/1024 ;// ! truncation of the last 10 bits
|
---|
[2082] | 649 | ip_med = ip_trunc%1024; //RzModFloor (int)fmod(ip_trunc,1024); ! content of the next 10 bits
|
---|
[1195] | 650 | ip_hi = ip_trunc/1024 ;//! content of the high weight 10 bits
|
---|
| 651 |
|
---|
| 652 | ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
|
---|
| 653 | iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
|
---|
| 654 |
|
---|
| 655 | //c transforms this in (horizontal, vertical) coordinates
|
---|
| 656 | jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
|
---|
| 657 | jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
|
---|
| 658 |
|
---|
| 659 | //c computes the z coordinate on the sphere
|
---|
| 660 | // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
|
---|
| 661 | jr = jrll[face_num]*nside - jrt - 1;
|
---|
| 662 | nr = nside;// ! equatorial region (the most frequent)
|
---|
| 663 | z = (2*nside-jr)*fact2;
|
---|
[2082] | 664 | kshift = (jr-nside) % 2; //RzModFloor (int)fmod(jr - nside, 2);
|
---|
[1195] | 665 | if( jr<nside ) { //then ! north pole region
|
---|
| 666 | nr = jr;
|
---|
| 667 | z = 1. - nr*nr*fact1;
|
---|
| 668 | kshift = 0;
|
---|
| 669 | }
|
---|
| 670 | else {
|
---|
| 671 | if( jr>3*nside ) {// then ! south pole region
|
---|
| 672 | nr = nl4 - jr;
|
---|
| 673 | z = - 1. + nr*nr*fact1;
|
---|
| 674 | kshift = 0;
|
---|
| 675 | }
|
---|
| 676 | }
|
---|
| 677 | theta = acos(z);
|
---|
| 678 |
|
---|
| 679 | //c computes the phi coordinate on the sphere, in [0,2Pi]
|
---|
| 680 | // jp = (jpll[face_num+1]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
|
---|
| 681 | jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;
|
---|
| 682 | if( jp>nl4 ) jp = jp - nl4;
|
---|
| 683 | if( jp<1 ) jp = jp + nl4;
|
---|
| 684 | phi = (jp - (kshift+1)*0.5) * (piover2 / nr);
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[1196] | 687 |
|
---|