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 Verification throws exception even after fixing an invalid header value

See original GitHub issue

Description

Using astopy.io.fits, I was trying to fix a header card that has non-ASCII characters in its value, by simply replacing its value with empty string. Though the hdu.header['XYZ'] prints '' after assigning the same to it. But on writing it to file (to save my changes) the same exception which was thrown during reading the file is thrown, as if no change has taken place in the hdu.

Expected behavior

Since the value in header card have changed, the .writeto() with default output_verify='exception should not lead to the exception.

Actual behavior

After my changes on 1st HDU i.e. hdul[0], when I do hdul.writeto() for saving the file or `hdul.verify(‘exception’), the following exception occurs:

VerifyError: 
Verification reported errors:
HDU 0:
    Card 16:
        Card 'XYZ' is not FITS standard (invalid value string: "'\x00\x04-\x00?\x05-\x00\x02\x00\x0c9\x02\x00/\x17$\x0c'          /").
Note: astropy.io.fits uses zero-based indexing. 

Steps to Reproduce

hdul = fits.open('abc.fits') 

hdul.info()
# Filename: abc.fits
# No.    Name      Ver    Type      Cards   Dimensions   Format
#  0  PRIMARY       1 PrimaryHDU      17   (1024, 1024)   int16

hdul[0].header
# ValueError: FITS header values must contain standard printable ASCII characters; "'\x00\x04-\x00?\x05-\x00\x02\x00\x0c9\x02\x00/\x17$\x0c'          /" contains characters not representable in ASCII or non-printable characters.

hdul[0].verify('exception') # to identify which key has problem
# VerifyError: 
# Verification reported errors:
# Card 16:
#    Card 'XYZ' is not FITS standard (invalid value string: "'\x00\x04-\x00?\x05-\x00\x02\x00\x0c9\x02\x00/\x17$\x0c'          /").
# Note: astropy.io.fits uses zero-based indexing.

hdul[0].header['XYZ']="" # it's non-important so replacing value by "" is enough

hdul[0].header['XYZ'] # check if value has changed
# '' 

hdul.writeto('fixed_abc.fits') # or hdul.verify('exception')
# VerifyError: 
# Verification reported errors:
# HDU 0:
#     Card 16:
#         Card 'XYZ' is not FITS standard (invalid value string: "'\x00\x04-\x00?\x05-\x00\x02\x00\x0c9\x02\x00/\x17$\x0c'          /").
# Note: astropy.io.fits uses zero-based indexing. 

I’m guessing that changes made by header key assignment in the hdu in memory, is not the hdu which is being used for verification?

And surprisingly, I found a strange workaround that if I print complete header before saving hdul, it works fine:

hdul[0].header
# SIMPLE  =                    T          /                                       
# BITPIX  =                   16          /                                       
# NAXIS   =                    2          /                                       
# NAXIS1  =                 1024          /                                       
# NAXIS2  =                 1024          /    
# ...
# XYZ     = ''

hdul.writeto('fixed_abc.fits') # works!!

Is this something related how hdu is modified in memory by printing full header?

System Details

Linux-5.4.0-67-generic-x86_64-with-glibc2.31 Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:02:46) [GCC 9.3.0] Numpy 1.20.1 astropy 4.2 Scipy 1.6.1 Matplotlib 3.3.4

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
embraycommented, Apr 6, 2021

I agree this is a duplicate of #11108. Thanks for the test case @jaladh-singhal , I was able to create an invalid FITS file like the one you described, and confirmed that your test case is now fixed.

1reaction
pllimcommented, Mar 25, 2021

Maybe something to do with lazy loading, @saimn ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid FITS header error on Nebulosity 3.0 FIT files · Issue #115
I just received a fit file from one of my Beta testers that doens't seem to work. The fit was created by Nebulosity...
Read more >
Verification — Astropy v5.2
If it is not fixable, it will throw an exception. The principle behind fixing is to do no harm. For example, it is...
Read more >
spring boot request header validation against enum
I have a spring boot application which has rest controller. Along with the payload i want to validate header values.
Read more >
How to fix the "Invalid Header" error for CSV uploads
I tried to upload a CSV and got an "Invalid Header" error. What should I do next? This error is usually caused by...
Read more >
Verification Options - Astropy v1.0.4
If it is not fixable, it will throw an exception. The principle behind fixing is to do no harm. For example, it is...
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