; 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\BASE\HNMAT00A.PRN"
FILEO RECO[3] = "D:\JOB\D3\Model\C\BASE\INPUT\TEST.CSV"
FILEO PRINTO[1] = "D:\JOB\D3\Model\C\BASE\OUTPUT\SPDCAP_ERR.PRN"
FILEO RECO[2] = "D:\JOB\D3\Model\C\BASE\INPUT\SPD_CAP_ORG.DBF",
FIELDS=INDEX(6.0),CAPACITY(10.2),SPEED(10.2)
FILEO RECO[1] = "D:\JOB\D3\Model\C\BASE\INPUT\SPDCAP_07E.DBF",
FIELDS=INDEX(6.0),CAPACITY(10.2),SPEED(10.2)
FILEI RECI = "D:\JOB\D3\Model\C\Base\Input\DEFAULT SPDCAP.dbf"

ARRAY SPDLOOKUP=999999 CAPLOOKUP=999999,SPDORIGNL=999999, CAPORIGNL=999999

LATVAL=RI.ATLOW
HATVAL=RI.ATHIGHT
LFTVAL=RI.FTLOW
HFTVAL=RI.FTHIGHT
LLNVAL=RI.N2
HLNVAL=RI.N3
CAPVAL=RI.N4
CAPFUNC=RI.CAP_ADJ
SPDVAL=RI.N5
SPDFUNC=RI.SPD_ADJ

; There are no double lookups in Voyager script syntax,
; a INDEXVAL is used for speed/capacity lookup based on the combination of area type, facility type, and number of lane
IF (CAPFUNC=' ')
    LOOP ATYPE=LATVAL,HATVAL
      LOOP FTYPE=LFTVAL,HFTVAL
        LOOP LANES=LLNVAL,HLNVAL
          INDEXVAL=ATYPE*10000+FTYPE*100+LANES
          CAPLOOKUP[INDEXVAL]=CAPVAL
          CAPORIGNL[INDEXVAL] = CAPVAL  
        ENDLOOP
      ENDLOOP
    ENDLOOP
ENDIF
IF (SPDFUNC=' ')
    LOOP ATYPE=LATVAL,HATVAL
      LOOP FTYPE=LFTVAL,HFTVAL
        LOOP LANES=LLNVAL,HLNVAL
          INDEXVAL=ATYPE*10000+FTYPE*100+LANES
          SPDLOOKUP[INDEXVAL]=SPDVAL
          SPDORIGNL[INDEXVAL] = SPDVAL  
        ENDLOOP
      ENDLOOP
    ENDLOOP
ENDIF
IF (CAPFUNC='MUL')
    LOOP ATYPE=LATVAL,HATVAL
      LOOP FTYPE=LFTVAL,HFTVAL
        LOOP LANES=LLNVAL,HLNVAL
        INDEXVAL=ATYPE*10000+FTYPE*100+LANES
        CAPLOOKUP[INDEXVAL]=CAPLOOKUP[INDEXVAL]*CAPVAL
        ENDLOOP
      ENDLOOP
    ENDLOOP
ENDIF
IF (SPDFUNC='MUL'|SPDFUNC='ADD'|SPDFUNC='MIN')
    LOOP ATYPE=LATVAL,HATVAL
      LOOP FTYPE=LFTVAL,HFTVAL
        LOOP LANES=LLNVAL,HLNVAL
        INDEXVAL=ATYPE*10000+FTYPE*100+LANES
        IF (SPDFUNC='MUL') SPDLOOKUP[INDEXVAL]=SPDLOOKUP[INDEXVAL]*SPDVAL
        IF (SPDFUNC='ADD') SPDLOOKUP[INDEXVAL]=SPDLOOKUP[INDEXVAL]+SPDVAL
        IF (SPDFUNC='MIN') SPDLOOKUP[INDEXVAL]=SPDLOOKUP[INDEXVAL]-SPDVAL
        ENDLOOP
      ENDLOOP
    ENDLOOP
ENDIF

IF (I=0)
  ;Print output
  LOOP IVAL=1,999999

     IF (CAPLOOKUP[IVAL]>0|SPDLOOKUP[IVAL]>0)
         INDEX=IVAL
         CAPACITY=CAPLOOKUP[IVAL]
         SPEED=SPDLOOKUP[IVAL]
         WRITE RECO=1
         CAPACITY=CAPORIGNL[IVAL]
         SPEED=SPDORIGNL[IVAL]
         WRITE RECO=2
     ENDIF
     IF (CAPLOOKUP[IVAL]<0)
        CAPERRCNT=CAPERRCNT+1
       
     ENDIF
     IF (SPDLOOKUP[IVAL]<0)
        SPDERRCNT=SPDERRCNT+1
     ENDIF
  ENDLOOP
  ;SPDCAP.ERR is used to generate the network/SPDCAP lookup errors


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
