我一直在使用matplotlib.pyplot从Python中的拟合文件绘制光谱,并获得强度与像素的关系,但是我真正需要的是将像素转换为波长。我看到过类似的问题使我走上了正确的路(例如similar question,RGB example),但我仍然感到迷失。
我有一些FITS文件,它们的波长分别为3500和6000(A),格式为float32,尺寸为(53165,)。
据我了解,我需要将像素位置校准为波长。我有我的其余波长标头(RESW),“步进”波长标头(STW),我需要获取:
x = RESW +(像素数* STW)
并绘制它。到目前为止,这就是我在代码中得到的。
import os, glob
from glob import glob
from pylab import *
from astropy.io import ascii
import scipy.constants as constants
import matplotlib.pylab as plt
from astropy.io import fits
#add directory you have your files in
dir = ''
#OPEN ALL FILES AND STORE THEM INTO A LIST
files= glob(dir + '*.fits')
for fi in (files):
print(fi)
name = fi[:-len('.fits')] #Remove '.fits' from the file name
with fits.open(dir + fi) as hdu:
hdu.info()
data = hdu[0].data
hdr = hdu[0].header #added to try2
step = hdr['CDELT1'] #added to try2
restw = hdr['CRVAL1'] #added to try2
#step = fits.getheader('STW') #added to try
#restw = fits.getheader('RESW') #added to try
spectra = restw + (data * step) #added to try
plt.clf()
plt.plot(spectra)
plt.savefig(name + '.pdf')
我尝试使用fits.getheader(''),但由于这种方式无法正常工作,我不知道在哪里放置或如何放置它。
有人可以帮忙吗?预先感谢!