Real :: X(80), M(80)
Integer :: M1(80), Imax1
Real :: Imax
!Data X /0,1,1,1,0,0,0,0,0,0,1,0,1,1,1,0,0,1,1,1, &
!        1,1,0,0,0,0,1,1,0,0,1,1,1,1,0,0,0,0,1,1, &
!        1,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1,1,0,0, &
!        -99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99 /
!Data X /-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99, &
!        -99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99, &
!        -99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99, &
!        -99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99 /
Data X  /1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, &
         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, &
         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, &
         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 /

Call NSpell_and_Leng (X, 60, M, Imax); Imax1=Imax; M1=M
print "(60I2)", Int(X)
print*, Imax1
Print"(20I3)", M1(1:Imax1)
print*,Ave_Spell(M, Int(Imax))

Where (X==0); X=2; Endwhere; Where (X==1); X=0; Endwhere; Where (X==2); X=1; Endwhere;
print*,">>>>"
Call NSpell_and_Leng (X, 60, M, Imax); Imax1=Imax; M1=M
print*,"??????"
print "(60I2)", Int(X)
print*, Imax1
Print"(20I3)", M1(1:Imax1)
print*,Ave_Spell(M, Int(Imax))


CONTAINS
!--------------------------------------------------------------------------------
Real Function Ave_Spell (X, N)
Integer :: N, N1, i
Real :: X(N), tmp
N1=0; tmp=0
Do i=1, N
   if (X(i) > 0) then
      N1=N1+X(i); tmp=tmp+X(i)*I
   endif
Enddo
if (N1>0) then; tmp=tmp/real(N1); else; tmp=Miss; endif
Ave_Spell=tmp
End Function Ave_Spell
!------------------------------------------------------------------------------
Subroutine NSpell_and_Leng (X1, N, M1, Imax1)   ! So dot han (Imax1) va do dai cac dot (M1(Imax1))
! Input: Array X(N) with values of 0, 1, or -99
! Output: Array M(Imax) with values of number of spell 
Integer :: N, i, k
!Integer :: X(NYear*NWeek), M(NYear*NWeek), Imax
Integer :: X(N), M(N), IMAX
Real :: X1(N), M1(N), IMAX1

!N = NTime ! NYear*NWeek
X(:) = X1(:)
!Print*,N," - ",X

M(:) = 0; IMAX=0; I = 0; M1(:) = 0; IMAX1=0
DO
   I = I + 1  ! Loop until end of series data
   IF (I>N) EXIT
   IF (X(I)==1) THEN  ! Event occur at first time of this spell
      K = 0  ! Number of days event occur continuosly
      DO
         IF (X(I)==0.OR.X(I)==-99.OR.I==N) THEN   ! End of this spell
            IF (I==N.AND.X(I)==1) K=K+1   ! For the last spell
            IF (K/=0) M(K)=M(K)+1   ! Increase for this spell
            IMAX=IMAX+1             ! Increase number of spells
   !         PRINT*, K, M(K), IMAX
            EXIT
         END IF
         I = I + 1
         K = K + 1   ! How many days of this spell
      ENDDO
   ELSE
      CYCLE
   END IF
   IF (I>=N) EXIT
ENDDO
!! I=IMAX
I=N
!print*,"I, IMAX=",I, IMAX
if (IMAX > 0) then
   DO
      IF (M(I)/=0) EXIT    ! Determine exactly how many spells
      I=I-1
   ENDDO
   IMAX = I 
!   print*, IMAX
   if (IMAX < N) M(IMAX+1:N) = -99
! Gan lai cho so thuc:
   M1(:) = M(:); IMAX1 = IMAX
else
   M1(:) = -99; IMAX1 = 0
endif
!print*,"imax=",imax,"M=",M(1:imax)
End Subroutine NSpell_and_Leng
!--------------------------------End of this file--------------------------
END
