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

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

Compilation Mac pour CodeWarrior PRO 5

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