我在Ubuntu 20.10上以空闲状态使用Python 3.8.10。
简单地说,我有几个.fits文件,我必须从中读取一些参数。我已经有了readfits函数:它打开文件并将需要的值添加到列表中。现在我需要创建一个函数,将readfits应用于当前目录中的一些文件(这不是问题),然后将它们打印到表中。问题是我拥有的每个列表都是表中的一列,所以我不知道如何做到这一点。我想以递归的方式创建它,因为实际上有104.fits文件,所以手动操作相当长。
以下是目前的代码:
#import needed packages
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
import os
from tabulate import tabulate
#make empty lists where rquantities will be added
rv = []
date = []
rv_errors = []
airmass = []
BIS = []
normal_dates = []
names = []
instrument = []
#create function which reads .fits files and adds values to global lists
def readfits(filename):
#open files and give names to data and header
with fits.open(filename) as hdu:
data = hdu[0].data
head = hdu[0].header
#Useful quantities
#Contrast of CCF in %
contrast = head['HIERARCH TNG DRS CCF CONTRAST']
#FWHM of CCF in km/s
fwhm_ccf = head['HIERARCH TNG DRS CCF FWHM']
#Number of lines used
nr_lines = head['HIERARCH TNG DRS CCF LINES']
#Mask type
mask = head['HIERARCH TNG DRS CCF MASK']
#Right ascension and declination in radians
ra = head['RA']
dec = head['DEC']
#Air mass
air_mass = head['AIRMASS']
#Mean Julian Date of observation
jd = head['MJD-OBS']
#Mean BJD subtracted for plotting reasons
bjd = head['HIERARCH TNG DRS BJD'] - 2450000
#Exposure time in s
exp_time = head['EXPTIME']
#Estimated RV uncertainty in m/s
err_rv = head['HIERARCH TNG DRS DVRMS']
#Noise at order 46 where the stellar peak is
#sn46 = head['HIERARCH TNG DRS SPE EXT SN46']
#Half-window of the CCF
half_wind = head['HIERARCH IA2 YABI WIDTHCCF']
#Step of the CCF
ccf_step = head['HIERARCH IA2 YABI STEPCCF']
#Apparent magnitude
mag = head['HIERARCH TNG TEL TARG MAG']
#Th-lamp drift
th_drift = head['HIERARCH TNG DRS DRIFT SPE RV']
#RV of the system (YABI input)
#rv_sys = head['HIERARCH TNG TEL TARG RADVEL']
#BIS
bis = head['HIERARCH TNG DRS BIS SPAN']
#CCF noise from km/s to m/s
ccf_noise = head['HIERARCH TNG DRS CCF NOISE']*1000
#Measured RV for this target in this night in km/s
rv_mis = head['HIERARCH TNG DRS CCF RVC']
#RV in m/s
rv_true = rv_mis*1000
#readable date
dates = head['DATE-OBS']
#instrument used
strum = head['INSTRUME']
#name of target
name = head['HIERARCH TNG OBS TARG NAME']
#add quantities to the initial lists
global rv
rv.append(rv_true)
global date
date.append(bjd)
global rv_errors
rv_errors.append(ccf_noise)
global airmass
airmass.append(air_mass)
global BIS
BIS.append(bis)
global normal_dates
normal_dates.append(dates)
global instrument
instrument.append(strum)
global names
names.append(name)
#writes values from fits files to txt table
def writetable():
#list with all files in directory
list_all = os.listdir()
list_data = []
#take only files with 'bis' in the name
for i in range(0, len(list_all)):
if 'bis' in list_all[i]:
list_data.append(list_all[i])
#sort elements in alphabetical order
list_data.sort()
#apply readfits to all 'bis' files
n = len(list_data)
for i in range(0,n):
readfits(list_data[i])
global names
global normal_dates
global instrument
global rv
global rv_errors
global date
global BIS
global airmass
#headers for table
headers = ['Target', 'Date of observation', 'Instrument', 'RV',
'RV error', 'BJD', 'BIS', 'Air mass']
param_table = tabulate([], headers = headers)
print(param_table)
既然是我自己想出来的,我会把答案留在这里,以防别人需要。这比我想象的要容易得多。基本上我做了一个矩阵作为一个列表,然后我转置了它。通过这种方式,我得到了我想要的行和列。然后,您只需将它与正确的标题列表,就可以了。代码如下:
相关问题 更多 >
编程相关推荐