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

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

divers nettoyages : const. de copie, surcharge = etc.

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 for (int sliceIndex=0; sliceIndex< nSide_-1; sliceIndex++)
464 {
465 sliceBeginIndex_(sliceIndex) = 2*sliceIndex*(sliceIndex+1);
466 sliceLenght_(sliceIndex) = 4*(sliceIndex+1);
467 }
468 for (int sliceIndex= nSide_-1; sliceIndex< 3*nSide_; sliceIndex++)
469 {
470 sliceBeginIndex_(sliceIndex) = 2*nSide_*(2*sliceIndex-nSide_+1);
471 sliceLenght_(sliceIndex) = 4*nSide_;
472 }
473 for (int sliceIndex= 3*nSide_; sliceIndex< 4*nSide_-1; sliceIndex++)
474 {
475 int_4 nc= 4*nSide_-1-sliceIndex;
476 sliceBeginIndex_(sliceIndex) = nPix_-2*nc*(nc+1);
477 sliceLenght_(sliceIndex) = 4*nc;
478 }
479}
480
481/* --Methode-- */
482//++
483template<class T>
484T& SphereHEALPix<T>::PixVal(int_4 k)
485
486// Return value of pixel with "RING" index k
487//--
488{
489 if((k < 0) || (k >= nPix_))
490 {
491 throw RangeCheckError("SphereHEALPix::PIxVal Pixel index out of range");
492 }
493 return pixels_(k);
494}
495
496/* --Methode-- */
497//++
498template<class T>
499T const& SphereHEALPix<T>::PixVal(int_4 k) const
500
501// Return value of pixel with "RING" index k
502//--
503{
504 if((k < 0) || (k >= nPix_))
505 {
506 throw RangeCheckError("SphereHEALPix::PIxVal Pixel index out of range");
507 }
508 return *(pixels_.Data()+k);
509}
510
511//++
512template<class T>
513T& SphereHEALPix<T>::PixValNest(int_4 k)
514
515// Return value of pixel with "NESTED" index k
516//--
517{
518 if((k < 0) || (k >= nPix_))
519 {
520 throw RangeCheckError("SphereHEALPix::PIxValNest Pixel index out of range");
521 }
522 return pixels_(nest2ring(nSide_,k));
523}
524//++
525
526template<class T>
527T const& SphereHEALPix<T>::PixValNest(int_4 k) const
528
529// Return value of pixel with "NESTED" index k
530//--
531{
532 if((k < 0) || (k >= nPix_))
533 {
534 throw RangeCheckError("SphereHEALPix::PIxValNest Pixel index out of range");
535 }
536 int_4 pix= nest2ring(nSide_,k);
537 return *(pixels_.Data()+pix);
538}
539
540/* --Methode-- */
541//++
542template<class T>
543bool SphereHEALPix<T>::ContainsSph(double /*theta*/, double /*phi*/) const
544//--
545{
546return(true);
547}
548
549/* --Methode-- */
550//++
551template<class T>
552int_4 SphereHEALPix<T>::PixIndexSph(double theta,double phi) const
553
554// Return "RING" index of the pixel corresponding to
555// direction (theta, phi).
556//--
557{
558 return ang2pix_ring(nSide_,theta,phi);
559}
560
561//++
562template<class T>
563int_4 SphereHEALPix<T>::PixIndexSphNest(double theta,double phi) const
564
565// Return "NESTED" index of the pixel corresponding to
566// direction (theta, phi).
567//--
568{
569 return ang2pix_nest(nSide_,theta,phi);
570}
571
572
573/* --Methode-- */
574//++
575template<class T>
576void SphereHEALPix<T>::PixThetaPhi(int_4 k,double& theta,double& phi) const
577
578// Return (theta,phi) coordinates of middle of pixel with "RING" index k
579//--
580{
581 pix2ang_ring(nSide_,k,theta,phi);
582}
583
584template <class T>
585T SphereHEALPix<T>::SetPixels(T v)
586{
587pixels_.Reset(v);
588return(v);
589}
590
591//++
592template<class T>
593double SphereHEALPix<T>::PixSolAngle(int_4 /*dummy*/) const
594// Pixel Solid angle (steradians)
595// All the pixels have the same solid angle. The dummy argument is
596// for compatibility with eventual pixelizations which would not
597// fulfil this requirement.
598//--
599{
600 return omeg_;
601}
602
603//++
604template<class T>
605void SphereHEALPix<T>::PixThetaPhiNest(int_4 k,double& theta,double& phi) const
606
607// Return (theta,phi) coordinates of middle of pixel with "NESTED" index k
608//--
609{
610 pix2ang_nest(nSide_,k,theta,phi);
611}
612
613//++
614template<class T>
615int_4 SphereHEALPix<T>::NestToRing(int_4 k) const
616
617// translation from NESTED index into RING index
618//
619//--
620{
621 return nest2ring(nSide_,k);
622}
623
624//++
625template<class T>
626int_4 SphereHEALPix<T>::RingToNest(int_4 k) const
627//
628// translation from RING index into NESTED index
629//
630//--
631{
632 return ring2nest(nSide_,k);
633}
634
635
636template<class T>
637int_4 SphereHEALPix<T>::nest2ring(int_4 nside, int_4 ipnest) const
638{
639 /*
640 ====================================================
641 subroutine nest2ring(nside, ipnest, ipring)
642 ====================================================
643 c conversion from NESTED to RING pixel number
644 ====================================================
645 */
646 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
647 // (16/12/98)
648
649 const PIXELS_XY& PXY= PIXELS_XY::instance();
650
651 int npix, npface, face_num, ncap, n_before;
652 int ipf, ip_low, ip_trunc, ip_med, ip_hi;
653 int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
654 int ns_max=8192;
655 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
656 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
657
658 if( nside<1 || nside>ns_max ) {
659 cout << "nside out of range" << endl;
660 exit(0);
661 }
662 npix = 12 * nside* nside;
663 if( ipnest<0 || ipnest>npix-1 ) {
664 cout << "ipnest out of range" << endl;
665 exit(0);
666 }
667
668 ncap = 2* nside*( nside-1);// ! number of points in the North Polar cap
669 nl4 = 4* nside;
670
671 //c finds the face, and the number in the face
672 npface = nside* nside;
673 //cccccc ip = ipnest - 1 ! in {0,npix-1}
674
675 face_num = ipnest/npface;// ! face number in {0,11}
676 ipf =ipnest%npface;// ! pixel number in the face {0,npface-1}
677 //c finds the x,y on the face (starting from the lowest corner)
678 //c from the pixel number
679 ip_low=ipf%1024; // ! content of the last 10 bits
680 ip_trunc = ipf/1024; // ! truncation of the last 10 bits
681 ip_med=ip_trunc%1024; // ! content of the next 10 bits
682 ip_hi = ip_trunc/1024;// ! content of the high weight 10 bits
683
684 ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
685 iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
686
687 //c transforms this in (horizontal, vertical) coordinates
688 jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
689 jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
690
691 //c computes the z coordinate on the sphere
692 // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
693 jr = jrll[face_num]*nside - jrt - 1;
694 nr = nside;// ! equatorial region (the most frequent)
695 n_before = ncap + nl4 * (jr - nside);
696 kshift=(jr - nside)%2;
697 if( jr<nside ) {//then ! north pole region
698 nr = jr;
699 n_before = 2 * nr * (nr - 1);
700 kshift = 0;
701 }
702 else if( jr>3*nside ) {//then ! south pole region
703 nr = nl4 - jr;
704 n_before = npix - 2 * (nr + 1) * nr;
705 kshift = 0;
706 }
707
708 //c computes the phi coordinate on the sphere, in [0,2Pi]
709 jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
710
711 if( jp>nl4 ) jp = jp - nl4;
712 if( jp<1 ) jp = jp + nl4;
713
714 int aux=n_before + jp - 1;
715 return (n_before + jp - 1);// ! in {0, npix-1}
716}
717
718template<class T>
719int_4 SphereHEALPix<T>::ring2nest(int_4 nside, int_4 ipring) const
720{
721 /*
722 ==================================================
723 subroutine ring2nest(nside, ipring, ipnest)
724 ==================================================
725 c conversion from RING to NESTED pixel number
726 ==================================================
727 */
728 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
729 // (16/12/98)
730
731 const PIXELS_XY& PXY= PIXELS_XY::instance();
732
733 double fihip, hip;
734 int npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1;
735 int kshift, face_num, nr;
736 int irn, ire, irm, irs, irt, ifm , ifp;
737 int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf;
738 int ns_max(8192);
739
740 // coordinate of the lowest corner of each face
741 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};// ! in unit of nside
742 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};//! in unit of nside/2
743
744 if( nside<1 || nside>ns_max ) {
745 cout << "nside out of range" << endl;
746 exit(0);
747 }
748 npix = 12 * nside*nside;
749 if( ipring<0 || ipring>npix-1 ) {
750 cout << "ipring out of range" << endl;
751 exit(0);
752 }
753
754 nl2 = 2*nside;
755 nl4 = 4*nside;
756 npix = 12*nside*nside;// ! total number of points
757 ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
758 ipring1 = ipring + 1;
759
760 //c finds the ring number, the position of the ring and the face number
761 if( ipring1<=ncap ) {//then
762
763 hip = ipring1/2.;
764 fihip = floor ( hip );
765 irn = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
766 iphi = ipring1 - 2*irn*(irn - 1);
767
768 kshift = 0;
769 nr = irn ;// ! 1/4 of the number of points on the current ring
770 face_num = (iphi-1) / irn;// ! in {0,3}
771 }
772 else if( ipring1<=nl2*(5*nside+1) ) {//then
773
774 ip = ipring1 - ncap - 1;
775 irn = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
776 iphi = (int)fmod(ip,nl4) + 1;
777
778 kshift = (int)fmod(irn+nside,2);// ! 1 if irn+nside is odd, 0 otherwise
779 nr = nside;
780 ire = irn - nside + 1;// ! in {1, 2*nside +1}
781 irm = nl2 + 2 - ire;
782 ifm = (iphi - ire/2 + nside -1) / nside;// ! face boundary
783 ifp = (iphi - irm/2 + nside -1) / nside;
784 if( ifp==ifm ) {//then ! faces 4 to 7
785 face_num = (int)fmod(ifp,4) + 4;
786 }
787 else if( ifp + 1==ifm ) {//then ! (half-)faces 0 to 3
788 face_num = ifp;
789 }
790 else if( ifp - 1==ifm ) {//then ! (half-)faces 8 to 11
791 face_num = ifp + 7;
792 }
793 }
794 else {
795
796 ip = npix - ipring1 + 1;
797 hip = ip/2.;
798 fihip = floor ( hip );
799 irs = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
800 iphi = 4*irs + 1 - (ip - 2*irs*(irs-1));
801
802 kshift = 0;
803 nr = irs;
804 irn = nl4 - irs;
805 face_num = (iphi-1) / irs + 8;// ! in {8,11}
806 }
807
808 //c finds the (x,y) on the face
809 irt = irn - jrll[face_num]*nside + 1;// ! in {-nside+1,0}
810 ipt = 2*iphi - jpll[face_num]*nr - kshift - 1;// ! in {-nside+1,nside-1}
811
812
813 if( ipt>=nl2 ) ipt = ipt - 8*nside;// ! for the face #4
814
815 ix = (ipt - irt ) / 2;
816 iy = -(ipt + irt ) / 2;
817
818 ix_low = (int)fmod(ix,128);
819 ix_hi = ix/128;
820 iy_low = (int)fmod(iy,128);
821 iy_hi = iy/128;
822 ipf=(PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
823
824 return (ipf + face_num* nside *nside);// ! in {0, 12*nside**2 - 1}
825}
826
827template<class T>
828int_4 SphereHEALPix<T>::ang2pix_ring(int_4 nside, double theta, double phi) const
829{
830 /*
831 ==================================================
832 c gives the pixel number ipix (RING)
833 c corresponding to angles theta and phi
834 c==================================================
835 */
836 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
837 // (16/12/98)
838
839 int nl2, nl4, ncap, npix, jp, jm, ipix1;
840 double z, za, tt, tp, tmp;
841 int ir, ip, kshift;
842
843 double piover2(Pi/2.);
844 double twopi(2.*Pi);
845 double z0(2./3.);
846 int ns_max(8192);
847
848 if( nside<1 || nside>ns_max ) {
849 cout << "nside out of range" << endl;
850 exit(0);
851 }
852
853 if( theta<0. || theta>Pi) {
854 cout << "theta out of range" << endl;
855 exit(0);
856 }
857
858 z = cos(theta);
859 za = fabs(z);
860 if( phi >= twopi) phi = phi - twopi;
861 if (phi < 0.) phi = phi + twopi;
862 tt = phi / piover2;// ! in [0,4)
863
864 nl2 = 2*nside;
865 nl4 = 4*nside;
866 ncap = nl2*(nside-1);// ! number of pixels in the north polar cap
867 npix = 12*nside*nside;
868
869 if( za <= z0 ) {
870
871 jp = (int)floor(nside*(0.5 + tt - z*0.75));// ! index of ascending edge line
872 jm = (int)floor(nside*(0.5 + tt + z*0.75));// ! index of descending edge line
873
874 ir = nside + 1 + jp - jm;// ! in {1,2n+1} (ring number counted from z=2/3)
875 kshift = 0;
876 if (fmod(ir,2)==0.) kshift = 1;// ! kshift=1 if ir even, 0 otherwise
877
878 ip = (int)floor( ( jp+jm - nside + kshift + 1 ) / 2 ) + 1;// ! in {1,4n}
879 if( ip>nl4 ) ip = ip - nl4;
880
881 ipix1 = ncap + nl4*(ir-1) + ip ;
882 }
883 else {
884
885 tp = tt - floor(tt);// !MOD(tt,1.d0)
886 tmp = sqrt( 3.*(1. - za) );
887
888 jp = (int)floor( nside * tp * tmp );// ! increasing edge line index
889 jm = (int)floor( nside * (1. - tp) * tmp );// ! decreasing edge line index
890
891 ir = jp + jm + 1;// ! ring number counted from the closest pole
892 ip = (int)floor( tt * ir ) + 1;// ! in {1,4*ir}
893 if( ip>4*ir ) ip = ip - 4*ir;
894
895 ipix1 = 2*ir*(ir-1) + ip;
896 if( z<=0. ) {
897 ipix1 = npix - 2*ir*(ir+1) + ip;
898 }
899 }
900 return (ipix1 - 1);// ! in {0, npix-1}
901}
902
903template<class T>
904int_4 SphereHEALPix<T>::ang2pix_nest(int_4 nside, double theta, double phi) const
905{
906 /*
907 ==================================================
908 subroutine ang2pix_nest(nside, theta, phi, ipix)
909 ==================================================
910 c gives the pixel number ipix (NESTED)
911 c corresponding to angles theta and phi
912 c
913 c the computation is made to the highest resolution available (nside=8192)
914 c and then degraded to that required (by integer division)
915 c this doesn't cost more, and it makes sure
916 c that the treatement of round-off will be consistent
917 c for every resolution
918 ==================================================
919 */
920 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
921 // (16/12/98)
922
923 const PIXELS_XY& PXY= PIXELS_XY::instance();
924
925 double z, za, z0, tt, tp, tmp;
926 int face_num,jp,jm;
927 int ifp, ifm;
928 int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf, ntt;
929 double piover2(Pi/2.), twopi(2.*Pi);
930 int ns_max(8192);
931
932 if( nside<1 || nside>ns_max ) {
933 cout << "nside out of range" << endl;
934 exit(0);
935 }
936 if( theta<0 || theta>Pi ) {
937 cout << "theta out of range" << endl;
938 exit(0);
939 }
940 z = cos(theta);
941 za = fabs(z);
942 z0 = 2./3.;
943 if( phi>=twopi ) phi = phi - twopi;
944 if( phi<0. ) phi = phi + twopi;
945 tt = phi / piover2;// ! in [0,4[
946 if( za<=z0 ) { // then ! equatorial region
947
948 //(the index of edge lines increase when the longitude=phi goes up)
949 jp = (int)floor(ns_max*(0.5 + tt - z*0.75));// ! ascending edge line index
950 jm = (int)floor(ns_max*(0.5 + tt + z*0.75));// ! descending edge line index
951
952 //c finds the face
953 ifp = jp / ns_max;// ! in {0,4}
954 ifm = jm / ns_max;
955 if( ifp==ifm ) face_num = (int)fmod(ifp,4) + 4; //then ! faces 4 to 7
956 else if( ifp<ifm ) face_num = (int)fmod(ifp,4); // (half-)faces 0 to 3
957 else face_num = (int)fmod(ifm,4) + 8;//! (half-)faces 8 to 11
958
959 ix = (int)fmod(jm, ns_max);
960 iy = ns_max - (int)fmod(jp, ns_max) - 1;
961 }
962 else { //! polar region, za > 2/3
963
964 ntt = (int)floor(tt);
965 if( ntt>=4 ) ntt = 3;
966 tp = tt - ntt;
967 tmp = sqrt( 3.*(1. - za) );// ! in ]0,1]
968
969 //(the index of edge lines increase when distance from the closest pole goes up)
970 jp = (int)floor(ns_max*tp*tmp); // ! line going toward the pole as phi increases
971 jm = (int)floor(ns_max*(1.-tp)*tmp); // ! that one goes away of the closest pole
972 jp = (int)min(ns_max-1, jp);// ! for points too close to the boundary
973 jm = (int)min(ns_max-1, jm);
974
975 // finds the face and pixel's (x,y)
976 if( z>=0 ) {
977 face_num = ntt;// ! in {0,3}
978 ix = ns_max - jm - 1;
979 iy = ns_max - jp - 1;
980 }
981 else {
982 face_num = ntt + 8;// ! in {8,11}
983 ix = jp;
984 iy = jm;
985 }
986 }
987
988 ix_low = (int)fmod(ix,128);
989 ix_hi = ix/128;
990 iy_low = (int)fmod(iy,128);
991 iy_hi = iy/128;
992 ipf= (PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
993 // ipf = ipf / pow(ns_max/nside,2.);// ! in {0, nside**2 - 1}
994 // return ( ipf + face_num*pow(nside,2));// ! in {0, 12*nside**2 - 1}
995 // $CHECK$ Reza 25/10/99 , pow remplace par *
996 ipf = ipf / ((ns_max/nside)*(ns_max/nside));
997 return (ipf + face_num*nside*nside);
998}
999
1000template<class T>
1001void SphereHEALPix<T>::pix2ang_ring(int_4 nside,int_4 ipix,double& theta,double& phi) const {
1002 /*
1003 ===================================================
1004 c gives theta and phi corresponding to pixel ipix (RING)
1005 c for a parameter nside
1006 ===================================================
1007 */
1008 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
1009 // (16/12/98)
1010
1011 int nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
1012 double fact1, fact2, fodd, hip, fihip;
1013
1014 int ns_max(8192);
1015
1016 if( nside<1 || nside>ns_max ) {
1017 cout << "nside out of range" << endl;
1018 exit(0);
1019 }
1020 npix = 12*nside*nside; // ! total number of points
1021 if( ipix<0 || ipix>npix-1 ) {
1022 cout << "ipix out of range" << endl;
1023 exit(0);
1024 }
1025
1026 ipix1 = ipix + 1; // in {1, npix}
1027 nl2 = 2*nside;
1028 nl4 = 4*nside;
1029 ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
1030 fact1 = 1.5*nside;
1031 fact2 = 3.0*nside*nside;
1032
1033 if( ipix1 <= ncap ) { //! North Polar cap -------------
1034
1035 hip = ipix1/2.;
1036 fihip = floor(hip);
1037 iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
1038 iphi = ipix1 - 2*iring*(iring - 1);
1039
1040 theta = acos( 1. - iring*iring / fact2 );
1041 phi = ((double)iphi - 0.5) * Pi/(2.*iring);
1042 // cout << theta << " " << phi << endl;
1043 }
1044 else if( ipix1 <= nl2*(5*nside+1) ) {//then ! Equatorial region ------
1045
1046 ip = ipix1 - ncap - 1;
1047 iring = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
1048 iphi = ip%nl4 + 1;
1049
1050 fodd = 0.5 * (1 + (iring+nside)%2 );// ! 1 if iring+nside is odd, 1/2 otherwise
1051 theta = acos( (nl2 - iring) / fact1 );
1052 phi = ((double)iphi - fodd) * Pi /(2.*nside);
1053 }
1054 else {//! South Polar cap -----------------------------------
1055
1056 ip = npix - ipix1 + 1;
1057 hip = ip/2.;
1058 fihip = floor(hip);
1059 iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
1060 iphi = (int)(4.*iring + 1 - (ip - 2.*iring*(iring-1)));
1061
1062 theta = acos( -1. + iring*iring / fact2 );
1063 phi = ((double)iphi - 0.5) * Pi/(2.*iring);
1064 // cout << theta << " " << phi << endl;
1065 }
1066}
1067
1068template<class T>
1069void SphereHEALPix<T>::pix2ang_nest(int_4 nside,int_4 ipix,double& theta,double& phi) const {
1070 /*
1071 ==================================================
1072 subroutine pix2ang_nest(nside, ipix, theta, phi)
1073 ==================================================
1074 c gives theta and phi corresponding to pixel ipix (NESTED)
1075 c for a parameter nside
1076 ==================================================
1077 */
1078 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
1079 // (16/12/98)
1080
1081 const PIXELS_XY& PXY= PIXELS_XY::instance();
1082
1083 int npix, npface, face_num;
1084 int ipf, ip_low, ip_trunc, ip_med, ip_hi;
1085 int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
1086 double z, fn, fact1, fact2;
1087 double piover2(Pi/2.);
1088 int ns_max(8192);
1089
1090 // ! coordinate of the lowest corner of each face
1091 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};//! in unit of nside
1092 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};// ! in unit of nside/2
1093
1094 if( nside<1 || nside>ns_max ) {
1095 cout << "nside out of range" << endl;
1096 exit(0);
1097 }
1098 npix = 12 * nside*nside;
1099 if( ipix<0 || ipix>npix-1 ) {
1100 cout << "ipix out of range" << endl;
1101 exit(0);
1102 }
1103
1104 fn = 1.*nside;
1105 fact1 = 1./(3.*fn*fn);
1106 fact2 = 2./(3.*fn);
1107 nl4 = 4*nside;
1108
1109 //c finds the face, and the number in the face
1110 npface = nside*nside;
1111
1112 face_num = ipix/npface;// ! face number in {0,11}
1113 ipf = (int)fmod(ipix,npface);// ! pixel number in the face {0,npface-1}
1114
1115 //c finds the x,y on the face (starting from the lowest corner)
1116 //c from the pixel number
1117 ip_low = (int)fmod(ipf,1024);// ! content of the last 10 bits
1118 ip_trunc = ipf/1024 ;// ! truncation of the last 10 bits
1119 ip_med = (int)fmod(ip_trunc,1024);// ! content of the next 10 bits
1120 ip_hi = ip_trunc/1024 ;//! content of the high weight 10 bits
1121
1122 ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
1123 iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
1124
1125 //c transforms this in (horizontal, vertical) coordinates
1126 jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
1127 jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
1128
1129 //c computes the z coordinate on the sphere
1130 // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
1131 jr = jrll[face_num]*nside - jrt - 1;
1132 nr = nside;// ! equatorial region (the most frequent)
1133 z = (2*nside-jr)*fact2;
1134 kshift = (int)fmod(jr - nside, 2);
1135 if( jr<nside ) { //then ! north pole region
1136 nr = jr;
1137 z = 1. - nr*nr*fact1;
1138 kshift = 0;
1139 }
1140 else {
1141 if( jr>3*nside ) {// then ! south pole region
1142 nr = nl4 - jr;
1143 z = - 1. + nr*nr*fact1;
1144 kshift = 0;
1145 }
1146 }
1147 theta = acos(z);
1148
1149 //c computes the phi coordinate on the sphere, in [0,2Pi]
1150 // jp = (jpll[face_num+1]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
1151 jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;
1152 if( jp>nl4 ) jp = jp - nl4;
1153 if( jp<1 ) jp = jp + nl4;
1154 phi = (jp - (kshift+1)*0.5) * (piover2 / nr);
1155}
1156
1157
1158
1159template <class T>
1160void SphereHEALPix<T>::print(ostream& os) const
1161{
1162 if(mInfo_) os << " DVList Info= " << *mInfo_ << endl;
1163 //
1164 os << " nSide_ = " << nSide_ << endl;
1165 os << " nPix_ = " << nPix_ << endl;
1166 os << " omeg_ = " << omeg_ << endl;
1167
1168 os << " content of pixels : ";
1169 for(int i=0; i < nPix_; i++)
1170 {
1171 if(i%5 == 0) os << endl;
1172 os << pixels_(i) <<", ";
1173 }
1174 os << endl;
1175
1176 os << endl;
1177 //const PIXELS_XY& PXY= PIXELS_XY::instance();
1178
1179 //os << endl; os << " contenu des tableaux conversions "<<endl;
1180 //for(int i=0; i < 5; i++)
1181 // {
1182 // os<<PXY.pix2x_(i)<<", "<<PXY.pix2y_(i)<<", "<<PXY.x2pix_(i)<<", "<<PXY.y2pix_(i)<<endl;
1183 // }
1184 os << endl;
1185
1186}
1187
1188
1189
1190//*******************************************************************
1191
1192#ifdef __CXX_PRAGMA_TEMPLATES__
1193#pragma define_template SphereHEALPix<uint_2>
1194#pragma define_template SphereHEALPix<r_8>
1195#pragma define_template SphereHEALPix<r_4>
1196#pragma define_template SphereHEALPix< complex<r_4> >
1197#pragma define_template SphereHEALPix< complex<r_8> >
1198#endif
1199#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
1200template class SphereHEALPix<uint_2>;
1201template class SphereHEALPix<r_8>;
1202template class SphereHEALPix<r_4>;
1203template class SphereHEALPix< complex<r_4> >;
1204template class SphereHEALPix< complex<r_8> >;
1205#endif
1206
Note: See TracBrowser for help on using the repository browser.