      SUBROUTINE soil(NA,ND,NT,NE,NAX,NDX,NTX,NEX
     2,NHW,NHE,NVN,NVS)
C
C     THIS IS THE MAIN SUBROUTINE FROM WHICH ALL OTHERS ARE CALLED
C
      include "parameters.h"
      include "filec.h"
      include "files.h"
      include "blkc.h"
      CHARACTER*16 DATA(30),DATAC(30,250,250),DATAP(JP,JY,JX)
     2,DATAM(JP,JY,JX),DATAX(JP),DATAY(JP),DATAZ(JP,JY,JX)
     3,OUTS(10),OUTP(10),OUTFILS(10,JY,JX),OUTFILP(10,JP,JY,JX)
      CHARACTER*3 CHOICE(102,20)
      CHARACTER*8 CDATE
      SAVE NF,NX,NTZ,NTZX,NFX
      DATA NF,NX,NTZ,NTZX,NFX/0,0,0,0,0/
C
C     READ INPUT DATA FOR SITE, SOILS AND MANAGEMENT IN 'READS'
C     AND SET UP OUTPUT AND CHECKPOINT FILES IN 'FOUTS'
C
      IF(IGO.EQ.0)THEN
C     WRITE(*,333)'READI'
      CALL READI(NA,ND,NT,NE,NAX,NDX,NTX,NEX,NF,NFX,NTZ
     2,NTZX,NHW,NHE,NVN,NVS)
      ENDIF
C     WRITE(*,333)'READS'
      CALL READS(NA,ND,NT,NE,NAX,NDX,NTX,NEX,NF,NFX,NTZ
     2,NTZX,NHW,NHE,NVN,NVS)
C     WRITE(*,333)'FOUTS'
      CALL FOUTS(NT,NE,NAX,NDX,NTX,NEX,NF,NFX,NHW,NHE,NVN,NVS)
C
C     INITIALIZE ALL SOIL VARIABLES IN 'STARTS'
C
      IF((DATA(20).EQ.'YES'.AND.IGO.EQ.0).OR.IDAYR.NE.IOLD)THEN
C     WRITE(*,333)'STARTS'
      CALL STARTS(NHW,NHE,NVN,NVS)
C
C     RECOVER VALUES OF ALL SOIL STATE VARIABLES FROM EARLIER RUN
C     IN 'ROUTS' IF NEEDED
C
      IF(DATA(20).EQ.'YES')THEN
      IF((IDAYR.GE.IRUN.AND.IYRR.EQ.IDATA(9))
     2.OR.IYRR.GT.IDATA(9))THEN
C     WRITE(*,333)'ROUTS'
      CALL ROUTS(NHW,NHE,NVN,NVS)
      ENDIF
      ENDIF
      ENDIF
