-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotCorrelationMap11.py
More file actions
executable file
·1349 lines (1072 loc) · 52.1 KB
/
Copy pathplotCorrelationMap11.py
File metadata and controls
executable file
·1349 lines (1072 loc) · 52.1 KB
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
#!/usr/bin/env python
"""
By David French (frenchd@astro.wisc.edu)
$Id: plotCorrelationMap11.py, v 11.3 10/03/16
This program takes in a list of AGN targets and generates an environment map (i.e. nearby
galaxies) for each.
Adapted from:
'
plotAGNCorrelationSquare9.py, v 9.0 02/12/2014
Make a square plot showing the positions of correlated galaxies around a target AGN
Starts with the older 'plotAGNCorrelationSquare6.py' and makes adjustments for the current
data.
v8: include inclinations in galaxy annotation tags
v9: plots all targets in the input file, and does everything better faster
'
v1: Removes group vs field galaxy bits
v2: Draw ellipses to denote inclination and position angle at the same time
v3: Make nicer plots for poster (09/10/14)
v4: change the labeling for individual plots (09/11/14)
v5: adopt to make plots for general correlation tables
v6: include option to make skyplot as well (plot just the ra and dec of all objects)
This was final AAS poster version, made targetmaps3
v7: More general version - 06/19/15
v7.1: Now saves into 'ambiguous' or 'associated' folders depending on the 'include' answer
- 06/25/15
7.2: Reverse PA direction. Now PA is measured clockwise from N (up) to E (on right). This
changes angle = float(PA[i]) to angle = float(-PA[i]) in plotting
Also changed the name annotation slightly, to xy=(-60, size[i]/6) from
xy=(-65, size[i]/4), and unknown type marker size increased to size[i]*4
E type galaxies now have no edge, spiral have black edges, unknown are diamonds
- 07/07/15
- unfortunately, this version was still named "plotCorrelationMap7_1.py".
7.3: Sort the map table by the quantity: abs(dif_velocity)*impact parameter/virial radius
- low means the galaxy in question close in both velocity and distance. These get
listed first.
7.4: Same as above, but tweaking the "likelihood" parameter to the following:
abs(dif_velocity) * (impact parameter - virial radius)
7.5: Now the likelihood parameter is:
abs(dif_velocity) * (impact parameter - virial radius) / (virial radius)**2
if this value is >2 times the next largest, call it associated and put it in that folder
11/6/15: (approximate date) Now use a gaussian form likelihood parameter, and take
the velocity difference into account as well (normalized to 200 km/s)
-looks like: exp((impact/virial)^2) * exp((vel_dif/200)^2) - Targetmaps12
11/11/15: continued with more tweaks to try to get satellite galaxies to be rated
lower. Targetmaps13 uses diam^1.4 instead of a virial radius
Targetmaps14 uses diam^1.5
11/15/15: Targetmaps15 uses diam^1.5, and multiplies likelihood by 2 if the virial
radius is larger than the impact parameter
Targetmaps16 requires 3* greater likelihood for associated tag
Targetmaps17 back to standard virial radius, but with the above tweaks for
multiplying likelihood *2 and requiring 3* difference
11/17/15: Targetmaps18 back to diam^1.5, and now with 5* difference required
11/19/15: Targetmaps19 uses standard r_vir, but requires a 5* difference
v8.0: Computes both standard R_vir and diam^1.5 results. Adds the results automatically to
LG_correlation_combined5.csv,
v9.0: velocity colormap in units of delta-v instead of absolute velocity units
v9.1: 'include' now requires a hard L limit (L>=0.001 etc)
v10: correlate with galaxy v_helio, not vcorr - using correlateSingle7 now
- (03/24/16 - 4/13/16)
v11: moved to .../inclination/git_inclination/ so it can be updated with git, and
now including rc tex formating for the plots (05/12/2016)
v11.1: updates for the newest round of sightlines in LG_correlation_combined5_10.csv
- Made LG_correlation_combined5_11.csv and targetmaps34 (07/06/16)
v11.2: minor formatting updates. (8/08/16)
v11.3: more minor updates - make the tick labels NOT bold (10/03/16)
- make LG_correlation_combined5_11_25cut_edit5.csv and targetmaps37/
v11.3: more minor updates - fix the colorbar bolded ticks problem (10/10/16)
- remake LG_correlation_combined5_11_25cut_edit5.csv and targetmaps37/
"""
import sys
import os
import csv
import string
import math
import ast
from pylab import *
import correlateSingle8 as correlateSingle
from matplotlib.patches import Ellipse
# from astropy.coordinates import SkyCoord
# from astropy import units as u
from utilities import *
import getpass
from matplotlib import rc
fontScale = 18
rc('text', usetex=True)
rc('font', size=18,family='serif',weight='medium')
rc('xtick.major',size=8,width=0.6)
rc('xtick.minor',size=5,width=0.6)
rc('ytick.major',size=8,width=0.6)
rc('ytick.minor',size=5,width=0.6)
rc('xtick',labelsize = fontScale)
rc('ytick',labelsize = fontScale)
rc('axes',labelsize = fontScale)
rc('xtick', labelsize = fontScale)
rc('ytick',labelsize = fontScale)
# rc('font', weight = 450)
# rc('axes',labelweight = 'bold')
rc('axes',linewidth = 1,labelweight='normal')
# from matplotlib.patches import Ellipse
################################################################
def buildTargetList(file,AGNheader,velocityHeader):
# builds a list consisting of tuples: (AGNname, velocity, include)
#
# requires a column called 'galaxyName' in the file. Only includes targets for which
# 'galaxyName' != 'x'
#
# - AGN name is the name of the target
# - velocity is the velocity of the absorption feature centroid in km/s
# - include is a boolean indicating whether the results should be saved into the
# 'ambiguous' folder (False), or the 'associated' folder (True)
f = open(file,'rU')
reader = csv.DictReader(f)
targetList = []
for l in reader:
AGNname = l[AGNheader]
include = l['include']
if include == 'yes':
include = True
else:
include = False
# velocityHeader is the name of the columns containing the center velocity, or
# it can also be just a number designating the center velocity
if isNumber(velocityHeader):
vel = velocityHeader
else:
vel = l[velocityHeader]
galaxyName = l['galaxyName']
if isNumber(vel) and galaxyName !='x':
pair = (AGNname,int(vel), include)
targetList.append(pair)
f.close()
return targetList
def buildFullTargetList(file,AGNheader,velocityHeader):
# makes a targetmap for every absorption line, regardless of whether there is an
# associated galaxy name or not, so long as the line velocity entry is a number
#
# builds a list consisting of tuples: (AGNname, velocity, include)
#
# - AGN name is the name of the target
# - velocity is the velocity of the absorption feature centroid in km/s
# - include is a boolean indicating whether the results should be saved into the
# 'ambiguous' folder (False), or the 'associated' folder (True)
f = open(file,'rU')
reader = csv.DictReader(f)
targetList = []
for l in reader:
AGNname = l[AGNheader]
include = l['include']
if include == 'yes':
include = True
else:
include = False
# velocityHeader is the name of the columns containing the center velocity, or
# it can also be just a number designating the center velocity
if isNumber(velocityHeader):
vel = velocityHeader
else:
vel = l[velocityHeader]
if isNumber(vel):
pair = (AGNname,int(vel),include)
targetList.append(pair)
f.close()
return targetList
def main():
# main function to create targetmaps around selected sightlines
counter = 0
AGNList = []
rowList = []
masterVirList = []
masterCustomList = []
# max impact parameter to use
maxSep = 500
# +/- galaxy velocity to search within around absorption velocity
velocityWindow = 400
# minimum galaxy velocity to include (False to ignore)
minVcorr = 500
# minimum galaxy size to include (False to ignore)
minSize = False
# minimum separation in km/s between the redshift of the AGN and the galaxy (False to ignore)
agnSeparation = False
# include name tags on galaxies? They don't scale very well...
includeNameTags = True
# include a title on the plots?
includeTitle = False
# also make a plot of just real positions of galaxies on the sky in RA and Dec coords?
includeSkyPlot = False
# Save the map plots?
saveMaps = True
# Save the individual map plot tables?
saveMapTables = True
# Save the full results with "include" tags?
saveResults = False
# 2nd place galaxy likelihood * rigor <= 1st place galaxy for 'include'
rigor = 5
# hard limit for likelihood
l_min = 0.01
# bypass l_min for lone galaxies? (i.e. include lone galaxies no matter what likelihood is)
loner = False
# sort results into /associated/, ~/ambiguous/, and ~/nonassociated/ folders?
# if True, these folders must already exist
# if False, puts all the files into saveDirectory as set below
sortIntoFolders = True
# where to save figures and tables
user = getpass.getuser()
if user == "David":
targetFile = '/Users/David/Research_Documents/inclination/git_inclination/LG_correlation_combined5_11_25cut_edit4.csv'
saveDirectory = '/Users/David/Research_Documents/inclination/git_inclination/targetmaps38/'
outputFile = '/Users/David/Research_Documents/inclination/git_inclination/LG_correlation_combined5_11_25cut_edit5.csv'
elif user == "frenchd":
targetFile = '/usr/users/frenchd/inclination/git_inclination/LG_correlation_combined5_11_25cut_edit4.csv'
# saveDirectory = '/usr/users/frenchd/inclination/git_inclination/targetmaps38/'
saveDirectory = '/usr/users/frenchd/iraf/NGC5364/'
# outputFile = '/usr/users/frenchd/inclination/git_inclination/LG_correlation_combined5_11_25cut_edit5.csv'
outputFile = '/usr/users/frenchd/iraf/NGC5364_correlationMap.csv'
else:
print "Unknown user: ",user
sys.exit()
# what are the column names in this file for the AGN name and absorption velocity?
AGNheader = 'AGNname'
velocityHeader = 'Lya_v'
# targets from a file, use this:
# targets = buildFullTargetList(targetFile,AGNheader,velocityHeader)
# or build up a custom list of AGN names and absorption velocities here:
targets = [('SDSSJ135726.27+043541.4',1094.0,True)]
c = 0
for i in targets:
# find AGN environment using the imported version of correlateSingle
AGNname,center,include = i
correlation = correlateSingle.correlateTarget(AGNname, maxSep, agnSeparation, minVcorr, minSize, slow=False)
galaxyInfo = correlation[AGNname]
print '{0} = {1}'.format(AGNname,len(galaxyInfo))
print
# galaxyInfo.sort()
# instantiate some lists for later
galaxyNames = []
separations = []
positions = []
plotPositionsRA = []
plotPositionsDec = []
plotRA = []
plotDec = []
plotAGNposition = []
plotSizes = []
plotVelocity = []
PA = []
inc = []
typeList = []
infoDict = {}
# loop through the returned galaxy environment data, making calculations and
# populating lists as we go
counter = 0
for row in galaxyInfo:
counter+=1
vhel, galaxyRow = row
AGNposition = eval(str(galaxyRow['degreesJ2000RA_DecAGN']))
# crop off results that fall out of the 'velocityWindow' parameter
# 'velocityWindow' = a cut in velocity space
if counter <=5000 and float(vhel)-velocityWindow <= center and float(vhel)+velocityWindow >= center:
separation = galaxyRow['impactParameter (kpc)']
galaxyName = galaxyRow['galaxyName']
galaxyPosition = eval(str(galaxyRow['degreesJ2000RA_DecGalaxy']))
AGNposition = eval(str(galaxyRow['degreesJ2000RA_DecAGN']))
galaxyDist = galaxyRow['distGalaxy (Mpc)']
group = eval(str(galaxyRow['groups_dist_std (Mpc)']))
major,minor = eval(str(galaxyRow['linDiameters (kpc)']))
galaxyVcorr = galaxyRow['vcorrGalaxy (km/s)']
galaxyVel = galaxyRow['radialVelocity (km/s)']
morphology = galaxyRow['morphology']
RC3Type = galaxyRow['RC3type']
RC3PA = galaxyRow['RC3pa (deg)']
RC3inc = galaxyRow['RC3inc (deg)']
positionAngle = galaxyRow['positionAngle (deg)']
inclination = galaxyRow['inclination (deg)']
azimuth = galaxyRow['azimuth (deg)']
# include = galaxyRow['include']
if isNumber(inclination):
inclination = round(eval(inclination),0)
elif isNumber(RC3inc):
inclination = round(eval(RC3inc),0)
if isNumber(galaxyDist):
positions.append(galaxyPosition)
separations.append(float(separation))
if isNumber(positionAngle):
pa = float(positionAngle)
elif isNumber(RC3PA):
pa = float(RC3PA)
else:
pa = 0
#find plot placement
# calculate angular separations in ra and dec to determine positions on chart w.r.t. target AGN
gRA,gDec = float(galaxyPosition[0]),float(galaxyPosition[1])
agnRA,agnDec = float(AGNposition[0]),float(AGNposition[1])
# calculate separation in RA only
# dRA = correlateSingle.calculateImpactParameter_slow(gRA,agnDec,agnRA,agnDec,galaxyDist)
dRA = correlateSingle.calculateImpactParameter(gRA,agnDec,agnRA,agnDec,galaxyDist)
# calculate separation in Dec only
# dDec = correlateSingle.calculateImpactParameter_slow(agnRA,gDec,agnRA,agnDec,galaxyDist)
dDec = correlateSingle.calculateImpactParameter(agnRA,gDec,agnRA,agnDec,galaxyDist)
# add signs back into physical impact parameters
if gRA < agnRA:
dRA = -dRA
# 'edge' effects
if gRA >= 359.0 and agnRA <= 1.0:
dRA = -dRA
if gDec < agnDec:
dDec = -dDec
# calculate size by finding radius
noSize = False
if isNumber(major):
averageSize = float(major)
elif isNumber(minor) and isNumber(inclination):
averageSize = float(minor) / math.cos(float(inclination) * math.pi/180)
elif isNumber(minor) and not isNumber(inclination) and not isNumber(major):
averageSize = float(minor)
inclination = 0
else:
averageSize = 2
inclination = 0
noSize = True
if not isNumber(galaxyVcorr):
galaxyVcorr = 0
galaxyVel = 0
localType = 'x'
rc3Type = RC3Type.lower()
r = rc3Type[:3]
morphology = morphology.lower()
m = morphology[:3]
if morphology != 'x':
if bfind(m,'s'):
if not bfind(m,'s0'):
# straight spiral type
localType = 's'
else:
# lenticular or S0 type
localType = 'e'
elif bfind(m,'e') or bfind(m,'dwarf') or bfind(m,'pec'):
localType = 'e'
elif bfind(m,'len'):
localType = 'e'
elif bfind(m,'Ir') or bfind(m,'Im') or bfind(m,'I ') or bfind(m,'IA'):
localType = 's'
else:
localType = 'x'
# try RC3 types
elif rc3Type !='x':
if bfind(r,'s'):
if not bfind(r,'s0'):
# straight spiral type
localType = 's'
else:
# lenticular or S0 type
localType = 'e'
elif bfind(r,'e') or bfind(r,'dwarf') or bfind(r,'pec'):
localType = 'e'
elif bfind(r,'len'):
localType = 'e'
elif bfind(r,'Ir') or bfind(r,'Im') or bfind(r,'I ') or bfind(r,'IA'):
# irregular type
localType = 's'
else:
localType = 'x'
# Latex format galaxyName
galaxyName = r'{0}'.format(galaxyName)
galaxyName = galaxyName.replace('_','\_')
infoDict[galaxyName]=galaxyRow
galaxyType = {'s':2,'e':1,'x':3}
if noSize:
galaxyNames.append('*'+galaxyName)
else:
galaxyNames.append(galaxyName)
plotPositionsRA.append(dRA)
plotPositionsDec.append(dDec)
plotSizes.append(averageSize)
plotVelocity.append(float(galaxyVel))
typeList.append(galaxyType[localType])
PA.append(pa)
inc.append(inclination)
plotRA.append(gRA)
plotDec.append(gDec)
plotAGNposition.append(AGNposition)
if len(galaxyNames) >=1:
##################################
print 'starting first plot...'
# color map:
colmap = cm.RdBu
x = arange(len(galaxyNames))+1
fig = figure(figsize=(9,7))
ax = fig.add_subplot(111)
width = 0.30
# format the axes:
#
# x-axis
majorLocator = MultipleLocator(100)
majorFormatter = FormatStrFormatter(r'$\rm %d$')
minorLocator = MultipleLocator(50)
ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(majorFormatter)
ax.xaxis.set_minor_locator(minorLocator)
# y axis
majorLocator = MultipleLocator(100)
majorFormatter = FormatStrFormatter(r'$\rm %d$')
minorLocator = MultipleLocator(50)
ax.yaxis.set_major_locator(majorLocator)
ax.yaxis.set_major_formatter(majorFormatter)
ax.yaxis.set_minor_locator(minorLocator)
# ax.xaxis.set_tick_params(labelweight='normal')
# ax.yaxis.set_tick_params(labelweight='normal')
# scale sizes and velocities
maxSize = 300
minSize = 80
largest = float(max(plotSizes))
smallest = float(min(plotSizes))
newSizes = []
# multiply sizes of galaxies by 10
for s in plotSizes:
new = s*10
newSizes.append(new)
vmaxVal = 400
vminVal = -400
# +/- 400 km/s around the center
largestVelocity = velocityWindow
smallestVelocity = -velocityWindow
newVelocities = []
# check if there's more than one
for v in plotVelocity:
# convert to delta-v = v_absorber - v_galaxy (neg = absorber is blueward of galaxy)
velocity = center - v
# newVelocity = ((float(velocity) - smallestVelocity)/(largestVelocity-smallestVelocity)) * (vmaxVal-0)+0
# newVelocities.append(newVelocity)
newVelocities.append(velocity)
# rounding = -1
# step = round(((largestVelocity-smallestVelocity)/vmaxVal),rounding)
# ticks = arange(int(round(smallestVelocity,-2)),int(round(largestVelocity,-2))+int(step),int(step))
rounding = -1
step = 50
ticks = arange(-velocityWindow,velocityWindow+step,int(step))
print 'ticks: ',ticks
norm = matplotlib.colors.Normalize(vmin = vminVal, vmax = vmaxVal)
m = matplotlib.cm.ScalarMappable(norm=norm, cmap=colmap)
numberGalaxies = len(newVelocities)
galaxyIndices = range(numberGalaxies)
# make ellipses to indicate position angle and inclination at once
if len(plotVelocity) !=0:
mapping = dict((i,n) for i,n in zip(galaxyIndices,galaxyNames))
for i in range(numberGalaxies):
if typeList[i] == 1 or typeList[i] == 2:
# typeList[i] == 2 is a spiral, 1 is E type
print 'i: ',i,galaxyIndices[i]
e = Ellipse(xy=(plotPositionsRA[i],plotPositionsDec[i]),\
width=newSizes[i] * math.cos(float(inc[i]) * math.pi/180)/2, height=float(newSizes[i])/2, angle=float(-PA[i]))
# no transparency
e.set_alpha(0.9)
if typeList[i] == 2:
# spiral - edge color is black
ax.add_artist(e)
e.set_facecolor(m.to_rgba(newVelocities[i]))
e.set_edgecolor('black')
if typeList[i] == 1:
# elliptical - edge color is same as galaxy
ax.add_artist(e)
e.set_facecolor(m.to_rgba(newVelocities[i]))
e.set_edgecolor(m.to_rgba(newVelocities[i]))
else:
ax.scatter(plotPositionsRA[i],plotPositionsDec[i],s=newSizes[i]*4,c=newVelocities[i],vmin=vminVal,vmax=vmaxVal,marker=(2,1,float(-PA[i])),lw=1,cmap=colmap)
# annotate with galaxy names if includeNameTags == True
if includeNameTags:
if galaxyNames[i].find('*')!=-1:
# this indicates no size data is available
plt.annotate('*'+str(galaxyNames[i]),xy=(plotPositionsRA[i],plotPositionsDec[i]),xytext=(-60,newSizes[i]/6),textcoords='offset points',size=8)
else:
plt.annotate(galaxyNames[i],xy=(plotPositionsRA[i],plotPositionsDec[i]),xytext=(-60,newSizes[i]/6),textcoords='offset points',size=8)
plot1 = ax.scatter(plotPositionsRA,plotPositionsDec,s=0,c=newVelocities,vmin=vminVal,vmax=vmaxVal,marker='.',cmap=colmap)
#########################################################################################
#########################################################################################
# prepare all the stuff for writing. Deciding to write happens later
fieldnames = ('AGNname',\
'center',\
'galaxyName',\
'environment',\
'degreesJ2000RA_DecAGN',\
'degreesJ2000RA_DecGalaxy',\
'likelihood',\
'likelihood_1.5',\
'virialRadius',\
'd^1.5',\
'impactParameter (kpc)',\
'redshiftDistances',\
'vcorrGalaxy (km/s)',\
'radialVelocity (km/s)',\
'vel_diff',\
'distGalaxy (Mpc)',\
'AGN S/N',\
'majorAxis (kpc)',\
'minorAxis (kpc)',\
'inclination (deg)',\
'positionAngle (deg)',\
'azimuth (deg)',\
'RC3flag',\
'RC3type',\
'RC3inc (deg)',\
'RC3pa (deg)',\
'morphology',\
'final_morphology',\
'galaxyRedshift')
virList = []
customList = []
environment = numberGalaxies
for number in range(numberGalaxies):
if mapping[number].find("*")!=-1:
infoRow = infoDict[mapping[number].strip('*')]
else:
infoRow = infoDict[mapping[number]]
inc = infoRow['inclination (deg)']
RC3inc = infoRow['RC3inc (deg)']
if isNumber(inc):
inc = round(float(inc),1)
if isNumber(RC3inc) and str(RC3inc) != '-99':
RC3inc = round(float(RC3inc),1)
pa = infoRow['positionAngle (deg)']
RC3pa = infoRow['RC3pa (deg)']
if isNumber(pa):
pa = round(float(pa),1)
if isNumber(RC3pa) and str(RC3pa) != '-99':
RC3pa = round(float(RC3pa),1)
az = infoRow['azimuth (deg)']
if isNumber(az):
az = round(float(az),1)
else:
az = 'x'
vcorr = infoRow['vcorrGalaxy (km/s)']
vhel = infoRow['radialVelocity (km/s)']
if isNumber(vcorr):
vcorr = round(float(vcorr),1)
vhel = round(float(vhel),1)
vel_dif = vhel - float(center)
else:
vcorr = 'x'
vhel = 'x'
vel_dif = 'x'
major,minor = eval(infoRow['linDiameters (kpc)'])
if isNumber(major):
major = round(float(major),1)
if isNumber(minor):
minor = round(float(minor),1)
agn_sn = 'x'
corrected_az = az
redshiftDistance = 'x'
impact = float(infoRow['impactParameter (kpc)'])
if isNumber(major):
rVir = calculateVirialRadius(major)
# try this "sphere of influence" value instead
m15 = major**1.5
# first for the virial radius
likelihood = math.exp(-(impact/rVir)**2) * math.exp(-(vel_dif/200.)**2)
if rVir>= impact:
likelihood = likelihood*2
# then for the second 'virial like' m15 radius
likelihoodm15 = math.exp(-(impact/m15)**2) * math.exp(-(vel_dif/200.)**2)
if m15>= impact:
likelihoodm15 = likelihoodm15*2
# should be like 33% at 1R_v, and linear down after that.
# 66% at 0.5R_v, something quadratic, or logarithmic
# look up the "sphere of influence" for where the probability drops
# down to 10%
# use M/L, surpress environment by M/L ratios
# also include velocity difference to make the virial radius 3D -
# really a 3D impact parameter
else:
likelihood = -99
likelihoodm15 = -99
rVir = -99
m15 = -99
objectInfoList = [AGNname,\
center,\
infoRow['galaxyName'],\
environment,\
infoRow['degreesJ2000RA_DecAGN'],\
infoRow['degreesJ2000RA_DecGalaxy'],\
likelihood,\
likelihoodm15,\
rVir,\
m15,\
impact,\
redshiftDistance,\
vcorr,\
vhel,\
vel_dif,\
infoRow['distGalaxy (Mpc)'],\
agn_sn,\
major,\
minor,\
inc,\
pa,\
az,\
infoRow['RC3flag'],\
infoRow['RC3type'],\
RC3inc,\
RC3pa,\
infoRow['morphology'],\
infoRow['morphology'],\
infoRow['galaxyRedshift']]
virList.append([likelihood,objectInfoList])
customList.append([likelihoodm15,objectInfoList])
# now sort the list of galaxies by likelihood:
sorted_virList = sorted(virList,reverse=True)
sorted_cusList = sorted(customList,reverse=True)
print AGNname,' - ',center,': ', sorted_virList
print 'and : ', sorted_cusList
# if there are enough galaxies, compare the best two : VIRIAL
if len(sorted_virList) >=2:
first_vir = sorted_virList[0][0]
second_vir = sorted_virList[1][0]
# the most likely galaxy must have rigor * the likelihood of the #2 galaxy
if second_vir*rigor <= first_vir:
# larger than the min?
if first_vir >= l_min:
include_vir = True
else:
include_vir = False
else:
include_vir = False
# include lone galaxies if loner = TRUE, otherwise same l_min requirement
elif len(sorted_virList) == 1:
first_vir = sorted_virList[0][0]
if loner:
# include by default
include_vir = True
elif first_vir >= l_min:
# include only if above l_min threshold
print 'first_vir >= l_min: ',first_vir
print 'AGNname, center = ',AGNname, center
include_vir = True
else:
include_vir = False
print 'first_vir < l_min: ',first_vir
print 'AGNname, center = ',AGNname, center
else:
# if there are no galaxies, don't include
include_vir = False
# if there are enough galaxies, compare the best two : CUSTOM
if len(sorted_cusList) >=2:
first_cus = sorted_cusList[0][0]
second_cus = sorted_cusList[1][0]
# the most likely galaxy must have rigor * the likelihood of the #2 galaxy
if second_cus*rigor <= first_cus:
# above the hard lower limit?
if first_cus >= l_min:
include_cus = True
else:
include_cus = False
else:
include_cus = False
# include lone galaxies if loner = TRUE
elif len(sorted_cusList) == 1:
first_cus = sorted_cusList[0][0]
if loner:
# include by default
include_cus = True
elif first_cus >= l_min:
# include only if above l_min threshold
print 'first_cus >= l_min: ',first_cus
print 'AGNname, center = ',AGNname, center
include_cus = True
else:
# don't include
include_cus = False
print 'first_cus < l_min: ',first_cus
print 'AGNname, center = ',AGNname, center
else:
# if there are no galaxies, don't include
include_cus = False
# where to put it?
if sortIntoFolders:
if include_cus and include_vir:
include_folder = 'associated'
elif include_cus or include_vir:
include_folder = 'ambiguous'
print 'include_cus, include_vir = ',include_cus, include_vir
else:
include_folder = 'nonassociated'
else:
include_folder = ''
# append this result to the master lists
masterVirList.append(sorted_virList)
masterCustomList.append(sorted_cusList)
# include a simple position skyplot?
if includeSkyPlot:
# plot
fig = figure(figsize=(12,10))
ax = fig.add_subplot(111)
plot1 = ax.scatter(plotRA,plotDec,marker='d')
plot2 = ax.scatter(float(AGNposition[0]),float(AGNposition[1]),marker='*')
ax.set_xlabel('RA')
ax.set_ylabel('Dec')
savefig('{0}{1}/map2_{2}_{3}_simple.pdf'.format(saveDirectory,include_folder,AGNname,center),format='pdf')
# save the map plot tables
if saveMapTables:
writerOutFile = open('{0}{1}/map_{2}_{3}_table.csv'.format(saveDirectory,include_folder,AGNname,center),'wt')
writer = csv.DictWriter(writerOutFile, fieldnames=fieldnames)
headers = dict((n,n) for n in fieldnames)
writer.writerow(headers)
for s in sorted_virList:
likelihood, rest = s
row = dict((f,o) for f,o in zip(fieldnames,rest))
writer.writerow(row)
writerOutFile.close()
##########################################################################################
##########################################################################################
# plot AGN target in center
ax.scatter(0,0,s=200,c='black',marker='*')
# cbar = plt.colorbar(plot1,ticks=range(0,21),cmap=colmap,orientation='vertical')
# cbar = plt.colorbar(plot1,ticks=ticks,cmap=colmap,orientation='vertical')
cbar = plt.colorbar(plot1,ticks=ticks,format=r'$\rm %d$',cmap=colmap,orientation='vertical')
# cbar.ax.set_yticklabels([r'$\rm %d$' % i for i ticks])
# cbar.ax.set_yticklabels(['%d' % i for i ticks])
# ax.yaxis.set_ticklabels(['%.2f' % 0.1/100*i for i in np.arange(0,100,10)])
cbar.set_label(r'$\rm \Delta v ~[km ~s^{-1}]$')
ax.grid(b=None,which='major',axis='both')
ax.set_ylim(-500,500)
ax.set_xlim(-500,500)
ax.set_xlabel(r'$\rm R.A. ~Separation ~[kpc]$')
ax.set_ylabel(r'$\rm Dec. ~Separation ~[kpc]$')
if includeTitle:
title("{0} sightline map velocity = {1} +-/ {2} km/s".format(AGNname,center,velocityWindow))
# now write it all to file, or display the finished figure
if saveMaps:
savefig('{0}{1}/map_{2}_{3}.pdf'.format(saveDirectory,include_folder,AGNname,center),\
bbox_inches='tight',format='pdf')
else:
show()
else:
print 'TARGET: {0} = NO GALAXIES!!'.format(i)
#########################################################################################
#########################################################################################
#########################################################################################
#########################################################################################
def writeFullResults(listVir,listCustom):
# write the full results to a new file, specified by 'outputFile'
#
# this new file will contain all results, both positive and negative
'''
list contains entries like [likelihood, [all]], where all is:
('AGNname',\
'center',\
'galaxyName',\
'environment',\
'degreesJ2000RA_DecAGN',\
'degreesJ2000RA_DecGalaxy',\
'likelihood',\
'likelihood_1.5'
'virialRadius',\
'd^1.5',\
'impactParameter (kpc)',\
'redshiftDistances',\
'vcorrGalaxy (km/s)',\
'vel_diff',\
'distGalaxy (Mpc)',\
'AGN S/N',\
'majorAxis (kpc)',\
'minorAxis (kpc)',\
'inclination (deg)',\
'positionAngle (deg)',\
'azimuth (deg)',\
'RC3flag',\
'RC3type',\
'RC3inc (deg)',\
'RC3pa (deg)',\
'morphology',\
'galaxyRedshift')
listVir is sorted by likelihood, listCustom is sorted by custom likelihood
'''
# open the origin file
f = open(targetFile,'rU')
reader = csv.DictReader(f)
# fieldnames for the new outputFile
fieldnames = ('AGNname',\
'center',\
'galaxyName',\
'environment',\
'degreesJ2000RA_DecAGN',\
'degreesJ2000RA_DecGalaxy',\
'likelihood',\
'likelihood_1.5',\