-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.py
340 lines (274 loc) · 11 KB
/
cmd.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
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from mpl_toolkits.axes_grid1 import make_axes_locatable
from astropy.table import Table
plt.rcParams['legend.numpoints'] = 1
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.unicode'] = False
def points_inside_poly(points, all_verts):
"""Return bool array of points inside a polygon """
from matplotlib.path import Path
return Path(all_verts).contains_points(points)
def emboss(fg='w', lw=3):
"""force embossing"""
from matplotlib.patheffects import withStroke
myeffect = withStroke(foreground=fg, linewidth=lw, alpha=0.5)
return dict(path_effects=[myeffect])
def coolarrow(color='k'):
"""Custom arrow"""
return dict(arrowstyle='simple', ec="none", color=color, **emboss(lw=2))
def m2M(m, p='vista'):
"""shorter call to mag2Mag for M31"""
return mag2Mag(m, 'Ks', p, dmod=dmod, Av=Av)
def mag2Mag(mag, filterx, photsys, Av=0., dmod=0.):
'''
convert mag to Abs mag using Cardelli et al 1998 extinction curve
with Rv = 3.1
'''
if Av != 0.0:
Alam_Av = parse_mag_tab(photsys, filterx)
A = Alam_Av * Av
return mag-dmod-A
def parse_mag_tab(photsys, filt):
"""return Alambda/Av for a filter given the photsys -- need the file!"""
tab_mag = './data/tab_mag_{:s}.dat'.format(photsys)
tab = open(tab_mag, 'r').readlines()
mags = tab[1].strip().split()
Alam_Av = np.array(tab[3].strip().split(), dtype=float)
return Alam_Av[mags.index(filt)]
def annotations(jwst=False):
"""Add text annotations and arrows to plot"""
# Note: y values are in mags and converted to Abs Mag
# Milky Way foreground
red = '#FF0013' # used for HB, RC
orange = '#FF8C00' # used for AGB
blue = 'navy' # used for MS
green = '#138A00' # used for HeB
tcolor = 'k' # used for TRGB
ccolor = 'k' # used for MW, Galaxies
size = 9.5
ax.annotate(r'$\rm{MW}$',
xy=(0.35, m2M(13)), xytext=(-0.1, m2M(12)),
color=ccolor, arrowprops=coolarrow(ccolor),
size=size, **emboss())
ax.annotate(r'$\rm{MW}$',
xy=(0.72, m2M(18)), xytext=(0.9, m2M(17.1)),
color=ccolor, arrowprops=coolarrow(ccolor),
size=size, **emboss())
if jwst:
xytext=(1.8, 2.46)
else:
xytext=(1.3, m2M(22))
ax.annotate(r'$\rm{Galaxies}$',
xy=(1.5, m2M(20)), xytext=xytext,
color=ccolor, arrowprops=coolarrow(ccolor),
size=size, **emboss())
ax.annotate(r'$\rm{MS}$',
xy=(-0.0, m2M(17.8)), xytext=(-0.3, m2M(16.5)),
color=blue, arrowprops=coolarrow(blue),
size=size, **emboss())
ax.annotate(r'$\rm{HB}$',
xy=(0.3, m2M(17.6)), xytext=(0.0, m2M(16.5)),
color=red, arrowprops=coolarrow(red),
size=size, **emboss())
ax.annotate(r'$\rm{RC}$',
xy=(0.5, m2M(16.8)), xytext=(0.05, m2M(15.4)),
color=red, arrowprops=coolarrow(red),
size=size, **emboss())
ax.annotate(r'$\rm{HeB}$',
xy=(0.47, m2M(15.2)), xytext=(0.05, m2M(14)),
color=green, arrowprops=coolarrow(green),
size=size, **emboss())
ax.annotate(r'$\rm{TRGB}$',
xy=(1.05, m2M(12.2)), xytext=(1.3, m2M(12.7)),
color=tcolor, arrowprops=coolarrow(tcolor),
size=size, **emboss())
ax.annotate(r'$\rm{O\!-\!rich\ AGB}$',
xy=(1.2, m2M(10.3)), xytext=(0.9, m2M(9)),
color=orange, arrowprops=coolarrow(orange),
size=size, **emboss())
ax.annotate(r'$\rm{C\!-\!rich\ AGB}$',
xy=(1.7, m2M(11)), xytext=(1.8, m2M(12.7)),
color=orange, arrowprops=coolarrow(orange),
size=size, **emboss())
ax.annotate(r'$\rm{Extreme\ AGB}$',
xy=(2.5, m2M(10.5)), xytext=(2., m2M(9)),
color=orange, arrowprops=coolarrow(orange),
size=size, **emboss())
if jwst:
# WD label
ax.annotate(r'$\rm{WD}$', xy=(-0.2, 9.), xytext=(0, 9.),
color='#0EA5FF', arrowprops=coolarrow('#0EA5FF'),
size=size, **emboss(), va='center')
# BD label
ax.annotate(r'$\rm{BD}$', xy=(0.77, 9.), xytext=(1.1, 9.),
color='darkred', arrowprops=coolarrow('darkred'),
size=size, **emboss(), va='center')
# MS Kink ~5
ax.annotate(r'$\rm{MS\ Kink}$', xy=(0.85, 5.), xytext=(1.1, 5.),
color='darkred', arrowprops=coolarrow('darkred'),
size=size, **emboss(), va='center')
# Distance lines
# x is padded x limint
x = ax.get_xlim()[1] - 0.02
kw = {'color': 'k', 'size': size, 'va': 'bottom', 'ha': 'right'}
lkw = {'color': 'k', 'alpha': 0.4, 'lw': 1}
if not jwst:
tenmpc = -3
fourmpc = -1
onempc = 2
onefiftykpc = 6
else:
# From Jason: This Figure appears to be made for the rough F200W = 29
# (AB mag) limit that JWST reaches in 10,000s (3 hours).
# For the figure in the paper, I’d like to show cases that are a bit
# deeper than this...more like 10 or 20 hours. That will give us ~1
# magnitude of depth. For F200W = 30 (AB mag), the K (vega mag) will
# be 28.2.
tenmpc = -1.8
fourmpc = 0.19
onempc = 3.2
onefiftykpc = 7.32
fiftympc = -5.3
fivekpc = 14.71
ax.annotate(r'$10\ \rm{Mpc}$', (x, tenmpc), **kw)
ax.axhline(tenmpc, **lkw)
ax.annotate(r'$4\ \rm{Mpc}$', (x, fourmpc), **kw)
ax.axhline(fourmpc, **lkw)
ax.annotate(r'$1\ \rm{Mpc}$', (x, onempc), **kw)
ax.axhline(onempc, **lkw)
ax.axhline(onefiftykpc, **lkw)
if not jwst:
ax.annotate(r'$\rm{Galactic\ Satellites}$', (x, mwsats), **kw)
else:
ax.annotate(r'$\rm{MW\ Satellites}$', (x, onefiftykpc), **kw)
ax.annotate(r'$50\ \rm{Mpc}$', (x, fiftympc), **kw)
ax.axhline(fiftympc, **lkw)
ax.annotate(r'$\rm{Nearby\ Clusters}$', (x, fivekpc), **kw)
ax.axhline(fivekpc, **lkw)
return ax
def loadvmc(inputfile, testphase=False):
if testphase:
dat = fits.getdata(inputfile, data_end=5000)
else:
dat = fits.getdata(inputfile)
mag1 = mag2Mag(dat['jAperMag3'], 'J', 'vista', dmod=dmod, Av=Av)
mag = mag2Mag(dat['ksAperMag3'], 'Ks', 'vista', dmod=dmod, Av=Av)
cor = mag1 - mag
return cor, mag
def loadsim(inputfile):
sgal = Table.read(inputfile, format='ascii.commented_header', guess=False)
inds, = np.nonzero(sgal['stage'] >= 1)
smag = mag2Mag(sgal['Ks'][inds], 'Ks', 'vista', dmod=dmod, Av=Av)
smag1 = mag2Mag(sgal['J'][inds], 'J', 'vista', dmod=dmod, Av=Av)
scor = smag1 - smag
return scor, smag
def load2mass(inputfile):
# 2mass data:
dat = fits.getdata(inputfile)
mag1 = mag2Mag(dat['Jmag'], 'J', '2mass', dmod=dmod, Av=Av)
dmag = mag2Mag(dat['Kmag'], 'Ks', '2mass', dmod=dmod, Av=Av)
dcor = mag1 - dmag
return dcor, dmag
def loadwd(inputfile):
dat = Table.read(inputfile, format='ascii')
mag1 = mag2Mag(dat['J'], 'J', '2mass', dmod=0., Av=Av)
dmag = mag2Mag(dat['K'], 'Ks', '2mass', dmod=0., Av=Av)
dcor = mag1 - dmag
return dcor, dmag
def loadms(inputfile):
dat = Table.read(inputfile, format='ascii')
mag1 = mag2Mag(dat['F115W_NRC'], 'J', '2mass', dmod=0., Av=Av)
dmag = mag2Mag(dat['F200W_NRC'], 'Ks', '2mass', dmod=0., Av=Av)
dcor = mag1 - dmag
return dcor - 0.11 , dmag - 0.23
def hist2d(vcor, vmag, ax):
# Compute and plot the 2D histogram
xmin = -0.5
xmax = 3
dx = 0.025
ymin = mag2Mag(8, 'Ks', 'vista', dmod=dmod, Av=Av)
ymax = mag2Mag(22, 'Ks', 'vista', dmod=dmod, Av=Av)
dy = 0.06
H, xbins, ybins = np.histogram2d(vcor, vmag,
bins=(np.arange(xmin, xmax, dx),
np.arange(ymin, ymax, dy)))
H2 = H / (dx * dy)
H2[H2 <= .0] = 1 # prevent warnings in log10
density = np.log10(H2)
densmax = np.max(density)
densmin = densmax - 1.8
cmap = plt.cm.gnuplot
cmap.set_under(alpha=0.0)
cmap.set_over('white')
cmap.set_bad(alpha=0.0)
ax.plot(vcor[vmag > m2M(12)], vmag[vmag > m2M(12)], '.', c='black',
markersize=0.2, zorder=0)
cbar = ax.imshow(density.T, origin='lower',
extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
cmap=cmap, interpolation='nearest', aspect='auto',
vmin=densmin, vmax=densmax, zorder=1)
ax.set_xlim(xbins[0], xbins[-1])
ax.set_ylim(ybins[-1], ybins[0])
return ax
def plothist(mag, ax, norm=1, bins=None, alpha=1, findnorm=False):
if bins is None:
bins = np.arange(mag.min(), mag.max(), 0.1)
hist, bins = np.histogram(mag, bins=bins)
ax.plot(hist * norm, bins[:-1], color='k', alpha=alpha)
# iterated to estimate normalization
if findnorm:
try:
print(np.max(hist), np.argmax(hist), hist[84], bins[84])
except:
pass
return ax
# Globals
jwst = True
dmod = 18.493
Av = 0.04
# Data
vcor, vmag = loadvmc('./data/ADP.2011-09-22T15:44:49.967.fits')
tcor, tmag = load2mass('./data/asu.fit')
fig, ax = plt.subplots(figsize=(5., 3.75))
# Vista data
ax = hist2d(vcor, vmag, ax)
# 2mass data
ax.plot(tcor[tmag < m2M(12, p='2mass')], tmag[tmag < m2M(12, p='2mass')], '.',
c='black', markersize=0.4, zorder=0)
if not jwst:
scor, smag = loadsim('./data/wfirstsm.dat')
ax.plot(scor[smag > m2M(10)], smag[smag > m2M(10)], '.',
c='grey', markersize=0.4, zorder=0, alpha=0.3)
divider = make_axes_locatable(ax)
ax1 = divider.append_axes("right", 1., pad=0, sharey=ax)
# Iterated to find best normalization (arb mass simulation)...
norm = 20269 / 893.
bins = np.arange(1.8, smag.max(), 0.1)
ax1 = plothist(smag, ax1, norm=norm, bins=bins, alpha=0.4, findnorm=False)
# Cut MW and Galaxies from LF
verts = [[-0.5, 6.5], [-0.5, m2M(12)], [3, m2M(12)],
[3, -2.9], [.65, -2.9], [0.65, 6.5], [-0.5, 6.5]]
inds = points_inside_poly(np.column_stack([vcor, vmag]), verts)
ax1 = plothist(vmag[inds], ax1)
ax1 = plothist(tmag[[tmag <= m2M(12.1, p='2mass')]], ax1)
ax1.tick_params(labelleft=False, labelsize=10)
ax1.set_xlim(10, ax1.get_xlim()[1])
ax1.set_xscale('log')
ax.set_ylim(6.5, ax.get_ylim()[1])
ax.set_ylabel(r'$K_{\rm{s}}$')
else:
# WD Cooling curve
wcor, wmag = loadwd('./data/Table_Mass_0.5.dat.5')
ax.plot(wcor, wmag, c='#0EA5FF', lw=2.3)
# MS and Brown dwarfs
bcor, bmag = loadms('./data/all47comp11.data')
ax.plot(bcor[bmag > 1], bmag[bmag > 1], c='darkred', lw=2.3, zorder=0)
ax.set_ylabel(r'$K_{\rm{s}}\ \rm{(Vega\ mag)}$')
ax.set_ylim(16., -11)
annotations(jwst=jwst)
ax.set_xlabel(r'$J-K_{\rm{s}}$')
ax.tick_params(labelsize=10)
fig.subplots_adjust(hspace=0.0, wspace=0.0)
plt.savefig('vmc_sep.png', bbox_inches='tight', dpi=300)