; 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\TAMAT00E.PRN" MSG='5) Print station activity report'
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 SUMMARIZE AND REPORT STATION ACTIVITY
;************************************************************************************************

;************************************************************************************************
; INPUT FILES: 4 PEAK LOADED FILES
;              4 OFF-PEAK LOADED FILES
;              STATION DETAILS FILE 
; OUTPUT FIELS: STATION ACTIVITY REPORT FILE
;************************************************************************************************
FILEI DBI[9] = "{SCENARIO_DIR}\INPUT\STATREP_{year}{alt}.DAT",
                                      BRTNODE=1-5, DESCR(C)=8-49

;FIELDS=1-10


FILEO PRINTO[1] = "{SCENARIO_DIR}\output\Stations_{ALT}{YEAR}.prn"

;************************************************************************************************
; DEFINE ALL ARRAYS WITH SIZE AS MAX. NO. OF NODES IN A ROUTE (500)
;************************************************************************************************

ARRAY STNUM=500 STNODE=500 STNAME=500
ARRAY pACCVOL1=500 pEGRVOL1=500 pACCVOL2=500 pEGRVOL2=500 pACCVOL3=500 pEGRVOL3=500 pACCVOL5=500 pEGRVOL5=500
ARRAY pACCVOL6=500 pEGRVOL6=500 pACCVOL11=500 pEGRVOL11=500 pACCVOL12=500 pEGRVOL12=500
ARRAY pBoard=500 pAlite=500
ARRAY oACCVOL1=500 oEGRVOL1=500 oACCVOL2=500 oEGRVOL2=500 oACCVOL3=500 oEGRVOL3=500 oACCVOL5=500 oEGRVOL5=500
ARRAY oACCVOL6=500 oEGRVOL6=500 oACCVOL11=500 oEGRVOL11=500 oACCVOL12=500 oEGRVOL12=500
ARRAY oBoard=500 oAlite=500
array freqo=dbi.5.numrecords
array linkseq1=dbi.1.numrecords
array linkseq2=dbi.2.numrecords
array linkseq5=dbi.5.numrecords
array linkseq6=dbi.6.numrecords

;array routename=500-16


;************************************
; MAIN SCRIPT AREA
;************************************
ZONES=1

LOOP k=1,DBI.9.NUMRECORDS
  COMP x=DBIReadRecord(9,k)
  STNUM[k] = k
  STNODE[k] = DI.9.BRTNODE
ENDLOOP
_numStations = DBI.9.NUMRECORDS

_temp = 0

loop k=1,dbi.1.numrecords
COMP x=DBIReadRecord(1,k)
COMP xx=DBIReadRecord(2,k)

linkseq1[k]=di.1.linkseq
linkseq2[k]=di.2.linkseq

endloop

loop k=1,dbi.5.numrecords
COMP xxx=DBIReadRecord(5,k)
COMP xxxx=DBIReadRecord(6,k)

linkseq5[k]=di.5.linkseq
linkseq6[k]=di.6.linkseq

endloop

; Peak Period
LOOP k=1,DBI.1.NUMRECORDS
  IF ((k % 1000)==0) PRINT LIST="Reading peak period records...",k(C)," of ",DBI.1.NUMRECORDS(C),PRINTO=0
  COMP x=DBIReadRecord(1,k)
  _A = DI.1.A
  _B = DI.1.B

  LOOP y=1,_numStations
    COMP w=DBIReadRecord(9,y)
    ; Access Volumes by Access Mode
    IF (_B == STNODE[y])
      IF (DI.1.MODE == 1) pACCVOL1[y] = pACCVOL1[y] + DI.1.VOL
      IF (DI.1.MODE == 2) pACCVOL2[y] = pACCVOL2[y] + DI.1.VOL
      IF (DI.1.MODE ==11) pACCVOL11[y] = pACCVOL11[y] + DI.1.VOL
      IF (DI.1.MODE ==12) pACCVOL12[y] = pACCVOL12[y] + DI.1.VOL
 if(k <> dbi.1.numrecords)
     if(linkseq1[k+1]==1)
     IF (DI.1.MODE > 20) pALITE[y] = pALITE[y] + DI.1.OFFB
     endif
    else
     IF (DI.1.MODE > 20) pALITE[y] = pALITE[y] + DI.1.OFFB
     endif

    ENDIF
    ; pEGRess Volumes by pEGRess Mode
    IF (_A == STNODE[y])
      IF (DI.1.MODE == 1) pEGRVOL1[y] = pEGRVOL1[y] + DI.1.VOL
      IF (DI.1.MODE == 2) pEGRVOL2[y] = pEGRVOL2[y] + DI.1.VOL
      IF (DI.1.MODE ==11) pEGRVOL11[y] = pEGRVOL11[y] + DI.1.VOL
      IF (DI.1.MODE ==12) pEGRVOL12[y] = pEGRVOL12[y] + DI.1.VOL

      IF (DI.1.MODE > 20) pBOARD[y] = pBOARD[y] + DI.1.ONA
      IF (DI.1.MODE > 20) pALITE[y] = pALITE[y] + DI.1.OFFA
    ENDIF
  ENDLOOP
