[AstroPy] verify behavior

Joe Harrington jh at physics.ucf.edu
Tue Oct 31 16:38:45 EDT 2017


I have some non-compliant FITS images from an old instrument that I use
in my class (why? because the data are perfect for an example of median
combination, and the strange format and non-compliance help me teach
writing wrapper functions).  I had thought that doing

hdulist.verify('silentfix')

would silently fix the broken cards.  However, it doesn't.  It only
seems to set the option for how to do it later, should the need arise.
For example, the cards get fixed if printed.  Otherwise, when trying to
write the file, it errors out.  It's not a solution to write with
'silentfix', because I want to fix the data on read so the user doesn't
know it was ever bad.

So, my goal is to hide the weirdness of this instrument's data inside a
wrapper script, which means I need to print the header for each image as
it's read, which is not desired.  So, my hack is to open /dev/null
(portably) and print it to that.

Is this the best option, or is it a bug and hdulist.verify() should
actually DO the fix, or is there something else I can run to make it do
the fix on or right after the read that is cleaner?

The example script is below, set up to comment in and out various
lines to try stuff.  An example FITS(ish) file is here:

https://physics.ucf.edu/~jh/dark_13s_5.fits

--jh--

#! /usr/bin/env python3

# Astropy verify bug test case.
# Joseph Harrington <jh at physics.ucf.edu> 31 October 2017

# Note that floats header entries have lowercase 'e' in file, but they
# have uppercase 'E' in astropy FITS header object.  It gets fixed
# automatically.  However, it isn't really fixed.
# Note: CDELT1, CDELT2, RA_OFFS, DEC_OFFS, RA_RATE, DEC_RATE.

# Comment in and out the lines below.

# BAD cases:
# Simple read and write: prints errors, nothing written.
# read, silentfix, write: prints errors, nothing written.
# read, fix, write: prints warnings, prints errors, nothing written.

# NOT GREAT cases (can't hide fixes in reader function):
# read, print, write: prints warnings, writes good data.
# read and write with 'silentfix': writes good data

# GOOD cases:
# use hdulist.verify('silentfix') to set the kind of fixing option
#    print the header to /dev/null or equivalent to fix the header
#    write

import numpy as np
import astropy.io.fits as fits

import os

infile   = 'dark_13s_5.fits'
outfile  = 'writefix.fits'
hdulist  = fits.open(infile)
hdulist.verify('silentfix')
header = hdulist[0].header
data   = hdulist[0].data
#print("Printing header to /dev/null or OS equivalent")
with open(os.devnull, 'w') as null:
  print(header, file=null)

print("Writing file "+outfile)
fits.writeto(outfile, data, header, overwrite=True)
# This is NOT a solution, as user shouldn't know about problem.
#fits.writeto(outfile, data, header, overwrite=True, output_verify='silentfix')


More information about the AstroPy mailing list