-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHealpixObj.f90
2377 lines (1959 loc) · 71.9 KB
/
HealpixObj.f90
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
module HealpixObj
use healpix_types, ONLY: SP,DP,I4B,I8B,SPC
USE head_fits, ONLY : add_card, get_card
USE pix_tools, ONLY : npix2nside, nside2npix, query_disc
USE fitstools, ONLY : getsize_fits, input_map, read_par, read_dbintab, write_asctab, &
dump_alms,write_bintab, fits2alms
USE spinalm_tools
use AMLutils
implicit none
real(SP), parameter :: mK = 1. !internal units/microK
REAL(SP) :: fmissval = -1.6375e-30
real(DP), parameter :: HO_PI = 3.14159265358979323846264338328d0, &
HO_twopi=2*HO_pi, HO_fourpi=4*HO_pi
integer, parameter :: ord_ring = 1, ord_nest = 2
integer, parameter :: C_T = 1, C_E = 2, C_B = 3, C_C = 4
logical :: RandInited = .false.
integer, parameter :: nospinmap = 0
type HealpixMap
integer(I_NPIX) npix
integer(I4B) nmaps, ordering, nside, type
REAL(SP), dimension(:,:), pointer :: TQU => NULL()
COMPLEX(SP), dimension(:), pointer :: SpinField => NULL()
REAL(SP), dimension(:), pointer :: Phi => NULL()
integer :: spin
logical :: HasPhi
end type HealpixMap
type HealpixAlm
integer(I4B) lmax, npol, spin
logical HasPhi
COMPLEX(KIND=SPC), DIMENSION(:,:,:), pointer :: TEB => NULL() !T,E,B index, l,m
COMPLEX(KIND=SPC), DIMENSION(:,:,:), pointer :: SpinEB => NULL() ! E,B index, l, m
COMPLEX(KIND=SPC), DIMENSION(:,:,:), pointer :: Phi => NULL() !Has to be 3D array for Healpix defs
end type HealpixAlm
type HealpixPower
!Raw Cls
!read from text files in l(l+1)C_l/(2pi microK^2)
!PhiCl is lensing potential phi-phi and phi-T and phi-E.
!Phi is read in above units, but stored here as dimensionless
integer(I4B) lmax
logical pol, lens
REAL(SP), DIMENSION(:,:), POINTER :: Cl => NULL()
REAL(SP), DIMENSION(:,:), POINTER :: PhiCl => NULL()
end type HealpixPower
type HaarComponents
integer order
logical pol
type(HealpixMap) degraded
type(HealpixMap), dimension(:), pointer :: details => NULL()
end type HaarComponents
contains
subroutine HealpixPower_Init(P, almax, pol, dolens, nofree)
Type(HealpixPower) P
integer, intent(in) :: almax
logical, intent(in) :: pol
logical, intent(in), optional :: dolens
logical, intent(in), optional :: nofree
logical dofree
if (present(nofree)) then
dofree=.not. nofree
else
dofree = .true.
end if
if (dofree) call HealpixPower_Free(P)
P%lmax = almax
P%pol = pol
P%lens = .false.
if (present(dolens)) then
P%lens = dolens
end if
if (pol) then
allocate(P%Cl(0:almax,4))
else
allocate(P%Cl(0:almax,1))
end if
P%Cl=0
if (P%lens) then
allocate(P%PhiCl(0:almax,3))
P%PhiCl = 0
end if
end subroutine HealpixPower_Init
subroutine HealpixPower_Nullify(P)
Type(HealpixPower) P
nullify(P%Cl, P%PhiCl)
end subroutine HealpixPower_Nullify
subroutine HealpixPower_Free(P)
Type(HealpixPower) P
integer status
deallocate(P%Cl, stat = status)
deallocate(P%PhiCl, stat = status)
call HealpixPower_Nullify(P)
end subroutine HealpixPower_Free
subroutine HealpixPower_Assign(P, Pin)
Type(HealpixPower) P, Pin
call HealpixPower_Init(P, Pin%lmax, Pin%pol, Pin%lens)
P%Cl = Pin%Cl
if (Pin%lens) P%PhiCl = Pin%PhiCl
end subroutine HealpixPower_Assign
subroutine HealpixPower_ReadFromTextFile(P, f, lmax, pol, dolens, full_phi_cross)
use AMLutils
Type(HealpixPower) P
character(LEN=*), intent(IN) :: f
integer, intent(in) :: lmax
logical,intent(in), optional :: dolens, pol, full_phi_cross
logical full_phi
integer i,l, ColNum
real(DP) T,E,B,TE, scal, phi, phiT, phiE
logical tensors, dolens2, pol2
integer, parameter :: io_unit= 1
character(LEN=200) :: InLine
real(sp) test(9)
real(sp), parameter :: COBE_CMBTemp = 2.726
open(unit=io_unit,file=f,form='formatted',status='old')
call HealpixPower_Free(P)
dolens2=.false.
if (present(dolens)) then
dolens2 = dolens
end if
pol2 = .false.
if (present(pol)) then
pol2 = pol
end if
if (present(full_phi_cross)) then
full_phi=full_phi_cross
else
full_phi=.false.
end if
call HealpixPower_Init(P, lmax, pol = pol2, dolens = dolens2)
!See how many columns - includes magnetic polarization if four columns
read(io_unit,'(a)') InLine
do l=9,2,-1
Colnum=l
read(InLine,*, end=110) test(1:l)
exit
110 cycle
end do
tensors = colnum==5 .or. colnum > 7
if (P%lens .and. (colnum < 6 .or. colnum>8)) call MpiStop('can''t indentify text phi cl columns')
if (pol2 .and. colnum<3) call mpiStop('No polarization in C_l file' )
rewind io_unit
B=0
phiE=0
P%Cl = 0
if (dolens2) P%PhiCl = 0
do i=0,lmax
if (Colnum==2) then
read (io_unit,*,end=118) l, T
else
if (tensors) then
if (P%lens) then
if (colnum ==8) then
read (io_unit,*,end=118) l, T, E, B , TE, phi, phiT, phiE
else
read (io_unit,*,end=118) l, T, E, B , TE, phi, phiT
end if
else
read (io_unit,*,end=118) l, T, E, B , TE
end if
else
if (P%lens) then
read (io_unit,*,end=118) l, T, E, TE, phi, phiT
else
read (io_unit,*,end=118) l, T, E, TE
end if
end if
end if
if (l==0) then
P%Cl(l,1) = T/mK**2
else if (l<= lmax) then
scal=twopi/(l*(l+1))/mK**2
P%Cl(l,1) = T*scal
if (pol2 .and. l>=2 ) then
P%Cl(l,2) = E*scal
P%Cl(l,3) = B*scal
P%Cl(l,4) = TE*scal
end if
if (P%Lens .and. l>=1) then
if (colnum==8) then
! lens_potential_output_file from CAMB May 2010+
P%PhiCl(l,1) = phi * twopi/real(l*(l+1),dp)**2
if (l>300 .and. .not. full_phi) then
!just kill numerical noise on T and E lensing correlations on small scales
P%PhiCl(l,2:3)=0
else
P%PhiCl(l,2) = phiT * twopi/real(l*(l+1),dp)**1.5/mK
P%PhiCl(l,3) = phiE * twopi/real(l*(l+1),dp)**1.5/mK
end if
else
P%PhiCl(l,1) = phi/real(l,dp)**4/1e12/COBE_CMBTemp**2
P%PhiCl(l,2) = phiT/real(l,dp)**3/1e6/COBE_CMBTemp/mK
end if
end if
end if
enddo
118 close(io_unit)
end subroutine HealpixPower_ReadFromTextFile
subroutine HealpixPower_Write(P,fname)
use AMLutils
Type(HealpixPower) P
character(Len=*), intent(in) :: fname
integer l
real fac
call CreateTxtFile(fname,1)
if (P%pol) then
if (P%lens) then
write (1,'(1I7,7E17.7)') 0,P%Cl(0,:) *mK**2, 0.,0.,0.
else
write (1,'(1I7,4E17.7)') 0,P%Cl(0,:) *mK**2
end if
else
if (P%lens) then
write (1,'(1I7,3E17.7)') 0,P%Cl(0,1) *mK**2, 0.,0.
else
write (1,'(1I7,1E17.7)') 0,P%Cl(0,1) *mK**2
end if
end if
do l=1,P%lmax
fac = l*(l+1)
if (P%pol) then
if (P%lens) then
write (1,'(1I7,7E17.7)') l,fac*P%Cl(l,:)/twopi *mK**2, &
fac**2*P%PhiCl(l,1)/twopi,fac**1.5*P%PhiCl(l,2:3)/twopi * mK
else
write (1,'(1I7,4E17.7)') l,fac*P%Cl(l,:)/twopi *mK**2
end if
else
if (P%lens) then
write (1,'(1I7,3E17.7)') l,fac*P%Cl(l,1)/twopi *mK**2, &
fac**2*P%PhiCl(l,1)/twopi,fac**1.5*P%PhiCl(l,2)/twopi * mK
else
write (1,'(1I7,1E17.7)') l,fac*P%Cl(l,1)/twopi *mK**2
end if
end if
end do
close(1)
end subroutine HealpixPower_Write
subroutine HealpixPower_AddPower(Ptotal, P, AddPhi)
!Adds P to PTotal (useful for getting means over realisations).
Type(HealpixPower) P, Ptotal
logical, intent(in) :: AddPhi
if (all(shape(P%Cl) /= shape(Ptotal%Cl))) &
call MpiStop('HealpixPower_AddPower: must have same sized power spectra')
Ptotal%Cl = Ptotal%Cl + P%Cl
if (AddPhi) then
if (.not. PTotal%lens .or. .not. P%lens) call MpiStop('HealpixPower_AddPower: must both have phi')
Ptotal%PhiCl = PTotal%phiCl + P%PhiCl
end if
end subroutine HealpixPower_AddPower
subroutine HealpixPower_Smooth(P,fwhm, sgn)
Type(Healpixpower) :: P
integer l, sn
integer, intent(in), optional :: sgn
real(dp) xlc,sigma2,fwhm
if (present(sgn)) then
sn = sgn
else
sn = -1
end if
xlc= 180*sqrt(8.*log(2.))/HO_pi
sigma2 = (fwhm/xlc)**2
do l=2,P%lmax
P%Cl(l,:) = P%Cl(l,:)*exp(sn*l*(l+1)*sigma2)
end do
end subroutine HealpixPower_Smooth
subroutine HealpixPower_Smooth_Beam(P,beam, sgn)
Type(Healpixpower) :: P
integer l, sn
integer, intent(in), optional :: sgn
real(dp), intent(in) :: beam(0:)
if (present(sgn)) then
sn = sgn
else
sn = -1
end if
do l=0,P%lmax
if (sn==-1) then
P%Cl(l,:) = P%Cl(l,:)* beam(l)**2
else
P%Cl(l,:) = P%Cl(l,:)/ beam(l)**2
end if
end do
end subroutine HealpixPower_Smooth_Beam
subroutine HealpixPower_Smooth_Beam2(P,beam1,beam2, sgn)
Type(Healpixpower) :: P
integer l, sn
integer, intent(in), optional :: sgn
real(dp), intent(in) :: beam1(0:), beam2(0:)
if (present(sgn)) then
sn = sgn
else
sn = -1
end if
do l=0,P%lmax
if (sn==-1) then
P%Cl(l,:) = P%Cl(l,:)* beam1(l)*beam2(l)
else
P%Cl(l,:) = P%Cl(l,:)/ ( beam1(l)*beam2(l))
end if
end do
end subroutine HealpixPower_Smooth_Beam2
subroutine HealpixAlm2Power(A,P)
Type(HealpixPower) P
Type(HealpixAlm) :: A
integer l,i,ix
call HealpixPower_Init(P,A%lmax,A%npol==3, A%HasPhi)
P%Cl(0:1,:) = 0
if (A%npol>0) then
do l=0, P%lmax
if (l<2) then
ix= 1
else
ix = A%npol
end if
do i = 1, ix
P%Cl(l,i) = ( REAL(A%TEB(i,l,0))**2 &
+ 2.*SUM(A%TEB(i,l,1:l)*CONJG(A%TEB(i,l,1:l)) )) / (2.*l + 1.)
end do
if (ix==3) then
P%Cl(l,4) = ( REAL(A%TEB(1,l,0))*REAL(A%TEB(2,l,0)) &
+ 2.*SUM(real(A%TEB(1,l,1:l)*CONJG(A%TEB(2,l,1:l))) ) &
) / (2.*l + 1.)
end if
end do
end if
if (A%HasPhi) then
do l=0, P%lmax
P%PhiCl(l,1) = ( REAL(A%Phi(1,l,0))**2 &
+ 2.*SUM(A%Phi(1,l,1:l)*CONJG(A%Phi(1,l,1:l)) )) / (2*l + 1)
if (A%npol >0) then
!T-phi
P%PhiCl(l,2) = ( REAL(A%TEB(1,l,0))*REAL(A%Phi(1,l,0)) &
+ 2.*SUM(real(A%TEB(1,l,1:l)*CONJG(A%Phi(1,l,1:l))) )) /(2*l + 1)
if (l>1 .and. A%npol >2) then
!E-phi
P%PhiCl(l,3) = ( REAL(A%TEB(2,l,0))*REAL(A%Phi(1,l,0)) &
+ 2.*SUM(real(A%TEB(2,l,1:l)*CONJG(A%Phi(1,l,1:l))) )) /(2*l + 1)
end if
end if
end do
end if
end subroutine HealpixAlm2Power
subroutine HealpixAlm2CrossPower(A,A2, P)
Type(HealpixPower) P
Type(HealpixAlm) :: A, A2
integer l,i,ix
if (A%lmax /= A2%lmax) call MpiStop('HealpixAlm2CrossPower: mismatched lmax')
if (A%npol /= A2%npol) call MpiStop('HealpixAlm2CrossPower: different pol content')
call HealpixPower_Init(P,A%lmax,A%npol==3)
P%Cl(0:1,:) = 0
do l=0, P%lmax
if (l<2) then
ix= 1
else
ix = A%npol
end if
do i = 1, ix
P%Cl(l,i) = ( REAL(A%TEB(i,l,0)*A2%TEB(i,l,0)) &
+ 2.*SUM(A%TEB(i,l,1:l)*CONJG(A2%TEB(i,l,1:l)) )) / (2.*l + 1.)
end do
if (ix==3) then
P%Cl(l,4) = ( REAL(A%TEB(1,l,0))*REAL(A2%TEB(2,l,0)) &
+ 2.*SUM(real(A%TEB(1,l,1:l)*CONJG(A2%TEB(2,l,1:l))) ) &
) / (2.*l + 1.)
end if
end do
end subroutine HealpixAlm2CrossPower
subroutine HealpixAlm2CrossPhi(A,A2, P)
Type(HealpixPower) P
Type(HealpixAlm) :: A, A2
integer l,i,ix
call HealpixPower_Init(P,min(A%lmax,A2%lmax), pol=.false., dolens=.true.)
do l=0, P%lmax
P%PhiCl(l,1) = ( REAL(A%Phi(1,l,0)*A2%Phi(1,l,0)) &
+ 2.*SUM((A%Phi(1,l,1:l)*CONJG(A2%Phi(1,l,1:l)) ))) / (2*l + 1)
end do
end subroutine HealpixAlm2CrossPhi
subroutine HealpixAlm_Init(A,almax,npol,spinmap, HasPhi)
Type(HealpixAlm) :: A
integer, intent(in) :: almax
integer, intent(in), optional :: npol, spinmap
logical, intent(in), optional :: HasPhi
integer status
call HealpixAlm_Free(A)
A%lmax = almax
if (present(npol)) then
A%npol = npol
else
A%npol = 1
end if
if (A%npol /= 0) then
ALLOCATE(A%TEB(1:A%npol, 0:almax, 0:almax),stat = status)
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Init lmax = '//IntToStr(almax))
A%TEB=0
end if
if (present(spinmap)) then
if (spinmap /= nospinmap) then
if (spinmap<1 .or. spinmap > 3) call mpiStop( 'Spin must be 0<spin<4')
ALLOCATE(A%SpinEB(2, 0:almax, 0:almax),stat = status)
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Init spinmap')
end if
A%spin= spinmap
else
A%spin = nospinmap
end if
if (present(HasPhi)) then
A%HasPhi = HasPhi
else
A%HasPhi = .false.
end if
if (A%HasPhi) then
ALLOCATE(A%Phi(1:1,0:almax, 0:almax),stat = status)
A%Phi = 0
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Init phi')
end if
end subroutine HealpixAlm_Init
subroutine HealpixAlm_Assign(AOut, Ain, max_pol)
Type(HealpixAlm) :: AOut, Ain
integer, intent(in), optional :: max_pol
integer status
integer maxp
if (present(max_pol)) then
maxp=max_pol
else
maxp =Ain%npol
end if
call HealpixAlm_Free(AOut)
Aout = Ain
nullify(AOut%TEB, AOut%SpinEB, AOut%Phi)
if (maxp>0) then
ALLOCATE(AOut%TEB(1:maxp, 0:AOut%lmax, 0:AOut%lmax),stat = status)
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Assign')
AOut%TEB(1:maxp,:,:)= Ain%TEB(1:maxp,:,:)
end if
if (AIn%spin /= nospinmap) then
ALLOCATE(AOut%SpinEB(2, 0:AOut%lmax, 0:AOut%lmax),stat = status)
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Assign')
AOut%SpinEB = Ain%SpinEB
end if
if (AIn%HasPhi) then
ALLOCATE(AOut%Phi(1:1,0:AOut%lmax, 0:AOut%lmax),stat = status)
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Assign')
AOut%Phi = Ain%Phi
end if
end subroutine HealpixAlm_Assign
subroutine HealpixAlm_Nullify(A)
Type(HealpixAlm) :: A
nullify(A%TEB)
nullify(A%SpinEB)
nullify(A%Phi)
end subroutine HealpixAlm_Nullify
subroutine HealpixAlm_Free(A)
Type(HealpixAlm) :: A
integer status
deallocate(A%TEB,stat=status)
deallocate(A%SpinEB,stat=status)
deallocate(A%Phi,stat=status)
call HealpixAlm_Nullify(A)
end subroutine HealpixAlm_Free
subroutine HealpixAlm_PhiOnly(A)
Type(HealpixAlm) :: A
integer status
deallocate(A%TEB,stat=status)
deallocate(A%SpinEB,stat=status)
nullify(A%TEB)
nullify(A%SpinEB)
A%spin = nospinmap
A%npol = 0
end subroutine
subroutine HealpixAlm_GradientOf(A, B, field, updown)
type(HealpixAlm) :: A, B
integer l
character(LEN=*), intent(in) :: field
character(LEN=*), intent(in), optional :: updown
logical Div
integer spin
if (field(1:1) /= 'S') then
spin = 1
else
if (.not. present(updown)) call MpiStop('HealpixAlm_GradientOf: Must say which derivative')
Div = updown(1:1) == 'D'
if (Div) then
! spin = A%spin-1
spin = 1
else
! spin = A%spin+1
spin = 3
end if
end if
call HealpixAlm_Init(B,A%lmax,0,spinmap= spin)
B%SpinEB = 0
do l=B%spin, A%lmax
if (field(1:1)=='P') then
B%SpinEB(1,l,0:l) = - EB_sign*sqrt(real(l*(l+1),dp))*A%Phi(1,l,0:l)
else if (field(1:1)=='T') then
B%SpinEB(1,l,0:l) = - EB_sign*sqrt(real(l*(l+1),dp))*A%TEB(1,l,0:l)
else if (field(1:1)=='S') then
if (Div) then
!Divergence
B%SpinEB(:,l,0:l) = sqrt(real((l+2)*(l-1),dp))*A%TEB(2:3,l,0:l)
else
!STF of outer product
B%SpinEB(:,l,0:l) = -sqrt(real((l+3)*(l-2),dp))*A%TEB(2:3,l,0:l)
end if
else
call MpiStop('HealpixAlm_GradientOf: Unknown field')
end if
end do
end subroutine HealpixAlm_GradientOf
subroutine HealpixAlm_PolToSpin2(A)
Type(HealpixAlm) :: A
integer status
deallocate(A%SpinEB, stat=status)
ALLOCATE(A%SpinEB(2, 0:A%lmax, 0:A%lmax),stat = status)
A%SpinEB(1:2,:,:) = A%TEB(2:3,:,:)
A%spin = 2
end subroutine HealpixAlm_PolToSpin2
subroutine HealpixAlm_Spin2ToPol(A)
Type(HealpixAlm) :: A
integer :: status
if (A%npol==0) then
A%npol = 3
ALLOCATE(A%TEB(1:A%npol, 0:A%lmax, 0:A%lmax),stat = status)
if (status /= 0) call MpiStop('No Mem: HealpixAlm_Spin2ToPol')
A%TEB=0
end if
A%TEB(2:3,:,:) = A%SpinEB(1:2,:,:)
end subroutine HealpixAlm_Spin2ToPol
subroutine HealpixAlm_Smooth(A,fwhm, sgn)
Type(HealpixAlm) :: A
integer l, sn
integer, intent(in), optional :: sgn
real(dp) xlc,sigma2,fwhm
if (present(sgn)) then
sn = sgn
else
sn = -1
end if
xlc= 180*sqrt(8.*log(2.))/HO_pi
sigma2 = (fwhm/xlc)**2
if (A%npol/=0) then
do l=2,A%lmax
A%TEB(:,l,:) = A%TEB(:,l,:)*exp(sn*l*(l+1)*sigma2/2)
end do
end if
if (A%spin /= nospinmap) then
do l=2,A%lmax
A%SpinEB(:,l,:) = A%SpinEB(:,l,:)*exp(sn*l*(l+1)*sigma2/2)
end do
end if
end subroutine HealpixAlm_Smooth
subroutine HealpixAlm_Smooth_Beam(A,Beam, sgn)
Type(HealpixAlm) :: A
integer l, sn
real(dp) :: beam(0:)
integer, intent(in), optional :: sgn
if (present(sgn)) then
sn = sgn
else
sn = -1
end if
do l=0,A%lmax
if (sn ==-1) then
A%TEB(:,l,:) = A%TEB(:,l,:)*beam(l)
else
A%TEB(:,l,:) = A%TEB(:,l,:)/beam(l)
end if
end do
end subroutine HealpixAlm_Smooth_Beam
subroutine HealpixAlm_Read(A, fname, lmax, npol_in)
Type(HealpixAlm) :: A
character(LEN=*) :: fname
integer, intent(in) :: lmax
integer, intent(in), optional ::npol_in
integer i, nalm, npol
real, allocatable :: alms(:,:,:)
character(len=80) header(80,3)
if (present(npol_in)) then
npol=npol_in
else
npol=1
end if
call HealpixAlm_Init(A, lmax, npol)
nalm = (lmax+1)*(lmax+2)/2
allocate(alms(1:nalm,1:(3+1),1:npol))
call fits2alms(fname, nalm, alms, 3, header, 80, npol)
do i=1,nalm
A%TEB(1,nint(alms(i,1,1)),nint(alms(i,2,1)))=cmplx(alms(i,3,1),alms(i,4,1))
if (npol>1) then
A%TEB(2,nint(alms(i,1,2)),nint(alms(i,2,2)))=cmplx(alms(i,3,2),alms(i,4,2))
A%TEB(3,nint(alms(i,1,3)),nint(alms(i,2,3)))=cmplx(alms(i,3,3),alms(i,4,3))
end if
end do
deallocate(alms)
end subroutine HealpixAlm_Read
subroutine HealpixAlm_RotateEuler(A, psi,theta,phi)
! Euler angle convention is right handed, active rotation
! psi is the first rotation about the z-axis (vertical), in [-2pi,2pi]
! then theta about the ORIGINAL (unrotated) y-axis, in [-2pi,2pi]
! then phi about the ORIGINAL (unrotated) z-axis (vertical), in [-2pi,2pi]
use alm_tools
Type(HealpixAlm) :: A
real(dp), intent(in) :: psi, theta,phi
call rotate_alm(A%lmax, A%TEB, psi, theta, phi)
end subroutine HealpixAlm_RotateEuler
subroutine HealpixAlm_RotateGalEcliptic(A)
use coord_v_convert
Type(HealpixAlm) :: A
real(dp) :: psi, theta,phi
call coordsys2euler_zyz(2000.d0, 2000.d0, 'G', 'E', psi, theta, phi)
call HealpixAlm_RotateEuler(A, psi, theta, phi)
end subroutine HealpixAlm_RotateGalEcliptic
subroutine HealpixAlm_RotateLBtoNorthPole(A, l, b) !l,b in degrees
Type(HealpixAlm) :: A
real(dp), intent(in) :: l, b
call HealpixAlm_RotateEuler(A, -L/180*HO_PI,-(90.d0-b)/180*HO_PI,0.d0)
end subroutine HealpixAlm_RotateLBtoNorthPole
subroutine HealpixMap_GetAzimCut(M, npix,rad, theta,phi)
!1 inside disc radius rad centred at theta, phi (radians)
use pix_tools
Type(HealpixMap) :: M
real(dp), intent(in) :: rad, theta, phi
integer(I_NPIX), intent(in) :: npix
real(dp) vec(3)
call ang2vec(theta,phi,vec)
call HealpixMap_GetAzimCutVec(M, npix, rad, vec)
end subroutine HealpixMap_GetAzimCut
subroutine HealpixMap_GetAzimCutVec(M, npix,rad, Vec)
!inside disc radius rad centred at vec
Type(HealpixMap) :: M
real(dp), intent(in) :: rad
integer(I_NPIX), intent(in) :: npix
integer(I4B), dimension(:), allocatable :: listpix
real(dp), intent(in) :: vec(3)
integer nlist
call HealpixMap_Init(M,npix,1)
allocate(listpix(0:npix-1))
call query_disc(M%nside,vec, rad, listpix,nlist)
M%TQU = 0
M%TQU(listpix(0:nlist-1),1) = 1
deallocate(listpix)
end subroutine HealpixMap_GetAzimCutVec
function HealpixMap_Vec2pix(M, vec) result(pix)
use pix_tools
Type(HealpixMap) :: M
integer :: pix
real(dp), intent(in) :: vec(3)
if (M%Ordering == ord_ring) then
call vec2pix_ring(M%nside,vec,pix)
else
call vec2pix_nest(M%nside,vec,pix)
end if
end function HealpixMap_Vec2pix
subroutine HealpixMap_Pix2Vec(M, pix,vec)
use pix_tools
Type(HealpixMap) :: M
integer, intent(in) :: pix
real(dp), intent(out) :: vec(3)
if (M%Ordering == ord_ring) then
call pix2vec_ring(M%nside,pix, vec)
else
call pix2vec_nest(M%nside,pix, vec)
end if
end subroutine HealpixMap_Pix2Vec
subroutine HealpixMap_Pix2Vertex(M, pix,vertex)
use pix_tools
Type(HealpixMap) :: M
integer, intent(in) :: pix
real(dp), intent(out) :: vertex(3,4)
real(dp) :: vec(3)
if (M%Ordering == ord_ring) then
call pix2vec_ring(M%nside,pix, vec, vertex)
else
call pix2vec_nest(M%nside,pix, vec, vertex)
end if
end subroutine HealpixMap_Pix2Vertex
function HealpixMap_Ang2Pix(M, theta, phi) result(pix)
use pix_tools
Type(HealpixMap) :: M
real(dp), intent(in):: theta, phi
integer pix
if (M%Ordering == ord_ring) then
call Ang2Pix_ring(M%nside,theta,phi,pix)
else
call Ang2Pix_nest(M%nside,theta,phi,pix)
end if
end function HealpixMap_Ang2Pix
subroutine HealpixMap_Pix2Ang(M, pix, theta, phi)
use pix_tools
Type(HealpixMap) :: M
integer(I_NPIX), intent(in) :: pix
real(dp), intent(out):: theta, phi
if (M%Ordering == ord_ring) then
call Pix2Ang_ring(M%nside,pix,theta,phi)
else
call Pix2Ang_nest(M%nside,pix,theta,phi)
end if
end subroutine HealpixMap_Pix2Ang
subroutine Healpix_GetRotation(R, theta, phi, chi)
!Rotate phi about z axis, then rotation by theta about new y axis, then chi about new z axis
real(dp), intent(in) :: theta, phi, chi
real(dp), intent(inout) :: R(3,3)
call MpiStop('Healpix_GetRotation: You''ll have to check this routine')
R(1,1) = cos(phi)*cos(theta)*cos(chi) - sin(phi)*sin(chi)
R(1,2) = sin(phi)*cos(theta)*cos(chi) + cos(phi)*sin(chi)
R(1,3) = -sin(theta)*cos(chi)
R(2,1) = -cos(phi)*cos(theta)*sin(chi) - sin(phi)*cos(chi)
R(2,2) = -sin(phi)*cos(theta)*sin(chi) + cos(phi)*cos(chi)
R(2,3) = sin(phi)*sin(chi)
R(3,1) = cos(phi)*sin(theta)
R(3,2) = sin(phi)*sin(theta)
R(3,3) = cos(theta)
end subroutine Healpix_GetRotation
subroutine HealpixMap_Rotate(M, MR, theta, phi, chi)
!This is very crude for temp
use pix_tools
Type(HealpixMap) :: M, MR
real(dp), intent(in) :: theta, phi, chi
integer(I_NPIX) i, ix
real(dp) vec(3), R(3,3)
call MpiStop('Don''t use this')
call Healpix_GetRotation(R, theta, phi, chi)
call HealpixMap_Init(MR, M%npix, M%nmaps)
call HealpixMap_ForceRing(M)
do i=0, MR%npix -1
call pix2vec_ring(MR%nside, i, vec)
vec = matmul(R,vec)
call vec2pix_ring(MR%nside, vec, ix)
MR%TQU(i,:) = M%TQU(ix,:)
end do
end subroutine HealpixMap_Rotate
function HealpixMap_EclipticPixel(M, theta,phi) result (res)
!Pixel on galactic map for point at given ecliptic coordinates
use pix_tools
use coord_v_convert
Type(HealpixMap) :: M
real(dp), intent(in) :: theta, phi
integer res
real(dp) vec(3), vecout(3)
call ang2vec(theta,phi,vec)
call xcc_DP_E_TO_G(vec,2000.d0,vecout)
res = HealpixMap_Vec2pix(M, vecout)
end function HealpixMap_EclipticPixel
subroutine HealpixMap_MarkEclipticPlane(M, val)
!Mark pixels on ecliptic plan by value val, assuming M galactic
Type(HealpixMap) :: M
real(dp) :: val, theta, phi, delta
integer i
theta = HO_pi/2
delta = 2*HO_pi/ (M%nside*8)
do i=1, M%nside*8
phi = i*delta
M%TQU(HealpixMap_EclipticPixel(M,theta,phi),:) = val
end do
end subroutine HealpixMap_MarkEclipticPlane
subroutine HealpixMap_MarkEcliptic(M, rad, val)
!inside disc radius rad
use pix_tools
use coord_v_convert
Type(HealpixMap) :: M
real(dp), intent(in) :: rad
real(dp), intent(in) :: val
integer(I4B), dimension(:), allocatable :: listpix
real(dp):: vec(3),vecout(3)
integer nlist
allocate(listpix(0:M%npix-1))
call ang2vec(0.d0,0.d0,vec)
call xcc_DP_E_TO_G(vec,2000.d0,vecout)
call query_disc(M%nside,vecout, rad, listpix,nlist)
M%TQU(listpix(0:nlist-1),1) = val
deallocate(listpix)
end subroutine HealpixMap_MarkEcliptic
subroutine HealpixMap_GalacticToEcliptic(M)
!Not at all optimal
Type(HealpixMap) M, M2
integer(I_NPIX) i
real(dp) theta,phi
call HealpixMap_Assign(M2,M)
do i=0, M%npix-1
call HealpixMap_Pix2Ang(M, i, theta, phi)
M%TQU(i,:) = M2%TQU(HealpixMap_EclipticPixel(M,theta,phi),:)
end do
call HealpixMap_Free(M2)
end subroutine HealpixMap_GalacticToEcliptic
subroutine HealpixMap_AddWhiteNoise(M, N_T, N_QU )
!N_T and N_QU are the N_l of the noise
use Random
Type(HealpixMap) :: M
real(sp), intent(in) :: N_T
real(sp), intent(in), optional :: N_QU
real(sp) amp
integer(I_NPIX) i
amp = sqrt(N_T*M%npix/(HO_fourpi))
do i=0, M%npix-1
M%TQU(i,1)= M%TQU(i,1) + Gaussian1()*amp
end do
if (present(N_QU) .and. M%nmaps>1) then
if (M%nmaps /= 3) call MpiStop('HealpixMap_AddWhiteNoise: No polarization in map')
amp = sqrt(N_QU*M%npix/HO_fourpi)
do i=0, M%npix-1
M%TQU(i,2)= M%TQU(i,2) + Gaussian1()*amp
end do
do i=0, M%npix-1
M%TQU(i,3)= M%TQU(i,3) + Gaussian1()*amp
end do
end if
end subroutine HealpixMap_AddWhiteNoise