      SUBROUTINE FDERIV(X,I,A,CONST,DELTAA,NPARMS,DERIV,MDERIV,MODFIT)
C-----------------------------------------------------------------------
C @(#)fderiv.f	5.1 06/22/94
C-----------------------------------------------------------------------
C
C  THIS SUBROUTINE CALCULATES THE PARTIAL DERIVATIVES OF THE FITTING
C  FUNCTION WRT EACH PARAMETER OF THE FIT.  THE USER MAY CHOOSE
C  THROUGH THE ARGUMENT 'MDERIV' WHETHER ANALYTIC OR NUMERICAL
C  CALCULATIONS ARE TO BE MADE.
C  (*** THE ARRAY OF PARAMETER INCREMENTS DELTAA MUST BE CALCULATED
C  (AND SUPPLIED BY THE CALLING PROGRAM;  THIS IS NOT PRESENTLY
C  (IMPLEMENTED (1/84) ***)
C
C  THE PRESENT ROUTINE CALCULATES THE PARTIAL DERIVATIVES OF THE
C  ATMOSPHERIC OPTICAL DEPTH EQUATION
C    F = RTCHOP - ETAL*RTM*(1 - EXP(-TAU0*X)) - (1 - ETAL)*RTSBR
C         - ETAL*RTBG*EXP(-TAU0*X)
C
C  DESCRIPTION OF THE VARIABLES:
C    X - INDEPENDENT VARIABLE (AIR MASS) ARRAY.
C    I - INDEX OF DATA POINTS
C    A - ARRAY OF PARAMETERS OF THE FIT
C    DELTAA - ARRAY OF PARAMETER INCREMENTS (NEEDED FOR NUMERICAL CALC)
C    NPARMS - NUMBER OF PARAMETERS
C    DERIV - ARRAY OF PARTIAL DERIVATIVES OF THE FUNCTION.
C
      real X(*), A(*), DELTAA(*), DERIV(*), CONST(*), tau0, rtm, rtsbr,
     .     rtbg, rtchop, ex, etal, petal, ptau0, aj, delta, yfit,
     .     functn, xi
      integer*2 ideriv, i, nparms, mderiv, modfit, j, nterms
      character*2 cderiv
c
      equivalence (cderiv, ideriv)
c
      ideriv = mderiv
      IF(cderiv.EQ.'N') GO TO 440
      XI = X(I)
      TAU0 = A(1)
      RTM = CONST(5)
      RTSBR = CONST(8)
      RTBG = CONST(10)
      RTCHOP = CONST(14)
C
C  CALCULATE PARTIAL DERIVATIVES ACCORDING TO THE FIT-MODE FLAG, MODFIT.
C
      EX = EXP(-TAU0*XI)
      GO TO (410,415) MODFIT
  410 CONTINUE
      ETAL = CONST(15)
      GO TO 430
  415 CONTINUE
      ETAL = A(2)
      PETAL = -RTM*(1. - EX) + RTSBR - RTBG*EX
      DERIV(2) = PETAL
  430 CONTINUE
      PTAU0 = -XI*ETAL*RTM*EX + XI*ETAL*RTBG*EX
      DERIV(1) = PTAU0
      GO TO 499
C
  440 CONTINUE
C  CALCULATE PARTIAL DERIVATIVES BY THE NUMERICAL OPTION.
      DO 450 J = 1,NTERMS
      AJ = A(J)
      DELTA = DELTAA(J)
      A(J) = AJ + DELTA
      YFIT = FUNCTN(X,I,A,CONST,MODFIT)
      A(J) = AJ - DELTA
      DERIV(J) = (YFIT - FUNCTN(X,I,A,CONST,MODFIT))/(2.*DELTA)
      A(J) = AJ
  450 CONTINUE
C
  499 RETURN
      END

