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

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

ajout doc GLM

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