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

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