;+
; NAME:
;	Modify Image Pixels
; PURPOSE:
;	User interface to modify image pixels. It allows
;	to choose interactively the operation to be performed.
; CATEGORY:
;	Image Processing
; CALLING SEQUENCE
;	Modify, image [, xAxis, yAxis, $
;		[ xMin, xmax, yMin, yMax, [ name ]]]
; INPUT PARAMETERS:
;	image:  a two-dimensionnal real array
;	xAxis, yAxis: the image (real) axes values. When not 
;		present, they are considered to be equal to the
;		pixel indices.
;	xMin, xMax, yMin, yMax: the index boundaries of the
;		image. They are important e.g. in Fourier
;		transformation or cleaning processes when
;		operations are not performed on the whole
;		image. However, mathematical operations,
;		e.g. exponentiation, logarithmization
;		are done on the whole image, independently
;		of these parameters. When not present,
;		default are set to the lowest and highest pixels
;		in x- and y-direction.
;	name: the name of the image
; OUTPUT PARAMETERS:
; 	image: the modified two-dimensionnal array.
;	xAxis, yAxis: the axes modified by some of the options. 
; SIDE EFFECTS:
;	Use the terminal window to read values.
; RESTICTIONS: User Interface: works only interactively.
;
; MODIFICATION HISTORY:
; 	Created: May 1991, A. Csillaghy
;	Options added in December, 1991 A.Cs
;	Cross Correlation in Oct 92, A.Cs
;	1d Crosscorrelation in March 93, AC/SK
;	Wiener filter in March 93, AC
;       Background renamed to ETHZ_background for ssw May 98 ACs
;	PSH, 2004/04/16: added "Phoenix recalibrate" and "Callisto recalibrate" buttons.
;-

FUNCTION RAGExp, image

;+
; NAME:
;	RAG Exponentiation
; PURPOSE:
;	exponentiates the image with parameters asked
;	interactively. If no values are entered, the defaults
;	values for SFU decompression are used.
; CALLING SEQUENCE:
;	result = RAGExp( array )
; INPUT: 
;	array: any parameter but a string ( normally 2D image )
; RESULT:
;	result = a exp( b * input + c + d )
; RESTRICTION
;	works only after compilation of modify.pro
;-

  Print, "Exponentiation format : z' = a exp( b z + c ) + d"
  Read_Test,' Enter a ', 1, a
  Read_Test,' Enter b ', .0512, b
  Read_Test,' Enter c ', 0, c
  Read_Test,' Enter d ', -10, d
  
  RETURN, a* Exp( b*image + c ) + d

END ; RagExp


FUNCTION RAGLog, image

;+
; NAME:
;	RAG Log
; PURPOSE:
;	compacts the image with parameters asked
;	interactively. If no values are entered, the defaults
;	values for SFU compression are used. Negative values
;	are replaced by a constant ( 1e-7).
; CALLING SEQUENCE:
;	result = RAGLog( array )
; INPUT: 
;	array: any parameter but a string ( normally 2D image )
; RESULT:
;	result = a log( b * input + c + d ) (log basis e)
; RESTRICTION:
;	works only after compilation of modify.pro
;-

	
  eps = 1e-7

  Print, "Logarithm format : z' = a log( b z + c ) + d"
  Print, ''
  Read_Test,' Enter a ', 19.54, a
  Read_Test,' Enter b ', 1, b
  Read_Test,' Enter c ', 10, c
  Read_Test,' Enter d ', 0, d
  
  im = b*image + c
  nbPix = Total( im LT eps ) NE 0
  IF  nbPix  THEN  Print, $
	'WARNING: transformation not done for ' $
	+ String( nbPix ) + ' pixels '

  RETURN, a* ALog( im > eps ) + d


END ; Rag Log


