pro yo_file_check, iinfiles, badfiles, indir=indir, quiet=quiet, 	$
      status=status, allgood=allgood, delete=delete
;
;+
;   Name: yo_file_check
;
;   Purpose: verify integrity of Yohkoh data files
;
;   Calling Sequence:
;     yo_file_check, filearray [,badfiles, status=status, allgood=algood, /quiet] 
;
;   Input Paramters:
;      infiles - string or string array of filesnames to check
;
;   Output Parameters:
;      badfiles - names of infiles which failed one or more tests
;    
;   Keyword Parameters:
;      status - (output) - status flags to indicate test failures ; logical OR
;	                   (0=ok,1=missing,2=too small,4=size,8=integer,16=real)
;      allgood - (output) - scaler boolean (1=allok, 0= 1 or more files bad)
;                           (so caller can use: if allgood then ... else ....)
;      quiet  - (input) - switch, if set, suppress most error messages
;      delete - (input) - switch, if set, delete bad files
;
;   Method:
;      check - file_exist? file_size=internal size?
;              internal integer and real value verifications
;
;   History:
;      3-Jun-1994 (SLF) - rewrite yo_file_check (by MDM) - external size checks
;                         perform internal data integrity checks
;      7-Jun-1994 (SLF) - add ALLGOOD, INDIR, and DELETE keywords
;-

infiles=''
case 1 of 
   keyword_set(indir):infiles=file_list(indir,'*')
   data_chk(iinfiles,/string): infiles=iinfiles
   else: begin
      message,/info,"Need list of file names or directory..."
   endcase
endcase

if infiles(0) eq '' then begin
   message,/info,"no files found, returning..."
   return
endif

loud=1-keyword_set(quiet)
; Categories
sizes=file_stat(infiles,/size)
filess=intarr(n_elements(sizes))		
missing=where(sizes lt 0,mcnt)
small=where(sizes ge 0 and sizes lt 100,scnt)	; at least enough for a pointer record

case 1 of
   mcnt gt 0: filess(missing)=filess(missing) or 1
   scnt gt 0: filess(small)  = filess(small) or 2
   else:
endcase

; read pointers for files large enough not to crash rd_pointer
good=where(filess eq 0,gcnt)
if gcnt gt 0 then begin
   rd_pointer,infiles(good),pointers
;  check internal size
   filess(good) = filess(good) or ((pointers.totbytes ne sizes(good)) * 4)
   filess(good)  = filess(good) or ((pointers.itest ne 16909060l) * 8) 
   filess(good)  = filess(good) or ((pointers.rtest ne 123400.)   * 16)
endif

badss=where(filess ne 0,bcnt)
status=filess

errors=[	$
	"File not found",	$
	"File too small",	$
	"File size mismatch",	$
	"Integer Test Failed",	$
	"Real Test Failed"	]

if bcnt eq 0 then message,/info,"All consistency checks nominal" else begin
   badfiles=infiles(badss)
   message,/info,"Number of bad files: " + strtrim(bcnt,2)
   for i=0,n_elements(errors)-1 do begin
      bad=where(status and 2^i,bcnt)
      if bcnt gt 0 and loud then prstr,["Error: " + errors(i),infiles(bad),'']
   endfor
   if keyword_set(delete) then file_delete, infiles(badss)
endelse

allgood=(total(status) eq 0)(0)		; set global status variable

return
end

