PRO ReadFactor, char, default, value

  Read_Test, 'Please enter the factor '+char, default, value
  value = StrTrim( value, 2 )

END ; ReadFactor

PRO ReadCoeffs, a, b, c, d, e, f, g, h, i, j, k, l, m

  f = '' & g = ''

  Print,' 1. Functions: '
  Read,'    Insert the WAVE function f or RETURN for identity: ', f
  Read,'    Insert the WAVE function g or RETURN for identity: ', g
  Print, ' 2. Constants: '
  ReadFactor,' a ',1.0, a
  ReadFactor,' b ',1.0, b
  ReadFactor,' c ',1.0, c
  ReadFactor,' d ',0.0, d
  ReadFactor,' e ',1.0, e
  ReadFactor,' h ',1.0,h
  ReadFactor,' i ',1.0, i
  ReadFactor,' j ',1.0,j
  ReadFactor,' k ',0.0, k
  ReadFactor, ' l ',  1.0, l
  ReadFactor, ' m ', 0.0, m

END ; ReadCoeffs

PRO MathsFunc1D, x, y, length, nbSel, status, oper

  menuLen = 8

  mMenu = StrArr( menuLen )

  mMenu(0) = 'Exponentiation ... y = a*exp(b*y + c ) + d'
  mMenu(1) = 'Aritmetic Operations ... y =  a*(y^b)+ c'
  mMenu(2) = 'Logaritmization  ... y = a*ln(b*y + c ) + d'
  mMenu(3) = 'Functions  y = a*f(b*y^c + d)^e + h*g(i*x^j + k)^l + m'
  mMenu(4) = 'Functions y = a*f(b*y^c + d)^e * h*g( i*x^j + k )^l + m'
  mMenu(5) = 'Output variable: y '
  mMenu(6) = 'Exit this Menu'
  mMenu(7) = 'Redisplay this Menu'

  status = 'Not Done'
  oper = ''
  out = 'y'
  instr = ''

  REPEAT BEGIN

    choice = General_Menu( mMenu, 'Mathematical functions' )

    ;IF choice LT menuLen-2 THEN HiLiPrint, mMenu( choice-1 )	
    IF choice LT menuLen-2 THEN Print, mMenu( choice-1 )

    CASE choice OF
      1: BEGIN
           ReadFactor, 'a',1.0,a
           ReadFactor, 'b',2.3025851,b
           ReadFactor,'c',0.0,c
           ReadFactor,'d',0.0,d
           FOR i = 0, nbSel-1 DO BEGIN
             iChar = String(i)
             instr = out + '(0,'+iChar+') = a*Exp( b * '$
	+out+'(0:length('+iChar+')-1, '+iChar+') + c ) + d'
             res = Execute( instr )
           ENDFOR
           status = "Done" & oper = "exp"
        END
    2: BEGIN
           ReadFactor,'a', 1.0, a
           ReadFactor,'b', 1.0, b
           ReadFactor,'c',0.0, c
           FOR i = 0, nbSel-1 DO BEGIN
             iChar = String(i)
             instr = out+'(0,'+iChar+') = a*('$
	+out+'(0:length('+iChar+')-1,'+iChar+' )^b)+c'
             res = Execute( instr )
           ENDFOR
           status = "Done" & oper = "ar"
        END
    3: BEGIN
           ReadFactor,'a',0.4342945, a
           ReadFactor,'b',1.0 ,b
           ReadFactor,'c',0.0,c
           ReadFactor,'d',0.0, d
           FOR i = 0, nbSel-1 DO BEGIN
             iChar = String(i)
             instr = out+'(0,'+iChar+') = a*Alog(b*'+out+$
	'(0:length('+iChar+')-1,'+iChar+' )+c ) + d'
             result = Execute( instr )
           ENDFOR
           status = "Done" & oper = "log"
        END
    4: BEGIN
           ReadCoeffs, a, b, c, d, e, f, g, h, i, j, k, l, m
           instruction = 'FOR o=0, nbSel-1 DO ' + $
	out+'(0,o) = '+ a+'*'+f+'('+b+$
	'*y(0:length(o)-1,o)^('+ c + $
	')+'+ d+ ')^('+e +')+'+h+$
	'*'+g+'('+i+'*x(0:length(o)-1,o)^('+ $
	j+') + '+k+')^('+ l +')+ m
           IF NOT Execute(instruction) THEN BEGIN
             Print,'Function not executed ... '
             status = "Not Done"
           ENDIF ELSE BEGIN 
              status = "Done" & oper = "ev"
           ENDELSE
        END
    5: BEGIN
           ReadCoeffs, a, b, c, d, e, f, g, h, i, j, k, l, m
           instruction = 'FOR o=0, nbSel-1 DO ' + $
	out+'(0,o) = '+ a+'*'+f+'('+b+$
	'*y(0:length(o)-1,o)^('+ c + $
	')+'+ d+ ')^('+e +')*'+ $
	h+'*'+g+'('+i+'*x(0:length(o)-1,o)^('+ $
	j+') + '+k+')^('+ l +')+ m
           IF NOT Execute(instruction) THEN BEGIN
             Print,'Function not executed ... '
             status = "Not Done"
           ENDIF ELSE BEGIN 
              status = "Done" & oper = "ev"
           ENDELSE
        END
      6: BEGIN
            IF out EQ 'y' THEN out = 'x' ELSE out = 'y'
            mMenu(0) = 'Exponentiation ... ' + out + $
	'= a*exp(b*'+out+' + c ) + d'
            mMenu(1) = 'Aritmetic Operations ... ' + out + $
	'= a*('+out+'^b)+ c'
            mMenu(2) = 'Logaritmization  ...' + out + $
	'= a*ln(b*'+out+' + c ) + d'
            mMenu(3) = 'Functions  ' + out + $
	'= a*f(b*y^c + d)^e + h*g(i*x^j + k)^l + m'
            mMenu(4) = 'Functions ' + out + $
	'=a*f(b*y^c + d)^e * h*g( i*x^j + k )^l + m'
            mMenu(5) = 'Output variable: ' + out
          END
      ELSE:

    ENDCASE

  END UNTIL choice NE menuLen AND choice NE menuLen-2

