.FITS文件/表到csv?



关于如何将.fits文件(或表)转换为csv的任何建议?我试过了,代码运行了12个多小时。我目前使用pandas并读取表,并尝试保存为csv:

dr_file = get_pkg_data_filename('/users/...data.fits')
events = Table.read(dr_file, hdu=1)
df = pd.DataFrame(events)
df.to_csv('data.csv')

@Iguananaut是正确的;下面是一个作为模板的代码示例:

from astropy.io import fits
from astropy.table import Table
# set memmap=True for large files
with fits.open("my_file.fits", memmap=True):
# select the HDU you want
hdu = hdu_list[1]

# read into an astropy Table object
table = Table(hdu.data)
# write to a CSV file
data.write('path/to/output/my_file.ecsv', delimiter='t', format='ascii')

您当然不希望对图像这样做,但对于表,中间的CSV格式在某些情况下是有用的,例如导入到数据库中。

来得有点晚,但TOPCAT也有将fits表保存为其他格式的选项,包括csv。又快又简单。

Astropy表已经可以直接写入CSV。不需要先把它放在Pandas DataFrame中。Astropy具有面向天文学中使用的格式的广泛的CSV和ASCII表功能。看到https://docs.astropy.org/en/stable/io/ascii/write.html table-or-numpy-structured-array

然而,正如其他人指出的那样,将一个非常大的表转储为文本格式可能是不合适的,因为它的大小可能非常大,并且对于大多数软件来说很难有效地处理。您应该考虑其他二进制格式(其中一些,如HD5,也被Astropy支持),或者您应该只提取需要共享的特定数据,或者只共享FITS文件。

最新更新