	MODULE ModuleDefs3
	implicit none
	
	Integer numA, ncols, nrows,hour,runflag
	Double Precision Q,Pi,romin,cummF
	Double Precision angles(7)

!!!!!!!!!Rafa Zbrent variables!!!!!!!!!!!!!!!!!!!!
      Double Precision xk,xe,volZ,xaZ,xbZ

	Parameter (numA=7)
	Parameter (ncols = 7)
	Parameter (nrows = 7)
	Parameter (Pi = 3.14159265358979d0)
	
	Type FrontType
		Sequence
		Double Precision FAmt
		Double Precision WC
		Double Precision R(8)
		Double Precision x(8)
		Double Precision y(8)
		Double Precision kint
		Double Precision a
		Double Precision b
		Double Precision vol
		Double Precision SA
		Double Precision TInfil
		Double Precision RootAbs
		Double Precision RootAbsTotal
		Double Precision RootAbsActual
		Double Precision ThetaRA
!		Double Precision romin	
	End Type
		
	Type CellRateType
		Sequence
		Double Precision NormalRate
		Double Precision XDirRate
		Double Precision YDirRate
	End Type

	Type CellType
		Sequence
		Double Precision x1
		Double Precision x2
		Double Precision y1
		Double Precision y2
		Double Precision Width
		Double Precision Thick
		Double Precision CenterX
		Double Precision CenterY
		Double Precision NormalAngle
		Double Precision NormalAngleDeg
		Double Precision d(4)
		Double Precision e(4)
		Double Precision f(4)
		Double Precision g(4)
		Double Precision Area(4)
		Double Precision AreaTemp(4)
		Double Precision AvgWC
		Double Precision AvgWCold
		Double Precision RA
		Type (CellRateType) Rate
		
	End Type
		
	Type SoilDataType
		Sequence
		Double Precision H
		Double Precision Ksat
		Double Precision wcs
		Double Precision wcr
		Double Precision wci
		Double Precision wcmin
		Double Precision lambda
	End Type

cccc used for testing; root abs data will be provided by main calling program	
	Type RootAbsType
	  Sequence
	  Double Precision Percent
	  
	End Type
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc	
	
	
	Type (FrontType) WF3(4)
	Type (cellType) cell(nrows,ncols)
	Type (SoilDataType) SD
	Type (RootAbsType) cellRA(nrows,ncols)
	
	End MODULE
	
	MODULE ModuleDefs
	implicit none
	
	Integer MaxNumLayers, MaxNumPrecips
!	Double Precision deltaT 
	Parameter (MaxNumLayers = 50)
	Parameter (MaxNumPrecips = 1000)
!	Parameter (deltaT = 0.5)


	Type WettingFrontDataType
!		Sequence
		Double Precision z
		Double Precision FAmt
		Double Precision WC
		Double Precision WCHold
		Double Precision redistTime
		Double Precision adjFactor
		Integer	NumRedist
	End Type
	
	Type (WettingFrontDataType) WF(4)

cccccccc Data Read From Files cccccccccccccccccccccccc
	Double Precision Sav
	Double Precision Ksat
	Double Precision wcs
	Double Precision wcr
	Double Precision wci
	Double Precision wcmin
	Double Precision lambda
	Double Precision PsiB
	Double Precision Smax
	Integer NumObsLayers
	Double Precision ProfileDepth(MaxNumLayers)
	Double Precision TopObsLayers(MaxNumLayers)
	Double Precision deltaT 
	Double Precision Precips(MaxNumPrecips)
					
cccccccc Computed Data NOT Read From Files ccccccccccccccc
		
	
	Integer pondFlag
	Integer redistStatus
		