PRO FourierTransf, image, xAxis, yAxis, xMin, xMax, yMin, ymax, $
	 name, status

  imToTransf = image( xMin:xMax, yMin:yMax)
  Change_Selection, imi, xAxisi, yAxisi, xMini, xMaxi, yMini, yMaxi,$
	STATUS = status, TEXT = 'Choose the imaginary image' $
	+' or exit (real image to transform)'
  IF status EQ 'Done' THEN BEGIN
    IF (xMaxi-xMini NE xMax-xMin) THEN BEGIN
       Print, 'Real and Imaginary Images have not the same X size ', $
    	STRING(7B)
       Print, 'The smallest X size is taken into account'
    ENDIF
    IF (yMaxi-yMini NE yMax-yMin) THEN BEGIN
       Print, 'Real and Imaginary Images have not the same Y size ', $
    	STRING(7B)
       Print, 'The smallest Y size is taken into account'
    ENDIF
    imToTransf = Complex( imToTransf, imi )
  ENDIF

                    
  image = Fourier( imToTransf, STATUS = status )
  IF status EQ 'Done' THEN BEGIN 
    siz = Size(image)
    nx = xMax - xMin + 1 
    ny = yMax - yMin + 1
    xAxis = IndGen( nx ) & yAxis = IndGen( ny )
    xMin = 0 & yMin = 0
    xMax = nx-1 & yMax = ny-1
    IF siz(siz(0)+1) EQ 6 THEN BEGIN
      Show_Image, BytScl( Imaginary(image) ), xAxis, yAxis, $
	/SIZES_ON, WINNR = GetWNr()
       Put_Selection, Imaginary(image), xAxis, yAxis, $
	0, nx-1, 0, ny-1, $
	FILESTATE = "temporary", FNAME = name + '_im'
       image = FLOAT( image )
     ENDIF
  ENDIF

END ; Fourier transformation


FUNCTION Arithmetics, image

Print, ''
Print, "Arithemtics: z' = a*z^b + c "
Print, ''
Read_Test, ' Enter a ', 1, a
Read_Test, ' Enter b ', 1, b
Read_Test, ' Enter c ', 0, c

RETURN, a* image^b + c

END                             ;  Arithmetics 


PRO MathFunTra, image, oper, status

  len = 5

  mMenu = StrArr( len )

  mMenu( 0 ) = 'Exponentiation ... '
  mMenu( 1 ) = 'Logarithmization ... '
  mMenu( 2 ) = 'Arithmetics ... '
  mMenu( 3 ) = 'Exit This Menu'
  mMenu( 4 ) = 'Redisplay This Menu '
  
  status = 'Not Done'

  REPEAT BEGIN

   choice=General_Menu(mMenu, $
	'Mathematical Operations: Choose Operation or Exit')

    CASE choice OF
      1: BEGIN 
            image = RAGExp( image )
            oper = 'exp' & status = 'Done'
          END
      2: BEGIN
            image = RAGLog( image )
            oper = 'log' & status = 'Done'
          END
      3: BEGIN
            image = Arithmetics( image )
            oper = 'a' & status = 'Done'
          END
      ELSE:
    ENDCASE

  END UNTIL choice NE len

END ; mathematical operations


