source: Sophya/trunk/SophyaLib/SkyMap/spheregorski.cc@ 824

Last change on this file since 824 was 809, checked in by ansari, 25 years ago

Adaptation modifs TArray<T>,PPersist - Reza 03/04/2000

File size: 35.5 KB
Line 
1#include "machdefs.h"
2#include <math.h>
3#include <complex>
4
5#include "pexceptions.h"
6#include "fiondblock.h"
7#include "spheregorski.h"
8#include "strutil.h"
9
10extern "C"
11{
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15}
16
17
18//*******************************************************************
19// Class PIXELS_XY
20// Construction des tableaux necessaires a la traduction des indices RING en
21// indices NESTED (ou l'inverse)
22//*******************************************************************
23
24PIXELS_XY::PIXELS_XY()
25{
26 pix2x_.ReSize(1024);
27 pix2x_.Reset();
28 pix2y_.ReSize(1024);
29 pix2y_.Reset();
30 x2pix_.ReSize(128);
31 x2pix_.Reset();
32 y2pix_.ReSize(128);
33 y2pix_.Reset();
34 mk_pix2xy();
35 mk_xy2pix();
36}
37
38PIXELS_XY& PIXELS_XY::instance()
39{
40 static PIXELS_XY single;
41 return (single);
42}
43
44void PIXELS_XY::mk_pix2xy()
45{
46 /*
47 ==================================================
48 subroutine mk_pix2xy
49 ==================================================
50 c constructs the array giving x and y in the face from pixel number
51 c for the nested (quad-cube like) ordering of pixels
52 c
53 c the bits corresponding to x and y are interleaved in the pixel number
54 c one breaks up the pixel number by even and odd bits
55 ==================================================
56 */
57 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
58 // (16/12/98)
59
60 int kpix, jpix, IX, IY, IP, ID;
61
62 for(kpix = 0; kpix < 1024; kpix++)
63 {
64 jpix = kpix;
65 IX = 0;
66 IY = 0;
67 IP = 1 ;// ! bit position (in x and y)
68 while( jpix!=0 )
69 { // ! go through all the bits
70 ID=jpix%2;// ! bit value (in kpix), goes in ix
71 jpix = jpix/2;
72 IX = ID*IP+IX;
73
74 ID=jpix%2;// ! bit value (in kpix), goes in iy
75 jpix = jpix/2;
76 IY = ID*IP+IY;
77
78 IP = 2*IP;// ! next bit (in x and y)
79 }
80 pix2x_(kpix) = IX;// ! in 0,31
81 pix2y_(kpix) = IY;// ! in 0,31
82 }
83}
84
85void PIXELS_XY::mk_xy2pix()
86{
87 /*
88 =================================================
89 subroutine mk_xy2pix
90 =================================================
91 c sets the array giving the number of the pixel lying in (x,y)
92 c x and y are in {1,128}
93 c the pixel number is in {0,128**2-1}
94 c
95 c if i-1 = sum_p=0 b_p * 2^p
96 c then ix = sum_p=0 b_p * 4^p
97 c iy = 2*ix
98 c ix + iy in {0, 128**2 -1}
99 =================================================
100 */
101 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
102 // (16/12/98)
103
104 int K,IP,I,J,ID;
105 for(I = 1; I <= 128; I++)
106 {
107 J = I-1;// !pixel numbers
108 K = 0;//
109 IP = 1;//
110 truc : if( J==0 )
111 {
112 x2pix_(I-1) = K;
113 y2pix_(I-1) = 2*K;
114 }
115 else
116 {
117 ID = (int)fmod(J,2);
118 J = J/2;
119 K = IP*ID+K;
120 IP = IP*4;
121 goto truc;
122 }
123 }
124}
125
126//*******************************************************************
127//++
128// Class SphereGorski
129//
130// include spheregorski.h strutil.h
131//
132// Pixelisation Gorski
133//
134//
135//| -----------------------------------------------------------------------
136//| version 0.8.2 Aug97 TAC Eric Hivon, Kris Gorski
137//| -----------------------------------------------------------------------
138//
139// the sphere is split in 12 diamond-faces containing nside**2 pixels each
140//
141// the numbering of the pixels (in the nested scheme) is similar to
142// quad-cube
143// In each face the first pixel is in the lowest corner of the diamond
144//
145// the faces are (x,y) coordinate on each face
146//| . . . . <--- North Pole
147//| / \ / \ / \ / \ ^ ^
148//| . 0 . 1 . 2 . 3 . <--- z = 2/3 \ /
149//| \ / \ / \ / \ / y \ / x
150//| 4 . 5 . 6 . 7 . 4 <--- equator \ /
151//| / \ / \ / \ / \ \/
152//| . 8 . 9 .10 .11 . <--- z = -2/3 (0,0) : lowest corner
153//| \ / \ / \ / \ /
154//| . . . . <--- South Pole
155//|
156// phi:0 2Pi
157//
158// in the ring scheme pixels are numbered along the parallels
159// the first parallel is the one closest to the north pole and so on
160// on each parallel, pixels are numbered starting from the one closest
161// to phi = 0
162//
163// nside MUST be a power of 2 (<= 8192)
164//--
165//++
166//
167// Links Parents
168//
169// SphericalMap
170//--
171
172/* --Methode-- */
173//++
174// Titre Constructors
175//--
176//++
177
178template<class T>
179SphereGorski<T>::SphereGorski()
180
181//--
182{
183 InitNul();
184 pixels_.Reset();
185}
186
187//++
188template<class T>
189SphereGorski<T>::SphereGorski(int_4 m)
190
191// m is the "nside" of the Gorski algorithm
192//
193// The total number of pixels will be Npix = 12*nside**2
194//
195// nside MUST be a power of 2 (<= 8192)
196//--
197{
198
199 if(m <= 0 || m > 8192)
200 {
201 cout << "SphereGorski : m hors bornes [0,8192], m= " << m << endl;
202 throw RangeCheckError("SphereGorski<T>::SphereGorski() - Out of bound nside (< 8192)!");
203 }
204 // verifier que m est une puissance de deux
205 int x= m;
206 while(x%2 == 0) x/=2;
207 if(x != 1)
208 {
209 cout<<"SphereGorski: m doit etre une puissance de deux, m= "<<m<<endl;
210 throw ParmError("SphereGorski<T>::SphereGorski() - nside != 2^n !");
211 }
212 InitNul();
213 Pixelize(m);
214 SetThetaSlices();
215}
216//++
217template<class T>
218SphereGorski<T>::SphereGorski(const SphereGorski<T>& s, bool share)
219 : pixels_(s.pixels_, share)
220// copy constructor
221//--
222{
223 if(s.mInfo_) mInfo_= new DVList(*s.mInfo_);
224
225 nSide_= s.nSide_;
226 nPix_ = s.nPix_;
227 omeg_ = s.omeg_;
228}
229
230//++
231// Titre Destructor
232//--
233//++
234template<class T>
235SphereGorski<T>::~SphereGorski()
236
237//--
238{
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 SetThetaSlices();
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>
325uint_4 SphereGorski<T>::NbThetaSlices() const
326
327// Return number of slices in theta direction on the sphere
328//--
329{
330 uint_4 nbslices = uint_4(4*nSide_-1);
331 if (nSide_<=0)
332 {
333 nbslices = 0;
334 throw PException(" sphere not pixelized, NbSlice=0 ");
335 }
336 return nbslices;
337}
338
339//++
340template<class T>
341void SphereGorski<T>::GetThetaSlice(int_4 index, r_8& theta,TVector<r_8>& phi,TVector<T>& value) const
342
343// For a theta-slice with index 'index', return :
344//
345// the corresponding "theta"
346//
347// a vector containing the phi's of the pixels of the slice
348//
349// a vector containing the corresponding values of pixels
350//
351//--
352{
353
354 if (index<0 || index >= NbThetaSlices())
355 {
356 // THROW(out_of_range("SphereGorski::PIxVal Pixel index out of range"));
357 cout << " SphereGorski::GetThetaSlice : Pixel index out of range" <<endl;
358 THROW(rangeCheckErr);
359 }
360
361
362 int_4 iring= sliceBeginIndex_(index);
363 int_4 lring = sliceLenght_(index);
364
365
366 phi.ReSize(lring);
367 value.ReSize(lring);
368 double TH= 0.;
369 double FI= 0.;
370 for(int_4 kk = 0; kk < lring;kk++)
371 {
372 PixThetaPhi(kk+iring,TH,FI);
373 phi(kk)= FI;
374 value(kk)= PixVal(kk+iring);
375 }
376 theta= TH;
377}
378//++
379//++
380
381template<class T>
382void SphereGorski<T>::GetThetaSlice(int_4 sliceIndex,r_8& theta, r_8& phi0, TVector<int_4>& pixelIndices,TVector<T>& value) const
383
384// For a theta-slice with index 'sliceIndex', return :
385//
386// the corresponding "theta"
387// the corresponding "phi" for first pixel of the slice
388//
389// a vector containing the indices of the pixels of the slice
390// (equally distributed in phi)
391//
392// a vector containing the corresponding values of pixels
393//
394//--
395{
396
397 if (sliceIndex<0 || sliceIndex >= NbThetaSlices())
398 {
399 // THROW(out_of_range("SphereGorski::PIxVal Pixel index out of range"));
400 cout << " SphereGorski::GetThetaSlice : Pixel index out of range" <<endl;
401 THROW(rangeCheckErr);
402 }
403
404 int_4 iring= sliceBeginIndex_(sliceIndex);
405 int_4 lring = sliceLenght_(sliceIndex);
406 pixelIndices.ReSize(lring);
407 value.ReSize(lring);
408 for(int_4 kk = 0; kk < lring;kk++)
409 {
410 pixelIndices(kk)= kk+iring;
411 value(kk)= PixVal(kk+iring);
412 }
413 PixThetaPhi(iring, theta, phi0);
414}
415//++
416template<class T>
417void SphereGorski<T>::SetThetaSlices()
418
419//--
420{
421 sliceBeginIndex_.ReSize(4*nSide_-1);
422 sliceLenght_.ReSize(4*nSide_-1);
423 for (int sliceIndex=0; sliceIndex< nSide_-1; sliceIndex++)
424 {
425 sliceBeginIndex_(sliceIndex) = 2*sliceIndex*(sliceIndex+1);
426 sliceLenght_(sliceIndex) = 4*(sliceIndex+1);
427 }
428 for (int sliceIndex= nSide_-1; sliceIndex< 3*nSide_; sliceIndex++)
429 {
430 sliceBeginIndex_(sliceIndex) = 2*nSide_*(2*sliceIndex-nSide_+1);
431 sliceLenght_(sliceIndex) = 4*nSide_;
432 }
433 for (int sliceIndex= 3*nSide_; sliceIndex< 4*nSide_-1; sliceIndex++)
434 {
435 int_4 nc= 4*nSide_-1-sliceIndex;
436 sliceBeginIndex_(sliceIndex) = nPix_-2*nc*(nc+1);
437 sliceLenght_(sliceIndex) = 4*nc;
438 }
439}
440
441/* --Methode-- */
442//++
443template<class T>
444T& SphereGorski<T>::PixVal(int_4 k)
445
446// Return value of pixel with "RING" index k
447//--
448{
449 if((k < 0) || (k >= nPix_))
450 {
451 // THROW(out_of_range("SphereGorski::PIxVal Pixel index out of range"));
452 cout << " SphereGorski::PIxVal : exception a mettre en place, k= " << k <<endl;
453 THROW(rangeCheckErr);
454 }
455 return pixels_(k);
456}
457
458/* --Methode-- */
459//++
460template<class T>
461T const& SphereGorski<T>::PixVal(int_4 k) const
462
463// Return value of pixel with "RING" index k
464//--
465{
466 if((k < 0) || (k >= nPix_))
467 {
468 //THROW(out_of_range("SphereGorski::PIxVal Pixel index out of range"));
469 cout << " SphereGorski::PIxVal : exception a mettre en place, k= " << k <<endl;
470 THROW(rangeCheckErr);
471 }
472 return *(pixels_.Data()+k);
473}
474
475//++
476template<class T>
477T& SphereGorski<T>::PixValNest(int_4 k)
478
479// Return value of pixel with "NESTED" index k
480//--
481{
482 if((k < 0) || (k >= nPix_))
483 {
484 //THROW(out_of_range("SphereGorski::PIxValNest Pixel index out of range"));
485 cout<<" SphereGorski::PIxValNest : exceptions a mettre en place" <<endl;
486 THROW(rangeCheckErr);
487 }
488 return pixels_(nest2ring(nSide_,k));
489}
490//++
491
492template<class T>
493T const& SphereGorski<T>::PixValNest(int_4 k) const
494
495// Return value of pixel with "NESTED" index k
496//--
497{
498 if((k < 0) || (k >= nPix_))
499 {
500 //THROW(out_of_range("SphereGorski::PIxValNest Pixel index out of range"));
501 cout<<" SphereGorski::PIxValNest : exceptions a mettre en place" <<endl;
502 THROW(rangeCheckErr);
503 }
504 int_4 pix= nest2ring(nSide_,k);
505 return *(pixels_.Data()+pix);
506}
507
508/* --Methode-- */
509//++
510template<class T>
511bool SphereGorski<T>::ContainsSph(double /*theta*/, double /*phi*/) const
512//--
513{
514return(true);
515}
516
517/* --Methode-- */
518//++
519template<class T>
520int_4 SphereGorski<T>::PixIndexSph(double theta,double phi) const
521
522// Return "RING" index of the pixel corresponding to
523// direction (theta, phi).
524//--
525{
526 return ang2pix_ring(nSide_,theta,phi);
527}
528
529//++
530template<class T>
531int_4 SphereGorski<T>::PixIndexSphNest(double theta,double phi) const
532
533// Return "NESTED" index of the pixel corresponding to
534// direction (theta, phi).
535//--
536{
537 return ang2pix_nest(nSide_,theta,phi);
538}
539
540
541/* --Methode-- */
542//++
543template<class T>
544void SphereGorski<T>::PixThetaPhi(int_4 k,double& theta,double& phi) const
545
546// Return (theta,phi) coordinates of middle of pixel with "RING" index k
547//--
548{
549 pix2ang_ring(nSide_,k,theta,phi);
550}
551
552template <class T>
553T SphereGorski<T>::SetPixels(T v)
554{
555pixels_.Reset(v);
556return(v);
557}
558
559//++
560template<class T>
561double SphereGorski<T>::PixSolAngle(int_4 /*dummy*/) const
562// Pixel Solid angle (steradians)
563// All the pixels have the same solid angle. The dummy argument is
564// for compatibility with eventual pixelizations which would not
565// fulfil this requirement.
566//--
567{
568 return omeg_;
569}
570
571//++
572template<class T>
573void SphereGorski<T>::PixThetaPhiNest(int_4 k,double& theta,double& phi) const
574
575// Return (theta,phi) coordinates of middle of pixel with "NESTED" index k
576//--
577{
578 pix2ang_nest(nSide_,k,theta,phi);
579}
580
581//++
582template<class T>
583int_4 SphereGorski<T>::NestToRing(int_4 k) const
584
585// translation from NESTED index into RING index
586//
587//--
588{
589 return nest2ring(nSide_,k);
590}
591
592//++
593template<class T>
594int_4 SphereGorski<T>::RingToNest(int_4 k) const
595//
596// translation from RING index into NESTED index
597//
598//--
599{
600 return ring2nest(nSide_,k);
601}
602
603
604template<class T>
605int_4 SphereGorski<T>::nest2ring(int_4 nside, int_4 ipnest) const
606{
607 /*
608 ====================================================
609 subroutine nest2ring(nside, ipnest, ipring)
610 ====================================================
611 c conversion from NESTED to RING pixel number
612 ====================================================
613 */
614 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
615 // (16/12/98)
616
617 const PIXELS_XY& PXY= PIXELS_XY::instance();
618
619 int npix, npface, face_num, ncap, n_before;
620 int ipf, ip_low, ip_trunc, ip_med, ip_hi;
621 int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
622 int ns_max=8192;
623 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
624 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
625
626 if( nside<1 || nside>ns_max ) {
627 cout << "nside out of range" << endl;
628 exit(0);
629 }
630 npix = 12 * nside* nside;
631 if( ipnest<0 || ipnest>npix-1 ) {
632 cout << "ipnest out of range" << endl;
633 exit(0);
634 }
635
636 ncap = 2* nside*( nside-1);// ! number of points in the North Polar cap
637 nl4 = 4* nside;
638
639 //c finds the face, and the number in the face
640 npface = nside* nside;
641 //cccccc ip = ipnest - 1 ! in {0,npix-1}
642
643 face_num = ipnest/npface;// ! face number in {0,11}
644 ipf =ipnest%npface;// ! pixel number in the face {0,npface-1}
645 //c finds the x,y on the face (starting from the lowest corner)
646 //c from the pixel number
647 ip_low=ipf%1024; // ! content of the last 10 bits
648 ip_trunc = ipf/1024; // ! truncation of the last 10 bits
649 ip_med=ip_trunc%1024; // ! content of the next 10 bits
650 ip_hi = ip_trunc/1024;// ! content of the high weight 10 bits
651
652 ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
653 iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
654
655 //c transforms this in (horizontal, vertical) coordinates
656 jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
657 jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
658
659 //c computes the z coordinate on the sphere
660 // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
661 jr = jrll[face_num]*nside - jrt - 1;
662 nr = nside;// ! equatorial region (the most frequent)
663 n_before = ncap + nl4 * (jr - nside);
664 kshift=(jr - nside)%2;
665 if( jr<nside ) {//then ! north pole region
666 nr = jr;
667 n_before = 2 * nr * (nr - 1);
668 kshift = 0;
669 }
670 else if( jr>3*nside ) {//then ! south pole region
671 nr = nl4 - jr;
672 n_before = npix - 2 * (nr + 1) * nr;
673 kshift = 0;
674 }
675
676 //c computes the phi coordinate on the sphere, in [0,2Pi]
677 jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
678
679 if( jp>nl4 ) jp = jp - nl4;
680 if( jp<1 ) jp = jp + nl4;
681
682 int aux=n_before + jp - 1;
683 return (n_before + jp - 1);// ! in {0, npix-1}
684}
685
686template<class T>
687int_4 SphereGorski<T>::ring2nest(int_4 nside, int_4 ipring) const
688{
689 /*
690 ==================================================
691 subroutine ring2nest(nside, ipring, ipnest)
692 ==================================================
693 c conversion from RING to NESTED pixel number
694 ==================================================
695 */
696 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
697 // (16/12/98)
698
699 const PIXELS_XY& PXY= PIXELS_XY::instance();
700
701 double fihip, hip;
702 int npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1;
703 int kshift, face_num, nr;
704 int irn, ire, irm, irs, irt, ifm , ifp;
705 int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf;
706 int ns_max(8192);
707
708 // coordinate of the lowest corner of each face
709 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};// ! in unit of nside
710 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};//! in unit of nside/2
711
712 if( nside<1 || nside>ns_max ) {
713 cout << "nside out of range" << endl;
714 exit(0);
715 }
716 npix = 12 * nside*nside;
717 if( ipring<0 || ipring>npix-1 ) {
718 cout << "ipring out of range" << endl;
719 exit(0);
720 }
721
722 nl2 = 2*nside;
723 nl4 = 4*nside;
724 npix = 12*nside*nside;// ! total number of points
725 ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
726 ipring1 = ipring + 1;
727
728 //c finds the ring number, the position of the ring and the face number
729 if( ipring1<=ncap ) {//then
730
731 hip = ipring1/2.;
732 fihip = floor ( hip );
733 irn = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
734 iphi = ipring1 - 2*irn*(irn - 1);
735
736 kshift = 0;
737 nr = irn ;// ! 1/4 of the number of points on the current ring
738 face_num = (iphi-1) / irn;// ! in {0,3}
739 }
740 else if( ipring1<=nl2*(5*nside+1) ) {//then
741
742 ip = ipring1 - ncap - 1;
743 irn = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
744 iphi = (int)fmod(ip,nl4) + 1;
745
746 kshift = (int)fmod(irn+nside,2);// ! 1 if irn+nside is odd, 0 otherwise
747 nr = nside;
748 ire = irn - nside + 1;// ! in {1, 2*nside +1}
749 irm = nl2 + 2 - ire;
750 ifm = (iphi - ire/2 + nside -1) / nside;// ! face boundary
751 ifp = (iphi - irm/2 + nside -1) / nside;
752 if( ifp==ifm ) {//then ! faces 4 to 7
753 face_num = (int)fmod(ifp,4) + 4;
754 }
755 else if( ifp + 1==ifm ) {//then ! (half-)faces 0 to 3
756 face_num = ifp;
757 }
758 else if( ifp - 1==ifm ) {//then ! (half-)faces 8 to 11
759 face_num = ifp + 7;
760 }
761 }
762 else {
763
764 ip = npix - ipring1 + 1;
765 hip = ip/2.;
766 fihip = floor ( hip );
767 irs = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
768 iphi = 4*irs + 1 - (ip - 2*irs*(irs-1));
769
770 kshift = 0;
771 nr = irs;
772 irn = nl4 - irs;
773 face_num = (iphi-1) / irs + 8;// ! in {8,11}
774 }
775
776 //c finds the (x,y) on the face
777 irt = irn - jrll[face_num]*nside + 1;// ! in {-nside+1,0}
778 ipt = 2*iphi - jpll[face_num]*nr - kshift - 1;// ! in {-nside+1,nside-1}
779
780
781 if( ipt>=nl2 ) ipt = ipt - 8*nside;// ! for the face #4
782
783 ix = (ipt - irt ) / 2;
784 iy = -(ipt + irt ) / 2;
785
786 ix_low = (int)fmod(ix,128);
787 ix_hi = ix/128;
788 iy_low = (int)fmod(iy,128);
789 iy_hi = iy/128;
790 ipf=(PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
791
792 return (ipf + face_num* nside *nside);// ! in {0, 12*nside**2 - 1}
793}
794
795template<class T>
796int_4 SphereGorski<T>::ang2pix_ring(int_4 nside, double theta, double phi) const
797{
798 /*
799 ==================================================
800 c gives the pixel number ipix (RING)
801 c corresponding to angles theta and phi
802 c==================================================
803 */
804 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
805 // (16/12/98)
806
807 int nl2, nl4, ncap, npix, jp, jm, ipix1;
808 double z, za, tt, tp, tmp;
809 int ir, ip, kshift;
810
811 double piover2(Pi/2.);
812 double twopi(2.*Pi);
813 double z0(2./3.);
814 int ns_max(8192);
815
816 if( nside<1 || nside>ns_max ) {
817 cout << "nside out of range" << endl;
818 exit(0);
819 }
820
821 if( theta<0. || theta>Pi) {
822 cout << "theta out of range" << endl;
823 exit(0);
824 }
825
826 z = cos(theta);
827 za = fabs(z);
828 if( phi >= twopi) phi = phi - twopi;
829 if (phi < 0.) phi = phi + twopi;
830 tt = phi / piover2;// ! in [0,4)
831
832 nl2 = 2*nside;
833 nl4 = 4*nside;
834 ncap = nl2*(nside-1);// ! number of pixels in the north polar cap
835 npix = 12*nside*nside;
836
837 if( za <= z0 ) {
838
839 jp = (int)floor(nside*(0.5 + tt - z*0.75));// ! index of ascending edge line
840 jm = (int)floor(nside*(0.5 + tt + z*0.75));// ! index of descending edge line
841
842 ir = nside + 1 + jp - jm;// ! in {1,2n+1} (ring number counted from z=2/3)
843 kshift = 0;
844 if (fmod(ir,2)==0.) kshift = 1;// ! kshift=1 if ir even, 0 otherwise
845
846 ip = (int)floor( ( jp+jm - nside + kshift + 1 ) / 2 ) + 1;// ! in {1,4n}
847 if( ip>nl4 ) ip = ip - nl4;
848
849 ipix1 = ncap + nl4*(ir-1) + ip ;
850 }
851 else {
852
853 tp = tt - floor(tt);// !MOD(tt,1.d0)
854 tmp = sqrt( 3.*(1. - za) );
855
856 jp = (int)floor( nside * tp * tmp );// ! increasing edge line index
857 jm = (int)floor( nside * (1. - tp) * tmp );// ! decreasing edge line index
858
859 ir = jp + jm + 1;// ! ring number counted from the closest pole
860 ip = (int)floor( tt * ir ) + 1;// ! in {1,4*ir}
861 if( ip>4*ir ) ip = ip - 4*ir;
862
863 ipix1 = 2*ir*(ir-1) + ip;
864 if( z<=0. ) {
865 ipix1 = npix - 2*ir*(ir+1) + ip;
866 }
867 }
868 return (ipix1 - 1);// ! in {0, npix-1}
869}
870
871template<class T>
872int_4 SphereGorski<T>::ang2pix_nest(int_4 nside, double theta, double phi) const
873{
874 /*
875 ==================================================
876 subroutine ang2pix_nest(nside, theta, phi, ipix)
877 ==================================================
878 c gives the pixel number ipix (NESTED)
879 c corresponding to angles theta and phi
880 c
881 c the computation is made to the highest resolution available (nside=8192)
882 c and then degraded to that required (by integer division)
883 c this doesn't cost more, and it makes sure
884 c that the treatement of round-off will be consistent
885 c for every resolution
886 ==================================================
887 */
888 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
889 // (16/12/98)
890
891 const PIXELS_XY& PXY= PIXELS_XY::instance();
892
893 double z, za, z0, tt, tp, tmp;
894 int face_num,jp,jm;
895 int ifp, ifm;
896 int ix, iy, ix_low, ix_hi, iy_low, iy_hi, ipf, ntt;
897 double piover2(Pi/2.), twopi(2.*Pi);
898 int ns_max(8192);
899
900 if( nside<1 || nside>ns_max ) {
901 cout << "nside out of range" << endl;
902 exit(0);
903 }
904 if( theta<0 || theta>Pi ) {
905 cout << "theta out of range" << endl;
906 exit(0);
907 }
908 z = cos(theta);
909 za = fabs(z);
910 z0 = 2./3.;
911 if( phi>=twopi ) phi = phi - twopi;
912 if( phi<0. ) phi = phi + twopi;
913 tt = phi / piover2;// ! in [0,4[
914 if( za<=z0 ) { // then ! equatorial region
915
916 //(the index of edge lines increase when the longitude=phi goes up)
917 jp = (int)floor(ns_max*(0.5 + tt - z*0.75));// ! ascending edge line index
918 jm = (int)floor(ns_max*(0.5 + tt + z*0.75));// ! descending edge line index
919
920 //c finds the face
921 ifp = jp / ns_max;// ! in {0,4}
922 ifm = jm / ns_max;
923 if( ifp==ifm ) face_num = (int)fmod(ifp,4) + 4; //then ! faces 4 to 7
924 else if( ifp<ifm ) face_num = (int)fmod(ifp,4); // (half-)faces 0 to 3
925 else face_num = (int)fmod(ifm,4) + 8;//! (half-)faces 8 to 11
926
927 ix = (int)fmod(jm, ns_max);
928 iy = ns_max - (int)fmod(jp, ns_max) - 1;
929 }
930 else { //! polar region, za > 2/3
931
932 ntt = (int)floor(tt);
933 if( ntt>=4 ) ntt = 3;
934 tp = tt - ntt;
935 tmp = sqrt( 3.*(1. - za) );// ! in ]0,1]
936
937 //(the index of edge lines increase when distance from the closest pole goes up)
938 jp = (int)floor(ns_max*tp*tmp); // ! line going toward the pole as phi increases
939 jm = (int)floor(ns_max*(1.-tp)*tmp); // ! that one goes away of the closest pole
940 jp = (int)min(ns_max-1, jp);// ! for points too close to the boundary
941 jm = (int)min(ns_max-1, jm);
942
943 // finds the face and pixel's (x,y)
944 if( z>=0 ) {
945 face_num = ntt;// ! in {0,3}
946 ix = ns_max - jm - 1;
947 iy = ns_max - jp - 1;
948 }
949 else {
950 face_num = ntt + 8;// ! in {8,11}
951 ix = jp;
952 iy = jm;
953 }
954 }
955
956 ix_low = (int)fmod(ix,128);
957 ix_hi = ix/128;
958 iy_low = (int)fmod(iy,128);
959 iy_hi = iy/128;
960 ipf= (PXY.x2pix_(ix_hi)+PXY.y2pix_(iy_hi))*(128*128)+(PXY.x2pix_(ix_low)+PXY.y2pix_(iy_low));
961 // ipf = ipf / pow(ns_max/nside,2.);// ! in {0, nside**2 - 1}
962 // return ( ipf + face_num*pow(nside,2));// ! in {0, 12*nside**2 - 1}
963 // $CHECK$ Reza 25/10/99 , pow remplace par *
964 ipf = ipf / ((ns_max/nside)*(ns_max/nside));
965 return (ipf + face_num*nside*nside);
966}
967
968template<class T>
969void SphereGorski<T>::pix2ang_ring(int_4 nside,int_4 ipix,double& theta,double& phi) const {
970 /*
971 ===================================================
972 c gives theta and phi corresponding to pixel ipix (RING)
973 c for a parameter nside
974 ===================================================
975 */
976 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
977 // (16/12/98)
978
979 int nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
980 double fact1, fact2, fodd, hip, fihip;
981
982 int ns_max(8192);
983
984 if( nside<1 || nside>ns_max ) {
985 cout << "nside out of range" << endl;
986 exit(0);
987 }
988 npix = 12*nside*nside; // ! total number of points
989 if( ipix<0 || ipix>npix-1 ) {
990 cout << "ipix out of range" << endl;
991 exit(0);
992 }
993
994 ipix1 = ipix + 1; // in {1, npix}
995 nl2 = 2*nside;
996 nl4 = 4*nside;
997 ncap = 2*nside*(nside-1);// ! points in each polar cap, =0 for nside =1
998 fact1 = 1.5*nside;
999 fact2 = 3.0*nside*nside;
1000
1001 if( ipix1 <= ncap ) { //! North Polar cap -------------
1002
1003 hip = ipix1/2.;
1004 fihip = floor(hip);
1005 iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from North pole
1006 iphi = ipix1 - 2*iring*(iring - 1);
1007
1008 theta = acos( 1. - iring*iring / fact2 );
1009 phi = ((double)iphi - 0.5) * Pi/(2.*iring);
1010 // cout << theta << " " << phi << endl;
1011 }
1012 else if( ipix1 <= nl2*(5*nside+1) ) {//then ! Equatorial region ------
1013
1014 ip = ipix1 - ncap - 1;
1015 iring = (int)floor( ip / nl4 ) + nside;// ! counted from North pole
1016 iphi = ip%nl4 + 1;
1017
1018 fodd = 0.5 * (1 + (iring+nside)%2 );// ! 1 if iring+nside is odd, 1/2 otherwise
1019 theta = acos( (nl2 - iring) / fact1 );
1020 phi = ((double)iphi - fodd) * Pi /(2.*nside);
1021 }
1022 else {//! South Polar cap -----------------------------------
1023
1024 ip = npix - ipix1 + 1;
1025 hip = ip/2.;
1026 fihip = floor(hip);
1027 iring = (int)floor( sqrt( hip - sqrt(fihip) ) ) + 1;// ! counted from South pole
1028 iphi = (int)(4.*iring + 1 - (ip - 2.*iring*(iring-1)));
1029
1030 theta = acos( -1. + iring*iring / fact2 );
1031 phi = ((double)iphi - 0.5) * Pi/(2.*iring);
1032 // cout << theta << " " << phi << endl;
1033 }
1034}
1035
1036template<class T>
1037void SphereGorski<T>::pix2ang_nest(int_4 nside,int_4 ipix,double& theta,double& phi) const {
1038 /*
1039 ==================================================
1040 subroutine pix2ang_nest(nside, ipix, theta, phi)
1041 ==================================================
1042 c gives theta and phi corresponding to pixel ipix (NESTED)
1043 c for a parameter nside
1044 ==================================================
1045 */
1046 // tranlated from FORTRAN (Gorski) to C, by B. Revenu, revised Guy Le Meur
1047 // (16/12/98)
1048
1049 const PIXELS_XY& PXY= PIXELS_XY::instance();
1050
1051 int npix, npface, face_num;
1052 int ipf, ip_low, ip_trunc, ip_med, ip_hi;
1053 int ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
1054 double z, fn, fact1, fact2;
1055 double piover2(Pi/2.);
1056 int ns_max(8192);
1057
1058 // ! coordinate of the lowest corner of each face
1059 int jrll[12]={2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};//! in unit of nside
1060 int jpll[12]={1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};// ! in unit of nside/2
1061
1062 if( nside<1 || nside>ns_max ) {
1063 cout << "nside out of range" << endl;
1064 exit(0);
1065 }
1066 npix = 12 * nside*nside;
1067 if( ipix<0 || ipix>npix-1 ) {
1068 cout << "ipix out of range" << endl;
1069 exit(0);
1070 }
1071
1072 fn = 1.*nside;
1073 fact1 = 1./(3.*fn*fn);
1074 fact2 = 2./(3.*fn);
1075 nl4 = 4*nside;
1076
1077 //c finds the face, and the number in the face
1078 npface = nside*nside;
1079
1080 face_num = ipix/npface;// ! face number in {0,11}
1081 ipf = (int)fmod(ipix,npface);// ! pixel number in the face {0,npface-1}
1082
1083 //c finds the x,y on the face (starting from the lowest corner)
1084 //c from the pixel number
1085 ip_low = (int)fmod(ipf,1024);// ! content of the last 10 bits
1086 ip_trunc = ipf/1024 ;// ! truncation of the last 10 bits
1087 ip_med = (int)fmod(ip_trunc,1024);// ! content of the next 10 bits
1088 ip_hi = ip_trunc/1024 ;//! content of the high weight 10 bits
1089
1090 ix = 1024*PXY.pix2x_(ip_hi)+32*PXY.pix2x_(ip_med)+PXY.pix2x_(ip_low);
1091 iy = 1024*PXY.pix2y_(ip_hi)+32*PXY.pix2y_(ip_med)+PXY.pix2y_(ip_low);
1092
1093 //c transforms this in (horizontal, vertical) coordinates
1094 jrt = ix + iy;// ! 'vertical' in {0,2*(nside-1)}
1095 jpt = ix - iy;// ! 'horizontal' in {-nside+1,nside-1}
1096
1097 //c computes the z coordinate on the sphere
1098 // jr = jrll[face_num+1]*nside - jrt - 1;// ! ring number in {1,4*nside-1}
1099 jr = jrll[face_num]*nside - jrt - 1;
1100 nr = nside;// ! equatorial region (the most frequent)
1101 z = (2*nside-jr)*fact2;
1102 kshift = (int)fmod(jr - nside, 2);
1103 if( jr<nside ) { //then ! north pole region
1104 nr = jr;
1105 z = 1. - nr*nr*fact1;
1106 kshift = 0;
1107 }
1108 else {
1109 if( jr>3*nside ) {// then ! south pole region
1110 nr = nl4 - jr;
1111 z = - 1. + nr*nr*fact1;
1112 kshift = 0;
1113 }
1114 }
1115 theta = acos(z);
1116
1117 //c computes the phi coordinate on the sphere, in [0,2Pi]
1118 // jp = (jpll[face_num+1]*nr + jpt + 1 + kshift)/2;// ! 'phi' number in the ring in {1,4*nr}
1119 jp = (jpll[face_num]*nr + jpt + 1 + kshift)/2;
1120 if( jp>nl4 ) jp = jp - nl4;
1121 if( jp<1 ) jp = jp + nl4;
1122 phi = (jp - (kshift+1)*0.5) * (piover2 / nr);
1123}
1124
1125
1126
1127template <class T>
1128void SphereGorski<T>::print(ostream& os) const
1129{
1130 if(mInfo_) os << " DVList Info= " << *mInfo_ << endl;
1131 //
1132 os << " nSide_ = " << nSide_ << endl;
1133 os << " nPix_ = " << nPix_ << endl;
1134 os << " omeg_ = " << omeg_ << endl;
1135
1136 os << " content of pixels : ";
1137 for(int i=0; i < nPix_; i++)
1138 {
1139 if(i%5 == 0) os << endl;
1140 os << pixels_(i) <<", ";
1141 }
1142 os << endl;
1143
1144 os << endl;
1145 //const PIXELS_XY& PXY= PIXELS_XY::instance();
1146
1147 //os << endl; os << " contenu des tableaux conversions "<<endl;
1148 //for(int i=0; i < 5; i++)
1149 // {
1150 // os<<PXY.pix2x_(i)<<", "<<PXY.pix2y_(i)<<", "<<PXY.x2pix_(i)<<", "<<PXY.y2pix_(i)<<endl;
1151 // }
1152 os << endl;
1153
1154}
1155
1156//*******************************************************************
1157// Class FIO_SphereGorski<T>
1158// Les objets delegues pour la gestion de persistance
1159//*******************************************************************
1160
1161template <class T>
1162FIO_SphereGorski<T>::FIO_SphereGorski()
1163{
1164 dobj= new SphereGorski<T>;
1165 ownobj= true;
1166}
1167
1168template <class T>
1169FIO_SphereGorski<T>::FIO_SphereGorski(string const& filename)
1170{
1171 dobj= new SphereGorski<T>;
1172 dobj->pixels_.SetTemp(true);
1173 ownobj= true;
1174 Read(filename);
1175}
1176
1177template <class T>
1178FIO_SphereGorski<T>::FIO_SphereGorski(const SphereGorski<T>& obj)
1179{
1180 dobj= new SphereGorski<T>(obj, true);
1181 dobj->pixels_.SetTemp(true);
1182 ownobj= true;
1183}
1184
1185template <class T>
1186FIO_SphereGorski<T>::FIO_SphereGorski(SphereGorski<T>* obj)
1187{
1188 dobj= obj;
1189 ownobj= false;
1190}
1191
1192template <class T>
1193FIO_SphereGorski<T>::~FIO_SphereGorski()
1194{
1195 if (ownobj && dobj) delete dobj;
1196}
1197
1198template <class T>
1199AnyDataObj* FIO_SphereGorski<T>::DataObj()
1200{
1201 return(dobj);
1202}
1203
1204
1205template <class T>
1206void FIO_SphereGorski<T>::SetDataObj(AnyDataObj & o)
1207{
1208 SphereGorski<T> * po = dynamic_cast< SphereGorski<T> * >(&o);
1209 if (po == NULL) return;
1210 if (ownobj && dobj) delete dobj;
1211 dobj = po; ownobj = false;
1212}
1213
1214template <class T>
1215void FIO_SphereGorski<T>::ReadSelf(PInPersist& is)
1216{
1217
1218 if(dobj == NULL)
1219 {
1220 dobj= new SphereGorski<T>;
1221 dobj->pixels_.SetTemp(true);
1222 ownobj= true;
1223 }
1224
1225// On lit les 3 premiers uint_8
1226uint_8 itab[3];
1227is.Get(itab, 3);
1228// Let's Read the SphereCoordSys object -- ATTENTIOn - $CHECK$
1229 FIO_SphereCoordSys fio_scs( dobj->GetCoordSys());
1230 fio_scs.Read(is);
1231 // Pour savoir s'il y avait un DVList Info associe
1232 char strg[256];
1233 is.GetLine(strg, 255);
1234 bool hadinfo= false;
1235 if(strncmp(strg+strlen(strg)-7, "HasInfo", 7) == 0) hadinfo= true;
1236 if(hadinfo)
1237 { // Lecture eventuelle du DVList Info
1238 is >> dobj->Info();
1239 }
1240
1241 int_4 nSide;
1242 is.GetI4(nSide);
1243 int_4 nPix;
1244 is.GetI4(nPix);
1245 double Omega;
1246 is.GetR8(Omega);
1247 dobj->setParameters(nSide,nPix, Omega);
1248
1249// On lit les DataBlocks;
1250 FIO_NDataBlock<T> fio_nd(&dobj->pixels_);
1251 fio_nd.Read(is);
1252 FIO_NDataBlock<int> fio_ndi1(&dobj->sliceBeginIndex_);
1253 fio_ndi1.Read(is);
1254 FIO_NDataBlock<int> fio_ndi2(&dobj->sliceLenght_);
1255 fio_ndi2.Read(is);
1256}
1257
1258template <class T>
1259void FIO_SphereGorski<T>::WriteSelf(POutPersist& os) const
1260{
1261 if(dobj == NULL)
1262 {
1263 cout << " WriteSelf:: dobj= null " << endl;
1264 return;
1265 }
1266
1267// On ecrit 3 uint_8
1268// 0 : Numero de version, 1 : Size index, 2 reserve a l
1269uint_8 itab[3];
1270itab[0] = 1;
1271itab[1] = dobj->SizeIndex();
1272itab[2] = 0;
1273os.Put(itab, 3);
1274// Let's write the SphereCoordSys object
1275 FIO_SphereCoordSys fio_scs( dobj->GetCoordSys());
1276 fio_scs.Write(os);
1277
1278 char strg[256];
1279 int_4 nSide= dobj->SizeIndex();
1280 int_4 nPix = dobj->NbPixels();
1281
1282 if(dobj->ptrInfo())
1283 {
1284 sprintf(strg,"SphereGorski: NSide=%6d NPix=%9d HasInfo",nSide,nPix);
1285 os.PutLine(strg);
1286 os << dobj->Info();
1287 }
1288 else
1289 {
1290 sprintf(strg,"SphereGorski: NSide=%6d NPix=%9d ",nSide,nPix);
1291 os.PutLine(strg);
1292 }
1293
1294 os.PutI4(nSide);
1295 os.PutI4(nPix);
1296 os.PutR8(dobj->PixSolAngle());
1297// On ecrit les dataBlocks
1298 FIO_NDataBlock<T> fio_nd(&dobj->pixels_);
1299 fio_nd.Write(os);
1300 FIO_NDataBlock<int> fio_ndi1(&dobj->sliceBeginIndex_);
1301 fio_ndi1.Write(os);
1302 FIO_NDataBlock<int> fio_ndi2(&dobj->sliceLenght_);
1303 fio_ndi2.Write(os);
1304}
1305
1306#ifdef __CXX_PRAGMA_TEMPLATES__
1307#pragma define_template SphereGorski<uint_2>
1308#pragma define_template SphereGorski<r_8>
1309#pragma define_template SphereGorski<r_4>
1310#pragma define_template SphereGorski< complex<r_4> >
1311#pragma define_template SphereGorski< complex<r_8> >
1312#pragma define_template FIO_SphereGorski<r_8>
1313#pragma define_template FIO_SphereGorski<r_4>
1314#pragma define_template FIO_SphereGorski< complex<r_4> >
1315#pragma define_template FIO_SphereGorski< complex<r_8> >
1316#endif
1317#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
1318template class SphereGorski<uint_2>;
1319template class SphereGorski<r_8>;
1320template class SphereGorski<r_4>;
1321template class SphereGorski< complex<r_4> >;
1322template class SphereGorski< complex<r_8> >;
1323template class FIO_SphereGorski<r_8>;
1324template class FIO_SphereGorski<r_4>;
1325template class FIO_SphereGorski< complex<r_4> >;
1326template class FIO_SphereGorski< complex<r_8> >;
1327#endif
1328
Note: See TracBrowser for help on using the repository browser.