[AstroPy] PyFITS: Appending tables with variable length array...?

Erik Bray embray at stsci.edu
Fri May 6 16:56:03 EDT 2011

Yeah, that looks like a bug to me.  When you create the new hdu with 
nrows=nr, the length of the VLA column does not get extended.  Normally 
you shouldn't have to do any special treatment for VLA columns at all, 

Here's a workaround that worked for me:

for idx in range(len(t1[1].columns)):
     col = t1[1].columns[idx]
     if col.format[0] == 'P':
         hdu.columns[idx].array = np.resize(col.array, nr)
         hdu.data._convert[idx] = hdu.columns[idx].array
     hdu.data.field(idx)[nr1:] = t2[1].data.field(idx)

It's a hack, but it should work--this replaces the existing VLA column 
with one that's extended out the right size.  I'll add a bug report for 
this issue.


On 05/06/2011 01:11 PM, Taro Sato wrote:
> Hi there.  I'm trying to do something simple with PyFITS, appending
> tables but running into a problem.
> The manual provides a general procedure for tables with no varlen
> arrays, which I mostly follow in the code snippet below.
> import pyfits as pf
> ...
> t1 = pf.open(fits1)
> t2 = pf.open(fits2)
> nr1 = t1[1].data.shape[0]
> nr2 = t2[1].data.shape[0]
> nr = nr1 + nr2
> hdu = pf.new_table(t1[1].columns, nrows=nr)
> for i in range(len(t1[1].columns)):
>      if i == 4:
>          # this is a variable length array column; need to
>          # work on row by row
>          for j, x in enumerate(t2[1].data.field(i)):
>              print((nr, nr1+j))
>              hdu.data.field(i)[nr1+j] = x
>              continue
>      # for non variable length column, this is fine
>      hdu.data.field(i)[nr1:] = t2[1].data.field(i)
> The conditional was added to take care of a variable length array
> column (#5).  But this doesn't work...
> So my question is, how do we do this?  Is it at all possible to update
> values in a varlen array column?  Is my only option to create a new
> table from scratch?
> Thank you for your time,
> Taro
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy

More information about the AstroPy mailing list