<p>EDIT:  I moved the last paragraph to the beginning because it's really a Python question, but I left my &quot;manual&quot; method answer in case it helps, too.</p>
<p>Forgot to add <a href="https://github.com/MatheusJCastro/astropy_displayFITSfile/blob/master/open_fits.py" rel="nofollow noreferrer">this</a> 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 &quot;image_data = image_file[1].data&quot; because the image was in the HDU [1] instead of HDU [0].</p>
<p>I've had this exact problem when dealing with TESS images - I can't open them in the &quot;usual&quot; 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.</p>
<ol>
<li>Make a copy of the file and change the extension of the copied version to &quot;.txt&quot;</li>
<li>Open in a text editor like Notepad (I like Notepad++ better, but any text editor will work)</li>
<li>Turn on &quot;word wrap&quot; 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.</li>
<li>Adjust the width of your text editor to get close to 80 characters wide and you'll see the headers &quot;line up&quot; 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.</li>
<li>Look through the header info until you see the beginning of the first extension (probably named &quot;SCI&quot;).  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 &quot;MISSION= Hubble&quot; or something like that along with info about the pipeline that created it, etc.</li>
<li>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 &quot;SIMPLE= 1&quot; (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.</li>
<li>Save your changes and edit the extension to &quot;.fit&quot; or &quot;.fits&quot; and it should open fine.</li>
</ol>
<p>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.</p>
<p>To view fits files without having to edit the header, check out <a href="https://sites.google.com/cfa.harvard.edu/saoimageds9/home" rel="nofollow noreferrer">SAOimageDS9</a>.  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!</p>

CC BY-SA 4.0
2020-10-07T17:41:37.350Z
The python code on github you referenced is licensed under the MIT license, and it is archived here in case the github page goes down: https://archive.org/details/astropy_displayFITSfile-master
called2voyage
CC BY-SA 4.0
2020-08-14T20:28:27.207Z
2020-08-16T17:27:35.770Z
2020-08-16T17:27:35.770Z
AstroShannon
<p>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.</p>
<p>So what I need is to invert the order of headers. i.e, SCI header should come first.</p>
<p>When I run:</p>
<pre><code>from astropy.io import fits
image = 'image.fits'
fits.info(image)
</code></pre>
<p>I get</p>
<pre><code>No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     722   ()      
  1  SCI           1 ImageHDU       110   (1900, 1900)   float32
</code></pre>
<p>I already tried editing the first header, to add the needed info, but for some reason, it won't accept the changes.</p>

<hubble-telescope><fits-header><fits>
How to invert fits headers order (primary/secondary)?
Igor Kolesnikov
# How to invert fits headers order (primary/secondary)?

(Asked by: username_0 on 2020-08-14)

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'
fits.info(image)

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.

**Tags:** hubble-telescope, fits-header, fits


---

## 1 answers

### Answer by username_1 on 2020-08-14. Score: 3

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](https://github.com/MatheusJCastro/astropy_displayFITSfile/blob/master/open_fits.py) 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](https://sites.google.com/cfa.harvard.edu/saoimageds9/home). 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!

> **Comments:**
> **username_2**: The python code on github you referenced is licensed under the MIT license, and it is archived here in case the github page goes down: https://archive.org/details/astropy_displayFITSfile-master  

---

