Skip to content

"ReferenceError: weakly-referenced object no longer exists" with FITS columns #4345

Closed as not planned

Description

@embray

This issue was revealed by a StackOverflow question:

http://stackoverflow.com/questions/33850641/how-to-merge-two-table-with-pyfits

I feel that in principle what the user was doing here should have worked. The "Delayed" data mechanism being used here for the columns is a hold-over from much older versions of PyFITS, and needs to be reworked.

This would actually be addressed by the plan I've long had for refactoring FITS I/O so that the I/O details for reading an individual array (where that could also be a single column in a table) are fully separated from the HDU object itself, which is somewhat more ephemeral.

The way this would work is that all the details of the data structure for a FITS file would be determined from the header(s) when the file is first read, and cached in a data structure that associates the info with that file (along with a timestamp) or file-like object. When using mmap this might also hold an existing mmap to the file, though I'm less sure of how I feel about that. Anyways, point being, a "delayed read" of an array would work by looking into this cache for the physical details about where to find the array data, and then return it, without going through the HDU object itself. This would also speed up the "convenience" functions, since they can access cached details about a file without having to re-read all the file's headers each time the same file is accessed.

This has been my plan for a long time but it hasn't been high priority.

Activity

  1. AMAN3003 commented on Feb 7, 2016

    @AMAN3003

    @embray for table operation this would be the fix with pyfits to mergre table
    hdulist1 = pyfits.open(table1)
    hdulist2 = pyfits.open(table2)
    t1 = hdulist1[1].columns
    t2 = hdulist2[1].columns
    new_columns = t1 + t2
    hdu = pyfits.BinTableHDU.from_columns(new_columns)
    hdu.writeto(outtable)
    #explicitly close the HDUs; might free up memory
    hdulist1.close()
    hdulist2.close()

  2. AMAN3003 commented on Feb 7, 2016

    @AMAN3003

    @embray but when I tried to use any table operation in astropy it result in the following error

    from astropy.table import Table, hstack

    In [33]: t1 = Table.read(table1)

    NameError Traceback (most recent call last)
    in ()
    ----> 1 t1 = Table.read(table1)

    NameError: name 'table1' is not defined

    @embray do I Need to create table if so then where

  3. embray commented on Feb 8, 2016

    @embray
    MemberAuthor

    @AMAN3003 Those examples assume table1 and table are just filenames for some FITS files containing binary tables--as the first argument to fits.open is a filename or file-like object.

    Please don't take this the wrong way, but if you weren't able to understand the meaning of the above Traceback this issue is far too advanced to be worth your time.

  4. AMAN3003 commented on Feb 8, 2016

    @AMAN3003

    @embray I did understand the meaning of the above but could you provide sample table fits file to fix this issue

  5. embray commented on Feb 17, 2016

    @embray
    MemberAuthor

    You can find example tables in the test data, but you'll also note that this issue has been labeled "Package-expert" since I submitted it.

  6. saimn commented on Dec 4, 2025

    @saimn
    Contributor

    This works

    In [6]: with fits.open("lower.fits") as hdul1, fits.open("upper.fits") as hdul2:
       ...:     new_columns = hdul1[1].columns + hdul2[1].columns
       ...:     hdu = fits.BinTableHDU.from_columns(new_columns)
    

    so will just close this as "wontfix" (one should keep a reference to the parent HDU/HDUList).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions