PRO  STEPPER_S1, DATA, $
		XSIZE=XSIZE, YSIZE=YSIZE, START=START, $
		INFO_ARRAY=INFO_ARRAY,$
		NOSCALE=NOSCALE, SUBSCRIPT=SUBSCRIPT, MOVIE=MOVIE, INTERP=INTERP, $
		COLOR=COLOR, LASTSS=LASTSS, $
		index=index, panels=panels, nolcur=nolcur, nonormalize=nonormalize
;+
;  NAME:
;	STEPPER
;  PURPOSE:
;	Step through individual images a single frame at a time.
;  INPUTS:
;	DATA = Three-dimensional array
;  OPTIONAL INPUT KEYWORDS:
;	XSIZE,YSIZE = If present, the routine will use rebin to using the
;			the /sample option.  If only XSIZE is present, YSIZE
;			will be set equal to XSIZE.
;	START	    = Index of starting image
;	INFO_ARRAY  = String array containing descriptive text
;	NOSCALE	    = If set, will turn off tvscale
;	SUBSCRIPT   = Subset of array is displayed.
;	MOVIE	    = If present and =1, will initiate movie mode when called
; 	INTERP      = Controls how REBIN is done.  If present and set to 1,
;		      /INTERP ==> REBIN(A,xsize,ysize)
;		      else    ==> REBIN(A,xsize,ysize,/sample)
;	COLOR	    = The color to use for displaying the info text string.
;       nolcur  - If set, then stepper is being called from LCUR_IMAGE and
;                 it should not allow the LCUR option to be used
;  VERSION:
;	V1.2    17-NOV-92
;  HISTORY:
;	Written 20-sep-91, JRL and LWA
;	Updated 21-sep-91, JRL:  Added xloadct and zoom options.
;	Updated 24-sep-91, JRL:  Added SUBSCRIPT and MOVIE options.
;	Updated  2-dec-91, JRL:  Break the text string into two lines if
;				 info_array is > 44
;	Updated 15-dec-91, slf;  Replaced get_kbrd calls with get_kbrd2
;				 to work around SGI anomoly
;	Updated 16-apr-92, slf;  To work with single image
;	Updated 28-apr-92, JRL:  Break text if strlen(info) > 38
;	Updated 29-apr-92, JRL:  Added the INTERP keyword
;	Updated 17-nov-92, JRL:  Added color keyword.  Allow info_array to
;				 be a 2nd parameter
;	Updated 15-mar-93, JRL:  Added an option to call profiles
;	Updated 13-may-93, SLF;  Return last selected in lastss
;	------------------------------------------------------------
;       17-Oct-93 (MDM) -  Broke the routine into two parts
;                                Allowed new options
;                                       "g" will plot sxt grids
;                                       "l" will call LCUR_PLOT to plot curves
;                                       "h" will make hardcopies
;                                       "c" will call loadct
;                                       different panels for different filters
;	19-Oct-93 (MDM) - Allowed exit from STEPPER when using q to exit movie
;	25-Oct-93 (MDM) - Added passing NOSCALE parameter to LCUR_IMAGE
;	29-Nov-93 (MDM) - Added NOLCUR option
;			- Added /NORMALIZE to LCUR_IMAGE call
;	10-Jan-94 (MDM) - Added "O" option - obs region location
;	11-Jan-94 (MDM) - Corrected "O" option
;	21-Feb-94 (MDM) - Added "W" option - calls WSHOW to pop the window
;			- Replaced call to GET_KBRD2 with GET_KBRD
;	18-Apr-94 (MDM) - Added /NODEL_WIN_P to not delete window after light
;			  curve plot is made
;	10-Jun-94 (MDM) - Added option to establish the breakpoint location
;			  in the info_array by setting STEPPER_BREAK
;	24-Aug-94 (MDM) - Added NONORMALIZE option
;	15-Jan-02, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;		xloadct did not work (tested on Linux, IDL 5.3 and Windows NT,
;		IDL 5.5). Adding keyword /block fixed the problem. Assuming that
;		widget blocking is the problem and since blocking was introduced
;		in IDL 5.0, /block is now added to xloadct when
;		!version.release >= 5.0
;-
on_error,2		; Return to caller
ans= ''

