; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=MATRIX PRNFILE="D:\JOB\D3\Model\C\APPLICATIONS\TSMAT00B.PRN" MSG='Create auto-access vehicle trip table'
FILEO RECO[2] = "{SCENARIO_DIR}\output\TTAAOP_{alt}{year}.DBF",
 FIELDS=I,J,M,AutoBus,AutoProj
FILEO RECO[1] = "{SCENARIO_DIR}\output\TTAAPK_{alt}{year}.DBF",
 FIELDS=I,J,M,AutoBus,AutoProj
FILEI MATI[4] = "{SCENARIO_DIR}\output\STATIONOP2_{alt}{year}.DBF",
PATTERN=IJ:V, FIELDS=I,J,FROMNODE
FILEI MATI[3] = "{SCENARIO_DIR}\output\tloadmd_{alt}{year}.mat"
FILEI MATI[2] = "{SCENARIO_DIR}\output\STATIONPK2_{alt}{year}.DBF",
PATTERN=IJ:V, FIELDS=I,J,FROMNODE
FILEI MATI[1] = "{SCENARIO_DIR}\output\tloadam_{alt}{year}.mat"
FILEI LOOKUPI[1] = "{SCENARIO_DIR}\Output\STATION_{alt}{year}.DAT"

DistributeINTRAStep ProcessID="FSUTMSdist" ProcessList=1-4

PARAMETERS ZONEMSG=100

ARRAY buspk={ZONESA} prjpk={ZONESA} busop={ZONESA} prjop={ZONESA}

FILLMW MW[11]=MI.1.2
FILLMW MW[21]=MI.2.1
FILLMW MW[31]=MI.3.2
FILLMW MW[41]=MI.4.1

LOOKUP NAME=NearestZone,
   LOOKUP[1]=2, RESULT=3,
   INTERPOLATE=N, FAIL=0,0,0,
   LIST=T, LOOKUPI=1

LOOP x=1,{ZONESA}
  buspk[x]=0, prjpk[x]=0
  busop[x]=0, prjop[x]=0
ENDLOOP

JLOOP
  ;sum up all auto access trips from each zone
  IF (MW[11]>0)
    _statj=NearestZone(1,MW[21])
    IF (_statj>0)
      buspk[_statj] = buspk[_statj] + MW[11]
      prjpk[_statj] = prjpk[_statj] + 0
    ENDIF
  ENDIF
  IF (MW[31]>0)
    _statj=NearestZone(1,MW[41])
    IF (_statj>0)
      busop[_statj] = busop[_statj] + MW[31]
      prjop[_statj] = prjop[_statj] + 0
    ENDIF
  ENDIF
ENDJLOOP

;prepare auto access trips DBF
LOOP x=1,{ZONESA}
  IF (buspk[x]>0 || prjpk[x]>0)
    RO.I = I
    RO.J = x
    LOOP y=1
      RO.M = 1
      if (y==1) RO.AutoAccess = buspk[x] / {OCTA}
      WRITE RECO=1
    ENDLOOP
  ENDIF
  IF (busop[x]>0 || prjop[x]>0)
    RO.I = I
    RO.J = x
    LOOP y=1
      RO.M = 1
      if (y==1) RO.AutoAccess = busop[x] / {OCTA}
      WRITE RECO=2
    ENDLOOP
  ENDIF
ENDLOOP
ENDRUN
