4.2 - Visualisation du spectre 1D avec SpecUtils et Matplotlib

In [15]:
#Show a unique spectrum 

#imports
from astropy import units as u #units
import astropy.wcs as fitswcs #wcs
from specutils import Spectrum1D, SpectralRegion #spectrum1D (specutils)

spec_path = "dataset/GamCas_20201104T204413.fit"

#open & load spectrum file
file = fits.open(spec_path)  
specdata = file[0].data
header = file[0].header

#make WCS object
wcs_data = fitswcs.WCS(header={'CDELT1': header['CDELT1'], 'CRVAL1': header['CRVAL1'],
                               'CUNIT1': header['CUNIT1'], 'CTYPE1': 'WAVE',
                               'CRPIX1': header['CRPIX1']})

#set flux units
flux= specdata * u.Jy

#create a Spectrum1D object with specutils
spec = Spectrum1D(flux=flux, wcs=wcs_data)

#plot spectrum
fig, ax = plt.subplots(figsize=(16, 9))
ax.plot(spec.spectral_axis * u.AA, spec.flux)

#X axis label
ax.set_xlabel(header['CTYPE1'] + ' - ' + header['CUNIT1'])

#Y axis label
ax.set_ylabel('Relative Flux')

#Grid configuration
ax.grid(color='grey', alpha=0.8, linestyle='-.', linewidth=0.2, axis='both') 
    
#legend configuraiton
legend_value = header['OBJNAME'] + ' - ' + header['DATE-OBS']
ax.legend([legend_value], loc=('best'))

#prepare and set plot title with header infos 
spectrumTitle = header['OBJNAME'] + ' - ' + header['DATE-OBS'] + ' - '+ header['EXPTIME2']+ ' - ' + str(header['DETNAM'])
ax.set_title(spectrumTitle, loc='center', fontsize=12, fontweight=0.5)

#Show Plot
plt.show()