-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSN_tests_tau.py
224 lines (190 loc) · 8.84 KB
/
SN_tests_tau.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
import matplotlib
import pyfits
#matplotlib.use('agg')
import numpy as np
import tau_model as tm
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages as PDF
import os
import time
plt.ioff()
def make_monte(taulist = [0.1,1,2,4,10], SNlist = [3,5,7,10,15,20,30,40,60],
N = 30, SNmin = 5450., SNmax = 5550., lightmin = 5450., lightmax = 5550.):
modellist = ['/d/monk/eigenbrot/WIYN/14B-0456/anal/models/bc03_{}_ChabIMF.fits'.format(i) for i in ['solarZ','004Z','0004Z','0001Z','008Z','05Z']]
fraclist = np.array([1,0.2,0.02,0.005,0.4,2.5])
ssp = '/d/monk/eigenbrot/WIYN/14B-0456/anal/models/bc03_008Z_ChabIMF.fits'
if type(N) is not list:
N = [N]
for SN in SNlist:
direc = 'SN{}'.format(SN)
if not os.path.exists(direc):
os.makedirs(direc)
filename = '{}/run.pro'.format(direc)
if os.path.exists(filename):
n=2
filename = '{}_{}.pro'.format(filename.split('.pro')[0],n)
while os.path.exists(filename):
n += 1
filename = '{}_{}.pro'.format(filename.split('_')[0],n)
f = open(filename,'w')
for tau in taulist:
for i in range(*N):
name = '{}/SN{:02}_t{:03}_N{:03}'.format(direc,SN,tau,i+1)
print name
if i == N[0] - 1:
tm.make_galaxy(name,SSPs=ssp,SN=SN,tau_sf=tau,lightmin = lightmin, lightmax = lightmax,SNmin = SNmin, SNmax = SNmax,
makeplot=True)
else:
tm.make_galaxy(name,SSPs=ssp,SN=SN,tau_sf=tau,lightmin = lightmin, lightmax = lightmax,SNmin = SNmin, SNmax = SNmax,
makeplot=False)
for z in range(fraclist.size):
f.write("do_simple_tau, '{0:}.ms_lin.fits', '{0:}.me_lin.fits', '{0:}_Z{1:04}_fit.dat', wavemin=3750., wavemax=6800., lightmin={2:}, lightmax={3:}, model='{4:}'\n".format(os.path.basename(name),int(fraclist[z]*1000),lightmin,lightmax,modellist[z]))
f.close()
return
def compare_SN(taulist = [0.1,1,2,4,10], SNlist = [3,5,7,10,15,20,30,40,60],
dirpre = '', N = 30, output=None, quant='t'):
ratios = np.zeros((len(SNlist),len(taulist)))
errs = np.zeros(ratios.shape)
agelist = np.zeros(len(taulist))
if len(dirpre) > 0 and dirpre[-1] != '/':
dirpre += '/'
for s, SN in enumerate(SNlist):
direc = '{}SN{}'.format(dirpre,SN)
for t, tau in enumerate(taulist):
tmp = np.zeros(N)
for i in range(N):
model_name = '{}/SN{:02}_t{:03}_N{:03}_model.dat'.\
format(direc,SN,tau,i+1)
fit_name = '{}/SN{:02}_t{:03}_N{:03}_fit.dat'.\
format(direc,SN,tau,i+1)
print model_name
mMMWA, mMLWA = np.loadtxt(model_name,
usecols=(11,12),unpack=True)
fMMWA, fMLWA, ftauV, fZ = np.loadtxt(fit_name,usecols=(11,12,13,17),unpack=True)
if quant == 't':
tmp[i] = 1 - fMLWA/mMLWA
elif quant == 'a':
tmp[i] = 1 - ftauV/1.5
elif quant == 'z':
tmp[i] = 1 - fZ/0.4
# tmp[i] = 1 - fMMWA/mMMWA
agelist[t] = mMLWA
ratios[s,t] = np.mean(tmp)
errs[s,t] = np.sqrt(np.mean((tmp - np.mean(tmp))**2))
fig = plt.figure()
ax = fig.add_subplot(211)
eax = fig.add_subplot(212)
colors = ['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00']
for i in range(len(taulist)):
ax.plot(SNlist, ratios[:,i], color=colors[i], lw=1.5)
# ax.errorbar(SNlist, ratios[:,i], yerr=errs[:,i], color=colors[i],
# label='{:} $\Rightarrow$ {:4.2f} Gyr'.format(taulist[i],agelist[i]))
# ax.plot(SNlist,errs[:,i], label=taulist[i])
eax.plot(SNlist, errs[:,i], color=colors[i], lw=1.5,
label='{:} $\Rightarrow$ {:4.2f} Gyr'.format(taulist[i],agelist[i]))
fig.subplots_adjust(hspace=0.0001)
ax.set_xticklabels([])
ax.set_xlim(0,70)
if quant == 't':
# ax.set_ylim(0.04,1)
ax.set_ylabel(r'$1 - \tau_{L,\mathrm{fit}}/\tau_{L,\mathrm{true}}$')
elif quant == 'a':
# ax.set_ylim(-0.31,0)
ax.set_ylabel(r'$1 - A_{V,\mathrm{fit}}/A_{V,\mathrm{true}}$')
elif quant == 'z':
ax.set_ylabel(r'$1 - Z_\mathrm{fit}/Z_\mathrm{true}$')
eax.set_xlim(ax.get_xlim())
eax.set_ylim(eax.get_ylim()[0],eax.get_ylim()[1]*0.9)
# eax.set_ylim(-0.1,2)
# ax.set_xlabel('SNR')
# ax.set_ylabel('1 - MMWA$_{fit}$/MMWA$_{true}$')
# ax.set_ylim(-2,0.5)
eax.legend(loc=0,numpoints=1,scatterpoints=1,frameon=False,title=r'$\tau_{sf}\Rightarrow\tau_L$')
eax.set_xlabel('SNR')
eax.set_ylabel('RMS')
if output is not None:
pp = PDF(output)
pp.savefig(fig)
pp.close()
return ratios, errs, SNlist, taulist, fig
def plot_covariance(SN, taulist = [0.1,1,2,4,10], N = 30, output = None):
big_age = np.zeros((len(taulist),N))
big_Av = np.zeros(big_age.shape)
big_Z = np.zeros(big_age.shape)
agelist = np.zeros(len(taulist))
fig = plt.figure(figsize=(8,8))
age_Av_ax = fig.add_subplot(221)
age_Z_ax = fig.add_subplot(223)
Av_Z_ax = fig.add_subplot(224)
colors = ['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00']
direc = 'SN{}'.format(SN)
for t, tau in enumerate(taulist):
tmp_age = np.zeros(N)
tmp_Av = np.zeros(N)
tmp_Z = np.zeros(N)
for i in range(N):
model_name = '{}/SN{:02}_t{:03}_N{:03}_model.dat'.\
format(direc,SN,tau,i+1)
fit_name = '{}/SN{:02}_t{:03}_N{:03}_fit.dat'.\
format(direc,SN,tau,i+1)
print model_name
mMMWA, mMLWA = np.loadtxt(model_name,
usecols=(11,12),unpack=True)
fMMWA, fMLWA, ftauV, fZ = np.loadtxt(fit_name,usecols=(11,12,13,17),unpack=True)
tmp_age[i] = 1 - fMLWA/mMLWA
tmp_Av[i] = 1 - ftauV/1.5
tmp_Z[i] = 1 - fZ/0.4
age_Av_ax.scatter(tmp_age, tmp_Av, color=colors[t], s=20, alpha=0.7, linewidths=0,
label='{:} $\Rightarrow$ {:4.2f} Gyr'.format(tau,mMLWA))
age_Z_ax.scatter(tmp_age, tmp_Z, color=colors[t], s=20, alpha=0.7,linewidths=0)
Av_Z_ax.scatter(tmp_Av, tmp_Z, color=colors[t], s=20, alpha=0.7,linewidths=0)
Av_Z_ax.set_ylim(-6,1.8)
Av_Z_ax.set_xlim(-0.89,0.6)
age_Av_ax.set_xlim(-1,1.3)
age_Z_ax.set_xlim(*age_Av_ax.get_xlim())
age_Z_ax.set_ylim(*Av_Z_ax.get_ylim())
age_Av_ax.set_ylim(*Av_Z_ax.get_xlim())
age_Av_ax.set_xticklabels([])
Av_Z_ax.set_yticklabels([])
age_Av_ax.set_ylabel('$A_V$ frac. offset')
age_Z_ax.set_ylabel('$Z$ frac. offset')
age_Z_ax.set_xlabel(r'$\tau_L$ frac. offset')
Av_Z_ax.set_xlabel('$A_V$ frac. offset')
fig.subplots_adjust(hspace=0.001,wspace=0.001)
age_Av_ax.legend(loc='center', bbox_to_anchor=(1.5,0.5), title=r'$\tau_{SF}\Rightarrow\tau_L$',
frameon=False, numpoints=1, scatterpoints=1)
age_Av_ax.text(1.5,1,'SN = {}'.format(SN), ha='center',va='center',
transform=age_Av_ax.transAxes)
return fig
def plot_all_covar(output, SNlist = [3,5,7,10,15,20,30,40,60]):
pp = PDF(output)
for SN in SNlist:
fig = plot_covariance(SN)
pp.savefig(fig)
pp.close()
return
def get_metal(taulist = [0.1,1,2,4,10], SNlist = [5,10,20,40,60],
N = 100):
modellist = ['/d/monk/eigenbrot/WIYN/14B-0456/anal/models/bc03_{}_ChabIMF.fits'.format(i) for i in ['solarZ','004Z','0004Z','0001Z','008Z','05Z']]
fraclist = np.array([1,0.2,0.02,0.005,0.4,2.5])
for SN in SNlist:
direc = 'SN{}'.format(SN)
for tau in taulist:
for i in range(N):
tmp = np.zeros((fraclist.size,21))
for z in range(fraclist.size):
name = '{}/SN{:02}_t{:03}_N{:03}_Z{:04}_fit.dat'.format(direc,SN,tau,i+1,int(fraclist[z]*1000))
print name
tmp[z] = np.loadtxt(name)
bdx = np.argmin(tmp[:,16])
h = open(name,'r')
head = h.readlines()[4]
f = open('{}/SN{:02}_t{:03}_N{:03}_fit.dat'.format(direc,SN,tau,i+1),'w')
f.write('# Generated on {}\n'.format(time.asctime()))
f.write(head)
f.write(str('{:11n}'+12*'{:13.3e}'+'{:7.2f}{:12.3f}'+2*'{:12.3e}').format(*tmp[bdx][:-4]))
f.write('{:10.3f}'.format(fraclist[bdx]))
f.write('{:10.3e}{:10.3f}{:10.3f}\n'.format(*tmp[bdx][-3:]))
f.close()
h.close()
return