| [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 ) { | 
|---|
| [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 |  | 
|---|
|  | 234 | int aux=n_before + jp - 1; | 
|---|
|  | 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; | 
|---|
|  | 297 | irn   = (int)floor( ip / nl4 ) + nside;//               ! counted from North pole | 
|---|
|  | 298 | iphi  = (int)fmod(ip,nl4) + 1; | 
|---|
|  | 299 |  | 
|---|
|  | 300 | kshift  = (int)fmod(irn+nside,2);//  ! 1 if irn+nside is odd, 0 otherwise | 
|---|
|  | 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 | 
|---|
|  | 307 | face_num = (int)fmod(ifp,4) + 4; | 
|---|
|  | 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 |  | 
|---|
|  | 340 | ix_low = (int)fmod(ix,128); | 
|---|
|  | 341 | ix_hi  = ix/128; | 
|---|
|  | 342 | iy_low = (int)fmod(iy,128); | 
|---|
|  | 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; | 
|---|
|  | 399 | if (fmod(ir,2)==0.) kshift = 1;// ! kshift=1 if ir even, 0 otherwise | 
|---|
|  | 400 |  | 
|---|
|  | 401 | ip = (int)floor( ( jp+jm - nside + kshift + 1 ) / 2 ) + 1;// ! in {1,4n} | 
|---|
|  | 402 | if( ip>nl4 ) ip = ip - nl4; | 
|---|
|  | 403 |  | 
|---|
|  | 404 | ipix1 = ncap + nl4*(ir-1) + ip ; | 
|---|
|  | 405 | } | 
|---|
|  | 406 | else { | 
|---|
|  | 407 |  | 
|---|
|  | 408 | tp = tt - floor(tt);//      !MOD(tt,1.d0) | 
|---|
|  | 409 | tmp = sqrt( 3.*(1. - za) ); | 
|---|
|  | 410 |  | 
|---|
|  | 411 | jp = (int)floor( nside * tp * tmp );// ! increasing edge line index | 
|---|
|  | 412 | jm = (int)floor( nside * (1. - tp) * tmp );// ! decreasing edge line index | 
|---|
|  | 413 |  | 
|---|
|  | 414 | ir = jp + jm + 1;//        ! ring number counted from the closest pole | 
|---|
|  | 415 | ip = (int)floor( tt * ir ) + 1;// ! in {1,4*ir} | 
|---|
|  | 416 | if( ip>4*ir ) ip = ip - 4*ir; | 
|---|
|  | 417 |  | 
|---|
|  | 418 | ipix1 = 2*ir*(ir-1) + ip; | 
|---|
|  | 419 | if( z<=0. ) { | 
|---|
|  | 420 | ipix1 = npix - 2*ir*(ir+1) + ip; | 
|---|
|  | 421 | } | 
|---|
|  | 422 | } | 
|---|
|  | 423 | return (ipix1 - 1);// ! in {0, npix-1} | 
|---|
|  | 424 | } | 
|---|
|  | 425 |  | 
|---|
| [1196] | 426 | int_4 HEALPix::ang2pix_nest(int_4 nside, double theta, double phi) | 
|---|
| [1195] | 427 | { | 
|---|
|  | 428 | /* | 
|---|
|  | 429 | ================================================== | 
|---|
|  | 430 | subroutine ang2pix_nest(nside, theta, phi, ipix) | 
|---|
|  | 431 | ================================================== | 
|---|
|  | 432 | c     gives the pixel number ipix (NESTED) | 
|---|
|  | 433 | c     corresponding to angles theta and phi | 
|---|
|  | 434 | c | 
|---|
|  | 435 | c     the computation is made to the highest resolution available (nside=8192) | 
|---|
|  | 436 | c     and then degraded to that required (by integer division) | 
|---|
|  | 437 | c     this doesn't cost more, and it makes sure | 
|---|
|  | 438 | c     that the treatement of round-off will be consistent | 
|---|
|  | 439 | c     for every resolution | 
|---|
|  | 440 | ================================================== | 
|---|
|  | 441 | */ | 
|---|
|  | 442 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur | 
|---|
|  | 443 | //  (16/12/98) | 
|---|
|  | 444 |  | 
|---|
|  | 445 | const PIXELS_XY& PXY= PIXELS_XY::instance(); | 
|---|
|  | 446 |  | 
|---|
|  | 447 | double    z, za, z0, tt, tp, tmp; | 
|---|
|  | 448 | int face_num,jp,jm; | 
|---|
|  | 449 | int ifp, ifm; | 
|---|
|  | 450 | int  ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf, ntt; | 
|---|
|  | 451 | double piover2(Pi/2.), twopi(2.*Pi); | 
|---|
|  | 452 | int ns_max(8192); | 
|---|
|  | 453 |  | 
|---|
|  | 454 | if( nside<1 || nside>ns_max ) { | 
|---|
| [1954] | 455 | char buff[64]; | 
|---|
|  | 456 | sprintf(buff,"HEALPix:::ang2pix_nest nside(=%d) out of range ", nside); | 
|---|
|  | 457 | throw RangeCheckError(PExcLongMessage(buff)); | 
|---|
| [1195] | 458 | } | 
|---|
|  | 459 | if( theta<0 || theta>Pi ) { | 
|---|
| [1954] | 460 | char buff[64]; | 
|---|
|  | 461 | sprintf(buff,"HEALPix:::ang2pix_nest theta(=%g) out of range ", theta); | 
|---|
|  | 462 | throw RangeCheckError(PExcLongMessage(buff)); | 
|---|
| [1195] | 463 | } | 
|---|
|  | 464 | z  = cos(theta); | 
|---|
|  | 465 | za = fabs(z); | 
|---|
|  | 466 | z0 = 2./3.; | 
|---|
|  | 467 | if( phi>=twopi ) phi = phi - twopi; | 
|---|
|  | 468 | if( phi<0. )    phi = phi + twopi; | 
|---|
|  | 469 | tt = phi / piover2;// ! in [0,4[ | 
|---|
|  | 470 | if( za<=z0 ) { // then ! equatorial region | 
|---|
|  | 471 |  | 
|---|
|  | 472 | //(the index of edge lines increase when the longitude=phi goes up) | 
|---|
|  | 473 | jp = (int)floor(ns_max*(0.5 + tt - z*0.75));// !  ascending edge line index | 
|---|
|  | 474 | jm = (int)floor(ns_max*(0.5 + tt + z*0.75));// ! descending edge line index | 
|---|
|  | 475 |  | 
|---|
|  | 476 | //c        finds the face | 
|---|
|  | 477 | ifp = jp / ns_max;//  ! in {0,4} | 
|---|
|  | 478 | ifm = jm / ns_max; | 
|---|
|  | 479 | if( ifp==ifm ) face_num = (int)fmod(ifp,4) + 4; //then  ! faces 4 to 7 | 
|---|
|  | 480 | else if( ifp<ifm ) face_num = (int)fmod(ifp,4); // (half-)faces 0 to 3 | 
|---|
|  | 481 | else face_num = (int)fmod(ifm,4) + 8;//! (half-)faces 8 to 11 | 
|---|
|  | 482 |  | 
|---|
|  | 483 | ix = (int)fmod(jm, ns_max); | 
|---|
|  | 484 | iy = ns_max - (int)fmod(jp, ns_max) - 1; | 
|---|
|  | 485 | } | 
|---|
|  | 486 | else { //! polar region, za > 2/3 | 
|---|
|  | 487 |  | 
|---|
|  | 488 | ntt = (int)floor(tt); | 
|---|
|  | 489 | if( ntt>=4 ) ntt = 3; | 
|---|
|  | 490 | tp = tt - ntt; | 
|---|
|  | 491 | tmp = sqrt( 3.*(1. - za) );//  ! in ]0,1] | 
|---|
|  | 492 |  | 
|---|
|  | 493 | //(the index of edge lines increase when distance from the closest pole goes up) | 
|---|
|  | 494 | jp = (int)floor(ns_max*tp*tmp); // ! line going toward the pole as phi increases | 
|---|
|  | 495 | jm = (int)floor(ns_max*(1.-tp)*tmp); // ! that one goes away of the closest pole | 
|---|
|  | 496 | jp = (int)min(ns_max-1, jp);// ! for points too close to the boundary | 
|---|
|  | 497 | jm = (int)min(ns_max-1, jm); | 
|---|
|  | 498 |  | 
|---|
|  | 499 | // finds the face and pixel's (x,y) | 
|---|
|  | 500 | if( z>=0 ) { | 
|---|
|  | 501 | face_num = ntt;//  ! in {0,3} | 
|---|
|  | 502 | ix = ns_max - jm - 1; | 
|---|
|  | 503 | iy = ns_max - jp - 1; | 
|---|
|  | 504 | } | 
|---|
|  | 505 | else { | 
|---|
|  | 506 | face_num = ntt + 8;// ! in {8,11} | 
|---|
|  | 507 | ix =  jp; | 
|---|
|  | 508 | iy =  jm; | 
|---|
|  | 509 | } | 
|---|
|  | 510 | } | 
|---|
|  | 511 |  | 
|---|
|  | 512 | ix_low = (int)fmod(ix,128); | 
|---|
|  | 513 | ix_hi  =     ix/128; | 
|---|
|  | 514 | iy_low = (int)fmod(iy,128); | 
|---|
|  | 515 | iy_hi  =     iy/128; | 
|---|
|  | 516 | ipf= (PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low)); | 
|---|
|  | 517 | //  ipf = ipf / pow(ns_max/nside,2.);//  ! in {0, nside**2 - 1} | 
|---|
|  | 518 | //  return ( ipf + face_num*pow(nside,2));//    ! in {0, 12*nside**2 - 1} | 
|---|
|  | 519 | // $CHECK$  Reza 25/10/99 , pow remplace par * | 
|---|
|  | 520 | ipf = ipf / ((ns_max/nside)*(ns_max/nside)); | 
|---|
|  | 521 | return (ipf + face_num*nside*nside); | 
|---|
|  | 522 | } | 
|---|
|  | 523 |  | 
|---|
| [1196] | 524 | void HEALPix::pix2ang_ring(int_4 nside,int_4 ipix,double& theta,double& phi) | 
|---|
|  | 525 | { | 
|---|
| [1195] | 526 | /* | 
|---|
|  | 527 | =================================================== | 
|---|
|  | 528 | c     gives theta and phi corresponding to pixel ipix (RING) | 
|---|
|  | 529 | c     for a parameter nside | 
|---|
|  | 530 | =================================================== | 
|---|
|  | 531 | */ | 
|---|
|  | 532 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur | 
|---|
|  | 533 | //  (16/12/98) | 
|---|
|  | 534 |  | 
|---|
|  | 535 | int nl2, nl4, npix, ncap, iring, iphi, ip, ipix1; | 
|---|
|  | 536 | double  fact1, fact2, fodd, hip, fihip; | 
|---|
|  | 537 |  | 
|---|
|  | 538 | int ns_max(8192); | 
|---|
|  | 539 |  | 
|---|
|  | 540 | if( nside<1 || nside>ns_max ) { | 
|---|
| [1954] | 541 | char buff[64]; | 
|---|
|  | 542 | sprintf(buff,"HEALPix::pix2ang_ring nside(=%d) out of range ", nside); | 
|---|
|  | 543 | throw RangeCheckError(PExcLongMessage(buff)); | 
|---|
| [1195] | 544 | } | 
|---|
|  | 545 | npix = 12*nside*nside;      // ! total number of points | 
|---|
|  | 546 | if( ipix<0 || ipix>npix-1 ) { | 
|---|
| [1954] | 547 | char buff[64]; | 
|---|
|  | 548 | sprintf(buff,"HEALPix::pix2ang_ring ipix(=%d) out of range ", ipix); | 
|---|
|  | 549 | throw RangeCheckError(PExcLongMessage(buff)); | 
|---|
| [1195] | 550 | } | 
|---|
|  | 551 |  | 
|---|
|  | 552 | ipix1 = ipix + 1; // in {1, npix} | 
|---|
|  | 553 | nl2 = 2*nside; | 
|---|
|  | 554 | nl4 = 4*nside; | 
|---|
|  | 555 | ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1 | 
|---|
|  | 556 | fact1 = 1.5*nside; | 
|---|
|  | 557 | fact2 = 3.0*nside*nside; | 
|---|
|  | 558 |  | 
|---|
|  | 559 | if( ipix1 <= ncap ) {  //! North Polar cap ------------- | 
|---|
|  | 560 |  | 
|---|
|  | 561 | hip   = ipix1/2.; | 
|---|
|  | 562 | fihip = floor(hip); | 
|---|
|  | 563 | iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole | 
|---|
|  | 564 | iphi  = ipix1 - 2*iring*(iring - 1); | 
|---|
|  | 565 |  | 
|---|
|  | 566 | theta = acos( 1. - iring*iring / fact2 ); | 
|---|
|  | 567 | phi   = ((double)iphi - 0.5) * Pi/(2.*iring); | 
|---|
|  | 568 | //    cout << theta << " " << phi << endl; | 
|---|
|  | 569 | } | 
|---|
|  | 570 | else if( ipix1 <= nl2*(5*nside+1) ) {//then ! Equatorial region ------ | 
|---|
|  | 571 |  | 
|---|
|  | 572 | ip    = ipix1 - ncap - 1; | 
|---|
|  | 573 | iring = (int)floor( ip / nl4 ) + nside;// ! counted from North pole | 
|---|
|  | 574 | iphi  = ip%nl4 + 1; | 
|---|
|  | 575 |  | 
|---|
|  | 576 | fodd  = 0.5 * (1 + (iring+nside)%2 );//  ! 1 if iring+nside is odd, 1/2 otherwise | 
|---|
|  | 577 | theta = acos( (nl2 - iring) / fact1 ); | 
|---|
|  | 578 | phi   = ((double)iphi - fodd) * Pi /(2.*nside); | 
|---|
|  | 579 | } | 
|---|
|  | 580 | else {//! South Polar cap ----------------------------------- | 
|---|
|  | 581 |  | 
|---|
|  | 582 | ip    = npix - ipix1 + 1; | 
|---|
|  | 583 | hip   = ip/2.; | 
|---|
|  | 584 | fihip = floor(hip); | 
|---|
|  | 585 | iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;//     ! counted from South pole | 
|---|
|  | 586 | iphi  = (int)(4.*iring + 1 - (ip - 2.*iring*(iring-1))); | 
|---|
|  | 587 |  | 
|---|
|  | 588 | theta = acos( -1. + iring*iring / fact2 ); | 
|---|
|  | 589 | phi   = ((double)iphi - 0.5) * Pi/(2.*iring); | 
|---|
|  | 590 | //    cout << theta << " " << phi << endl; | 
|---|
|  | 591 | } | 
|---|
|  | 592 | } | 
|---|
|  | 593 |  | 
|---|
| [1196] | 594 | void HEALPix::pix2ang_nest(int_4 nside,int_4 ipix,double& theta,double& phi) | 
|---|
|  | 595 | { | 
|---|
| [1195] | 596 | /* | 
|---|
|  | 597 | ================================================== | 
|---|
|  | 598 | subroutine pix2ang_nest(nside, ipix, theta, phi) | 
|---|
|  | 599 | ================================================== | 
|---|
|  | 600 | c     gives theta and phi corresponding to pixel ipix (NESTED) | 
|---|
|  | 601 | c     for a parameter nside | 
|---|
|  | 602 | ================================================== | 
|---|
|  | 603 | */ | 
|---|
|  | 604 | // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur | 
|---|
|  | 605 | //  (16/12/98) | 
|---|
|  | 606 |  | 
|---|
|  | 607 | const PIXELS_XY& PXY= PIXELS_XY::instance(); | 
|---|
|  | 608 |  | 
|---|
|  | 609 | int npix, npface, face_num; | 
|---|
|  | 610 | int ipf, ip_low, ip_trunc, ip_med, ip_hi; | 
|---|
|  | 611 | int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4; | 
|---|
|  | 612 | double z, fn, fact1, fact2; | 
|---|
|  | 613 | double piover2(Pi/2.); | 
|---|
|  | 614 | int ns_max(8192); | 
|---|
|  | 615 |  | 
|---|
|  | 616 | // ! coordinate of the lowest corner of each face | 
|---|
|  | 617 | int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};//! in unit of nside | 
|---|
|  | 618 | int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};// ! in unit of nside/2 | 
|---|
|  | 619 |  | 
|---|
|  | 620 | if( nside<1 || nside>ns_max ) { | 
|---|
| [1954] | 621 | char buff[64]; | 
|---|
|  | 622 | sprintf(buff,"HEALPix::pix2ang_nest nside(=%d) out of range ", nside); | 
|---|
|  | 623 | throw RangeCheckError(PExcLongMessage(buff)); | 
|---|
| [1195] | 624 | } | 
|---|
|  | 625 | npix = 12 * nside*nside; | 
|---|
|  | 626 | if( ipix<0 || ipix>npix-1 ) { | 
|---|
| [1954] | 627 | char buff[64]; | 
|---|
|  | 628 | sprintf(buff,"HEALPix::pix2ang_nest ipix(=%d) out of range ", ipix); | 
|---|
|  | 629 | throw RangeCheckError(PExcLongMessage(buff)); | 
|---|
| [1195] | 630 | } | 
|---|
|  | 631 |  | 
|---|
|  | 632 | fn = 1.*nside; | 
|---|
|  | 633 | fact1 = 1./(3.*fn*fn); | 
|---|
|  | 634 | fact2 = 2./(3.*fn); | 
|---|
|  | 635 | nl4   = 4*nside; | 
|---|
|  | 636 |  | 
|---|
|  | 637 | //c     finds the face, and the number in the face | 
|---|
|  | 638 | npface = nside*nside; | 
|---|
|  | 639 |  | 
|---|
|  | 640 | face_num = ipix/npface;//  ! face number in {0,11} | 
|---|
|  | 641 | ipf = (int)fmod(ipix,npface);//  ! pixel number in the face {0,npface-1} | 
|---|
|  | 642 |  | 
|---|
|  | 643 | //c     finds the x,y on the face (starting from the lowest corner) | 
|---|
|  | 644 | //c     from the pixel number | 
|---|
|  | 645 | ip_low = (int)fmod(ipf,1024);//       ! content of the last 10 bits | 
|---|
|  | 646 | ip_trunc =   ipf/1024 ;//       ! truncation of the last 10 bits | 
|---|
|  | 647 | ip_med = (int)fmod(ip_trunc,1024);//  ! content of the next 10 bits | 
|---|
|  | 648 | ip_hi  =     ip_trunc/1024   ;//! content of the high weight 10 bits | 
|---|
|  | 649 |  | 
|---|
|  | 650 | ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low); | 
|---|
|  | 651 | iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low); | 
|---|
|  | 652 |  | 
|---|
|  | 653 | //c     transforms this in (horizontal, vertical) coordinates | 
|---|
|  | 654 | jrt = ix + iy;//  ! 'vertical' in {0,2*(nside-1)} | 
|---|
|  | 655 | jpt = ix - iy;//  ! 'horizontal' in {-nside+1,nside-1} | 
|---|
|  | 656 |  | 
|---|
|  | 657 | //c     computes the z coordinate on the sphere | 
|---|
|  | 658 | //      jr =  jrll[face_num+1]*nside - jrt - 1;//   ! ring number in {1,4*nside-1} | 
|---|
|  | 659 | jr =  jrll[face_num]*nside - jrt - 1; | 
|---|
|  | 660 | nr = nside;//                  ! equatorial region (the most frequent) | 
|---|
|  | 661 | z  = (2*nside-jr)*fact2; | 
|---|
|  | 662 | kshift = (int)fmod(jr - nside, 2); | 
|---|
|  | 663 | if( jr<nside ) { //then     ! north pole region | 
|---|
|  | 664 | nr = jr; | 
|---|
|  | 665 | z = 1. - nr*nr*fact1; | 
|---|
|  | 666 | kshift = 0; | 
|---|
|  | 667 | } | 
|---|
|  | 668 | else { | 
|---|
|  | 669 | if( jr>3*nside ) {// then ! south pole region | 
|---|
|  | 670 | nr = nl4 - jr; | 
|---|
|  | 671 | z = - 1. + nr*nr*fact1; | 
|---|
|  | 672 | kshift = 0; | 
|---|
|  | 673 | } | 
|---|
|  | 674 | } | 
|---|
|  | 675 | theta = acos(z); | 
|---|
|  | 676 |  | 
|---|
|  | 677 | //c     computes the phi coordinate on the sphere, in [0,2Pi] | 
|---|
|  | 678 | //      jp = (jpll[face_num+1]*nr + jpt + 1 + kshift)/2;//  ! 'phi' number in the ring in {1,4*nr} | 
|---|
|  | 679 | jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2; | 
|---|
|  | 680 | if( jp>nl4 ) jp = jp - nl4; | 
|---|
|  | 681 | if( jp<1 )   jp = jp + nl4; | 
|---|
|  | 682 | phi = (jp - (kshift+1)*0.5) * (piover2 / nr); | 
|---|
|  | 683 | } | 
|---|
|  | 684 |  | 
|---|
| [1196] | 685 |  | 
|---|