ENDLOOP


;****************************************
;try area
;****************************************
LOOP k=1,DBI.2.NUMRECORDS
  IF ((k % 1000)==0) PRINT LIST="Reading peak period records...",k(C)," of ",DBI.2.NUMRECORDS(C),PRINTO=0
  COMP x=DBIReadRecord(2,k)
  _A = DI.2.A
  _B = DI.2.B

  LOOP y=1,_numStations
    COMP w=DBIReadRecord(9,y)

    ; Access Volumes by Access Mode
    IF (_B == STNODE[y])
      IF (DI.2.MODE == 1) pACCVOL1[y] = pACCVOL1[y] + DI.2.VOL
     IF (DI.2.MODE == 2) pACCVOL2[y] = pACCVOL2[y] + DI.2.VOL
      IF (DI.2.MODE ==11) pACCVOL11[y] = pACCVOL11[y] + DI.2.VOL
      IF (DI.2.MODE ==12) pACCVOL12[y] = pACCVOL12[y] + DI.2.VOL
if(k <> dbi.2.numrecords)
     if(linkseq2[k+1]==1)
     IF (DI.2.MODE > 20) pALITE[y] = pALITE[y] + DI.2.OFFB
     endif
    else
     IF (DI.2.MODE > 20) pALITE[y] = pALITE[y] + DI.2.OFFB
     endif

    ENDIF
    ; pEGRess Volumes by pEGRess Mode
    IF (_A == STNODE[y])
      IF (DI.2.MODE == 1) pEGRVOL1[y] = pEGRVOL1[y] + DI.2.VOL
      IF (DI.2.MODE == 2) pEGRVOL2[y] = pEGRVOL2[y] + DI.2.VOL
      IF (DI.2.MODE ==11) pEGRVOL11[y] = pEGRVOL11[y] + DI.2.VOL
      IF (DI.2.MODE ==12) pEGRVOL12[y] = pEGRVOL12[y] + DI.2.VOL

      IF (DI.2.MODE > 20) pBOARD[y] = pBOARD[y] + DI.2.ONA
      IF (DI.2.MODE > 20) pALITE[y] = pALITE[y] + DI.2.OFFA
    ENDIF
  ENDLOOP
ENDLOOP





altstr='{alt}'
yearstr='{year}'
altyr=insertstr(altstr,yearstr,3)



LOOP k=1,DBI.5.NUMRECORDS
  IF ((k % 1000)==0) PRINT LIST="Reading offpeak period records...",k(C)," of ",DBI.5.NUMRECORDS(C),PRINTO=0
  COMP x=DBIReadRecord(5,k)
  _A = DI.5.A
  _B = DI.5.B
  LOOP y=1,_numStations
    COMP w=DBIReadRecord(9,y)
    ; Access Volumes by Access Mode
    IF (_B == STNODE[y])
      IF (DI.5.MODE == 1) oACCVOL1[y] = oACCVOL1[y] + DI.5.VOL
      IF (DI.5.MODE == 2) oACCVOL2[y] = oACCVOL2[y] + DI.5.VOL
      IF (DI.5.MODE ==11) oACCVOL11[y] = oACCVOL11[y] + DI.5.VOL
      IF (DI.5.MODE ==12) oACCVOL12[y] = oACCVOL12[y] + DI.5.VOL
if(k <> dbi.5.numrecords)
    if(linkseq5[k+1]==1)
     IF (DI.5.MODE > 20) oALITE[y] = oALITE[y] + DI.5.OFFB
    endif
    else
     IF (DI.5.MODE > 20) oALITE[y] = oALITE[y] + DI.5.OFFB
     endif

    ENDIF
    ; oEGRess Volumes by oEGRess Mode
    IF (_A == STNODE[y])
      IF (DI.5.MODE == 1) oEGRVOL1[y] = oEGRVOL1[y] + DI.5.VOL
      IF (DI.5.MODE == 2) oEGRVOL2[y] = oEGRVOL2[y] + DI.5.VOL
      IF (DI.5.MODE ==11) oEGRVOL11[y] = oEGRVOL11[y] + DI.5.VOL
      IF (DI.5.MODE ==12) oEGRVOL12[y] = oEGRVOL12[y] + DI.5.VOL
      IF (DI.5.MODE > 20) oBOARD[y] = oBOARD[y] + DI.5.ONA
      IF (DI.5.MODE > 20) oALITE[y] = oALITE[y] + DI.5.OFFA
    ENDIF
  ENDLOOP
ENDLOOP


LOOP k=1,DBI.6.NUMRECORDS
  IF ((k % 1000)==0) PRINT LIST="Reading offpeak period records...",k(C)," of ",DBI.6.NUMRECORDS(C),PRINTO=0
  COMP x=DBIReadRecord(6,k)
  _A = DI.6.A
  _B = DI.6.B
  LOOP y=1,_numStations
    COMP w=DBIReadRecord(9,y)
    ; Access Volumes by Access Mode
    IF (_B == STNODE[y])
     IF (DI.6.MODE == 1) oACCVOL1[y] = oACCVOL1[y] + DI.6.VOL
     IF (DI.6.MODE == 2) oACCVOL2[y] = oACCVOL2[y] + DI.6.VOL
     IF (DI.6.MODE ==11) oACCVOL11[y] = oACCVOL11[y] + DI.6.VOL
     IF (DI.6.MODE ==12) oACCVOL12[y] = oACCVOL12[y] + DI.6.VOL
 
if(k <> dbi.6.numrecords)
     if(linkseq6[k+1]==1)
     IF (DI.6.MODE > 20) oALITE[y] = oALITE[y] + DI.6.OFFB
     endif
    else
     IF (DI.6.MODE > 20) oALITE[y] = oALITE[y] + DI.6.OFFB
     endif

   ENDIF
    ; oEGRess Volumes by oEGRess Mode
    IF (_A == STNODE[y])
      IF (DI.6.MODE == 1) oEGRVOL1[y] = oEGRVOL1[y] + DI.6.VOL
      IF (DI.6.MODE == 2) oEGRVOL2[y] = oEGRVOL2[y] + DI.6.VOL
      IF (DI.6.MODE ==11) oEGRVOL11[y] = oEGRVOL11[y] + DI.6.VOL
      IF (DI.6.MODE ==12) oEGRVOL12[y] = oEGRVOL12[y] + DI.6.VOL
      IF (DI.6.MODE > 20) oBOARD[y] = oBOARD[y] + DI.6.ONA
      IF (DI.6.MODE > 20) oALITE[y] = oALITE[y] + DI.6.OFFA
    ENDIF
  ENDLOOP
ENDLOOP



loop qw=1,dbi.5.numrecords
abc=DBIReadRecord(5,qw)
freqo[qw]=dBi.5.NFIELD[10]
endloop




PRINT LIST="STATION ACTIVITY & ACCESS/EGRESS REPORT (station node is in paranthesis after name)",PRINTO=1
print list="\n",printo=1
IF (I == _ZONES)
print list="Title : ",'{SCENARIO_FULLNAME}',printo=1
print list="Alternative : ",altyr,printo=1
;print list="Year : ",'{year}',printo=1
;print list="\n",printo=1
print list='@date.rundate@',"     ",'@time.runtime@', printo=1
print list="\n",printo=1


 
PRINT LIST="\n\n",PRINTO=1
;PRINT LIST=" "(44),"       P E A K      ","    O F F P E A K   ","      D A I L Y     ",PRINTO=1
;  PRINT LIST="No.   Node      Station Name                    Access    Egress    Access    Egress    Access    Egress     ",PRINTO=1
  print list="\n",printo=1
    LOOP x=1,_numStations
    cumroutes=0
    cumpeakvph=0
    cumopeakvph=0
    COMP y=DBIReadRecord(9,x)
