!-----------------------------------------------------------------------------
Subroutine Write_Spell_To_NC(Dr_IDX, Models)
! Write Spells (Duration of each Spell)
Character (Len=*) :: Dr_IDX, Models
Real, Allocatable :: XDot(:)      ! So thu tu cac dot (=1,2,... Max_So_Dot)
Real, Allocatable :: Fre_Dur(:,:) ! Do dai cac dot cua tung vung (1:Nreg, 1:Max_So_Dot_Reg)
Real, Allocatable :: FD_grd(:,:,:) ! Do dai cac dot cua tung grid (Nlon,Nlat,Max_So_Dot)
Real :: Xreg(Nreg)
Integer :: ilon,ilat, ireg, idot, LengDot

! For Sub-Regions:
Allocate(XDot(1:Max_So_Dot_Reg),Fre_Dur(1:Nreg,1:Max_So_Dot_Reg))
Fname=trim(Models)//trim(Dr_IDX)//"_Spell_Duration_Reg_D1-3.nc"
! Drought D1:
Do ireg=1, Nreg; Xreg(ireg)=ireg; Enddo                 ! Mang gia X Cac vung
Do idot=1, Max_So_Dot_Reg; XDot(idot)=idot; Enddo   ! Mang gia Y Do dai cac dot
Fre_Dur(:,:)=Miss;
Do ireg=1, Nreg
   LengDot=NSpel_Reg1(ireg)
   Fre_Dur(ireg,1:LengDot)=Spel_Reg1(ireg,1:LengDot)
Enddo
Call wr_ncdf_2d (trim(Fname), Xreg, XDot, Nreg, Max_So_Dot_Reg, Fre_Dur, "Spell_D1", "Spell Duration of D1","Mon","Subregion","Spell", Miss)

! Drought D2:
Fre_Dur(:,:)=Miss;
Do ireg=1, Nreg
   LengDot=NSpel_Reg2(ireg)
   Fre_Dur(ireg,1:LengDot)=Spel_Reg2(ireg,1:LengDot)
Enddo
Call wr_ncdf_2d_apd (trim(Fname), Fre_Dur,"Spell_D2",Nreg, Max_So_Dot_Reg,"Spell Duration of D2","Mon","Subregion","Spell", Miss)

! Drought D3:
Fre_Dur(:,:)=Miss;
Do ireg=1, Nreg
   LengDot=NSpel_Reg3(ireg)
   Fre_Dur(ireg,1:LengDot)=Spel_Reg3(ireg,1:LengDot)
Enddo
Call wr_ncdf_2d_apd (trim(Fname), Fre_Dur,"Spell_D3",Nreg, Max_So_Dot_Reg,"Spell Duration of D3","Mon","Subregion","Spell", Miss)

! For each Station:

Fname=trim(Models)//trim(Dr_IDX)//"_Spell_Duration_Grd_D1-3.nc"
DeAllocate(XDot,Fre_Dur); Allocate(FD_grd(Nlon,Nlat,Max_So_Dot),XDot(Max_So_Dot))  ! Mang gia XDot
Do idot=1, Max_So_Dot; XDot(idot)=idot; Enddo

! Drought D1:
FD_grd(:,:,:)=Miss
Do ilon=1, Nlon
   Do ilat=1, Nlat
      LengDot=N_Spel1(ilon,ilat); FD_grd(ilon,ilat,1:LengDot)=Spel1(ilon,ilat,1:LengDot)
   Enddo
Enddo
!wr_ncdf_3d (filenm,lon,lat,t, nlon,nlat,nt, var,var_nm, t_unit, var_long_name, var_unit, lon_nm, lat_nm, t_nm, flag)
!wr_ncdf_3d_apd (filenm,var,var_nm,nlon,nlat,nt, var_long_name, var_unit, lon_nm, lat_nm, t_nm, flag)
Call wr_ncdf_3d(trim(Fname),Lon,Lat,XDot, Nlon,Nlat,Max_So_Dot, FD_grd,"Spell_D1","months since 1980-01-15 00:00:00","Spell Duration of D1","Mon","Lon","Lat","Spell",Miss)

! Drought D2:
FD_grd(:,:,:)=Miss
Do ilon=1, Nlon
   Do ilat=1, Nlat
      LengDot=N_Spel2(ilon,ilat); FD_grd(ilon,ilat,1:LengDot)=Spel2(ilon,ilat,1:LengDot)
   Enddo
Enddo
Call wr_ncdf_3d_apd(trim(Fname),FD_grd,"Spell_D2", Nlon,Nlat,Max_So_Dot, "Spell Duration of D2","Mon","Lon","Lat","Spell",Miss)

! Drought D3:
FD_grd(:,:,:)=Miss
Do ilon=1, Nlon
   Do ilat=1, Nlat
      LengDot=N_Spel3(ilon,ilat); FD_grd(ilon,ilat,1:LengDot)=Spel3(ilon,ilat,1:LengDot)
   Enddo
Enddo
Call wr_ncdf_3d_apd(trim(Fname),FD_grd,"Spell_D3", Nlon,Nlat,Max_So_Dot, "Spell Duration of D3","Mon","Lon","Lat","Spell",Miss)

End Subroutine Write_Spell_To_NC
!-----------------------------------------------------------------------------

