pro plot_sdl_sub1, stday, enday, v, vuniq, index, avg, dev, res_arr, exp_arr, avg_arr, dev_arr, n_arr, rbm
;
;
;
;;rbm = index.gen.rbm_status
temp = gt_temp_ccd(index)
percentd = index.sxt.percentd
;
n_arr   = intarr(n_elements(vuniq))
res_arr = intarr(n_elements(vuniq))
exp_arr = fltarr(n_elements(vuniq))
avg_arr = fltarr(n_elements(vuniq))
dev_arr = fltarr(n_elements(vuniq))
for iuniq=0,n_elements(vuniq)-1 do begin
    ss = where( (percentd eq 255) and (rbm eq 0) and (temp lt -20) and $
				(v eq vuniq(iuniq)) and (index.gen.day gt stday) and (index.gen.day le enday), count)
    if (count ne 0) then begin
	res_arr(iuniq) = gt_res(index(ss(0)))
	exp_arr(iuniq) = gt_dpe(index(ss(0)), /conv)
	avg_arr(iuniq) = total(avg(ss)) / count
	dev_arr(iuniq) = total(dev(ss)) / count
	n_arr(iuniq)   = count
    end
end
;
end
;------------------------------------------------------------------------------
;pro plot_sdl, index, data
;
;
;
;	code(0)		- mission time plot for each unique res/dpe - all exposures
;	code(1)		- mission time plot for each unique res/dpe - non-warm and non-SAA
;	code(4) 	- plot tim2fms versus average signal
;	code(5)		- mission time plot #for pixels over a threshold
;
;	code(2)		- signal versus exposure duration for 60 day increments
;	code(3)		- mission time plot of dark current rate
;
if (n_elements(code) eq 0) then code = intarr(10)+1
if (n_elements(sdl) eq 0) then rd_sdl, '1-sep-91', !stime, index, sdl
str_ver = 'PLOT_SDL.PRO  Ver 2.1  18-Dec-92
;
!p.multi = [0,1,2]
n = n_elements(index)
;
if (n_elements(avg) eq 0) then begin
    avg = fltarr(n)
    dev = fltarr(n)
    ;
    vals = sxt_decomp(indgen(256))
    for i=0,n-1 do begin
	npix = total(sdl(i).hist)
	v = sdl(i).hist * vals
	avg0 = total(v)/npix
	tmp = (vals-avg0)
	tmp = tmp^2*sdl(i).hist
	dev0 = sqrt( total(tmp) / (npix-1) )
	avg(i) = avg0
	dev(i) = dev0
    end
end
;
;
;;rbm = index.gen.rbm_status
if (n_elements(rbm) eq 0) then tim2orbit, index, saa=rbm		;modified 12-Nov-92
v = gt_res(index) + index.sxt.explevmode*256
res = gt_res(index)
temp = gt_temp_ccd(index)
percentd = index.sxt.percentd
vuniq = v( uniq(v, sort(v)) )
for iloop=0,1 do if (code(iloop)) then begin			;first loop plot all, second only non-rbm
    for iuniq=0,n_elements(vuniq)+1 do begin
	if (iloop eq 0) then case iuniq of
	    n_elements(vuniq): 		ss = where((percentd eq 255) and (res eq 2), count)
	    n_elements(vuniq)+1:	ss = where((percentd eq 255) and (res eq 1), count)
	    else:			ss = where((percentd eq 255) and (v eq vuniq(iuniq)), count)
	endcase
	if (iloop eq 1) then case iuniq of
	    n_elements(vuniq): 	 ss = where((percentd eq 255) and (res eq 2) and (rbm eq 0) and (temp lt -20), count)
	    n_elements(vuniq)+1: ss = where((percentd eq 255) and (res eq 1) and (rbm eq 0) and (temp lt -20), count)
	    else:		 ss = where((percentd eq 255) and (v eq vuniq(iuniq)) and (rbm eq 0) and (temp lt -20), count)
	endcase
	if (count ge 5) then begin
		if (iloop eq 0) then ss2 = where(rbm(ss) eq 128, count2)
		if (iloop eq 1) then count2 = 0		;don't over plot RBM images
		if (iloop eq 0) then ss3 = where(temp(ss) gt -20, count3)
		if (iloop eq 1) then count3 = 0		;don't over plot RBM images

		tit1 = gt_res(index(ss(0)), /str)
		tit2 = gt_dpe(index(ss(0)), /conv, space=4) + ' msec exposure' 
		tit3 = '  (' + strtrim(n_elements(ss),2) + ' images)'
		tit4 = ' '
		if (iuniq ge n_elements(vuniq)) then tit2 = '   All exposures'
		if (iloop eq 1) then tit4 = ' (excluding RBM and Warm images)'
		title = tit1 + tit2 + tit3 + tit4
		utplot, index(ss), avg(ss), psym=1, title=title, /ynozero, ytitle = 'Average', /year
		if (count2 ge 2) then outplot, index(ss(ss2)), avg(ss(ss2)), psym=2, color=150
		if (count3 ge 2) then outplot, index(ss(ss3)), avg(ss(ss3)), psym=4, color=150
		if ((iloop eq 1) and (iuniq lt n_elements(vuniq))) then begin
		    coeff = poly_fit(int2secarr(index(ss))/86400, avg(ss), 1, yfit)
		    outplot, index(ss), yfit, psym=0
		    xyouts, .70, .50, 'Slope = ' + string(coeff(1))+ ' DN/day', siz=1., /norm
		    xyouts, .70, .53, 'Int =   ' + string(coeff(0))+ ' DN', siz=1., /norm
		end
		utplot, index(ss), dev(ss), psym=1, /ynozer, ytitle = 'Standard Deviation', /year
		color = 150
		if (!d.name eq 'ps') then color=255
		if (count2 ge 2) then outplot, index(ss(ss2)), dev(ss(ss2)), psym=2, color=color
		if (count3 ge 2) then outplot, index(ss(ss3)), avg(ss(ss3)), psym=4, color=color
		plottime, 0, 0, str_ver & plottime & pause

		if (code(4) and (iuniq lt n_elements(vuniq))) then begin
		    tim2orbit, index(ss), tim2fms=tim2fms
		    plot, tim2fms, avg(ss), /ynozer, psym=2, xtit='Minutes since sun rise', ytit='Average', tit=title
		    plot, tim2fms, dev(ss), /ynozer, psym=2, xtit='Minutes since sun rise', ytit='Standard Deviation'
		    plottime, 0, 0, str_ver & plottime & pause
		end

		lprint

		if ((iloop eq 1) and code(5)) then begin		;only do it for non-RBM images
		    levs = [64, 48]
		    if (gt_res(index(ss(0))) eq 2) then levs = [72,80]
		    for j=0,1 do begin
			yy = fltarr(count)
			for i=0,count-1 do yy(i) = total(sdl(ss(i)).hist(levs(j)+1:*))
			utplot, index(ss), yy, psym=1, /ynozero, ytitle = '# pixels over '+strtrim(levs(j),2), title=title, /year
		    end
		    pause
		lprint
		end
	end
    end