END ; maths simple.


PRO MathsFGen1D, x, y, length, nbSel, status, oper

  menuLen = 8

  mMenu = StrArr( menuLen )

  mMenu(0) = 'Derivative'
  mMenu(1) = 'Fourier Transformation Forward (Real/Im)'
  mMenu(2) = 'Fourier Transformation Forward (Power Spectrum)'
  mMenu(3) = 'Fourier Transformation Backward'
  mMenu(4) = 'Autocorrelation ...'
  mMenu(5) = 'Set Value under Treshold to an Other Value ... '
  mMenu(6) = 'Exit this Menu'
  mMenu(7) = 'Redisplay this Menu'

  status = 'Not Done'
  oper = ''

  REPEAT BEGIN

    choice = General_Menu( mMenu, 'General Mathematical Functions' )

    CASE choice OF
      1: BEGIN
            FOR i = 0, nbSel-1 DO BEGIN
             l = length(i)-1
             y( 0, i ) = Deriv( x(0:l, i), y(0:l,i ) )
           ENDFOR
           status = "Done" & oper = "der"
         END
      2: BEGIN
           y = Complex(y)
           FOR i = 0, nbSel-1 DO BEGIN
             l = length(i)-1
             y( 0:l, i ) = FFT( y(0:l,i ), -1 )
             x( 0:l, i ) = IndGen( l+1 )
           ENDFOR
           status = "Done" & oper = "ftf"
        END
      3: BEGIN
           FOR i = 0, nbSel-1 DO BEGIN
             l = length(i)-1
             y( 0:l, i ) = Abs( FFT( y(0:l,i ), -1 )^2 )
             x( 0:l, i ) = IndGen( l+1 )
           ENDFOR
           status = "Done" & oper = "p"
        END
      4: BEGIN
           FOR i = 0, nbSel-1 DO BEGIN
             l = length(i)-1
             y( 0:l, i ) = FFT( y(0:l,i ), +1 )
             x( 0:l, i ) = IndGen( l+1 )
           ENDFOR
           status = "Done" & oper = "ftb"
        END
     5: BEGIN
          transf = ''
          Print, 'Autocorrelation with:
	  Print, '  the (fast) <F>ourier-Transform (periodic boundaries assumed) or'
	  Print, '  the <S> of squared terms (slow)'
          Read,  '  (default = S)? ', transf
          fast = StrUpcase( transf ) EQ 'F'
          IF NOT fast THEN Read_Test, 'Please enter the maximum lag', Min(length)-1, lag $
	  ELSE lag = Min(length)-1
          FOR i = 0, nbSel-1 DO BEGIN
             l = length(i)-1
  	     temp = Autocorr( y(0:l,i ), XLAGMAX = lag, $
				 /NORMALIZED, FAST = fast)
	     length(i) = N_Elements( temp )
             y( 0:length(i)-1, i ) = temp
             x( 0:length(i)-1, i ) = IndGen( length(i) )
           ENDFOR
           status = "Done" & oper = "ac"
        END
     6: BEGIN
            Read_Test, 'Please enter the treshold: ', 0.0, tresh
            Read_Test,' Please enter the new value: ', 0.0, new
            FOR i = 0, nbSel-1 DO BEGIN
               vector = y(0:length(i)-1,i)
               idxLst = Where( vector LT tresh )
               vector( idxLst ) = new
               y(0,i) = vector
             ENDFOR
             oper = 'tresh' & status = 'Done'
         END
       ELSE:
      ENDCASE

    END UNTIL choice NE menuLen

