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

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

constructeurs de copie

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