I have an image from Hubble telescope with two headers and this creates a problem because my program expects the NAXIS, NAXIS1, NAXIS2 in the first header, but in this image case, this info is in the second header.

So what I need is to invert the order of headers. i.e, SCI header should come first.

When I run:

from astropy.io import fits
image = 'image.fits'

I get

No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     722   ()      
  1  SCI           1 ImageHDU       110   (1900, 1900)   float32

I already tried editing the first header, to add the needed info, but for some reason, it won't accept the changes.

  • $\begingroup$ If your program is actually using FITSIO and not PyFITS/Astropy, then you can use the extended filename syntax to load any extension you want as if it were the primary one. If it's python and you can edit the program, you can specify fits.open(..., ext=1) or similar to load the extension HDU directly. If none of that works, my answer deals with the stated question of reordering the file. $\endgroup$
    – user26287
    Aug 18, 2020 at 23:41

2 Answers 2


EDIT: I moved the last paragraph to the beginning because it's really a Python question, but I left my "manual" method answer in case it helps, too.

Forgot to add this link to Python code on Github. It helped me get started with displaying images using Python and dealing with the headers without altering them. On line 19, I changed it to "image_data = image_file[1].data" because the image was in the HDU [1] instead of HDU [0].

I've had this exact problem when dealing with TESS images - I can't open them in the "usual" software I use for all of my other fits images. Inverting the headers won't solve it, either, because one of the keywords in the first header is needed. If you are only trying to make the change for one image, it's easy to do manually. You can edit the headers in Notepad or any other text editor. Here's the general workaround I use if it's only one file (or a couple of files) - if you need to open a lot then this is too tedious.

  1. Make a copy of the file and change the extension of the copied version to ".txt"
  2. Open in a text editor like Notepad (I like Notepad++ better, but any text editor will work)
  3. Turn on "word wrap" so you can see the header info, there aren't carriage returns or line breaks. Each keyword line is exactly 80 characters long regardless of what info it holds.
  4. Adjust the width of your text editor to get close to 80 characters wide and you'll see the headers "line up" pretty well but not perfectly. There is almost always a situation where the entire 80 characters are used so there isn't a space between the last character of a keyword line and the first character of the next one which will throw off the word wrapping. But that's ok, just frustrating.
  5. Look through the header info until you see the beginning of the first extension (probably named "SCI"). It's fine to select and delete the keyword lines you don't need, the ones breaking it up into two HDUs, as long as you don't need the info. Some of them will be entries such as "MISSION= Hubble" or something like that along with info about the pipeline that created it, etc.
  6. When I've located NAXIS, I put the cursor in front of it and select everything up to the end of the first keyword line in the first HDU which should be "SIMPLE= 1" (or true). The first keyword line with SIMPLE= 1 is 80 characters long and MUST remain 80 characters long or it will break it, so I delte everything between NAXIS and the end of the SIMPLE line. Look to see what the next line is after SIMPLE and delete to the first letter of that keyword. The SIMPLE keyword line is required and should be the first one. Make sure you don't add a carriage return or it'll break the file.
  7. Save your changes and edit the extension to ".fit" or ".fits" and it should open fine.

As long as keep each chunk of keyword info 80 characters in length, you can manually force it to have only one header so you can view the image. This is, of course, not practical for a large number of images and you could lose data that IS important to you such as the date, etc.

To view fits files without having to edit the header, check out SAOimageDS9. It has never failed me in opening an image fits file with multiple HDUs. If you are trying to work with large sets of images there are other workarounds depending on what you're trying to do. Good luck, I hope this works for you!


Create a new PrimaryHDU with the image data and header and write that to disk.

>>> hdul = fits.open(image)
>>> phdu = fits.PrimaryHDU(hdul[1].data, hdul[1].header)
>>> phdu.writeto('single.fits')
>>> fits.info('single.fits')
Filename: single.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  SCI           1 PrimaryHDU     108   (1750, 1750)   float32

You can extend the header to contain the cards of both HDUs; the unique= and update= keyword args of Header.extend can control the behaviour if keywords already exist, and you can use either hdul[0].header or hdul[1].header as the PrimaryHDU header and extend it with the respective other header, whatever works best:

>>> hdul = fits.open(image)
>>> phdu = fits.PrimaryHDU(hdul[1].data, hdul[0].header)  # or: hdul[1].header
>>> phdu.header.extend(hdul[1].header.cards)  # or: hdul[0].header.cards
>>> phdu.writeto('combined.fits')
>>> fits.info('combined.fits')
Filename: combined.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  SCI           1 PrimaryHDU     814   (1750, 1750)   float32

Or perhaps you just want to keep the other header around:

>>> hdul = fits.open(image)
>>> phdu = fits.PrimaryHDU(hdul[1].data, hdul[1].header)
>>> ihdu = fits.ImageHDU(hdul[0].data, hdul[0].header)
>>> newl = fits.HDUList([phdu, ihdu])
>>> newl.writeto('reordered.fits')
>>> fits.info('reordered.fits')
Filename: reordered.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  SCI           1 PrimaryHDU     109   (1750, 1750)   float32
  1                1 ImageHDU       747   ()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .