      program txt2bin
      IMPLICIT NONE
      INTEGER, PARAMETER        :: LM    = 50   
      REAL   , PARAMETER        :: FH    = 1.01
      REAL   , PARAMETER        :: undef = -9999
      REAL                      :: DQ2
      REAL   , DIMENSION(7,LM)  :: Q2
      REAL   , DIMENSION(7,LM)  :: Z
      REAL   , DIMENSION(7)     :: SM
      INTEGER, DIMENSION(7)     :: LMHK
      INTEGER, DIMENSION(7)     :: LPBL
      INTEGER, DIMENSION(7)     :: LPBL2
      INTEGER, DIMENSION(7)     :: IPonto
      INTEGER, DIMENSION(7)     :: JPonto
      REAL   , DIMENSION(7)     :: HPBL
      REAL                      :: EPSQ2
      
      INTEGER                   :: I
      INTEGER                   :: L 
      INTEGER                   :: M
      INTEGER                   :: NLEV_EPSQ2
      INTEGER                   :: i_dummy
      CHARACTER (LEN=200)       :: c_dummy
     
      hpbl=0.
      WRITE(6,'(a,$)') "EPSQ2? : "
!      READ(5,*)EPSQ2
      EPSQ2=0.2
 700  Format(a6,I4,a6,I7,a6,I3,a3,I4,a3,I4,a4,F3.0,a6,I3,a6,F10.6)   
 701  Format(a3,I3,a6,F8.2,a7,F10.6)      
      open (1,file="variaveis.txt", form="formatted",status="old")
      do m=1,720
        do I=1,7
!          read(1,700) c_dummy,i_dummy,c_dummy,i_dummy,c_dummy,LMHK(i),c_dummy,IPonto(I),c_dummy,JPonto(I),c_dummy,sm(i),c_dummy,lpbl(i),c_dummy,hpbl(i)
          read(1,700) c_dummy,i_dummy,c_dummy,i_dummy,c_dummy,LMHK(i),c_dummy,IPonto(I),c_dummy,JPonto(I),c_dummy,sm(i),c_dummy,lpbl(i),c_dummy
          do L=LM,1, -1
            read(1,701) c_dummy,i_dummy,c_dummy,Z(I,L),c_dummy, Q2(I,L)	  
          enddo
          IF (LMHK(i) < LM) THEN
	     do l=LM,LMHK(i)+1,-1 
	        z(i,l)=undef
	        q2(i,l)=undef
	     enddo
	   ENDIF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	  
	  LPBL2(I) = LMHK(I)
          NLEV_EPSQ2=1
          DO 200 L=LMHK(I), LMHK(I)/2, -1   !Daniela
             DQ2= Q2(I,L-1)- Q2(I,L)                      ! Vert Dif Q2  
 	     if (i == 1) Print*,m,L,Q2(I,L),DQ2
	     IF ( (Q2(I,L)<=EPSQ2*1.01.and.NLEV_EPSQ2<=2 )) THEN
                NLEV_EPSQ2=NLEV_EPSQ2+1
                CYCLE   ! raise L either in decreasing or in increasing tke with height
	     ELSEIF ((Q2(I,L)==EPSQ2.and.NLEV_EPSQ2==2 )) THEN	
	        EXIT
             END IF
             IF ( (ABS(DQ2 < 1E-2).and. Q2(I,L)<=EPSQ2*1.01)) THEN
                LPBL2(I) = L
                EXIT   ! raise L either in decreasing or in increasing tke with height
             END IF
          200 END DO
	  LPBL2(I)=LPBL2(I)-1
 	  if (i == 1) print*,"LPBL:", LPBL2(I),(m*2*180)/3600,'h'
        enddo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	
	write(8000) (sm(i),i=1,7)
	write(8000) (float(lmhk(i)),i=1,7)
	write(8000) (float(lpbl(i)),i=1,7)
	write(8000) (float(lpbl2(i)),i=1,7)
	write(8000) (hpbl(i),i=1,7)
	 	
        do l=LM,1,-1
          write(8001) (z(i,l),i=1,7)
        enddo	
        do l=LM,1,-1
          write(8001) (q2(i,l),i=1,7)
        enddo	
       enddo
      end program txt2bin
