简体   繁体   English

使用 astropy 打印 FITS 文件的标题:NameError: name 'header' is not defined

[英]Printing the Header of a FITS file with astropy: NameError: name 'header' is not defined

I am trying to learn to use FITS operations through astropy via http://docs.astropy.org/en/stable/io/fits/我正在尝试通过http://docs.astropy.org/en/stable/io/fits/通过 astropy 学习使用 FITS 操作

I am following the directions on this website.我正在按照本网站上的说明进行操作。 They are:他们是:

"To see the entire header as it appears in the FITS file (with the END card and padding stripped), simply enter the header object by itself, or print(repr(header)) " “要查看出现在 FITS 文件中的整个标题(去掉 END 卡和填充),只需输入标题对象本身,或print(repr(header))

But when I type header , I get the following error :但是当我输入header ,我收到以下错误:

NameError: name 'header' is not defined

I get the same error when I put print(header) or print(repr(header)) commands.当我输入print(header)print(repr(header))命令时,我得到了同样的错误。

My question is why is it that the "header" command did not work?我的问题是为什么“header”命令不起作用?

Am I supposed to define it somehow?我应该以某种方式定义它吗?

My code:我的代码:

from astropy.io import fits
hdulist = fits.open('test1.fits')
hdulist.info()
header

I am using jupyter notebook via Canopy.我正在通过 Canopy 使用 jupyter notebook。

My question is why is it that the "header" command did not work?我的问题是为什么“header”命令不起作用?

Am I supposed to define it somehow?我应该以某种方式定义它吗?

In short: It's not a command and you don't need to define it.简而言之:它不是命令,您不需要定义它。 It's actually an attribute, so you have to look it up on the hdulist .它实际上是一个属性,因此您必须在hdulist上查找它。

The hdulist contains hdu s and each hdu contains a data and a header , so to access the header of the first hdu you use: hdulist包含hdu并且每个hdu包含一个data和一个header ,因此要访问您使用的第一个 hdu 的标头:

print(repr(hdulist[0].header))

The [0] is because I wanted the first HDU (python uses zero-based indexing) and the .header accesses the header attribute of this HDU. [0]是因为我想要第一个 HDU(python 使用基于零的索引)并且.header访问此 HDU 的 header 属性。

Even though I said you don't need to define it, but you can define a variable called header:即使我说你不需要定义它,但你可以定义一个名为 header 的变量:

header = hdulist[0].header   # define a variable named "header" storing the header of the first HDU
print(repr(header))  # now it's defined and you can print it.

How many HDUs are present should be shown by the hdulist.info() , so you can decide which one you want to print or store. hdulist.info()应该显示存在多少 HDU,因此您可以决定要打印或存储哪一个。

Note that you should always use open as a context manager so it closes the file automatically (even in case of Errors):请注意,您应该始终使用open作为上下文管理器,以便它自动关闭文件(即使出现错误):

from astropy.io import fits

with fits.open('test1.fits') as hdulist:  # this is like the "hdulist = fits.open('test1.fits')"
     hdulist.info()
     for hdu in hdulist:
         print(repr(hdu.header))
# After leaving the "with" the file closes.

This example also shows how you can use a for loop to go over all HDUs in a HDUList .此示例还展示了如何使用for循环遍历 HDUList 中的所有HDUList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM