Use netcdf
Implicit None
! For 1980-2014 period:
Integer, Parameter :: Yr1=1980, Yr2=2014, Nmon=12, Nyear=(Yr2-Yr1)+1
Integer, Parameter :: Ntime=Nyear*Nmon, Nreg=7, Nst=131, Nbin=18, NEnso=3, N_ClimIDX=16
! For 1961-2014 period:
Integer, Parameter :: Yr11=1961, Yr22=2014, Nyear12=(Yr22-Yr11)+1
Integer, Parameter :: Ntime12=Nyear12*Nmon, Nst12=83
!---------------------

Integer, Parameter :: ENSO_Phase(NEnso)=(/1,-1,0/)   ! 1:El Nino, -1:La Nina, 0:Neutral

Real, Parameter :: Miss = -99.0

Real, Parameter :: Thre_1=-2, Thre_2=-3, Thre_3=-4   ! PDSI: Moderate, Severe, Extreme
Real, Parameter :: BIN(Nbin+1)=(/-30,-20,-15,-10,-6,-4,  -3,-2,-1,  0,  1,2,  3,4,6,10,15,20,30/)

Character (Len = 11), Dimension(N_ClimIDX) :: CL_Name   ! Clim_IDX
Real, Dimension (N_ClimIDX, Ntime) :: ClimIDX           ! 16 Clim_IDXs
Integer, Dimension(Nst)            :: YY                 ! So thu tu nam trong tap mau

! Definitions:
Real, Dimension(Nst,Ntime) :: D1, D2, D3    ! (Nst, Ntime); =0/1; D1: Moderate+Severe+Extreme, D2: Severe+Extreme, D3: Extreme

Real, Dimension(Nst,Ntime) :: Dur1, Dur2, Dur3        ! Tan so cac dot
Real, Dimension(Nst)       :: N_Dur1, N_Dur2, N_Dur3  ! Max cua Do dai cac dot

! ENSO investigate:
Real, dimension(Nreg+1,NEnso) :: Geo_ENSO_Reg_D1, Geo_ENSO_Reg_D2, Geo_ENSO_Reg_D3      ! Spatial Extent with respect to ENSO phases
Real, dimension(Nreg+1,NEnso) :: Fre_ENSO_Reg_D1, Fre_ENSO_Reg_D2, Fre_ENSO_Reg_D3      ! Frequency (NDmon/NEnsoMon) with respect to ENSO phases
Real, dimension(Nreg+1,NEnso) :: Sev_ENSO_Reg_D1, Sev_ENSO_Reg_D2, Sev_ENSO_Reg_D3      ! Severity with respect to ENSO phases
Real, Dimension(Nst,NEnso)    :: Fre_ENSO_D1, Fre_ENSO_D2, Fre_ENSO_D3      ! Frequency (NDmon/NEnsoMon) with respect to ENSO phases
Real, Dimension(Nst,NEnso)    :: Sev_ENSO_D1, Sev_ENSO_D2, Sev_ENSO_D3      ! Severity with respect to ENSO phases
Integer, dimension(Ntime)     :: ENSO     ! Based on ONI: El Nino: 1; La Nina=-1; Neutral=0

!Real, dimension(Nreg+1,Ntime) :: Fre_D1, Fre_D2, Fre_D3                     ! Frequency
!Real, dimension(Nreg+1,Ntime) :: Sev_D1, Sev_D2, Sev_D3                     ! Severity 
Real, dimension(Nreg+1,Ntime) :: Geo_D1, Geo_D2, Geo_D3                     ! Spatial Extent 7 subregion + Entire Country
Real, Dimension(Nst,Ntime) :: Spel1, Spel2, Spel3 ! Do dai tung dot cua tung tram
Real, Dimension(Nst,Ntime) :: DS1, DS2, DS3, DI1, DI2, DI3 ! Severity & Intensity of each spell at each grid    <<<<<<<<<
Real, Dimension(Nst)       :: N_Spel1, N_Spel2, N_Spel3 ! So dot trong toan chuoi cua tung tram
Real, Dimension(Nst,Ntime) :: IS1, IS2, IS3   ! Chi so dau cua cac dot trong chuoi
Real, Dimension(Nst,Ntime) :: IE1, IE2, IE3   ! Chi so cuoi cua cac dot trong chuoi

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)
Integer :: Max_So_Dot_Reg        ! So dot lon nhat cua tat ca cac tram (=Max {Ndot} - Gia tri lon nhat cua N_Spell)
Integer :: Max_Length_Dot_Reg    ! Do keo dai dai nhat trong tat ca cac dot cua cac tram (=Max {MaxDot} - Gia tri lon nhat cua Imax)

!Real :: PDSI(Nst,Ntime) ! Drought Index
!Real :: T2m(Nst,Ntime), Prec(Nst,Ntime)   ! Monthly Temperature and Precipitation 
!Real :: Lon(Nst), Lat(Nst), Reg(Nst)