END ; maths general


PRO Edit_1D, x, y, qual, length, nbSel, status, oper

  status = 'Not Done'
  oper = ''
  nPlots = N_Elements( x( 0, * ))

  len = 11
  eMenu = StrArr( len )

  eMenu(0) = 'Low and High Thresholds ... '
  eMenu(1) = 'Plot Shift in x Direction ... '
  eMenu(2) = 'Plot Extend/Shrink in x Direction ... '
  eMenu(3) = 'Interactive Shift ...'
  eMenu(4) = 'Interactive Extension/Reduction ... '
  eMenu(5) = 'Keep only a Part of the Plot ... '
  eMenu(6) = 'Exit this Menu'
  eMenu(7) = 'Redisplay this Menu'

  REPEAT BEGIN 

    choice = General_Menu( eMenu, $
	'Edit: Choose the Operation or Exit')

    CASE choice OF
      1: BEGIN
            ltr = '' & htr = ''
            Read,'Please enter the  lowest treshold '+ $
	'(<ret> when not defined): ', ltr
            Read, 'Please enter the highest treshold ' + $
	'(<ret> when not defined): ', htr
            IF ltr NE '' THEN y = y > FLOAT(ltr) $
            ELSE IF htr NE '' THEN y = y < FLOAT(htr) $
            ELSE y = y > FLOAT(ltr) < FLOAT(htr)
            status = "Done" & oper = "tr"
          END
      2: FOR i=0, nPlots-1 DO BEGIN
              plotToShift = x(*,i)
              Shift_Plot, plotToShift, status 
              oper = "sh"
              x(*,i) = plotToShift
          ENDFOR
      3: FOR i=0, nPlots-1 DO BEGIN
              plotToMul = x(*,i)
              Extend_Shrink, plotToMul, status
              oper = "ext"
              x(*,i) = plotToMul
           ENDFOR
     4: BEGIN
             Data_Display, /MDISP
             FOR i=0, nPlots-1 DO BEGIN
               plotToShift = x(*,i)
               Shift_Plot, plotToShift, status,   /INTERACTIVE
               x(*,i) = plotToShift
               oper = "sh"
             ENDFOR
           END
     5: BEGIN
             Data_Display, /MDISP
             FOR i=0, nPlots-1 DO BEGIN
               plotToMul = x(*,i)
               Extend_Shrink, plotToMul, status, /INTERACTIVE
               x(*,i) = plotToMul
               oper = "ext"
             ENDFOR
          END
      6: BEGIN
             Data_Display, /MDISP
             ClickLimits, low, high
             idx = Where( x(0:length(0)-1,0) GE low AND $
		x(0:length(0)-1,0) LE high )
             x = x(idx, *) & y = y( idx,  * ) & qual = qual( idx, * )
             oper = 'cut' & status = 'Done'
             IF nbSel GT 1 THEN $
               length = Replicate( N_Elements( idx ), nbSel ) $
             ELSE length = N_Elements( idx )
          END
      ELSE:
    ENDCASE

  END UNTIL choice NE len