C
C     RECOVER PLANT SPECIES DISTRIBUTION IN 'ROUTQ'
C
C     WRITE(*,333)'ROUTQ'
      CALL ROUTQ(NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
C
C     READ INPUT DATA FOR PLANT SPECIES AND MANAGEMENT IN 'READQ'
C     AND SET UP OUTPUT AND CHECKPOINT FILES IN 'FOUTP'
C
C     WRITE(*,333)'READQ'
      CALL READQ(NA,ND,NT,NE,NAX,NDX,NTX,NEX,NF,NFX,NTZ
     2,NTZX,NHW,NHE,NVN,NVS)
C     WRITE(*,333)'FOUTP'
      CALL FOUTP(NT,NE,NAX,NDX,NTX,NEX,NF,NFX,NHW,NHE,NVN,NVS)
C
C     INITIALIZE ALL PLANT VARIABLES IN 'STARTQ'
C
      IF((DATA(20).EQ.'YES'.AND.IGO.EQ.0).OR.IDAYR.NE.IOLD)THEN
C     WRITE(*,333)'STARTQ'
      CALL STARTQ(NHW,NHE,NVN,NVS,1,5)
C
C     RECOVER VALUES OF ALL PLANT STATE VARIABLES FROM EARLIER RUN
C     IN 'ROUTP' IF NEEDED
C
      IF(DATA(20).EQ.'YES')THEN
      IF((IDAYR.GT.IRUN.AND.IYRR.EQ.IDATA(9))
     2.OR.IYRR.GT.IDATA(9))THEN
C     WRITE(*,333)'ROUTP'
      CALL ROUTP(NHW,NHE,NVN,NVS)
      ENDIF
      ENDIF
      ENDIF
C
C     INITIALIZE ALL SOIL CHEMISTRY VARIABLES IN 'STARTE'
C
C     WRITE(*,333)'STARTE'
      CALL STARTE(NHW,NHE,NVN,NVS)
C
C     BEGIN DAILY TIME STEP
C
9000  I=IDAYR+1
      IF(I.NE.LYRC.OR.NYR.NE.0)THEN
      IF(I.GT.LYRX.AND.NYR.NE.0)THEN
      I=I-LYRX
      IYRR=IYRR+1
      ENDIF
      ENDIF
      IF(I.GT.IFIN)GO TO 9999
C     WRITE(*,333)'FINISH'
C
C     UPDATE DAILY VARIABLES SUCH AS MANAGEMENT INPUTS
C
      CALL DAY(I,NHW,NHE,NVN,NVS)
      DO 9995 J=1,24
C
C     UPDATE HOURLY VARIABLES IN 'HOUR1'
C
C     WRITE(*,333)'WTHR'
      CALL WTHR(I,J,NHW,NHE,NVN,NVS)
C     WRITE(*,333)'HOUR1'
333   FORMAT(A8)
      CALL HOUR1(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE SOIL ENERGY BALANCE, WATER AND HEAT FLUXES IN 'WATSUB'
C
C     WRITE(*,333)'WAT'
      CALL WATSUB(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE SOIL BIOLOGICAL TRANSFORMATIONS IN 'NITRO'
C
C     WRITE(*,333)'NIT'
      CALL NITRO(I,J,NHW,NHE,NVN,NVS)
C
C     UPDATE PLANT PHENOLOGY IN 'HFUNC'
C
C     WRITE(*,333)'HFUNC'
      CALL HFUNC(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE CANOPY CO2 UPTAKE AT FULL TURGOR, CANOPY WATER POTENTIAL,
C     HYDRAULIC AND STOMATAL RESISTANCES,AND CANOPY ENERGY BALANCE IN 'UPTAKE'
C     CALCULATE ROOT UPTAKE OF WATER, OXYGEN, NH4, NO3 AND PO4 IN 'UPTAKE'
C
C     WRITE(*,333)'UPTK'
      CALL UPTAKE(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE CANOPY CO2 UPTAKE AT AMBIENT TURGOR, AUTOTROPHIC AND GROWTH
C     RESPIRATION, PLANT C ALLOCATION, CANOPY AND ROOT GROWTH IN 'GROSUB'
C
C     WRITE(*,333)'GRO'
      CALL GROSUB(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE ROOT-SOIL C AND NUTRIENT EXCHANGE FOR ALL PLANT SPECIES
C     IN 'EXTRACT'
C
C     WRITE(*,333)'EXTR'
      CALL EXTRACT(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE SOLUTE EQUILIBRIA IN 'SOLUTE'
C
C     WRITE(*,333)'SOL'
      CALL SOLUTE(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE GAS AND SOLUTE FLUXES IN 'TRNSFR'
C
C     WRITE(*,333)'TRN'
      CALL TRNSFR(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE ADDITIONAL SOLUTE FLUXES IN 'TRNSFRS' IF SALT OPTION SELECTED
C
      CALL TRNSFRS(I,J,NHW,NHE,NVN,NVS)
C
C     CALCULATE SOIL SEDIMENT TRANSPORT IN 'EROSION'
C
      CALL EROSION(I,J,NHW,NHE,NVN,NVS)
C
C     UPDATE ALL SOIL STATE VARIABLES FOR WATER, HEAT, GAS, SOLUTE
C     AND SEDIMENT FLUXES IN 'REDIST'
C
C     WRITE(*,333)'RED'
      CALL REDIST(I,J,NHW,NHE,NVN,NVS)
C     WRITE(*,333)'END'
C
C     WRITE HOURLY SOIL AND PLANT OUTPUT IN 'OUTSH' AND 'OUTPH'
C
      IF((J/JOUT)*JOUT.EQ.J)THEN
C     WRITE(*,333)'OUTSH'
      CALL OUTSH(I,J,NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
      CALL OUTPH(I,J,NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
      ENDIF
C
C     WRITE OUTPUT FOR DYNAMIC VISUALIZATION
C
      IF(DATA(18).EQ.'YES')THEN
      IF((J/JOUT)*JOUT.EQ.J)THEN
C     WRITE(*,333)'VIS'
      CALL VISUAL(I,J,NHW,NHE,NVN,NVS)
      ENDIF
      ENDIF
9995  CONTINUE
      IF(DATA(19).EQ.'YES'.AND.KOUT.GT.0)THEN
C
C     WRITE ALL SOIL AND PLANT STATE VARIABLES AND OTHER INFORMATION
C     NEEDED TO RE-INITIALIZE THE MODEL TO CHECKPOINT FILES
C     IN 'WOUTS', 'WOUTP' AND 'WOUTQ'
C
      IF((I/KOUT)*KOUT.EQ.I.OR.I.EQ.IFIN)THEN
C     WRITE(*,333)'WOUTS'
      CALL WOUTS(I,NHW,NHE,NVN,NVS)
      CALL WOUTP(I,NHW,NHE,NVN,NVS)
      CALL WOUTQ(I,NHW,NHE,NVN,NVS)
      ENDIF
      ENDIF
C
C     WRITE DAILY SOIL AND PLANT OUTPUT IN 'OUTSD' AND 'OUTPD'
C
      IF((I/IOUT)*IOUT.EQ.I)THEN
C     WRITE(*,333)'OUTSD'
      CALL OUTSD(I,NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
      CALL OUTPD(I,NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
      ENDIF
C
C     PERFORM MASS AND ENERGY BALANCE CHECKS IN 'EXEC'
C
C     WRITE(*,333)'EXEC'
      CALL EXEC(I)
C
C     RE-INITIALIZE MODEL FROM CHECKPOINT FILES IF NEEDED
C
      IF(NYR.NE.1.AND.IDAYR.NE.IOLD)THEN
C     WRITE(*,333)'REINIT'
      CALL ROUTS(NHW,NHE,NVN,NVS)
      CALL ROUTP(NHW,NHE,NVN,NVS)
      ENDIF
      GO TO 9000
9999  CONTINUE
C     WRITE(*,333)'LOOP'
C
C     WRITE OUTPUT FILES FOR EACH GRID CELL IN 'SPLIT'
C
Cifdef _WIN_
      CALL SPLIT(NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
Celse
C     CALL SPLITC(NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
Cendif
C     WRITE(*,333)'SPLIT'
      RETURN
      END
