pro check1dump, img, staddr, n, file, out, img0, img1, qplot=qplot, list=list, shift=shift
;
img0 = rfits(concat_dir('$MDI_OPS_LISTS', file), h=h)
if (n_elements(img0) lt 25) then return		;MDM 21-Jul-97
;
nn = n_elements(img0)
if (n ne nn) then begin
    print, 'Having to truncate the list in the OPS database
    img0 = img0(0:n-1)
end
;
img1 = img(staddr:staddr+n-1)
if (keyword_set(shift)) then begin
    if (shift eq 1) then set_list_addr, -1*staddr, img1 $
		    else set_list_addr, -1*shift, img1
end
img1 = fix(img1)
print, 'Now processing: ', file
;
diff = img0-img1
if (keyword_set(list)) then begin
    print, 'Masking out time stamp and non-used portion'
    diff(4:6) = 0	;mask out time stamp
    diff(7:15) = 0	;unused
    if (file eq 'list_rwbin_5005.fits') then diff(168:511) = 0		;MDM added 7-Apr-97 (memory unused)
end
ss = where(diff ne 0, nss)
for i=0,(nss-1)<25 do begin
    ist = (ss(i) - 5) >0
    ien = (ss(i) + 5) < (n_elements(img0)-1)
    print, 'Bad subscript: ', ss(i)
    print, 'From File: ', img0(ist:ien) and 'ffff'x, format='(a, 11z5)'
    print, 'From Dump: ', img1(ist:ien) and 'ffff'x, format='(a, 11z5)'
end
;
if (keyword_set(qplot)) then begin
    !p.multi=[0,1,3]
    ;plot, img0, tit='From reference file in /mdisw/dbase/ops/lists'
    plot, img0, tit='From reference file in ' + file
    plot, img1, tit='From dump
    plot, diff
    pause
end
;
out0 = string(file+'                       ', format='(a25)')
if (nss eq 0) then begin
    out0 = out0 + 'PASSED' 
end else begin
    out0 = out0 + '** FAILED ** ' + strtrim(nss,2) + ' points differ'
end
;
out = [out, out0]
end
;------------------------------------------------------------------
pro check1ff_dump, img, qhalf, out, img0, ssmask, qstop=qstop
;
if (n_elements(img0) eq 0) then begin
    ;img0=rfits('/mdisw/dbase/cal/files/flat_003.fits')
    img0=rfits('/mdisw/dbase/cal/files/flat_005.fits')
    ssmask = circle_mask(img0, 512, 512, 'GT', 520)
    img0(ssmask)=1.0
    img0=round(img0*16384L) >0 <32767
    img0 = reform(img0, n_elements(img0), /overwrite)
end
;
case 1 of
    (qhalf lt 0):  diff = img0 - [img, img, img, img, img]	;stupid way to do it
    ;(qhalf eq 10): diff = img0 - [intarr('60000'x), img('80000'x:*), intarr('90000'x)]
    (qhalf eq 10): diff = img0 - [img('20000'x:*), intarr('90000'x)]
    (qhalf eq 11): diff = img0 - [intarr('70000'x), img]		;'90000'x - '20000'x
    else:          diff = img0 - [img, img]
endcase
diff(ssmask) = 0        ;don't care outside the mask
;
n = n_elements(img)
case qhalf of
   -1: diff(n:*) = 0			;1st 1/5th
   -2: begin & diff(0:n-1)   = 0 & diff(2*n:*)  = 0 & end			;2nd 1/5th
   -3: begin & diff(0:2*n-1) = 0 & diff(3*n:*)  = 0 & end			;3rd 1/5th
   -4: begin & diff(0:3*n-1) = 0 & diff(4*n:*)  = 0 & end			;4th 1/5th
   -5: diff(0:4*n-1) = 0 						;5th 1/5th

    1: diff(n_elements(img):*) = 0	;don't check second half
    2: diff(0:n_elements(img)-1) = 0	;don't check first half

    ;10: begin & diff(0:384*1024L-1) = 0	& diff(576*1024L:*) = 0	& end	;first 384 lines are missing
    10: diff(448*1024L:*) = 0		;last 1/2 or so is missing
    11: diff(0:448*1024L-1) = 0		;first 448 lines are missing