if n_elements(color) eq 0 then color=255	; Default color
ss = size(DATA)

; slf 26-apr-92, Allow single image in addition to data cube
case ss(0) of
   2: begin
         nx=ss(1) & ny=ss(2) & nimg=1
      endcase
   3: begin
         nx = ss(1) & ny = ss(2) & nimg = ss(3)
      endcase
   else: begin & message,'Image or Image cube required' & return & endelse
endcase

if (n_elements(subscript) eq 0) then subscript = indgen(nimg)
if (n_elements(panels) eq 0) then begin
    qpanels = 0
    panels = intarr(nimg)
end else begin
    qpanels = 1		;doing panel plots
end
n_sel = n_elements(subscript)
;
npan = fix(sqrt(max(panels)+1))
if (npan^2 ne max(panels)+1) then npan = npan + 1

;  See if we need to rebing or not

if (n_elements(xsize) eq 0) then begin
    rebinnn = 0
    xsize = nx
end else begin
    rebinnn = 1				; Must rebin
    if n_elements(ysize) eq 0 then ysize = fix(xsize/float(nx)*ny)
    print,'Will rebin to XSIZE=',strtrim(string(xsize),2),  $
		    ', YSIZE=',strtrim(string(ysize),2)
    if keyword_set(interp) then sample=0 else sample=1	; if interp=1, sample=0
endelse
if (n_elements(ysize) eq 0) then ysize = ny

;  See if we should turn off the tvscl option:
if keyword_set(noscale) then nosc = 0 else nosc = 2

DISP = rebinnn + nosc

;  Set up info_array if it does not exist already:

if n_elements(info_array) eq 0 then begin
    if n_elements(info_p) eq 0 then 		$
		info_array = strtrim(string(indgen(nimg))+' :',2) $
		else info_array = info_p
endif