PRO MathFunGen, image, xAxis, yAxis, xMin, xMax, yMin, yMax, $
	nx, ny, name, status, oper

  len = 8

  nMenu = StrArr( len )
  
  nMenu(0) = 'Fourier transformations ... '
  nMenu(1) = 'Derivation'
  nMenu(2) = 'Autocorrelation ... '
  nMenu(3) = 'Cross correlation'
  nMenu(4) = '1D cross correlation'
  nMenu(5) = 'Spline interpolation ... '
  nMenu(6) = 'Exit this menu'
  nMenu(7) = 'Redisplay this menu'

  status = 'Not Done '

  REPEAT BEGIN

    choice = General_Menu( nMenu, 'Choose Operation or Exit' )

    CASE choice OF
      1:BEGIN
            FourierTransf, image, xAxis, yAxis, xMin, xMax, $
	yMin, yMax, name, status
            oper = 'f'
          END
      2:BEGIN
            direction = ''
            Read,' Derivation in X or in Y ? (X) ', direction
            IF StrUpcase( direction ) NE 'Y' THEN $
              FOR i = 0, ny-1 DO image(*,i) = Deriv( xAxis, image(*,i) )  $
            ELSE FOR i = 0, nx-1 DO $
              image(i,*) = Deriv( yAxis, image(i,*) )
            status = 'Done' & oper = 'der'
          END
      3:BEGIN
            nx = xMax-xMin+1
            ny = yMax-yMin+1
            answ = ''
            Read_Test,' Enter the lag in x: ' ,nx, xLag
            Read_Test,' Enter the lag in y: ', ny, yLag
            Read, ' Is the Wiener-Khintchin Method to be used (n) ? ', answ
            IF StrUpcase( answ ) NE 'Y' THEN fast = 0 ELSE fast = 1
            Read,' Is the result normalized (y) ? ', answ
            IF StrUpcase( answ ) NE 'N' THEN norm = 1 ELSE norm = 0
            image = AutoCorr( image(xMin:xMax, yMin:yMax), $
	XLAGMAX = xLag, YLAGMAX = yLag, FAST = fast, $
	 NORMALIZED = norm )
            xAxis = IndGen( xLag+1 )
            yAxis = IndGen( yLag+1 )
            xMin = 0 & yMin = 0
            xMax = xLag & yMax = yLag
            status = 'Done' & oper = 'ac'
          END
      4: BEGIN
            nx = xMax-xMin+1
            ny = yMax-yMin+1
            answ = ''
            Read_Test,' Enter the lag in x: ' ,nx-1, xLag
            Read_Test,' Enter the lag in y: ', ny-1, yLag
            Read_Test,' Enter the offset  in x: ' ,0, xOffset
            Read_Test,' Enter the offset  in y: ', 0, yOffset
            Change_Selection, image2, xAxis2, yAxis2, xMin2, xMax2, $
	yMin2, yMax2, STATUS = status
            IF status EQ 'Not Done' THEN RETURN $
            ELSE status = 'Not Done'
            Print, 'Working ... '
            image = Correl_Images( image(xMin:xMax, yMin:yMax), $
	image2(xMin2: xMax2, yMin2: yMax2), $
	XSHIFT = xLag, YSHIFT = yLag, $
 	XOFFSET = xOffset, YOFFSET = yOffset )
            xAxis = IndGen( xLag*2+1 ) - xLag
            yAxis = IndGen( yLag*2+1 ) - yLag
            xMin = 0 & yMin = 0
            xMax = xLag*2 & yMax = yLag*2
            status = 'Done' & oper = 'cc'
	    PutHMS, 0
          END
      5: BEGIN
            print
            print
            Read_Test,' Enter the reference y-value: ' ,yAxis(yMin), yRef
            ref=yref
            IF yAxis(0) LE yAxis(ny-1) THEN BEGIN
              IF abs(yAxis(Min(Where( yAxis GE yRef )))-yRef) LE $
                 abs(yAxis(Max(Where( yAxis LE yRef )))-yRef) THEN $
                 yRef = Min(Where( yAxis GE yRef )) ELSE $
                 yRef = Max(Where( yAxis LE yRef ))
            ENDIF ELSE BEGIN
              IF abs(yAxis(Max(Where( yAxis GE yRef )))-yRef) LE $
                 abs(yAxis(Min(Where( yAxis LE yRef )))-yRef) THEN $
                 yRef = Max(Where( yAxis GE yRef )) ELSE $
                 yRef = Min(Where( yAxis LE yRef ))
            ENDELSE
            IF Min(Where( yAxis EQ ref )) EQ -1 THEN $
               print,'Reference y-value was changed to xAxis(',yRef,')=',yAxis(yRef)
            IF yRef LT yMin OR yRef GT yMax THEN BEGIN
              Message, 'y-value reference out of range.', /INFO, /CONT
              status = "Not Done"
              RETURN
            ENDIF
            Correl1D, image( xMin:xMax, yMin:yMax ), xAxis(xMin:xMax), $
	yRef-yMin, imageTemp, xAxisTemp
            image = imageTemp & xAxis = xAxisTemp
            imageTemp = 0 & xAxisTemp = 0
            yAxis = yAxis( yMin: yMax )
            nx = N_Elements( xAxis )
            ny = N_Elements( yAxis )
            xMin = 0 & yMin = 0
            xMax = nx-1 & yMax = ny-1
            status = 'Done' & oper = 'c1'  & PutHMS,0       
          END
      6: BEGIN
            Print, 'This option transforms an irregular axis to a regular one.'
            Print, 'The new image values are computed with a ' + $
	' spline interpolation.'
            direction = ''
            Read, 'Is the interpolation in <Y> or in <X> direction [Y] ?', $
	direction
            answ = ''
            Print, 'Do you want to define manually the new axis? '
            Read, ' <y>es, <n>o, <a>bort, [n]', answ
            answ = StrUpcase( answ )
            direction = StrUpcase( direction )
            IF direction EQ 'X' THEN n = nx ELSE n = ny
            image = image(xMin:xMax, yMin:yMax)
            xAxis = xAxis(xMin:xMax)
            yAxis = yAxis(yMin:yMax)
            IF direction EQ 'X' THEN BEGIN
               axis = xAxis & n = xMax - xMin + 1
            ENDIF ELSE BEGIN
               axis = yAxis & n = yMax - yMin + 1
            ENDELSE
            IF answ EQ 'A' THEN RETURN
            IF answ EQ 'Y' THEN BEGIN
               Print, ' '
               Read_Test, 'Least value  ', Min(axis), first
               Read_Test, 'Step ', axis(1)-axis(0), step
               Read_Test, 'Number of elements ', n, nbEls
             ENDIF
             SplineImage, image, axis, n, direction, first, step, nbEls 
             IF direction EQ 'X' THEN xAxis = axis $
             ELSE yAxis = axis
             status = 'Done' & oper = 'spline'
             xMin = 0 & yMin = 0
             nx = N_Elements( xAxis )
             ny = N_Elements( yAxis )
             xMax = nx -1 
             yMax =  ny -1
           END
      ELSE:
    ENDCASE

  END UNTIL choice NE len

