【问题标题】:How can I make a table using lists as columns?如何使用列表作为列制作表格?
【发布时间】:2021-12-05 16:16:42
【问题描述】:

我在 Ubuntu 20.10 的 IDLE 中使用 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)

【问题讨论】:

    标签: python fits tabulate


    【解决方案1】:

    既然是我自己想出来的,我会在这里留下答案,以防其他人需要它。这比我想象的要容易得多。基本上我制作了一个矩阵作为列表列表,然后我将其转置。这样,我就有了我想要的行和列。然后,您只需将其与正确的标题一起制成表格即可。代码如下:

        #make matrix as list of lists and transpose it
        A = np.array([names, normal_dates, instrument, rv, rv_errors,
                           date, BIS, airmass])
        B = np.transpose(A)
        
        #headers for table
        headers = ['Target', 'Date of observation', 'Instrument', 'RV',
                   'RV error', 'BJD', 'BIS', 'Air mass']
    
        
        param_table = tabulate(B, headers = headers)
        
        print(param_table)
    
    

    【讨论】:

    • 什么是tabulate()??我不认为这是 Astropy 中的内置函数。
    • 不,它是另一个模块。您需要安装它(pip install tabulate on Ubuntu),然后“从制表导入制表”(见我的代码的开头)。很快,它可以让您快速打印漂亮的表格。这是信息:pypi.org/project/tabulate
    • 我明白了,谢谢你的澄清。当您写到“制作桌子”时,我以为您的意思是astropy.table.Table,所以这个问题令人困惑。顺便说一句,不相关,但您不需要在所有这些全局变量上显式声明 global。仅当您打算从函数内分配给全局变量时才需要这样做(撇开是否应该使用全局变量,尽管对于小型研究脚本我认为这很好)。
    • 好的,谢谢建议!
    猜你喜欢
    • 2019-01-18
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2021-11-01
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多