next up previous contents FITSIO Home
Next: 2.3 WRITEBINTABLE - write Up: 2. Example FITSIO Programs Previous: 2.1 WRITEIMAGE - write   Contents

2.2 WRITEASCII - write a FITS ASCII table

This routine writes a simple FITS ASCII table containing 3 columns and 6 rows. For convenience, the ASCII table extension is appended to the FITS image file created previously by the WRITEIMAGE routine.

  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. FTCRHD creates a new empty FITS extension following the current extension and moves to it. In this case, FITSIO was initially positioned on the primary array when the FITS file was first opened, so FTCRHD appends an empty extension and moves to it. All future FITSIO calls then operate on the new extension (which will be an ASCII table).
  5. FTGABC is a convenient subroutine for calculating the total width of the table and the starting position of each column in an ASCII table. Any number of blank spaces (including zero) may be inserted between each column of the table, as specified by the NSPACE parameter.
  6. FTPHTB writes all the required header keywords which define the structure of the ASCII table. NROWS and TFIELDS give the number of rows and columns in the table, and the TTYPE, TBCOL, TFORM, and TUNIT arrays give the column name, starting position, format, and units, respectively of each column. The values of the ROWLEN and TBCOL parameters were previously calculated by the FTGABC routine.
  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.
  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 writeascii

C     Create an ASCII table containing 3 columns and 6 rows

      integer status,unit,readwrite,blocksize,tfields,nrows,rowlen
      integer nspace,tbcol(3),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/'A8','I6','F4.2'/
      data tunit/' ','km','g/cm'/
      data name/'Mercury','Venus','Earth','Mars','Jupiter','Saturn'/
      data diameter/4880,12112,12742,6800,143000,121000/
      data density/5.1,5.3,5.52,3.94,1.33,0.69/

 1    status=0
C     Name of the FITS file to append the ASCII table to:
      filename='ATESTFILEZ.FITS'

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     append a new empty extension onto the end of the primary array
 4    call ftcrhd(unit,status)

C     define parameters for the ASCII table (see the above data statements)
      tfields=3
      nrows=6
      extname='PLANETS_ASCII'
      
C     calculate the starting position of each column, and the total row length
      nspace=1
 5    call ftgabc(tfields,tform,nspace,rowlen,tbcol,status)

C     write the required header parameters for the ASCII table
 6    call ftphtb(unit,rowlen,nrows,tfields,ttype,tbcol,tform,tunit,
     &            extname,status)

C     write names to the first column, diameters to 2nd col., and density to 3rd
      frow=1
      felem=1
      colnum=1
 7    call ftpcls(unit,colnum,frow,felem,nrows,name,status)
      colnum=2
      call ftpclj(unit,colnum,frow,felem,nrows,diameter,status)  
      colnum=3
      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)
      end

next up previous contents FITSIO Home
Next: 2.3 WRITEBINTABLE - write Up: 2. Example FITSIO Programs Previous: 2.1 WRITEIMAGE - write   Contents