; Script for program NETWORK in file "D:\JOB\D3\Model\C\APPLICATIONS\HNNET00A.S"
; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=NETWORK PRNFILE="{Scenario_Dir}\OUTPUT\HNNET00A.PRN" MSG='Process Highway Network'
FILEO LINKO = "{Scenario_Dir}\OUTPUT\LINK4CNT.DBF",
FORMAT=DBF,INCLUDE=A,B
FILEO NETO = "{Scenario_Dir}\OUTPUT\TEMP.NET",
EXCLUDE=LINKCNT
FILEI LOOKUPI[2] = "{Scenario_Dir}\Input\SPDCAP_{Year}{ALT}.CSV"
FILEI LOOKUPI[1] = "{Scenario_Dir}\Input\Vfactors_{Year}{ALT}.CSV"
FILEI LINKI[2] = "{SCENARIO_DIR}\\Input\Tolllink_{Year}{ALT}.DAT",
var=TOLL,beg=1,len=2,  
 var=TOLLTYPE,beg=3,len=2,
 var=A,beg=5,len=6, 
 var=B,beg=11,len=6, 
 var=PLAZADESC,beg=17,len=25,typ=a,
 var=PLZALNSMIN,beg=55,len=2,
 var=PLZALNSMAX,beg=61,len=2,
 var=CARTOLL,beg=67,len=5,
 var=SVCMINUTES,beg=75,len=2,
 var=SVCSECONDS,beg=79,len=2,
 var=DECELCODE,beg=85,len=1,
 var=ACCELCODE,beg=91,len=1,
 var=EXACTCHGLNS,beg=97,len=2,
 var=AVILANES,beg=103,len=2,
 var=PCTTRUCKS,beg=109,len=4 

FILEI LINKI[1] = "{Scenario_Dir}\Input\HWYNET_{Year}{ALT}.NET"

;PROCESS  PHASE=INPUT
;Use this phase to modify data as it is read, such as recoding node numbers.


;ENDPROCESS


;PROCESS  PHASE=NODEMERGE  
; Use this phase to make computations and selections of any data on the NODEI files.


;ENDPROCESS

PAR  LIST_ERRS=0 MAX_IP_ERRS=10000
ARRAY _ATCNT=99,_FTCNT=99
LOOKUP, NAME=VFACTORS, 
    LOOKUP[1]=1, RESULT=2,
    LOOKUP[2]=1, RESULT=3,
    LOOKUP[3]=1, RESULT=4,
    LOOKUP[4]=1, RESULT=5,
    INTERPOLATE=N, LOOKUPI=1
LOOKUP, NAME=SPDCAP, 
    LOOKUP[1]=1, RESULT=2,
    LOOKUP[2]=1, RESULT=3,
    INTERPOLATE=N, LOOKUPI=2

PROCESS PHASE=LINKMERGE
; if no distance in network, calculate one
  distance=ROUND(100*sqrt((a.x-b.x)^2+(a.y-b.y)^2)/{units})/100
; PUT VFACTORS ON NETWORK
  CARTOLL=0
  UROADFACTOR=VFACTORS(1,Facility_Type)
  CONFAC=VFACTORS(2,Facility_Type)
  BPRCOEFFICIENT=VFACTORS(3,Facility_Type)
  BPREXPONENT=VFACTORS(4,Facility_Type)

; PUT SPEEDS AND CAPACITIES ON NETWORK
  _INDEXVAL=10000*Area_Type+100*Facility_Type+Num_LANES

  CAPACITY=SPDCAP(1,_INDEXVAL)
  SPEED=SPDCAP(2,_INDEXVAL)

  IF (SPEED!=0)
    TIME=ROUND(100*60*distance/speed)/100
  ENDIF
  if (time<0.01) time=0.01
ENDPROCESS



;PROCESS  PHASE=SUMMARY   
; Use this phase for combining and reporting of working variables.


;ENDPROCESS

ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\HNMAT00A.S"
; 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\HNMAT00A.PRN" MSG='Count Times Nodes Used by Links'
FILEO RECO[1] = "{Scenario_Dir}\OUTPUT\NODECNT.DBF",
FIELDS=N,TIMESUSED
FILEI RECI = "{Scenario_Dir}\OUTPUT\LINK4CNT.DBF"

