question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FITS headers don't update immediately?

See original GitHub issue

This is an issue noted on https://github.com/astropy/astropy/issues/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

see: https://github.com/astropy/astropy/issues/5396#issuecomment-252687514, https://github.com/astropy/astropy/issues/5396#issuecomment-252699025, https://github.com/astropy/astropy/issues/5396#issuecomment-252700815, https://github.com/astropy/astropy/issues/5396#issuecomment-252701891

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
saimncommented, May 22, 2017

Just to confirm that there is clearly an issue :

In [1]: %astropy
Numpy 1.12.1
Astropy 2.0.dev18422

In [2]: c = fits.Card.fromstring('FOO     = BAR\n   ')

In [3]: c.value
---------------------------------------------------------------------------
VerifyError                               Traceback (most recent call last)
<ipython-input-3-d335c3962011> in <module>()
----> 1 c.value

/home/simon/dev/astropy/astropy/io/fits/card.py in value(self)
    279             value = self._value
    280         elif self._valuestring is not None or self._image:
--> 281             self._value = self._parse_value()
    282             value = self._value
    283         else:

/home/simon/dev/astropy/astropy/io/fits/card.py in _parse_value(self)
    738         if m is None:
    739             raise VerifyError("Unparsable card ({}), fix it first with "
--> 740                               ".verify('fix').".format(self.keyword))
    741 
    742         if m.group('bool') is not None:

VerifyError: Unparsable card (FOO), fix it first with .verify('fix').

In [4]: c.value = 'FIXED'

In [5]: c.value
Out[5]: 'FIXED'

In [6]: c
Out[6]: ('FOO', 'FIXED', '')

In [7]: c.verify('fix')
WARNING: VerifyWarning: Verification reported errors: [astropy.io.fits.verify]
WARNING: VerifyWarning: Card 'FOO' is not FITS standard (invalid value string: 'BAR').  Fixed 'FOO' card to meet the FITS standard. [astropy.io.fits.verify]
WARNING: VerifyWarning: Note: astropy.io.fits uses zero-based indexing.
 [astropy.io.fits.verify]

In [8]: c.value
Out[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 ! 😨

1reaction
mwcraigcommented, 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):

from ccdproc import ImageFileCollection
from astropy.io.fits import VerifyError

ic = ImageFileCollection('.')

for hdu in ic.hdus(save_location='foo'):
    h = hdu.header
    bads = []
    for i, c in enumerate(h.cards):
        if isinstance(c[1], str) and '\n' in c[1]:
           bads.append(i)
    for b in bads:
        new = h[b].replace('\n', '~~')
        h[b] = new
    try:
        h.tostring()
    except VerifyError:
        # Yes, seriously, this.
        h.tostring()
    h.update()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Headers — Astropy v5.2
Write the header as text to a file or a file-like object. Update the Header with new keyword values, updating the values of...
Read more >
Batch edit fits headers | PixInsight Forum
I have a bunch of darks that mistakenly have the OBJECT keyword as M1. Is there a way to batch edit the keyword...
Read more >
Headers — Astropy v1.0.4
A string representing a FITS header. Update the Header with new keyword values, updating the values of existing keywords and appending new keywords...
Read more >
How to conserve header when saving an edited .fits file with ...
First of all don't do this: im = fits.getdata('myfile.fits') header = fits.getheader('myfile.fits'). As explained in the warning here, ...
Read more >
Editing the FITS Header - Mirametrics
The header of a FITS Format Image contains a lot of image documentation. Most Mira commands update and change it as necessary to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found