C
C User specifies non-irrigation, NI, and irrigation, I, season lengths.  
C Months are entered as 1-12 for January-December.  NIBEG and NIEND are the 
C first and last months of the NI season; IBEG and IEND are the first and last 
C months of the I season, respectively.  Program calculates NICNT and ICNT 
C (the number of months per NI and I season).  
C
C Program calculates number of seconds per season, based on user-specification,
C and inserts into a new TBASIC.DAT file for MODFLOW based on the original TRBASIC.DAT.
C
C BESTSM runs from 1925-1995 while MODFLOW runs from 1915-1995.  The period
C from 1915-1925 is used in MODFLOW to initialize the model.  This program 
C modifies the BESTSM output file, BESTOUT.DAT, that begins in water year 1925 
C to begin in water year 1915.  Data BESTOUT.DAT from 1925-1935 is copied for 
C years 1915-1925.  The data is only used for an initialization period and
C should not affect model results.
C
C Program sends data to and runs the three pre-processor programs used to 
C create MODFLOW input files.
C  
      PROGRAM PREMAIN
C
      INTEGER NIBEG,NIEND,IBEG,IEND,IEND9,NICNT,ICNT,ICNT9,ICNTS
      INTEGER ICOUNT,YEAR,NISEC,ISEC,ISEC9,LOSS
      INTEGER YR(1000),YRINT(1000),MO(1000),Q1(1000),Q5MI(1000)
      REAL Q2AG(1000),Q2MI(1000),Q3AG(1000),Q4AG(1000),Q4MI(1000)
      CHARACTER*120 LINE
C
      OPEN(1,"TRBASIC.DAT",STATUS="OLD")
      OPEN(2,"TBASIC.DAT",STATUS="UNKNOWN")
      OPEN(3,"BESTOUT.DAT",STATUS="OLD")
      OPEN(4,"BESTINT.DAT",STATUS="UNKNOWN")
C
C Set the number of years to be forecasted.
C
      ICOUNT = 81
C        
C User input.  Historically, NIBEG=11, NIEND=2, IBEG=3, IEND=10.  If the user
C chooses year-round irrigation, use January as the non-irrigation season to 
C allow current MODFLOW set-up to be used.  If there is a non-irrigation 
C season, it must begin in October or later.  Send error message otherwise.
C
      WRITE(*,*) 'Enter 1 for Historic Simulation, 0 for Forecasting Sim
     !ulation: '
      READ(*,*) IHIST
  10  WRITE(*,*) 'Enter months as 1-12 for January-December.'
      WRITE(*,*)
      WRITE(*,*) 'Beginning month of the non-irrigation season'
      WRITE(*,*) '(Non-irrigation season must be at least 1 month in len
     !gth): '
      READ(*,*) NIBEG
      IF (NIBEG.LT.10 .AND. NIBEG .GT. 3) THEN
        WRITE(*,*)
        WRITE(*,*) 'THE NON-IRRIGATION SEASON MUST BEGIN IN OR AFTER OCT
     'OBER.'
        WRITE(*,*) 'PLEASE RE-ENTER DATA.'
        WRITE(*,*)
        GOTO 10
      ENDIF
      WRITE(*,*) 'Ending month of the non-irrigation season: '
      READ(*,*) NIEND
      WRITE(*,*) 'Beginning month of the irrigation season: '
      READ(*,*) IBEG
      IF (IBEG.EQ.0) THEN
        WRITE(*,*) 
        WRITE(*,*) 'AN IRRIGATION SEASON MUST BE SPECIFIED.'
        WRITE(*,*) 'PLEASE RE-ENTER DATA.'
        WRITE(*,*)
        GOTO 10
      ENDIF
      WRITE(*,*) 'Ending month of the irrigation season: '
      READ(*,*) IEND
C
C Calculate non-irrigation and irrigation season lengths.  BESTSM uses water      
C years -- data begins in month 10 and ends in month 9.  Use IEND 9 and ICNT9 
C variables to calculate the months of BESTSM data available for the irrigation
C season of the last year if it ends after month 9.
C
      IF(NIBEG.LE.NIEND) THEN
        NICNT = NIEND - NIBEG + 1
      ELSEIF(NIBEG.GT.NIEND) THEN
        NICNT = 12 - NIBEG + 1 + NIEND
      ENDIF
