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

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

portage SGI-CC , redeclaration d'indice de boucle - Reza 20/4/2000

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