-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindIsochrones.py
executable file
·286 lines (231 loc) · 11.4 KB
/
FindIsochrones.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
from __future__ import print_function
from __future__ import division
from builtins import str
import os
import sys
import logging
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
plt.style.use(['seaborn-muted'])
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
from isochrones import SingleStarModel, get_ichrone
from isochrones.mist import MIST_Isochrone
from isochrones.priors import FlatPrior
#@profile
def calc_values(starname, T, err_T, logg, err_logg, met, err_met, photometry,\
makeplot=True, mass_from_file=False,\
is_giant=False, age_limits=None,\
PATH_ISO='./isochrones', Av=[]):
print('\t\tCreating isochrones')
mist = get_ichrone('mist')
props = {'Teff': (T, min(err_T, 250)),
'logg': (logg, min(err_logg, 0.5)),
'feh': (met, err_met)}
props.update(photometry)
#props.update({'density': (2.72, 0.16)})
if os.path.isfile(os.path.join(os.path.relpath(PATH_ISO, '.'), '%s_samples.h5' % starname))\
and mass_from_file:
model = SingleStarModel.load_hdf(os.path.join(os.path.relpath(PATH_ISO, '.'),
'%s_samples.h5' % starname))
print('\t\tModel read from file')
makeplot = False
else:
model = SingleStarModel(mist, name=starname, **props)
model._priors['eep'].bounds = (200, 1710)
if is_giant:
model._priors['eep'].bounds = (353, 1710)
logging.info('EEP prior boundaries set to (%d, %d)', *model._priors['eep'].bounds)
if Av:
Avmax = min(max(np.max(Av), 0.1), 1.0)
#Avmax = 1.0
model.set_prior(AV=FlatPrior((0.0, Avmax)))
logging.info('Prior for Av changed to flat prior with boundaries: (%.1f, %.1f)',
0, Avmax)
logging.info(props)
model.fit(overwrite=True, basename=starname+'_chains_',\
verbose=False, refit=True, n_live_points=1000)
model.save_hdf(os.path.join(os.path.relpath(PATH_ISO, '.'), '%s_samples.h5' % starname),
overwrite=True)
derived = model.derived_samples
mass_s = derived['mass']
age_s = derived['age']
logg_s = derived['logg']
radius_s = derived['radius']
logL_s = derived['logL']
AV = derived['AV']
EEP = derived['eep']
age_s = 10**(age_s)/(1E9)
mass_p = np.percentile(mass_s, [16, 50, 84])
age_p = np.percentile(age_s, [16, 50, 84])
logg_p = np.percentile(logg_s, [16, 50, 84])
radius_p = np.percentile(radius_s, [16, 50, 84])
logL_p = np.percentile(logL_s, [16, 50, 84])
AV_p = np.median(AV)
EEP_p = np.percentile(EEP, [16, 50, 84])
P_preMS, P_MS, P_RGB, P_HB, P_postHB = Pevol(EEP)
if makeplot:
try:
fig = model.corner_physical()
fig.savefig(os.path.join(os.path.relpath(PATH_ISO, '.'),
'plots', '%s_corner_physical.pdf' % starname))
plt.close('all')
del fig
fig = model.corner_observed()
fig.savefig(os.path.join(os.path.relpath(PATH_ISO, '.'),
'plots', '%s_corner_observed.pdf' % starname))
plt.close('all')
del fig
fig = model.corner_derived(['Teff', 'logg', 'age', 'mass',
'radius', 'age', 'distance', 'eep', 'logL'])
fig.savefig(os.path.join(os.path.relpath(PATH_ISO, '.'),
'plots', '%s_corner_derived.pdf' % starname))
plt.close('all')
del fig
plot_isochrones(starname, derived, props, N=10, PATH_ISO=PATH_ISO)
except RuntimeError:
logging.error('Error while plotting. Skipping.')
os.system('rm -f chains/' + starname + '_chains_*')
del model, derived, mass_s, age_s, logg_s, radius_s, logL_s, AV, EEP
return mass_p[1], mass_p[2]-mass_p[1], mass_p[1]-mass_p[0],\
age_p[1], age_p[2]-age_p[1], age_p[1]-age_p[0],\
logg_p[1], logg_p[2]-logg_p[1], logg_p[1]-logg_p[0],\
radius_p[1], radius_p[2]-radius_p[1], radius_p[1]-radius_p[0],\
logL_p[1], logL_p[2]-logL_p[1], logL_p[1]-logL_p[0], AV_p,\
EEP_p[1], EEP_p[2]-EEP_p[1], EEP_p[1]-EEP_p[0],\
P_preMS, P_MS, P_RGB, P_HB, P_postHB
def Pevol(eep):
i = np.where(~np.isnan(eep))[0]
N = i.size
if N == 0:
return 0.0, 0.0, 0.0, 0.0, 0.0
N_preMS = np.where(eep[i] < 202)[0].size
N_MS = np.where((eep[i] >= 202) & (eep[i] <= 454))[0].size
N_RGB = np.where((eep[i] > 454) & (eep[i] < 631))[0].size
N_HB = np.where((eep[i] >= 631) & (eep[i] <= 707))[0].size
N_postHB = np.where(eep[i] > 707)[0].size
return N_preMS/N, N_MS/N, N_RGB/N, N_HB/N, N_postHB/N
def plot_isochrones(starname, derived, props, N=10, PATH_ISO='.'):
try:
mist = MIST_Isochrone()
age_samples = np.random.choice(derived['age'], size=N)
feh_samples = np.random.choice(derived['feh'], size=N)
fig, ax = plt.subplots()
for i in range(10):
grid = mist.isochrone(age_samples[i], feh_samples[i])
ax.plot(grid['logTeff'], grid['logL'],
color='gray', alpha=0.4, lw=0.8, label='_nolegend_')
del grid
age_mean = np.median(derived['age'])
feh_mean = np.median(derived['feh'])
grid = mist.isochrone(age_mean, feh_mean)
ll = ax.scatter(grid['logTeff'], grid['logL'], c=grid['mass'],
cmap='viridis', s=8, label='_nolegend_', edgecolors='face')
cbar = fig.colorbar(ll, ax=ax)
cbar.set_label(r'$M_{\star}$', fontsize='x-large')
del grid
t16, t50, t84 = np.percentile(derived['logTeff'], [16, 50, 84])
l16, l50, l84 = np.percentile(derived['logL'], [16, 50, 84])
ax.errorbar(t50, l50, xerr=[[t50-t16], [t84-t50]], yerr=[[l50-l16], [l84-l50]],
marker='o', color='red', markeredgecolor='red', markersize=10,
label=r'$T$ = %.0f K'
'\n'
r'[Fe/H] = %.2f'
'\n'
r'$M$ = %.2f $M_{\odot}$'
'\n'
r'$R$ = %.2f $R_{\odot}$' % (props['Teff'][0], props['feh'][0],\
np.median(derived['mass']),\
np.median(derived['radius'])))
ax.invert_xaxis()
ax.legend(numpoints=1, loc='lower left')
ax.set_xlabel('logTeff', fontsize='x-large')
ax.set_ylabel('logL', fontsize='x-large')
fig.savefig(os.path.join(os.path.relpath(PATH_ISO, '.'),
'plots', '%s_isochrones.pdf' % starname))
plt.close('all')
del mist, age_samples, feh_samples, fig
except Exception as e:
logging.error('ERROR IN CODE, STOPPING THE COMPUTATION')
_, _, exc_tb = sys.exc_info()
logging.error('line %d: %s', exc_tb.tb_lineno, e)
return 0
def find_mass_age(starname, T, logg, met, err_T, err_logg, err_met, exception, \
photometry, make_plot=True, mass_from_file=False,\
age_limits=None, is_giant=False, PATH_ISO='./isochrones',\
skip_mass=False, plot_mass=False, Av=0.0):
if skip_mass:
logging.warning('Skipping mass computation.')
return 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,\
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
err_T = max(err_T, 0.01)
err_logg = max(err_logg, 0.01)
err_met = max(err_met, 0.01)
AV = 0.0
use_mags = True
logging.info('Computing the mass, age, and photometric logg.')
# Check that PATH_ISO contains a directory called '/plots'
# If not, create it.
if not os.path.exists(os.path.join(os.path.relpath(PATH_ISO, '.'), 'plots')):
os.makedirs(os.path.join(os.path.relpath(PATH_ISO, '.'), 'plots'))
if exception == 1:
photometry_mass = {}
mags = ['J', 'H', 'K', 'B', 'V', 'G']#, 'W1', 'W2', 'W3']
#mags = ['B', 'V', 'G']#, 'W1', 'W2', 'W3']
mags_Av = ['B', 'V', 'Vt', 'Bt']
Av = [Av]
if is_giant:
use_mags = False
for m in mags_Av:
if m in photometry:
if photometry[m][4] > 1.0:
use_mags = False
if not use_mags:
mags = ['J', 'H', 'K', 'G']
for m in mags:
if m in photometry:
#if 'HIPPARCOS' not in photometry[m][3]:
c = photometry[m][0]# - photometry[m][4]
if photometry[m][1] <= 0.3:
photometry_mass[m] = (c, max(photometry[m][1], 0.01))
if photometry[m][4] > 0.0 and photometry[m][4] < 1.0:
Av.append(photometry[m][4])
elif photometry[m][4] <= 0.0:
Av.append(0.1)
if 'parallax' in photometry:
photometry_mass['parallax'] = (photometry['parallax'][0].value, \
photometry['parallax'][1].value)
logging.info('Values used for finding mass and age are: '\
'%s, %f, %f, %f, %f, %f, %f',\
starname, T, logg, met, err_T, err_logg, err_met)
logging.info('Photometry used is: %s', photometry_mass)
if (-2.5 <= met <= 0.5) is False:
print('\t\t[Fe/H] out of bounds, no extrapolation curve created')
met = 0.5 if met > 0.5 else -2.5
print('\t\tIsochrones created for [Fe/H] = %s' % str(met))
mass, err_mass1, err_mass2, age, err_age1, err_age2, logg_iso, \
err_logg_iso1, err_logg_iso2, radius, err_radius1, err_radius2, \
logL, err_logL1, err_logL2, AV, eep, err_eep1, err_eep2, \
p_prems, p_ms, p_rgb, p_hb, p_posthb =\
calc_values(starname, T, err_T, logg, err_logg, met, err_met,
photometry_mass, makeplot=make_plot,
mass_from_file=mass_from_file, PATH_ISO=PATH_ISO,
is_giant=is_giant, age_limits=age_limits, Av=Av)
del photometry_mass
else:
mass, err_mass1, err_mass2, age, err_age1, err_age2, logg_iso, err_logg_iso1, \
err_logg_iso2, radius, err_radius1, err_radius2, \
logL, err_logL1, err_logL2, AV, eep, err_eep1, err_eep2, \
p_prems, p_ms, p_rgb, p_hb, p_posthb = \
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,\
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
logging.warning('Stopping the calculation because exception = 2.')
logging.info('Obtained M=%f + %f - %f, Age=%f + %f - %f, '
'logg=%f + %f - %f, radius=%f + %f - %f',\
mass, err_mass1, err_mass2, age, err_age1, err_age2, logg_iso, \
err_logg_iso1, err_logg_iso2, radius, err_radius1, err_radius2)
return mass, err_mass1, err_mass2, age, err_age1, err_age2,\
logg_iso, err_logg_iso1, err_logg_iso2,\
radius, err_radius1, err_radius2, logL, err_logL1, err_logL2, AV,\
eep, err_eep1, err_eep2, p_prems, p_ms, p_rgb, p_hb, p_posthb