<p>This code snippet should do what you want for an individual fits file. It searches for the HDU containing 'DATE-BEG' and then writes a header 'DATE-OBS' containing the value of 'DATE-BEG'. You can use glob to search to get a list of all your fits files and run this code on all of them.</p>
<pre><code>from astropy.io import fits

file = 'insert path of fits file'
hdul = fits.open(file)
image_hdu_number = 0
for i in range(len(hdul)):
    if 'DATE-BEG' in hdul[i].header:
        image_hdu_number = i

date_beg = fits.getval(file, 'DATE-BEG', ext=image_hdu_number)
fits.setval(file, 'DATE-OBS', value=date_beg, ext=image_hdu_number)
</code></pre>

CC BY-SA 4.0
2022-06-27T09:47:38.777Z
if the batch is all in the same folder then`files = glob.glob('d*.txt')` and `for file in files:` could work, with a `try` and `except` to avoid fatal crashing for simple errors.
uhoh
CC BY-SA 4.0
2022-06-27T17:57:27.503Z
Can't tell from the above example header (a `hdul = fits.open(fits_file) ; print(hdul[0].header)` would get you a more readable version) but you should check whether there is an `EXPTIME` or `EXPOSURE` in the header as well when converting `DATE-BEG` to `DATE-OBS` so AstroImageJ can correct the time to the midpoint of the exposure (which is what you really want). Otherwise you will want to modify the above answer's code to insert this also.
astrosnapper
CC BY-SA 4.0
2022-06-28T05:41:59.423Z
Wow thanks, folks, Astro snapper, and Uhoh you were all right on with that. I solved it. ...but...although my header already contains an EXPTIME ...AIJ is still throwing me an error at the end saying it "can't calculate exposure time and beginning of the exposure."    I'll post my interim solution. and go from there. Not 100% sure how to do that.  I'll post two.
strange_octopi
CC BY-SA 4.0
2022-06-27T08:43:54.770Z
2022-06-27T08:43:54.770Z
antoine
<p>I would be so grateful if someone could help me. I am stuck for over a week now trying to find some way to use Astropy to write some small code to edit one keyword in batches of FITS file headers so that they can be used in AstroimageJ(AIJ) for a summer undergrad research project. Thank you so much for any help. AstroimageJ wants DATE-OBS (for example) but my files say DATE-BEG, which is not an accepted KEYWORD for the start of exposure date/time. <a href="https://i.sstatic.net/v2mSb.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/v2mSb.jpg" alt="AstroimageJ says must call exposure start as DATE-OBS" /></a>  <a href="https://i.sstatic.net/0BUvL.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0BUvL.jpg" alt="DATE-BEG" /></a> Thank you so much for any help. It's very appreciated. I've been stuck for a week now. Above is a screenshot of the AIJ documentation. Followed by a screenshot of one my .FITS file headers that I accessed just by renaming it as a .TXT file, and then I can edit it by hand. But I have thousands of files to edit. Thank you!</p>

CC BY-SA 4.0
2022-06-26T02:50:08.283Z
*Welcome to Stack Exchange!* Usually use of screenshots of text is discouraged, and text capture is recommended, but in this particular case that's going to be pretty difficult. I think the title of your question alone may be good enough for an answer, but by explaining so much more others may be able to provide a better or at least different solution as well.
uhoh
CC BY-SA 4.0
2022-06-27T03:04:31.060Z
OK thank you for the "heads" up. Pun intended. 
strange_octopi
CC BY-SA 4.0
2022-06-28T05:58:04.223Z
Wow, thank you so much! Everyone. Including the very last post! Still not 100% sure what to do about the EXPTIME not being picked up by AIJ, it's telling me that at the end. The answers question guidelines say to "share your research" so here goes: This is a Hot Jupiter orbital decay project! I'm guessing the WASP-12b object name in the header file gave that away already! Thanks again! This may not be the last I post on this and I am so appreciative.
strange_octopi
CC BY-SA 4.0
2022-06-29T01:28:47.853Z
Thank you everyone so much! I ran this bit of code, on the science, flat and bias and it did work.. AIJ accepted it.                                                                                                                                                      #science, bias, and flats
from astropy.io import fits
import glob
files = glob.glob('/file_path/*.fits')
for ifile in files:
    data,header = fits.getdata(ifile,header=True)
    datebeg = header['DATE-BEG']
    header['DATE-OBS'] = datebeg
    fits.writeto(ifile,data,header,overwrite=True)
    #header.remove(‘DATE-BEG’)
strange_octopi
<data-analysis><python><astropy><fits-header>
How to use Astropy to edit a keyword (not value) in batches of FITS file headers to be used in AstroimageJ (AIJ)? (summer undergrad research project)
strange_octopi
# How to use Astropy to edit a keyword (not value) in batches of FITS file headers to be used in AstroimageJ (AIJ)? (summer undergrad research project)

(Asked by: username_0 on 2022-06-25)

I would be so grateful if someone could help me. I am stuck for over a week now trying to find some way to use Astropy to write some small code to edit one keyword in batches of FITS file headers so that they can be used in AstroimageJ(AIJ) for a summer undergrad research project. Thank you so much for any help. AstroimageJ wants DATE-OBS (for example) but my files say DATE-BEG, which is not an accepted KEYWORD for the start of exposure date/time. [![AstroimageJ says must call exposure start as DATE-OBS](https://i.sstatic.net/v2mSb.jpg)](https://i.sstatic.net/v2mSb.jpg) [![DATE-BEG](https://i.sstatic.net/0BUvL.jpg)](https://i.sstatic.net/0BUvL.jpg) Thank you so much for any help. It's very appreciated. I've been stuck for a week now. Above is a screenshot of the AIJ documentation. Followed by a screenshot of one my .FITS file headers that I accessed just by renaming it as a .TXT file, and then I can edit it by hand. But I have thousands of files to edit. Thank you!

**Tags:** data-analysis, python, astropy, fits-header

> **Comments:**
> **username_1**: *Welcome to Stack Exchange!* Usually use of screenshots of text is discouraged, and text capture is recommended, but in this particular case that's going to be pretty difficult. I think the title of your question alone may be good enough for an answer, but by explaining so much more others may be able to provide a better or at least different solution as well.  
> **username_0**: OK thank you for the "heads" up. Pun intended.   
> **username_0**: Wow, thank you so much! Everyone. Including the very last post! Still not 100% sure what to do about the EXPTIME not being picked up by AIJ, it's telling me that at the end. The answers question guidelines say to "share your research" so here goes: This is a Hot Jupiter orbital decay project! I'm guessing the WASP-12b object name in the header file gave that away already! Thanks again! This may not be the last I post on this and I am so appreciative.  
> **username_0**: Thank you everyone so much! I ran this bit of code, on the science, flat and bias and it did work.. AIJ accepted it.                                                                                                                                                      #science, bias, and flats from astropy.io import fits import glob files = glob.glob('/file_path/*.fits') for ifile in files:     data,header = fits.getdata(ifile,header=True)     datebeg = header['DATE-BEG']     header['DATE-OBS'] = datebeg     fits.writeto(ifile,data,header,overwrite=True)     #header.remove(‘DATE-BEG’)  

---

## 1 answers

### Answer by username_2 on 2022-06-27. Score: 2

This code snippet should do what you want for an individual fits file. It searches for the HDU containing 'DATE-BEG' and then writes a header 'DATE-OBS' containing the value of 'DATE-BEG'. You can use glob to search to get a list of all your fits files and run this code on all of them.
from astropy.io import fits

file = 'insert path of fits file'
hdul = fits.open(file)
image_hdu_number = 0
for i in range(len(hdul)):
    if 'DATE-BEG' in hdul[i].header:
        image_hdu_number = i

date_beg = fits.getval(file, 'DATE-BEG', ext=image_hdu_number)
fits.setval(file, 'DATE-OBS', value=date_beg, ext=image_hdu_number)

> **Comments:**
> **username_1**: if the batch is all in the same folder then`files = glob.glob('d*.txt')` and `for file in files:` could work, with a `try` and `except` to avoid fatal crashing for simple errors.  
> **username_3**: Can't tell from the above example header (a `hdul = fits.open(fits_file) ; print(hdul[0].header)` would get you a more readable version) but you should check whether there is an `EXPTIME` or `EXPOSURE` in the header as well when converting `DATE-BEG` to `DATE-OBS` so AstroImageJ can correct the time to the midpoint of the exposure (which is what you really want). Otherwise you will want to modify the above answer's code to insert this also.  
> **username_0**: Wow thanks, folks, Astro snapper, and Uhoh you were all right on with that. I solved it. ...but...although my header already contains an EXPTIME ...AIJ is still throwing me an error at the end saying it "can't calculate exposure time and beginning of the exposure."    I'll post my interim solution. and go from there. Not 100% sure how to do that.  I'll post two.  

---

