Skip to content

ValueError: Illegal value: [astropy.io.fits.card] related to fits header comment card #3603

Open

Description

@wadawson

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

  1. embray commented on Mar 18, 2015

    @embray
    Member

    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.

  2. embray commented on Mar 18, 2015

    @embray
    Member

    I get the following error when I try to read in a fits file

    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?

  3. wadawson commented on Mar 18, 2015

    @wadawson
    Author

    @embray

    What do you mean here by "read"?

    "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.

    What the steps that lead to this warning being displayed?

        # read in the fits files and their headers
        hdulist1 = pyfits.open(fits1)
        # parse the WCS keywords in the primary HDU
        wcs1 = astropy.wcs.WCS(hdulist1[0].header)
    

    Is this on Python 3 by any chance?

    No it is Python 2.7.

  4. embray commented on Mar 18, 2015

    @embray
    Member

    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.

  5. embray commented on Mar 18, 2015

    @embray
    Member

    Yeah, I can't reproduce this with an files I make locally. Could you attach a sample file?

  6. embray commented on Mar 18, 2015

    @embray
    Member

    Also, just to be sure, you're not using pyfits simultaneously with astropy, right?

  7. wadawson commented on Mar 18, 2015

    @wadawson
    Author

    Also, just to be sure, you're not using pyfits simultaneously with astropy, right?

    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.

  8. embray commented on Mar 18, 2015

    @embray
    Member

    astropy.io.fits is pyfits, just under a different name. Eventually I'm going to be releasing a version of pyfits that just aliases astropy.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)

  9. embray commented on Mar 18, 2015

    @embray
    Member

    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 Header objects, 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...

  10. embray commented on Mar 18, 2015

    @embray
    Member

    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.

  11. self-assigned this
    on Mar 18, 2015
  12. wadawson commented on Mar 18, 2015

    @wadawson
    Author

    Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions