pro check_ff_load, infil, dsets, img2, dattim, img0, diff, hc=hc, $
	tit=tit, half=half, nmaxdiff=nmaxdiff, start_addr=start_addr, $
	imgfil=imgfil, page10=page10
;+
;NAME:
;	check_ff_load
;PURPOSE:
;	To check the status of the flat field load
;SAMPLE CALLING SEQUENCE:
;	check_ff_load, infil
;	check_ff_load, infil, dsets, img2, dattim
;	check_ff_load, get_last_tfr(), [17,18], img2, dattim, tit='SIM System'
;	check_ff_load, infil, dsets, img2, dattim, img0, diff, half=1
;	check_ff_load, infil, -1, img2, dattim, img0, diff
;	check_ff_load,infil,[129,130],a,b,c,d,start_addr='20000'x	;page 10 backup check
;	check_ff_load,infil,[130,131],a,b,c,d,imgfil='/mdisw/dbase/cal/files/flat_005.fits'
;	check_ff_load, get_last_tfr(), /page10, imgfil='/mdisw/dbase/cal/files/flat_005.fits'
;INPUTS:
;	infil	- The file to check
;	dsets	- The datasets to read (the dump data sets)
;OUTPUTS:
;	img2	- The assembled image
;	dattim	- The date/time of the dump
;	img0	- The proper image (read from the database)
;	diff	- The difference image
;OPTIONAL KEYWORD INPUT:
;	hc	- If set, make a hardcopy of the image
;	tit	- Optional description to be displayed as a title
;	half	- =1 means mask out the second half, we only have data for
;		   the first half.  Equal 2 means mask out the first half
;	nmaxdiff- The maximum number of pixels that can be listed before
;		  not bothering to list them (default = 40)
;HISTORY:
;	Written May-96 by M.Morrison
;	   Oct-96 (MDM) - Various modifications
;			  (like allowing it to search for the datasets
;			  to be processed).
;	 4-Nov-96 (MDM) - Added documentation header
;	12-Nov-96 (MDM) - Added nmaxdiff
;	11-Dec-96 (MDM) - Added START_ADDR
;	13-Dec-96 (MDM) - Corrected for bug when there are no bad pixels
;	16-Apr-97 (MDM) - Added 1/5 checks (assuming they are all there)
;	19-May-97 (MDM) - Corrected search for 1/5th dumps -- was not
;			  looking at the second half.
;	10-Jul-97 (MDM) - Added IMGFIL option
;			- Added /page10 option
;	15-Jul-97 (MDM) - Made IMGFIL default to flat_005.fits
;-
;
if (n_elements(nmaxdiff) eq 0) then nmaxdiff = 40
if (n_elements(imgfil) eq 0) then imgfil = '/mdisw/dbase/cal/files/flat_005.fits'
if (keyword_set(page10)) then base_addr = '64005000'x else base_addr = '64006000'x
;
qsea = (n_elements(dsets) eq 0)
if (not qsea) then qsea = (dsets(0) eq -1)
if (qsea) then begin
    dsets = [-1, -1]
    list_tfi, infil, info, /quiet
    if (data_type(info) ne 8) then begin
	print, 'No data in that file.  Returning...
	return
    end
    ss = where(info.dpc eq base_addr, nss)
    if (nss ne 0) then dsets(0) = ss(nss-1) else half = 2
    ss = where(info.dpc eq base_addr+9, nss)
    if (nss ne 0) then dsets(1) = ss(nss-1) else half = 1
    if (max(dsets) eq -1) then begin
	dsets = where((info.dpc and 'fffff000'x) eq base_addr)
	half = 0	;19-May-97 MDM
	if (max(dsets) eq -1) then begin
	    print, 'No page 12 dumps.  Returning...'
	    return
        end
    end
    if (min(dsets) eq -1) then dsets = dsets>max(dsets)
end
;
if (n_elements(img2) eq 0) then begin
    read_mdi,infil,dsets,img,h,tfr=tfr
    dattim = ccd_hdr_info(tfr(*,0), 'reftime_str')
    if (n_elements(start_addr) eq 0) then start_addr = 0
    img2=reform(img(start_addr:start_addr+1024*1024L-1),1024,1024)
end

tit0 = 'Flat Field Load'

if (n_elements(img0) eq 0) then begin
    img0=rfits(imgfil)
    ss = circle_mask(img0, 512, 512, 'GT', 520)
    img0(ss)=1.0
    img0=round(img0*16384L) >0 <32767
    diff=img0-img2
    diff(ss) = 0	;don't care outside the mask
end

if (keyword_set(half)) then case half of
    1: diff(589824:*) = 0
    2: diff(0:589823) = 0
end

ss=where(total(diff,1) ne 0, nss)

ss2 = [-99, ss, -99]
ist = 1
for i=1,nss+1 do if (ss2(i-1) ne ss2(i)-1) then begin & print, ss2(ist), ' to', ss2(i-1) & ist = i & end
;
str = ''
for i=1,nss+1 do if (ss2(i-1) ne ss2(i)-1) then begin
    if (ss2(ist) eq ss2(i-1)) then str = [str, strtrim(ss2(ist),2)] $
			else str = [str, strtrim(ss2(ist),2) + "-" + strtrim(ss2(i-1),2)]
    ist = i
end
if (n_elements(str) eq 2) then msubtit2 = ' *** NO BAD PIXEL!! *** ' $
			else msubtit2 = 'Bad Lines: ' + arr2str(str(2:*))

if (n_elements(tit) ne 0) then tit0 = tit + ' ' + tit0
disp_gen, 1, rebin(img2,512,512)/16384., smin=0.8, smax=1.2, nlab=9, tit0, dattim, msubtit2, fmt='(f5.2)', hc=hc

;for i=1,nss+1 do if (ss2(i-1) ne ss2(i)-1) then begin & plots,[520,520],[ss2(ist), ss2(i-1)]/2,/dev & ist = i & end

ss=where(diff ne 0, nss)
if (nss gt nmaxdiff) then begin
    print, 'Too many pixels different to list.  N =', nss
end else begin
    print, '  #   Pix     X, Y       Proper Value     Dump Value     Delta (abs hex)
	   ;  0  145235 851, 141     17593 (44b9)    -21319 (acb9)   38912
    fmt = '(i3, i8, i4,",",i4, 3(4x, i6," (",z4.4,")"))'
    for i=0,nss-1 do print, i, ss(i), ss(i) mod 1024, ss(i)/1024, img0(ss(i)), img0(ss(i)) and 'ffff'x, $
			img2(ss(i)), img2(ss(i)) and 'ffff'x, diff(ss(i)), abs(diff(ss(i))) and 'ffff'x, format=fmt
end
;
end