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

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

Introduction de SpherePosition and SphereCoordSys, and Initiator for module Samba - Reza+I. Grivell 26/10/99

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