ARRAY NODECNT=999999

NODECNT[RI.A]=NODECNT[RI.A]+1
NODECNT[RI.B]=NODECNT[RI.B]+1
IF (RI.A>HINODE) HINODE=RI.A
IF (RI.B>HINODE) HINODE=RI.B

IF (I=0)

  LOOP N=1,HINODE
    IF (NODECNT[N]>0)
     TIMESUSED=NODECNT[N] 
     WRITE RECO=1
    ENDIF
  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


; Script for program NETWORK in file "D:\JOB\D3\Model\C\APPLICATIONS\HNNET00B.S"
; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=NETWORK PRNFILE="{Scenario_Dir}\OUTPUT\HNNET00B.PRN"
FILEO PRINTO[1] = "{Scenario_Dir}\OUTPUT\TEMPNODE.DAT"
FILEI LINKI[1] = "{Scenario_Dir}\OUTPUT\TEMP.NET"

if (li.1.TOLLTYPE=1); Toll Road
  a_accel=0
  a_decel=1
  b_accel=1
  b_decel=0
  PRINT form=0, LIST=A,' ',a_accel,' ',a_decel,' ',li.1.toll,printo=1
  PRINT form=0, LIST='NODE= ',A,' ACCEL_NODE= ',a_accel,' DECEL_NODE= ',a_decel,' PLAZA ID= ',li.1.toll
  PRINT form=0, LIST=B,' ',b_accel,' ',b_decel,' ',li.1.toll,printo=1
  PRINT form=0, LIST='NODE= ',B,' ACCEL_NODE= ',b_accel,' DECEL_NODE= ',b_decel,' PLAZA ID= ',li.1.toll
endif

if (li.1.TOLLTYPE=2); ORT
  a_accel=0
  a_decel=1 ; added for ORT
  b_accel=1 ; added for ORT
  b_decel=0
  PRINT form=0, LIST=A,' ',a_accel,' ',a_decel,' ',li.1.toll,printo=1
  PRINT form=0, LIST='NODE= ',A,' ACCEL_NODE= ',a_accel,' DECEL_NODE= ',a_decel,' PLAZA ID= ',li.1.toll
  PRINT form=0, LIST=B,' ',b_accel,' ',b_decel,' ',li.1.toll,printo=1
  PRINT form=0, LIST='NODE= ',B,' ACCEL_NODE= ',b_accel,' DECEL_NODE= ',b_decel,' PLAZA ID= ',li.1.toll
endif


ENDRUN


; Script for program NETWORK in file "D:\JOB\D3\Model\C\APPLICATIONS\HNNET00D.S"
; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=NETWORK PRNFILE="{Scenario_Dir}\OUTPUT\HNNET00D.PRN"
FILEO PRINTO[2] = "{Scenario_Dir}\OUTPUT\LINKS.CSV"
FILEO PRINTO[1] = "{Scenario_Dir}\OUTPUT\NODES.CSV"
FILEO NETO = "{Scenario_Dir}\OUTPUT\UNLOADED_{ALT}{Year}.NET"
FILEI NODEI[3] = "{Scenario_Dir}\OUTPUT\NODECNT.DBF"
FILEI NODEI[2] = "{Scenario_Dir}\OUTPUT\TEMPNODE.DAT",
 VAR=N,_TOLL_ACC,_TOLL_DEC,_PLAZAID
FILEI LINKI[1] = "{Scenario_Dir}\OUTPUT\TEMP.NET"



PROCESS  PHASE=INPUT, FILEI=LI.1
  TOLL_ACC=0
  TOLL_DEC=0
ENDPROCESS

PROCESS PHASE=NODEMERGE
   PRINT CSV=T, LIST=N(8.0),X,Y printo=1
   IF (TIMESUSED=0) DELETE
ENDPROCESS

