source: Sophya/trunk/SophyaLib/Samba/spheregorski.cc@ 574

Last change on this file since 574 was 574, checked in by ansari, 26 years ago

ajout arg share ds constructeurs, et methode ContainsSph ds localmap.cc et operateur = , Reza 12/11/99

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