cccccccc	wetting front  WF cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c     10  1 front infiltration only
c     11  1 front redistributing and possible infiltration if precip < ks
c     20  2 fronts - WF 1 redistributing only; WF 2 infiltrating only
c     21  2 fronts - WF 1 redistributing only; WF 2 redistributing w/ possible infiltration
c     30  3 fronts - WF 1 and 2 redistributing only, WF 3 infiltrating only
c     31  3 fronts - WF 1 and 2 redistributing only, WF 3 redist w/possible infil
c     40  4 fronts - WF 1, 2, and 3 redistributing only, WF 4 infil only}
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc	
	
	Double Precision  WCO		 ! water content at surface
	Double Precision  RWC		 ! relative water content
	Double Precision  fp		 ! infiltration rate
	Double Precision  Bfp		 ! potential infiltration rate
	Double Precision  timeToPond ! time to ponding
	Double Precision  tpp		 ! time shift
	Double Precision  precipRate ! precipitation rate
	Double Precision  cummPrecip ! cummulative precipitation
	Double Precision  pondingAmt ! amount of ponding on surface
	Double Precision  infilRate	 ! infiltration rate
	Double Precision  cummInfil  ! cummulative infiltration
	Double Precision  runoffAmt  ! amount water available to runoff (not ponding)	
	
	Double Precision tstarts(MaxNumPrecips) ! start time for precip info
	Double Precision tends(MaxNumPrecips)   ! end time for precip info
	
	Double Precision LayerAvgWC(MaxNumLayers) ! avg wc of Obs layers
	Double Precision LayerBottomZ(MaxNumLayers) ! depth of bottom of layer
	
	Integer NumRedist			! cumulative number of redistribution phases
	Double Precision Time		! simulation time
	Double Precision redistTime ! time within redistribution phase
	Integer tpAdj				! time adjustment for time to pond calculation
	
	
	End MODULE



c	Program OneThreeMGAR
c	USE ModuleDefs3
c	Implicit None
c	Integer Dimen
c
c	Parameter (Dimen = 1)
c
c	runFlag = 1
c	IF (Dimen.EQ.1) THEN
c		CALL MGAR
c		print *, 'Run Successful'
c	ELSEIF (Dimen.EQ.3) THEN
c		CALL ThreeDGAR
c       print *, 'Run Successful'
c	ELSE
c		print *, 'Error: Dimension must be 1 or 3 only'
c	ENDIF
c
c      End
      
	Program OneThreeMGAR      
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C     (c) Rafael Munoz-Carpena/Leslie Gowdish, UFL, carpena@ufl.edu    c
c     Select 1D or 3D MGAR options from a command line input string    C
C     USE: at the command line type "mgar 1" or "mgar 3"               C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
	USE ModuleDefs3
	Implicit None
	Integer Dimen, INARGS
c	Parameter (Dimen = 1)
	CHARACTER*1 CDIMEN

c*** Print banner
      WRITE(*,*)'   @     @  @@@  @@   @@@@    @    @@@   @@@'
      WRITE(*,*)'   @@   @@ @    @  @  @   @  @@       @  @  @'
      WRITE(*,*)'   @ @ @ @ @ @@ @@@@  @@@@    @ @@  @@   @  @'
      WRITE(*,*)'   @  @  @ @  @ @  @  @   @   @       @  @  @'
      WRITE(*,*)'   @     @  @@  @  @  @   @   @    @@@   @@@ 12/2018'
      WRITE(*,160)
      WRITE(*,*)'       L. Gowdish, R.Munoz-Carpena'
      WRITE(*,*)'       U. of FL - USA'
      WRITE(*,*)'       carpena@ufl.edu'
      WRITE(*,160)
      WRITE(*,*)'PROGRAM TO CALCULATE INFILTRATION & REDISTRIBUTION' 
      WRITE(*,*)'DURING A SERIES OF STORMS IN 1/3D DOMAINS USING THE'
      WRITE(*,*)'MGAR METHOD OF GOWDISH & MUNOZ-CARPENA (2008,2019)'
      WRITE(*,160)
      WRITE(*,*)

c*** Command line option to select Dimen (1 or 3) case
c*** Comment out the following depending for which system you complile
CDOS
      INARGS=NARGS()-1
CDOS
      IF (INARGS.EQ.1) THEN
CDOS
          CALL GETARG(1,CDIMEN)
CUNIX	INARGS=IARGC()
CUNIX	IF (INARGS.EQ.1) THEN
CUNIX	  CALL GETARG(1,FILENM1)
c***End of UNIX/DOS selection***********
          OPEN(1,STATUS='SCRATCH')
          WRITE(1,*)CDIMEN
          BACKSPACE(1)
          READ(1,*)Dimen
          CLOSE(1)
	 ELSE
		print *, 'Error: Dimension must be 1 or 3 only'
		print *, '       Type "mgar 1" or "mgar 3" only'
          STOP
       ENDIF
 
c*** Select MGAR1D or MGAR3D case and run
	runFlag = 1
	IF (Dimen.EQ.1) THEN
		print *, 'MGAR-1D case selected'
		CALL MGAR
		print *, 'Run Successful'
	  ELSE
		print *, 'MGAR-3D case selected'
		CALL ThreeDGAR
        print *, 'Run Successful'
      ENDIF

160     FORMAT(72('-'))
      
      End
       
       

