Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

pyfits - Copy header into new file astropy

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'))

Now I have tried:

headermain = fits.getheader(file,0)

and

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

But both give me errors saying that

ValueError: header must be a Header object

How can I fix this?

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

see http://pastebin.com/JXki7EPV

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

or with getheader:

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

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

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...