; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=MATRIX PRNFILE="{SCENARIO_DIR}\output\TAMAT00A.PRN" MSG='3) Combine Peak and Off-Peak files'
FILEI DBI[1] = "{SCENARIO_DIR}\output\TLINKPK1_{alt}{year}.DBF"
FILEI DBI[6] = "{SCENARIO_DIR}\output\TLINKOP3_{alt}{year}.DBF"
FILEI DBI[5] = "{SCENARIO_DIR}\output\TLINKOP1_{alt}{year}.DBF"
FILEI DBI[2] = "{SCENARIO_DIR}\output\TLINKPK3_{alt}{year}.DBF"

; ***************************************************************************************************
; SCRIPT TO COMBINE THE PEAK FILES TO ONE PEAK FILE AND OFF-PEAK FILES TO ONE OFF-PEAK FILE
;****************************************************************************************************


;***************************************************************************************************
;DEFINE PEAK AND OFF-PEAK FILES AS INPUT AND COMBINED PEAK AND OFF-PEAK FILES AS OUTPUT FILES
;***************************************************************************************************



FILEO RECO[2] = "{SCENARIO_DIR}\OUTPUT\TLINKOP_{ALT}{YEAR}.DBF",
fields =   A , B, MODE,  OPERATOR , NAME , DIST,  TIME  ,   LINKSEQ   , HEADWAY ,   VOL1 ,VOL2 ,VOL3, VOL4,  STOPA ,  ONA1 ,ONA2 ,ONA3 , ONA4, OFFA1, OFFA2, OFFA3 , OFFA4, STOPB,   ONB1, ONB2, ONB3 , ONB4, OFFB1 ,OFFB2 ,OFFB3, OFFB4, REV_VOL1, REV_VOL2, REV_VOL3, REV_VOL4, REV_ONA1 ,REV_ONA2, REV_ONA3, REV_ONA4, REV_OFFA1, REV_OFFA2 ,REV_OFFA3 , REV_OFFA4, REV_ONB1, REV_ONB2, REV_ONB3 , REV_ONB4, REV_OFFB1 ,REV_OFFB2, REV_OFFB3, REV_OFFB4, index, 
cform=40.0 fields=longname
FILEO RECO[1] = "{SCENARIO_DIR}\OUTPUT\TLINKPK_{ALT}{YEAR}.DBF",
fields =   A , B, MODE,  OPERATOR , NAME , DIST,  TIME  ,   LINKSEQ   , HEADWAY ,   VOL1 ,VOL2 ,VOL3, VOL4,  STOPA ,  ONA1 ,ONA2 ,ONA3 , ONA4, OFFA1, OFFA2, OFFA3 , OFFA4, STOPB,   ONB1, ONB2, ONB3 , ONB4, OFFB1 ,OFFB2 ,OFFB3, OFFB4, REV_VOL1, REV_VOL2, REV_VOL3, REV_VOL4, REV_ONA1 ,REV_ONA2, REV_ONA3, REV_ONA4, REV_OFFA1, REV_OFFA2 ,REV_OFFA3 , REV_OFFA4, REV_ONB1, REV_ONB2, REV_ONB3 , REV_ONB4, REV_OFFB1 ,REV_OFFB2, REV_OFFB3, REV_OFFB4, index,
cform=40.0 fields=longname








;************************************
; MAIN SCRIPT AREA
;************************************


zones=1

;***********************************************
;READ IN DATA FROM PK1 FILE AND WRITE TO PK FILE
;***********************************************

loop k=1,DBI.1.NUMRECORDS
 x=DBIReadRecord(1,k)
if(di.1.mode >20)
 RO.A = DI.1.A
 RO.B=DI.1.B
 RO.MODE=DI.1.MODE
 RO.OPERATOR=DI.1.OPERATOR
 RO.NAME=DI.1.NAME
 RO.LONGNAME=DI.1.LONGNAME
 RO.DIST=DI.1.DIST
 RO.LINKSEQ=DI.1.LINKSEQ
 RO.STOPA=DI.1.STOPA
 RO.STOPB=DI.1.STOPB
 RO.TIME=DI.1.TIME

 RO.HEADWAY=DBI.1.NFIELD[10]
 RO.VOL1=DI.1.VOL
 RO.ONA1=DI.1.ONA
 RO.OFFA1=DI.1.OFFA
 RO.ONB1=DI.1.ONB
 RO.OFFB1=DI.1.OFFB
 RO.REV_VOL1=DI.1.REV_VOL
 RO.REV_ONA1=DI.1.REV_ONA
 RO.REV_ONB1=DI.1.REV_ONB
 RO.REV_OFFA1=DI.1.REV_OFFA
 RO.REV_OFFB1=DI.1.REV_OFFB
 ro.index=k
 write reco=1
endif
endloop


;***********************************************
;READ IN DATA FROM PK2 FILE AND WRITE TO PK FILE
;***********************************************



