pro checkip_avail, all, update_utest=update_utest, infil=infil
;
;
;
if (n_elements(infil) eq 0) then infil = concat_dir(all.indir, (str2arr(all.infil))(0))
;
all.test_exist = 0	;start with a fresh slate
;
qdebug = 1
if (qdebug) then print,'CHECKIP_AVAIL: ',infil
infil0   = infil + '_01.tfr'
if (file_exist(infil0)) then begin
    list_tfi, infil, tfi, /quiet
    for i=0,all.ntest_type-1 do begin
	match = all.test_dpc(*,i)
	match = match(uniq(match, sort(match)))
	ss = where(match ne 0)
	if (ss(0) ne -1) then match = match(ss)		;drop of the last optional dpc
	ss = where_arr(tfi.dpc, match, nn)
	if (qdebug) then print, i, '  ', all.test_type(i), nn, all.test_ndp(i), n_elements(tfi)
	if (nn ge 2) then begin
	    v = tfi(ss).dpc
	    uss = uniq(v, sort(v))
	    qok = (n_elements(uss) eq n_elements(match))
	    ;;if (all.test_ndp(i) ne 0) then qok = (n_elements(uss) eq n_elements(match)) and $
	    ;;					(all.test_ndp(i) eq n_elements(tfi))
	    if (all.test_ndp(i) ne 0) then qok = (n_elements(uss) eq n_elements(match)) and $
						(all.test_ndp(i) eq n_elements(where(tfi.dpc eq all.test_dpc(2,i))) )
	    ;stop
	    if (qok) then begin
		all.test_exist(i) = 1
		;;all.test_run(i)   = 1
		test_class = strmid(all.test_type(i), 0, strpos(all.test_type(i),'_'))
		ss2 = where(all.utest eq test_class)
		if ((ss2(0) ne -1) and keyword_set(update_utest)) then begin
		    if (qdebug) then print, 'Setting active for: ', all.utest(ss2)
		    widget_control, all.id_test(ss2), /set_button, set_uvalue=1
		end
		;stop
	    end
	end
    end
end
;
end
;---------------------------------------------------------
pro check1test, type, infil, all, results, ground_data
;
;
;
pf_arr = ['  PASSED', '  FAILED']
test_class = strmid(type, 0, strpos(type,'_'))
;
ii = (where(all.test_type eq type))(0)
;
list_tfi, infil, tfi, /quiet
i1 = (where(tfi.dpc eq all.test_dpc(0,ii)))(0)
i2 = (where(tfi.dpc eq all.test_dpc(1,ii)))(0)
i3 = (where(tfi.dpc eq all.test_dpc(2,ii)))(0)
i4 = (where(tfi.dpc eq all.test_dpc(3,ii)))
i4 = i4(1 < (n_elements(i4)-1) )		;take the second if there are two
dpc_list = arr2str(string(all.test_dpc(*,ii), format='(z8.8)'), delim=', ')
img_list = all.test_img_list(ii)
;
if (min([i1,i3]) eq -1) then begin
    results = [' .... No test data of this type in this file']
    return
end
;
print, '------------- Running ' + type + ' Check ----------------'
;
if (all.test_ndp(ii) eq 0) then begin
    print, 'Reading images', i1, i2, i3, ' and ', i4
    ;
    if (i1 ne -1) then read_mdi, infil, i1, raw_img
    if (i2 ne -1) then read_mdi, infil, i2, list
    if (i3 ne -1) then read_mdi, infil, i3, ip_data
    if (i4 ne -1) then read_mdi, infil, i4, ip_data2
end
;
qok = 1
label = test_class
case 1 of
    (test_class eq 'CROP') or (test_class eq 'LIMB'): begin
		makecrop, ip_data, list, ground_data
		checkcrop, ip_data, raw_img, list, lerr, terr
	    end
    (test_class eq 'RWBIN'): chkrwimg, raw_img, ip_data, list, ground_data, terr
    (test_class eq 'VWBIN'): chkvwimg, raw_img, ip_data, list, ground_data, terr
    (test_class eq 'AWBIN'): chkawimg, raw_img, ip_data, list, ground_data, terr
    (test_class eq 'LRBIN'): chkrblr, raw_img, ip_data, ip_data2, list, ground_data1, ground_data2, terr, label
    (test_class eq 'VLDC'):  chkvldc, infil, img_list, terr, label
    (test_class eq 'FFSCL'): chkffscl, infil, img_list, terr, label
    (test_class eq 'TWAVG'): chktwavg, infil, img_list, terr
    (test_class eq 'ALIGN'): chkalign, raw_img, ip_data, ip_data2, terr, label
    else: begin
		qok = 0
		results = [test_class + ' not recognized.  Please update the routine CHECKIP_SUBS']
	end
endcase
;
if (qok) then begin
    results = ['                        DPC List: ' + dpc_list, $
		strmid(label+'              ', 0, 12) + pf_arr(terr ne 0) + '        #Errors: ' + strtrim(terr,2)]
    ;
    if (all.show_plot) then begin
	wdef, 0, 512, tit='Full raw image'	& tvscl, rebin(raw_img, 512, 512)
	wdef, 1, 512, tit='Reconstructed DP'	& tvscl, rebin(ground_data, 512, 512)
    end
end
;
prstr, results
end