endcase
;
diff = reform(diff,1024,1024,/over)
ss=where(total(diff,1) ne 0, nss)
;
if (nss eq 0) then begin
    out0 = 'Flat Field               PASSED'
end else begin
    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
    out0 = 'Flat Field               FAILED: Bad Lines: ' + arr2str(str(2:*))
    if (keyword_set(qstop)) then stop
end
;
out = [out, out0]
end
;------------------------------------------------------------------
pro check_half_dump, page_dpc, img, out, img0, ssmask, qplot=qplot, qstop=qstop, do10=do10
;
qdo10 = keyword_set(do10)
;
case page_dpc of
  '64005000'x: if (qdo10) then check1ff_dump, img, 10, out, img_ff, ssmask, qstop=qstop
  '64005009'x: if (qdo10) then check1ff_dump, img, 11, out, img_ff, ssmask, qstop=qstop
  '64006000'x: check1ff_dump, img, 1, out, img_ff, ssmask, qstop=qstop
  '64006009'x: check1ff_dump, img, 2, out, img_ff, ssmask, qstop=qstop
  '64007000'x: begin
      check1dump, img, '00000'x, 3044, 'list_crop_0fc0.fits',  out, img0, img1, qplot=qplot, /list
      check1dump, img, '11000'x, 25444,'list_rwbin_5005.fits', out, img0, img1, qplot=qplot, /list, /shift
      check1dump, img, '30000'x, 15312,'list_irbin_7007.fits', out, img0, img1, qplot=qplot, /list, /shift
      check1dump, img, '34000'x, 15312,'list_irbin_7005.fits', out, img0, img1, qplot=qplot, /list, /shift
      ;check1dump, img, '40000'x, 52580,'list_vwbin_6007.fits', out, img0, img1, qplot=qplot, /list, /shift
      check1dump, img, '40000'x, 52580,'list_vwbin_6007a.fits', out, img0, img1, qplot=qplot, /list
      check1dump, img, '50000'x, 2900, 'list_crop_0f00.fits',  out, img0, img1, qplot=qplot, /list
    end 
  '64007009'x:
  '64008000'x: begin
      check1dump, img, '28000'x, 32768, 'lookup_velocity2.fits', out, qplot=qplot 
      check1dump, img, '80000'x, 32768, 'lookup_sqrt2.fits', out, qplot=qplot  
      check1dump, img, '70000'x, 16384, 'lookup_sqrt3.fits', out, qplot=qplot  
      check1dump, img, '60000'x, 16384, 'lookup_sqrt4.fits', out, qplot=qplot  
    end 
  '64008009'x: begin
      check1dump, img, '90000'x-'90000'x, 16383, 'lookup_sqrt1.fits', out, qplot=qplot  
      check1dump, img, '98000'x-'90000'x, 32768, 'lookup_recip1.fits', out, qplot=qplot
      ;check1dump, img, 'b0000'x-'90000'x, 32768, 'lookup_velocity1.fits', out, qplot=qplot  
    end 
  '65006000'x: check1ff_dump, img, -1, out, img_ff, ssmask, qstop=qstop
  '65006003'x: check1ff_dump, img, -2, out, img_ff, ssmask, qstop=qstop
  '65006007'x: check1ff_dump, img, -3, out, img_ff, ssmask, qstop=qstop
  '6500600a'x: check1ff_dump, img, -4, out, img_ff, ssmask, qstop=qstop
  '6500600e'x: check1ff_dump, img, -5, out, img_ff, ssmask, qstop=qstop
  '65007000'x: begin
      check1dump, img, '00000'x, 3044, 'list_crop_0fc0.fits',  out, img0, img1, qplot=qplot, /list
      check1dump, img, '11000'x, 25444,'list_rwbin_5005.fits', out, img0, img1, qplot=qplot, /list, /shift
      check1dump, img, '30000'x, 15312,'list_irbin_7007.fits', out, img0, img1, qplot=qplot, /list, /shift
      check1dump, img, '34000'x, 15312,'list_irbin_7005.fits', out, img0, img1, qplot=qplot, /list, /shift
    end 
  '65007003'x: begin
      ;check1dump, img, '40000'x-'39999'x, 52580,'list_vwbin_6007.fits', out, img0, img1, qplot=qplot, /list, shift='40000'x
      check1dump, img, '40000'x-'39999'x, 52580,'list_vwbin_6007a.fits', out, img0, img1, qplot=qplot, /list
      check1dump, img, '50000'x-'39999'x, 2900, 'list_crop_0f00.fits',  out, img0, img1, qplot=qplot, /list
    end 
  '65008000'x: check1dump, img, '28000'x, 32768, 'lookup_velocity2.fits', out, qplot=qplot 
  '65008003'x: check1dump, img, '60000'x-'39999'x, 16384, 'lookup_sqrt4.fits', out, qplot=qplot  
  '65008007'x: begin
      ;check1dump, img, '70000'x-'77332'x, 16384, 'lookup_sqrt3.fits', out, qplot=qplot  
      check1dump, img, '80000'x-'77332'x, 32768, 'lookup_sqrt2.fits', out, qplot=qplot  
      check1dump, img, '98000'x-'77332'x, 32768, 'lookup_recip1.fits', out, qplot=qplot
      check1dump, img, '90000'x-'77332'x, 16383, 'lookup_sqrt1.fits', out, qplot=qplot  
    end 
