next up previous contents FITSIO Home
Next: 2.8 READTABLE - read Up: 2. Example FITSIO Programs Previous: 2.6 READHEADER - read   Contents

2.7 READIMAGE - read the pixel values in a FITS image

This example program reads the FITS image and determines the minimum and maximum pixel values. Rather than reading the entire image in at once (which could require a very large array), the image is read in pieces, 100 pixels at a time.

  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. The FTGKNJ routine reads the set of NAXISn indexed keywords. In this case, we expect to find the NAXIS1 and NAXIS2 keywords.
  5. A check is made to ensure that both NAXISn keywords were found by FTGKNJ.
  6. FTGPVE is used to read in the next group of 100 pixels in the image. This loop is repeated until all the pixels have been read.
  7. The FITS file must always be closed before exiting the program. Any unit numbers allocated with FTGIOU must be freed with FTFIOU.
  8. PRINTERROR is a general routine to print out error messages and is described later in the Utilities section of this cookbook.

      subroutine readimage

C     Read a FITS image and determine the minimum and maximum pixel value

      integer status,unit,readwrite,blocksize,naxes(2),nfound
      integer group,firstpix,nbuffer,npixels,i
      real datamin,datamax,nullval,buffer(100)
      logical anynull
      character filename*80

 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     determine the size of the image
 4    call ftgknj(unit,'NAXIS',1,2,naxes,nfound,status)

C     check that it found both NAXIS1 and NAXIS2 keywords
 5    if (nfound .ne. 2)then
          print *,'READIMAGE failed to read the NAXISn keywords.'
          return
       end if

C     initialize variables
      npixels=naxes(1)*naxes(2)
      group=1
      firstpix=1
      nullval=-999
      datamin=1.0E30
      datamax=-1.0E30

      do while (npixels .gt. 0)
C         read up to 100 pixels at a time 
          nbuffer=min(100,npixels)
      
 6        call ftgpve(unit,group,firstpix,nbuffer,nullval,
     &            buffer,anynull,status)

C         find the min and max values
          do i=1,nbuffer
              datamin=min(datamin,buffer(i))
              datamax=max(datamax,buffer(i))
          end do

C         increment pointers and loop back to read the next group of pixels
          npixels=npixels-nbuffer
          firstpix=firstpix+nbuffer
      end do

C     print out the min and max values
      print *,'Min and max values in the image are:',datamin,datamax

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

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

next up previous contents FITSIO Home
Next: 2.8 READTABLE - read Up: 2. Example FITSIO Programs Previous: 2.6 READHEADER - read   Contents