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

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

modifs constructeurs copie et operateur =

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