! For 1980-2014 period:
Real, Allocatable, Dimension (:,:) :: PDSI, T2m, Prec   ! Monthly Temperature and Precipitation 
Real, Allocatable, Dimension (:)   :: Reg
! For 1961-2014 period:
Real, Allocatable, Dimension (:,:) :: PDSI12, T2m12, Prec12   ! Monthly Temperature and Precipitation 
Real, Allocatable, Dimension (:)   :: Reg12

!----------------------------------------------------------
Real, dimension (Nst, Nmon)    :: T2m_mon, Pre_mon   ! Trung binh thang nhieu nam
Real, dimension (Nst, Ntime)   :: T2m_Ano, Pre_Ano   ! Anomaly

Real, dimension (Nreg+1,Ntime) :: T2m_Reg, Pre_Reg
Real, dimension (Nreg+1,Nmon)  :: T2m_Reg_Mon, Pre_Reg_Mon

Real, dimension (Nst,Nmon) :: Mon_NDM_D1, Mon_NDM_D2, Mon_NDM_D3   ! Monthly of Num Dr Mon_D1,2,3
Real, dimension (Nst,Nmon) :: Mon_Sev_D1, Mon_Sev_D2, Mon_Sev_D3   ! Monthly of DS1,2,3

Real, dimension (Nreg+1,Nmon) :: Sum_Geo_D1, Sum_Geo_D2, Sum_Geo_D3   ! Sum over Ntime of Geo_D1,2,3
Real, dimension (Nreg+1,Nmon) :: Sum_NDM_D1, Sum_NDM_D2, Sum_NDM_D3   ! Sum over Ntime of Num Dr Mon_D1,2,3
Real, dimension (Nreg+1,Nmon) :: Sum_Sev_D1, Sum_Sev_D2, Sum_Sev_D3   ! Sum over Ntime of DS1,2,3

!08/06: To be considerd

!Real, dimension (Nreg+1,Nmon) :: Std_Geo_D1, Std_Geo_D2, Std_Geo_D3   ! Sum over Ntime of Geo_D1,2,3
!Real, dimension (Nreg+1,Nmon) :: Std_NDM_D1, Std_NDM_D2, Std_NDM_D3   ! Sum over Ntime of Num Dr Mon_D1,2,3
!Real, dimension (Nreg+1,Nmon) :: Std_Sev_D1, Std_Sev_D2, Std_Sev_D3   ! Sum overNtime of DS1,2,3

Real, Dimension (Nst) :: Nmon_D1, Nmon_D2, Nmon_D3      ! Sum over Ntime of D1,2,3 
Real, Dimension (Nst) :: Sum_DS1, Sum_DS2, Sum_DS3   ! Sum over Ntime of Drought Severity based on PDSI & D1-3
Real, Dimension (Nst) :: Inter_Arr_Time_D1, Inter_Arr_Time_D2, Inter_Arr_Time_D3
Real, Dimension (Nst) :: Mean_Duration_D1, Mean_Duration_D2, Mean_Duration_D3

! Regional mean of Drought Characteristics over entire periods:
Real, dimension (Nreg+1) :: DMon1, DMon2, DMon3, &
                            DGeo1, DGeo2, DGeo3, &
                            DDur1, DDur2, DDur3, &
                            DInt1, DInt2, DInt3, &
                            DSev1, DSev2, DSev3

! Trend:
Real, Dimension (Nst)      :: Sen_PDSI, Pro_PDSI  ! Sen's slopes and p-values
Real, Dimension (Nst,Nmon) :: Sen_Mon,  Pro_Mon   ! Sen's slopes and p-values  
Real, Dimension (Nreg+1, N_ClimIDX) :: Corr_Cli_Geo_D1, Corr_Cli_Geo_D2, Corr_Cli_Geo_D3
Real, Dimension (Nreg+1, N_ClimIDX) :: Prob_Cli_Geo_D1, Prob_Cli_Geo_D2, Prob_Cli_Geo_D3

Logical :: status
Character (Len=255) :: Dir, Fname
!-----------------------------------------------------------------------------
Dir="/home/tanpv/SEACLID_New/RES_Stn/"

!!!  Call Read_Clim_IDX              ! Return: ClimIDX, CL_Name (N_ClimIDX)
!!!  Call Read_ENSO_Phase            ! Return: ENSO(it)

! print "(12I3)", ENSO; pause
! Reading PDSI
  Fname="/home/tanpv/SEACLID_New/RES/PDSI_Res/PDSI_Stn_No_Rain_Center.nc"
  Call Read_PDSI_Stn (Fname)     ! Read PDSI, T2m, Prec(Nst,Ntime), and Reg(Nst)

  Fname="/home/tanpv/SEACLID_New/RES/PDSI_Res/PDSI_Stn_1961_2014.nc"
  Call Read_PDSI_Stn_61_14 (Fname)

  Where (PDSI<-20.0) PDSI=-20.0; Where (PDSI12<-20.0) PDSI12=-20.0

  print*,PDSI
  pause
  print*,PDSI12

