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

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

nouvelle gestion des thetaslices

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