-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs_DConeMaps.py~
1405 lines (982 loc) · 48.7 KB
/
funcs_DConeMaps.py~
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
import sys
import numpy as np
import scipy as sp
import os
import os.path
from scipy import ndimage
from astropy.io import fits as pf
import re
from copy import deepcopy
from astropy.wcs import WCS
from scipy import optimize
import time
from time import gmtime,strftime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from pylab import *
import matplotlib.colors as colors
include_path='/Users/simon/common/python/include/'
sys.path.append(include_path)
from Resamp import gridding
if not sys.warnoptions:
import os, warnings
#warnings.simplefilter("default") # Change the filter in this process
warnings.simplefilter("ignore") # Change the filter in this process
#os.environ["PYTHONWARNINGS"] = "default" # Also affect subprocesses
os.environ["PYTHONWARNINGS"] = "ignore" # Also affect subprocesses
def cartesian2conicpolar(outcoords, inputshape, origin, inc=0.,tanpsi=0.):
"""Coordinate transform for converting a polar array to Cartesian coordinates.
inputshape is a tuple containing the shape of the polar array. origin is a
tuple containing the x and y indices of where the origin should be in the
output array."""
rindex, thetaindex = outcoords
x0, y0 = origin
theta = float(thetaindex) * 2. * np.pi / float(inputshape[0])
y = rindex*np.cos(theta)
height = tanpsi * rindex
#height = -tanpsi * rindex
x = (rindex*np.sin(theta))/np.cos(inc) + \
(height - (rindex*np.sin(theta))*np.tan(inc))*np.sin(inc)
ix = -x + x0
iy = y + float(y0)
return (iy,ix)
def cartesian2polar(outcoords, inputshape, origin, inc=0.):
"""Coordinate transform for converting a polar array to Cartesian coordinates.
inputshape is a tuple containing the shape of the polar array. origin is a
tuple containing the x and y indices of where the origin should be in the
output array."""
rindex, thetaindex = outcoords
x0, y0 = origin
theta = float(thetaindex) * 2. * np.pi / float(inputshape[0]-1.)
y = rindex*np.cos(theta)
x = rindex*np.sin(theta)*np.cos(inc)
ix = -x + x0
iy = y + float(y0)
return (iy,ix)
def conicpolar2cartesian_ellipse(outcoords, inputshape, origin,inc=0.,tanpsi=0.):
yindex, xindex = outcoords
x0, y0 = origin
nx=inputshape[0]
ny=inputshape[1]
x = -float(xindex - x0)
y = float(yindex - y0)
tanpsi0=tanpsi
a=((np.tan(inc) * tanpsi0)**2-1.0)
b=-2.*x*np.sin(inc)* tanpsi0/(np.cos(inc))**2
c=y**2+(x**2/(np.cos(inc)**2))
Delta=b**2-4.*a*c
rho=(-b-np.sqrt(Delta))/(2.*a)
rindex = rho
if (rho == 0.):
costheta = 0.
else:
costheta = y / rho
theta=np.arccos(costheta)
thetaindex = (theta * float(nx) / (2. * np.pi))
return (rindex,thetaindex)
def polar2cartesian(outcoords, inputshape, origin,inc=0.):
yindex, xindex = outcoords
x0, y0 = origin
nx=inputshape[0]-1
ny=inputshape[1]-1
x = -float(xindex - x0)
y = float(yindex - y0)
rho=np.sqrt((x**2/(np.cos(inc)**2))+y**2)
rindex = rho
if (rho == 0.):
costheta = 0.
else:
costheta = y / rho
# theta=np.arccos(costheta)
theta = np.arctan2((-x/np.cos(inc)), y)
if (theta < 0):
theta = theta + 2.*np.pi
thetaindex = (theta * float(nx) / (2. * np.pi))
return (rindex,thetaindex)
# def exec_flatpolar(filename_fullim,hdr2,aresampim,PA,cosi):
# rotangle= PA - 180.
# # rotangle= PA
# im1rot = ndimage.rotate(aresampim, rotangle, reshape=False)
# fileout_rotated=re.sub('fullim.fits', 'rotated.fits', filename_fullim)
# pf.writeto(fileout_rotated,im1rot, hdr2, overwrite=True)
#
#
# hdr3 = deepcopy(hdr2)
# hdr3['CDELT1']=hdr3['CDELT1']*cosi
# fileout_stretched=re.sub('fullim.fits', 'stretched.fits', filename_fullim)
# im3=np.double(gridding(fileout_rotated,hdr3))
# pf.writeto(fileout_stretched,im3, hdr2, overwrite=True)
#
#
# nx=hdr2['NAXIS1']
# ny=hdr2['NAXIS2']
#
# aim_polar = sp.ndimage.geometric_transform(im3,cartesian2polar,
# order=1,
# output_shape = (im3.shape[0], im3.shape[1]),
# extra_keywords = {'inputshape':im3.shape,
# 'origin':(((float(nx)+1.)/2.)-1.,((float(ny)+1.)/2.)-1.)})
#
# return aim_polar
def carttoconicpolar(im,inc,tanpsi):
(ny,nx)=im.shape
(i0,j0)=(((float(nx)+1.)/2.)-1.,((float(ny)+1.)/2.)-1.)
im_polar = sp.ndimage.geometric_transform(im,cartesian2conicpolar,
order=1,
output_shape = (im.shape[0], im.shape[1]),
extra_keywords = {
'inputshape':im.shape,
'inc':inc,'tanpsi':tanpsi,
'origin':(i0,j0)})
return im_polar
def carttopolar(im,inc):
(ny,nx)=im.shape
(i0,j0)=(((float(nx)+1.)/2.)-1.,((float(ny)+1.)/2.)-1.)
im_polar = sp.ndimage.geometric_transform(im,cartesian2polar,
order=1,
output_shape = (im.shape[0], im.shape[1]),
extra_keywords = {
'inputshape':im.shape,
'inc':inc,
'origin':(i0,j0)})
return im_polar
def conicpolartocart(im_polar,inc,tanpsi):
(ny,nx)=im_polar.shape
(i0,j0)=(((float(nx)+1.)/2.)-1.,((float(ny)+1.)/2.)-1.)
im_cart = sp.ndimage.geometric_transform(im_polar,conicpolar2cartesian_ellipse,
order=1,
output_shape = (im_polar.shape[0], im_polar.shape[1]),
extra_keywords = {'inputshape':im_polar.shape,
'inc':inc,'tanpsi':tanpsi,
'origin':(i0,j0)})
im_cart=np.nan_to_num(im_cart)
return im_cart
def polartocart(im_polar,inc):
(ny,nx)=im_polar.shape
(i0,j0)=(((float(nx)+1.)/2.)-1.,((float(ny)+1.)/2.)-1.)
im_cart = sp.ndimage.geometric_transform(im_polar,polar2cartesian,
order=1,
output_shape = (im_polar.shape[0], im_polar.shape[1]),
extra_keywords = {'inputshape':im_polar.shape,
'inc':inc,
'origin':(i0,j0)})
im_cart=np.nan_to_num(im_cart)
return im_cart
def exec_prep_files(M):
filename_source=M.filename_source
workdir=M.workdir
DumpAllFitsFiles=M.DumpAllFitsFiles
if (not re.search(r"\/$",workdir)):
workdir+='/'
M.workdir=workdir
print("added trailing back slash to outputdir")
inbasename=os.path.basename(filename_source)
filename_fullim=re.sub('.fits', '_fullim.fits', inbasename)
filename_fullim=workdir+filename_fullim
if (M.DoErrorMap):
inbasenameerr=os.path.basename(M.filename_errormap)
filename_fullimerr=re.sub('.fits', '_fullim.fits', inbasenameerr)
filename_fullimerr=workdir+filename_fullimerr
filename_fullimw=re.sub('.fits', '_fullimw.fits', inbasenameerr)
filename_fullimw=workdir+filename_fullimw
if (M.Verbose): #
print( "BUILDING WORKDIR AND GENERATING CENTERED IMAGE")
hdu = pf.open(filename_source)
hdr0=hdu[0].header
if (hdr0['NAXIS'] > 2):
if (M.Verbose): #
print( "running cube2im")
hdu=cube2im(filename_source,False)
im1=hdu[0].data
im1=im1*M.unitscale
print( "applied unit scale factor:",M.unitscale)
hdr1=hdu[0].header
typicalerror=M.ExpectedError
if (M.InjectNoise):
print( "INJECTING NOISE")
im1=im1 + np.random.normal(loc=0.0, scale=typicalerror, size=im1.shape)
if (DumpAllFitsFiles):
pf.writeto(filename_fullim,im1, hdr1, overwrite=True)
hdu[0].data=im1
hduw=False
if (M.DoErrorMap):
hduerr = pf.open(M.filename_errormap)
hdrerr = hduerr[0].header
if (hdrerr['NAXIS'] > 2):
hduerr=cube2im(M.filename_errormap,False)
imerr1=hduerr[0].data
imerr1=imerr1*M.unitscale
#typicalerror=np.median(imerr1)
#imerr1[np.where(imerr1 < (typicalerror/3.))] = typicalerror
#imerr1[np.where(imerr1 > (100.*typicalerror))] = 1E20
hdrerr1=hduerr[0].header
imw0=1./imerr1**2
imw0=np.nan_to_num(imw0)
imw0[np.where(np.fabs(imw0) > 1E10)] = 0.
if (M.Verbose):
print( "fullim weights: max",np.max(imw0)," min ",np.min(imw0))
imerr=np.sqrt(1./imw0)
imerr=np.nan_to_num(imerr)
imerr[np.where(np.fabs(imw0) < 1E-30)] = 1E20
typicalerror=np.median(imerr)
if (M.Verbose):
print( "resamp errors: max",np.max(imerr)," min ",np.min(imerr))
print( "typicalerror= ",typicalerror," vs", M.ExpectedError)
if (DumpAllFitsFiles):
pf.writeto(filename_fullimw,imw0, hdrerr1, overwrite=True)
hduw=pf.PrimaryHDU()
hduw.data=imw0
hduw.header=hdrerr1
M.Hdu=hdu
M.Hduw=hduw
return
def exec_grid_4center(M):
workdir=M.workdir
RA=M.RA
DEC=M.DEC
x_center=M.x_center
y_center=M.y_center
fieldscale=M.fieldscale # shrink radial field of view of polar maps by this factor
hdu=M.Hdu
hduw=M.Hduw
filename_source=M.filename_source
inbasename=os.path.basename(filename_source)
filename_fullim=re.sub('.fits', '_fullim.fits', inbasename)
filename_fullim=workdir+filename_fullim
fileout_centered=re.sub('fullim.fits', 'centered.fits', filename_fullim)
if (M.DoErrorMap):
inbasenameerr=os.path.basename(M.filename_errormap)
filename_fullimerr=re.sub('.fits', '_fullim.fits', inbasenameerr)
filename_fullimerr=workdir+filename_fullimerr
filename_fullimw=re.sub('.fits', '_fullimw.fits', inbasenameerr)
filename_fullimw=workdir+filename_fullimw
fileout_centerederr=re.sub('fullim.fits', 'centered.fits', filename_fullimerr)
fileout_centeredw=re.sub('fullim.fits', 'wcentered.fits', filename_fullimerr)
hdr1=hdu[0].header
if (not (isinstance(y_center,bool))):
if (not isinstance(RA,float)):
RA=hdr1['CRVAL1']
DEC=hdr1['CRVAL2']
if (M.Verbose):
print( "using center of coords CRVAL1 CRVAL2")
#RA=RA+(np.sin(x_center*np.pi/180.)*y_center/3600.)/np.cos(DEC*np.pi/180.)
RA=RA+((x_center/3600.)/np.cos(DEC*np.pi/180.))
#DEC=DEC+np.cos(x_center*np.pi/180.)*y_center/3600.
DEC=DEC+((y_center/3600.)*np.pi/180.)
if (M.Verbose):
print( "RA =", RA)
print( "DEC =",DEC)
nx=int(hdr1['NAXIS1']/(M.pixscale_factor*M.fieldscale))
ny=nx
if ( (nx % 2) == 0):
nx=nx+1
ny=ny+1
hdr2 = deepcopy(hdr1)
hdr2['NAXIS1']=nx
hdr2['NAXIS2']=ny
hdr2['CRPIX1']=(nx+1)/2
hdr2['CRPIX2']=(ny+1)/2
hdr2['CRVAL1']=RA
hdr2['CRVAL2']=DEC
hdr2['CDELT1']=M.pixscale_factor*hdr2['CDELT1']
hdr2['CDELT2']=M.pixscale_factor*hdr2['CDELT2']
resamp=gridding(hdu,hdr2, fullWCS=False)
resamp=np.nan_to_num(resamp)
if (M.DumpAllFitsFiles):
fileout_centered=re.sub('fullim.fits', 'centered.fits', filename_fullim)
pf.writeto(fileout_centered,resamp, hdr2, overwrite=True)
hducentered=pf.PrimaryHDU()
hducentered.data=resamp
hducentered.header=hdr2
hduwcentered=False
if (M.DoErrorMap):
resampw=gridding(hduw,hdr2, fullWCS=False)
resampw=np.nan_to_num(resampw)
resampw[np.where(resampw < 0.)] =0.
if (M.Verbose): #
print( "resamp weights: max",np.max(resampw)," min ",np.min(resampw))
resamperr=np.sqrt(1./resampw)
if (M.DumpAllFitsFiles):
fileout_centeredw=re.sub('fullim.fits', 'wcentered.fits', filename_fullimerr)
pf.writeto(fileout_centeredw,resampw, hdr2, overwrite=True)
fileout_centerederr=re.sub('fullim.fits', 'centered.fits', filename_fullimerr)
pf.writeto(fileout_centerederr,resamperr, hdr2, overwrite=True)
hduwcentered=pf.PrimaryHDU()
hduwcentered.data=resampw
hduwcentered.header=hdr2
M.Ncorr=(np.pi/(4.*np.log(2)))*M.bmaj*M.bmin/(hdr2['CDELT2']*3600.)**2
M.Hducentered=hducentered
M.Hduwcentered=hduwcentered
def exec_conicpolar_expansions(M):
filename_source=M.filename_source
workdir=M.workdir
inc=M.inc
tanpsi=M.tanpsi
RA=M.RA
DEC=M.DEC
x_center=M.x_center
y_center=M.y_center
DoAzimuthalProfile=M.DoAzimuthalProfile
PlotRadialProfile=M.PlotRadialProfile
a_min=M.a_min
a_max=M.a_max
ExpectedError=M.ExpectedError
InjectNoise=M.InjectNoise
DumpAllFitsFiles=M.DumpAllFitsFiles
DoDCone=M.DoDCone
domain=M.domain
fieldscale=M.fieldscale # shrink radial field of view of polar maps by this factor
DoAccr=M.DoAccr
DoMerid=M.DoMerid
hdu=M.Hducentered
hduw=M.Hduwcentered
typicalerror=ExpectedError
#cosi=np.cos(inc*np.pi/ 180.)
inbasename=os.path.basename(filename_source)
filename_fullim=re.sub('.fits', '_fullim.fits', inbasename)
filename_fullim=workdir+filename_fullim
fileout_centered=re.sub('fullim.fits', 'centered.fits', filename_fullim)
if (M.DoErrorMap):
inbasenameerr=os.path.basename(M.filename_errormap)
filename_fullimerr=re.sub('.fits', '_fullim.fits', inbasenameerr)
filename_fullimerr=workdir+filename_fullimerr
filename_fullimw=re.sub('.fits', '_fullimw.fits', inbasenameerr)
filename_fullimw=workdir+filename_fullimw
fileout_centerederr=re.sub('fullim.fits', 'centered.fits', filename_fullimerr)
fileout_centeredw=re.sub('fullim.fits', 'wcentered.fits', filename_fullimerr)
resamp=hdu.data
hdr2=hdu.header
nx=hdr2['NAXIS1']
ny=hdr2['NAXIS2']
if M.Verbose:
print( "M.Ncorr = ",M.Ncorr)
if (M.DoErrorMap):
resampw=hduw.data
if (M.InheritMumap):
if (M.mumap is None):
mumap=np.ones(resamp.shape)
else:
mumap=M.mumap
else:
mumap=np.ones(resamp.shape)
#rotangle= M.PA - 180.
rotangle= M.PA
im1rot = ndimage.rotate(resamp, rotangle, reshape=False)
hdr3 = deepcopy(hdr2)
im3=np.double(im1rot)
if (DumpAllFitsFiles):
fileout_rotated=re.sub('fullim.fits', 'rotated.fits', filename_fullim)
pf.writeto(fileout_rotated,im1rot, hdr2, overwrite=True)
if (M.DoErrorMap):
if (np.any(resampw < 0.)):
print("min / max:",np.min(resampw),np.max(resampw))
sys.exit("negative sky weights!!!!")
im1rotw = ndimage.rotate(resampw, rotangle, reshape=False,order=0)
im3w=np.double(im1rotw)
if (DumpAllFitsFiles):
fileout_rotatedw=re.sub('fullim.fits', 'wrotated.fits', filename_fullimerr)
pf.writeto(fileout_rotatedw,im1rotw, hdr2, overwrite=True)
if (np.any(im1rotw < 0.)):
print("min / max:",np.min(im1rotw),np.max(im1rotw))
sys.exit("negative rot sky weights!!!!")
# #####################################################################
# take conic polar transforms
if (M.Verbose):
print( "CARTESIAN2CONICPOLAR TRANSFORM START")
print( "using inc ",inc*np.pi/180.," tanpsi ",tanpsi)
im_polar = carttoconicpolar(im3,inc,tanpsi)
if (DoDCone):
mumap_polarpos = carttoconicpolar(mumap,inc,tanpsi)
nphis,nrs=im_polar.shape
if ((nphis != nx) or (nrs != ny)):
sys.exit("bug")
hdupolar = pf.PrimaryHDU()
hdupolar.data = im_polar
hdrpolar=hdupolar.header
hdrpolar['CRPIX1']=1
hdrpolar['CRVAL1']=0.
hdrpolar['CDELT1']=2. * np.pi / nphis
hdrpolar['CRPIX2']=1
hdrpolar['CRVAL2']=0.
hdrpolar['CDELT2']=(hdr3['CDELT2'])
hdupolar.header = hdrpolar
fileout_polar=re.sub('fullim.fits', 'polar.fits', filename_fullim)
if (DumpAllFitsFiles):
hdupolar.writeto(fileout_polar, overwrite=True)
if (M.DoErrorMap):
im_polarw = carttoconicpolar(im3w,inc,tanpsi)
nphis,nrs=im_polarw.shape
hdupolarw = pf.PrimaryHDU()
hdupolarw.data = im_polarw
hdupolarw.header = hdrpolar
fileout_polarw=re.sub('fullim.fits', 'wpolar.fits', filename_fullimerr)
if (DumpAllFitsFiles):
hdupolarw.writeto(fileout_polarw, overwrite=True)
else:
im_polarw=np.ones(im_polar.shape)/typicalerror**2
######################################################################
# take azimuthal averages on polar maps
weights=im_polarw.copy()
im_Npolcorr=np.ones(im_polarw.shape)
if (np.any(weights < 0.)):
print("min / max:",np.min(weights),np.max(weights))
sys.exit("negative polarweights!!")
im_polar_av = np.copy(im_polar)
rrs = 3600.*(np.arange(hdrpolar['NAXIS2'])-hdrpolar['CRPIX2']+1.0)*hdrpolar['CDELT2']+hdrpolar['CRVAL2']
phis = (180./np.pi)*((np.arange(hdrpolar['NAXIS1'])-hdrpolar['CRPIX1']+1.0)*hdrpolar['CDELT1']+hdrpolar['CRVAL1'])
phis_rad = np.double(((np.arange(hdrpolar['NAXIS1'])-hdrpolar['CRPIX1']+1.0)*hdrpolar['CDELT1']+hdrpolar['CRVAL1']))
KepAmps= np.double(np.zeros(len(rrs)))
sKepAmps= np.double(np.zeros(len(rrs)))
AccrAmps= np.double(np.zeros(len(rrs)))
sAccrAmps= np.double(np.zeros(len(rrs)))
MeridAmps= np.double(np.zeros(len(rrs)))
sMeridAmps= np.double(np.zeros(len(rrs)))
vsysts=np.zeros(hdrpolar['NAXIS2'])
if (a_min > 0):
ia_min = np.argmin(np.abs(rrs - a_min))
if (a_max > 0):
ia_max = np.argmin(np.abs(rrs - a_max))
if (M.ComputeSystVelo):
if (M.DoErrorMap):
for irrs in range(len(rrs)):
v0_vec=im_polar[irrs,:]
w_vec=im_polarw[irrs,:]
av_v0=np.sum(w_vec*v0_vec)/np.sum(w_vec)
av_cosphi=np.sum(w_vec*np.cos(phis_rad))/np.sum(w_vec)
KepAmp = np.sum( (v0_vec - av_v0) * w_vec * np.cos(phis_rad)) / np.sum( w_vec * (np.cos(phis_rad))**2 - w_vec * np.cos(phis_rad) * av_cosphi)
vsysts[irrs]=np.sum(w_vec*(v0_vec - KepAmp*np.cos(phis_rad)))/np.sum(w_vec)
else:
for irrs in range(len(rrs)):
v0_vec=im_polar[irrs,:]
KepAmp = np.sum( (v0_vec - np.average(v0_vec)) * np.cos(phis_rad)) / np.sum( (np.cos(phis_rad))**2 - np.cos(phis_rad)* np.average(np.cos(phis_rad)))
vsysts[irrs]=np.average(v0_vec - KepAmp*np.cos(phis_rad))
vsyst=np.asscalar(np.median(vsysts[ia_min:ia_max]))
sigma_vsyst=np.asscalar(np.std(vsysts[ia_min:ia_max]))
print( "vsyst calculated = ",vsyst,"+-",sigma_vsyst)
M.vsyst=vsyst
else:
vsyst=M.vsyst
if (M.Verbose):
print( "vsyst from M = ",vsyst)
RestrictAvToRadialDomain=False # set to True is faster but may lead to discontinuities in region averages.
for irrs in range(len(rrs)):
KepAmps[irrs] = 0.
sKepAmps[irrs] = 0.
AccrAmps[irrs] = 0.
sAccrAmps[irrs] = 0.
MeridAmps[irrs] = 0.
sMeridAmps[irrs] = 0.
im_polar_av[irrs,:] = 0.
if (( (irrs < ia_min) or (irrs > ia_max) ) and RestrictAvToRadialDomain):
continue
v0_vec=im_polar[irrs,:]-vsyst
if (M.DoErrorMap):
w_vec=weights[irrs,:]
else:
w_vec=1/typicalerror**2
mask=(w_vec < 1E-10)
w_vec_nozeros=w_vec
w_vec_nozeros[mask]=1E-10
err_vec=(1.0/np.sqrt(w_vec_nozeros))
err_vec[mask] = 1E20 # np.inf
w_vec[mask]=0.
thisradius=rrs[irrs] #arcsec
cosi=np.cos(M.inc)
Nind=2.*np.pi*thisradius * np.fabs(cosi) /(M.bmaj) # aprox number of beams at each radius
Nsum=len(w_vec)
Npolcorr=Nsum/Nind
#print("irrs ",irrs,Nsum, Nind, Npolcorr)
if Npolcorr < 1:
Npolcorr=1.
im_Npolcorr[irrs,:]=Npolcorr
if ( (np.sum(w_vec) == 0.) or ( np.sum( w_vec * (np.cos(phis_rad))**2)**2 == 0.)):
KepAmp=0.
sKepAmp=1E20
AccrAmp=0.
sAccrAmp=1E20
MeridAmp=0.
sMeridAmp=1E20
else:
if (M.InheritMumap):
mumap_vec=mumap_polarpos[irrs,:]
if (DoAccr):
sys.exit("Not yet programmed DoAccr with mumap")
else:
KepAmp = np.sum(w_vec* v0_vec * mumap_vec * np.cos(phis_rad)) / np.sum(w_vec * mumap_vec *(np.cos(phis_rad))**2)
AccrAmp = 0.
else:
Cramer = True
if (DoMerid and Cramer):
sinphis = np.sin(phis_rad)
cosphis = np.cos(phis_rad)
a_1 = np.sum(w_vec * cosphis**2)
b_1 = np.sum(w_vec * sinphis * cosphis)
c_1 = np.sum(w_vec * cosphis)
d_1 = np.sum(w_vec * cosphis * v0_vec)
vard_1 = np.sum(w_vec * cosphis**2)
a_2 = b_1
b_2 = np.sum(w_vec * sinphis**2)
c_2 = np.sum(w_vec * sinphis)
d_2 = np.sum(w_vec * sinphis * v0_vec)
vard_2 = np.sum(w_vec * sinphis**2)
a_3 = c_1
b_3 = c_2
c_3 = np.sum(w_vec)
d_3 = np.sum(w_vec * v0_vec)
vard_3 = np.sum(w_vec)
detM = a_1*(b_2*c_3 - b_3*c_2) - b_1*(a_2*c_3 - a_3*c_2) + c_1*(a_2*b_3 - a_3*b_2)
if (detM == 0.):
#print("singular matrix")
continue
A= (d_1 * (b_2*c_3 - b_3*c_2) + d_2 * (c_1*b_3 - b_1*c_3) + d_3 * (b_1*c_2-c_1*b_2))/detM
sigmaA= np.sqrt( vard_1*(b_2*c_3 - b_3*c_2)**2 + vard_2*(c_1*b_3 - b_1*c_3)**2 + vard_3*(b_1*c_2-c_1*b_2)**2)/detM
B= (d_1 * (a_3*c_2 - a_2*c_3) + d_2 *(a_1*c_3 - c_1*a_3) + d_3 * (c_1*a_2 - a_1*c_2))/detM
sigmaB= np.sqrt( vard_1*(a_3*c_2 - a_2*c_3)**2 + vard_2*(a_1*c_3 - c_1*a_3)**2 + vard_3*(c_1*a_2-a_1*c_2)**2)/detM
C= (d_1 * (a_2*b_3 - a_3*b_2) + d_2 * (b_1*a_3 - a_1*b_3) + d_3 * (a_1*b_2-b_1*a_2))/detM
sigmaC= np.sqrt(vard_1 * (a_2*b_3 - a_3*b_2)**2 + vard_2 * (b_1*a_3 - a_1*b_3)**2 + vard_3 * (a_1*b_2-b_1*a_2)**2)/detM
KepAmp = A
sKepAmp = sigmaA
AccrAmp = B
sAccrAmp = sigmaB
MeridAmp = C
sMeridAmp = sigmaC
sAccrAmp *= np.sqrt(Npolcorr)
sKepAmp *= np.sqrt(Npolcorr)
sMeridAmp *= np.sqrt(Npolcorr)
elif (DoMerid):
sum_w=np.sum(w_vec)
sum_wv0=np.sum(w_vec * v0_vec)
sum_wsinphi = np.sum(w_vec*np.sin(phis_rad))
sum_wcosphi = np.sum(w_vec*np.cos(phis_rad))
sinphis = np.sin(phis_rad)
cosphis = np.cos(phis_rad)
subA_num= (np.sum( w_vec * sinphis**2 - w_vec * sinphis * sum_wsinphi /sum_w ) /
np.sum( w_vec * sinphis * cosphis - w_vec * cosphis * sum_wsinphi /sum_w ))
A_num = ( np.sum(v0_vec * w_vec * sinphis - w_vec * sinphis * sum_wv0 / sum_w)
- subA_num * np.sum( v0_vec * w_vec * cosphis - w_vec * cosphis * sum_wv0 / sum_w))
subA_denom1_num1 = np.sum( w_vec * cosphis**2 - w_vec * cosphis * sum_wcosphi /sum_w )
subA_denom1_denom1 = np.sum( w_vec * cosphis * sinphis - w_vec * cosphis * sum_wsinphi /sum_w )
A_denom = ( np.sum( (w_vec * sinphis * cosphis - w_vec * sinphis * sum_wcosphi /sum_w )
- (w_vec * sinphis**2 - w_vec * sinphis * sum_wsinphi / sum_w) * subA_denom1_num1 / subA_denom1_denom1 ) )
A = A_num / A_denom
varA_num = np.sum( sinphis**2 * w_vec + w_vec**2 * sinphis**2 / sum_w) + subA_num**2 * np.sum( cosphis**2 * w_vec + w_vec**2 * cosphis**2 / sum_w)
sigma_A = np.sqrt(varA_num/A_denom**2)
B_denom = np.sum( w_vec*cosphis *sinphis - w_vec * cosphis * sum_wsinphi / sum_w)
subB_num = np.sum(w_vec * cosphis**2 - w_vec * cosphis * sum_wcosphi / sum_w)
B = ( (np.sum( v0_vec * w_vec * cosphis - w_vec * cosphis * sum_wv0 / sum_w ) -
A * subB_num) / B_denom )
varB = ( np.sum( w_vec * cosphis**2 + w_vec**2 * cosphis**2 / sum_w)
+ sigma_A**2 * subB_num**2 )/B_denom**2
sigma_B = np.sqrt(varB)
C_denom = sum_w * abs(np.cos(M.inc))
C = np.sum( w_vec * (v0_vec - A * cosphis - B * sinphis )) / C_denom
varC = np.sum( w_vec**2 * ( err_vec**2 + sigma_A**2 * cosphis**2 + sigma_B**2 + sinphis**2 )) / C_denom**2
sigma_C= np.sqrt(varC)
KepAmp = A
sKepAmp = sigma_A
AccrAmp = B
sAccrAmp = sigma_B
MeridAmp = C
sMeridAmp = sigma_C
sAccrAmp *= np.sqrt(Npolcorr)
sKepAmp *= np.sqrt(Npolcorr)
sMeridAmp *= np.sqrt(Npolcorr)
elif (DoAccr):
subsum1=np.sum(w_vec * v0_vec * np.cos(phis_rad)) / np.sum( w_vec * (np.cos(phis_rad))**2)
varsubsum1=np.sum((np.sqrt(w_vec) * np.cos(phis_rad)) / np.sum( w_vec * (np.cos(phis_rad))**2)**2)
numerator=np.sum(w_vec * np.sin(phis_rad) * (v0_vec - np.cos(phis_rad) * subsum1))
#dum1=w_vec**2 * np.sin(phis_rad)**2
##if ((~np.isfinite(varsubsum1)).any()):
## print("varsubsum1 bad values")
#dum2=np.cos(phis_rad)**2 * varsubsum1
#dum3=((err_vec)**2 + dum2)
#varnumerator=np.sum(dum1*dum3)
varnumerator=np.sum(w_vec**2 * np.sin(phis_rad)**2 * ((err_vec)**2 + np.cos(phis_rad)**2 * varsubsum1))
subsum2=np.sum(w_vec * np.sin(phis_rad) * np.cos(phis_rad)) / np.sum( w_vec * (np.cos(phis_rad))**2)
denom=np.sum(w_vec * ( (np.sin(phis_rad))**2 - subsum2))
AccrAmp=numerator/denom
sAccrAmp=np.sqrt(varnumerator)/denom
KepAmp = np.sum(w_vec * (v0_vec - AccrAmp*np.sin(phis_rad)) * np.cos(phis_rad)) / np.sum(w_vec * (np.cos(phis_rad))**2)
sKdenom=np.sum(w_vec * (np.cos(phis_rad))**2)
varKnum=np.sum( w_vec**2 * (err_vec**2 + sAccrAmp**2 * np.sin(phis_rad)**2) * np.cos(phis_rad)**2)
sKnum=np.sqrt(varKnum)
sKepAmp = sKnum/sKdenom
#print("sKepAmp>",rrs[irrs],KepAmp, varKnum, sKnum,sKdenom, sKepAmp, sAccrAmp)
sAccrAmp *= np.sqrt(Npolcorr)
sKepAmp *= np.sqrt(Npolcorr)
MeridAmp = 0.
sMeridAmp = 1E20
else:
denom=np.sum(w_vec * (np.cos(phis_rad))**2)
numerator = np.sum(w_vec* v0_vec * np.cos(phis_rad))
KepAmp = numerator / denom
sdenom=np.sqrt(np.sum(w_vec * (np.cos(phis_rad))**2))
sKepAmp = 1./sdenom
#print( "bmaj = ",M.bmaj, " thisradius ",thisradius, " inc ", M.inc ,"Nind ",Nind," Ncorr ",Ncorr, " \n")
sKepAmp *= np.sqrt(Npolcorr)
AccrAmp = 0.
sAccrAmp = 1E20
MeridAmp = 0.
sMeridAmp = 1E20
# else:
# v0_vec=im_polar[irrs,:]-vsyst
# KepAmp = np.sum(v0_vec * np.cos(phis_rad)) / np.sum((np.cos(phis_rad))**2)
if (M.Verbose and (KepAmp < 0.) and (irrs == int(len(rrs)/4.))):
print( "KepAmp negative, wrong PA: KepAmp=",KepAmp," PA: ",M.PA," inc",M.inc*np.pi/180.,"deg tanpsi",tanpsi )
print((np.array(zip(v0_vec,np.cos(phis_rad)))))
KepAmps[irrs] = KepAmp
sKepAmps[irrs] = sKepAmp
AccrAmps[irrs] = AccrAmp
sAccrAmps[irrs] = sAccrAmp
MeridAmps[irrs] = MeridAmp
sMeridAmps[irrs] = sMeridAmp
if (DoMerid):
v0_vec_av = KepAmp * np.cos(phis_rad) + AccrAmp * np.sin(phis_rad) * MeridAmp*np.cos(M.inc)
im_polar_av[irrs,:] = v0_vec_av + vsyst
elif (DoAccr):
v0_vec_av = KepAmp * np.cos(phis_rad) + AccrAmp * np.sin(phis_rad)
im_polar_av[irrs,:] = v0_vec_av + vsyst
else:
v0_vec_av = KepAmp * np.cos(phis_rad)
im_polar_av[irrs,:] = v0_vec_av + vsyst
v_Phi_prof = KepAmps
sv_Phi_prof = sKepAmps
v_Phi_prof = np.nan_to_num(v_Phi_prof)
sv_Phi_prof = np.nan_to_num(sv_Phi_prof)
#v_Phi_prof[np.isnan(v_Phi_prof)]=0.
#sv_Phi_prof[np.isnan(sv_Phi_prof)]=0.
v_R_prof = AccrAmps
sv_R_prof = sAccrAmps
v_R_prof = np.nan_to_num(v_R_prof)
sv_R_prof = np.nan_to_num(sv_R_prof)
v_z_prof = MeridAmps
sv_z_prof = sMeridAmps
v_z_prof = np.nan_to_num(v_z_prof)
sv_z_prof = np.nan_to_num(sv_z_prof)
#bmaj=hdr2['BMAJ']
#print( "bmaj = ",bmaj,"\n";)
#Nind=2.*np.pi*rrs /(cosi * bmaj*3600.)
#dispv_Phi_prof=sv_Phi_prof.copy()
#sv_Phi_prof=sv_Phi_prof/np.sqrt(Nind)
######################################################################
# now compute chi2 in polar coords
if (DoDCone):
imazim_far=conicpolartocart(im_polar_av,inc,-tanpsi)
im_polar_av_far_near=carttoconicpolar(imazim_far,inc,tanpsi)
for irrs in range(len(rrs)):
v0_vec=im_polar[irrs,:]-vsyst
v0_vec_m=im_polar_av[irrs,:]-vsyst
v0_vec_FN_m=im_polar_av_far_near[irrs,:]-vsyst
if (M.DoErrorMap):
w_vec=weights[irrs,:]
else:
w_vec=1./typicalerror**2
Delta_o=v0_vec - v0_vec_FN_m
Delta_m=v0_vec_m - v0_vec_FN_m
cos_vec = np.fabs(np.cos(phis_rad))
A = np.sum(w_vec * cos_vec * Delta_m * (Delta_m - Delta_o)) / np.sum(w_vec * cos_vec**2 * Delta_m**2)
if (A < 0.):
A=0.
# sys.exit("BUG")
if (A > 0.5):
#print( "setting A",A,"to 1")
A=0.5
mu_vec=(1. - A * cos_vec)
mumap_polarpos[irrs,:]=mu_vec
# testing for chi2 improvement:
TestChi2Improv=False
if TestChi2Improv:
subchi2 = np.sum(w_vec * (v0_vec - v0_vec_m)**2)
subchi2_mumap = np.sum(w_vec * (v0_vec - (v0_vec_m * mu_vec + v0_vec_FN_m * (1. -mu_vec)) )**2)
flag=''
if (subchi2_mumap > subchi2):
flag='<<'
print(("irrs %d irrs r %f A %f chi2 %f chi2_mu %f %s " % (irrs,rrs[irrs],A,subchi2,subchi2_mumap,flag)))
#(nr,nphis)=im_polar_av.shape
#im_phi=np.tile(phis_rad,(nphis,1))
#im_cosphi=np.tile(np.cos(phis_rad),(nphis,1))
zeimage = weights*im_Npolcorr*(im_polar-im_polar_av)**2
zeimage = np.nan_to_num(zeimage)
deltaChi2 = np.sum(zeimage,axis=1)
deltaimage=im_polar-im_polar_av
velodev_med=np.sqrt( np.median(deltaimage[ia_min:ia_max]**2))
velodev_std=np.std(deltaimage[ia_min:ia_max,:])
velodev_std_vec=np.std(deltaimage,axis=1)
velodev_std2=np.std(velodev_std_vec[ia_min:ia_max])
im_polar_av_DCone = im_polar_av * mumap_polarpos + im_polar_av_far_near * (1. - mumap_polarpos)
#M.DConeDeltaChi2=False
zeimage_DCone=weights*(im_polar - im_polar_av_DCone)**2
deltaChi2_DCone=np.sum(zeimage_DCone,axis=1)
chi2=sum(deltaChi2[ia_min:ia_max])
#chi2_DCone=sum(deltaChi2_DCone[ia_min:ia_max])
if (((np.fabs(chi2 - M.chi2_prev)/chi2) < 1E-8) and not M.DConeDeltaChi2):
print( "using DConeDeltaChi2 chi2: ",chi2," chi2_prev", M.chi2_prev)
M.DConeDeltaChi2=True