END ; edit plots


PRO Modify_1D

;+
; NAME:
;	Modification of 1D Plots
; PURPOSE:
;	As a part of mdisp, it is the analyser. It is done to
;	allow the user to effectuate interactively mathematic
;	operations on the plots selected.
; CATEGORY
;	1D
; CALLING SEQUENCE
;	Modify_1D
; SIDE EFFECTS:
;	The selectioner of mdisp is used. New plots are generated
;	and replace the other plots in the selection (but not
;	in the memory).
;
; MODIFICATION HISTORY:
;	Created in June 1991 by A. Csillaghy
;	Minor modifs in March 1995 ACs
;	In July 1995: Autocorrelation parameter input procedure modified ACs
;	Oct 95: modification by "keep only a part of the plot" ACs
;-


mainMenuLength = 7
mainMenu = StrArr(mainMenuLength)

overplot = 1

mainMenu(0) = 'Mathematical Functions (simple) ... '
mainMenu(1) = 'Mathematical Functions (general) ... '
mainMenu(2) = 'Smooth & Fit ... '
mainMenu(3) = 'Edit Plots (incl. x Axis Transformations ) ... '
mainMenu(4) = 'Overplot the Modified Data'
mainMenu(5) = 'Return to Main Menu'
mainMenu(6) = 'Redisplay this Menu'


  choice = 0
  nbSel = N_Selected()
  IF nbSel EQ 0 THEN BEGIN
    Error, 13, 'Analyse'
    RETURN
  ENDIF
  
  REPEAT BEGIN 
    status = "Not Done"

    LoadSelection, name, length, x, y, qual, dx, dy,  $
	color, lineStyle, thick, offset, overallQual,  userSym, $
	title, xTitle,  yTitle,  subtitle, text
    dim = N_Elements(y(*,0))

    choice = General_Menu(mainMenu, $
	'Analyse: Choose the Operation or Return')
    CASE choice OF
      1: MathsFunc1D, x, y, length, nbSel, status, oper
      2: MathsFGen1D, x, y, length, nbSel, status, oper
      3: Fitting, x, y, qual, length, nbSel, dim, status, oper
      4: Edit_1D, x, y, qual, length, nbSel, status, oper
      5: IF overplot EQ 1 THEN BEGIN
            mainMenu( 4 ) = 'Plot Only the Modified Data'
            overplot = 0
          ENDIF ELSE BEGIN
            mainMenu( 4 ) = 'Overplot the Modified Data'
            overplot = 1
          ENDELSE
    ELSE:
    ENDCASE

  IF status EQ "Done" THEN BEGIN
    
    name = name +  "_" + oper
      

    siz = Size(y) & type = siz( siz(0)+1 )

    IF (NOT overplot) OR (oper EQ 'cut')  THEN InitSel
    IF type NE 6 THEN BEGIN
      IF oper NE 'lin' THEN $
        InsertPlot, name, length, x, y, qual, dx, dy,  $
	color, lineStyle, thick, offset, overallQual,  userSym, $
	title, xTitle,  yTitle,  subtitle,  text, /NEW $
      ELSE  InsertPlot, name(0), length(0), x, y, 0, ud, ud, color(0), $
	lineStyle(0), thick(0), offset(0), 0, $
	userSym(0), title(0), xTitle(0), yTitle(0), subtitle(0), $
	text(0), /NEW
    ENDIF ELSE BEGIN
      namei = name + 'i'
      name =  name+'r'
      InsertPlot, [name, namei] , [length, length], [[x],[x]], $
	[[Float(y)], [Imaginary(y)]],  [[qual], [qual]], undef, undef,  $
	[color, color], [lineStyle, linestyle], [thick, thick],  $
 	[offset, offset], [overallQual, overallQual],  $
                    [userSym, userSym], $
	[title, title], [xTitle, xTitle], [ yTitle, yTitle ],  $
	[subtitle, subtitle],  [text, text], /NEW
    ENDELSE

    IF oper NE 'lin' THEN InsertSel, name ELSE InsertSel, name(0)

  ENDIF

  IF choice LT mainMenuLength-2 AND status EQ "Done" THEN $
    Data_Display, /MDISP, RESET = oper EQ 'cut'

END UNTIL choice EQ mainMenuLength-1


END