; NAME:
;       pro viewslope.pro (v1.0)
;
; PURPOSE:
;		This program displays an image of the last minus first planes in a cube and
; 		allows the user to move the mouse over the image and display a plot of that
;		pixel's values in another window.
;
; CALLING SEQUENCE:
;       viewslope,dataarray
;
; INPUTS:
;       dataarray	- 3-D data cube
;
; KEYWORD PARAMETERS:

; EXAMPLE
;
;		Display a portion of myimage.fits and allow interactive plots
;
;		IDL> x=readpiece('myimage.fits',[21,500,30,509])
;		IDL> viewslope,x
;
; REFERENCE:
;
;
; MODIFICATION HISTORY:
;       Written by:	Eddie Bergeron & Don Figer, IDTL, April 8, 2003
;-

pro viewslope,dataarray
	;
	; interactive procedure to view pixel values in data cubes as a plot of signal versus read number
	;

	; display image of last read minus first read
;	plot,[0],[0],xrange=[0,n_elements(dataarray(*,0,0))],yrange=[0,n_elements(dataarray(0,*,0))],/nodata,xstyle=1,ystyle=1
;	tv,bytscl(dataarray(*,*,n_elements(dataarray(0,0,*))-1)-dataarray(*,*,0))
	displaysigma=0.1
	display,dataarray(*,*,n_elements(dataarray(0,0,*))-1)-dataarray(*,*,0),min=median(dataarray(*,*,n_elements(dataarray(0,0,*))-1)-dataarray(*,*,0))-displaysigma*stddev(dataarray(*,*,n_elements(dataarray(0,0,*))-1)-dataarray(*,*,0)), $
		max=median(dataarray(*,*,n_elements(dataarray(0,0,*))-1)-dataarray(*,*,0))+displaysigma*stddev(dataarray(*,*,n_elements(dataarray(0,0,*))-1)-dataarray(*,*,0))
	xcoords = !x.crange & ycoords = !y.crange ; save coordinates to return to this coord system

	print,'select a pixel, then click on the left mouse button'
	print,' click on the right mouse button to end'
	cursor,xpixel,ypixel,/down,/data
    xcur = round(xpixel) & ycur = round(ypixel)
	window,1

	; continuously draw plot of pixel values as mouse is moved over various pixels
	while !err ne 4 do begin
		wset,1
		plot,float(dataarray(xcur,ycur,*))-float(dataarray(xcur,ycur,0)),xtitle='Read Number',ytitle='Signal'
		xyouts,[(!X.CRANGE(1)-!X.CRANGE(0))*0.7+!X.CRANGE(0)],[(!Y.CRANGE(1)-!Y.CRANGE(0))*0.7+!Y.CRANGE(0)],['pixel = ('+strtrim(string(fix(xcur)),2)+','+strtrim(string(fix(ycur)),2)+')']
		wset,0
		plot,xcoords,ycoords,xstyle=5,ystyle=5,/noerase,/nodata ; return to display coordinate system
		cursor,xpixel,ypixel,/data,/change
		xcur = round(xpixel) & ycur = round(ypixel)
	endwhile

end



