-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotCUBES.py
More file actions
282 lines (177 loc) · 6.96 KB
/
plotCUBES.py
File metadata and controls
282 lines (177 loc) · 6.96 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 11 14:50:16 2019
@author: Heitor
"""
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from astropy import units as u
from astropy.nddata import StdDevUncertainty
from astropy.modeling import models
from specutils import Spectrum1D, SpectralRegion
import matplotlib.pyplot as plt #doctest:+SKIP
from specutils.analysis import snr
from specutils.analysis import snr_derived
from specutils.analysis import gaussian_sigma_width, gaussian_fwhm, fwhm, fwzi
import random
#-----------------------------------------
def snr_spec(flux,wl,n):
sample = len(wl)
noise = n * np.asarray(random.sample(range(0,len(wl)),sample))/len(wl)
unc = StdDevUncertainty(noise)
fluxn = [[] for i in range(len(wl))]
i=0
for inc in unc:
fluxn[i]=flux[i]+noise[i]
i=i+1
spec1d = Spectrum1D(spectral_axis=wl*u.AA, flux=fluxn*u.Jy , uncertainty= unc )
#ax = plt.subplots()[1]
#ax.plot(spec1d.spectral_axis, spec1d.flux)
#ax.set_xlim([3520,3550])
sn1 = snr(spec1d, SpectralRegion(3070*u.AA, 3090*u.AA))
sn = snr_derived(spec1d,SpectralRegion(3070*u.AA, 3090*u.AA))
#print('SNR1: '+ str(snr(spec1d)), SpectralRegion(3500*u.AA, 3550*u.AA))
print('SNR: '+ str(sn1))
#print('SNR: '+ str(sn))
#print('FWHM:'+str(fwhm(spec1d)))
#0.042 = snr 50
#
try:
return fluxn
except:
raise Exception('Check S/N function')
#-----------------------------------------
sns.set_style("white")
sns.set_context("paper", font_scale=2.0, rc={"lines.linewidth": 2.5})
#-----------------------------------------
line_list= 'linelistCUBES.txt'
Z,lamb = np.genfromtxt(line_list, unpack=True, dtype=str)
print('\n' +str(len(Z))+ ' lines. \n')
#-----------------------------------------
#spectrum
INPUT_spec11= 'StellarGrid/flux_G_m3_m05.norm.nulbad.0.150.B'
INPUT_spec12= 'StellarGrid/flux_G_m3_0.norm.nulbad.0.150.B'
INPUT_spec13= 'StellarGrid/flux_G_m3_05.norm.nulbad.0.150.B'
wl11,flux11 = np.genfromtxt(INPUT_spec11, skip_header=2, unpack=True)
wl12,flux12 = np.genfromtxt(INPUT_spec12, skip_header=2, unpack=True)
wl13,flux13 = np.genfromtxt(INPUT_spec13, skip_header=2, unpack=True)
INPUT_spec21= 'StellarGrid/flux_G_m3_m05.norm.nulbad.0.070.B'
INPUT_spec22= 'StellarGrid/flux_G_m3_0.norm.nulbad.0.070.B'
INPUT_spec23= 'StellarGrid/flux_G_m3_05.norm.nulbad.0.070.B'
wl21,flux21 = np.genfromtxt(INPUT_spec21, skip_header=2, unpack=True)
wl22,flux22 = np.genfromtxt(INPUT_spec22, skip_header=2, unpack=True)
wl23,flux23 = np.genfromtxt(INPUT_spec23, skip_header=2, unpack=True)
OUTPUT=['fig1.pdf']
#-----------------------------------------
f1 = plt.figure(figsize=(12,7))
ax1 = f1.add_subplot(211)
snr1=0.06 #snr 50
ax1.plot(wl11,snr_spec(flux11,wl11,snr1),linewidth=2.5, label='-0.5',color='purple')
ax1.plot(wl12,snr_spec(flux12,wl12,snr1),linewidth=2.5, label='0.0',color='black')
ax1.plot(wl13,snr_spec(flux13,wl13,snr1),linewidth=2.5, label='+0.5',color='blue')
#ax1.legend(loc=2)
ax1.set_title('R=20,000')
#ax1.set_xlabel('Wavelength ( $\AA$ )')
ax1.set_ylabel('Arbritrary Flux')
#ax1.set_xlim([lamb_1-1,lamb_1+1])
#ax1.set_ylim([0.8,1.02])
n=0
for i in Z:
ax1.text(float(lamb[n]), 0.2, i , fontsize=12)
ax1.axvline(x= float(lamb[n]), linewidth=0.6, color='k', ls='--')
n=n+1
#------------------
ax2 = f1.add_subplot(212, sharex=ax1)
snr2=0.06 #snr 50
ax2.plot(wl21,snr_spec(flux21,wl21,snr2),linewidth=2.5, label='-0.5',color='purple')
ax2.plot(wl22,snr_spec(flux22,wl22,snr2),linewidth=2.5, label='0.0',color='black')
ax2.plot(wl23,snr_spec(flux23,wl23,snr2),linewidth=2.5, label='+0.5',color='blue')
#ax1.legend(loc=2)
ax2.set_title('R=40,000')
ax2.set_xlabel('Wavelength ( $\AA$ )')
ax2.set_ylabel('Arbritrary Flux')
#ax1.set_xlim([lamb_1-1,lamb_1+1])
#ax1.set_ylim([0.8,1.02])
n=0
for i in Z:
ax2.text(float(lamb[n]), 0.2, i , fontsize=12)
ax2.axvline(x= float(lamb[n]), linewidth=0.6, color='k', ls='--')
n=n+1
plt.tight_layout()
plt.show()
#f1.savefig(OUTPUT[0])
print('\n')
#-----------------------------------------
#-----------------------------------------
f2 = plt.figure(figsize=(12,10))
ax11 = f2.add_subplot(221)
snr1=0.02 #snr 100
ax11.plot(wl11,snr_spec(flux11,wl11,snr1),linewidth=2.5, label='-0.5',color='purple')
ax11.plot(wl12,snr_spec(flux12,wl12,snr1),linewidth=2.5, label='0.0',color='black')
ax11.plot(wl13,snr_spec(flux13,wl13,snr1),linewidth=2.5, label='+0.5',color='blue')
n=0
for i in Z:
ax11.text(float(lamb[n]), 0.3, i , fontsize=12)
ax11.axvline(x= float(lamb[n]), linewidth=0.6, color='k', ls='--')
n=n+1
#ax1.legend(loc=2)
ax11.set_title('R=20,000 S/N=50')
#ax1.set_xlabel('Wavelength ( $\AA$ )')
ax11.set_ylabel('Arbritrary Flux')
#ax1.set_xlim([lamb_1-1,lamb_1+1])
#ax1.set_ylim([0.8,1.02])
#------------------
ax12 = f2.add_subplot(223, sharex=ax11)
snr2=0.02 #snr 100
ax12.plot(wl21,snr_spec(flux21,wl21,snr2),linewidth=2.5, label='-0.5',color='purple')
ax12.plot(wl22,snr_spec(flux22,wl22,snr2),linewidth=2.5, label='0.0',color='black')
ax12.plot(wl23,snr_spec(flux23,wl23,snr2),linewidth=2.5, label='+0.5',color='blue')
#ax1.legend(loc=2)
ax12.set_title('R=40,000 S/N=50')
ax12.set_xlabel('Wavelength ( $\AA$ )')
ax12.set_ylabel('Arbritrary Flux')
#ax1.set_xlim([lamb_1-1,lamb_1+1])
#ax1.set_ylim([0.8,1.02])
n=0
for i in Z:
ax12.text(float(lamb[n]), 0.3, i , fontsize=12)
ax12.axvline(x= float(lamb[n]), linewidth=0.6, color='k', ls='--')
n=n+1
#-----------------------##########################
ax21 = f2.add_subplot(222, sharex=ax11)
snr1=0.01 #snr 100
ax21.plot(wl11,snr_spec(flux11,wl11,snr1),linewidth=2.5, label='-0.5',color='purple')
ax21.plot(wl12,snr_spec(flux12,wl12,snr1),linewidth=2.5, label='0.0',color='black')
ax21.plot(wl13,snr_spec(flux13,wl13,snr1),linewidth=2.5, label='+0.5',color='blue')
n=0
for i in Z:
ax21.text(float(lamb[n]), 0.3, i , fontsize=12)
ax21.axvline(x= float(lamb[n]), linewidth=0.6, color='k', ls='--')
n=n+1
#ax1.legend(loc=2)
ax21.set_title('R=20,000 S/N=100')
#ax1.set_xlabel('Wavelength ( $\AA$ )')
#ax21.set_ylabel('Arbritrary Flux')
#ax1.set_xlim([lamb_1-1,lamb_1+1])
#ax1.set_ylim([0.8,1.02])
#------------------
ax22 = f2.add_subplot(224, sharex=ax11)
snr2=0.01 #snr 100
ax22.plot(wl21,snr_spec(flux21,wl21,snr2),linewidth=2.5, label='-0.5',color='purple')
ax22.plot(wl22,snr_spec(flux22,wl22,snr2),linewidth=2.5, label='0.0',color='black')
ax22.plot(wl23,snr_spec(flux23,wl23,snr2),linewidth=2.5, label='+0.5',color='blue')
#ax1.legend(loc=2)
ax22.set_title('R=40,000 S/N=100')
ax22.set_xlabel('Wavelength ( $\AA$ )')
#ax22.set_ylabel('Arbritrary Flux')
#ax1.set_xlim([lamb_1-1,lamb_1+1])
#ax1.set_ylim([0.8,1.02])
n=0
for i in Z:
ax22.text(float(lamb[n]), 0.3, i , fontsize=12)
ax22.axvline(x= float(lamb[n]), linewidth=0.6, color='k', ls='--')
n=n+1
#plt.clf()
#-----------------------------------------