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

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

surcharge operateur =

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