;+
; NAME:
;	BOX_LC
; PURPOSE:
; 	dump out a time series for an interactively determined box
;	of pixels within a given data cube. Calls STEPPER to present
;	an image to interact with. Does EXP_NORM. Plots the results.
; CALLING SEQUENCE:
;	box_lc, index, timeseries, boxout, data=data, ave=ave, back=back, $
;               not_dn = not_dn, gt_zero = gt_zero, old_cube=old_cube
; INPUT PARAMETERS:
;	data and index file from an spr
; OUTPUT PARAMETERS:
;	time series, in DN/sec, and the box coordinates
; WARNINGS:
;	* single-pixel light curves will only be good for dejittered cubes
;	* in odd-sized cubes composed (e.g.) with mk_mosaic, beware of the
;	  edges - they are counted as zeros!
; KEYWORDS:
;       /data is the data to be input, if not set, then the cube in
;         COMMON Ocube, will be used.
;	/ave gives the time series in DN/sec/pixel (the box average)
; 	/back is the background you want to subtract.
;	/not_dn tells you that this is not SXT data numbers, so you 
;		do not do background subtraction
;	/gt_zero you set all of the pixels less than zero to zero
; SPECIAL TRICKS:
; 	1) there is an invisible Q entry required to stop STEPPER
;       2) lower left first, then upper right please
;	3) right button quits
; HISTORY:
;	Hugh Hudson, Sept. 24, 1992
;       HSH added keyword /ave Sept. 26
;	HSH added multiple box capability Oct. 2
;       HSH added general dimensions Oct. 5
;	jmm switched from exp_norm to dcube_norm, which uses dark 
;		current subtraction for background, feb 27 93
;	jmm added /back and /not_dn keywords Feb 28
;       jmm changed data to a keyword, Mar. 1 93
;	MDM modification to allow /AVE option to work, 21-Apr-93
;	MDM modification to not STOP after exiting, 29-Apr-93
;-
PRO Box_lc, index, ts, boxout, data=data, ave=ave, back=back, not_dn=not_dn, $
            gt_zero=gt_zero
   COMMON Ocube, cube
   IF(KEYWORD_SET(data)) THEN BEGIN
      IF(KEYWORD_SET(not_dn)) THEN cube = data ELSE $
        IF(KEYWORD_SET(back)) THEN cube = dcube_norm(data, index, back) $
        ELSE cube = dcube_norm(data, index)
      IF(KEYWORD_SET(gt_zero)) THEN cube = cube > 0
   ENDIF
   siz = size(cube)
   xedge = siz(1)
   yedge = siz(2)
   depth = siz(3)
   scale = 8.0*(64.0/xedge)
   ts = fltarr(depth, 100) & boxout = intarr(2, 2, 100)
   print, "now starting stepper. hit q when you're ready to make a box."
   window, 20, xs = fix(scale*xedge), ys = fix(scale*yedge)
   stepper, cube, xs = fix(scale*xedge), ys = fix(scale*yedge)
   ii = 0
   print, 'use the left button to mark a box'
   print, 'double click on the center button to terminate'
   cursor, xa, ya, /dev, /down 
   cursor, xb, yb, /dev, /down
   WHILE ii LT 100 DO BEGIN
      x0 = fix(min([xa, xb])/scale) & x1 = fix(max([xa, xb])/scale)
      y0 = fix(min([ya, yb])/scale) & y1 = fix(max([ya, yb])/scale)
      plots, [xa, xa, xb, xb, xa], [ya, yb, yb, ya, ya], /dev 
      number = (x1-x0+1) * (y1-y0+1)
      boxout(*, *, ii) = [[x0, y0], [x1, y1]]
      print, 'box coordinates are ', boxout(*, *, ii)
      print, 'total number of pixels ', number
;;      FOR i = 0, depth-1 DO ts(i, ii) = total(cube(x0:x1, y0:y1, i))
;;      IF KEYWORD_SET(ave) THEN ts(i, ii) = ts(i,ii)/number
      FOR i = 0, depth-1 DO begin		;MDM changed 21-Apr-93
	ts(i, ii) = total(cube(x0:x1, y0:y1, i))
	IF KEYWORD_SET(ave) THEN ts(i, ii) = ts(i,ii)/number
      end
      window, 22, xs = 400, ys = 400
      IF(KEYWORD_SET(ave)) THEN BEGIN
         IF(KEYWORD_SET(not_dn)) THEN ytit = 'avg ' $ 
           ELSE  ytit = 'avg dn/sec'
      ENDIF ELSE BEGIN
         IF(KEYWORD_SET(not_dn)) THEN ytit = 'total ' $ 
           ELSE  ytit = 'total dn/sec'
      ENDELSE
      utplot, index(*), ts(*, ii), psym = -1, ytit = ytit
      wset, 20
      cursor, xa, ya, /dev, /down 
      IF !Err EQ 2 THEN ii = 99
      cursor, xb, yb, /dev, /down
      ii = ii + 1
   ENDWHILE
   ;;stop		;MDM removed 29-Apr-93
   nonzero = where(ts(0, *) NE 0)
   boxout = boxout(*, *, nonzero)
   ts = ts(*, nonzero)
   wdelete, 20 & wdelete, 22
END

