! This simulator generates time series of groundwater level and collective precipitation

! DEFINE VARIABLES 
	Implicit none
	REAL beta
	REAL,ALLOCATABLE:: P(:),WL(:),Recession(:),DM_Simu(:),WL_Simu(:)
	REAL,ALLOCATABLE:: Period_P(:),Period_P_Simu(:),DM(:)
	Integer,ALLOCATABLE:: Period_Start(:),Period_End(:),Date_I(:)
	Integer,ALLOCATABLE:: Month(:)
	Integer Month_I,Duration_Total,i,j,k,Period,MONTHCOUNT
	Integer CURRENTMONTH
	Character gaugenam*100,wellnam*100,dummy*158,currentline*79

! READ PARAMETERS 
	Open (6,FILE='currentw.lst')
	Read (6,*) gaugenam,wellnam
	Close (6)
	Open (6,FILE=trim(gaugenam)//'\'//trim(wellnam)//'\case.pst')
	read (6,500,advance='YES') currentline 
	read (6,500,advance='YES') currentline 
	read (6,500,advance='YES') currentline 
	Read (6,*) MONTHCOUNT,dummy,dummy,dummy,dummy
	MONTHCOUNT=MONTHCOUNT-1
	Close (6)
	Allocate(DM(MONTHCOUNT))
	OPEN (2,FILE=trim(gaugenam)//'\'//trim(wellnam)//'\Para.dat')
	i=1
	do while(i.le.MONTHCOUNT)
		Read (2,*) DM(i)
		i=i+1
	end do
	Read (2,*) beta
	Close (2)
	!i=1
	!w_Total=0
	!Do while(i.le.12)
	!	w_Total=w_Total+weight(i)
	!	i=i+1
	!End do

! READ INPUT AND PERIODS
	OPEN (3,FILE=trim(gaugenam)//'\'//trim(wellnam)//'\Input.dat')
	Read (3,*) Duration_Total, dummy
	OPEN (4,FILE=trim(gaugenam)//'\'//trim(wellnam)//'\Periods.dat')
	Read (4,*) Period

! ALLOCATE DATA ARRAYS
	j = Duration_Total
	k = Period
	Allocate(P(j))
	Allocate(WL(j))
	Allocate(Date_I(j))
	Allocate(Month(j))
	Allocate(Recession(j))
	Allocate(Period_Start(k))
	Allocate(Period_End(k))
	Allocate(DM_Simu(j))
	Allocate(WL_Simu(j))
	Allocate(Period_P(k))
	Allocate(Period_P_Simu(k))

! CALCULATE Month 
	i=1
	CURRENTMONTH=0
	MONTHCOUNT=0
	do while(i.le.Duration_Total)
		Read (3,*) Date_I(i),Month(i),P(i),Recession(i),WL(i)
		IF(CURRENTMONTH.NE.Month(i)) then
			CURRENTMONTH=Month(i)
			MONTHCOUNT=MONTHCOUNT+1
		end if
		Month(i)=MONTHCOUNT
		i=i+1
	end do

	Close (3)

! READ PERIODS 
	i=1
	do while(i.le.Period)
		Read (4,*) Period_Start(i),Period_End(i)
		i=i+1
	end do
	Close (4)

! **********************************************************************
! Start Writing Results
	OPEN (1,FILE=trim(gaugenam)//'\'//trim(wellnam)//'\OUT.dat',STATUS='REPLACE')
	OPEN (5,FILE=trim(gaugenam)//'\'//trim(wellnam)//'\DM.dat',STATUS='REPLACE')
	i = 1
	WL_Simu(i)=WL(i)
	DM_Simu(i)=DM(Month(i))
		write (1,*)WL_Simu(i)
		write (5,*)DM_Simu(i)
	Do while(i.lt.Duration_Total)
		i=i+1
		WL_Simu(i)=WL_Simu(i-1)+P(i)*beta-DM(Month(i))-Recession(i)
		DM_Simu(i)=DM(Month(i))
		write (1,*)WL_Simu(i)
		write (5,*)DM_Simu(i)
	End do
	Close (5)

	j=1
	Do while(j.le.Period)
		k=1
		Period_P(j)=0
		Period_P_Simu(j)=0
		Do while(k.le.(Period_End(j)-Period_Start(j))+1)
			Period_P(j)=Period_P(j)+P(Period_Start(j)-Date_I(1)+k)
			Period_P_Simu(j)=Period_P_Simu(j)+(DM_Simu(Period_Start(j)-Date_I(1)+k)+Recession(Period_Start(j)-Date_I(1)+k))/beta
			k=k+1
		End do
		write (1,*)Period_P_Simu(j)
		j=j+1
	End do
	Close (1)
500     format(a79)
1000	Deallocate(P)
	Deallocate(WL)
	Deallocate(Date_I)
	Deallocate(Month)
	Deallocate(Recession)
	Deallocate(Period_Start)
	Deallocate(Period_End)
	Deallocate(DM_Simu)
	Deallocate(WL_Simu)
	Deallocate(Period_P)
	Deallocate(Period_P_Simu)
	STOP
	END
