Viewing contents of file '../idllib/ghrs/pro/chkimhd.pro'
pro chkimhd,im,hd,xsize,ysize,idltype
;+
; NAME:
;   CHKIMHD
; PURPOSE:
;   Check that the first parameter is an array type, that the second 
;   parameter is a header type, and that the size info in the header
;   matches that in the array.  One may optionally check if the 
;   datatype specified by DATATYPE matches the data array.  Program
;   will set !ERR=1 and print error message if one of these conditions 
;   does not hold.
; CALLING SEQUENCE:
;   chkimhd,im,hd               ;Check header matches image size
;   chkimhd,im,hd,xsize,ysize	;Output size in X and Y dimensions
;   chkimhd,im,hd,xsize,ysize,idltype	;Also check DATATYPE keyword
; INPUTS:
;   im - image array
;   hd - header array
; OPTIONAL OUTPUTS:
;   xsize - size of image array in X-direction, formatted as I4 string
;   ysize - size of image array in Y-direction, formatted as I4 string
;   idltype- type of the array as specified in the IDL SIZE function
;           (1 for BYTE, 2 for INTEGER*2, 3 for INTEGER*4, etc.)
; PROCEDURE:
;   Program checks the NAXIS1 and NAXIS2 parameters in the header to
;   see if they match the image array dimensions.
; MODIFICATION HISTORY:
;   Written, October 1986 W. Landsman
;   Modified for version 2 IDL, Jan 1990, B. Pfarr
;- 
!ERR = 0
err = string(7B) + 'CHKIMHD: ERROR - '
hinfo = size(hd)
if (hinfo(0) ne 1) then begin	;Is hd of string type?
	print,err,'Image header is not a string array'
        !ERR = -1 & return
endif
im_info = size(im)
ndim = im_info(0)
if (ndim lt 2) or $		;Don't worry about degenerate dimensions
   (im_info(ndim+2) ne im_info(1)*im_info(2)) then begin
	print,err,'Image array is not 2 dimensional'
        !ERR = -1 & return
endif
;
xsize = string(format='(i4)',im_info(1))
ysize = string(format='(i4)',im_info(2))
;
naxis = sxpar(hd,'NAXIS*')
nax = n_elements(naxis)
if nax lt 2 then begin
	print,err,'Header shows only',string(format='(i2)',nax),' NAXIS'
        !ERR = -1 & return
endif
for i=0,1 do begin
      if naxis(i) ne im_info(i+1) then begin
      print,err,'Size information in header does not match image array'
      print,'NAXIS'+string(format='(i1)',i+1) + ' =',naxis(i)
      !ERR = -1 &  return
endif
endfor
if n_params(0) eq 5 then begin
   idltype = im_info(im_info(0)+1)
   datatype = strtrim(sxpar(hd,'DATATYPE'))
   if !ERR ne -1 then begin
   case idltype of
     1: if  datatype eq 'LOGICAL*1' then return
     2: if (datatype eq 'INTEGER*2') or (datatype eq 'UNSIGNED*2') then return
     4: if  datatype eq 'REAL*4' then return
     3:if (datatype eq 'INTEGER*4') or (datatype eq 'UNSIGNED*4') then return
     5:if  datatype eq 'REAL*8' then return
     6:if  datatype eq 'COMPLEX*16' then return
     else: begin
	   print,err,'Image array is non-numeric datatype'
           !ERR = -1  & return
      end
      endcase
      print,err,'Array type does not match DATATYPE of ',datatype
      print,'Use SXADDPAR to change DATATYPE keyword'
      !ERR = -1
    endif else begin
    case idltype of
    1: datatype = 'LOGICAL*1'
    2: datatype = 'INTEGER*2'
    4: datatype = 'REAL*4'
    3: datatype = 'INTEGER*4'
    5: datatype = 'REAL*8'
    6: datatype = 'COMPLEX*16'
   endcase
   print,'Image array is type "',datatype,'" - DATATYPE keyword added to header'
   sxaddpar,hd,'DATATYPE',datatype,'FITS/SDAS version of BITPIX','HISTORY'
   !ERR = 0
endelse
endif
;
return
end