PROCESS PHASE=LINKMERGE   
  IF(B._TOLL_DEC=1 & B._PLAZAID>0)
     TOLL_DEC=1
     TOLL=B._PLAZAID;_PLAZAID
  ENDIF
  IF(A._TOLL_ACC=1 & A._PLAZAID>0)
     TOLL_ACC=1
     TOLL=A._PLAZAID;_PLAZAID
  ENDIF

  if (time>0)
    _AUTOSPEED=60*(DISTANCE/{units})/TIME
  else
    _AUTOSPEED=15
  ENDIF
  if (Distance=0) distance=0.01 

_focus=1
_ft=int(facility_type/10)
_at=int(area_type/10)
print list=_focus(2.0),a(5.0),b(5.0),distance*100/{units}(4.0),toll(2.0),_ft(1.0),_at(1.0), printo=2


ENDPROCESS
;ENDPROCESS

ENDRUN


; Script for program HIGHWAY in file "D:\JOB\D3\Model\C\APPLICATIONS\HNHWY00A.S"
; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=HIGHWAY PRNFILE="{Scenario_Dir}\OUTPUT\HNHWY00A.PRN"
FILEO MATO[1] = "{SCENARIO_DIR}\OUTPUT\FREESKIM_{ALT}{Year}.MAT",
 MO=1-9, NAME=STOLL,SDISTANCE,STIME,STERMINALTIME,STIME2,HTOLL,HDISTANCE,HTIME,HTIME2
FILEI TURNPENI = "{SCENARIO_DIR}\Input\TURN_{Year}{ALT}.PEN"
FILEI LOOKUPI[1] = "{Catalog_Dir}\Parameters\TTPREP.CSV"
FILEI NETI = "{Scenario_Dir}\OUTPUT\UNLOADED_{ALT}{Year}.NET"

PAR ZONES={ZONESA}
PAR ZONEMSG=100

; ARRAY TERMINAL TIMES INTO MEMORY
;ARRAY ATYPE={ZONESA}
LOOKUP, NAME=TERMTIME, LOOKUP[1]=1, RESULT=2, INTERPOLATE=N, LOOKUPI=1

PROCESS PHASE=LINKREAD
   LW.TIME=li.time+li.svcminutes+li.svcseconds/60+li.cartoll*{ctoll}*60
ENDPROCESS

PROCESS PHASE=ILOOP
   PATHLOAD PATH=LW.TIME, PENI=1, 
      MW[1]=PATHTRACE(LI.CARTOLL), NOACCESS=0,
      MW[2]=PATHTRACE(LI.DISTANCE), NOACCESS=0,
      MW[3]=PATHTRACE(LI.TIME), NOACCESS=0
     ;MW[4]=PATHTRACE(LW.TIME), NOACCESS=0
; BUILD TERMINAL TIME MATRIX
      MW[4]=TERMTIME(1,I)+TERMTIME(1,J)
AG={IZzONE};*2
loop _IZ=1,3;4
  mw[_IZ][i]=lowest(_IZ,{IZzone})/AG
endloop
; set intrazonal array to zero for cost
mw[1][i]=0
MW[5]=MW[3]
MW[6]=MW[1]
MW[7]=MW[2]
MW[8]=MW[3]
MW[9]=MW[5]

ENDPROCESS


;PROCESS PHASE=ADJUST
; This phase is automatically run after the ILOOP phase.  The volume-delay functions (TC) are
; automatically recomputed in this phase to update the link congested travel times based 
; on the volumes from the ILOOP phase.  User defined link computations (LW.varname) that 
; need to be updated based on the new congested travel times should be coded in this phase.


;ENDPROCESS

ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\HNMAT00B.S"
; 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\HNMAT00B.PRN" MSG='Build Transit Pahts/Skims'
FILEO MATO[1] = "{Scenario_Dir}\OUTPUT\FHSKIMTT.{ALT}{Year}",
MO=1-4,NAME=COST,DISTANCE,TIME,TIME2, FORMAT=TRANPLAN

FILEI MATI[1] = "{SCENARIO_DIR}\OUTPUT\FREESKIM_{ALT}{Year}.MAT"

; 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.
MW[1]=MI.1.1*100
MW[2]=MI.1.2*100
MW[3]=MI.1.3*100
MW[4]=MI.1.5*100
ENDRUN


