Repository navigation
ValueError: Illegal value: [astropy.io.fits.card] related to fits header comment card #3603
Description
I get the following error when I try to read in a fits file created with pyfits that contains a simple header comment card.
In previous versions of astropy (sometime before v1.0) I know it could read in such files without a problem. Perhaps older versions had fewer checks and it just skipped past whatever was tripping it
ERROR: ValueError: Illegal value: A simple test comment. [astropy.io.fits.card]
ERROR:astropy:ValueError: Illegal value: A simple test comment.
The fits file was created with pyfits. Here is the relevant code:
hdu = pyfits.PrimaryHDU(numpy.float32(H))
hdu.header.add_comment('A simple test comment')
hdu.writeto(prefix+'.fits',clobber=True)
Note that astropy.wcs.WCS() apparently has no problem reading in all of the cards before this comment card, which is the last. See:
In [254]:
htemp = pyfits.open('1RXSJ0603_redsequence_LuminosityDensity.fits')
htemp[0].header
Out[254]:
SIMPLE = T / conforms to FITS standard
BITPIX = -32 / array data type
NAXIS = 2 / number of array dimensions
NAXIS1 = 401
NAXIS2 = 400
EXTEND = T
CTYPE1 = 'RA---TAN'
CTYPE2 = 'DEC--TAN'
CRVAL1 = 90.83
CRVAL2 = 42.227222
CRPIX1 = 200.0
CRPIX2 = 200.0
CD1_1 = 0.001249989509154433
CD1_2 = 0
CD2_1 = 0
CD2_2 = 0.00125
COMMENT A simple test comment
and if I delete the comment card then everything is fine.
I don't think that this is a problem related to some change in pyfits. I encounter a similar error when I try to read in an older fits that I used to be able to handle with astropy.wcs.WCS().
I am not sure if it is related to the following warning:
In[] wcs1 = wcs.WCS(hdulist1[0].header)
WARNING: AstropyDeprecationWarning: Config parameter 'enabled_record_valued_keyword_cards' in section [io.fits] of the file '/Users/dawson/.astropy/config/astropy.cfg' is deprecated. Use 'enable_record_valued_keyword_cards' in section [config.configuration] instead. [astropy.config.configuration]
WARNING:astropy:AstropyDeprecationWarning: Config parameter 'enabled_record_valued_keyword_cards' in section [io.fits] of the file '/Users/dawson/.astropy/config/astropy.cfg' is deprecated. Use 'enable_record_valued_keyword_cards' in section [config.configuration] instead.
which seems related to [#2772].
Oh, and I don't think this issue is related to [#3497] since it doesn't seem like I have any non printable ASCII characters in the comment line.
Activity
embray commentedon Mar 18, 2015
I don't think the config warning is related. You can just make the change in the config file that it suggests and the warning should go away.
embray commentedon Mar 18, 2015
What do you mean here by "read"? What the steps that lead to this warning being displayed? It's pretty weird because I don't see how that could possibly happen (though clearly it is). There's certainly nothing wrong with the value.
Is this on Python 3 by any chance?
wadawson commentedon Mar 18, 2015
@embray
"read" was perhaps the wrong verb. I said it because the error is associated with astropy.io.fits.card, thus I interpreted this as being related to astropy reading (or rather parsing) the fits file and header.
No it is Python 2.7.
embray commentedon Mar 18, 2015
Weird. The code where that error message arises is only when setting values on a header. I don't know why WCS would be setting values, much less "illegal" ones.
embray commentedon Mar 18, 2015
Yeah, I can't reproduce this with an files I make locally. Could you attach a sample file?
embray commentedon Mar 18, 2015
Also, just to be sure, you're not using pyfits simultaneously with astropy, right?
wadawson commentedon Mar 18, 2015
Wow that is an unfortunate issue.
As noted earlier I was using pyfits.open() to open the fits file and then astropy.wcs.WCS(). When I switched to using astropy.io.fits() to open the fits file the problem was resolved.
In my humble opinion, this is something that should be resolved since pyfits is such a standard program. But since I don't want to do it I will understand if no one else does either.
embray commentedon Mar 18, 2015
astropy.io.fitsis pyfits, just under a different name. Eventually I'm going to be releasing a version of pyfits that just aliasesastropy.io.fits. In the meantime, there's no reason to mix them and it only leads to confusion (although in this case I believe the WCS module tries to resolve this, and it's still weird that this exception is happening--especially on Python 2)embray commentedon Mar 18, 2015
Interesting--I see why this happens now. Since you passed in a pyfits Header object it gets to this code:
https://github.com/astropy/astropy/blob/master/astropy/wcs/wcs.py#L381
With FITS
Headerobjects, when you read the value of a'COMMENT'card it actually returns the value of all'COMMENT'cards combined into a single newline-separated string, which is generally convenient. But then when it tries to store that back into the new header this occurs because newlines aren't valid in header values.I feel like I disallowed that deliberately even for commentary keywords like COMMENT. But I feel like it should allow that, and just write multiple COMMENT cards...
embray commentedon Mar 18, 2015
I see--over in this issue I made the suggestion of allowing a commentary card value containing newlines to be split into multiple cards. It seems like I didn't rule that out for any reason, but I didn't implement it either, which led to a slight regression.
So definitely don't mix pyfits and astropy anyways, but I'll see if I can also fix this issue.
wadawson commentedon Mar 18, 2015
Thank you!