C
      IF(IBEG.LE.IEND) THEN
        ICNT = IEND - IBEG + 1
        IF(IEND.GT.9) THEN 
          IEND9 = 9
          ICNT9 = IEND9 - IBEG + 1
        ELSE
          IEND9 = IEND
          ICNT9 = ICNT
        ENDIF
      ELSEIF(IBEG.GT.IEND) THEN
        ICNT = 12 - IBEG + 1 + IEND
        IF(IEND.GT.9) THEN
          IEND9 = 9
          ICNT9 = IEND9 - IBEG + 1
        ELSE
          IEND9 = IEND
          ICNT9 = ICNT
        ENDIF
      ENDIF
C
C Check to make sure total season lengths add to one year.
C
      YEAR = NICNT + ICNT
      IF (YEAR.NE.12) THEN
        WRITE(*,*) 
        WRITE(*,*) 'NON-IRRIGATION AND IRRIGATION SEASON LENGTHS MUST AD
     'D UP TO 12 MONTHS.'  
        WRITE(*,*) 'PLEASE RE-ENTER DATA.'
        WRITE(*,*)
        GOTO 10
      ENDIF

      WRITE(*,*) 'Enter the % of headgate diversion lost in canals (26):
     ! '
      READ(*,*) LOSS
C
C Calculate number of seconds per season and send to TBASIC.DAT.
C
   20 NISEC = NICNT * 30.4 * 86400
      ISEC = ICNT * 30.4 * 86400
      IF(IEND.GT.9) THEN
        ISEC9 = ICNT9 * 30.4 * 86400
      ELSE
        ISEC9 = ISEC
      ENDIF
      DO 30 I=1,1310
        READ(1,101) LINE
        WRITE(2,101) LINE
   30 CONTINUE
      DO 40 I=1,ICOUNT
        IF (I.EQ.ICOUNT) ISEC = ISEC9
        WRITE(2,102) NISEC,1,1.0 
        WRITE(2,102) ISEC,1,1.0
   40 CONTINUE     
C
C Adjust BESTOUT.DAT file to begin in 1915.  Original file begins in Oct, 1924
C and ends in Oct, 1995.  Adjusted file begins in Oct, 1914 and ends in OCT, 1995.
C First 10 years are duplicated (not used in the BESTSM model).  Flows in AC-FT/Mo.
C Q1 = FLOW IN RIVER ABOVE LEASBURG CANAL, Q2AG = LEASBURG CANAL DIVERSION FOR AG USE
C Q2MI = LEASBURG CANAL DIVERSION FOR M&I USE, Q3AG = EASTSIDE CANAL DIVERSION FOR AG USE
C Q4AG = WESTSIDE CANAL DIVERSION FOR AG USE, Q4MI = WESTSIDE CANAL DIVERSION FOR M&I USE
C Q5MI = DIVERSION ABOVE EAST DRAIN FOR M&I USE
C
      DO 50 I=1,852
        READ(3,*) YR(I),MO(I),Q1(I),Q2AG(I),Q2MI(I),Q3AG(I),Q4AG(I),
     !            Q4MI(I),Q5MI(I)
   50 CONTINUE   
      DO 60 I=1,120
        YRINT(I) = YR(I) - 10
        WRITE(4,103) YRINT(I),MO(I),Q1(I),Q2AG(I),Q2MI(I),Q3AG(I),
     !               Q4AG(I),Q4MI(I),Q5MI(I)
   60 CONTINUE
      DO 70 I=1,852
        WRITE(4,103) YR(I),MO(I),Q1(I),Q2AG(I),Q2MI(I),Q3AG(I),
     !  Q4AG(I),Q4MI(I),Q5MI(I)
   70 CONTINUE
      CLOSE(1)
      CLOSE(2)
      CLOSE(3)
      CLOSE(4)
C
C Call pre-processors.  Store user-specified variables so that they can be 
C re-initialized between each subroutine.
C      
      ICNTS = ICNT
      WRITE(*,*) 'CALL STRMFORC SUB'
      CALL STRMFORC(NIBEG,NIEND,IBEG,IEND,NICNT,ICNT,ICNT9,LOSS,IHIST)
      ICNT = ICNTS
      WRITE(*,*) 'CALL WELLFORC SUB'
      CALL WELLFORC(NIBEG,NIEND,IBEG,IEND,IEND9,NICNT,ICNT,ICNT9,
     +              LOSS,IHIST)
      ICNT = ICNTS
      WRITE(*,*) 'CALL RECHFORC SUB'
      CALL RECHFORC(NIBEG,NIEND,IBEG,IEND,IEND9,NICNT,ICNT,ICNT9,
     +              LOSS,IHIST)
C
  101 FORMAT(A120)
  102 FORMAT(I10,I10,F10.1)
  103 FORMAT(I7,I3,7F12.0)
C      
      END

