next up previous contents FITSIO Home
Next: 3. Utility Subroutines Up: 2. Example FITSIO Programs Previous: 2.7 READIMAGE - read   Contents

2.8 READTABLE - read data from a FITS table

This example reads and prints out all the data in the ASCII and the binary tables that were previously created by WRITEASCII and WRITEBINTABLE. Note that the exact same FITSIO routines are used to read both types of tables.

  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 READONLY access.
  4. We loop though the main block of code 2 times, first to read the ASCII table, then to read the binary table.
  5. FTMRHD moves forward one HDU in the FITS file. The HDUTYPE parameter indicates whether the extension is an ASCII table (= 1) or a binary table (= 2).
  6. FTGKNS is used to read the indexed array of TTYPEn keywords that specify the names of the columns in the tables.
  7. FTGCVS reads the NAMES from the first column of the table.
  8. FTGCVJ reads the DIAMETER values from the second column.
  9. FTGCVE reads the DENSITY values from the third column.
  10. The FITS file must always be closed before exiting the program. Any unit numbers allocated with FTGIOU must be freed with FTFIOU.
  11. PRINTERROR is a general routine to print out error messages and is described later in the Utilities section of this cookbook.

      subroutine readtable

C     read and print data values from an ASCII or binary table

      integer status,unit,readwrite,blocksize,hdutype,ntable
      integer felem,nelems,nullj,diameter,nfound,irow,colnum
      real nulle,density
      character filename*40,nullstr*1,name*8,ttype(3)*10
      logical anynull

 1    status=0

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

C     open the FITS file previously created by WRITEIMAGE
      filename='ATESTFILEZ.FITS'
      readwrite=0
 3    call ftopen(unit,filename,readwrite,blocksize,status)

C     loop twice, first reading the ASCII table, then the binary table
 4    do ntable=1,2

C         move to the next extension
 5        call ftmrhd(unit,1,hdutype,status)

          print *,' '
          if (hdutype .eq. 1)then
              print *,'Extension ',ntable,' is an ASCII table.'
          else if (hdutype .eq. 2)then
              print *,'Extension ',ntable,' is a binary table.'
          end if

C         read the TTYPEn keywords, which give the names of the columns
 6        call ftgkns(unit,'TTYPE',1,3,ttype,nfound,status)
          write(*,2000)ttype
2000      format(8x,3a10)

C         read the data, one row at a time, and print them out
          felem=1
          nelems=1
          nullstr=' '
          nullj=0
          nulle=0.
          do irow=1,6
              colnum=1
 7            call ftgcvs(unit,colnum,irow,felem,nelems,nullstr,name,
     &                    anynull,status)
              colnum=2
 8            call ftgcvj(unit,colnum,irow,felem,nelems,nullj,diameter,
     &                    anynull,status)
              colnum=3
 9            call ftgcve(unit,colnum,irow,felem,nelems,nulle,density,
     &                    anynull,status)
              write(*,2001)irow,name,diameter,density
2001          format(i4,a10,i10,f10.2)
          end do
      end do

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

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

next up previous contents FITSIO Home
Next: 3. Utility Subroutines Up: 2. Example FITSIO Programs Previous: 2.7 READIMAGE - read   Contents