end
;
if (code(2) or code(3)) then for ires=1,2 do begin
    !p.multi = [0,1,2]

    imax = max(index.gen.day)
    imin = min(index.gen.day)+1
    index_ref = index(!c)
    ndays = imax-imin-1
    span = 60	;average every "span" days
    en_idays = ndays/span
    if (not code(3)) then en_idays = -1
    for idays=-1,ndays/span do begin
	if (idays eq -1) then begin
	    st_day = imin
	    en_day = imax
	end else begin
	    st_day = imin+idays*span
	    en_day = (st_day + span)<imax
	end
        plot_sdl_sub1, st_day, en_day, v, vuniq, index, avg, dev, res_arr, exp_arr, avg_arr, dev_arr, n_arr, rbm

	ss = where((res_arr eq ires) and (n_arr ne 0), count)
	if (count gt 2) then begin
	    for iloop=0,1 do begin
		x = exp_arr(ss)/1000.
		case  iloop of
			0: begin & y = avg_arr(ss)	& ytit = 'Average' & end
			1: begin & y = dev_arr(ss)	& ytit = 'Standard Deviation' & end
		endcase
		coeff = poly_fit(x, y, 1, yfit)
		tit = gt_res(ires, /str) + ' from ' + gt_day(st_day,/str) + ' to ' + gt_day(en_day, /str) + $
						'  (' + strtrim(en_day-st_day,2) + ' days)'
		plot, x, y, psym=1, title=tit, /ynozero, xtitle = 'Exposure in seconds', ytitle = ytit
		oplot, x, yfit
		xyouts, .70, .50-.4*iloop, 'Slope = ' + string(coeff(1))+ ' DN/sec', siz=1., /norm
		xyouts, .70, .53-.4*iloop, 'Int =   ' + string(coeff(0))+ ' DN', siz=1., /norm

		if ((iloop eq 0) and (idays ge 0)) then begin
		    xrate0 = st_day + (en_day-st_day)/2.
		    yrate0 = coeff(1)
		    if (idays eq 0) then begin
			xrate = xrate0
			yrate = yrate0
		    end else begin
			xrate = [xrate, xrate0]
			yrate = [yrate, yrate0]
		    end
		end
	    end
	    plottime, 0, 0, str_ver
	    plottime
	    pause
		lprint
	end
    end

    if (code(3)) then begin
	!p.multi = 0
	x = (xrate - index_ref.gen.day)*86400
	y = yrate
	coeff = poly_fit(x/86400, y, 1, yfit)

	utplot, x, y, index_ref, psym=2, ytitle='DN/Sec', tit=gt_res(ires,/str), /year
	outplot, x, yfit
	xyouts, .60, .2,  'Slope = ' + string(coeff(1))+ ' (DN/sec)/day', siz=1., /norm
	xyouts, .60, .23, 'Int =   ' + string(coeff(0))+ ' (DN/sec)', siz=1., /norm
	plottime, 0, 0, str_ver
	plottime
	pause
		lprint
    end
end
;
!p.multi = 0
end
