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

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

correction de la traduction en c++ de l'interface avec la pixelisation Gorski (pix2ang_ring)

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