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          
!       IF( (ABS(DQ2) <  0.00005)) THEN
        IF( (ABS(DQ2) <  0.00005).AND. Q2(L) <= 0.2) THEN
          IF  (L > LPBL ) THEN   ! Testar se já foi encontrada camada turbulenta, na camada abaixo 
            CYCLE
          ELSE  
	                         ! está acima da camada turbulenta, e acabou a turbulência.
           EXIT
          ENDIF
!       ELSEIF ( (DQ2 >= 0. .OR. (DQ2 < 0.) .And. (ABS(DQ2)>= 0.01) ) .Or. (Q2(L) > 0.2)) THEN   ! Estamos na camada turbulenta
        ELSEIF ( (DQ2 >= 0. .OR. (DQ2 < 0.) .And. (ABS(DQ2)>= 0.01) ) ) THEN
            LPBL = L-1
            Cycle    ! raise L either in decreasing or in increasing tke with height
       ELSE
            EXIT
       END IF
200    END DO
     PRINT*,'LPBL = ',LPBL
END PROGRAM TESTE_LPBL