print list="-----------------------------------------------------------------------------------------------",printo=1
print list=stnum[x](3.0L),". Station Name : ",DI.9.DESCR(40),"(",stnode[x](5.0),")",printo=1 
print list="\n",printo=1
;print list="  Transit Service",printo=1
print list="                                   PEAK              OFF PEAK            ",printo=1
print list="  ----------------------------------+---------+---------+---------",printo=1
print list="  Transit Service          Frequency      vph  Frequency      vph ",printo=1
print list="  ----------------------------------+---------+---------+---------",printo=1
count=0
;prevroute="0"
loop n=1,dbi.1.numrecords
  zz=DBIReadRecord(1,n)

 if((stnode[x]=di.1.a)|(stnode[x]=di.1.b))

    if(di.1.mode>20)
    if(di.1.name != prevroute)
    
             if((DBI.1.NFIELD[10]=0)&(freqo[n]=0))

          print list="  ",cform=23.0 list=di.1.name,form=10.1 list=DBI.1.NFIELD[10],"0"(10.0R),freqo[n],"0"(10.0R),printo=1
             
    
             cumroutes=cumroutes+1
             cumpeakvph=cumpeakvph
             cumopeakvph=cumopeakvph


             elseif((DBI.1.NFIELD[10] !=0)&(freqo[n]=0))
               print list="  ",cform=23.0 list=di.1.name,form=10.1 list=DBI.1.NFIELD[10],(60/DBI.1.NFIELD[10]),freqo[n],"0"(10.0R),printo=1
             
             cumroutes=cumroutes+1
             cumpeakvph=cumpeakvph+(60/DBI.1.NFIELD[10])
             cumopeakvph=cumopeakvph

             elseif((DBI.1.NFIELD[10] =0)&(freqo[n]!=0))
               print list="  ",cform=23.0 list=di.1.name,form=10.1 list=DBI.1.NFIELD[10],"0"(10.0R),freqo[n],(60/freqo[n]),printo=1
             
             cumroutes=cumroutes+1
             cumpeakvph=cumpeakvph
             cumopeakvph=cumopeakvph+(60/freqo[n])

             else              
               print list="  ",cform=23.0 list=di.1.name,form=10.1 list=DBI.1.NFIELD[10],(60/DBI.1.NFIELD[10]),freqo[n],(60/freqo[n]),printo=1
             
             cumroutes=cumroutes+1
             cumpeakvph=cumpeakvph+(60/DBI.1.NFIELD[10])
             cumopeakvph=cumopeakvph+(60/freqo[n])
 
             endif
         
         prevroute=di.1.name
     endif
     endif

 endif

endloop


;print list="\n",printo=1
;print list="                                   PEAK              OFF PEAK            DAILY",printo=1
print list="  ----------------------------------+---------+---------+---------",printo=1
print list="  Total no. of services :",form=3.0 list=cumroutes,"           ",form=6.1 list=cumpeakvph,"              ",form=6.1 list=cumopeakvph,printo=1
print list="  ----------------------------------+---------+---------+---------",printo=1
print list="\n",printo=1
print list="                                   PEAK              OFF PEAK            DAILY",printo=1
print list="  ----------------------------------+---------+---------+---------+---------+--------",printo=1
print list="  Activity                   Access    Egress    Access    Egress    Access    Egress",printo=1
print list="  ----------------------------------+---------+---------+---------+---------+--------",printo=1

   ; PRINT LIST=stnum[x](4.0),stnode[x](9.0),"  ",DI.6.DESCR(40),PRINTO=1

    PRINT LIST="  ","Walk Access            ",pACCVOL1[x](10.1),pEGRVOL1[x](10.1),oACCVOL1[x](10.1),oEGRVOL1[x](10.1),(pACCVOL1[x]+oACCVOL1[x])(10.1),(pEGRVOL1[x]+oEGRVOL1[x])(10.1),PRINTO=1
    PRINT LIST="  ","Auto Access            ",pACCVOL2[x](10.1),pEGRVOL2[x](10.1),oACCVOL2[x](10.1),oEGRVOL2[x](10.1),(pACCVOL2[x]+oACCVOL2[x])(10.1),(pEGRVOL2[x]+oEGRVOL2[x])(10.1),PRINTO=1
    PRINT LIST="  ","Transfer Connector     ",pACCVOL11[x](10.1),pEGRVOL11[x](10.1),oACCVOL11[x](10.1),oEGRVOL11[x](10.1),(pACCVOL11[x]+oACCVOL11[x])(10.1),(pEGRVOL11[x]+oEGRVOL11[x])(10.1),PRINTO=1
    PRINT LIST="  ","Sidewalk Connector     ",pACCVOL12[x](10.1),pEGRVOL12[x](10.1),oACCVOL12[x](10.1),oEGRVOL12[x](10.1),(pACCVOL12[x]+oACCVOL12[x])(10.1),(pEGRVOL12[x]+oEGRVOL12[x])(10.1),PRINTO=1       
    PRINT LIST="  ","Boardings              "," "(10),pBOARD[x](10.1)," "(10),oBOARD[x](10.1)," "(10),(pBOARD[x]+oBOARD[x])(10.1),PRINTO=1
    PRINT LIST="  ","Alightings             "," "(10),pALITE[x](10.1)," "(10),oALITE[x](10.1)," "(10),(pALITE[x]+oALITE[x])(10.1),PRINTO=1
    
print list="-----------------------------------------------------------------------------------------------",printo=1
PRINT LIST="\n\n\n\n",PRINTO=1
  ENDLOOP
ENDIF

; The MATRIX module does not have any explicit phases.  The module does run within an implied ILOOP
; where I is the origin zones.  All user statements in the module are processed once for each origin.
; Matrix computation (MW[#]=) are solved for all values of J for each I.  Thus for a given origin zone I
; the values for all destination zones J are automatically computed.  The user can control the computations
; at each J by using a JLOOP.

ENDRUN
