-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhealpix_utils.c
1783 lines (1526 loc) · 44.3 KB
/
healpix_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
hacked apart healpix base2 and healpix_base functions
converted int64 to long to work in C on 64 bit machines
-Matthew R Becker, Univ. of Chicago 2009
*/
/*
* This file is part of Healpix_cxx.
*
* Healpix_cxx is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Healpix_cxx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Healpix_cxx; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* For more information about HEALPix, see http://healpix.jpl.nasa.gov
*/
/*
* Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
* (DLR).
*/
/*! \file healpix_base2.h
* Copyright (C) 2003, 2004, 2005, 2006 Max-Planck-Society
* \author Martin Reinecke
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <gsl/gsl_math.h>
#include "healpix_utils.h"
static long ctab[0x100];
static long utab[0x100];
static const long jrll[] = { 2,2,2,2,3,3,3,3,4,4,4,4 };
static const long jpll[] = { 1,3,5,7,0,2,4,6,1,3,5,7 };
static int HEALPIX_TOOLS_INIT = 1;
long isqrt(long i)
{
return sqrt(((double) (i)) + 0.5);
}
long ilog2(long arg)
{
unsigned long res=0;
while(arg > 0x0000FFFF)
{
res += 16;
arg >>= 16;
}
if(arg > 0x000000FF)
{
res |= 8;
arg >>= 8;
}
if(arg > 0x0000000F)
{
res |= 4;
arg >>= 4;
}
if(arg > 0x00000003)
{
res |= 2;
arg >>= 2;
}
if(arg > 0x00000001)
{
res |= 1;
}
return res;
}
long imodulo(long v1, long v2)
{
return (v1>=0) ? ((v1<v2) ? v1 : (v1%v2)) : ((v1%v2)+v2);
}
void ang2radec(double theta, double phi, double *ra, double *dec)
{
*ra = phi/M_PI*180.0;
*dec = (M_PI/2.0 - theta)/M_PI*180.0;
}
void radec2ang(double *theta, double *phi, double ra, double dec)
{
*phi = ra/180.0*M_PI;
*theta = M_PI/2.0 - dec/180.0*M_PI;
}
void vec2radec(double vec[3], double *ra, double *dec)
{
/*double rad;
rad = sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
if(vec[0] == 0.0 && vec[1] == 0.0)
*ra = 0.0;
else
*ra = (float) ((vec[1] < 0.0) ? 360.0 + atan2(vec[1],vec[0])*180.0/M_PI : atan2(vec[1],vec[0])*180.0/M_PI);
*dec = (float) (90.0-acos(vec[2]/rad)*180.0/M_PI);
*/
double theta,phi;
vec2ang(vec,&theta,&phi);
ang2radec(theta,phi,ra,dec);
}
void vec2ang(double vec[3], double *theta, double *phi)
{
double norm = sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
if(vec[0] == 0.0 && vec[1] == 0.0)
*phi = 0.0;
else
*phi = atan2(vec[1],vec[0]);
if(*phi < 0.0)
*phi = *phi + 2.0*M_PI;
*theta = acos(vec[2]/norm);
}
void ang2vec(double vec[3], double theta, double phi)
{
double costheta = cos(theta);
double sintheta = sqrt((1.0 + costheta)*(1.0 - costheta));
vec[0] = sintheta*cos(phi);
vec[1] = sintheta*sin(phi);
vec[2] = costheta;
}
void tablefiller(void)
{
assert(CHAR_BIT == 8);
int m;
for (m=0; m<0x100; ++m)
{
ctab[m] =
(m&0x1 ) | ((m&0x2 ) << 7) | ((m&0x4 ) >> 1) | ((m&0x8 ) << 6)
| ((m&0x10) >> 2) | ((m&0x20) << 5) | ((m&0x40) >> 3) | ((m&0x80) << 4);
utab[m] =
(m&0x1 ) | ((m&0x2 ) << 1) | ((m&0x4 ) << 2) | ((m&0x8 ) << 3)
| ((m&0x10) << 4) | ((m&0x20) << 5) | ((m&0x40) << 6) | ((m&0x80) << 7);
}
}
long order2npix(long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
return npix_;
}
long order2nside(long order_)
{
long nside_ = 1;
nside_ = nside_ << order_;
return nside_;
}
long npix2nside(long npix)
{
long res=isqrt(npix/12);
assert(npix==res*res*12);
return res;
}
long npix2order(long npix)
{
return nside2order(npix2nside(npix));
}
long nside2npix(long nside)
{
assert(nside>0 && !((nside)&(nside-1)));
return 12*nside*nside;
}
long nside2order(long nside)
{
assert(nside>0 && !((nside)&(nside-1)));
return ilog2(nside);
}
void nest2xyf(long pix, long *ix, long *iy, long *face_num, long order_)
{
if(HEALPIX_TOOLS_INIT)
{
tablefiller();
HEALPIX_TOOLS_INIT = 0;
}
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
long npface_ = 1;
npface_ = npface_ << (2*order_);
*face_num = pix>>(2*order_);
pix &= (npface_-1);
long raw = ((pix&0x555500000000ull)>>16)
| ((pix&0x5555000000000000ull)>>31)
| (pix&0x5555)
| ((pix&0x55550000)>>15);
*ix = ctab[raw&0xff]
| (ctab[(raw>>8)&0xff]<<4)
| (ctab[(raw>>16)&0xff]<<16)
| (ctab[(raw>>24)&0xff]<<20);
pix >>= 1;
raw = ((pix&0x555500000000ull)>>16)
| ((pix&0x5555000000000000ull)>>31)
| (pix&0x5555)
| ((pix&0x55550000)>>15);
*iy = ctab[raw&0xff]
| (ctab[(raw>>8)&0xff]<<4)
| (ctab[(raw>>16)&0xff]<<16)
| (ctab[(raw>>24)&0xff]<<20);
}
long xyf2nest(long ix, long iy, long face_num, long order_)
{
if(HEALPIX_TOOLS_INIT)
{
tablefiller();
HEALPIX_TOOLS_INIT = 0;
}
long pix = ((face_num)<<(2*order_)) +
( ((utab[ ix &0xff]))
| ((utab[(ix>> 8)&0xff])<<16)
| ((utab[(ix>>16)&0xff])<<32)
| ((utab[(ix>>24)&0xff])<<48)
| ((utab[ iy &0xff])<<1)
| ((utab[(iy>> 8)&0xff])<<17)
| ((utab[(iy>>16)&0xff])<<33)
| ((utab[(iy>>24)&0xff])<<49) );
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
return pix;
}
void ring2xyf(long pix, long *ix, long *iy, long *face_num, long order_)
{
long iring, iphi, kshift, nr;
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
long nside_ = 1;
nside_ = nside_ << order_;
long npface_ = 1;
npface_ = npface_ << (2*order_);
long ncap_ = (npface_-nside_)<<1;
long nl2 = 2*nside_;
if (pix<ncap_) // North Polar cap
{
iring = (long) (0.5*(1+isqrt(1+2*pix))); //counted from North pole
iphi = (pix+1) - 2*iring*(iring-1);
kshift = 0;
nr = iring;
*face_num=0;
long tmp = iphi-1;
if (tmp>=(2*iring))
{
*face_num=2;
tmp-=2*iring;
}
if (tmp>=iring) (*face_num) = (*face_num) + 1;
}
else if (pix<(npix_-ncap_)) // Equatorial region
{
long ip = pix - ncap_;
if (order_>=0)
{
iring = (ip>>(order_+2)) + nside_; // counted from North pole
iphi = (ip&(4*nside_-1)) + 1;
}
else
{
iring = (ip/(4*nside_)) + nside_; // counted from North pole
iphi = (ip%(4*nside_)) + 1;
}
kshift = (iring+nside_)&1;
nr = nside_;
long ire = iring-nside_+1;
long irm = nl2+2-ire;
long ifm, ifp;
if (order_>=0)
{
ifm = (iphi - ire/2 + nside_ -1) >> order_;
ifp = (iphi - irm/2 + nside_ -1) >> order_;
}
else
{
ifm = (iphi - ire/2 + nside_ -1) / nside_;
ifp = (iphi - irm/2 + nside_ -1) / nside_;
}
if (ifp == ifm) // faces 4 to 7
*face_num = (ifp==4) ? 4 : ifp+4;
else if (ifp<ifm) // (half-)faces 0 to 3
*face_num = ifp;
else // (half-)faces 8 to 11
*face_num = ifm + 8;
}
else // South Polar cap
{
long ip = npix_ - pix;
iring = (long) (0.5*(1+isqrt(2*ip-1))); //counted from South pole
iphi = 4*iring + 1 - (ip - 2*iring*(iring-1));
kshift = 0;
nr = iring;
iring = 2*nl2-iring;
*face_num=8;
long tmp = iphi-1;
if (tmp>=(2*nr))
{
*face_num=10;
tmp-=2*nr;
}
if (tmp>=nr) (*face_num) = (*face_num) + 1;
}
long irt = iring - (jrll[*face_num]*nside_) + 1;
long ipt = 2*iphi- jpll[*face_num]*nr - kshift -1;
if (ipt>=nl2) ipt-=8*nside_;
*ix = (ipt-irt) >>1;
*iy =(-(ipt+irt))>>1;
}
long xyf2ring(long ix, long iy, long face_num, long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
long nside_ = 1;
nside_ = nside_ << order_;
long npface_ = 1;
npface_ = npface_ << (2*order_);
long ncap_ = (npface_-nside_)<<1;
long nl4 = 4*nside_;
long jr = (jrll[face_num]*nside_) - ix - iy - 1;
long nr, kshift, n_before;
if (jr<nside_)
{
nr = jr;
n_before = 2*nr*(nr-1);
kshift = 0;
}
else if (jr > 3*nside_)
{
nr = nl4-jr;
n_before = npix_ - 2*(nr+1)*nr;
kshift = 0;
}
else
{
nr = nside_;
n_before = ncap_ + (jr-nside_)*nl4;
kshift = (jr-nside_)&1;
}
long jp = (jpll[face_num]*nr + ix - iy + 1 + kshift) / 2;
if (jp>nl4)
jp-=nl4;
else
if (jp<1) jp+=nl4;
long pix = n_before + jp - 1;
assert(pix >= 0 && pix < npix_);
return pix;
}
long nest2ring(long pix, long order_)
{
long ix, iy, face_num;
nest2xyf(pix,&ix,&iy,&face_num,order_);
return xyf2ring(ix,iy,face_num,order_);
}
long ring2nest(long pix, long order_)
{
long ix, iy, face_num;
ring2xyf(pix,&ix,&iy,&face_num,order_);
return xyf2nest(ix,iy,face_num,order_);
}
long nest2peano(long pix, long order_)
{
static const unsigned long subpix[8][4] = {
{ 0, 1, 3, 2 }, { 3, 0, 2, 1 }, { 2, 3, 1, 0 }, { 1, 2, 0, 3 },
{ 0, 3, 1, 2 }, { 1, 0, 2, 3 }, { 2, 1, 3, 0 }, { 3, 2, 0, 1 } };
static const unsigned long subpath[8][4] = {
{ 4, 0, 6, 0 }, { 7, 5, 1, 1 }, { 2, 4, 2, 6 }, { 3, 3, 7, 5 },
{ 0, 2, 4, 4 }, { 5, 1, 5, 3 }, { 6, 6, 0, 2 }, { 1, 7, 3, 7 } };
static const unsigned long face2path[12] = {
2, 5, 2, 5, 3, 6, 3, 6, 2, 3, 2, 3 };
static const unsigned long face2peanoface[12] = {
0, 5, 6, 11, 10, 1, 4, 7, 2, 3, 8, 9 };
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
long face = pix>>(2*order_);
unsigned long path = face2path[face];
long result = 0;
long shift;
for(shift=2*order_-2; shift>=0; shift-=2)
{
unsigned char spix = (pix>>shift) & 0x3;
result <<= 2;
result |= subpix[path][spix];
path=subpath[path][spix];
}
return result + ((face2peanoface[face])<<(2*order_));
}
long peano2nest(long pix, long order_)
{
static const unsigned long subpix[8][4] = {
{ 0, 1, 3, 2 }, { 1, 3, 2, 0 }, { 3, 2, 0, 1 }, { 2, 0, 1, 3 },
{ 0, 2, 3, 1 }, { 1, 0, 2, 3 }, { 3, 1, 0, 2 }, { 2, 3, 1, 0 } };
static const unsigned long subpath[8][4] = {
{ 4, 0, 0, 6 }, { 5, 1, 1, 7 }, { 6, 2, 2, 4 }, { 7, 3, 3, 5 },
{ 0, 4, 4, 2 }, { 1, 5, 5, 3 }, { 2, 6, 6, 0 }, { 3, 7, 7, 1 } };
static const unsigned long face2path[12] = {
2, 6, 2, 3, 3, 5, 2, 6, 2, 3, 3, 5 };
static const unsigned long peanoface2face[12] = {
0, 5, 8, 9, 6, 1, 2, 7, 10, 11, 4, 3 };
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
long face = pix>>(2*order_);
unsigned long path = face2path[face];
long result = 0;
long shift;
for(shift=2*order_-2; shift>=0; shift-=2)
{
unsigned long spix = (pix>>shift) & 0x3;
result <<= 2;
result |= subpix[path][spix];
path=subpath[path][spix];
}
return result + ((peanoface2face[face])<<(2*order_));
}
long ang2ring(double theta, double phi, long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
long nside_ = 1;
nside_ = nside_ << order_;
long npface_ = 1;
npface_ = npface_ << (2*order_);
long ncap_ = (npface_-nside_)<<1;
double z = cos(theta);
double za = fabs(z);
double tt = phi;
long tt_long = (floor(tt/2/M_PI));
tt = tt - ((double) (tt_long))*2*M_PI;
tt *= M_2_PI; // in [0,4)
if(za <= 2.0/3.0) // Equatorial region
{
double temp1 = nside_*(0.5+tt);
double temp2 = nside_*z*0.75;
long jp = (long) (temp1-temp2); // index of ascending edge line
long jm = (long) (temp1+temp2); // index of descending edge line
// ring number counted from z=2/3
long ir = nside_ + 1 + jp - jm; // in {1,2n+1}
long kshift = 1-(ir&1); // kshift=1 if ir even, 0 otherwise
long ip = (jp+jm-nside_+kshift+1)/2; // in {0,4n-1}
ip = imodulo(ip,4*nside_);
return ncap_ + (ir-1)*4*nside_ + ip;
}
else // North & South polar caps
{
double tp = tt- ((long) (tt));
double tmp = nside_*sqrt(3*(1-za));
long jp = (long) (tp*tmp); // increasing edge line index
long jm = (long) ((1.0-tp)*tmp); // decreasing edge line index
long ir = jp+jm+1; // ring number counted from the closest pole
long ip = (long) (tt*ir); // in {0,4*ir-1}
ip = imodulo(ip,4*ir);
if(z>0)
return 2*ir*(ir-1) + ip;
else
return npix_ - 2*ir*(ir+1) + ip;
}
}
long ang2nest(double theta, double phi, long inorder_)
{
//done at highest resolution and then degraded to specified resolution
long order_ = 29;
long nside_ = 1;
nside_ = nside_ << order_;
long innside_ = 1;
innside_ = innside_ << inorder_;
double z = cos(theta);
double za = fabs(z);
double tt = phi;
long tt_long = (floor(tt/2/M_PI));
tt = tt - ((double) (tt_long))*2*M_PI;
tt *= M_2_PI; // in [0,4)
long face_num, ix, iy;
if(za<=2.0/3.0) // Equatorial region
{
double temp1 = nside_*(0.5+tt);
double temp2 = nside_*(z*0.75);
long jp = (long) (temp1-temp2); // index of ascending edge line
long jm = (long) (temp1+temp2); // index of descending edge line
long ifp = jp >> order_; // in {0,4}
long ifm = jm >> order_;
if(ifp == ifm) // faces 4 to 7
face_num = (ifp==4) ? 4: ifp+4;
else if(ifp < ifm) // (half-)faces 0 to 3
face_num = ifp;
else // (half-)faces 8 to 11
face_num = ifm + 8;
ix = jm & (nside_-1);
iy = nside_ - (jp & (nside_-1)) - 1;
}
else // polar region, za > 2/3
{
long ntt = (long) (tt);
if (ntt>=4) ntt=3;
double tp = tt-ntt;
double tmp = nside_*sqrt(3*(1-za));
long jp = (long) (tp*tmp); // increasing edge line index
long jm = (long) ((1.0-tp)*tmp); // decreasing edge line index
if(jp>=nside_) jp = nside_-1; // for points too close to the boundary
if(jm>=nside_) jm = nside_-1;
if(z >= 0)
{
face_num = ntt; // in {0,3}
ix = nside_ - jm - 1;
iy = nside_ - jp - 1;
}
else
{
face_num = ntt + 8; // in {8,11}
ix = jp;
iy = jm;
}
}
long opix = xyf2nest(ix,iy,face_num,order_);
//fprintf(stderr,"opix = %ld\n",opix);
//degrade to inorder_ map resolution
long ip = opix - nside_*nside_*face_num;
//fprintf(stderr,"ip = %ld, nside_ = %ld, innside_ = %ld\n",ip,nside_,innside_);
long difffac = 1;
difffac = difffac << 2*(order_ - inorder_);
ip = ip/difffac;
//fprintf(stderr,"ip = %ld, nside_ = %ld, innside_ = %ld\n",ip,nside_,innside_);
return (ip + face_num*innside_*innside_);
}
long vec2nest(double *vec, long order_)
{
double theta,phi;
vec2ang(vec,&theta,&phi);
return ang2nest(theta,phi,order_);
}
long vec2ring(double *vec, long order_)
{
double theta,phi;
vec2ang(vec,&theta,&phi);
return ang2ring(theta,phi,order_);
}
void ring2ang(long pix, double *theta, double *phi, long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
long nside_ = 1;
nside_ = nside_ << order_;
long npface_ = 1;
npface_ = npface_ << (2*order_);
long ncap_ = (npface_-nside_)<<1;
double z;
double fact2_ = 4./npix_;
double fact1_ = (nside_<<1)*fact2_;
if(pix<ncap_) // North Polar cap
{
long iring = (long) (0.5*(1+isqrt(1+2*pix))); //counted from North pole
long iphi = (pix+1) - 2*iring*(iring-1);
z = 1.0 - (iring*iring)*fact2_;
*theta = acos(z);
*phi = (iphi-0.5) * M_PI_2/iring;
}
else if(pix<(npix_-ncap_)) // Equatorial region
{
long ip = pix - ncap_;
long iring = ip/(4*nside_) + nside_; // counted from North pole
long iphi = ip%(4*nside_) + 1;
// 1 if iring+nside is odd, 1/2 otherwise
double fodd = ((iring+nside_)&1) ? 1 : 0.5;
long nl2 = 2*nside_;
z = (nl2-iring)*fact1_;
*theta = acos(z);
*phi = (iphi-fodd) * M_PI/nl2;
}
else // South Polar cap
{
long ip = npix_ - pix;
long iring = (long) (0.5*(1+isqrt(2*ip-1))); //counted from South pole
long iphi = 4*iring + 1 - (ip - 2*iring*(iring-1));
z = -1.0 + (iring*iring)*fact2_;
*theta = acos(z);
*phi = (iphi-0.5) * M_PI_2/iring;
}
}
void ring2vec(long pix, double *vec, long order_)
{
double theta,phi;
ring2ang(pix,&theta,&phi,order_);
ang2vec(vec,theta,phi);
}
void nest2ang(long pix, double *theta, double *phi, long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
assert(pix >= 0 && pix < npix_);
long nside_ = 1;
nside_ = nside_ << order_;
double z;
double fact2_ = 4./npix_;
double fact1_ = (nside_<<1)*fact2_;
long nl4 = nside_*4;
long face_num, ix, iy;
nest2xyf(pix,&ix,&iy,&face_num,order_);
long jr = ((jrll[face_num])<<order_) - ix - iy - 1;
long nr;
long kshift;
if (jr<nside_)
{
nr = jr;
z = 1 - nr*nr*fact2_;
kshift = 0;
}
else if (jr > 3*nside_)
{
nr = nl4-jr;
z = nr*nr*fact2_ - 1;
kshift = 0;
}
else
{
nr = nside_;
z = (2*nside_-jr)*fact1_;
kshift = (jr-nside_)&1;
}
long jp = (jpll[face_num]*nr + ix -iy + 1 + kshift) / 2;
if (jp>nl4) jp-=nl4;
if (jp<1) jp+=nl4;
*phi = (jp-(kshift+1)*0.5)*(M_PI_2/nr);
*theta = acos(z);
}
void nest2vec(long pix, double *vec, long order_)
{
double theta,phi;
nest2ang(pix,&theta,&phi,order_);
ang2vec(vec,theta,phi);
}
void getneighbors_nest(long pix, long *result, long order_)
{
static const long xoffset[] = { -1,-1, 0, 1, 1, 1, 0,-1 };
static const long yoffset[] = { 0, 1, 1, 1, 0,-1,-1,-1 };
static const long facearray[][12] =
{ { 8, 9,10,11,-1,-1,-1,-1,10,11, 8, 9 }, // S
{ 5, 6, 7, 4, 8, 9,10,11, 9,10,11, 8 }, // SE
{ -1,-1,-1,-1, 5, 6, 7, 4,-1,-1,-1,-1 }, // E
{ 4, 5, 6, 7,11, 8, 9,10,11, 8, 9,10 }, // SW
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }, // center
{ 1, 2, 3, 0, 0, 1, 2, 3, 5, 6, 7, 4 }, // NE
{ -1,-1,-1,-1, 7, 4, 5, 6,-1,-1,-1,-1 }, // W
{ 3, 0, 1, 2, 3, 0, 1, 2, 4, 5, 6, 7 }, // NW
{ 2, 3, 0, 1,-1,-1,-1,-1, 0, 1, 2, 3 } }; // N
static const long swaparray[][12] =
{ { 0,0,0,0,0,0,0,0,3,3,3,3 }, // S
{ 0,0,0,0,0,0,0,0,6,6,6,6 }, // SE
{ 0,0,0,0,0,0,0,0,0,0,0,0 }, // E
{ 0,0,0,0,0,0,0,0,5,5,5,5 }, // SW
{ 0,0,0,0,0,0,0,0,0,0,0,0 }, // center
{ 5,5,5,5,0,0,0,0,0,0,0,0 }, // NE
{ 0,0,0,0,0,0,0,0,0,0,0,0 }, // W
{ 6,6,6,6,0,0,0,0,0,0,0,0 }, // NW
{ 3,3,3,3,0,0,0,0,0,0,0,0 } }; // N
long ix, iy, face_num;
nest2xyf(pix,&ix,&iy,&face_num,order_);
long nside_ = 1;
nside_ = nside_ << order_;
long nsm1 = nside_-1;
if((ix>0)&&(ix<nsm1)&&(iy>0)&&(iy<nsm1))
{
int m;
for(m=0; m<8; ++m)
result[m] = xyf2nest(ix+xoffset[m],iy+yoffset[m],face_num,order_);
}
else
{
long i;
for(i=0; i<8; ++i)
{
long x=ix+xoffset[i];
long y=iy+yoffset[i];
long nbnum=4;
if (x<0)
{ x+=nside_; nbnum-=1; }
else if (x>=nside_)
{ x-=nside_; nbnum+=1; }
if (y<0)
{ y+=nside_; nbnum-=3; }
else if (y>=nside_)
{ y-=nside_; nbnum+=3; }
long f = facearray[nbnum][face_num];
if(f>=0)
{
if (swaparray[nbnum][face_num]&1) x=nside_-x-1;
if (swaparray[nbnum][face_num]&2) y=nside_-y-1;
if (swaparray[nbnum][face_num]&4)
{
long temp = x;
x = y;
y = temp;
//swap(x,y);
}
result[i] = xyf2nest(x,y,f,order_);
}
else
result[i] = -1;
}
}
}
void getneighbors_ring(long pix, long *result, long order_)
{
static const long xoffset[] = { -1,-1, 0, 1, 1, 1, 0,-1 };
static const long yoffset[] = { 0, 1, 1, 1, 0,-1,-1,-1 };
static const long facearray[][12] =
{ { 8, 9,10,11,-1,-1,-1,-1,10,11, 8, 9 }, // S
{ 5, 6, 7, 4, 8, 9,10,11, 9,10,11, 8 }, // SE
{ -1,-1,-1,-1, 5, 6, 7, 4,-1,-1,-1,-1 }, // E
{ 4, 5, 6, 7,11, 8, 9,10,11, 8, 9,10 }, // SW
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }, // center
{ 1, 2, 3, 0, 0, 1, 2, 3, 5, 6, 7, 4 }, // NE
{ -1,-1,-1,-1, 7, 4, 5, 6,-1,-1,-1,-1 }, // W
{ 3, 0, 1, 2, 3, 0, 1, 2, 4, 5, 6, 7 }, // NW
{ 2, 3, 0, 1,-1,-1,-1,-1, 0, 1, 2, 3 } }; // N
static const long swaparray[][12] =
{ { 0,0,0,0,0,0,0,0,3,3,3,3 }, // S
{ 0,0,0,0,0,0,0,0,6,6,6,6 }, // SE
{ 0,0,0,0,0,0,0,0,0,0,0,0 }, // E
{ 0,0,0,0,0,0,0,0,5,5,5,5 }, // SW
{ 0,0,0,0,0,0,0,0,0,0,0,0 }, // center
{ 5,5,5,5,0,0,0,0,0,0,0,0 }, // NE
{ 0,0,0,0,0,0,0,0,0,0,0,0 }, // W
{ 6,6,6,6,0,0,0,0,0,0,0,0 }, // NW
{ 3,3,3,3,0,0,0,0,0,0,0,0 } }; // N
long ix, iy, face_num;
ring2xyf(pix,&ix,&iy,&face_num,order_);
long nside_ = 1;
nside_ = nside_ << order_;
long nsm1 = nside_-1;
if((ix>0)&&(ix<nsm1)&&(iy>0)&&(iy<nsm1))
{
long m;
for(m=0; m<8; ++m)
result[m] = xyf2ring(ix+xoffset[m],iy+yoffset[m],face_num,order_);
}
else
{
long i;
for(i=0; i<8; ++i)
{
long x=ix+xoffset[i];
long y=iy+yoffset[i];
long nbnum=4;
if (x<0)
{ x+=nside_; nbnum-=1; }
else if (x>=nside_)
{ x-=nside_; nbnum+=1; }
if (y<0)
{ y+=nside_; nbnum-=3; }
else if (y>=nside_)
{ y-=nside_; nbnum+=3; }
long f = facearray[nbnum][face_num];
if (f>=0)
{
if (swaparray[nbnum][face_num]&1) x=nside_-x-1;
if (swaparray[nbnum][face_num]&2) y=nside_-y-1;
if (swaparray[nbnum][face_num]&4)
{
long temp = x;
x = y;
y = temp;
//swap(x,y);
}
result[i] = xyf2ring(x,y,f,order_);
}
else
result[i] = -1;
}
}
}
void get_ring_info2(long ring, long *startpix, long *ringpix, double *costheta, double *sintheta, long *shifted, long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
long nside_ = 1;
nside_ = nside_ << order_;
long npface_ = 1;
npface_ = npface_ << (2*order_);
long ncap_ = (npface_-nside_)<<1;
double fact2_ = 4./npix_;
double fact1_ = (nside_<<1)*fact2_;
long northring = (ring>2*nside_) ? 4*nside_-ring : ring;
if(northring < nside_)
{
double tmp = northring*northring*fact2_;
*costheta = 1 - tmp;
*sintheta = sqrt(tmp*(2-tmp));
//double theta = atan2(*sintheta,*costheta);
*ringpix = 4*northring;
*shifted = 1;
*startpix = 2*northring*(northring-1);
}
else
{
*costheta = (2*nside_-northring)*fact1_;
*sintheta = sqrt((1.0 - *costheta)*(1.0 + *costheta));
//*theta = acos(*costheta);
*ringpix = 4*nside_;
if(((northring-nside_) & 1) == 0)
*shifted = 1;
else
*shifted = 0;
*startpix = ncap_ + (northring-nside_)*(*ringpix);
}
if(northring != ring) // southern hemisphere
{
//*theta = M_PI-(*theta);
*costheta = -1.0*(*costheta);
*startpix = npix_ - (*startpix) - (*ringpix);
}
}
long ring_above(double z, long order_)
{
long nside_ = 1;
nside_ = nside_ << order_;
double az=fabs(z);
if (az > 2.0/3.0) // polar caps
{
long iring = (long) (nside_*sqrt(3*(1-az)));
return (z>0) ? iring : 4*nside_-iring-1;
}
else // ----- equatorial region ---------
return (long) (nside_*(2-1.5*z));
}
/* returns ring indexed pixels and weights for interpolation */
void get_interpol(double theta, double phi, long pix[4], double wgt[4], long order_)
{
long npix_ = 1;
npix_ = 12*(npix_ << (2*order_));
long nside_ = 1;
nside_ = nside_ << order_;
double z = cos(theta);
long ir1 = ring_above(z,order_);
long ir2 = ir1+1;
double theta1=0.0, theta2=0.0, w1, tmp, dphi;
double cth1,cth2,sth1,sth2;
long sp,nr;
long shift;
long i1,i2;
if (ir1>0)
{
get_ring_info2(ir1,&sp,&nr,&cth1,&sth1,&shift,order_);
theta1 = atan2(sth1,cth1);
//get_ring_info2 (ir1, sp, nr, theta1, shift);
dphi = 2.0*M_PI/nr;
tmp = (phi/dphi - .5*shift);
i1 = (tmp<0) ? ((long) (tmp))-1 : (long) (tmp);
w1 = (phi-(i1+.5*shift)*dphi)/dphi;
i2 = i1+1;
if (i1<0) i1 +=nr;
if (i2>=nr) i2 -=nr;
pix[0] = sp+i1; pix[1] = sp+i2;
wgt[0] = 1-w1; wgt[1] = w1;
}
if (ir2<(4*nside_))
{
get_ring_info2(ir2,&sp,&nr,&cth2,&sth2,&shift,order_);
theta2 = atan2(sth2,cth2);
//get_ring_info2 (ir2, sp, nr, theta2, shift);
dphi = 2.0*M_PI/nr;
tmp = (phi/dphi - .5*shift);
i1 = (tmp<0) ? ((long) (tmp))-1 : (long) (tmp);
w1 = (phi-(i1+.5*shift)*dphi)/dphi;
i2 = i1+1;