;+
; NAME:
;	Fitting
; PURPOSE:
;	User Interface for accessing  a collection of algorithms
;	for fitting curves to a data set.
; CALLING SEQUENCE
;	Fitting, x, y [, length, nbSel, dim [, status, oper ]]
; INPUTS:
;	x, y: the coordinates of the plots. If there are more than
;		one plot to fit, the x and y array variables may
;		be of dimension 2. In this case, "x(i,j)" or 
;		"y(i,j)" is the i-th elements of the j-th plot.
;	length: an array containing the relevant number of
;		values in a line of "x" and "y".
;	nbSel: the number of plots per (2D) array
; OUTPUTS:
;	y: the fitted coordinates
;	status : "Done" or "Not Done"
;	oper: name of the operation done.
;
; MODIFICATION HISTORY:
;	Created in January 1992 by A.Csillaghy
;		Inst. of Astronomy, ETH Zurich
;	Some modifs in March 95, ACs
;-

PRO Fitting, x, y, qual, length, nbSel, dim, status, oper
 
; INPUT: all
; OUTPUT: x, y, qual, length, nbSel

  status = 'Not Done'

  nbParams = N_Params()
  IF nbParams LT 2 THEN BEGIN
    Error, 1, 'Fitting'
    RETURN
  ENDIF ELSE IF nbParams EQ 2 THEN BEGIN
    length = N_Elements(x(*,0))
    nbSel = N_Elements(x(0,*))
    qual = y*0
    dim = length
  ENDIF ELSE IF nbParams NE 6 AND nbParams NE 8 THEN BEGIN
    Error, 4, 'Fitting'
    RETURN
  ENDIF

  len = 7

  menuF = StrArr( len )
  menuF(0) = 'Gauss Fit '
  menuF(1) = 'Polynomial fit ... '
  menuF(2) = 'Spline Interpolation ... '
  menuF(3) = 'Boxcar Smooth ... '
  menuF(4) = 'Linear Regression'
  menuF(5) = 'Return to Main Menu'
  menuF(6) = 'Redisplay this Menu'

  REPEAT BEGIN
  
    choice = General_Menu( menuF, 'Fitting Procedures' )
    IF choice LT len-1 THEN Print, menuF( choice-1 )

    CASE choice OF
      1: BEGIN
            Print,'Equation: F(z) = a0*exp(-z^2/2) + a3 + a4*z + a5*z^2'
            Print,'and z = (x-a1)/a2'
            Print,' '
            FOR i = 0, nbSel-1 DO  BEGIN
              y(0,i) = GaussFit( x(0:length(i)-1,i), y(0:length(i)-1,i), coeffs )
              Print, 'Plot Name: ', StrTrim(Selection(i),2), $
	'; Coefficients: a0 to a5: '
              Print, FORMAT = '( 6(F10.4, :, "  ") )', coeffs
            ENDFOR
           oper = 'g' & status = 'Done'
          END
      2: BEGIN
             Print, 'a0 + a1 x + a2 x^2 + ... + an x^n'
             Read_Test,'What is the degree n of the ploynomial to fit ? ', 1, d
             coeffs = DblArr( d+1 ) 
             FOR i = 0, nbSel-1 DO BEGIN
                lenVect = length(i)-1
                xSmall = x( 0: lenVect, i )
                coeffs = Transpose( $
	Poly_Fit( xSmall,  y( 0: lenVect, i ), d ))
                y(0,i ) = Poly( xSmall, coeffs )
                Print,' Plot Name: ', StrTrim(Selection(i),2), $
	'; Coefficients computed (a0 to an ): '
                Print, coeffs
              ENDFOR
              status = 'Done' & oper = 'p'
            END
      3: BEGIN
             Read_Test, 'Please enter the dilatation factor',1,dilate, $
               MINIMUM = 1
             Read_Test, 'Please enter the tension factor', 1, tension
             FOR i = 0, nbSel-1 DO BEGIN
                n = length(i) & lenVect = n-1
                xSmall = x( 0: lenVect, i )
                ySmall = y( 0: lenVect, i )
                nNew = (n-1)*dilate + 1
                IF nNew GT dim THEN BEGIN
                  AdjustTable, x, nNew & AdjustTable, y, nNew
                  dim = nNew
                ENDIF
                xLarge  = FltArr( nNew )
                j = 0
                WHILE j LT n-1 DO BEGIN
                  xLarge(j*dilate) = (IndGen(dilate) / dilate *$
	(xSmall(j+1)-xSmall(j))) + xSmall(j)
                  j = j + 1
                ENDWHILE
                xLarge( nNew-1 ) = xSmall( n-1 )
                yLarge = Spline( xSmall, ySmall, xLarge, tension )
                y(0,i) = yLarge & x(0,i) = xLarge
                length(i) = nNew
              ENDFOR
              status = 'Done' & oper = 's'
            END
      4: BEGIN
           Read_Test, 'Please enter number of points to smooth ', 1, sm
           IF (sm GT 1)  AND  (sm LT dim) THEN BEGIN
             FOR i=0, nbSel-1 DO y(0,i) = Smooth( y(*,i), sm ) 
             status = "Done"
             oper = "sm"
           ENDIF ELSE BEGIN
             Error, 'No smoothig performed: number too low or too high ',$
	'Analyse'
             status = "Not Done"
           ENDELSE
        END
      5: BEGIN
            Print, 'over all the selected data'
            Print, 'y(x) = a0 + a1 * x'
            xRegr = x(0:length(0)-1,0) & yRegr = y(0:length(0)-1,0)
            i = 1
            WHILE i LT nbSel DO BEGIN
              xRegr = [ xRegr,  x(0:length(i)-1,i)] 
              yRegr = [ yRegr, y(0:length(i)-1,i)]
              i = i+1
            END
            newList = Sort( xRegr )
            xRegr = xRegr( newList ) 
            yRegr = yRegr( newList )
            coeffs = Poly_Fit( xRegr, yRegr, 1, yft )
            nEls = N_Elements( yft )
            y = [yft(0), yft(nEls-1)]
            x = [xRegr(0), xRegr(nEls-1)]
            qual = [0,0]
            length = 2 & nbSel = 1
            Print, '                    a0 = ', StrTrim(coeffs(0),2), $
	';  a1 = ', StrTrim(coeffs(1),2 )
            status = 'Done' & oper = 'lin'
          END
      ELSE: 
    ENDCASE

    END UNTIL choice NE len

END ; Fitting 