How can I write a fits table into an output LDAC fits catalog using Python
Question: I have an [LDAC](http://marvinweb.astro.uni-
bonn.de/data_products/THELIWWW/LDAC/LDAC_concepts.html) fits catalog which in
a Python code I need to add the elements of two arrays as two new columns to
it.  
I open the original catalog in python:

    
    
    from astropy.io import fits
    from astropy.table import Table
    import astromatic_wrapper as aw  
    
    cat1='catalog.cat'
    hdulist1 =fits.open(cat1)
    data1=hdulist1[1].data
    

The two arrays are ready and called **ra** and **dec**. I give them the key
name, format and other needed info and invert them to columns. Finally, I join
the two new columns to the original table (Checking **newtab.columns** and
**newtab.data** shows that the new columns are attached successfully).

    
    
    racol=fits.Column(name = 'ALPHA_J2000', format = '1D', unit = 'deg', disp = 'F11.7',array=ra)
    deccol=fits.Column(name = 'DELTA_J2000', format = '1D', unit = 'deg', disp = 'F11.7',array=dec)
    
    cols  = fits.ColDefs([racol, deccol])
    tbhdu = fits.BinTableHDU.from_columns(cols)
    
    orig_cols= data1.columns
    newtab = fits.BinTableHDU.from_columns(cols + orig_cols)
    

When I save the new table into a new catalog:

    
    
    newtab.writeto('newcatalog.cat')
    

it is not in the format that I need. If I look into the description of each
catalog with

    
    
    ldacdes -i 
    

I see for _catalog.cat_ :

    
    
    > Reading catalog(s)  
    ------------------Catalog information----------------  
    Filename:..............catalog.cat  
    Number of segments:....3  
    
    ******  Table #1  
            Extension type:.........(Primary HDU)  
            Extension name:.........  
    
    ******  Table #2  
            Extension type:.........BINTABLE  
            Extension name:.........OBJECTS  
            Number of dimensions:...2  
            Number of elements:.....24960  
            Number of data fields...23  
            Body size:..............4442880 bytes  
    
    ******  Table #3
            Extension type:.........BINTABLE  
            Extension name:.........FIELDS  
            Number of dimensions:...2  
            Number of elements:.....1  
            Number of data fields...4  
            Body size:..............28 bytes  
    
    
    > All done
    

and for the new one:

    
    
    > Reading catalog(s)  
    ------------------Catalog information----------------  
    Filename:..............newcatalog.cat  
    Number of segments:....2  
    
    ******  Table #1
            Extension type:.........(Primary HDU)
            Extension name:.........
    
    ******  Table #2
            Extension type:.........BINTABLE
            Extension name:.........
            Number of dimensions:...2
            Number of elements:.....24960
            Number of data fields...25
            Body size:..............4842240 bytes
    
    
    > All done
    

As seen above, in the original catalog _catalog.cat_ there are three tables
and I tried to add two columns to the OBJECTS table.  
I need that _newcatalog.cat_ also keeps the same structure which is required
by other programs, but it does not have the OBJECTS table and considering the
"Number of elements" and the "Number of data fields" the newtab is saved into
the Table #2.

Is there any solution for controlling the output fits catalog format?

Thank you for your help and I hope that I could structure my very first
question on stackoverflow properly .


Answer: I don't know specifically about the LDAC format, but from your example file
_catalog.cat_ , it appears to be a _multi-extension_ FITS file. That is, each
table is stored in a separate HDU (as is typical for any file containing
multiple tables with different sets of columns).

When you do something like

    
    
    newtab = fits.BinTableHDU.from_columns(cols + orig_cols)
    newtab.writeto('newcatalog.cat')
    

You're just creating a single new binary table HDU and writing that HDU to a
file by itself (along with the mandatory primary HDU). What you really want is
to take the same HDU structure as the original file and replace the existing
table HDU with the one to which you added new columns.

Creating multi-extension FITS is discussed some
[here](http://docs.astropy.org/en/stable/io/fits/appendix/faq.html#how-do-i-
create-a-multi-extension-fits-file-from-scratch), but you don't even need to
recreate the full HDU structure from scratch. The
[`HDUList`](http://docs.astropy.org/en/stable/io/fits/api/hdulists.html#astropy.io.fits.HDUList)
object return from `fits.open` is just a list of HDUs that can be manipulated
like a normal Python list (with some extensions, for example, to support
indexing by EXTNAME) and written out to a file:

    
    
     hdulist = fits.open(cat1)
     hdulist['OBJECTS'] = newtab
     hdulist.writeto('newcatalog.cat')
    