END  ; numerical operations


PRO FiltSmInt, image, xAxis, yAxis, status, oper, nx, ny, $
	xMin, xMax, yMin, yMax

  len = 9
 
  fMenu = StrArr( len )
  
  fMenu(0) = 'Operation done in X direction'
  fMenu(1) = 'Boxcar smooth in one direction ... '
  fMenu(2) = 'Smooth in two dimensions... '
  fMenu(3) = 'Integration ... '
  fMenu(4) = 'Digital filtering ... '
  fMenu(5) = 'Wiener filter (2D)... '
  fMenu(6) = 'Median filter ...'
  fMenu(7) = 'Exit this menu'
  fMenu(8) = 'Redisplay this menu'

  direction = 'X'

  REPEAT BEGIN

    status = 'Not Done'
    choice = General_Menu( fMenu, $
	'Filter - Smooth - Integration: Choose Option' )

    CASE choice OF
      1: BEGIN
            IF direction EQ 'X' THEN direction = 'Y' $
            ELSE direction = 'X'
            fMenu(0) = 'Operation done in '+ direction + ' direction '
          END
      2: BEGIN
            Print ,'Smooth image in '+direction +  ' Direction'
            Print, ' '
            Read_Test, ' Enter the box car width ', 1, width
            IF width GT 1 THEN BEGIN
              IF direction EQ 'X' THEN BEGIN
                width = width < nx
                FOR i = 0, ny-1 DO $
                  image( *, i ) = Smooth( image( *,i), width )
              ENDIF ELSE BEGIN
                width = width < ny
                FOR i = 0, nx-1 DO $
                  image( i, * ) = Smooth( image( i, * ), width )
              ENDELSE
              status = 'Done' & oper = 'sm'+direction
            ENDIF
          END
      3: BEGIN
            Print,'Smooth Image' & Print, ' '
            Read_Test, ' Enter the box car width ', 1, width, $
   	MAXIMUM = (nx-1) < (ny-1)
            IF width GT 1 THEN image = Smooth( image, width )
            status = 'Done' & oper = 'sm'
          END
      4: BEGIN
            Integrate_2D, image, xAxis, yAxis, status, $
	xMin, xMax, yMin, yMax
            nx = xMax + 1 & ny = yMax + 1
            IF status EQ 'Done' THEN oper = 'int'
          END
      5: BEGIN
            Print, 'Image Filter'
            Print,'The numbers ar given in % of the Nyquist frequency'
            Read_Test, 'Enter the lowest relative frequency ', 0, flow, $
	MINIMUM = 0, MAXIMUM = 1
            Read_Test, 'Enter the highest relative frequency ', 1, fhigh, $
	MINIMUM = 0, MAXIMUM = 1
            CASE 1 OF
              flow EQ 0: senseMaking = 1./ fhigh
              fhigh EQ 0: senseMaking = 1./ flow
              flow LT fhigh: senseMaking = 1./ ( flow < fhigh )
              fhigh LT flow: senseMaking = 1./ ( flow > fhigh )
              ELSE: BEGIN
                 Print, 'That does not make any sense ... '
                 senseMaking = 0
               END
            ENDCASE
            Print, 'Enter the mimimal sense-making width of the filter'
            Read_Test, '(if you dont understand, just press return)', $
	senseMaking, nTerms
            coeffs = Digital_Filter( flow, fhigh, 50, nTerms )
            IF direction EQ 'X' THEN image = Convol( image, coeffs ) $
            ELSE image = Transpose( Convol( Transpose(image), coeffs ) )
            oper = 'filt' & status = 'Done'
          END
      6: BEGIN
            Print, 'Wiener filter:'
            answ = ''
            Read, '    Do you want to subtract a backgroud (y) ? ', answ
            IF StrUpcase( StrMid(answ, 0, 1) ) NE 'N' THEN BEGIN
               Print, '    Subtracting background ... '
               image = ConstBackSub( image, /AUTOMATIC )
            ENDIF
            Read_Test, '    Enter the number of iterations', 3, iterationNb
            image = WienerFilter( image( xMin: xMax, yMin: yMax), iterationNb )
            xAxis = xAxis( xMin: xMax ) & yAxis = yAxis( yMin: yMax )
            xMax = xMax - xMin & yMax = yMax - yMin
            xMin = 0 & yMin = 0
            oper = 'wien' & status = 'Done'
          END
      7: BEGIN
            Print, 'Median filter (action on the whole image):'
            Read_Test, '    Enter the with of the median window ', 3, width
            image = Median( image, width )
            oper = 'med' & status = 'Done'
          END
      ELSE:
      ENDCASE

  END UNTIL choice NE len AND choice NE 1

