	module readfits

	contains

      subroutine readimage(filename,buffer,naxes)

!     Read a FITS image and determine the minimum and maximum pixel value
	implicit none

      real, pointer :: buffer(:,:)
!!$      real, allocatable :: buffer(:,:)
      integer status,unit,readwrite,blocksize,naxes(2),nfound
      integer group,firstpix,npixels
      real nullval
      logical anynull
      character*(*) filename

 1    status=0

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

      readwrite=0
 3    call ftopen(unit,filename,readwrite,blocksize,status)

!     determine the size of the image
 4    call ftgknj(unit,'NAXIS',1,2,naxes,nfound,status)

!     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

!     initialize variables
      npixels=product(naxes)
      allocate(buffer(naxes(1),naxes(2))); buffer=0.

      group=1
      firstpix=1
      nullval=-999

 6    call ftgpve(unit,group,firstpix,npixels,nullval,buffer,anynull,status)

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

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

	end module readfits
