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

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

mise a jour de persistances par objets delegues

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