END ;  filters/ smooth / integrate
       

PRO Merge, im1, im2, xAxis1, xAxis2, yAxis1, yAxis2, $
	im, xAxis, yAxis, ADJUST = adjust

  nx1 = N_Elements( xAxis1 ) & nx2 = N_Elements( xAxis2 )
  ny1 = N_Elements( yAxis1 ) & ny2 = N_Elements( yAxis2 )

  IF nx1 GT nx2 THEN BEGIN
    xAxis = xAxis1
    nxBoth = nx1
    IF Keyword_Set( ADJUST ) THEN $
      im2 = ConGrid1D( im2, nxBoth, ny2 ) 
  ENDIF ELSE  BEGIN
    xAxis = xAxis2
    nxBoth = nx2
    IF Keyword_Set( ADJUST)  THEN $
      im1 = ConGrid1D( im1, nxBoth, ny1 )
  ENDELSE

  yAxis = [ yAxis1, yAxis2 ]
  nyBoth = ny1 + ny2

  im = FltArr( nxBoth, nyBoth )
  im(0, 0) = im1
  im(0, ny1) = im2

END ; Merge images  
  

PRO TwoIms, image, xAxis, yAxis, xMin, xMax, yMin, yMax, $
	nx, ny, status, oper

  menuLen = 5

  menuTwo = StrArr( menuLen )

  menuTwo(0) = 'Merge  Images ... '
  menuTwo(1) = 'Add ( Subtract ) Images ... '
  menuTwo(2) = 'Multiply ( Divide ) Images '
  menuTwo(3) = 'Exit Two Image Operations'
  menuTwo(4) = 'Redisplay this Menu'

  winnr = WMesg( [ $
	[' The image currently displayed is the first image.' ], $
	[' In the next operations, it has the name "z1"' ], $
	[' '], [' Now select the second image, "z2" ' ] ], $
	'Select images for operations', 1000, 600 )

  Change_Selection, image2, xAxis2, yAxis2, xMin2, xMax2, $
	yMin2, yMax2, STATUS = status

  WDelete, winNr

  IF status EQ 'Not Done' THEN RETURN ELSE status = 'Not Done'

  nx = xMax-xMin+1 & ny = yMax-yMin+1
  nx2 = xmax2-xMin2+1 & ny2 = yMax2-yMin2 + 1

  REPEAT BEGIN
    
    choice = General_Menu( menuTwo, 'Select Operation' )

    CASE choice OF 
      1: BEGIN
            Print, 'Merge Images'
            Print, ' ' 
            answ = ''
            Read, 'Do you want the second image beside (b) the first ' +$ 	
	'one or on top (default =t) ? ', answ
            IF StrUpcase( answ ) EQ 'T' OR answ EQ '' THEN bes = 0 $
            ELSE bes= 1 
            Read, 'Do you want to adjust the size of the smallest '+ $
	'image (y) ? ', answ
            IF StrUpcase( answ ) EQ 'Y' OR answ EQ '' THEN adj = 1 $
            ELSE adj = 0
            Print, 'Working ... '
            IF bes THEN BEGIN
              Merge, Transpose( image( xMin:xMax, yMin:yMax)), $
	Transpose( image2( xMin2: xMax2, yMin2:yMax2 )), $
                    yAxis( yMin: yMax ), yAxis2( yMin2: yMax2 ), $
	xAxis( xMin: xMax ), xAxis2( xMin2: xMax2 ), $
	result, yAxisRes, xAxisRes, ADJUST = adj 
                    image = Transpose(  result )
            ENDIF ELSE BEGIN   
              Merge, image( xMin:xMax, yMin:yMax), $
	image2( xMin2: xMax2, yMin2:yMax2 ), $
	xAxis( xMin: xMax ), xAxis2( xMin2: xMax2), $
                    yAxis( yMin: yMax ), yAxis2( yMin2: yMax2 ), $
	result, xAxisRes, yAxisRes, ADJUST = adj 
                    image = result
            ENDELSE
            xAxis = xAxisRes & yAxis = yAxisRes
            oper = 'me'
          END
       2: BEGIN
           Print, ''
           Print, 'Addition or Subtraction of Images'
           Print, ''
           Print, ' z = a*z1^b + c*z2^d + e '
           Read_Test, 'Enter a ', 1, a
           Read_Test, 'Enter b ', 1, b
           Read_Test, 'Enter c ', 1, c
           Read_Test, 'Enter d ', 1, d
           Read_Test, 'Enter e ', 0, e
           result = FltArr( nx > nx2, ny > ny2 )
           result(0,0) = a*image(xMin:xMax, yMin:yMax)^b 
           result(0,0) = result + $
             c*image2( xMin2:xMax2, yMin2:yMax2)^d + e
           oper = 'ad'
       END
       3: BEGIN
           Print, ''
           Print, 'Multiplication or Division of Images'
           Print, ' z = a * z1^b * z2^c  + d '
           Read_Test, 'Enter a ', 1, a
           Read_Test, 'Enter b ', 1, b
           Read_Test, 'Enter c ', 1, c
           Read_Test, 'Enter d ', 1, d
           result = FltArr( nx > nx2, ny > ny2 )
           result(0,0) = a*image(xMin:xMax, yMin:yMax)^b 
           result(0,0) = result* $
             image2( xMin2:xMax2, yMin2:yMax2)^c + d
           oper = 'mu'
       END
       ELSE:
   ENDCASE

    IF choice EQ 2 OR choice EQ 3 THEN BEGIN
      IF nx LT nx2 THEN xAxis = xAxis2( xMin2: xMax2 ) $
      ELSE xAxis = xAxis( xMin: xMax )
      IF ny LT ny2 THEN yAxis = yAxis2( yMin2: yMax2 ) $
      ELSE yAxis = yAxis( yMin: yMax )
      image = result
    ENDIF

     xMin = 0 & yMin = 0
     nx = N_Elements( xAxis )
     ny = N_Elements( yAxis )
     xMax =  nx - 1
     yMax =  ny -1
     status = 'Done'

  END UNTIL choice LT menuLen

