source: Sophya/trunk/SophyaLib/SkyMap/spherehealpix.cc@ 853

Last change on this file since 853 was 853, checked in by ansari, 25 years ago

Passage SphereHealpix a SphereHEALPix , FITS_SphereHEALPix mis ds FitsIOServer

  1. Ansari 10/4/2000
File size: 31.7 KB
Line 
1#include "machdefs.h"
2#include <math.h>
3#include <complex>
4
5#include "pexceptions.h"
6#include "fiondblock.h"
7#include "spherehealpix.h"
8#include "strutil.h"
9
10extern "C"
11{
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15}
16
17
18//*******************************************************************
19// Class PIXELS_XY
20// Construction des tableaux necessaires a la traduction des indices RING en
21// indices NESTED (ou l'inverse)
22//*******************************************************************
23
24PIXELS_XY::PIXELS_XY()
25{
26 pix2x_.ReSize(1024);
27 pix2x_.Reset();
28 pix2y_.ReSize(1024);
29 pix2y_.Reset();
30 x2pix_.ReSize(128);
31 x2pix_.Reset();
32 y2pix_.ReSize(128);
33 y2pix_.Reset();
34 mk_pix2xy();
35 mk_xy2pix();
36}
37
38PIXELS_XY& PIXELS_XY::instance()
39{
40 static PIXELS_XY single;
41 return (single);
42}
43
44void PIXELS_XY::mk_pix2xy()
45{
46 /*
47 ==================================================
48 subroutine mk_pix2xy
49 ==================================================
50 c constructs the array giving x and y in the face from pixel number
51 c for the nested (quad-cube like) ordering of pixels
52 c
53 c the bits corresponding to x and y are interleaved in the pixel number
54 c one breaks up the pixel number by even and odd bits
55 ==================================================
56 */
57 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
58 // (16/12/98)
59
60 int kpix, jpix, IX, IY, IP, ID;
61
62 for(kpix = 0; kpix < 1024; kpix++)
63 {
64 jpix = kpix;
65 IX = 0;
66 IY = 0;
67 IP = 1 ;// ! bit position (in x and y)
68 while( jpix!=0 )
69 { // ! go through all the bits
70 ID=jpix%2;// ! bit value (in kpix), goes in ix
71 jpix = jpix/2;
72 IX = ID*IP+IX;
73
74 ID=jpix%2;// ! bit value (in kpix), goes in iy
75 jpix = jpix/2;
76 IY = ID*IP+IY;
77
78 IP = 2*IP;// ! next bit (in x and y)
79 }
80 pix2x_(kpix) = IX;// ! in 0,31
81 pix2y_(kpix) = IY;// ! in 0,31
82 }
83}
84
85void PIXELS_XY::mk_xy2pix()
86{
87 /*
88 =================================================
89 subroutine mk_xy2pix
90 =================================================
91 c sets the array giving the number of the pixel lying in (x,y)
92 c x and y are in {1,128}
93 c the pixel number is in {0,128**2-1}
94 c
95 c if i-1 = sum_p=0 b_p * 2^p
96 c then ix = sum_p=0 b_p * 4^p
97 c iy = 2*ix
98 c ix + iy in {0, 128**2 -1}
99 =================================================
100 */
101 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
102 // (16/12/98)
103
104 int K,IP,I,J,ID;
105 for(I = 1; I <= 128; I++)
106 {
107 J = I-1;// !pixel numbers
108 K = 0;//
109 IP = 1;//
110 truc : if( J==0 )
111 {
112 x2pix_(I-1) = K;
113 y2pix_(I-1) = 2*K;
114 }
115 else
116 {
117 ID = (int)fmod(J,2);
118 J = J/2;
119 K = IP*ID+K;
120 IP = IP*4;
121 goto truc;
122 }
123 }
124}
125
126//*******************************************************************
127//++
128// Class SphereHEALPix
129//
130// include SphereHealpix.h strutil.h
131//
132// Pixelisation Gorski
133//
134//
135//| -----------------------------------------------------------------------
136//| version 0.8.2 Aug97 TAC Eric Hivon, Kris Gorski
137//| -----------------------------------------------------------------------
138//
139// the sphere is split in 12 diamond-faces containing nside**2 pixels each
140//
141// the numbering of the pixels (in the nested scheme) is similar to
142// quad-cube
143// In each face the first pixel is in the lowest corner of the diamond
144//
145// the faces are (x,y) coordinate on each face
146//| . . . . <--- North Pole
147//| / \ / \ / \ / \ ^ ^
148//| . 0 . 1 . 2 . 3 . <--- z = 2/3 \ /
149//| \ / \ / \ / \ / y \ / x
150//| 4 . 5 . 6 . 7 . 4 <--- equator \ /
151//| / \ / \ / \ / \ \/
152//| . 8 . 9 .10 .11 . <--- z = -2/3 (0,0) : lowest corner
153//| \ / \ / \ / \ /
154//| . . . . <--- South Pole
155//|
156// phi:0 2Pi
157//
158// in the ring scheme pixels are numbered along the parallels
159// the first parallel is the one closest to the north pole and so on
160// on each parallel, pixels are numbered starting from the one closest
161// to phi = 0
162//
163// nside MUST be a power of 2 (<= 8192)
164//--
165//++
166//
167// Links Parents
168//
169// SphericalMap
170//--
171
172/* --Methode-- */
173//++
174// Titre Constructors
175//--
176//++
177
178template<class T>
179SphereHEALPix<T>::SphereHEALPix()
180
181//--
182{
183 InitNul();
184 pixels_.Reset();
185}
186
187//++
188template<class T>
189SphereHEALPix<T>::SphereHEALPix(int_4 m)
190
191// m is the "nside" of the Gorski algorithm
192//
193// The total number of pixels will be Npix = 12*nside**2
194//
195// nside MUST be a power of 2 (<= 8192)
196//--
197{
198
199 if(m <= 0 || m > 8192)
200 {
201 cout << "SphereHEALPix : m hors bornes [0,8192], m= " << m << endl;
202 throw RangeCheckError("SphereHEALPix<T>::SphereHEALPix() - Out of bound nside (< 8192)!");
203 }
204 // verifier que m est une puissance de deux
205 int x= m;
206 while(x%2 == 0) x/=2;
207 if(x != 1)
208 {
209 cout<<"SphereHEALPix: m doit etre une puissance de deux, m= "<<m<<endl;
210 throw ParmError("SphereHEALPix<T>::SphereHEALPix() - nside != 2^n !");
211 }
212 InitNul();
213 Pixelize(m);
214 SetThetaSlices();
215}
216//++
217template<class T>
218SphereHEALPix<T>::SphereHEALPix(const SphereHEALPix<T>& s, bool share)
219 : pixels_(s.pixels_, share), sliceBeginIndex_(s.sliceBeginIndex_, share),
220 sliceLenght_(s.sliceLenght_, share)
221// copy constructor
222//--
223{
224 if(s.mInfo_) mInfo_= new DVList(*s.mInfo_);
225
226 nSide_= s.nSide_;
227 nPix_ = s.nPix_;
228 omeg_ = s.omeg_;
229}
230
231//++
232// Titre Destructor
233//--
234//++
235template<class T>
236SphereHEALPix<T>::~SphereHEALPix()
237
238//--
239{
240}
241
242//++
243// Titre Public Methods
244//--
245
246//++
247template<class T>
248void SphereHEALPix<T>::Resize(int_4 m)
249
250// m is the "nside" of the Gorski algorithm
251//
252// The total number of pixels will be Npix = 12*nside**2
253//
254// nside MUST be a power of 2 (<= 8192)
255//--
256{
257 if (m<=0 || m> 8192) {
258 cout << "SphereHEALPix : m hors bornes [0,8192], m= " << m << endl;
259 exit(1);
260 }
261 // verifier que m est une puissance de deux
262 int x= m;
263 while (x%2==0) x/=2;
264 if(x != 1)
265 {
266 cout<<"SphereHEALPix: m doit etre une puissance de deux, m= "<<m<<endl;
267 exit(1);
268 }
269 InitNul();
270 Pixelize(m);
271 SetThetaSlices();
272}
273
274template<class T>
275void SphereHEALPix<T>::Pixelize( int_4 m)
276
277// prépare la pixelisation Gorski (m a la même signification
278// que pour le constructeur)
279//
280//
281{
282 // On memorise les arguments d'appel
283 nSide_= m;
284
285 // Nombre total de pixels sur la sphere entiere
286 nPix_= 12*nSide_*nSide_;
287
288 // pour le moment les tableaux qui suivent seront ranges dans l'ordre
289 // de l'indexation GORSKY "RING"
290 // on pourra ulterieurement changer de strategie et tirer profit
291 // de la dualite d'indexation GORSKY (RING et NEST) : tout dependra
292 // de pourquoi c'est faire
293
294 // Creation et initialisation du vecteur des contenus des pixels
295 pixels_.ReSize(nPix_);
296 pixels_.Reset();
297
298 // solid angle per pixel
299 omeg_= 4.0*Pi/nPix_;
300}
301
302template<class T>
303void SphereHEALPix<T>::InitNul()
304//
305// initialise à zéro les variables de classe
306{
307 nSide_= 0;
308 nPix_ = 0;
309 omeg_ = 0.;
310// pixels_.Reset(); - Il ne faut pas mettre les pixels a zero si share !
311}
312
313/* --Methode-- */
314//++
315template<class T>
316int_4 SphereHEALPix<T>::NbPixels() const
317
318// Retourne le nombre de pixels du découpage
319//--
320{
321 return(nPix_);
322}
323
324//++
325template<class T>
326uint_4 SphereHEALPix<T>::NbThetaSlices() const
327
328// Return number of slices in theta direction on the sphere
329//--
330{
331 uint_4 nbslices = uint_4(4*nSide_-1);
332 if (nSide_<=0)
333 {
334 nbslices = 0;
335 throw PException(" sphere not pixelized, NbSlice=0 ");
336 }
337 return nbslices;
338}
339
340//++
341template<class T>
342void SphereHEALPix<T>::GetThetaSlice(int_4 index,r_8& theta,TVector<r_8>& phi,TVector<T>& value) const
343
344// For a theta-slice with index 'index', return :
345//
346// the corresponding "theta"
347//
348// a vector containing the phi's of the pixels of the slice
349//
350// a vector containing the corresponding values of pixels
351//
352//--
353{
354
355 if (index<0 || index >= NbThetaSlices())
356 {
357 // THROW(out_of_range("SphereHEALPix::PIxVal Pixel index out of range"));
358 cout << " SphereHEALPix::GetThetaSlice : Pixel index out of range" <<endl;
359 throw RangeCheckError(" SphereHEALPix::GetThetaSlice : Pixel index out of range");
360 }
361
362
363 int_4 iring= sliceBeginIndex_(index);
364 int_4 lring = sliceLenght_(index);
365
366 phi.ReSize(lring);
367 value.ReSize(lring);
368
369 double TH= 0.;
370 double FI= 0.;
371 for(int_4 kk = 0; kk < lring;kk++)
372 {
373 PixThetaPhi(kk+iring,TH,FI);
374 phi(kk)= FI;
375 value(kk)= PixVal(kk+iring);
376 }
377 theta= TH;
378}
379//++
380//++
381
382template<class T>
383void SphereHEALPix<T>::GetThetaSlice(int_4 sliceIndex,r_8& theta, r_8& phi0, TVector<int_4>& pixelIndices,TVector<T>& value) const
384
385// For a theta-slice with index 'sliceIndex', return :
386//
387// the corresponding "theta"
388// the corresponding "phi" for first pixel of the slice
389//
390// a vector containing the indices of the pixels of the slice
391// (equally distributed in phi)
392//
393// a vector containing the corresponding values of pixels
394//
395//--
396{
397
398 if (sliceIndex<0 || sliceIndex >= NbThetaSlices())
399 {
400 // THROW(out_of_range("SphereHEALPix::PIxVal Pixel index out of range"));
401 cout << " SphereHEALPix::GetThetaSlice : Pixel index out of range" <<endl;
402 throw RangeCheckError(" SphereHEALPix::GetThetaSlice : Pixel index out of range");
403 }
404 int_4 iring= sliceBeginIndex_(sliceIndex);
405 int_4 lring = sliceLenght_(sliceIndex);
406 pixelIndices.ReSize(lring);
407 value.ReSize(lring);
408
409 for(int_4 kk = 0; kk < lring;kk++)
410 {
411 pixelIndices(kk)= kk+iring;
412 value(kk)= PixVal(kk+iring);
413 }
414 PixThetaPhi(iring, theta, phi0);
415}
416//++
417template<class T>
418void SphereHEALPix<T>::SetThetaSlices()
419
420//--
421{
422 sliceBeginIndex_.ReSize(4*nSide_-1);
423 sliceLenght_.ReSize(4*nSide_-1);
424 for (int sliceIndex=0; sliceIndex< nSide_-1; sliceIndex++)
425 {
426 sliceBeginIndex_(sliceIndex) = 2*sliceIndex*(sliceIndex+1);
427 sliceLenght_(sliceIndex) = 4*(sliceIndex+1);
428 }
429 for (int sliceIndex= nSide_-1; sliceIndex< 3*nSide_; sliceIndex++)
430 {
431 sliceBeginIndex_(sliceIndex) = 2*nSide_*(2*sliceIndex-nSide_+1);
432 sliceLenght_(sliceIndex) = 4*nSide_;
433 }
434 for (int sliceIndex= 3*nSide_; sliceIndex< 4*nSide_-1; sliceIndex++)
435 {
436 int_4 nc= 4*nSide_-1-sliceIndex;
437 sliceBeginIndex_(sliceIndex) = nPix_-2*nc*(nc+1);
438 sliceLenght_(sliceIndex) = 4*nc;
439 }
440}
441
442/* --Methode-- */
443//++
444template<class T>
445T& SphereHEALPix<T>::PixVal(int_4 k)
446
447// Return value of pixel with "RING" index k
448//--
449{
450 if((k < 0) || (k >= nPix_))
451 {
452 throw RangeCheckError("SphereHEALPix::PIxVal Pixel index out of range");
453 }
454 return pixels_(k);
455}
456
457/* --Methode-- */
458//++
459template<class T>
460T const& SphereHEALPix<T>::PixVal(int_4 k) const
461
462// Return value of pixel with "RING" index k
463//--
464{
465 if((k < 0) || (k >= nPix_))
466 {
467 throw RangeCheckError("SphereHEALPix::PIxVal Pixel index out of range");
468 }
469 return *(pixels_.Data()+k);
470}
471
472//++
473template<class T>
474T& SphereHEALPix<T>::PixValNest(int_4 k)
475
476// Return value of pixel with "NESTED" index k
477//--
478{
479 if((k < 0) || (k >= nPix_))
480 {
481 throw RangeCheckError("SphereHEALPix::PIxValNest Pixel index out of range");
482 }
483 return pixels_(nest2ring(nSide_,k));
484}
485//++
486
487template<class T>
488T const& SphereHEALPix<T>::PixValNest(int_4 k) const
489
490// Return value of pixel with "NESTED" index k
491//--
492{
493 if((k < 0) || (k >= nPix_))
494 {
495 throw RangeCheckError("SphereHEALPix::PIxValNest Pixel index out of range");
496 }
497 int_4 pix= nest2ring(nSide_,k);
498 return *(pixels_.Data()+pix);
499}
500
501/* --Methode-- */
502//++
503template<class T>
504bool SphereHEALPix<T>::ContainsSph(double /*theta*/, double /*phi*/) const
505//--
506{
507return(true);
508}
509
510/* --Methode-- */
511//++
512template<class T>
513int_4 SphereHEALPix<T>::PixIndexSph(double theta,double phi) const
514
515// Return "RING" index of the pixel corresponding to
516// direction (theta, phi).
517//--
518{
519 return ang2pix_ring(nSide_,theta,phi);
520}
521
522//++
523template<class T>
524int_4 SphereHEALPix<T>::PixIndexSphNest(double theta,double phi) const
525
526// Return "NESTED" index of the pixel corresponding to
527// direction (theta, phi).
528//--
529{
530 return ang2pix_nest(nSide_,theta,phi);
531}
532
533
534/* --Methode-- */
535//++
536template<class T>
537void SphereHEALPix<T>::PixThetaPhi(int_4 k,double& theta,double& phi) const
538
539// Return (theta,phi) coordinates of middle of pixel with "RING" index k
540//--
541{
542 pix2ang_ring(nSide_,k,theta,phi);
543}
544
545template <class T>
546T SphereHEALPix<T>::SetPixels(T v)
547{
548pixels_.Reset(v);
549return(v);
550}
551
552//++
553template<class T>
554double SphereHEALPix<T>::PixSolAngle(int_4 /*dummy*/) const
555// Pixel Solid angle (steradians)
556// All the pixels have the same solid angle. The dummy argument is
557// for compatibility with eventual pixelizations which would not
558// fulfil this requirement.
559//--
560{
561 return omeg_;
562}
563
564//++
565template<class T>
566void SphereHEALPix<T>::PixThetaPhiNest(int_4 k,double& theta,double& phi) const
567
568// Return (theta,phi) coordinates of middle of pixel with "NESTED" index k
569//--
570{
571 pix2ang_nest(nSide_,k,theta,phi);
572}
573
574//++
575template<class T>
576int_4 SphereHEALPix<T>::NestToRing(int_4 k) const
577
578// translation from NESTED index into RING index
579//
580//--
581{
582 return nest2ring(nSide_,k);
583}
584
585//++
586template<class T>
587int_4 SphereHEALPix<T>::RingToNest(int_4 k) const
588//
589// translation from RING index into NESTED index
590//
591//--
592{
593 return ring2nest(nSide_,k);
594}
595
596
597template<class T>
598int_4 SphereHEALPix<T>::nest2ring(int_4 nside, int_4 ipnest) const
599{
600 /*
601 ====================================================
602 subroutine nest2ring(nside, ipnest, ipring)
603 ====================================================
604 c conversion from NESTED to RING pixel number
605 ====================================================
606 */
607 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
608 // (16/12/98)
609
610 const PIXELS_XY& PXY= PIXELS_XY::instance();
611
612 int npix, npface, face_num, ncap, n_before;
613 int ipf, ip_low, ip_trunc, ip_med, ip_hi;
614 int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
615 int ns_max=8192;
616 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
617 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
618
619 if( nside<1 || nside>ns_max ) {
620 cout << "nside out of range" << endl;
621 exit(0);
622 }
623 npix = 12 * nside* nside;
624 if( ipnest<0 || ipnest>npix-1 ) {
625 cout << "ipnest out of range" << endl;
626 exit(0);
627 }
628
629 ncap = 2* nside*( nside-1);// ! number of points in the North Polar cap
630 nl4 = 4* nside;
631
632 //c finds the face, and the number in the face
633 npface = nside* nside;
634 //cccccc ip = ipnest - 1 ! in {0,npix-1}
635
636 face_num = ipnest/npface;// ! face number in {0,11}
637 ipf =ipnest%npface;// ! pixel number in the face {0,npface-1}
638 //c finds the x,y on the face (starting from the lowest corner)
639 //c from the pixel number
640 ip_low=ipf%1024; // ! content of the last 10 bits
641 ip_trunc = ipf/1024; // ! truncation of the last 10 bits
642 ip_med=ip_trunc%1024; // ! content of the next 10 bits
643 ip_hi = ip_trunc/1024;// ! content of the high weight 10 bits
644
645 ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
646 iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
647
648 //c transforms this in (horizontal, vertical) coordinates
649 jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
650 jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
651
652 //c computes the z coordinate on the sphere
653 // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
654 jr = jrll[face_num]*nside - jrt - 1;
655 nr = nside;// ! equatorial region (the most frequent)
656 n_before = ncap + nl4 * (jr - nside);
657 kshift=(jr - nside)%2;
658 if( jr<nside ) {//then ! north pole region
659 nr = jr;
660 n_before = 2 * nr * (nr - 1);
661 kshift = 0;
662 }
663 else if( jr>3*nside ) {//then ! south pole region
664 nr = nl4 - jr;
665 n_before = npix - 2 * (nr + 1) * nr;
666 kshift = 0;
667 }
668
669 //c computes the phi coordinate on the sphere, in [0,2Pi]
670 jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
671
672 if( jp>nl4 ) jp = jp - nl4;
673 if( jp<1 ) jp = jp + nl4;
674
675 int aux=n_before + jp - 1;
676 return (n_before + jp - 1);// ! in {0, npix-1}
677}
678
679template<class T>
680int_4 SphereHEALPix<T>::ring2nest(int_4 nside, int_4 ipring) const
681{
682 /*
683 ==================================================
684 subroutine ring2nest(nside, ipring, ipnest)
685 ==================================================
686 c conversion from RING to NESTED pixel number
687 ==================================================
688 */
689 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
690 // (16/12/98)
691
692 const PIXELS_XY& PXY= PIXELS_XY::instance();
693
694 double fihip, hip;
695 int npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1;
696 int kshift, face_num, nr;
697 int irn, ire, irm, irs, irt, ifm , ifp;
698 int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf;
699 int ns_max(8192);
700
701 // coordinate of the lowest corner of each face
702 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};// ! in unit of nside
703 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};//! in unit of nside/2
704
705 if( nside<1 || nside>ns_max ) {
706 cout << "nside out of range" << endl;
707 exit(0);
708 }
709 npix = 12 * nside*nside;
710 if( ipring<0 || ipring>npix-1 ) {
711 cout << "ipring out of range" << endl;
712 exit(0);
713 }
714
715 nl2 = 2*nside;
716 nl4 = 4*nside;
717 npix = 12*nside*nside;// ! total number of points
718 ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
719 ipring1 = ipring + 1;
720
721 //c finds the ring number, the position of the ring and the face number
722 if( ipring1<=ncap ) {//then
723
724 hip = ipring1/2.;
725 fihip = floor ( hip );
726 irn = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
727 iphi = ipring1 - 2*irn*(irn - 1);
728
729 kshift = 0;
730 nr = irn ;// ! 1/4 of the number of points on the current ring
731 face_num = (iphi-1) / irn;// ! in {0,3}
732 }
733 else if( ipring1<=nl2*(5*nside+1) ) {//then
734
735 ip = ipring1 - ncap - 1;
736 irn = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
737 iphi = (int)fmod(ip,nl4) + 1;
738
739 kshift = (int)fmod(irn+nside,2);// ! 1 if irn+nside is odd, 0 otherwise
740 nr = nside;
741 ire = irn - nside + 1;// ! in {1, 2*nside +1}
742 irm = nl2 + 2 - ire;
743 ifm = (iphi - ire/2 + nside -1) / nside;// ! face boundary
744 ifp = (iphi - irm/2 + nside -1) / nside;
745 if( ifp==ifm ) {//then ! faces 4 to 7
746 face_num = (int)fmod(ifp,4) + 4;
747 }
748 else if( ifp + 1==ifm ) {//then ! (half-)faces 0 to 3
749 face_num = ifp;
750 }
751 else if( ifp - 1==ifm ) {//then ! (half-)faces 8 to 11
752 face_num = ifp + 7;
753 }
754 }
755 else {
756
757 ip = npix - ipring1 + 1;
758 hip = ip/2.;
759 fihip = floor ( hip );
760 irs = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
761 iphi = 4*irs + 1 - (ip - 2*irs*(irs-1));
762
763 kshift = 0;
764 nr = irs;
765 irn = nl4 - irs;
766 face_num = (iphi-1) / irs + 8;// ! in {8,11}
767 }
768
769 //c finds the (x,y) on the face
770 irt = irn - jrll[face_num]*nside + 1;// ! in {-nside+1,0}
771 ipt = 2*iphi - jpll[face_num]*nr - kshift - 1;// ! in {-nside+1,nside-1}
772
773
774 if( ipt>=nl2 ) ipt = ipt - 8*nside;// ! for the face #4
775
776 ix = (ipt - irt ) / 2;
777 iy = -(ipt + irt ) / 2;
778
779 ix_low = (int)fmod(ix,128);
780 ix_hi = ix/128;
781 iy_low = (int)fmod(iy,128);
782 iy_hi = iy/128;
783 ipf=(PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
784
785 return (ipf + face_num* nside *nside);// ! in {0, 12*nside**2 - 1}
786}
787
788template<class T>
789int_4 SphereHEALPix<T>::ang2pix_ring(int_4 nside, double theta, double phi) const
790{
791 /*
792 ==================================================
793 c gives the pixel number ipix (RING)
794 c corresponding to angles theta and phi
795 c==================================================
796 */
797 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
798 // (16/12/98)
799
800 int nl2, nl4, ncap, npix, jp, jm, ipix1;
801 double z, za, tt, tp, tmp;
802 int ir, ip, kshift;
803
804 double piover2(Pi/2.);
805 double twopi(2.*Pi);
806 double z0(2./3.);
807 int ns_max(8192);
808
809 if( nside<1 || nside>ns_max ) {
810 cout << "nside out of range" << endl;
811 exit(0);
812 }
813
814 if( theta<0. || theta>Pi) {
815 cout << "theta out of range" << endl;
816 exit(0);
817 }
818
819 z = cos(theta);
820 za = fabs(z);
821 if( phi >= twopi) phi = phi - twopi;
822 if (phi < 0.) phi = phi + twopi;
823 tt = phi / piover2;// ! in [0,4)
824
825 nl2 = 2*nside;
826 nl4 = 4*nside;
827 ncap = nl2*(nside-1);// ! number of pixels in the north polar cap
828 npix = 12*nside*nside;
829
830 if( za <= z0 ) {
831
832 jp = (int)floor(nside*(0.5 + tt - z*0.75));// ! index of ascending edge line
833 jm = (int)floor(nside*(0.5 + tt + z*0.75));// ! index of descending edge line
834
835 ir = nside + 1 + jp - jm;// ! in {1,2n+1} (ring number counted from z=2/3)
836 kshift = 0;
837 if (fmod(ir,2)==0.) kshift = 1;// ! kshift=1 if ir even, 0 otherwise
838
839 ip = (int)floor( ( jp+jm - nside + kshift + 1 ) / 2 ) + 1;// ! in {1,4n}
840 if( ip>nl4 ) ip = ip - nl4;
841
842 ipix1 = ncap + nl4*(ir-1) + ip ;
843 }
844 else {
845
846 tp = tt - floor(tt);// !MOD(tt,1.d0)
847 tmp = sqrt( 3.*(1. - za) );
848
849 jp = (int)floor( nside * tp * tmp );// ! increasing edge line index
850 jm = (int)floor( nside * (1. - tp) * tmp );// ! decreasing edge line index
851
852 ir = jp + jm + 1;// ! ring number counted from the closest pole
853 ip = (int)floor( tt * ir ) + 1;// ! in {1,4*ir}
854 if( ip>4*ir ) ip = ip - 4*ir;
855
856 ipix1 = 2*ir*(ir-1) + ip;
857 if( z<=0. ) {
858 ipix1 = npix - 2*ir*(ir+1) + ip;
859 }
860 }
861 return (ipix1 - 1);// ! in {0, npix-1}
862}
863
864template<class T>
865int_4 SphereHEALPix<T>::ang2pix_nest(int_4 nside, double theta, double phi) const
866{
867 /*
868 ==================================================
869 subroutine ang2pix_nest(nside, theta, phi, ipix)
870 ==================================================
871 c gives the pixel number ipix (NESTED)
872 c corresponding to angles theta and phi
873 c
874 c the computation is made to the highest resolution available (nside=8192)
875 c and then degraded to that required (by integer division)
876 c this doesn't cost more, and it makes sure
877 c that the treatement of round-off will be consistent
878 c for every resolution
879 ==================================================
880 */
881 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
882 // (16/12/98)
883
884 const PIXELS_XY& PXY= PIXELS_XY::instance();
885
886 double z, za, z0, tt, tp, tmp;
887 int face_num,jp,jm;
888 int ifp, ifm;
889 int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf, ntt;
890 double piover2(Pi/2.), twopi(2.*Pi);
891 int ns_max(8192);
892
893 if( nside<1 || nside>ns_max ) {
894 cout << "nside out of range" << endl;
895 exit(0);
896 }
897 if( theta<0 || theta>Pi ) {
898 cout << "theta out of range" << endl;
899 exit(0);
900 }
901 z = cos(theta);
902 za = fabs(z);
903 z0 = 2./3.;
904 if( phi>=twopi ) phi = phi - twopi;
905 if( phi<0. ) phi = phi + twopi;
906 tt = phi / piover2;// ! in [0,4[
907 if( za<=z0 ) { // then ! equatorial region
908
909 //(the index of edge lines increase when the longitude=phi goes up)
910 jp = (int)floor(ns_max*(0.5 + tt - z*0.75));// ! ascending edge line index
911 jm = (int)floor(ns_max*(0.5 + tt + z*0.75));// ! descending edge line index
912
913 //c finds the face
914 ifp = jp / ns_max;// ! in {0,4}
915 ifm = jm / ns_max;
916 if( ifp==ifm ) face_num = (int)fmod(ifp,4) + 4; //then ! faces 4 to 7
917 else if( ifp<ifm ) face_num = (int)fmod(ifp,4); // (half-)faces 0 to 3
918 else face_num = (int)fmod(ifm,4) + 8;//! (half-)faces 8 to 11
919
920 ix = (int)fmod(jm, ns_max);
921 iy = ns_max - (int)fmod(jp, ns_max) - 1;
922 }
923 else { //! polar region, za > 2/3
924
925 ntt = (int)floor(tt);
926 if( ntt>=4 ) ntt = 3;
927 tp = tt - ntt;
928 tmp = sqrt( 3.*(1. - za) );// ! in ]0,1]
929
930 //(the index of edge lines increase when distance from the closest pole goes up)
931 jp = (int)floor(ns_max*tp*tmp); // ! line going toward the pole as phi increases
932 jm = (int)floor(ns_max*(1.-tp)*tmp); // ! that one goes away of the closest pole
933 jp = (int)min(ns_max-1, jp);// ! for points too close to the boundary
934 jm = (int)min(ns_max-1, jm);
935
936 // finds the face and pixel's (x,y)
937 if( z>=0 ) {
938 face_num = ntt;// ! in {0,3}
939 ix = ns_max - jm - 1;
940 iy = ns_max - jp - 1;
941 }
942 else {
943 face_num = ntt + 8;// ! in {8,11}
944 ix = jp;
945 iy = jm;
946 }
947 }
948
949 ix_low = (int)fmod(ix,128);
950 ix_hi = ix/128;
951 iy_low = (int)fmod(iy,128);
952 iy_hi = iy/128;
953 ipf= (PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
954 // ipf = ipf / pow(ns_max/nside,2.);// ! in {0, nside**2 - 1}
955 // return ( ipf + face_num*pow(nside,2));// ! in {0, 12*nside**2 - 1}
956 // $CHECK$ Reza 25/10/99 , pow remplace par *
957 ipf = ipf / ((ns_max/nside)*(ns_max/nside));
958 return (ipf + face_num*nside*nside);
959}
960
961template<class T>
962void SphereHEALPix<T>::pix2ang_ring(int_4 nside,int_4 ipix,double& theta,double& phi) const {
963 /*
964 ===================================================
965 c gives theta and phi corresponding to pixel ipix (RING)
966 c for a parameter nside
967 ===================================================
968 */
969 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
970 // (16/12/98)
971
972 int nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
973 double fact1, fact2, fodd, hip, fihip;
974
975 int ns_max(8192);
976
977 if( nside<1 || nside>ns_max ) {
978 cout << "nside out of range" << endl;
979 exit(0);
980 }
981 npix = 12*nside*nside; // ! total number of points
982 if( ipix<0 || ipix>npix-1 ) {
983 cout << "ipix out of range" << endl;
984 exit(0);
985 }
986
987 ipix1 = ipix + 1; // in {1, npix}
988 nl2 = 2*nside;
989 nl4 = 4*nside;
990 ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
991 fact1 = 1.5*nside;
992 fact2 = 3.0*nside*nside;
993
994 if( ipix1 <= ncap ) { //! North Polar cap -------------
995
996 hip = ipix1/2.;
997 fihip = floor(hip);
998 iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
999 iphi = ipix1 - 2*iring*(iring - 1);
1000
1001 theta = acos( 1. - iring*iring / fact2 );
1002 phi = ((double)iphi - 0.5) * Pi/(2.*iring);
1003 // cout << theta << " " << phi << endl;
1004 }
1005 else if( ipix1 <= nl2*(5*nside+1) ) {//then ! Equatorial region ------
1006
1007 ip = ipix1 - ncap - 1;
1008 iring = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
1009 iphi = ip%nl4 + 1;
1010
1011 fodd = 0.5 * (1 + (iring+nside)%2 );// ! 1 if iring+nside is odd, 1/2 otherwise
1012 theta = acos( (nl2 - iring) / fact1 );
1013 phi = ((double)iphi - fodd) * Pi /(2.*nside);
1014 }
1015 else {//! South Polar cap -----------------------------------
1016
1017 ip = npix - ipix1 + 1;
1018 hip = ip/2.;
1019 fihip = floor(hip);
1020 iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
1021 iphi = (int)(4.*iring + 1 - (ip - 2.*iring*(iring-1)));
1022
1023 theta = acos( -1. + iring*iring / fact2 );
1024 phi = ((double)iphi - 0.5) * Pi/(2.*iring);
1025 // cout << theta << " " << phi << endl;
1026 }
1027}
1028
1029template<class T>
1030void SphereHEALPix<T>::pix2ang_nest(int_4 nside,int_4 ipix,double& theta,double& phi) const {
1031 /*
1032 ==================================================
1033 subroutine pix2ang_nest(nside, ipix, theta, phi)
1034 ==================================================
1035 c gives theta and phi corresponding to pixel ipix (NESTED)
1036 c for a parameter nside
1037 ==================================================
1038 */
1039 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
1040 // (16/12/98)
1041
1042 const PIXELS_XY& PXY= PIXELS_XY::instance();
1043
1044 int npix, npface, face_num;
1045 int ipf, ip_low, ip_trunc, ip_med, ip_hi;
1046 int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
1047 double z, fn, fact1, fact2;
1048 double piover2(Pi/2.);
1049 int ns_max(8192);
1050
1051 // ! coordinate of the lowest corner of each face
1052 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};//! in unit of nside
1053 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};// ! in unit of nside/2
1054
1055 if( nside<1 || nside>ns_max ) {
1056 cout << "nside out of range" << endl;
1057 exit(0);
1058 }
1059 npix = 12 * nside*nside;
1060 if( ipix<0 || ipix>npix-1 ) {
1061 cout << "ipix out of range" << endl;
1062 exit(0);
1063 }
1064
1065 fn = 1.*nside;
1066 fact1 = 1./(3.*fn*fn);
1067 fact2 = 2./(3.*fn);
1068 nl4 = 4*nside;
1069
1070 //c finds the face, and the number in the face
1071 npface = nside*nside;
1072
1073 face_num = ipix/npface;// ! face number in {0,11}
1074 ipf = (int)fmod(ipix,npface);// ! pixel number in the face {0,npface-1}
1075
1076 //c finds the x,y on the face (starting from the lowest corner)
1077 //c from the pixel number
1078 ip_low = (int)fmod(ipf,1024);// ! content of the last 10 bits
1079 ip_trunc = ipf/1024 ;// ! truncation of the last 10 bits
1080 ip_med = (int)fmod(ip_trunc,1024);// ! content of the next 10 bits
1081 ip_hi = ip_trunc/1024 ;//! content of the high weight 10 bits
1082
1083 ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
1084 iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
1085
1086 //c transforms this in (horizontal, vertical) coordinates
1087 jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
1088 jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
1089
1090 //c computes the z coordinate on the sphere
1091 // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
1092 jr = jrll[face_num]*nside - jrt - 1;
1093 nr = nside;// ! equatorial region (the most frequent)
1094 z = (2*nside-jr)*fact2;
1095 kshift = (int)fmod(jr - nside, 2);
1096 if( jr<nside ) { //then ! north pole region
1097 nr = jr;
1098 z = 1. - nr*nr*fact1;
1099 kshift = 0;
1100 }
1101 else {
1102 if( jr>3*nside ) {// then ! south pole region
1103 nr = nl4 - jr;
1104 z = - 1. + nr*nr*fact1;
1105 kshift = 0;
1106 }
1107 }
1108 theta = acos(z);
1109
1110 //c computes the phi coordinate on the sphere, in [0,2Pi]
1111 // jp = (jpll[face_num+1]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
1112 jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;
1113 if( jp>nl4 ) jp = jp - nl4;
1114 if( jp<1 ) jp = jp + nl4;
1115 phi = (jp - (kshift+1)*0.5) * (piover2 / nr);
1116}
1117
1118
1119
1120template <class T>
1121void SphereHEALPix<T>::print(ostream& os) const
1122{
1123 if(mInfo_) os << " DVList Info= " << *mInfo_ << endl;
1124 //
1125 os << " nSide_ = " << nSide_ << endl;
1126 os << " nPix_ = " << nPix_ << endl;
1127 os << " omeg_ = " << omeg_ << endl;
1128
1129 os << " content of pixels : ";
1130 for(int i=0; i < nPix_; i++)
1131 {
1132 if(i%5 == 0) os << endl;
1133 os << pixels_(i) <<", ";
1134 }
1135 os << endl;
1136
1137 os << endl;
1138 //const PIXELS_XY& PXY= PIXELS_XY::instance();
1139
1140 //os << endl; os << " contenu des tableaux conversions "<<endl;
1141 //for(int i=0; i < 5; i++)
1142 // {
1143 // os<<PXY.pix2x_(i)<<", "<<PXY.pix2y_(i)<<", "<<PXY.x2pix_(i)<<", "<<PXY.y2pix_(i)<<endl;
1144 // }
1145 os << endl;
1146
1147}
1148
1149
1150
1151//*******************************************************************
1152
1153#ifdef __CXX_PRAGMA_TEMPLATES__
1154#pragma define_template SphereHEALPix<uint_2>
1155#pragma define_template SphereHEALPix<r_8>
1156#pragma define_template SphereHEALPix<r_4>
1157#pragma define_template SphereHEALPix< complex<r_4> >
1158#pragma define_template SphereHEALPix< complex<r_8> >
1159#endif
1160#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
1161template class SphereHEALPix<uint_2>;
1162template class SphereHEALPix<r_8>;
1163template class SphereHEALPix<r_4>;
1164template class SphereHEALPix< complex<r_4> >;
1165template class SphereHEALPix< complex<r_8> >;
1166#endif
1167
Note: See TracBrowser for help on using the repository browser.