PRO AskValues, axis, minVal, maxVal, pixelMin, pixelMax

  Read_Test,$
	'Where begins the background definition ', $
	minVal, minRead
  Read_Test,$
	'Where does it end ', maxVal, maxRead
  
  IF minRead GE maxRead THEN BEGIN
     pixelMin = MIN( Where( axis LE (minRead > Min( axis )) ))
     pixelMax = MAX( Where( axis GE (maxRead < Max( axis )) )) 
  ENDIF ELSE BEGIN
    pixelMin = MAX( Where( axis LE (minRead > Min( axis )) ))
    pixelMax = MIN( Where( axis GE (maxRead < Max( axis )) )) 
  ENDELSE

END ; ask values


PRO ETHZ_Background, image, x, y, xMin, xMax, yMin, yMax, status
;+
;NAME:
;	ETHZ_Background
;PURPOSE:
;	Image background handling. It starts a menu interface to let the
;       user define which kind of function will determine the 
;       image background.
;CALLING SEQUENCE:
;	ETHZ_Background, image [, xAxis, yAxis 
;		[, xMin, xMax, yMin, yMax ] ]
; INPUT ARGUMENTS
;	image: a 2D array of any type (usually real).
; 	xAxis, yAxis: 1D arrays representing the axes of the image.
;	xMin, xMax, yMin, yMax: scalar index values that indicate 
;               the part of the image that is displayed. They 
;               are used when the background is interactively 
;               displayed on the terminal window. Note, however
;		that the background handling is done on the whole
;		image, and not only on the image part that is displayed.
; OUTPUT ARGUMENT:
;	image: a 2D array of reals, the image with background handled
; SIDE EFFECT:
;	Uses the terminal window to read parameters
;
;MODIFICATION HISTORY:
;	Created in September 1991 by A.Csillaghy
;		Institute of Astronomy, ETH Zurich
;	Automatic background in July 93, A.Cs.
;       Renamed from Background to ETHZ_Background for ssw, ACs, May 98 
;-

status = 'Not Done'

IF N_Params() EQ 0 THEN BEGIN
  Message, 'The image parameter should be given ... ', /INFO, /CONT
  RETURN
ENDIF

nx = N_Elements( image(*,0 ))
ny = N_Elements( image( 0, * ))
IF N_Elements( x ) EQ 0 THEN x = IndGen(nx)
IF N_Elements( y) EQ 0 THEN y = IndGen(ny)
IF N_Elements( xMin ) EQ 0 THEN xMin = 0
IF N_Elements( xMax ) EQ 0 THEN xMax = nx-1
IF N_Elements( yMin ) EQ 0 THEN yMin = 0
IF N_Elements( yMax ) EQ 0 THEN yMax = ny-1

menuLen = 9

backMenu = StrArr(menuLen)

backMenu(0) = 'Constant background ... '
backMenu(1) = 'Minimum envelope ...'
backMenu(2) = 'Gliding background ... '
backMenu(3) = 'Automatic constant background ... '
backMenu(4) = ''
backMenu(5) = ''
backMenu(6) = 'X direction'
backMenu(7) = 'Exit background subtraction menu'
backMenu(8) = 'Redisplay this menu'

direction = 'X' 

  REPEAT BEGIN

    choice = General_Menu( backMenu, $
	'Choose the Background Handling,'+$
	' Change Direction or Abort ' )

    CASE choice OF
      1: BEGIN
            IF direction EQ 'X' THEN $
	AskValues, x, x( xMin ), x( xMax ), pixelMin, pixelMax $
            ELSE $
	AskValues, y, y( yMin ), y( yMax ), pixelMin, pixelMax
            Print, 'Working ...            '
            image = ConstBackSub( image, pixelMin, pixelMax, direction )
            status = 'Done'
         END
      2: BEGIN
             Print, ''
             Print, 'Minimum Envelope Subtraction'
             Print, ' '
             Read_Test, 'Please give the window length (pixels)', 1, wlen, $
	MINIMUM = 1, MAXIMUM= nx*(direction EQ 'X') + $
	ny*(direction EQ 'Y')
             IF wLen GT 1 THEN BEGIN
               Print,'Working ... '
               image = MinEnvBack( image, wLen, direction )
               status = 'Done'
             ENDIF
          END
      3: BEGIN
             Print, ' '
             Print, 'Gliding Background Subtraction'
             Print, ' '
             Read_Test, 'Please give the window length (pixels)', 1, wlen, $
	MINIMUM = 1, MAXIMUM= nx*(direction EQ 'X') + $
	ny*(direction EQ 'Y')
              answ = ''
              Read, 'Is the window weighted ? [n]', answ
              IF StrUpcase( answ ) EQ 'N' OR answ EQ '' THEN $
	weighted = 0 $
              ELSE weighted = 1
              Print,'Working ... '
              image = GlidBackSub( image, wLen, direction, $
	WEIGHTED = weighted )
              status = 'Done'
          END
      4: BEGIN
            Read_Test, 'Enter the percent of profiles to consider ', 0.05, $
	automatic, MIN = 0.0, MAX = 0.99
            Print, 'Working ... '
            image = ConstBackSub( image, AUTOMATIC = automatic )
            status = 'Done'
         END
         5: BEGIN
             Print, ''
             Print,'Subtracting most abundant value in each profile'
             image = MostBackSub( image )
             status = 'Done' 
         END
      menuLen-2: BEGIN
          IF direction EQ 'X' THEN direction = 'Y' ELSE direction = 'X'
          backMenu(6) = direction + ' Direction'
        END
      ELSE:
    ENDCASE

  END UNTIL choice NE menuLen AND choice NE menuLen-2

END
