      SUBROUTINE FIXTET(PS,WORK,DEL,RPI,RPIREC,TG,QG,TETA,NUN,SL100K)
      DIMENSION DEL( 7 ),RPI( 6 ),RPIREC( 6 ),
     1    PS( 50 ),
     2  WORK( 50 ),
     3    TG( 50 , 7 ),
     4   TETA( 50 , 7 ),
     5   QG( 50 , 6 )
      DIMENSION TET( 7 ),Q( 6 )
C***  PS IS ASSUMED TRUE SURF. PRESS. IN CB.----------------------
      RK =  287.05 / 1005.
      DO 1 I=1, 50
      WORK(I) = SL100K*PS(I)**RK
1     CONTINUE
C     COMPUTE BOTTOM POT. TEMP. IN TETA( 50 ,1)
      DO 2 I=1, 50
      TETA(I,1) = TG(I,1)/WORK(I)
2     CONTINUE
C     COMPUTE REST OF POT TEMPS FROM BOTTOM ONE UP
      DO 4 K=1, 6
      DO 3 I=1, 50
      TETA(I,K+1) = TG(I,K+1)*TETA(I,K)*RPIREC(K)/TG(I,K)
3     CONTINUE
4     CONTINUE
C     NOW HAVE POT. TEMP. IN TETA, ADJUST.
C     .................................
C***   QG IS MIXING RATIO ---------------------
C     .................................
      DO 66666 I=1, 50
      DO 5 K=1, 7
5     TET(K) = TETA(I,K)
      DO 6 K=1, 6
6     Q(K)=QG(I,K)
      ITEST=0
C     TET(K) = POT. TEMP. AT LON.
55555  CONTINUE
      DO    13 K=2, 7
      KK = K
      IF (TET(K)-TET(K-1)) 14, 13, 13
   13  CONTINUE
      GO TO    26
   14 KBOT = KK - 1
      KTOP = KK
   15 SUMTET = 0.
      SUMDEL = 0.
      SUMCOL = 0.
      SUMQ = 0.
      ITEST=1
      DO    16 K=KBOT,KTOP
      SUMDEL = SUMDEL + DEL(K)
   16 SUMTET = SUMTET + DEL(K)*TET(K)
      TET(KBOT) = SUMTET/SUMDEL
      IF (KTOP.GT. 6 )  GO TO 19
      DO    17 K=KBOT,KTOP
   17 SUMQ = SUMQ + DEL(K)*Q(K)
      Q(KBOT) = SUMQ/SUMDEL
      DO    18 K=KK,KTOP
   18 Q(K) = Q(KBOT)
      GO TO    22
   19 IF (KBOT.GE. 6 )  GO TO 22
      DO    20 K=KBOT, 6
      SUMCOL = SUMCOL + DEL(K)
      SUMQ = SUMQ + DEL(K)*Q(K)
   20 CONTINUE
      Q(KBOT) = SUMQ/SUMCOL
      DO    21 K=KK, 6
      Q(K) = Q(KBOT)
   21 CONTINUE
   22 DO    23 K=KK,KTOP
   23 TET(K) = TET(KBOT)
      IF (KTOP- 7 ) 24, 26, 26
   24 IF (TET(KTOP+1)-TET(KTOP)) 25, 55555, 55555
   25 KTOP = KTOP + 1
      GO TO    15
   26 CONTINUE
      IF(ITEST)27,66666,27
27    NUN=NUN+1
C     RETURN ADJUSTED POTENTIAL TEMPS TO TETA
      DO 7 K=1, 7
7     TETA(I,K) = TET(K)
      DO 8 K=1, 6
8     QG(I,K)=Q(K)
66666   CONTINUE
C     .................................
C     .................................
C     CONVERT POT. TEMPS. TO TEMP. IN TG( 50 , 7 )
C     START BY COMPUTING BOTTOM TEMP. AND BUILD UP.
      DO 9 I=1, 50
      TG(I,1) = TETA(I,1)*WORK(I)
9     CONTINUE
      DO 11 K=1, 6
      DO 10 I=1, 50
      TG(I,K+1) = TG(I,K)*TETA(I,K+1)*RPI(K)/TETA(I,K)
10    CONTINUE
11    CONTINUE
C(DEBUGLINE) VERSION CREATED JULY 8 1982 BY J.SELA,NMC.
      RETURN
      END