!!!  Call Initialization
!!!  Call Define_Drought_Events     ! Return D1, D2, D3 (Nst,Ntime)

!!!  Call Calc_Spells               ! Return Dur1,2,3; Spel1,2,3; IS1,2,3; IE1,2,3; N_Dur1,2,3; Max_Length_Dot, Max_So_Dot
!!!  Call Calc_Mean_Inter_Arr_Time  ! Return Inter_Arr_Time_D1,2,3; Mean_Duration_D1,2,3
!!!  Call Calc_Geographic_Extent    ! Return Geo_D1, Geo_D2, Geo_D3(Nreg+1,Ntime)
!!!  Call Calc_Drought_Characteristics  ! Return Nmon_D1,2,3; Sum_DS1,2,3; Sum_Geo_D1,2,3; DMon1,2,3; DDur1,2,3; DInt1,2,3; DSev1,2,3; DGeo1,2,3
!!!  Call Calc_Trend_Sen            ! Retunr Sen_PDSI, Pro_PDSI(Nst)

!!!  Fname=trim(Dir)//"PDSI_Regional_Monthly_T_P_Geo.nc"
!!!  Call Write_2D_Reg (Fname)
!!!  Fname=trim(Dir)//"PDSI_Stn_4_Chars.nc"
!!!  Call Write_1D_Char_Stn (Fname)
!!!  Fname=trim(Dir)//"PDSI_SenSlp.nc"
!!!  Call Write_1D_Senslp (Fname)
!!!  Fname=trim(Dir)//"PDSI_SenSlp_Mon.nc"
!!!  Call Write_2D_SenSlp (Fname)
!!!  Fname=trim(Dir)//"PDSI_Reg_5_Chars.nc"
!!!  Call Write_1D_Char_Reg (Fname)
!!!  Fname=trim(Dir)//"PDSI_Drought_ENSO_Reg.nc"
!!!  Call Write_2D_Reg_ENSO (Fname)
!!!  Fname=trim(Dir)//"PDSI_Drought_ENSO_Stn.nc"
!!!  Call Write_2D_Drought_ENSO_Stn (Fname)
!!!  Fname=trim(Dir)//"PDSI_Corr_Geo_Ext_ClimIDXs_Reg.nc"
!!!  Call Calc_Corr_Geo_Ext_ClimIDXs (Fname)
!!!  Fname=trim(Dir)//"PDSI_Corr_Geo_Ext_EOF_Cli_Reg.nc"
!!!  Call Calc_Corr_Geo_Ext_EOF_Clim (Fname)
CONTAINS
!-----------------------------------------------------------------------------

Subroutine Write_PDSI_131_83_Stn_to_NC

Fname="/home/tanpv/SEACLID_New/Post_Pro_Stn_No_Rain_Center/PDSI_Stat_Area_Spell_D2.nc"
Call wr_ncdf_2d (trim(Fname), Xreg, YSpel, Nreg, NSpel_Max, XSpell, "Spells",
"Drought Spells", "Mon", "Subregion", "Spell", Miss)
Call wr_ncdf_2d_apd (trim(Fname), XArea, "Area", Nreg, NSpel_Max, "Geographic
Extent", "%", "Subregion", "Spell", Miss)
Deallocate(YSpel,XSpell,XArea)


End Write_PDSI_131_83_Stn_to_NC
!---------------------------------------------- 
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Corr_Geo_Ext_EOF_Clim.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Corr_Geo_Ext_ClimIDXs.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Write_2D_Drought_ENSO_Stn.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Write_2D_Reg_ENSO.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Read_ENSO_Phase.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Read_Clim_IDX.f90"
Include "../Post_Stn_Apr_2018/SUB/Read_PDSI_Stn.f90"
Include "Read_PDSI_Stn_61_14.f90"

!!! Include "../Post_Stn_Apr_2018/SUB/Initialization.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Define_Drought_Events.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Spells.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Mean_Inter_Arr_Time.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Geographic_Extent.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Drought_Characteristics.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Trend_Sen.f90"

!!! Include "../Post_Stn_Apr_2018/SUB/Write_2D_Reg.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Write_1D_Char_Stn.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Write_1D_Senslp.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Write_1D_Char_Reg.f90"
!!! Include "../Post_Stn_Apr_2018/SUB/Write_2D_SenSlp.f90"

!!! Include "../Libs/Sub_Trend_MK_Linear.f90"
!Include "../Libs/sub_rd_ncdf.F90"
Include "../Libs/sub_rd_ncdf_Tan.F90"
Include "../Libs/sub_wr_ncdf_Tan.F90"
Include "../Post_Grd/All_Num_Dr_Spells.f90"  ! Tinh ca do dai tung dot
Include "../Post_Grd/SUB_New/Time_Ave.f90"

!!! Include "../Post_Stn_Apr_2018/SUB/Calc_Correlation_Coefficient.f90"

!-----------------------------------------------------------------------------
END

Include "../Libs/Student_Test.f90"
Include "../Libs/Special_Function.f90"