endcase

end
;------------------------------------------------------------------
pro check_dumps, infil, dset, out, qplot=qplot, qstop=qstop, outfil=outfil, do10=do10
;+
;NAME:
;	check_dumps
;PURPOSE:
;	To check the dumps of IP tables and list pages
;SAMPLE CALLING SEQUENCE:
;	check_dumps, infil, dset, page
;	check_dumps, '960713_000014', [43,44], 16
;	infil = '/data13/data_eof/sci160k/960522/960522_003057'
;	check_dumps, infil, [124,140], 14, /qplot
;HISTORY:
;	Written 15-Jul-96 by M.Morrison
;	16-Jul-96 (MDM) - Added check for page 14
;	 6-Aug-96 (MDM) - REvamped a bunch
;			- Added page 12 (flat field) checks
;	16-Aug-96 (MDM) - Added selection code if "dset" is not defined
;	27-Aug-96 (MDM) - Removed "velocity1" from the check
;	15-Nov-96 (MDM) - Added checking 6500xxxx dumps.
;	13-Mar-97 (MDM) - Added checking of page 10 dumps (FF)
;	 7-Apr-97 (MDM) - Commented out page 10 dump checks (errors too often)
;			- Masked out more of RWBIN memory checks
;			- Changed length of RWBIN and VWBIN
;	11-Apr-97 (MDM) - Added /DO10 option to check page 10 dumps
;			- Fixed up option for page 10 dumps checks
;	15-Jul-97 (MDM) - Changed to use flat_005 instead of flat_003.fits
;	20-Oct-98 (CNF) - Changed number of pixels it would list from 10 to
;			  25. 
;	 3-dec-98 (CNF) - Modified program to read new vwbin 6007a to fix
;			  bad pixel 400.
;	 1-Oct-99 (RIB) - Modified program to work with IDL 5.2, primarily
;                         correcting dpc hex comparisons 
;-
;
if (n_elements(dset) eq 0) then begin
    list_tfi, infil, info, /quiet
    nss = 0
    if (data_type(info) eq 8) then dset = $
       where( ((info.dpc and 'ffff0000'x) eq '64000000'x) or $
	      ((info.dpc and 'ffff0000'x) eq '65000000'x), nss)
    if (nss eq 0) then begin
	print, 'No memory dump data products found in file: ' + infil
	return
    end
end
;
out = ''
;
for i=0,n_elements(dset)-1 do begin
    read_mdi, infil, dset(i), img, h
    dpc = sxpar(h, 'dpc')
    dattim = sxpar(h(*,0), 'date-obs') + ' ' + sxpar(h(*,0), 'time-obs')
    fid = ex2fid( anytim2ex(dattim))
    dpcstr = string(dpc, format='(z8.0)')
    pre = fid + ' ' + strmid(dpcstr,4,4)
    out0 = ''
    check_half_dump, dpc, img, out0, img0, maskss, qplot=qplot, do10=do10
    prstr, out0
    if (n_elements(out0) gt 1) then out = [out, pre + ' ' + out0(1:*)]
end
;
break_file, infil, dsk_log, dir, filnam
prstr, out
if (keyword_set(outfil)) then prstr, out, file=outfil
if (keyword_set(qstop)) then stop
end