loop k=1,DBI.2.NUMRECORDS
 x=DBIReadRecord(2,k)
 if(di.2.mode >20)
  RO.A = DI.2.A
  RO.B=DI.2.B
  RO.MODE=DI.2.MODE
  RO.OPERATOR=DI.2.OPERATOR
  RO.NAME=DI.2.NAME
  RO.LONGNAME=DI.2.LONGNAME
  RO.DIST=DI.2.DIST
  RO.LINKSEQ=DI.2.LINKSEQ
  RO.STOPA=DI.2.STOPA
  RO.STOPB=DI.2.STOPB
  RO.TIME=DI.2.TIME

  RO.HEADWAY=DBI.2.NFIELD[10]
  RO.VOL2=DI.2.VOL
  RO.ONA2=DI.2.ONA
  RO.OFFA2=DI.2.OFFA
  RO.ONB2=DI.2.ONB
  RO.OFFB2=DI.2.OFFB
  RO.REV_VOL2=DI.2.REV_VOL
  RO.REV_ONA2=DI.2.REV_ONA
  RO.REV_ONB2=DI.2.REV_ONB
  RO.REV_OFFA2=DI.2.REV_OFFA
  RO.REV_OFFB2=DI.2.REV_OFFB
  RO.VOL1=0
  RO.ONA1=0
  RO.OFFA1=0

  RO.ONB1=0
  RO.OFFB1=0
  RO.REV_VOL1=0
  RO.REV_ONA1=0

  RO.REV_ONB1=0
  RO.REV_OFFA1=0
  RO.REV_OFFB1=0
  ro.index=k

  write reco =1
 endif
endloop



;***********************************************
;READ IN DATA FROM OP1 FILE AND WRITE TO OP FILE
;***********************************************

loop k=1,DBI.5.NUMRECORDS
 x=DBIReadRecord(5,k)
 if(di.5.mode >20)
 RO.A=DBI.5.NFIELD[1]
 RO.B=DBI.5.NFIELD[2]
 RO.MODE=DBI.5.NFIELD[3]
 RO.OPERATOR=DBI.5.NFIELD[4]
 RO.NAME=DBI.5.CFIELD[5]
 RO.LONGNAME=DBI.5.CFIELD[6]
 RO.DIST=DBI.5.NFIELD[7]
 RO.LINKSEQ=DBI.5.NFIELD[9]
 RO.STOPA=DI.5.STOPA
 RO.STOPB=DI.5.STOPB
 RO.TIME=DBI.5.NFIELD[8]
 
 RO.HEADWAY=DBI.5.NFIELD[10] 

 RO.VOL1=DI.5.VOL
 RO.ONA1=DI.5.ONA
 RO.OFFA1=DI.5.OFFA
 RO.ONB1=DI.5.ONB
 RO.OFFB1=DI.5.OFFB
 RO.REV_VOL1=DI.5.REV_VOL
 RO.REV_ONA1=DI.5.REV_ONA
 RO.REV_ONB1=DI.5.REV_ONB
 RO.REV_OFFA1=DI.5.REV_OFFA
 RO.REV_OFFB1=DI.5.REV_OFFB
 ro.index=k


RO.VOL2=0
RO.ONA2=0
RO.OFFA2=0
RO.ONB2=0
RO.OFFB2=0
RO.REV_VOL2=0
RO.REV_ONA2=0
RO.REV_ONB2=0
RO.REV_OFFA2=0
RO.REV_OFFB2=0

 write reco=2
endif
endloop



;***********************************************
;READ IN DATA FROM OP2 FILE AND WRITE TO OP FILE
;***********************************************



loop k=1,DBI.6.NUMRECORDS
x=DBIReadRecord(6,k)
if(di.6.mode >20)
RO.A=DBI.6.NFIELD[1]
RO.B=DBI.6.NFIELD[2]
RO.MODE=DBI.6.NFIELD[3]
RO.OPERATOR=DBI.6.NFIELD[4]
RO.NAME=DBI.6.CFIELD[5]
RO.LONGNAME=DBI.6.CFIELD[6]
RO.DIST=DBI.6.NFIELD[7]
RO.LINKSEQ=DBI.6.NFIELD[9]
RO.STOPA=DI.6.STOPA
RO.STOPB=DI.6.STOPB
RO.TIME=DBI.6.NFIELD[8]
 
RO.HEADWAY=DBI.6.NFIELD[10] 
RO.VOL2=DI.6.VOL
RO.ONA2=DI.6.ONA
RO.OFFA2=DI.6.OFFA
RO.ONB2=DI.6.ONB
RO.OFFB2=DI.6.OFFB
RO.REV_VOL2=DI.6.REV_VOL
RO.REV_ONA2=DI.6.REV_ONA
RO.REV_ONB2=DI.6.REV_ONB
RO.REV_OFFA2=DI.6.REV_OFFA
RO.REV_OFFB2=DI.6.REV_OFFB

RO.VOL1=0
RO.ONA1=0
RO.OFFA1=0
RO.ONB1=0
RO.OFFB1=0
RO.REV_VOL1=0
RO.REV_ONA1=0
RO.REV_ONB1=0
RO.REV_OFFA1=0
RO.REV_OFFB1=0
ro.index=k
write reco=2
endif
endloop



;endif
;*******************************************************************************************
; REACHED END OF SCRIPT
;*******************************************************************************************



ENDRUN
