# MultiHDU FITS Files

**URL:** <https://community.openastronomy.org/t/multihdu-fits-files/890>\
**Category:** Astropy\
**Created:** [February 1, 2024, 12:29am UTC](https://community.openastronomy.org/t/multihdu-fits-files/890 "2024-02-01T00:29:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RickW](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RickW](https://community.openastronomy.org/u/RickW)\
**Post date:** [February 1, 2024, 12:29am UTC](https://community.openastronomy.org/t/multihdu-fits-files/890/1 "2024-02-01T00:29:35Z")

</div>

For some years now I have been using astropy to reduce and combine my master reduction frames. Suddenly for the past month or so, with no changes to my programs, I it is producing FITS files with 3 HDUs. Looks like one is the uncertainty, and the other is a mask, all zeros. I have never used the uncertainty (yes, I will try to learn what to do with it) or the mask (eventually I will use it to mask hot/dead pixels?) How do I prevent their appearance in my FITS files?  
Thanks

---

<div class="post-metadata">

**Author:** ![hamogu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/hamogu/32/93_2.png) [@hamogu](https://community.openastronomy.org/u/hamogu)\
**Post date:** [February 1, 2024, 12:13pm UTC](https://community.openastronomy.org/t/multihdu-fits-files/890/2 "2024-02-01T12:13:05Z")

</div>

Can you elaborate a bit more on what “Suddenly […] with no changes to my programs” means? Clearly something changed… Did you upgrade either astropy or CCDproc or a similar package that you use?  
Can you post (or link to) an example of the code you use to write the files, so we can see what the input to your program is? It’s hard to find out what’s going on without seeing the code.

---

<div class="post-metadata">

**Author:** ![RickW](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RickW](https://community.openastronomy.org/u/RickW)\
**Post date:** [February 1, 2024, 11:24pm UTC](https://community.openastronomy.org/t/multihdu-fits-files/890/3 "2024-02-01T23:24:53Z")

</div>

Astropy and/or ccdproc have been updated on my system within the past month or so. It is my own programs which use astropy/ccdproc functions which have not changed. What I am looking for is a setting in ccdproc.combine() to prevent the two extra HDUs from being created. Or, second best would be a method to remove them before writing to disk.

Here is code that reproduces my problem:

> files = glob.glob(‘ASASSN-V\_J082218.33-001824.3\*.fit’)  
> stack = ccdp.combine(files, method=‘average’,  
> sigma\_clip=True, sigma\_clip\_low\_thresh=3, sigma\_clip\_high\_thresh=3,  
> sigma\_clip\_func=np.ma.median, sigma\_clip\_dev\_func=mad\_std,  
> mem\_limit=4e9)  
> stack.write(‘test.fit’, overwrite=True)

I get the following extra two HDUs in each combined image:

> HDU number = 2  
> XTENSION= 'IMAGE ’ / Image extension  
> BITPIX = 8 / array data type  
> NAXIS = 2 / number of array dimensions  
> NAXIS1 = 3326  
> NAXIS2 = 2504  
> PCOUNT = 0 / number of parameters  
> GCOUNT = 1 / number of groups  
> EXTNAME = 'MASK ’ / extension name  
> END
> 
> HDU number = 3  
> XTENSION= 'IMAGE ’ / Image extension  
> BITPIX = -64 / array data type  
> NAXIS = 2 / number of array dimensions  
> NAXIS1 = 3326  
> NAXIS2 = 2504  
> PCOUNT = 0 / number of parameters  
> GCOUNT = 1 / number of groups  
> UTYPE = ‘StdDevUncertainty’  
> EXTNAME = 'UNCERT ’ / extension name  
> END

---

<div class="post-metadata">

**Author:** ![hamogu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/hamogu/32/93_2.png) [@hamogu](https://community.openastronomy.org/u/hamogu)\
**Post date:** [February 2, 2024, 9:41pm UTC](https://community.openastronomy.org/t/multihdu-fits-files/890/4 "2024-02-02T21:41:43Z")

</div>

I did not run this code, because I don’t have CCD images handy to try it on, however, in general you can delete elements from a [HDUList](https://docs.astropy.org/en/stable/io/fits/api/hdulists.html#astropy.io.fits.HDUList), e.g. like so:

```auto
stack = ccdp.combine(files, method=‘average’,
sigma_clip=True, sigma_clip_low_thresh=3, sigma_clip_high_thresh=3,
sigma_clip_func=np.ma.median, sigma_clip_dev_func=mad_std,
mem_limit=4e9)
i_dont_want_this_1 = stack.pop()
i_dont_want_this_2 = stack.pop()
stack.write(‘test.fit’, overwrite=True)

```

---

<div class="post-metadata">

**Author:** ![dhomeier](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/dhomeier/32/65_2.png) [@dhomeier](https://community.openastronomy.org/u/dhomeier)\
**Post date:** [February 3, 2024, 9:42pm UTC](https://community.openastronomy.org/t/multihdu-fits-files/890/5 "2024-02-03T21:42:36Z")

</div>

I think this won’t work directly on `stack` as it is a `CCDData` object, not an `HDUList`, but it looks like you could also get rid of the extra HDUs sort of on the fly by changing the default parameters:

```python
stack.to_hdu(hdu_mask=None, hdu_uncertainty=None).writeto(‘test.fit’, overwrite=True)

```

---

<div class="post-metadata">

**Author:** ![RickW](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RickW](https://community.openastronomy.org/u/RickW)\
**Post date:** [February 9, 2024, 6:47pm UTC](https://community.openastronomy.org/t/multihdu-fits-files/890/6 "2024-02-09T18:47:07Z")

</div>

Thanks hamogu - I’m a little nervous about popping the hdus in case I pop the image one instead of one of the extras even though it worked when I tried it in a Jupyter notebook. But it’s good to know about this facility.  
Thanks dhomeier - this is exactly what I was looking for - prevent the extra hdus from being created in the first place.