END ; two images operations.

           
PRO Modify, image , xAxis, yAxis, xMin, xMax, yMin, yMax, name


  IF N_Params() EQ 0 THEN BEGIN
    Error, 1, 'Modify'
    RETURN
  ENDIF

  nx = N_Elements(image(*,0))
  ny = N_Elements(image(0,*))
  IF N_Elements(xAxis) EQ 0 THEN xAxis = IndGen( nx )
  IF N_Elements(yAxis) EQ 0 THEN yAxis = 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
  IF N_Elements( name ) EQ 0 THEN $
	name = StrCompress( !p.title, /REMOVE_ALL )


  mainMenuLength = 10

  mainMenu = StrArr(mainMenuLength)

  mainMenu(0)='Mathematical functions (transcendental) ... '
  mainMenu(1)='Mathematical functions (general) ...'
  mainMenu(2)='Filters / smooth / integration ....'
  mainMenu(3)='Edit image ... '
  mainMenu(4)='Background subtraction ... '
  mainMenu(5)='Two images arithmetics ... '
  mainMenu(6)='Phoenix recalibrate'
  mainMenu(7)='Callisto recalibrate'
  mainMenu(8) = 'Exit modify menu'
  mainMenu(9) = 'Redisplay this menu'

  oper = ''

  REPEAT BEGIN
 
    status = 'Not Done'
    oldOper = oper

    choice=General_Menu(mainMenu, $
	'Modify Image: Choose Option')

    CASE choice OF
      1: MathFunTra, image, oper, status
      2: MathFunGen, image, xAxis, yAxis, xMin, xMax, yMin, yMax, $
	nx, ny, name, status, oper
      3: FiltSmInt, image, xAxis, yAxis, status, oper, nx, ny, $
	xMin, xMax, yMin, yMax
      4: Edit, image, xAxis, yAxis, xMin, xMax, yMin, yMax, nx, ny, $
	status, oper
      5: BEGIN
            ETHZ_Background, image, xAxis, yAxis, $
	xMin, xMax,  yMin, yMax, status
            oper = 'bkg'
          END
      6: BEGIN
            TwoIms, image, xAxis, yAxis, xMin, xMax, yMin, yMax, $
	 nx, ny, status, oper
          END
      7: BEGIN
      	PRINT,"Assuming the data are encoded as 45*LOG10(SFU+10). Doing Phoenix recalibration..."
	image=45.*ALOG10(phoenix_spg_recalibrate(10^(image/45.)-10.,yAxis) + 10.)
      	status='Done'
	oper='phCal'
      END
      8: BEGIN
      	PRINT,"Doing Callisto recalibration..."
	image=callisto_spg_recalibrate(image,yAxis)
      	status='Done'
	oper='caCal'
      END
      

    ELSE:
  ENDCASE

  IF status EQ 'Done' AND choice NE mainMenuLength-1 $
	THEN BEGIN
      IF oper NE oldOper THEN name = name + '_' + oper
      Show_Image,image(xMin:xMax, yMin:yMax), $
	xAxis(xMin:xMax),yAxis(yMin:yMax), /SIZES_ON, $
	WINNR = GetWNr(), WNAME = name 
      Image_Info,image(xMin:xMax, yMin:yMax), $
	xAxis(xMin:xMax),yAxis(yMin:yMax), FILE =name
      Put_Selection, image, xAxis, yAxis, 0, nx-1, 0, ny-1, $
	FILESTATE = "temporary", FNAME = name
  ENDIF

END UNTIL choice EQ mainMenuLength-1


END
