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

Last change on this file since 842 was 828, checked in by ansari, 25 years ago

Suppression erreur compil CC (redecl. variables de boucle) - Reza 6/4/2000

File size: 35.5 KB
RevLine 
[764]1#include "machdefs.h"
2#include <math.h>
3#include <complex>
4
5#include "pexceptions.h"
[809]6#include "fiondblock.h"
[764]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);
[828]423 int sliceIndex;
424 for (sliceIndex=0; sliceIndex< nSide_-1; sliceIndex++)
[764]425 {
426 sliceBeginIndex_(sliceIndex) = 2*sliceIndex*(sliceIndex+1);
427 sliceLenght_(sliceIndex) = 4*(sliceIndex+1);
428 }
[828]429 for (sliceIndex= nSide_-1; sliceIndex< 3*nSide_; sliceIndex++)
[764]430 {
431 sliceBeginIndex_(sliceIndex) = 2*nSide_*(2*sliceIndex-nSide_+1);
432 sliceLenght_(sliceIndex) = 4*nSide_;
433 }
[828]434 for (sliceIndex= 3*nSide_; sliceIndex< 4*nSide_-1; sliceIndex++)
[764]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
1205
1206template <class T>
1207void FIO_SphereGorski<T>::SetDataObj(AnyDataObj & o)
1208{
1209 SphereGorski<T> * po = dynamic_cast< SphereGorski<T> * >(&o);
1210 if (po == NULL) return;
1211 if (ownobj && dobj) delete dobj;
1212 dobj = po; ownobj = false;
1213}
1214
1215template <class T>
1216void FIO_SphereGorski<T>::ReadSelf(PInPersist& is)
1217{
1218
1219 if(dobj == NULL)
1220 {
1221 dobj= new SphereGorski<T>;
1222 dobj->pixels_.SetTemp(true);
1223 ownobj= true;
1224 }
1225
1226// On lit les 3 premiers uint_8
1227uint_8 itab[3];
1228is.Get(itab, 3);
1229// Let's Read the SphereCoordSys object -- ATTENTIOn - $CHECK$
1230 FIO_SphereCoordSys fio_scs( dobj->GetCoordSys());
1231 fio_scs.Read(is);
1232 // Pour savoir s'il y avait un DVList Info associe
1233 char strg[256];
1234 is.GetLine(strg, 255);
1235 bool hadinfo= false;
1236 if(strncmp(strg+strlen(strg)-7, "HasInfo", 7) == 0) hadinfo= true;
1237 if(hadinfo)
1238 { // Lecture eventuelle du DVList Info
1239 is >> dobj->Info();
1240 }
1241
1242 int_4 nSide;
1243 is.GetI4(nSide);
1244 int_4 nPix;
1245 is.GetI4(nPix);
1246 double Omega;
1247 is.GetR8(Omega);
1248 dobj->setParameters(nSide,nPix, Omega);
1249
1250// On lit les DataBlocks;
1251 FIO_NDataBlock<T> fio_nd(&dobj->pixels_);
1252 fio_nd.Read(is);
1253 FIO_NDataBlock<int> fio_ndi1(&dobj->sliceBeginIndex_);
1254 fio_ndi1.Read(is);
1255 FIO_NDataBlock<int> fio_ndi2(&dobj->sliceLenght_);
1256 fio_ndi2.Read(is);
1257}
1258
1259template <class T>
1260void FIO_SphereGorski<T>::WriteSelf(POutPersist& os) const
1261{
1262 if(dobj == NULL)
1263 {
1264 cout << " WriteSelf:: dobj= null " << endl;
1265 return;
1266 }
1267
1268// On ecrit 3 uint_8
1269// 0 : Numero de version, 1 : Size index, 2 reserve a l
1270uint_8 itab[3];
1271itab[0] = 1;
1272itab[1] = dobj->SizeIndex();
1273itab[2] = 0;
1274os.Put(itab, 3);
1275// Let's write the SphereCoordSys object
1276 FIO_SphereCoordSys fio_scs( dobj->GetCoordSys());
1277 fio_scs.Write(os);
1278
1279 char strg[256];
1280 int_4 nSide= dobj->SizeIndex();
1281 int_4 nPix = dobj->NbPixels();
1282
1283 if(dobj->ptrInfo())
1284 {
1285 sprintf(strg,"SphereGorski: NSide=%6d NPix=%9d HasInfo",nSide,nPix);
1286 os.PutLine(strg);
1287 os << dobj->Info();
1288 }
1289 else
1290 {
1291 sprintf(strg,"SphereGorski: NSide=%6d NPix=%9d ",nSide,nPix);
1292 os.PutLine(strg);
1293 }
1294
1295 os.PutI4(nSide);
1296 os.PutI4(nPix);
1297 os.PutR8(dobj->PixSolAngle());
1298// On ecrit les dataBlocks
1299 FIO_NDataBlock<T> fio_nd(&dobj->pixels_);
1300 fio_nd.Write(os);
1301 FIO_NDataBlock<int> fio_ndi1(&dobj->sliceBeginIndex_);
1302 fio_ndi1.Write(os);
1303 FIO_NDataBlock<int> fio_ndi2(&dobj->sliceLenght_);
1304 fio_ndi2.Write(os);
1305}
1306
1307#ifdef __CXX_PRAGMA_TEMPLATES__
[771]1308#pragma define_template SphereGorski<uint_2>
[764]1309#pragma define_template SphereGorski<r_8>
1310#pragma define_template SphereGorski<r_4>
1311#pragma define_template SphereGorski< complex<r_4> >
1312#pragma define_template SphereGorski< complex<r_8> >
1313#pragma define_template FIO_SphereGorski<r_8>
1314#pragma define_template FIO_SphereGorski<r_4>
1315#pragma define_template FIO_SphereGorski< complex<r_4> >
1316#pragma define_template FIO_SphereGorski< complex<r_8> >
1317#endif
1318#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
[771]1319template class SphereGorski<uint_2>;
[764]1320template class SphereGorski<r_8>;
1321template class SphereGorski<r_4>;
1322template class SphereGorski< complex<r_4> >;
1323template class SphereGorski< complex<r_8> >;
1324template class FIO_SphereGorski<r_8>;
1325template class FIO_SphereGorski<r_4>;
1326template class FIO_SphereGorski< complex<r_4> >;
1327template class FIO_SphereGorski< complex<r_8> >;
1328#endif
1329
Note: See TracBrowser for help on using the repository browser.