      SUBROUTINE MAKWLV(NPROF,MAXABS,MINABS,W,ERSTAT)
C***********************************************************************
C
C   NAME- MAKWLV
C
C   LANGUAGE- FORTRAN        TYPE- SUBROUTINE      COMPUTER- RS/6000
C
C   VERSION- 2.0    DATE- 2/7/96     PROGRAMMER- CARLA NEIGH
C   VERSION- 3.0    DATE- 06/17/96   PROGRAMMER- TIM GARDNER
C
C   FUNCTION- THIS ROUTINE MAKES THE ABSORBER SPACE OF OPTPARM:NW LEVELS
C             IDENTIFIED AS W(0:NW).  THE MOST IMPORTANT VALUE
C             FOR IDENTIFYING THE PARAMETER 'ALPHA' IS W(200),
C             WHICH MUST BE AT LEAST THE MAXIMUM POSSIBLE ABSORBER
C             AMOUNT.  THE CODE IS STRUCTURED TO PRODUCE DIFFERENT
C             STANDARD PROFILES FOR EACH CHANNEL.
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                ON THE CRAY.
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME    TYPE   I/O   DESCRIPTION
C   ----    ----   ---   -----------
C   NPROF   INT     I    NUMBER OF PROFILES TO USE.
C   MAXABS  REAL    I    ARRAY (DIM NPROF) CONTAINING THE ABSORBER
C                        AMOUNT AT W(200).
C   MINABS  REAL    I    ABSORBER AMOUNT AT W(1), WHERE W(0) = 0.
C   W       REAL    O    ARRAY (DIM 0:NW,NPROF) CONTAINING THE
C                        STANDARD ABSORBER PROFILES.
C   ERSTAT  INT     O    ERROR RETURN CODE-
C                        SEE EXIT STATES BELOW.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'OPTPARM'
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   IPROF   INT    DO LOOP VARIABLE FOR PROFILES.
C   ITERR   INT    COUNTER FOR NUMBER OF ITERATIONS.
C   K       INT    DO LOOP INDEX.
C   ALPHA   REAL   EXPONENTIAL PARAMETER.
C   TOLRNC  REAL   ITERATION-TO-ITERATION CONVERGENCE CRITERION.
C   MAXITR  INT    ARBITRARY VALUE TO TEST NON-CONVERGENCE.
C   X1      REAL   PREVIOUS APPROXIMATION FOR NEWTON'S METHOD.
C   X2      REAL   NEW APPROXIMATION FOR NEWTON'S METHOD.
C   FX      REAL   FUNCTION FOR NEWTON'S METHOD.
C   FP      REAL   FUNCTION PRIMED (AS IN DERIVATIVE) FOR NEWTON'S
C                  METHOD.
C
C   EXIT STATES-
C    ERSTAT = 0 : NORMAL EXECUTION.
C    ERSTAT = 99: CONVERGENCE FAILED.
C
C   COMMENTS- THIS ROUTINE IS BASED ON THE SUBROUTINE NAMED
C             'MAKE_WLEVELS' WRITTEN BY THOMAS J. KLEESPIES OF
C             THE ORA PHYSICS BRANCH.
C
C***********************************************************************
C
      INTEGER NPROF, ERSTAT	
      REAL MAXABS(NPROF), MINABS, W(0:NW,NPROF)	
C
      INTEGER IPROF, ITER, K
      REAL ALPHA, TOLRNC
      INTEGER MAXITR
      REAL X1, X2, FX, FP
C
      DATA TOLRNC /1.0E-10/, MAXITR /100/
C
      ERSTAT = 0
C
C  LOOP OVER THE PROFILES.
C
      DO 100 IPROF = 1, NPROF
C
C     TOP OF THE ATMOSPHERE ALWAYS HAS ZERO ABSORBER.
C
         W(0,IPROF) = 0		
         W(1,IPROF) = MINABS
         W(NW,IPROF) = MAXABS(IPROF)
C
C     SOLVE FOR ALPHA BY NEWTON'S METHOD
C
C       THESE ARE SOMEWHAT ARBITRARY INITIAL GUESSES
C       THE LOWER ONE IS IMPORTANT FOR THE WET ATMOSPHERES, AND THE
C       UPPER ONE IS IMPORTANT FOR THE DRY ATMOSPHERES.
C
         X2 = 0.3	
         X1 = 3000.
         ITER = 0
         DO 50 WHILE (ABS(X2-X1) .GE. TOLRNC)
C
            ITER = ITER + 1
            IF(ITER .GT. MAXITR) THEN
              ERSTAT = 99
              RETURN
            ENDIF
C
            X1 = X2
            FX = (EXP(NW*X1)-1.) - (W(NW,IPROF)/W(1,IPROF))*(EXP(X1)-1.)
            FP = NW*EXP(NW*X1) - (W(NW,IPROF)/W(1,IPROF))* EXP(X1)
            X2 = X1 - FX/FP
C
   50    CONTINUE
C
         ALPHA = X2
C
C	NOW THAT WE HAVE ALPHA, CONSTRUCT THE PROFILE
C
         DO 70 K = 2 , NW
           W(K,IPROF) = W(1,IPROF) * (EXP(K*ALPHA)-1.)/(EXP(ALPHA)-1.)
   70    CONTINUE
C
  100 CONTINUE
C
      RETURN
      END