if n_elements(start) eq 0 then stt=0 else stt=start	; Starting image
stt = min(abs(stt-subscript)) & stt=!c & !c=0		; Get closest index
i = stt
;
qmovie = keyword_set(movie)
qdisp = 1
qprint = 1	;always print the menu message the first time
qdone = 0
qfirst = 1
while not qdone do begin
    if (qfirst or qprint or (ans eq '?')) then begin
	print, '-------- STEPPER Options --------'
	print,'There are ',strtrim(string(nimg),2),' images in the array'
	print,'There are ',strtrim(string(n_sel),2),' images selected'
               ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	print, 'Enter "b" to step backwards                 "h" to call HARDCOPY
	print, '      "s" to select new start index         "z" for ZOOM
	print, '      "c" to call LOADCT                    "x" for XLOADCT
	if (not qpanels) then print, '      "p" to call the IDL PROFILES routine  "l" to call light curve routine
	print, '      "g" to overlay SXT solar grid         "o" for observing region location
	print, '      "q" to quit                           "m" for movie mode
	print, '      "?" to display this help menu         "anything else" to step forward
    end
    qprint = 0

    if (not qfirst) then ans = get_kbrd(1-qmovie)
    ans = strlowcase(ans)				; Convert to lower case

    if (qmovie) and (ans ne '') then begin
	qmovie = 0
	qdisp = 0
	;;ans = '-'		;zero out the answer which stopped movie (might be "q")
    end

    qdisp = qfirst
    case ans of
	'-': ;do nothing
	'm': begin		;movie mode selected
		print,'Type anything to abort movie mode ("q" will exit movie and STEPPER)'
		qmovie = 1
		qdisp = 1
	     end
	'b': begin
		i = i-1
		if i lt 0 then i=n_sel-1
		qdisp = 1
	     end
	'q': qdone = 1
	's': begin
		print,'* Enter the requested start image number',format='($,a)'
		read,i
		i=min(abs(i-subscript))
		i=!c
		!c=0
		qdisp = 1
	     end
	'x': begin

		if !version.release lt 5.0 then	$
			xloadct						$
		else							$
			xloadct, /block

		end
	'w': wshow
	'c': begin
		input, 'Enter the new color table number (0-37)', icol, 0
		loadct, icol
	     end
	'z': begin
		zoom
		qprint = 1
	     end
	'p': begin
		if (qpanels) then begin
		    tbeep, 3
		    print, 'STEPPER: You cannot use profiles option when using panels option'
		end else begin
		    if rebinnn then profiles,rebin(data(*,*,j),xsize,ysize,sample=sample)	$
			else profiles,data(*,*,j)
		    qprint = 1
		end
	     end
	'g': begin
		if (qpanels) then begin
		    tbeep, 3
		    print, 'STEPPER: You cannot use grid option when using panels option'
		end else begin
		    if (data_type(index) eq 8) then begin
			sxt_grid, index(j), mag=xsize/nx
		    end else begin
			tbeep, 3
			print, 'You must pass the index in in order to use the grid option'
		    end
		end
	     end
	'o': begin
		if (qpanels) then begin
		    tbeep, 3
		    print, 'STEPPER: You cannot use OR location option when using panels option'
		end else begin
		    if (data_type(index) eq 8) then begin
			if (gt_pfi_ffi(index(j), /ffi)) then begin
			   pfi_loc, index(j), bin=bin
			end else begin
			    print, 'STEPPER: OR location option only works with full frame images'
			end
		    end else begin
			tbeep, 3
			print, 'You must pass the index in in order to use the OR location option'
		    end
		end
	     end
	'l': begin
		if (qpanels) then begin
		    tbeep, 3
		    print, 'STEPPER: You cannot use light curve option when using panels option'
		end else begin
		    if ( (data_type(index) eq 8) and (not keyword_set(nolcur)) ) then begin
			lcur_image, index, data, subscript=subscript, xsize=xsize, /nodisplay, $
					lastss=j, noscale=noscale, /norm, /nodel_win_p, nonormalize=nonormalize
			qprint = 1
		    end else begin
			tbeep, 3
			if (keyword_set(nolcur)) then print, 'Exit STEPPER to return to the LCUR_IMAGE routine' $
						else print, 'You must pass the index in in order to use the light curve option'
		    end
		end
	     end
	'h': begin
		hardcopy
		qprint = 1
	     end
	'*': stop

	else: if (not qfirst) then begin
		i = i + 1
		if i ge n_sel then i = 0
		qdisp = 1
	      end
     endcase

    if (qdisp) then begin
	j = subscript(i)				; Get selected images
	bin = xsize / float(n_elements(data(*,0,0)))

	;;if (!d.window ne -1) then wshow
	x0 = (panels(j) mod npan) * xsize
	y0 = (panels(j)/npan)     * ysize
        case DISP of 							; rebinn,noscale
	  0: tv,data(*,*,j), x0, y0					;    0  ,   0
	  1: tv,rebin(data(*,*,j),xsize,ysize,sample=sample), x0, y0	;    1  ,   0
	  2: tvscl,data(*,*,j), x0, y0					;    0  ,   2
	  3: tvscl,rebin(data(*,*,j),xsize,ysize,sample=sample), x0, y0	;    1  ,   2
	endcase

	break_point = 38	; Break point
	if (get_logenv('STEPPER_BREAK') ne '') then break_point = fix(getenv('STEPPER_BREAK'))
	if strlen(info_array(j)) le break_point then begin
	    xyouts,x0,y0+10,info_array(j),charsize = 1.26,/device,color=color
	endif else begin
	    str0 = strmid(info_array(j),0,break_point)
	    str1 = strmid(info_array(j),break_point,strlen(info_array(j)))
	    xyouts,x0+10,y0+10,str1,charsize = 1.26,/device,color=color
	    xyouts,   x0,y0+23,str0,charsize = 1.26,/device,color=color
	endelse
    end

    qfirst = 0
end

lastss=j		; output current (last) subscript

return
end
