You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an issue noted on #5396, but is independent of that issue.
In short, given a FITS header with an invalid entry (e.g., a newline within the 'ORIGIN' value), the following will happen:
fh = fits.open(fn)
fh[0].header.tostring() # produces exception
fh[0].header.tostring() # does not produce exception
>>> h = fits.open('test1n.fits')
>>> h[0].header['origin'] = 'goodval'
>>> w = wcs.WCS(h[0].header) # <- crashes
# a repeat attempt here would (probably) not crash
No idea how to fix this, but thanks for the workaround @keflavich -- it just saved me having to reprocess several thousand images from a couple years ago.
That is indeed the fix. I had several hundred images with newline characters in the header, fixed them with this snippet (your workaround is in the last few lines):
In [1]: %astropyNumpy1.12.1Astropy2.0.dev18422In [2]: c=fits.Card.fromstring('FOO = BAR\n ')
In [3]: c.value---------------------------------------------------------------------------VerifyErrorTraceback (mostrecentcalllast)
<ipython-input-3-d335c3962011>in<module>()
---->1c.value/home/simon/dev/astropy/astropy/io/fits/card.pyinvalue(self)
279value=self._value280elifself._valuestringisnotNoneorself._image:
-->281self._value=self._parse_value()
282value=self._value283else:
/home/simon/dev/astropy/astropy/io/fits/card.pyin_parse_value(self)
738ifmisNone:
739raiseVerifyError("Unparsable card ({}), fix it first with "-->740".verify('fix').".format(self.keyword))
741742ifm.group('bool') isnotNone:
VerifyError: Unparsablecard (FOO), fixitfirstwith .verify('fix').
In [4]: c.value='FIXED'In [5]: c.valueOut[5]: 'FIXED'In [6]: cOut[6]: ('FOO', 'FIXED', '')
In [7]: c.verify('fix')
WARNING: VerifyWarning: Verificationreportederrors: [astropy.io.fits.verify]
WARNING: VerifyWarning: Card'FOO'isnotFITSstandard (invalidvaluestring: 'BAR'). Fixed'FOO'cardtomeettheFITSstandard. [astropy.io.fits.verify]
WARNING: VerifyWarning: Note: astropy.io.fitsuseszero-basedindexing.
[astropy.io.fits.verify]
In [8]: c.valueOut[8]: 'BAR'
It looks like the private Card._image is not updated when setting a new value, and this attribute is used at least when verifying the card which brings back the old value ! 😨
I was able to reproduce this much in the same way as @yannick1974 by creating a FITS header containing an invalid card value (the literal INF in this case).
If I open the file, call header.tostring()first (which implicitly verifies and fixes the broken card--another aspect of FITS verification that's a bit weird and broken), then manually update the value and write it out, it works:
>>>f=fits.open('bogus.fits')
>>>f[0].header.tostring()
WARNING: VerifyWarning: Verificationreportederrors: [astropy.io.fits.verify]
WARNING: VerifyWarning: Card'KW'isnotFITSstandard (invalidvaluestring: 'INF'). Fixed'KW'cardtomeettheFITSstandard. [astropy.io.fits.verify]
WARNING: VerifyWarning: Note: astropy.io.fitsuseszero-basedindexing.
[astropy.io.fits.verify]
"SIMPLE = T / conforms to FITS standard BITPIX = 8 / array data type NAXIS = 0 / number of array dimensions EXTEND = T KW = 'INF ' END ">>>f[0].header['KW'] =-1>>>f.writeto('bogus_fixed.fits', overwrite=True)
>>>fits.getheader('bogus_fixed.fits')
SIMPLE=T/conformstoFITSstandardBITPIX=8/arraydatatypeNAXIS=0/numberofarraydimensionsEXTEND=TKW=-1
But if I just open the file, try to update the value of KW before the the header has been fully parsed/verified, and then try to write it out, it doesn't take the update and produces an exception:
Activity
mwcraig commentedon Nov 15, 2016
No idea how to fix this, but thanks for the workaround @keflavich -- it just saved me having to reprocess several thousand images from a couple years ago.
keflavich commentedon Feb 9, 2017
For clarification, the workaround is to do exactly as above, but with a try/except around it
(@mwcraig - let me know if I'm misremembering and there was some other component that is the "actual" workaround)
mwcraig commentedon Feb 10, 2017
That is indeed the fix. I had several hundred images with newline characters in the header, fixed them with this snippet (your workaround is in the last few lines):
saimn commentedon May 22, 2017
Just to confirm that there is clearly an issue :
It looks like the private
Card._imageis not updated when setting a new value, and this attribute is used at least when verifying the card which brings back the old value ! 😨embray commentedon Nov 30, 2020
I was able to reproduce this much in the same way as @yannick1974 by creating a FITS header containing an invalid card value (the literal
INFin this case).If I open the file, call
header.tostring()first (which implicitly verifies and fixes the broken card--another aspect of FITS verification that's a bit weird and broken), then manually update the value and write it out, it works:But if I just open the file, try to update the value of
KWbefore the the header has been fully parsed/verified, and then try to write it out, it doesn't take the update and produces an exception:[astropy#5408] Some refactoring of Card parsing
[astropy#5408] Add regression test and fix for astropy#5408
[astropy#5408] Some refactoring of Card parsing
[astropy#5408] Add regression test and fix for astropy#5408
[astropy#5408] Add regression test and fix for astropy#5408
[astropy#5408] Some refactoring of Card parsing
1 remaining item
[astropy#5408] Add changelog entry. [skip ci]
Merge pull request #11108 from embray/issue-5408
[astropy#5408] Some refactoring of Card parsing
[astropy#5408] Add regression test and fix for astropy#5408
[astropy#5408] Add changelog entry. [skip ci]
[astropy#5408] Some refactoring of Card parsing
[astropy#5408] Add regression test and fix for astropy#5408
[astropy#5408] Add changelog entry. [skip ci]
[astropy#5408] Some refactoring of Card parsing
[astropy#5408] Add regression test and fix for astropy#5408
[astropy#5408] Add changelog entry. [skip ci]
Merge pull request #11108 from embray/issue-5408
Merge pull request astropy#11108 from embray/issue-5408
Merge pull request astropy#11108 from embray/issue-5408