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 DATE-BEG 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!

  • $\begingroup$ 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. $\endgroup$
    – uhoh
    Commented Jun 26, 2022 at 2:50
  • 1
    $\begingroup$ OK thank you for the "heads" up. Pun intended. 😉 $\endgroup$ Commented Jun 27, 2022 at 3:04
  • $\begingroup$ 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. $\endgroup$ Commented Jun 28, 2022 at 5:58
  • $\begingroup$ 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’) $\endgroup$ Commented Jun 29, 2022 at 1:28

1 Answer 1


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)
  • $\begingroup$ if the batch is all in the same folder thenfiles = glob.glob('d*.txt') and for file in files: could work, with a try and except to avoid fatal crashing for simple errors. $\endgroup$
    – uhoh
    Commented Jun 27, 2022 at 9:47
  • $\begingroup$ 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. $\endgroup$ Commented Jun 27, 2022 at 17:57
  • $\begingroup$ 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. $\endgroup$ Commented Jun 28, 2022 at 5:41

You must log in to answer this question.

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