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
CASA 4.7 was just released and it appears to produce invalid FITS headers, at least sometimes:
ValueError: FITS header values must contain standard printable ASCII characters; "'CASA 4.7.0-REL (r38335)\n'" contains characters not representable in ASCII or non-printable characters.
There is already a bug report upstream to fix this, but is there a workaround for the files that are created by this rogue version? These files will fail whenever the header is accessed or written.
Note that fh.verify('fix') results in the same traceback.
Header origin: "CASA 4.7.0-REL (r38335)"
Traceback (most recent call last):
File "<ipython-input-72-2c3f12ce28f0>", line 16, in <module>
wcs.WCS(fh[0].header)
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/wcs/wcs.py", line 391, in __init__
try:
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/header.py", line 639, in tostring
s += sep + _pad('END')
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/card.py", line 461, in __str__
return self.image
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/card.py", line 765, in image
self.verify('fix')
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/verify.py", line 74, in verify
errs = self._verify(opt)
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/card.py", line 1392, in _verify
fix=self._fix_value))
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/verify.py", line 48, in run_option
fix()
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/card.py", line 1131, in _fix_value
self.value = valuecomment
File "/users/aginsbur/anaconda/lib/python3.5/site-packages/astropy/io/fits/card.py", line 582, in value
'ASCII or non-printable characters.' % value)
ValueError: FITS header values must contain standard printable ASCII characters; "'CASA 4.7.0-REL (r38335)\n'" contains characters not representable in ASCII or non-printable characters.
How is WCS accessing some older version of the header? It's as if the header isn't being updated.
This has some heisenbug elements to it. If I run this as a block of code (i.e., with %cpaste), I get the error. If I run each command in turn (just paste the whole thing on the command line), it doesn't happen. Maybe it's something peculiar ipython is doing. The overall issue remains important, though.
@keflavich I have tried to reproduce your example from #5396 (comment) (using an STScI image modified to contain \n in the ORIGIN string value) and I cannot reproduce your crash: WCS can be successfully constructed from a "corrected" header. Are you sure you are not attempting to create the WCS by providing the file name instead of the header to the WCS constructor? It looks to me that you are reloading the old file...
In [56]: h=fits.open('test1n.fits')
In [57]: w=wcs.WCS(h[0].header)
---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyin_fix_value(self)
1128self.value=value.strip()
->1129self.comment=comment.strip()
1130except (ValueError, IndexError):
/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyincomment(self, comment)
703'representable in ASCII or non-printable characters.'%-->704comment)
705ValueError: FITSheadercommentsmustcontainstandardprintableASCIIcharacters; "CFITSIO March 2010\n'"containscharactersnotrepresentableinASCIIornon-printablecharacters.
Duringhandlingoftheaboveexception, anotherexceptionoccurred:
ValueErrorTraceback (mostrecentcalllast)
<ipython-input-57-01ff27fbd8dc>in<module>()
---->1w=wcs.WCS(h[0].header)
/Users/.../lib/python3.4/site-packages/astropy/wcs/wcs.pyin__init__(self, header, fobj, key, minerr, relax, naxis, keysel, colsel, fix, translate_units, _do_set)
401402ifisinstance(header, fits.Header):
-->403header_string=header.tostring().rstrip()
404else:
405header_string=header/Users/.../lib/python3.4/site-packages/astropy/io/fits/header.pyintostring(self, sep, endcard, padding)
640lines= []
641forcardinself._cards:
-->642s=str(card)
643# Cards with CONTINUE cards may be longer than 80 chars; so break644# them into multiple lines/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyin__str__(self)
459460def__str__(self):
-->461returnself.image462463def__len__(self):
/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyinimage(self)
763764ifself._imageandnotself._verified:
-->765self.verify('fix')
766ifself._imageisNoneorself._modified:
767self._image=self._format_image()
/Users/.../lib/python3.4/site-packages/astropy/io/fits/verify.pyinverify(self, option)
72return73--->74errs=self._verify(opt)
7576# Break the verify option into separate options related to reporting of/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyin_verify(self, option)
1390'string: %r).'% (self.keyword, valuecomment),
1391fix_text=fix_text,
->1392fix=self._fix_value))
13931394# verify the comment (string), it is never fixable/Users/.../lib/python3.4/site-packages/astropy/io/fits/verify.pyinrun_option(self, option, err_text, fix_text, fix, fixable)
46else:
47iffix:
--->48fix()
49text+=' '+fix_text50/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyin_fix_value(self)
1129self.comment=comment.strip()
1130except (ValueError, IndexError):
->1131self.value=valuecomment1132self._valuestring=self._value1133return/Users/.../lib/python3.4/site-packages/astropy/io/fits/card.pyinvalue(self, value)
580'FITS header values must contain standard printable ASCII '581'characters; %r contains characters not representable in '-->582'ASCII or non-printable characters.'%value)
583elifisinstance(value, binary_type):
584# Allow str, but only if they can be decoded to ASCII text; noteValueError: FITSheadervaluesmustcontainstandardprintableASCIIcharacters; "'HSTIO/CFITSIO March 2010\n'"containscharactersnotrepresentableinASCIIornon-printablecharacters.
In [58]: h[0].header['origin']='goodval'In [59]: w=wcs.WCS(h[0].header)
In [60]: h.verify()
In [61]:
@mcara - in your example, you get a traceback the first time you access the header, but not the second. I get the same, even if I don't correct the header. i.e.:
fh = fits.open(fn)
fh[0].header.tostring() # produces exception
fh[0].header.tostring() # does not produce exception
If in my previous example I do not attempt to create a WCS immediately after opening FITS file, then attempting to create a WCS after correcting the header still crashes (as you have reported):
... On the other hand, if I use tostring() instead of attempting to create a WCS, I get the following:
>>>fromastropy.ioimportfits>>>fromastropyimportwcs>>>h=fits.open('test1n.fits')
>>>h[0].header['origin'] ='goodval'>>>h[0].header.tostring()
WARNING: VerifyWarning: Verificationreportederrors: [astropy.io.fits.verify]
WARNING: VerifyWarning: Card'ORIGIN'isnotFITSstandard (invalidvaluestring: "'HSTIO/CFITSIO March 2010\n'"). Fixed'ORIGIN'cardtomeettheFITSstandard. [astropy.io.fits.verify]
WARNING: VerifyWarning: Note: astropy.io.fitsuseszero-basedindexing.
[astropy.io.fits.verify]
"SIMPLE = T / conforms to FITS standard BITPIX = -32 / array data type NAXIS = 2 / number of array dimensions NAXIS1 = 2061 NAXIS2 = 1921 COMMENT FITS (Flexible Image Transport System) format is defined in 'AstronomyCOMMENT and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H ORIGIN = '''HSTIO ' / CFITSIO March 2010\n' DATE = '2014-09
(scroll to the right to see 'ORIGIN'). So, it seems that h[0].header['origin'] = 'goodval' had no effect (but it does work if run twice)!
Interesting! If I do the same, I get a crash on tostring the first time I call it, but the second time it shows that the keyword was successfully changed:
>>> fh = fits.open(fn)
>>> fh[0].header['ORIGIN']='goodval'
# exception:
# ValueError: FITS header values must contain standard printable ASCII characters; "'CASA 4.7.0-REL (r38335)\n'" contains characters not representable in ASCII or non-printable characters.
>>> fh[0].header.tostring()
# ... snip ... ORIGIN = \'goodval \' ... snip ...
The upstream fix has been implemented, so most likely we will not hear much about this specific error - we caught it early enough that probably no data have been distributed with the bad headers. However, a preferred workaround would still be helpful to have here.
Activity
MSeifert04 commentedon Oct 6, 2016
It would be interesting to know which unprintable characters. Could you share one "corrupted" file?
keflavich commentedon Oct 6, 2016
@MSeifert04 it's a newline at the end of the string: the
\nin the above examplekeflavich commentedon Oct 7, 2016
This problem has gotten a bit worse for me. I tried the obvious workaround of editing the HDU inplace:
but it results in the same traceback:
How is WCS accessing some older version of the header? It's as if the header isn't being updated.
pllim commentedon Oct 7, 2016
Try
mode=updateor... make a copy of that header, modify the copy, and pass in the modified copy to WCS. Does either of those work?keflavich commentedon Oct 7, 2016
@pllim Thanks for the ideas. Copy fails (same). It actually shows that WCS is not accessing the same header... which I don't understand at all.
same problem with
fh=fits.open(fn, mode='update')EDIT: this works:
Is the WCS call triggering some sort of flush action?
keflavich commentedon Oct 7, 2016
This has some heisenbug elements to it. If I run this as a block of code (i.e., with
%cpaste), I get the error. If I run each command in turn (just paste the whole thing on the command line), it doesn't happen. Maybe it's something peculiar ipython is doing. The overall issue remains important, though.mcara commentedon Oct 8, 2016
@keflavich Is there a way to get your test image?
mcara commentedon Oct 8, 2016
@keflavich I have tried to reproduce your example from #5396 (comment) (using an STScI image modified to contain
\nin theORIGINstring value) and I cannot reproduce your crash: WCS can be successfully constructed from a "corrected" header. Are you sure you are not attempting to create the WCS by providing the file name instead of the header to the WCS constructor? It looks to me that you are reloading the old file...keflavich commentedon Oct 10, 2016
@mcara - in your example, you get a traceback the first time you access the header, but not the second. I get the same, even if I don't correct the header. i.e.:
Can you reproduce that behavior?
mcara commentedon Oct 10, 2016
@keflavich Yes, I can reproduce this behavior.
mcara commentedon Oct 10, 2016
If in my previous example I do not attempt to create a WCS immediately after opening FITS file, then attempting to create a WCS after correcting the header still crashes (as you have reported):
This is exciting! 😄
mcara commentedon Oct 10, 2016
... On the other hand, if I use
tostring()instead of attempting to create a WCS, I get the following:(scroll to the right to see
'ORIGIN'). So, it seems thath[0].header['origin'] = 'goodval'had no effect (but it does work if run twice)!keflavich commentedon Oct 10, 2016
Interesting! If I do the same, I get a crash on
tostringthe first time I call it, but the second time it shows that the keyword was successfully changed:mcara commentedon Oct 10, 2016
Well, I think this has to be a bug in
fits.iobut it would probably be better to open a new issue as this is independent of CASA's bug.keflavich commentedon Oct 10, 2016
Hmm, right. There are two independent issues here. This issue is about how best to use files that have illegal header keywords.
keflavich commentedon Oct 14, 2016
The upstream fix has been implemented, so most likely we will not hear much about this specific error - we caught it early enough that probably no data have been distributed with the bad headers. However, a preferred workaround would still be helpful to have here.
mcara commentedon Oct 14, 2016
I am still worried that one cannot change header values right away - it seems that header is re-loading old values from somewhere...
keflavich commentedon Oct 14, 2016
@mcara: agreed. I opened the new issue here now: #5408
bsipocz commentedon Jun 19, 2019
This seems to be fixed by #6084 based on #6084 (comment).
Please feel free to reopen if I misunderstood and it didn't get fixed.