Viewing contents of file '../idllib/uit/pro/chkdtype.pro'
pro chkdtype, im, hd, information_only = inf,silent=silent,Autochange=Autochange
;+
; NAME:
;	CHKDTYPE
; PUROPOSE:
;	Compares Datatype of an Image to that stored in its header, and corrects
;	the header if any discrepancy is found (at user's discretion)
; CALLING SEQUENCE:
;	CHKDTYPE, IM, HD [, /INFO, /SILENT, /AUTO] 
; INPUTS:
;	IM - 2-dimensional image
;	HD - FITS header for IM
; OUTPUTS:
;	If necessary, BITPIX and DATATYPE fields of HD will be changed.
; OPTIONAL KEYWORD INPUTS:
;	INFO - Informational status only-- will not change FITS header
;	AUTO - Automatically updates header with new parameters
;	SILENT - Supresses informational messages unless IM and HD disagree. 
; PROCEDURE:
;	Compares BITPIX and DATATYPE in HD to the datatype of IM.  If they
;	agree, return.  If not, allow the user the option to change the header,
;	unless INFO or AUTO are selected.  If the routine returns with the
;	header and image still disagreeing, !ERR is set to -1, otherwise
;	!ERR is set to zero.
; SIDE EFFECTS:
;	None
; COMMON BLOCKS:
;	None
; HISTORY:
;	Written by J. D. Offenberg, Hughes-STX, 05 DEC 1991
;-
!ERR = 0
SZ = Size(IM)     &  NSZ = N_elements(SZ)
SZHD = Size(HD)   &  NHD = N_elements(SZHD)
;
;Check to make sure that 2 parameters have been supplied, and that they are 
;an image array and a FITS header, respectively.
;
IF N_Params() LT 2 then begin
	Message,/inf,'ERROR: Calling Sequence-- CHKDTYPE, Image, Header, [/inf]'
	return
endIF ELSE $
IF SZ(0) NE 2 then begin		;Is Array 2-dimensional?
	Message,/inf,'ERROR: Image array is not 2 dimensional!'
	return
endIF ELSE $
IF (SZHD(0) ne 1) or (SZHD(NHD-2) ne 7) then begin
	message,/inf,'ERROR:  FITS header must be supplied with image'
	return
endIF
;
;Now that we are sure that we have an image array and a FITS header, it is
;time to compare them.
;
BITPIX = sxpar(hd,'BITPIX')
DTYPE = sxpar(hd,'DATATYPE')

If Datatype(DTYPE) eq 'STR' then DType = strtrim(strupcase(DType))  $
ELSE	DTYPE = 'NONE'

IF SZ(3) GE 7 or SZ(3) LE 0 then begin  	;Is image a non-numerical type?
	message,/inf,'Your array is type STRING or STRUCTURE-- Cannot be converted!'
	return
endIF

CASE SZ(3) of
1:	BEGIN
	IMType = 'LOGICAL*1'  &  IMBitPix = 8
	END
2:	BEGIN
	IMType = 'INTEGER*2'  &  IMBitPix = 16
	END
3:	BEGIN
	IMType = 'INTEGER*4'  &  IMBitPix = 32
	END
4:	BEGIN
	IMType = 'REAL*4'     &  IMBitPix = -32
	END
5:	BEGIN
	IMType = 'REAL*8'     &  IMBitPix = -64
	END
6:	BEGIN
	IMType = 'COMPLEX*8'  &  IMBitPix = 64
	END
ELSE:   BEGIN
	message,/inf,'Programming error!  Should not reach this point!'
	return
	endELSE
endCASE

IF IMType EQ DTYPE and IMBITPIX eq BITPIX then begin	;Does everything agree?
	IF not keyword_set(silent) then $
	message,/inf,'Image and header agree.'
	return
endIF ELSE $
IF IMType EQ "LOGICAL*1" and DTYPE EQ "INTEGER*1" then begin	;Special Case
	IF not keyword_set(silent) then $
	message,/inf,'Image and header agree.'
	return
endIF ELSE $						;Things don't agree.
	IF not(keyword_set(silent) and keyword_set(autochange)) then begin
		print,'Image and header do not agree!'
		print,'Image datatype: ',IMtype,'Header datatype: ',DTYPE,$
			f='(A20,A12,A30,A12)'
		print,'Image BitPix: ',IMBitPix,'Header BitPix: ',BitPix,$
			f='(A20,I12,A30,I12)'
	endIF
	!ERR = -1
;
;If the user requested INFORMATION_ONLY then return
;
IF keyword_set(Inf) then RETURN
;
;If the user requested AUTOCHANGE then change the header, and return
;
IF Keyword_set(autochange) then BEGIN
	message,/inf,'Changing DATATYPE and BITPIX in header: '+ IMType + $ 
							        string(IMBitpix)
	sxaddpar,hd,'BITPIX',IMBitpix
	sxaddpar,hd,'DATATYPE',IMType
	!err = 0
	return
endIF
;
;If the user did not specify INFO_ONLY or AUTOCHANGE, then ask before changing.
;
YorN = ''
read,'Do you wish to change the header to match the array [YES]? ',YorN
IF strupcase(strmid(YorN,0,1)) ne 'N' then BEGIN
	message,/inf,'Changing DATATYPE and BITPIX in header: '+ IMType + $ 
							        string(IMBitpix)
	sxaddpar,hd,'BITPIX',IMBitpix
	sxaddpar,hd,'DATATYPE',IMType
	!Err = 0
endIF
return
end