next up previous contents FITSIO Home
Next: 2.4 COPYHDU - copy Up: 2. Example FITSIO Programs Previous: 2.2 WRITEASCII - write   Contents

2.3 WRITEBINTABLE - write a FITS binary table

This routine creates a FITS binary table, or BINTABLE, containing 3 columns and 6 rows. This routine is nearly identical to the previous WRITEASCII routine, except that the call to FTGABC is not needed, and FTPHBN is called rather than FTPHTB to write the required header keywords.

  1. The STATUS parameter must always be initialized. See the WRITEIMAGE program for a more detailed discussion.

  2. FTGIOU allocates a logical unit number for use when opening a file. See the WRITEIMAGE program for a more detailed discussion.

  3. The FITS file is opened with WRITE access by specifying READWRITE = 1.
  4. FTMAHD moves the internal FITSIO pointer to the second HDU (as specified here by the second parameter) which in this case is the ASCII table extension created by the previous WRITEASCII routine.
  5. FTCRHD creates a new empty FITS extension following the current extension and moves to it. All future FITSIO calls now operate on this new extension which will become a binary table.

  6. FTPHBN writes all the required header keywords which define the structure of the binary table. NROWS and TFIELDS gives the number of rows and columns in the table, and the TTYPE, TFORM, and TUNIT arrays give the column name, format, and units, respectively of each column.

  7. FTPCLS writes the string values to the NAME column (column 1) of the table. The FTPCLJ and FTPCLE routines write the diameter (integer) and density (real) value to the 2nd and 3rd columns. The FITSIO routines are column oriented, so it is usually easier to read or write data in a table in a column by column order rather than row by row. Note that the identical subroutine calls are used to write to either ASCII or binary FITS tables.
  8. The FITS file must always be closed before exiting the program. Any unit numbers allocated with FTGIOU must be freed with FTFIOU.
  9. PRINTERROR is a general routine to print out error messages and is described later in the Utilities section of this cookbook.

      subroutine writebintable

C     Create a binary table containing 3 columns and 6 rows

      integer status,unit,readwrite,blocksize,hdutype,tfields,nrows
      integer varidat,diameter(6), colnum,frow,felem
      real density(6)
      character filename*40,extname*16
      character*16 ttype(3),tform(3),tunit(3),name(6)
      data ttype/'Name','Diameter','Density'/
      data tform/'8A','1J','1E'/
      data tunit/' ','km','g/cm'/
      data name/'Mars','Jupiter','Saturn','Uranus','Neptune','Pluto'/
      data diameter/6800,143000,121000,47000,45000,6000/
      data density/3.94,1.33,0.69,1.56,2.27,1.0/

 1    status=0
C     Name of the FITS file to append the ASCII table to:

C     Get an unused Logical Unit Number to use to open the FITS file
 2    call ftgiou(unit,status)

C     open the FITS file, with write access
 3    readwrite=1
      call ftopen(unit,filename,readwrite,blocksize,status)

C     move to the last (2nd) HDU in the file
 4    call ftmahd(unit,2,hdutype,status)

C     append/create a new empty HDU onto the end of the file and move to it
 5    call ftcrhd(unit,status)

C     define parameters for the binary table (see the above data statements)
C     write the required header parameters for the binary table
 6    call ftphbn(unit,nrows,tfields,ttype,tform,tunit,
     &            extname,varidat,status)

C     write names to the first column, diameters to 2nd col., and density to 3rd
 7    call ftpcls(unit,colnum,frow,felem,nrows,name,status)
      call ftpclj(unit,colnum,frow,felem,nrows,diameter,status)  
      call ftpcle(unit,colnum,frow,felem,nrows,density,status)  

C     close the FITS file and free the unit number
 8    call ftclos(unit, status)
      call ftfiou(unit, status)

C     check for any error, and if so print out error messages
 9    if (status .gt. 0)call printerror(status)

next up previous contents FITSIO Home
Next: 2.4 COPYHDU - copy Up: 2. Example FITSIO Programs Previous: 2.2 WRITEASCII - write   Contents