!-----------------------------------------------------------------------------
Subroutine Cal_Spells
Integer :: ilon, ilat, ireg, NDot, MaxDot
Real :: X(Ntime), M(Ntime), Spell(Ntime), I_Start(Ntime), I_End(Ntime), Imax, NSpell
!Integer :: Max_So_Dot        ! So dot lon nhat cua tat ca cac tram (=Max {Ndot} - Gia tri lon nhat cua N_Spell)
!Integer :: Max_Length_Dot    ! Do keo dai dai nhat trong tat ca cac dot cua cac tram (=Max {MaxDot} - Gia tri lon nhat cua Imax)

! M(MaxDot): Luu tan so cua tung dot, tu 1,2,..,MaxDot; MaxDot la do dai cuc dai cua cac dot
! Spell(NDot): Luu do dai tung dot, tu 1,2,..,NDot; NDot la so dot trong toan chuoi
! I_Start(NDot), I_End(NDot): Luu chi so dau va cuoi cua tung dot
Dur1=Miss; Dur2=Miss; Dur3=Miss;  Spel1=Miss; Spel2=Miss; Spel3=Miss;
Max_So_Dot=0; Max_Length_Dot=0; Max_So_Dot_Reg=0; Max_Length_Dot_Reg=0
Do ilon=1, Nlon   ! Tinh tan so ung voi do dai tung dot va Tan so tung dot cua tung tram
   Do ilat=1, Nlat
      X(1:Ntime) = D1(ilon,ilat,1:Ntime); Call NSpell_and_Leng (X, Ntime, M, Imax, Spell, NSpell, I_Start, I_End); MaxDot=Int(Imax); NDot=Int(NSpell)
      Dur1(ilon,ilat,1:MaxDot)=M(1:MaxDot); Spel1(ilon,ilat,1:NDot)=Spell(1:NDot); IS1(ilon,ilat,1:NDot)=I_Start(1:NDot); IE1(ilon,ilat,1:NDot)=I_End(1:NDot)
      N_Dur1(ilon,ilat)=MaxDot; N_Spel1(ilon,ilat)=NDot
      if (Max_Length_Dot<MaxDot) Max_Length_Dot=MaxDot; if (Max_So_Dot<NDot) Max_So_Dot=NDot

      X(1:Ntime) = D2(ilon,ilat,1:Ntime); Call NSpell_and_Leng (X, Ntime, M, Imax, Spell, NSpell, I_Start, I_End); MaxDot=Int(Imax); NDot=Int(NSpell)
      Dur2(ilon,ilat,1:MaxDot)=M(1:MaxDot); Spel2(ilon,ilat,1:NDot)=Spell(1:NDot); IS2(ilon,ilat,1:NDot)=I_Start(1:NDot); IE2(ilon,ilat,1:NDot)=I_End(1:NDot)
      N_Dur2(ilon,ilat)=MaxDot; N_Spel2(ilon,ilat)=NDot
      if (Max_Length_Dot<MaxDot) Max_Length_Dot=MaxDot; if (Max_So_Dot<NDot) Max_So_Dot=NDot

      X(1:Ntime) = D3(ilon,ilat,1:Ntime); Call NSpell_and_Leng (X, Ntime, M, Imax, Spell, NSpell, I_Start, I_End); MaxDot=Int(Imax); NDot=Int(NSpell)
      Dur3(ilon,ilat,1:MaxDot)=M(1:MaxDot); Spel3(ilon,ilat,1:NDot)=Spell(1:NDot); IS3(ilon,ilat,1:NDot)=I_Start(1:NDot); IE3(ilon,ilat,1:NDot)=I_End(1:NDot)
      N_Dur3(ilon,ilat)=MaxDot; N_Spel3(ilon,ilat)=NDot
      if (Max_Length_Dot<MaxDot) Max_Length_Dot=MaxDot; if (Max_So_Dot<NDot) Max_So_Dot=NDot
   Enddo
Enddo
Dur_Reg1=Miss; Dur_Reg2=Miss; Dur_Reg3=Miss; Spel_Reg1=Miss; Spel_Reg2=Miss; Spel_Reg3=Miss
Do ireg=1, Nreg   ! Tinh tan so ung voi do dai tung dot va Tan so tung dot cua tung vung
   X(1:Ntime) = D1_Reg(ireg,1:Ntime); Call NSpell_and_Leng (X, Ntime, M, Imax, Spell, NSpell, I_Start, I_End); MaxDot=Int(Imax); NDot=Int(NSpell)
!print*,MaxDot,NDot, M(1:MaxDot), Spell(1:NDot); pause
   Dur_Reg1(ireg,1:MaxDot)=M(1:MaxDot); Spel_Reg1(ireg,1:NDot)=Spell(1:NDot); IS_Reg1(ireg,1:NDot)=I_Start(1:NDot); IE_Reg1(ireg,1:NDot)=I_End(1:NDot)
   NDur_Reg1(ireg)=MaxDot; NSpel_Reg1(ireg)=NDot
   if (Max_Length_Dot_Reg<MaxDot) Max_Length_Dot_Reg=MaxDot; if (Max_So_Dot_Reg<NDot) Max_So_Dot_Reg=NDot

   X(1:Ntime) = D2_Reg(ireg,1:Ntime); Call NSpell_and_Leng (X, Ntime, M, Imax, Spell, NSpell, I_Start, I_End); MaxDot=Int(Imax); NDot=Int(NSpell)
   Dur_Reg2(ireg,1:MaxDot)=M(1:MaxDot); Spel_Reg2(ireg,1:NDot)=Spell(1:NDot); IS_Reg2(ireg,1:NDot)=I_Start(1:NDot); IE_Reg2(ireg,1:NDot)=I_End(1:NDot)
   NDur_Reg2(ireg)=MaxDot; NSpel_Reg2(ireg)=NDot
   if (Max_Length_Dot_Reg<MaxDot) Max_Length_Dot_Reg=MaxDot; if (Max_So_Dot_Reg<NDot) Max_So_Dot_Reg=NDot

   X(1:Ntime) = D3_Reg(ireg,1:Ntime); Call NSpell_and_Leng (X, Ntime, M, Imax, Spell, NSpell, I_Start, I_End); MaxDot=Int(Imax); NDot=Int(NSpell)
   Dur_Reg3(ireg,1:MaxDot)=M(1:MaxDot); Spel_Reg3(ireg,1:NDot)=Spell(1:NDot); IS_Reg3(ireg,1:NDot)=I_Start(1:NDot); IE_Reg3(ireg,1:NDot)=I_End(1:NDot)
   NDur_Reg3(ireg)=MaxDot; NSpel_Reg3(ireg)=NDot
   if (Max_Length_Dot_Reg<MaxDot) Max_Length_Dot_Reg=MaxDot; if (Max_So_Dot_Reg<NDot) Max_So_Dot_Reg=NDot
Enddo
End Subroutine Cal_Spells
!-----------------------------------------------------------------------------

