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.
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