PROGRAM TESTE_LPBL
IMPLICIT NONE
   INTEGER, PARAMETER        :: LM  = 50   
   REAL   , PARAMETER        :: EPSQ2  =    0.1
   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) < EPSQ2*0.01 ).AND. Q2(L) <= EPSQ2) THEN
         IF  (L > LPBL ) CYCLE  ! Testar se já foi encontrada camada turbulenta, na camada abaixo 
       ELSEIF ( (ABS(DQ2)>= EPSQ2*0.01) ) THEN
         LPBL = L-1
         CYCLE   ! raise L either in decreasing or in increasing tke with height
       ELSEIF (L >= LMHK/2) THEN
         CYCLE
       END IF
       IF ( LPBL <= LMHK/2) LPBL=LMHK-1
200 END DO
    PRINT*,'LPBL  = ',LPBL
END PROGRAM TESTE_LPBL
