Skip to content

Primary HDU checksum data must be np.array? #8660

Closed

Description

@jaytmiller

I have some code which attempts to update the FITS checksums:

 output = "out.fits"
 with fits.open(self.filepath, do_not_scale_image_data=True) as hdus:
         for hdu in hdus:
               fits.append(output, hdu.data, hdu.header, checksum=True)

For a file which has no data in an HDU (primary), this code now raises an exception requiring that the data array be an np.array rather than None.

There's a relatively easy "work" around something like this:

     data = hdu.data if hdu.data is not None else np.array([])

but I wasn't positive it's correct.

Since it used to be possible to round trip these HDUs, I thought I should raise it as an issue and possible bug.

I (think) this problem is exposed with:

astropy 4.0.0.dev12788+g4166b6a94 py36_0 http://ssb.stsci.edu/conda-dev
numpy 1.16.3 py36hacdab7b_0
numpy-base 1.16.3 py36h6575580_0

Activity

  1. pllim commented on May 6, 2019

    @pllim
  2. saimn commented on May 6, 2019

    @saimn
    Contributor

    It's my fault, sorry. This is because of 6800eec : _makehdu relies on _BaseHDU(data, header) being able to use the HDU detection from the header, and there is no test for this...

    I removed _BaseHDU.__new__ because when reading from a FITS file this HDU detection was done twice. So I'm hesitating between just reverting this commit, or adding a classmethod which would explicitly call the class detection. I will look into this later today.

    In [2]:  output = "out.fits" 
       ...:  with fits.open('astropy/io/fits/tests/data/test0.fits', do_not_scale_image_data=True) as hdus: 
       ...:          for hdu in hdus: 
       ...:                fits.append(output, hdu.data, hdu.header, checksum=True) 
       ...:                                                                                                                                                                                        
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-2-579e0ac3115e> in <module>
          2 with fits.open('astropy/io/fits/tests/data/test0.fits', do_not_scale_image_data=True) as hdus:
          3         for hdu in hdus:
    ----> 4               fits.append(output, hdu.data, hdu.header, checksum=True)
    
    ~/dev/astropy/astropy/io/fits/convenience.py in append(filename, data, header, checksum, verify, **kwargs)
        619         # output to the empty object.
        620         #
    --> 621         writeto(filename, data, header, checksum=checksum, **kwargs)
        622     else:
        623         hdu = _makehdu(data, header)
    
    ~/dev/astropy/astropy/utils/decorators.py in wrapper(*args, **kwargs)
        519                             kwargs[new_name[i]] = value
        520 
    --> 521             return function(*args, **kwargs)
        522 
        523         return wrapper
    
    ~/dev/astropy/astropy/io/fits/convenience.py in writeto(filename, data, header, output_verify, overwrite, checksum)
        414     """
        415 
    --> 416     hdu = _makehdu(data, header)
        417     if hdu.is_image and not isinstance(hdu, PrimaryHDU):
        418         hdu = PrimaryHDU(data, header=header)
    
    ~/dev/astropy/astropy/io/fits/convenience.py in _makehdu(data, header)
       1045             hdu = ImageHDU(data, header=header)
       1046         else:
    -> 1047             raise KeyError('Data must be a numpy array.')
       1048     return hdu
       1049 
    
    KeyError: 'Data must be a numpy array.'
    

    EDIT: xref #8502

  3. added
    Affects-devPRs and issues that do not impact an existing Astropy release
    and removed on May 6, 2019
  4. modified the milestone: v3.2 on May 6, 2019
  5. added a commit that references this issue on May 6, 2019
  6. added this to the v3.2 milestone on May 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Affects-devPRs and issues that do not impact an existing Astropy releaseBugio.fits

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions