pro tprofiles, index, data, frame=frame, factor=factor, disp_data=disp_data, psym=psym
;+
;NAME:
;	tprofiles
;PURPOSE:
;	Interactively draw time profiles along third dimension of a
;	data cube.
;CALLING SEQUENCE:
;	tprofiles, index, data
;	tprofiles, index, data, frame = 10
;	tprofiles, index, data, disp_data=bdata
;INPUTS:
;	data = data cube
;OPITONAL KEYWORD INPUT:
;	frame	- The frame number in the data cube to display
;	factor	- The rebin factor for displaying the images
;	disp_data- If the input data has been normalized for exposures
;		   for time profiles, then the contrast for displaying the
;		   image is not very good.  It is possible to pass in the
;		   original compressed byte type data to be used for the
;		   display using "disp_data"
;	psym	- plotting symbol.  It must be less than or equal to 7
;		  (cannot use histogram symbol 10).  If not passed, -7 is used
;HISTORY:
;	Written 13-Jul-92 by M.Morrison
;-
;
;
s = size(data)
maxv = max(data) 			;Get extrema
minv = min(data)
nx = s(1)				;Cols in image
ny = s(2)				;Rows in image
nz = s(3)
if ((s(0) ne 3) or ((s(0) eq 3) and (nz eq 1))) then begin
    message, 'Input must be three dimentional and third dimension be greater than 1', /info
    return
end
;
if (n_elements(frame) eq 0) then frame = 0
if (n_elements(factor) eq 0) then factor = fix(512/nx)
if (n_elements(psym) eq 0) then psym = -7
;
print, 'Left mouse button to increment forward the frame to display
print, 'Middle mouse button to access additonal options'
print, '	* change # pixel to bin
print, '	* display different image
print, '	* hardcopy
print, 'Right mouse button to Exit

w_image = !d.window
if (w_image eq -1) then begin
    window, 0, xsiz=nx*factor, ysiz=ny*factor
    w_image = 0
end
window,/free ,xs=640, ys=512,title='T-Profiles' ;Make new window
w_plot = !d.window
wshow, w_image
;
old_font = !p.font			;Use hdw font
!p.font = 0
tickl = 0.1				;Cross length

sumx = 1
sumy = 1
xarr = int2secarr(index)
ref_time = index(0)

wset, w_image
tvcrs,nx/2*factor,ny/2*factor,/dev
;
qnew = 1
while 1 do begin
	wset, w_image		;Image window
	cursor,x,y,2,/dev	;Read position
	ximg = x/factor
	yimg = y/factor
	button = !err

	if (button eq 1) then begin
	    frame = frame + 1
	    if (frame eq nz) then frame = 0
	    qnew = 1
	    repeat cursor,x,y,0,/dev until !err eq 0
	endif

	if (button eq 2) then begin
	    menu = ['Exit menu with no option selected', $
			'Exit TPROFILES', $
			'Select an image to display using time profile curve', $
			'Select a box size/shape for binning', $
			'Hardcopy', $
			'Stop within TPROFILES (for debugging)']
	    imenu = wmenu(menu)
	    case imenu of
		0: ;do nothing
		1: button = 4			;quit
		2: begin
			wset, w_plot
			print, 'Click on the curve for the image to display'
			cursor, xplot, yplot, 1, /data
			dummy = min(abs(xarr-xplot), frame)
			qnew = 1
		   end
		3: begin	;new box size/shape
			print, 'Not available at this time'
		   end
		4: begin	;hardcopy
			print, 'Not available at this time'
		   end
		5: stop
	    endcase
	end

	if (button eq 4) then begin		;Quit
		wset, w_image
		tvcrs,nx/2,ny/2,/dev	;curs to old window
		tvcrs,0			;Invisible
		wdelete, w_plot
		!p.font = old_font
		return
	endif

	wset, w_plot		;Graph window
	if (ximg lt nx) and (yimg lt ny) and (x ge 0) and (y ge 0) then begin		;Draw it
	    tit = 'Profile (image # ' + strtrim(frame,2) + '  ' + fmt_tim(index(frame)) + ' displayed)'
	    if (qnew) then begin
		wset, w_image
		if (keyword_set(disp_data)) then tvscl, rebin(disp_data(*,*,frame), nx*factor, ny*factor, /sample) $
					else tvscl, rebin(data(*,*,frame), nx*factor, ny*factor, /sample)
		wset, w_plot
		utplot, xarr, intarr(nz)+maxv, ref_time, /nodata, title = tit
		qfirst = 1
	    end
	    qnew = 0
	    if (not qfirst) then begin			;Erase?
		plots, xarr, yarr, col=0, psym=psym	;Erase graph
		xyouts,.1,0,/norm,value,col=0		;Erase text
		empty
	    end
	    qfirst = 0

	    yarr = data(ximg, yimg, *)
	    value = strmid(ximg,8,4)+strmid(yimg,8,4) + '  #pixels: ' + strtrim(sumx,2) + 'x' + strtrim(sumy,2)
	    xyouts,.1,0,/norm,value				;Text of locn
	    plots, xarr, yarr, psym=psym			;Graph
	endif
endwhile
end
