PROGRAM TESTE_LPBL
IMPLICIT NONE
   INTEGER, PARAMETER        :: LM  = 50   
   REAL   , PARAMETER        :: EPSQ2  =    0.2
   REAL   , PARAMETER        :: FH     =    1.01
   INTEGER                   :: L
   INTEGER                   :: LPBL 
   INTEGER                   :: LMHK
   REAL                      :: DQ2
   REAL   , DIMENSION(LM)    :: Q2


   READ(10,*) LMHK
   DO L=LM,1,-1
    READ(10,*) Q2(L)
    WRITE(*,*) 'Q2(',L,')',Q2(L)
   ENDDO


    LPBL = LMHK/2
    print*,LPBL
    DO 200 L=LMHK-1, LMHK/2, -1   !Daniela
       DQ2= Q2(L-1)- Q2(L)                      ! Vert Dif Q2  
!       PRINT*, L-1, L, DQ2,  Q2(L-1), Q2(L) 
!       PRINT*,'LPBL = ',LPBL    
       IF ( (ABS(DQ2) <  0.000005).AND. Q2(L) <= 0.2) THEN
         IF  (L > LPBL ) THEN   ! Testar se já foi encontrada camada turbulenta, na camada abaixo 
           CYCLE
         ELSE  	                         
          ! EXIT ! está acima da camada turbulenta, e acabou a turbulência.
         ENDIF
       ELSEIF ( (DQ2 >= 0. .OR. (DQ2 < 0.) .And. (ABS(DQ2)>= 0.00001) ) ) THEN
         LPBL = L-1
         CYCLE   ! raise L either in decreasing or in increasing tke with height
       ELSEIF (L >= LMHK/2) THEN
         CYCLE
       ELSE 
         EXIT
      END IF
      IF ( LPBL <= LMHK/2) LPBL=LMHK-1
      print*,L,Q2(L)
200 END DO
    PRINT*,'LPBL  = ',LPBL
END PROGRAM TESTE_LPBL
