将标头复制到新文件中 [英] Copy header into new file astropy

查看:135
本文介绍了将标头复制到新文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,可以使文件具有多个扩展名,但是我想将旧文件的标头添加到扩展名中.

I have this script that makes a file with multiple extensions, but I would like to add headers from the old files to the extensions.

new_hdul = fits.HDUList()
new_hdul.append(fits.PrimaryHDU(header=headermain))
new_hdul.append(fits.ImageHDU(nod1, header=header1, name='Chop1')) 
new_hdul.append(fits.ImageHDU(nod2, header=header2, name='Chop2'))
new_hdul.append(fits.ImageHDU(diff1, name='Dif'))

现在我已经尝试:

headermain = fits.getheader(file,0)

headermain = fits.open(file).header.copy()

但是两者都给我说错了

ValueError:标头必须是Header对象

ValueError: header must be a Header object

我该如何解决?

headermain = fits.getheader(file,0)
print(headermain)

请参见 http://pastebin.com/JXki7EPV

推荐答案

通常,从文件获取标头作为Header对象并不复杂.您astropy.io.fits.open()该文件,并使用以下命令从PrimaryHDU中提取标题:

Generally getting the header as Header object from a file isn't complicated. You astropy.io.fits.open() the file and extract the Header from the PrimaryHDU with:

from astropy.io import fits

filename = 'test.fits'

with fits.open(filename) as hdus:
    headermain = hdus[0].header

或使用getheader:

headermain = fits.getheader(filename) # Defaults to primary header!

,结果将是一个fits.Header对象,您可以在编写过程中使用它.

and the result will be a fits.Header-object that you can use during writing.

但是,如果您的文件不是有效的FITS文件,则可能会出现问题.如果这不起作用,您可以编辑问题并显示这两个函数中的任何一个的输出吗?

But if your file is not a valid FITS file there might be problems. If this doesn't work could you edit your question and show the output of any of these two functions?

print(headermain)

这篇关于将标头复制到新文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