天文图像处理学习笔记—(1)天文图像的读写

如题所述

FITS全称Flexible Image Transport System,天文学界广泛使用的数据格式,由国际天文学会于1982年确定为标准格式,用于不同平台间数据传输、交换。FITS文件由头文件和数据单元组成,头文件包含关键词、值、注释,记录拍摄环境、文件描述等信息,数据单元存储数据内容。Astropy是天文学常用的Python包,用于FITS文件的读写操作。

安装Astropy核心包命令如下:

pip install astropy

进行FITS文件基本操作时,首先导入Astropy包。

例如打开名为“d4466637BIASR213.fit”的FITS文件:

python
from astropy.io import fits
fits.open('raw_data/d4466637BIASR213.fit').info()

输出信息包括文件的头文件内容。获取头文件信息:

python
fits.open('raw_data/d4466637BIASR213.fit')[0].header

可获取关键词、值、注释。往头文件中添加或更新数据,例如添加关键词`a1`,值`a2`,注释`a3`:

python
fits.open('raw_data/d4466637BIASR213.fit')[0].header['a1'] = ('a1', '/a2')

获取头文件全部关键词:

python
fits.open('raw_data/d4466637BIASR213.fit')[0].header.keys()

读取FITS文件数据:

python
fits.open('raw_data/d4466637BIASR213.fit')[0].data

创建新FITS文件,若为图像,用numpy数组封装为PrimaryHDU对象,创建HDU列表,写入文件:

python
numpy.arange(100).tobytes() # 创建numpy数组
hdulist = fits.HDUList([fits.PrimaryHDU(numpy.arange(100))])
hdulist.writeto('newfile.fits')

若为表数据,创建Column对象,封装为BinTableHDU或使用Astropy Table写入文件:

python
# 二进制表
from astropy.table import Table
table = Table([[1, 2], [4, 5]], names=('a', 'b'))
table.write('newfile.fits', format='fits')
# 使用Table封装
from astropy.table import Table
table = Table([[1, 2], [4, 5]], names=('a', 'b'))
table.write('newfile.fits')

以上是FITS文件常用操作方法,更多细节请参考Astropy官方文档。
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