#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*
  Every program which uses the CFITSIO interface must include the
  the fitsio.h header file.  This contains the prototypes for all
  the routines and defines the error status values and other symbolic
  constants used in the interface.  
*/
#include "fitsio.h"

int main(int argc, char *argv[]);
void readimage( char *filename);



int main(int argc, char *argv[])
{
/*************************************************************************
   This is a simple main program that calls the following routines:

   
    readimage     - read a FITS image and compute the min and max value
    
**************************************************************************/

   
    readimage(argv[1]);
   

    printf("\n\n");
    return(0);
}

/*--------------------------------------------------------------------------*/
void readimage( char *filename)

    /************************************************************************/
    /* Read a FITS image and determine the minimum and maximum pixel values */
    /************************************************************************/
{
    fitsfile *fptr,*fptr1;       /* pointer to the FITS file, defined in fitsio.h */
    int status,  nfound, anynull;
    long naxes[2], fpixel, nbuffer, npixels, ii;

#define buffsize 1000
    float datamin, datamax, nullval, buffer[buffsize];
    status = 0;

    fits_open_file(&fptr, filename, READONLY, &status) ;
    if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }    

    
    /* read the NAXIS1 and NAXIS2 keyword to get image size */
    fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status) ;
     if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  

    npixels  = naxes[0] * naxes[1];         /* number of pixels in the image */
    fpixel   = 1;
    nullval  = 0;                /* don't check for null values in the image */
    datamin  = 1.0E30;
    datamax  = -1.0E30;

    while (npixels > 0)
    {
      nbuffer = npixels;
      if (npixels > buffsize)
        nbuffer = buffsize;     /* read as many pixels as will fit in buffer */

      /* Note that even though the FITS images contains unsigned integer */
      /* pixel values (or more accurately, signed integer pixels with    */
      /* a bias of 32768),  this routine is reading the values into a    */
      /* float array.   Cfitsio automatically performs the datatype      */
      /* conversion in cases like this.                                  */

      fits_read_img(fptr, TFLOAT, fpixel, nbuffer, &nullval,
		    buffer, &anynull, &status) ;
      if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  
      
      for (ii = 0; ii < nbuffer; ii++)  {
        printf("\t%f", buffer[ii]) ;
        if ( buffer[ii] < datamin )
            datamin = buffer[ii];

        if ( buffer[ii] > datamax )
            datamax = buffer[ii];
      }
      npixels -= nbuffer;    /* increment remaining number of pixels */
      fpixel  += nbuffer;    /* next pixel to be read in image */
    }

    printf("\nMin and max image pixels =  %.0f, %.0f\n", datamin, datamax);

    fits_close_file(fptr, &status) ;
     if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  
   
    return;
}


