Subject: FITSIO: ASCII tables definition

From: "Marcelo Magallon Gh." <MMAGALLO%UCRVM2.BITNET at uga.cc.uga.edu>
Date: Fri, 29 Oct 93 10:34:20 UCR

Hello everybody!

I have a question concerning FITSIO. I'm trying to put some information
into an ASCII table, but I don't know how much rows I have to write,
unless I read the entire data file... the data file is very long and
I have to do a lot of parsing before writing the information to the
ASCII table. Before trying to solve the whole problem at once (ASCII table,
parsing, some calculations), I decided to break it appart and solve
each part separatedly. Now, I'm trying to create an ASCII table in the
same way that I'll have to do it later, i.e. without knowing the number
of rows I'll have to write. Now, the problem:

I have to call FTPHTB and FTADEF in order to define table's characteristics.
That's ok, but I have to state the number of rows in the table. I'm
putting a dummy number (5)... it works ok. FITSIO makes no complaint. Now
I write about 500 rows, so I have to change NAXIS2 keyword, but I don't
want to change the keyword directly (I don't know if I'm allowed to do
that), rather, I want to UPDATE table's header... the question is
(finally!) how do I do that!?

Some of my solutions (DW= Didn't work; AP=Apparently worked, something
failed)
1) Call FTADEF again after writing the data. DW, no change in NAXIS2
2) Call FTPHTB again. AP, NAXIS2 changed but the header is dupplicated.
3) Call FTPHTB *and* FTADEF again. AP, but the data is gone!
4) Call FTPHTB and FTADEF *after* the data is written to the file.
   AP, but, again, the data is gone.
5) Call FTPHTB before and FTADEF after the data is written. DW.
6) Invert procedure in 5), same result.
7) I'm going nuts... This is the only thing that's working... %)

ANY help... May I rewrite NAXIS2?

Thaks A LOT, really.

---

From: pence at tetra.gsfc.nasa.gov (William Pence)
Date: Fri, 29 Oct 93 14:16:31 EDT

>unless I read the entire data file...

The recommended procedure for creating an ASCII or binary FITS table
using FITSIO when the total number or rows to be written to the table
is unknown at the start, is described in some detail in section 4.3
(Advanced Usage) of the FITSIO documentation.   Here is a brief outline
of the procedure:

1.  Write the required header keywords with ftphtb or ftphbn.  Set the 
NAXIS2 keyword value (=the number of rows in the table) to zero (or any
other positive integer dummy value). 

2.  Define the structure of the table with ftadef or ftbdef.  Again, set
the number of rows equal to zero, or some other positive dummy value (see
additional note about this below).

3.  Write the data to the table, while keeping track of the total number
of rows that have been written.

4.  After all the rows have been written, 2 more steps must be performed
before the table can be cleanly closed:

     a)  update the correct value of the NAXIS2 keyword using the ftmkyj
         subroutine

     b)  update the total size of the table (in bytes) using the ftddef
         subroutine.  Normally FITSIO determines the table size from
         the ftadef of ftbdef subroutine arguments, but since the number
         of rows have changed, one must explicitly redefine the size of
         of the table. 

5. Close the FITS file with ftclos (or move on to write another new 
extension).

There is just one possible problem with this procedure when writing
an ASCII FITS table.  The FITS Standard