-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_functionsv2.py
317 lines (253 loc) · 13.9 KB
/
spec_functionsv2.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
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from specutils.manipulation import gaussian_smooth
from specutils import Spectrum1D
import astropy.units as u
def plot_spectrum_with_sliders(star_spectrum_file):
''' # Example usage:
plot_spectrum_with_sliders('./observed_stars/vHD14143i.fits')'''
# Directory containing the spectra files
spectra_directory = './norm_tlusty'
# Read all spectra files in the directory
spectra_files = [file for file in os.listdir(spectra_directory) if file.endswith('.fits')]
spectra_files.sort() # Sort the files for consistent ordering
# Extract temperature and gravity values from filenames
temperatures = sorted(list(set([int(file.split('g')[0]) for file in spectra_files])))
gravities = sorted(list(set([int(file.split('g')[1].split('v2')[0]) for file in spectra_files])))
# Load spectra only for existing combinations of temperature and gravity
spectra = {}
for temperature in temperatures:
spectra[temperature] = {}
for gravity in gravities:
filename = f"{temperature}g{gravity}v2.fits"
if filename in spectra_files:
spectra[temperature][gravity] = Spectrum1D.read(os.path.join(spectra_directory, filename))
# Initial values for temperature, gravity, and smoothing
initial_temperature = temperatures[0]
initial_gravity = gravities[0]
initial_smoothing = 1
current_spectrum = spectra[initial_temperature][initial_gravity]
fig, ax = plt.subplots(figsize=(10, 5))
fig.subplots_adjust(
top=0.965,
bottom=0.23,
left=0.03,
right=0.99,
hspace=0.2,
wspace=0.2
)
line, = ax.plot(current_spectrum.spectral_axis, current_spectrum.flux, lw=2, color='red', alpha=0.5, label='Template')
# Plot some general lines
Hlines = {r'$\mathrm{H}_{\alpha}$': 6562.79, r'$\mathrm{H}_{\beta}$': 4861.35, r'$\mathrm{H}_{\gamma}$': 4340.47, r'$\mathrm{H}_{\delta}$': 4101.73, r'$\mathrm{H}_{\epsilon}$': 3970.07}
color_map = plt.get_cmap('Spectral_r')
min_wavelength = 4000
max_wavelength = 7000
for name, Hline in Hlines.items():
normalized_wavelength = (Hline - min_wavelength) / (max_wavelength - min_wavelength) # Adjust min and max accordingly
ax.vlines(Hline, ymin=-.5, ymax=0.4, color=color_map(normalized_wavelength), alpha=0.7, linestyles='dashdot', lw=2)
ax.text(Hline, 0, name, rotation=0, va='bottom', ha='center', fontsize=20)
# Load the star spectrum
star_spectrum = Spectrum1D.read(star_spectrum_file)
star_line, = ax.plot(star_spectrum.spectral_axis, star_spectrum.flux, color='blue', alpha=0.7, label=f"{star_spectrum_file.split('/')[-1][1:-6]}")
plt.title(star_spectrum_file.split('/')[-1][1:-6])
plt.xlabel(r'Wavelength $\AA$')
plt.ylabel(r'Flux')
plt.xlim(4000, 4500)
plt.ylim(0, 1.2)
axcolor = 'red'
# Define axes positions
ax_smoothing_star = plt.axes([0.2, 0.10, 0.5, 0.03], facecolor=axcolor)
ax_smoothing_spec = plt.axes([0.2, 0.07, 0.5, 0.03], facecolor=axcolor)
ax_temp = plt.axes([0.2, 0.04, 0.5, 0.03], facecolor=axcolor)
ax_gravity = plt.axes([0.2, 0.01, 0.5, 0.03], facecolor=axcolor)
ax_radial_velocity = plt.axes([0.2, 0.14, 0.5, 0.03], facecolor=axcolor)
slider_temp = Slider(ax_temp, 'Temp. ($K$)', temperatures[0], temperatures[-1], valinit=initial_temperature,
valstep=1000)
slider_gravity = Slider(ax_gravity, r'$\log{g}$', gravities[0], gravities[-1], valinit=initial_gravity, valstep=25)
slider_smoothing_spec = Slider(ax_smoothing_spec, 'smooth (template)', 1, 200, valinit=initial_smoothing, valstep=5)
slider_smoothing_star = Slider(ax_smoothing_star, 'smooth (star)', 1, 10, valinit=initial_smoothing, valstep=0.5)
slider_radial_velocity = Slider(ax_radial_velocity, 'Radial Velocity (km/s)', -50, 50, valinit=0, valstep=2)
def update(val):
temperature = int(slider_temp.val)
gravity = int(slider_gravity.val)
smoothing_spec = (slider_smoothing_spec.val)
smoothing_star = (slider_smoothing_star.val)
radial_velocity = slider_radial_velocity.val
try:
current_spectrum = spectra[temperature][gravity]
# Apply smoothing to the spectrum
smoothed_spectrum = gaussian_smooth(current_spectrum, smoothing_spec)
line.set_ydata(smoothed_spectrum.flux)
line.set_xdata(smoothed_spectrum.spectral_axis)
# Apply radial velocity shift to the star spectrum
shifted_star_spectrum = star_spectrum.shift_spectrum_to(radial_velocity=-radial_velocity * u.Unit("km/s"))
star_line.set_ydata(star_spectrum.flux)
star_line.set_xdata(star_spectrum.spectral_axis)
# Apply smoothing to the star spectrum
smoothed_star_spectrum = gaussian_smooth(star_spectrum, smoothing_star)
star_line.set_ydata(smoothed_star_spectrum.flux)
star_line.set_xdata(smoothed_star_spectrum.spectral_axis)
# Remove previous annotation text
for txt in ax.texts:
txt.remove()
# Update text annotation
ax.text(0.95, 0.05, f'Temperature: {temperature} K\n $\log{{g}}$: {gravity/100} dex \n RV: {radial_velocity} km/s',
horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes,
bbox=dict(facecolor='white', alpha=0.5))
except KeyError:
pass # Do nothing if the spectra for the given temperature and gravity don't exist
plt.legend()
plt.draw()
slider_temp.on_changed(update)
slider_gravity.on_changed(update)
slider_smoothing_spec.on_changed(update)
slider_smoothing_star.on_changed(update)
slider_radial_velocity.on_changed(update)
# Function to handle keyboard events
def on_key(event):
if event.key == 'left':
slider_temp.set_val(max(slider_temp.val - slider_temp.valstep, slider_temp.valmin))
elif event.key == 'right':
slider_temp.set_val(min(slider_temp.val + slider_temp.valstep, slider_temp.valmax))
elif event.key == 'down':
slider_gravity.set_val(max(slider_gravity.val - slider_gravity.valstep, slider_gravity.valmin))
elif event.key == 'up':
slider_gravity.set_val(min(slider_gravity.val + slider_gravity.valstep, slider_gravity.valmax))
# Connect the keyboard event handler
fig.canvas.mpl_connect('key_press_event', on_key)
plt.show()
def plot_spectrum_with_sliders_k(star_spectrum_file):
''' # Example usage:
plot_spectrum_with_sliders('./observed_stars/vHD14143i.fits')'''
# Directory containing the spectra files
import os
spectra_directory = './Kuruczall'
# Read all spectra files in the directory
spectra_files = [file for file in os.listdir(spectra_directory) if file.endswith('.fits')]
spectra_files.sort() # Sort the files for consistent ordering
# Extract temperature and gravity values from filenames
temperatures = sorted(list(set([int(file.split("_")[0]) for file in spectra_files])))
gravities = sorted(list(set([float(file.split('_')[1].split(".fits")[0]) for file in spectra_files])))
gravities_ = [f"{value:.5f}" for value in gravities]
# Load spectra only for existing combinations of temperature and gravity
spectra = {}
for temperature in temperatures:
spectra[temperature] = {}
for gravity in gravities:
filename = f"{temperature}_{gravity:.5f}.fits"
if filename in spectra_files:
spectra[temperature][gravity] = Spectrum1D.read(os.path.join(spectra_directory, filename))
# Initial values for temperature, gravity, and smoothing
initial_temperature = temperatures[0]
initial_gravity = gravities[0]
initial_smoothing = 1
current_spectrum = spectra[initial_temperature][initial_gravity]
fig, ax = plt.subplots(figsize=(10, 5))
fig.subplots_adjust(
top=0.965,
bottom=0.23,
left=0.03,
right=0.99,
hspace=0.2,
wspace=0.2
)
line, = ax.plot(current_spectrum.spectral_axis, current_spectrum.flux, lw=2, color='red', alpha=0.5, label='Template')
# Plot some general lines
Hlines = {r'$\mathrm{H}_{\alpha}$': 6562.79, r'$\mathrm{H}_{\beta}$': 4861.35, r'$\mathrm{H}_{\gamma}$': 4340.47, r'$\mathrm{H}_{\delta}$': 4101.73, r'$\mathrm{H}_{\epsilon}$': 3970.07}
color_map = plt.get_cmap('Spectral_r')
min_wavelength = 4000
max_wavelength = 7000
for name, Hline in Hlines.items():
normalized_wavelength = (Hline - min_wavelength) / (max_wavelength - min_wavelength) # Adjust min and max accordingly
ax.vlines(Hline, ymin=-.5, ymax=0.4, color=color_map(normalized_wavelength), alpha=0.7, linestyles='dashdot', lw=2)
ax.text(Hline, 0, name, rotation=0, va='bottom', ha='center', fontsize=20)
# Load the star spectrum
star_spectrum = Spectrum1D.read(star_spectrum_file)
star_line, = ax.plot(star_spectrum.spectral_axis, star_spectrum.flux, color='blue', alpha=0.7, label=f'{star_spectrum_file.split("/")[-1][1:-6]}')
plt.title(star_spectrum_file.split('/')[-1][1:-6])
plt.xlabel(r'Wavelength $\AA$')
plt.ylabel(r'Flux')
plt.xlim(4000, 4500)
plt.ylim(0, 1.2)
axcolor = 'red'
# Define axes positions
ax_smoothing_star = plt.axes([0.2, 0.10, 0.5, 0.03], facecolor=axcolor)
ax_smoothing_spec = plt.axes([0.2, 0.07, 0.5, 0.03], facecolor=axcolor)
ax_temp = plt.axes([0.2, 0.04, 0.5, 0.03], facecolor=axcolor)
ax_gravity = plt.axes([0.2, 0.01, 0.5, 0.03], facecolor=axcolor)
ax_radial_velocity = plt.axes([0.2, 0.14, 0.5, 0.03], facecolor=axcolor)
slider_temp = Slider(ax_temp, 'Temp. ($K$)', temperatures[0], temperatures[-1], valinit=initial_temperature,
valstep=250)
slider_gravity = Slider(ax_gravity, r'$\log{g}$', gravities[0], gravities[-1], valinit=initial_gravity, valstep=0.5)
slider_smoothing_spec = Slider(ax_smoothing_spec, 'smooth (template)', 1, 200, valinit=initial_smoothing, valstep=5)
slider_smoothing_star = Slider(ax_smoothing_star, 'smooth (star)', 1, 10, valinit=initial_smoothing, valstep=0.5)
slider_radial_velocity = Slider(ax_radial_velocity, 'Radial Velocity (km/s)', -50, 50, valinit=0, valstep=2)
def update(val):
temperature = int(slider_temp.val)
gravity = int(slider_gravity.val)
smoothing_spec = (slider_smoothing_spec.val)
smoothing_star = (slider_smoothing_star.val)
radial_velocity = slider_radial_velocity.val
try:
current_spectrum = spectra[temperature][gravity]
# Apply smoothing to the spectrum
smoothed_spectrum = gaussian_smooth(current_spectrum, smoothing_spec)
line.set_ydata(smoothed_spectrum.flux)
line.set_xdata(smoothed_spectrum.spectral_axis)
# Apply radial velocity shift to the star spectrum
shifted_star_spectrum = star_spectrum.shift_spectrum_to(radial_velocity=-radial_velocity * u.Unit("km/s"))
star_line.set_ydata(star_spectrum.flux)
star_line.set_xdata(star_spectrum.spectral_axis)
# Apply smoothing to the star spectrum
smoothed_star_spectrum = gaussian_smooth(star_spectrum, smoothing_star)
star_line.set_ydata(smoothed_star_spectrum.flux)
star_line.set_xdata(smoothed_star_spectrum.spectral_axis)
# Remove previous annotation text
for txt in ax.texts:
txt.remove()
# Update text annotation
ax.text(0.95, 0.05, f'Temperature: {temperature} K\n $\log{{g}}$: {gravity/100} dex \n RV: {radial_velocity} km/s',
horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes,
bbox=dict(facecolor='white', alpha=0.5))
except KeyError:
pass # Do nothing if the spectra for the given temperature and gravity don't exist
plt.draw()
slider_temp.on_changed(update)
slider_gravity.on_changed(update)
slider_smoothing_spec.on_changed(update)
slider_smoothing_star.on_changed(update)
slider_radial_velocity.on_changed(update)
# Function to handle keyboard events
def on_key(event):
if event.key == 'left':
slider_temp.set_val(max(slider_temp.val - slider_temp.valstep, slider_temp.valmin))
elif event.key == 'right':
slider_temp.set_val(min(slider_temp.val + slider_temp.valstep, slider_temp.valmax))
elif event.key == 'down':
slider_gravity.set_val(max(slider_gravity.val - slider_gravity.valstep, slider_gravity.valmin))
elif event.key == 'up':
slider_gravity.set_val(min(slider_gravity.val + slider_gravity.valstep, slider_gravity.valmax))
# Connect the keyboard event handler
fig.canvas.mpl_connect('key_press_event', on_key)
plt.show()
plt.legend()
def plot_spectrum(spec, smooth=None, labels=None):
if smooth is None:
smooth = np.ones(len(spec))
spec_s = []
for _spec, _smooth in zip(spec, smooth):
spec_s.append(gaussian_smooth(_spec,_smooth))
plt.title(f'smooth : {smooth}')
plt.xlabel(r'Wavelength $\AA$')
plt.ylabel(r'Normalized Flux')
plt.tight_layout()
if labels is not None:
print('has al')
for spectra,labl in zip(spec_s, labels):
plt.plot(spectra.spectral_axis, spectra.flux, alpha=0.8, label=labl)
plt.legend()
if labels is None:
for spectra in spec_s:
plt.plot(spectra.spectral_axis, spectra.flux, alpha=0.8)