; PILOT Script



*echo Date: %date% >"{SCENARIO_DIR}\output\ModelRun_{alt}{year}.prn"
*echo Time: %time% >>"{SCENARIO_DIR}\output\ModelRun_{alt}{year}.prn"
*echo FSUMTS Model Run - {SCENARIO_FULLNAME}>>"{SCENARIO_DIR}\output\ModelRun_{alt}{year}.prn"
*echo Input Directory {SCENARIO_DIR}\input >>"{SCENARIO_DIR}\output\ModelRun_{alt}{year}.prn"
*echo ======================================================================================>>"{SCENARIO_DIR}\output\ModelRun_{alt}{year}.prn"

*del "{SCENARIO_DIR}\output\ModeDebug.TXT"
*del "{SCENARIO_DIR}\output\ModeSum.TXT"
*del "{SCENARIO_DIR}\output\CalibSum.TXT"
*del "{SCENARIO_DIR}\output\modeshares.csv"
*del "{SCENARIO_DIR}\output\NEW_MCCONSTS.CSV"
*del "{SCENARIO_DIR}\output\CBDSHARES.CSV"
; End of PILOT Script

; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00C.S"

; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=MATRIX MSG='store time and date'
FILEI RECI = "{SCENARIO_DIR}\output\ModelRun_{alt}{year}.prn"

zones={ZONESA}

s1=strpos('Date:',reci)
s2=strpos('Time:',reci)

; get the date and time
if (s1 > 0)
 rundate=substr(reci,s1,80)
 log prefix=date var=rundate
endif
if (s2 > 0) 
 runtime=substr(reci,s2,80)
 log prefix=time var=runtime
endif

ENDRUN


RUN PGM="C:\Program Files\Citilabs\Cube\TRCOPY.bat" PARAMETERS='"{CATALOG_DIR}\parameters\MC_CONSTANTS.DBF" "{SCENARIO_DIR}\output\MC_CONSTANTS.DBF" '
ENDRUN

RUN PGM="C:\Program Files\Citilabs\Cube\TRCOPY.bat" PARAMETERS='"{CATALOG_DIR}\parameters\TRN_COEFFICIENTS.DBF" "{SCENARIO_DIR}\output\TRN_COEFFICIENTS.DBF" '
ENDRUN

; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCPREP.S"
; 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\MCMAT00B.PRN" MSG='Auto ownership %age by TAZ'
;ZONE=1-5,mpo=6-10,county=11-15
FILEI LOOKUPI[2] = "{SCENARIO_DIR}\output\DUWEIGHTS.DBF"
FILEI LOOKUPI[1] = "{SCENARIO_DIR}\output\GRATES_MC.DBF"
FILEO RECO[1] = "{SCENARIO_DIR}\output\A1DECK.dbf",
fields=ZONE(4.0),ltp(4.0),stp(4.0),wksh0(4.0),wksh1(4.0),wksh2(4.0),nwsh0(4.0),nwsh1(4.0),nwsh2(4.0)

FILEI ZDATI[1] = "{SCENARIO_DIR}\input\ZONEDATA_{year}{alt}.DBF"

PARAMETERS ZONEMSG=100 ZONES={ZONESA}

ARRAY DRANGE=16, SFDUM1=5, SFDUM2=5, SFDUM3=5, MFDUM1=5, MFDUM2=5, MFDUM3=5, HMDUM=5, SFTOT=3, MFTOT=3, TERM=5

_seli={SelOrigin}

LOOKUP LOOKUPI=1,
       NAME=RATES1,
         LOOKUP[1]=COUNTER, RESULT=PPDU,
         LOOKUP[2]=COUNTER, RESULT=APDU,
         LOOKUP[3]=COUNTER, RESULT=DUTYPE,
         LOOKUP[4]=COUNTER, RESULT=HBWPR,
         LOOKUP[5]=COUNTER, RESULT=HBSHPR,
         LOOKUP[6]=COUNTER, RESULT=HBSRPR,
         LOOKUP[7]=COUNTER, RESULT=HBOPR,
       FAIL[1]=0,FAIL[2]=0,FAIL[3]=0
; example of use: v=RATES(7,25)
; look for 25 in the COUNTER field and returns the HBOPR value

LOOKUP LOOKUPI=2,
       NAME=DSHARE,
         LOOKUP[1]=SIZERANGE, RESULT=PCT1PER,
         LOOKUP[2]=SIZERANGE, RESULT=PCT2PER,
         LOOKUP[3]=SIZERANGE, RESULT=PCT3PER,
         LOOKUP[4]=SIZERANGE, RESULT=PCT4PER,
         LOOKUP[5]=SIZERANGE, RESULT=PCT5PER,
       FAIL[1]=0,FAIL[2]=0,FAIL[3]=0
; example of use: v=DUWEIGHT(5,25)
; look for 25 in the SIZERANGE field and returns the HH5 value

;**************************************************************
;BEGIN POTENTIAL SCRIPT ADJUSTMENT
;**************************************************************


;LOOKUP LOOKUPI=3,
;       NAME=TYPE,
;         LOOKUP[4]=1, RESULT=5,         ; Two-digit area type
;         LOOKUP[5]=1, RESULT=6,         ; County
;         LOOKUP[6]=1, RESULT=7,         ; MPO
;       FAIL[1]=0,FAIL[2]=0,FAIL[3]=0


;LOOKUP LOOKUPI=4,
;       NAME=RATES2,
;         LOOKUP[1]=COUNTER, RESULT=PPDU,
;         LOOKUP[2]=COUNTER, RESULT=APDU,
;         LOOKUP[3]=COUNTER, RESULT=DUTYPE,
;         LOOKUP[4]=COUNTER, RESULT=HBWPR,
;         LOOKUP[5]=COUNTER, RESULT=HBSHPR,
;         LOOKUP[6]=COUNTER, RESULT=HBSRPR,
;         LOOKUP[7]=COUNTER, RESULT=HBOPR,
;       FAIL[1]=0,FAIL[2]=0,FAIL[3]=0

;LOOKUP LOOKUPI=5,
;       NAME=RATES3,
;         LOOKUP[1]=COUNTER, RESULT=PPDU,
;         LOOKUP[2]=COUNTER, RESULT=APDU,
;         LOOKUP[3]=COUNTER, RESULT=DUTYPE,
;         LOOKUP[4]=COUNTER, RESULT=HBWPR,
;         LOOKUP[5]=COUNTER, RESULT=HBSHPR,
;         LOOKUP[6]=COUNTER, RESULT=HBSRPR,
;         LOOKUP[7]=COUNTER, RESULT=HBOPR,
;       FAIL[1]=0,FAIL[2]=0,FAIL[3]=0

; Initialize DRANGE (DUWEIGHT categories) & TERM (terminal times by areatype)
DRANGE[1]=1.125
DRANGE[2]=1.375
DRANGE[3]=1.625
DRANGE[4]=1.875
DRANGE[5]=2.125
DRANGE[6]=2.375
DRANGE[7]=2.625
DRANGE[8]=2.875
DRANGE[9]=3.125
DRANGE[10]=3.375
DRANGE[11]=3.625
DRANGE[12]=3.875
DRANGE[13]=4.125
DRANGE[14]=4.375
DRANGE[15]=4.625
DRANGE[16]=5.995

; Initialize arrays & temporary variables here
LOOP x=1,5
  SFDUM1[x]=0, SFDUM2[x]=0, SFDUM3[x]=0
  MFDUM1[x]=0, MFDUM2[x]=0, MFDUM3[x]=0
ENDLOOP

LOOP x=1,3
  SFTOT[x]=0, MFTOT[x]=0
ENDLOOP

_xdum=0, _SFPPDU=0, _MFPPDU=0, _HMPPDU=0
_tmp=0, _HMTRIP=0, _HMTOT=0
_SFTRIP1=0, _SFTRIP2=0, _SFTRIP3=0
_MFTRIP1=0, _MFTRIP2=0, _MFTRIP3=0
_WKA0=0, _WKA1=0, _WKA2=0, _WKTOT=0
_NWA0=0, _NWA1=0, _NWA2=0, _NWTOT=0
_WKSHA0=0, _WKSHA1=0, _WKSHA2=0, _WKSHT=0
_NWSHA0=0, _NWSHA1=0, _NWSHA2=0, _NWSHT=0
_at=0, _area=0
_SFIDX = 1, _MFIDX=1, _HMIDX=1

; Calculations
_xdum = ZI.1.SFDU * (1.0 - 0.01*ZI.1.SF_PCTVNP)
IF (_xdum > 0) _SFPPDU = 1.0 * ZI.1.SFPOP/_xdum
_xdum = ZI.1.MFDU * (1.0 - 0.01*ZI.1.MF_PCTVNP)
IF (_xdum > 0) _MFPPDU = 1.0 * ZI.1.MFPOP/_xdum
_xdum = ZI.1.HMDU * (0.01*ZI.1.HMOCC)
IF (_xdum > 0) _HMPPDU = 1.0 * ZI.1.HMPOP/_xdum

; Debug
IF (I==_seli) LIST="2077 SFDU  ",ZI.1.SFDU(7.0)," SF_PCTVAC1 ",ZI.1.SF_SEAS(4.0)," SF_PCTVAC2 ",ZI.1.SF_PCTVAC(4.0)," SFPOP ",ZI.1.SFPOP
IF (I==_seli) LIST="2077 MFDU  ",ZI.1.MFDU(7.0)," MF_PCTVAC1 ",ZI.1.MF_SEAS(4.0)," MF_PCTVAC2 ",ZI.1.MF_PCTVAC(4.0)," MFPOP ",ZI.1.MFPOP
IF (I==_seli) LIST="2077 HMDU  ",ZI.1.HMDU(7.0)," HMOCC  ",ZI.1.HMOCC(4.0), " HMPOP  ",ZI.1.HMPOP(4.0)
IF (I==_seli) LIST="2077 SFPPDU",_SFPPDU(7.2)," MFPPDU ",_MFPPDU(7.2)," HMPPDU ",_HMPPDU(7.2)

; Find correct DUWEIGHT SIZERANGE
LOOP x=1,16
  IF (_SFPPDU > DRANGE[x]) _SFIDX=x+1
  IF (_MFPPDU > DRANGE[x]) _MFIDX=x+1
  IF (_HMPPDU > DRANGE[x]) _HMIDX=x+1
ENDLOOP

; Debug
IF (I==_seli) LIST="2077 SFIDX",_SFIDX(3.0)," MFIDX ",_MFIDX(3.0)," HMIDX ",_HMIDX(3.0)

; Main computations
LOOP x=1,5
  HMDUM[x]=1.0*ZI.1.HMDU*0.01*ZI.1.HMOCC*DSHARE(x,_HMIDX)
  SFDUM1[x]=0.01*ZI.1.SFDU*(1.0-0.01*SF_PCTVAC)*ZI.1.SF_0AUTO*DSHARE(x,_SFIDX)
  SFDUM2[x]=0.01*ZI.1.SFDU*(1.0-0.01*SF_PCTVAC)*ZI.1.SF_1AUTO*DSHARE(x,_SFIDX)
  SFDUM3[x]=0.01*ZI.1.SFDU*(1.0-0.01*SF_PCTVAC)*(ZI.1.SF_2AUTO)*DSHARE(x,_SFIDX)
  MFDUM1[x]=0.01*ZI.1.MFDU*(1.0-0.01*MF_PCTVAC)*ZI.1.MF_0AUTO*DSHARE(x,_MFIDX)
  MFDUM2[x]=0.01*ZI.1.MFDU*(1.0-0.01*MF_PCTVAC)*ZI.1.MF_1AUTO*DSHARE(x,_MFIDX)
  MFDUM3[x]=0.01*ZI.1.MFDU*(1.0-0.01*MF_PCTVAC)*(ZI.1.MF_2AUTO)*DSHARE(x,_MFIDX)
ENDLOOP

; Identify Rate District (see page 18 of TM2-CFRPM4.0 Calibration & Validation Report)

;_mpo = TYPE(5,I)
;_gendist = 1
;IF (_mpo==1)          ; Region 1
;  _gendist = 1
;ELSEIF (_mpo==2)      ; Region 2
;  _gendist = 2
;ELSEIF (_mpo==3)   ; Region 3
;  _gendist = 3
;ENDIF

; Debug
IF (I==_seli) LIST="2077 MPO",_MPO(3.0)," GENDIST ",_GENDIST(3.0)

LOOP L=1,4

 _HMTOT=0
 SFTOT[1]=0, SFTOT[2]=0, SFTOT[3]=0
 MFTOT[1]=0, MFTOT[2]=0, MFTOT[3]=0

LOOP x=1,5
 _tmp1 = 9 * (x-1) + 6
 _tmp2 = 9 * (x-1) + 1
 _tmp3 = 9 * (x-1) + 4
 _tmp4 = 9 * (x-1) + 7
 _tmp5 = 9 * (x-1) + 2
 _tmp6 = 9 * (x-1) + 5
 _tmp7 = 9 * (x-1) + 8

 ;  IF (_gendist==1)
 _HMTRIP  = RATES1(L+3,_tmp1)*HMDUM[x]
 _SFTRIP1 = RATES1(L+3,_tmp2)*SFDUM1[x]
 _SFTRIP2 = RATES1(L+3,_tmp3)*SFDUM2[x]
 _SFTRIP3 = RATES1(L+3,_tmp4)*SFDUM3[x]
 _MFTRIP1 = RATES1(L+3,_tmp5)*MFDUM1[x]
 _MFTRIP2 = RATES1(L+3,_tmp6)*MFDUM2[x]
 _MFTRIP3 = RATES1(L+3,_tmp7)*MFDUM3[x]

;    ELSEIF (_gendist==2)
;      _HMTRIP  = RATES2(L+3,_tmp1)*HMDUM[x]
;      _SFTRIP1 = RATES2(L+3,_tmp2)*SFDUM1[x]
;      _SFTRIP2 = RATES2(L+3,_tmp3)*SFDUM2[x]
;      _SFTRIP3 = RATES2(L+3,_tmp4)*SFDUM3[x]
;      _MFTRIP1 = RATES2(L+3,_tmp5)*MFDUM1[x]
;      _MFTRIP2 = RATES2(L+3,_tmp6)*MFDUM2[x]
;      _MFTRIP3 = RATES2(L+3,_tmp7)*MFDUM3[x]
;    ELSEIF (_gendist==3)
;      _HMTRIP  = RATES3(L+3,_tmp1)*HMDUM[x]
;      _SFTRIP1 = RATES3(L+3,_tmp2)*SFDUM1[x]
;      _SFTRIP2 = RATES3(L+3,_tmp3)*SFDUM2[x]
;      _SFTRIP3 = RATES3(L+3,_tmp4)*SFDUM3[x]
;      _MFTRIP1 = RATES3(L+3,_tmp5)*MFDUM1[x]
;      _MFTRIP2 = RATES3(L+3,_tmp6)*MFDUM2[x]
;      _MFTRIP3 = RATES3(L+3,_tmp7)*MFDUM3[x]
; ENDIF

    _HMTOT = _HMTOT + _HMTRIP
    SFTOT[1] = SFTOT[1] + _SFTRIP1
    SFTOT[2] = SFTOT[2] + _SFTRIP2
    SFTOT[3] = SFTOT[3] + _SFTRIP3
    MFTOT[1] = MFTOT[1] + _MFTRIP1
    MFTOT[2] = MFTOT[2] + _MFTRIP2
    MFTOT[3] = MFTOT[3] + _MFTRIP3
  ENDLOOP
  IF (L==1)
    _WKA0 = SFTOT[1] + MFTOT[1]
    _WKA1 = SFTOT[2] + MFTOT[2] + _HMTOT
    _WKA2 = SFTOT[3] + MFTOT[3]
    _WKTOT = _WKA0 + _WKA1 + _WKA2
  ELSE
    _NWA0 = _NWA0 + SFTOT[1] + MFTOT[1]
    _NWA1 = _NWA1 + SFTOT[2] + MFTOT[2] + _HMTOT
    _NWA2 = _NWA2 + SFTOT[3] + MFTOT[3]
    _NWTOT = _NWA0 + _NWA1 + _NWA2
  ENDIF

  IF (I==_seli) LIST="2077 SFTOT L=",L(1.0)," ",SFTOT[1](7.1),SFTOT[2](7.1),SFTOT[3](7.1)
  IF (I==_seli) LIST="2077 MFTOT L=",L(1.0)," ",MFTOT[1](7.1),MFTOT[2](7.1),MFTOT[3](7.1)
  IF (I==_seli) LIST="2077 HMTOT L=",L(1.0)," ",_HMTOT(7.1)

ENDLOOP

; Debug
IF (I==_seli) LIST="2077 WKA0 ",_WKA0(7.1)," WKA1 ",_WKA1(7.1)," WKA2 ",_WKA2(7.2)," WKTOT ",_WKTOT(7.1)
IF (I==_seli) LIST="2077 NWA0 ",_NWA0(7.1)," NWA1 ",_NWA1(7.1)," NWA2 ",_NWA2(7.2)," NWTOT ",_NWTOT(7.1)

IF (_WKTOT > 0)
  _WKSHA0=ROUND(100.0*_WKA0/_WKTOT)
  _WKSHA1=ROUND(100.0*_WKA1/_WKTOT)
  _WKSHA2=ROUND(100.0*_WKA2/_WKTOT)
  _WKSHT=ROUND(_WKSHA0 + _WKSHA1 + _WKSHA2)
  IF (_WKSHT <> 100)
    IF (_WKSHA0 > _WKSHA1)
      IF (_WKSHA0 > _WKSHA2)
        _WKSHA0 = _WKSHA0 + 100 - _WKSHT
      ELSE
        _WKSHA2 = _WKSHA2 + 100 - _WKSHT
      ENDIF
    ELSE
      IF (_WKSHA1 > _WKSHA2)
        _WKSHA1 = _WKSHA1 + 100 - _WKSHT
      ELSE
        _WKSHA2 = _WKSHA2 + 100 - _WKSHT
      ENDIF
    ENDIF
  ENDIF
ENDIF

IF (_NWTOT > 0)
  _NWSHA0=ROUND(100.0*_NWA0/_NWTOT)
  _NWSHA1=ROUND(100.0*_NWA1/_NWTOT)
  _NWSHA2=ROUND(100.0*_NWA2/_NWTOT)
  _NWSHT=ROUND(_NWSHA0 + _NWSHA1 + _NWSHA2)
  IF (_NWSHT <> 100)
    IF (_NWSHA0 > _NWSHA1)
      IF (_NWSHA0 > _NWSHA2)
        _NWSHA0 = _NWSHA0 + 100 - _NWSHT
      ELSE
        _NWSHA2 = _NWSHA2 + 100 - _NWSHT
      ENDIF
    ELSE
      IF (_NWSHA1 > _NWSHA2)
        _NWSHA1 = _NWSHA1 + 100 - _NWSHT
      ELSE
        _NWSHA2 = _NWSHA2 + 100 - _NWSHT
      ENDIF
    ENDIF
  ENDIF
ENDIF

ZONE=i
ltp=zi.1.LONGPARK
stp=zi.1.SHORTPARK
wksh0=_WKSHA0
wksh1=_WKSHA1
wksh2=_WKSHA2
nwsh0=_NWSHA0
nwsh1=_NWSHA1
nwsh2=_NWSHA2
write reco=1

;**************************************************************
;END POTENTIAL SCRIPT ADJUSTMENT
;**************************************************************

ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00B.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\MCMAT00A3.PRN" MSG='Preparation for mode choice model'
FILEI ZDATI[2] = "{SCENARIO_DIR}\output\ZONESAT.DAT",
Z=6-10,A1=16-20
FILEI ZDATI[1] = "{SCENARIO_DIR}\input\ZONEDATA_{year}{alt}.DBF"
FILEO RECO[1] = "{SCENARIO_DIR}\output\TAZDATA_{alt}{year}.DBF",
   FIELDS=ZONE,DESCR,SFDU,SF_PCTVNP,SF_PCTVAC,SFPOP,SF_0AUTO,SF_1AUTO,SF_2AUTO,SF3CAR,MFDU,MF_PCTVNP,
           MF_PCTVAC,MFPOP,MF_0AUTO,MF_1AUTO,MF_2AUTO,MF3CAR,HMDU,HMOCC,HMPOP,EMPMFG,EMPIND,EMPCOM,EMPSVC,EMPTOT,SCHENR,SHORTPARK,LONGPARK,
           PCWPRDPK,PCWATTPK,PCWPRDOP,PCWATTOP,AREA_TYPE,CBD_DUMMY,EXU_DUMMY,TERMTIME,PERCAUTO0,PERCAUTO1,PERCAUTO2

PAR ZONES={ZONESI,Spin-INTEGER,"Maximum internal zone number","3700",5,10000} ZONEMSG=100

;**************************************************************
;BEGIN POTENTIAL SCRIPT ADJUSTMENT
;**************************************************************

RO.ZONE=ZI.1.ZONE

RO.SFDU=ZI.1.SFDU
RO.SF_PCTVNP=ZI.1.SF_PCTVNP
RO.SF_PCTVAC=ZI.1.SF_PCTVAC
RO.SFPOP=ZI.1.SFPOP
RO.SF_0AUTO=ZI.1.SF_0AUTO
RO.SF_1AUTO=ZI.1.SF_1AUTO
RO.SF_2AUTO=ZI.1.SF_2AUTO

RO.MFDU=ZI.1.MFDU
RO.MF_PCTVNP=ZI.1.MF_PCTVNP
RO.MF_PCTVAC=ZI.1.MF_PCTVAC
RO.MFPOP=ZI.1.MFPOP
RO.MF_0AUTO=ZI.1.MF_0AUTO
RO.MF_1AUTO=ZI.1.MF_1AUTO
RO.MF_2AUTO=ZI.1.MF_2AUTO

RO.HMDU=ZI.1.HMDU
RO.HMOCC=ZI.1.HMOCC
RO.HMPOP=ZI.1.HMPOP

RO.SHORTPARK=ZI.1.SHORTPARK
RO.LONGPARK=ZI.1.LONGPARK

RO.AREA_TYPE=ZI.2.A1
_atype=ROUND(ZI.2.A1/10)
if (_atype=1) RO.TERMTIME={TERM10}
if (_atype=2) RO.TERMTIME={TERM20}
if (_atype=3) RO.TERMTIME={TERM30}
if (_atype=4) RO.TERMTIME={TERM40}
if (_atype=5) RO.TERMTIME={TERM50}

CBD_DUMMY=0
EXU_DUMMY=0
; define CBD zones for mode chocie
if (ZI.2.A1>10 & ZI.2.A1<20) CBD_DUMMY=1
; define EXURBAN zones for mode choice
if (ZI.2.A1>40 & ZI.2.A1<50) EXU_DUMMY=1

WRITE RECO=1

;**************************************************************
;END POTENTIAL SCRIPT ADJUSTMENT
;**************************************************************

ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00P.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\MCMAT00A2.PRN" MSG='Calculate HOV time savings (to be used in mode choice model)'
FILEO MATO[3] = "{SCENARIO_DIR}\OUTPUT\RHSKIM.MAT",
mo=41-49,name=STOLL,SDISTANCE,STIME,STIME2,HTOLL,HDISTANCE,HTIME,HTIME2,TERMTIME
FILEO MATO[2] = "{SCENARIO_DIR}\OUTPUT\FFSKIM.MAT",
mo=31-39,name=STOLL,SDISTANCE,STIME,STIME2,HTOLL,HDISTANCE,HTIME,HTIME2,TERMTIME
FILEI MATI[3] = "{SCENARIO_DIR}\Output\FREESKIM_{ALT}{YEAR}.MAT"
FILEI MATI[1] = "{SCENARIO_DIR}\Output\CONGSKIM_{ALT}{YEAR}.MAT"

FILEO MATO[1] = "{SCENARIO_DIR}\output\HOVSavings_{alt}{year}.MAT",
 MO=21-22,DEC=2*2,NAME=HOVsaveOP,HOVsavePK

PAR ZONEMSG=100

;DistributeINTRASTEP ProcessID="FSUTMSDist", ProcessList=1-3
FILLMW MW[1] =MI.1.9   ; note that time2 is used here (time2 contains both time and time-equivalent of toll)
FILLMW MW[2] =MI.1.5
FILLMW MW[11]=MI.3.9
FILLMW MW[12]=MI.3.5
FILLMW MW[31]=MI.3.1,2,3,5,6,7,8,9,4
FILLMW MW[41]=MI.1.1,2,3,5,6,7,8,9,4


MW[22]=MW[1] - MW[2]   ; hov time savings during off-peak hours
MW[21]=MW[11] - MW[12] ; hov time savings during peak hours

JLOOP
 IF (MW[21] > 0) MW[21]=0.0   ; if less than HOVMIN zero out the savings
 IF (MW[22] > 0) MW[22]=0.0
 IF (MW[21] < {HOVMIN}) MW[21]=0.0   ; if less than HOVMIN zero out the savings
 IF (MW[22] < {HOVMIN}) MW[22]=0.0
ENDJLOOP
ENDRUN


; PILOT Script


*del "{SCENARIO_DIR}\output\MODESHARES.CSV"
*del "{SCENARIO_DIR}\output\CBDSHARES.CSV"
; End of PILOT Script

LOOP PURPOSE = 1, 3, 1

; PILOT Script


IF (PURPOSE=1) ; HBW purpose - requres peak skims
PERIOD='PK'
PURP='HBW'
PURPNO=2
SKIMFILE='RHSKIM'
STATPRKCOST='STPKCS1'
OCC3={oc3vhbw}
RUNDATE='@DATE.RUNDATE@'
RUNTIME=@TIME.RUNTIME@
PRK='LONGPARK'
STATION_TABLE='2,3,4'
isNHB=';'
noNHB=' '

copy, FILE = "{SCENARIO_DIR}\OUTPUT\HBWHEAD.VAR"
UBTRNCOEF=-0.0250
UBAUTOCOEF=-0.0250
UBPURPOSE='HBW'
UBTOD='ALL'
UBALTNAME='{SCENARIO_FULLNAME}'
endcopy

ELSEIF (PURPOSE=2) ; HBO purpose - requres off-peak skims
PERIOD='OP'
PURP='HBO'
PURPNO=3
SKIMFILE='FFSKIM'
STATPRKCOST='STPKCS2'
OCC3={oc3vhbnw}
PRK='SHORTPARK'
STATION_TABLE='6,7,8'
isNHB=';'
noNHB=' '

copy, FILE = "{SCENARIO_DIR}\OUTPUT\HBOHEAD.VAR"
UBTRNCOEF=-0.0125
UBAUTOCOEF=-0.0125
UBPURPOSE='HBO'
UBTOD='ALL'
UBALTNAME='{SCENARIO_FULLNAME}'
endcopy

ELSEIF (PURPOSE=3) ; NHB purpose - requres off-peak skims
PERIOD='OP'
PURP='NHB'
PURPNO=4
SKIMFILE='FFSKIM'
STATPRKCOST='STPKCS2'
OCC3={oc3vnhb}
PRK='SHORTPARK'
STATION_TABLE='6,7,8'
isNHB=' '
noNHB=';'

copy, FILE = "{SCENARIO_DIR}\OUTPUT\NHBHEAD.VAR"
UBTRNCOEF=-0.0250
UBAUTOCOEF=-0.0250
UBPURPOSE='NHB'
UBTOD='ALL'
UBALTNAME='{SCENARIO_FULLNAME}'
endcopy

ENDIF
; End of PILOT Script

; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00A.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\MCMAT00D.PRN" MSG='Utilities and XCHOICE'
FILEI MATI[13] = "{Scenario_Dir}\OUTPUT\PSNTRIPS_{ALT}{YEAR}.MAT"
FILEO MATO[16] = "{SCENARIO_DIR}\output\@PURP@Utility.mat",
mo=401-406,421-426,441-446
FILEI ZDATI[3] = "{SCENARIO_DIR}\output\A1DECK.dbf"
FILEO MATO[15] = "{SCENARIO_DIR}\output\@PURP@Trip_2CAR.MAT",
mo=541-546
FILEO MATO[14] = "{SCENARIO_DIR}\output\@PURP@Trip_1CAR.MAT",
mo=521-526
FILEO MATO[13] = "{SCENARIO_DIR}\output\@PURP@Trip_0CAR.MAT",
mo=501-506
FILEI MATI[12] = "{SCENARIO_DIR}\output\ST2ST_{alt}{year}.MAT"
FILEI ZDATI[2] = "{SCENARIO_DIR}\output\STATDATA_{alt}{year}.DBF"
FILEO MATO[12] = "{SCENARIO_DIR}\output\farexfer_tmp.mat",
MO=211,213,215,220,222,224,DEC=6*2,NAME=farewb,farepb,farekb,xferwb,xferpb,xferkb
FILEO MATO[11] = "{SCENARIO_DIR}\output\expautoutil_tmp.mat",
MO=56-58,36-38,DEC=6*D,NAME=EXPA_0CAR,EXPA_1CAR,EXPA_2CAR,SPLITCMP_0CAR,SPLITCMP_1CAR,SPLITCMP_2CAR
FILEO MATO[10] = "{SCENARIO_DIR}\output\@PURP@Trip.MAT",
MO=501-506,521-526,541-546,251-254,261-269,DEC=31*S
FILEO PRINTO[2] = "{SCENARIO_DIR}\output\ModeSum.TXT",
append=t
FILEO PRINTO[1] = "{SCENARIO_DIR}\output\ModeDebug.TXT",
append=t

FILEI ZDATI[1] = "{SCENARIO_DIR}\output\TAZDATA_{alt}{year}.DBF"
FILEI LOOKUPI[2] = "{SCENARIO_DIR}\output\TRN_COEFFICIENTS.DBF"
FILEI LOOKUPI[1] = "{SCENARIO_DIR}\output\MC_CONSTANTS.DBF"

FILEI MATI[11] = "{SCENARIO_DIR}\output\HOVSavings_{alt}{year}.MAT"
           ; Table 1 - HOV time savings for peak period
           ; Table 2 - HOV time savings for off-peak period
FILEI MATI[4] = "{SCENARIO_DIR}\output\TSKIM@PERIOD@3.{Alt}{year}"
FILEI MATI[2] = "{SCENARIO_DIR}\output\TSKIM@PERIOD@1.{Alt}{year}"
           ; Tables 1-6: 1-WalkTime, 2-AutoTime, 3-XferTime, 4-BusTime, 5-PRJBusTime, 6-CircTime
           ; Tables 7-10: 7-RailTime, 8-CommuterRailTime, 9-OtherModeTime, 10-ProjectModeTime
           ; Tables 11-15: 11-NumXfers, 12-InitialWaitTime, 13-XferWaitTime, 14-Fare, 15-Total time
FILEI MATI[1] = "{SCENARIO_DIR}\output\@SKIMFILE@.MAT"
           ; Tables 1-4 for SOV: 1-Toll, 2-Distance, 3-Time, 4-Weighted Time
           ; Tables 5-8 for HOV: 5-Toll, 6-Distance, 7-Time, 8-Weighted Time
           ; Table 9 is terminal time

zonemsg=100
ARRAY PARK={ZONESA}

DistributeINTRASTEP ProcessID="FSUTMSDist", ProcessList=1-2

;Constants
LOOKUP, NAME=CONSTANTS, LOOKUP[1]=1, RESULT=@PURPNO@, INTERPOLATE=N, , LIST=Y, LOOKUPI=1
;K=CONSTANT
if (i=FirstZone)
 K_DA_AUTO0=CONSTANTS(1,1),         K_DA_AUTO1=CONSTANTS(1,13),        K_DA_AUTO2=CONSTANTS(1,25)
 K_SR1PASS_AUTO0=CONSTANTS(1,2),    K_SR1PASS_AUTO1=CONSTANTS(1,14),   K_SR1PASS_AUTO2=CONSTANTS(1,26)
 K_SR2PASS_AUTO0=CONSTANTS(1,3),    K_SR2PASS_AUTO1=CONSTANTS(1,15),   K_SR2PASS_AUTO2=CONSTANTS(1,27)
 K_WALKBUS_AUTO0=CONSTANTS(1,4),    K_WALKBUS_AUTO1=CONSTANTS(1,16),   K_WALKBUS_AUTO2=CONSTANTS(1,28)
 K_WALKPRJ_AUTO0=CONSTANTS(1,5),    K_WALKPRJ_AUTO1=CONSTANTS(1,17),   K_WALKPRJ_AUTO2=CONSTANTS(1,29)
 K_PNRBUS_AUTO0=CONSTANTS(1,6),     K_PNRBUS_AUTO1=CONSTANTS(1,18),    K_PNRBUS_AUTO2=CONSTANTS(1,30)
 K_PNRPRJ_AUTO0=CONSTANTS(1,7),     K_PNRPRJ_AUTO1=CONSTANTS(1,19),    K_PNRPRJ_AUTO2=CONSTANTS(1,31)
 K_KNRBUS_AUTO0=CONSTANTS(1,8),     K_KNRBUS_AUTO1=CONSTANTS(1,20),    K_KNRBUS_AUTO2=CONSTANTS(1,32)
 K_KNRPRJ_AUTO0=CONSTANTS(1,9),     K_KNRPRJ_AUTO1=CONSTANTS(1,21),    K_KNRPRJ_AUTO2=CONSTANTS(1,33)

endif

;Coefficients
LOOKUP, NAME=COEFF, LOOKUP[1]=1, RESULT=@PURPNO@, INTERPOLATE=N, LIST=Y, LOOKUPI=2

if (i=FirstZone)
; Transit Coefficients
 COEFF_IVTT   = COEFF(1,1)
 COEFF_OVT    = COEFF(1,3)
 COEFF_COST   = COEFF(1,4)
 COEFF_AAT    = COEFF(1,5)
 COEFF_XFER   = COEFF(1,6)
 COEFF_DCBD   = COEFF(1,7)

 COEFF_DEXUP  = COEFF(1,8)
 COEFF_DEXUA  = COEFF(1,9)
 NC_TRNAC     = COEFF(1,10)
 NC_TRN       = COEFF(1,11)
 NC_AUTO      = COEFF(1,12)
 NC_AUTOSR    = COEFF(1,13)

 COEFF_DCBD_WALK   = COEFF(1,15)
 COEFF_DCBD_PNR    = COEFF(1,16)
 COEFF_DCBD_KNR    = COEFF(1,17)

; Nesting Coefficients
 NC_DriveAlone = NC_AUTO
 NC_SharedRide = NC_AUTO * NC_AUTOSR
 NC_WALKBUS =  NC_TRNAC
 NC_PNRBus =  NC_TRNAC
 NC_KNRBus =  NC_TRNAC
endif


FILLMW MW[1]=MI.1.1,2,3,4,5,6,7,8 ;,9        ; highway skims
FILLMW MW[11]=MI.11.HOVsave@PERIOD@          ; HOVsavePK for peak period (HBW) and HOVsaveOP for off-peak period (HBNW and NHB)
FILLMW MW[291]=MI.12.@STATION_TABLE@         ; 7 tables - stations zones for pnr-bus,knr-bus,cbdknr-bus,fringe,pnr-prj,knr-prj,cbdknr-prj paths
FILLMW MW[21]=MI.2.1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ;tskim1
FILLMW MW[61]=MI.4.1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ;tskims3
FILLMW MW[211]=MI.2.14
FILLMW MW[213]=MI.4.14
FILLMW MW[215]=MI.4.14
FILLMW MW[220]=MI.2.11
FILLMW MW[222]=MI.4.11
FILLMW MW[224]=MI.4.11

FILLMW MW[251]=MI.13.@PURP@    ; person trip table

if (I=1)
; DEBUG MODE CHOICE
 IF ({DebugMC}>0)
   print list='MODE CHOICE TRACE @PURP@-@PERIOD@','\n\n',printo=1
   print list='-- @DATE.RUNDATE@     ',printo=1
   print list='-- @TIME.RUNTIME@     ','\n\n',printo=1
   print list='NERPM \n\nSelected Interchange for Tracing:    ',{SelOrigin}(4.0),'-',{SelDest}(4.0),'\n\n',printo=1
   print list='PURPOSE -           @PURP@  ',printo=1
   print list='SKIMS/PERIOD        @PERIOD@',printo=1
   print list='\nCoefficients                ',printo=1
   print list='       IVTT                 ',COEFF_IVTT(7.4),printo=1
   print list='       OVT                  ',COEFF_OVT(7.4),printo=1
   print list='       AAT                  ',COEFF_AAT(7.4),printo=1
   print list='       XFER                 ',COEFF_XFER(7.4),printo=1
   print list='       COST                 ',COEFF_COST(7.4),printo=1
   PRINT LIST='       DUM_CBD_WALK         ',COEFF_DCBD_WALK(7.4),printo=1
   PRINT LIST='       DUM_CBD_PNR          ',COEFF_DCBD_PNR(7.4),printo=1
   PRINT LIST='       DUM_CBD_KNR          ',COEFF_DCBD_KNR(7.4),printo=1
   print list='\n\nNesting Coeffcient      ',printo=1
   print list='        NC_TRNAC            ',NC_TRNAC(7.4),printo=1
   print list='        NC_TRN              ',NC_TRN(7.4),printo=1
   print list='        NC_AUTOSR           ',NC_AUTOSR(7.4),printo=1
   print list='        NC_AUTO             ',NC_AUTO(7.4),printo=1

   print list='\n\nConstants               ',printo=1
   print list='        Drive alone (0 car) ',K_da_auto0(8.4),printo=1
   print list='        HOV2 (0 car)        ',K_sr1pass_auto0(8.4),printo=1
   print list='        HOV2 (1 car)        ',K_sr1pass_auto1(8.4),printo=1
   print list='        HOV2 (2 car)        ',K_sr1pass_auto2(8.4),printo=1
   print list='        HOV3+ (0 car)       ',K_sr2pass_auto0(8.4),printo=1
   print list='        HOV3+ (1 car)       ',K_sr2pass_auto1(8.4),printo=1
   print list='        HOV3+ (2 car)       ',K_sr2pass_auto2(8.4),printo=1
   print list='        Walk Bus (0 car)    ',K_walkbus_auto0(8.4),printo=1
   print list='        Walk Bus (1 car)    ',K_walkbus_auto1(8.4),printo=1
   print list='        Walk Bus (2 car)    ',K_walkbus_auto2(8.4),printo=1
   print list='        PNR Bus (0 car)     ',K_PNRBUS_auto0(8.4),printo=1
   print list='        PNR Bus (1 car)     ',K_PNRBUS_auto1(8.4),printo=1
   print list='        PNR Bus (2 car)     ',K_PNRBUS_auto2(8.4),printo=1
   print list='        KNR Bus (0 car)     ',K_KNRBUS_auto0(8.4),printo=1
   print list='        KNR Bus (1 car)     ',K_KNRBUS_auto1(8.4),printo=1
   print list='        KNR Bus (2 car)     ',K_KNRBUS_auto2(8.4),printo=1

   print list='\n\nParameters used in disutility calculations:', printo=1
   print list='Highway Operating Cost (cents/mi)   ',{hwyopcost}(5.3),printo=1
   print list='CTOLL Multiplier (toll to time)     ',{CTOLL}(5.3),printo=1
   print list='Value of ZAPZERO                    ',{ZAPZERO}(5.3),printo=1
   print list='Auto Occupancy 3+ (HBW)             ',{oc3vhbw}(5.3),printo=1
   print list='Auto Occupancy 3+ (HBNW)            ',{oc3vhbnw}(5.3),printo=1
   print list='Inflation factor - Tolls            ',{InflationToll}(5.3),printo=1
   print list='Inflation factor - Auto Op Cost     ',{InflationAOC}(5.3),printo=1
   print list='Inflation factor - Parking Cost     ',{InflationParkCost}(5.3),printo=1
   print list='Inflation factor - Transit Fare     ',{InflationFare}(5.3),printo=1
   print list='Auto Access Auto Occupancy Factor   ',{OCCPNRAccess}(5.3),printo=1
   print list='Times are in minutes. Costs are in cents.        ',printo=1
 endif

 ; Echo to Mode Choice summary file
 PRINT LIST="\f+++++ MODE XCHOICE SUMMARY +++++\n" PRINTO=2
 PRINT LIST='-- @DATE.RUNDATE@     ','-- @TIME.RUNTIME@     ','\n',PRINTO=2
 PRINT LIST="{SCENARIO_FULLNAME}" PRINTO=2
 PRINT LIST="{SCENARIO_SHORTNAME}\n" PRINTO=2
 PRINT LIST='PURPOSE -           @PURP@  ',printo=2
 PRINT LIST='SKIMS/PERIOD        @PERIOD@',printo=2
 PRINT LIST='\nCoefficients                ',printo=2
 PRINT LIST='       IVTT                 ',COEFF_IVTT(7.4),printo=2
 PRINT LIST='       OVT                  ',COEFF_OVT(7.4),printo=2
 PRINT LIST='       AAT                  ',COEFF_AAT(7.4),printo=2
 PRINT LIST='       XFER                 ',COEFF_XFER(7.4),printo=2
 PRINT LIST='       COST                 ',COEFF_COST(7.4),printo=2
 PRINT LIST='       DUM_CBD_WALK         ',COEFF_DCBD_WALK(7.4),printo=2
 PRINT LIST='       DUM_CBD_PNR          ',COEFF_DCBD_PNR(7.4),printo=2
 PRINT LIST='       DUM_CBD_KNR          ',COEFF_DCBD_KNR(7.4),printo=2
 PRINT LIST='\n\nNesting Coeffcient      ',printo=2
 PRINT LIST='        NC_TRNAC            ',NC_TRNAC(7.4),printo=2
 PRINT LIST='        NC_TRN              ',NC_TRN(7.4),printo=2
 PRINT LIST='        NC_AUTOSR           ',NC_AUTOSR(7.4),printo=2
 PRINT LIST='        NC_AUTO             ',NC_AUTO(7.4),printo=2
 PRINT LIST='\n\nConstants               ',printo=2
 PRINT LIST='        Drive alone (0 car) ',K_da_auto0(8.4),printo=2
 PRINT LIST='        Drive alone (1 car) ',K_da_auto1(8.4),printo=2
 PRINT LIST='        Drive alone (2 car) ',K_da_auto2(8.4),printo=2
 PRINT LIST='        HOV2 (0 car)        ',K_sr1pass_auto0(8.4),printo=2
 PRINT LIST='        HOV2 (1 car)        ',K_sr1pass_auto1(8.4),printo=2
 PRINT LIST='        HOV2 (2 car)        ',K_sr1pass_auto2(8.4),printo=2
 PRINT LIST='        HOV3+ (0 car)       ',K_sr2pass_auto0(8.4),printo=2
 PRINT LIST='        HOV3+ (1 car)       ',K_sr2pass_auto1(8.4),printo=2
 PRINT LIST='        HOV3+ (2 car)       ',K_sr2pass_auto2(8.4),printo=2
 PRINT LIST='        Walk Bus (0 car)    ',K_walkbus_auto0(8.4),printo=2
 PRINT LIST='        Walk Bus (1 car)    ',K_walkbus_auto1(8.4),printo=2
 PRINT LIST='        Walk Bus (2 car)    ',K_walkbus_auto2(8.4),printo=2
 PRINT LIST='        PNR Bus (0 car)     ',K_PNRBUS_auto0(8.4),printo=2
 PRINT LIST='        PNR Bus (1 car)     ',K_PNRBUS_auto1(8.4),printo=2
 PRINT LIST='        PNR Bus (2 car)     ',K_PNRBUS_auto2(8.4),printo=2
 PRINT LIST='        KNR Bus (0 car)     ',K_KNRBUS_auto0(8.4),printo=2
 PRINT LIST='        KNR Bus (1 car)     ',K_KNRBUS_auto1(8.4),printo=2
 PRINT LIST='        KNR Bus (2 car)     ',K_KNRBUS_auto2(8.4),printo=2

 PRINT LIST='\n\nParameters used in disutility calculations:', printo=2
 PRINT LIST='Highway Operating Cost (cents/mi)   ',{hwyopcost}(5.3),printo=2
 PRINT LIST='CTOLL Multiplier (toll to time)     ',{CTOLL}(5.3),printo=2
 PRINT LIST='Value of ZAPZERO                    ',{ZAPZERO}(5.3),printo=2
 PRINT LIST='Auto Occupancy 3+ (HBNW)            ',{oc3vhbnw}(5.3),printo=2
 PRINT LIST='Auto Occupancy 3+ (NHB)             ',{oc3vnhb}(5.3),printo=2
 PRINT LIST='Inflation factor - Tolls            ',{InflationToll}(5.3),printo=2
 PRINT LIST='Inflation factor - Auto Op Cost     ',{InflationAOC}(5.3),printo=2
 PRINT LIST='Inflation factor - Parking Cost     ',{InflationParkCost}(5.3),printo=2
 PRINT LIST='Inflation factor - Transit Fare     ',{InflationFare}(5.3),printo=2
 PRINT LIST='Auto Access Auto Occupancy Factor   ',{OCCPNRAccess}(5.3),printo=2
 PRINT LIST='Times reflected in minutes.         ',printo=2
 PRINT LIST='Costs are in cents.                 ',printo=2

endif

; for debugging
;JLOOP
; if ({DebugMC}=1)
 ;  if (I>{SELORIGIN}) EXIT
  ;  if (I<>{SELDEST}) BREAK
  ;endif
;ENDJLOOP

; Divide person trip table into different markets
; Zero car HHs
; for NHB purpose, there is no market segmentation and hence put all trips into zer-car household trip table
if('@PURP@'='HBW')
 MW[252]=MW[251] * ZI.3.wksh0[I]*0.01
else
 MW[252]=MW[251] * ZI.3.nwsh0[I]*0.01
endif

; One car HHs

IF ('@PURP@'='NHB') MW[252]=MW[251]
IF('@PURP@'='HBW')
 MW[253]=MW[251] * ZI.3.wksh1[I]*0.01
ELSE
 MW[253]=MW[251] * ZI.3.nwsh1[I]*0.01
ENDIF

; Two+ car HHs
IF ('@PURP@'='NHB') MW[253]=0
IF('@PURP@'='HBW')
 MW[254]=MW[251] * ZI.3.wksh2[I]*0.01
ELSE
 MW[254]=MW[251] * ZI.3.nwsh2[I]*0.01
ENDIF

IF ('@PURP@'='NHB') MW[254]=0

; ===================================================================================
; @PURP@ (@PERIOD@) TRIP PURPOSE
; ===================================================================================

; (1) Terminal Time (both at production-end and attraction-end)
; (2) Auto run time
; (3) highway operating cost
; (4) Toll (assumed in $)
; (5) Parking cost
; (6) CBD/EXURBAN dummy variables
; (7) Transit fare
; (8) Out-of vehicle time (walk/transfer time/wait)

; ******************************** UTILITY CALCULATIONS ************************************
  ;   DRIVE ALONE ELEMENTS OF UTILITY ARE:
      MW[301]=COEFF_OVT * (ZI.1.TERMTIME[I] + ZI.1.TERMTIME[J]) +                                       ; Terminal time
              COEFF_IVTT * (MW[4] - ({InflationToll} * (MW[1] * {CTOLL} * 60))) +                       ; IVTT
              COEFF_IVTT * {InflationToll} * (MW[1] * {CTOLL} * 60) +                                   ; Toll converted to equivalent minutes
@noNHB@       COEFF_COST * ((({InflationParkCost} * ZI.1.@PRK@[J]) / 1.0) / 2.0) +                      ; Auto parking cost
@isNHB@       COEFF_COST * ((({InflationParkCost} * ZI.1.@PRK@[J]) / 1.0) / 1.0) +                      ; Auto parking cost (NHB)
              COEFF_COST * (({InflationAOC} * MW[2] * {hwyopcost}) / 1.0) +                             ; Auto operating cost
@isNHB@       COEFF_DCBD * (ZI.1.CBD_DUMMY[I] + ZI.1.CBD_DUMMY[J]) +                                    ; CBD dummy (NHB)
@noNHB@       COEFF_DCBD * ZI.1.CBD_DUMMY[J] +                                                          ; CBD dummy
              COEFF_DEXUP * ZI.1.EXU_DUMMY[I] +                                                         ; Exurban production dummy
              COEFF_DEXUA * ZI.1.EXU_DUMMY[J]                                                           ; Exurban attraction dummy

  ;   SHARED RIDE PERSONS/SHARE RIDE - ONE PASSENGER ELEMENTS OF UTILITY ARE:
      MW[302]=COEFF_OVT * (ZI.1.TERMTIME[I] + ZI.1.TERMTIME[J]) +                                       ; Terminal time
              COEFF_IVTT * (MW[4] - ({InflationToll} * (MW[5] * {CTOLL} * 60))) +                       ; IVTT
              COEFF_IVTT * ({InflationToll} * (MW[5] * {CTOLL} * 60) / 2.0) -                           ; Toll converted to equivalent minutes
              COEFF_IVTT * (MAX (MW[11], 0)) +                                                          ; HOV time savings
@noNHB@       COEFF_COST * ((({InflationParkCost} * ZI.1.@PRK@[J]) / 2.0) / 2.0) +                      ; Auto parking cost
@isNHB@       COEFF_COST * ((({InflationParkCost} * ZI.1.@PRK@[J]) / 2.0) / 1.0) +                      ; Auto parking cost (NHB)
              COEFF_COST * (({InflationAOC} * MW[2] * {hwyopcost}) / 2.0) +                             ; Auto operating cost
@isNHB@       COEFF_DCBD * (ZI.1.CBD_DUMMY[I] + ZI.1.CBD_DUMMY[J]) +                                    ; CBD dummy (NHB)
@noNHB@       COEFF_DCBD * ZI.1.CBD_DUMMY[J] +                                                          ; CBD dummy
              COEFF_DEXUP * ZI.1.EXU_DUMMY[I] +                                                         ; Exurban production dummy
              COEFF_DEXUA * ZI.1.EXU_DUMMY[J]                                                           ; Exurban attraction dummy

  ;   SHARED RIDE - 2+ PASSENGER ELEMENTS OF UTILITY ARE:
      MW[303]=COEFF_OVT * (ZI.1.TERMTIME[I] + ZI.1.TERMTIME[J]) +                                       ; Terminal time
              COEFF_IVTT * (MW[4] - ({InflationToll} * (MW[5] * {CTOLL} * 60))) +                       ; IVTT
              COEFF_IVTT * ({InflationToll} * (MW[5] * {CTOLL} * 60) / @OCC3@) -                        ; Toll converted to equivalent minutes
              COEFF_IVTT * (MAX (MW[11], 0)) +                                                          ; HOV time savings
@noNHB@       COEFF_COST * ((({InflationParkCost} * ZI.1.@PRK@[J]) / @OCC3@) / 2.0) +                   ; Auto parking cost
@isNHB@       COEFF_COST * ((({InflationParkCost} * ZI.1.@PRK@[J]) / @OCC3@) / 1.0) +                   ; Auto parking cost (NHB)
              COEFF_COST * (({InflationAOC} * MW[2] * {hwyopcost}) / @OCC3@) +                          ; Auto operating cost
@isNHB@       COEFF_DCBD * (ZI.1.CBD_DUMMY[I] + ZI.1.CBD_DUMMY[J]) +                                    ; CBD dummy (NHB)
@noNHB@       COEFF_DCBD * ZI.1.CBD_DUMMY[J] +                                                          ; CBD dummy
              COEFF_DEXUP * ZI.1.EXU_DUMMY[I] +                                                         ; Exurban production dummy
              COEFF_DEXUA * ZI.1.EXU_DUMMY[J]                                                           ; Exurban attraction dummy

 ;   WALK ACCESS - BUS ELEMENTS OF UTILITY ARE:
      MW[304]=COEFF_OVT * (MW[21] + MW[23]) +                                                   ; Walk & transfer time
              COEFF_OVT * (MW[32] + MW[33]) +                                                   ; Initial & transfer wait
              COEFF_IVTT * (MW[24] + MW[25] + MW[26] + MW[27] + MW[28] + MW[29] + MW[30]) +     ; IVTT
              COEFF_XFER * (MW[31]) +                                                           ; Number of transfers
              COEFF_COST * {InflationFare} * (MW[34]) +                                         ; Transit fare
@isNHB@       COEFF_DCBD_WALK * (ZI.1.CBD_DUMMY[I] + ZI.1.CBD_DUMMY[J]) +                       ; CBD dummy (NHB)
@noNHB@       COEFF_DCBD_WALK * ZI.1.CBD_DUMMY[J] +                                             ; CBD dummy
              COEFF_DEXUP * ZI.1.EXU_DUMMY[I] +                                                 ; Exurban production dummy
              COEFF_DEXUA * ZI.1.EXU_DUMMY[J]                                                   ; Exurban attraction dummy

 ;   PNR ACCESS - BUS ELEMENTS OF UTILITY ARE:
      MW[305]=COEFF_OVT * (MW[61] + MW[63]) +                                                   ; Walk & transfer time
              COEFF_OVT * (MW[72] + MW[73]) +                                                   ; Initial & transfer wait
              COEFF_IVTT * (MW[62]) +                                                           ; Auto access time
              COEFF_IVTT * (MW[64] + MW[65] + MW[66] + MW[67] + MW[68] + MW[69] + MW[70]) +     ; IVTT
              COEFF_XFER * (MW[71]) +                                                           ; Number of transfers
              COEFF_COST * {InflationFare} * (MW[74]) +                                         ; Transit fare
@isNHB@       COEFF_DCBD_PNR * (ZI.1.CBD_DUMMY[I] + ZI.1.CBD_DUMMY[J]) +                        ; CBD dummy (NHB)
@noNHB@       COEFF_DCBD_PNR * ZI.1.CBD_DUMMY[J] +                                              ; CBD dummy
              COEFF_DEXUP * ZI.1.EXU_DUMMY[I] +                                                 ; Exurban production dummy
              COEFF_DEXUA * ZI.1.EXU_DUMMY[J]                                                   ; Exurban attraction dummy

  ;   KNR ACCESS - BUS ELEMENTS OF UTILITY ARE:
      MW[306]=COEFF_OVT * (MW[61] + MW[63]) +                                                 ; Walk & transfer time
              COEFF_OVT * (MW[72] + MW[73]) +                                                 ; Initial & transfer wait
              COEFF_IVTT * (MW[62]-({InflationFare}*(COEFF(1,4)/COEFF(1,1))*MW[291])+(COEFF(1,3)/COEFF(1,1))*(-mw[292]+mw[293])) +                                 ; Auto access time
              COEFF_IVTT * (MW[64] + MW[65] + MW[66] + MW[67] + MW[68] + MW[69] + MW[70]) +    ; IVTT
              COEFF_XFER * (MW[71]) +                                                          ; Number of transfers
              COEFF_COST * {InflationFare} * (MW[74]) +                                        ; Transit fare
@isNHB@       COEFF_DCBD_KNR * (ZI.1.CBD_DUMMY[I] + ZI.1.CBD_DUMMY[J]) +                        ; CBD dummy (NHB)
@noNHB@       COEFF_DCBD_KNR * ZI.1.CBD_DUMMY[J] +                                              ; CBD dummy
              COEFF_DEXUP * ZI.1.EXU_DUMMY[I] +                                                 ; Exurban production dummy
              COEFF_DEXUA * ZI.1.EXU_DUMMY[J]                                                   ; Exurban attraction dummy

;******************************** END UTILITY CALCULATIONS ************************************

      JLOOP
; For PNR paths, determine the parking zone. This is used to get the station parking cost for NHB paths, as
;               we need to add in the other half of station parking costs.
; Check if the IVT times on the transit path exist (this avoids having a jloop in the utility calculations)
        IF ((MW[24]+MW[25]+MW[26]+MW[27]+MW[28]+MW[29]+MW[30])==0) MW[304]=-9999.99                                                                       ; walk-bus path

        IF ((MW[64] + MW[65] + MW[66] + MW[67] + MW[68] + MW[69] + MW[70])==0| mw[62]==0)                                                                                        ; pnr-bus path
          MW[305]=-9999.99
        ELSE
@isNHB@   MW[305]=MW[305] + COEFF_COST * ((({InflationFare}*mw[292])/{OCCPNRAccess})/2.0)                             ; add in other half of NHB PNR parking cost
        ENDIF

        IF ((MW[64] + MW[65] + MW[66] + MW[67] + MW[68] + MW[69] + MW[70])==0| mw[62]==0) MW[306]=-9999.99                                                                     ; knr bus path

       ; Minimum distance criteria on paths
        IF (MW[2] < {MinDistWalkAcc})
                MW[304]=-9999.99
        ENDIF
        IF (MW[2] < {MinDistAutoAcc})
                MW[305]=-9999.99
                MW[306]=-9999.99
        ENDIF
; NOTE: no min distance criteria for fringe parking
; For PNR paths, determine the parking zone. This is used to get the station parking cost for NHB paths, as
;               we need to add in the other half of station parking costs.
; debug report
       IF(({DebugMC} > 0) && (I={SelOrigin}) && (J={SelDest}))

IF('@PURP@'='HBW')
print list='Percent 0 car household at Production zone    ',ZI.3.wksh0[I],printo=1
print list='Percent 1 car household at Production zone    ',ZI.3.wksh1[I],printo=1
print list='Percent 2 car household at Production zone    ',ZI.3.wksh2[I],printo=1

ELSE
print list='Percent 0 car household at Production zone    ',ZI.3.nwsh0[I],printo=1
print list='Percent 1 car household at Production zone    ',ZI.3.nwsh1[I],printo=1
print list='Percent 2 car household at Production zone    ',ZI.3.nwsh2[I],printo=1
ENDIF


ENDIF
ENDJLOOP

;******************************** FINAL UTILITY CALCULATIONS ************************************
  ;   COMPOSITE UTILITY for drive alone
      MW[401]=(MW[301]+K_DA_AUTO0)/NC_DriveAlone
      MW[421]=(MW[301]+K_DA_AUTO1)/NC_DriveAlone
      MW[441]=(MW[301]+K_DA_AUTO2)/NC_DriveAlone
  ;   COMPOSITE UTILITY for shared ride 2
      MW[402]=(MW[302]+K_SR1PASS_AUTO0)/NC_SharedRide
      MW[422]=(MW[302]+K_SR1PASS_AUTO1)/NC_SharedRide
      MW[442]=(MW[302]+K_SR1PASS_AUTO2)/NC_SharedRide
  ;   COMPOSITE UTILITY for shared ride 2+
      MW[403]=(MW[303]+K_SR2PASS_AUTO0)/NC_SharedRide
      MW[423]=(MW[303]+K_SR2PASS_AUTO1)/NC_SharedRide
      MW[443]=(MW[303]+K_SR2PASS_AUTO2)/NC_SharedRide
  ;   COMPOSITE UTILITY for walk to bus
      MW[404]=((MW[304]+K_WALKBUS_AUTO0)/NC_WALKBUS )
      MW[424]=((MW[304]+K_WALKBUS_AUTO1)/NC_WALKBUS)
      MW[444]=((MW[304]+K_WALKBUS_AUTO2)/NC_WALKBUS)
  ;   COMPOSITE UTILITY for PNR to bus
      MW[405]=((MW[305]+K_PNRBUS_AUTO0)/NC_PNRBus )
      MW[425]=((MW[305]+K_PNRBUS_AUTO1)/NC_PNRBus)
      MW[445]=((MW[305]+K_PNRBUS_AUTO2)/NC_PNRBus )
  ;   COMPOSITE UTILITY for KNR to bus
      MW[406]=((MW[306]+K_KNRBUS_AUTO0)/NC_KNRBus )
      MW[426]=((MW[306]+K_KNRBUS_AUTO1)/NC_KNRBus )
      MW[446]=((MW[306]+K_KNRBUS_AUTO2)/NC_KNRBus )

; use ZAPZERO here to zero out the drive alone and PNR access trips for zero-car households
      if ( ({ZAPZERO} = 1) && (('@PURP@'=='HBW') || ('@PURP@'=='HBNW')) )
        MW[401] = -9999.99     ; drive alone for 0car households
        MW[405] = -9999.99     ; PNR-bus trips for 0car households
     endif

 JLOOP
  if (({DebugMC} > 0) && (I={SelOrigin}) && (J={SelDest}))
   print list='\n\n********************************************************************************************', printo=1
;   print list=' Access Type: ',ACC(1.0),'. ',ACC_NAME, printo=1
   print list='********************************************************************************************','\n' printo=1
   print list='Value of CBD dummy at attraction-end (1 or 0)                     ',ZI.1.CBD_DUMMY[J],printo=1
   print list='Value of EXURBAN dummy at production-end (1 or 0)                 ',ZI.1.EXU_DUMMY[I],printo=1
   print list='Value of EXURBAN dummy at attraction-end (1 or 0)                 ',ZI.1.EXU_DUMMY[J],printo=1

   print list='\n\n\nElements used in Drive Alone disutility calculations: ***************************', printo=1
   print list='In-Vehicle time                                                   ',MW[3][J],printo=1
   print list='Out of Vehicle time (Terminal time at production-end)             ',ZI.1.TERMTIME[I],printo=1
   print list='Out of Vehicle time (Terminal time at attraction-end)             ',ZI.1.TERMTIME[J],printo=1
   print list='Parking Cost                                                      ',ZI.1.@PRK@[J],printo=1
   print list='Highway Distance (in miles)                                       ',MW[2][J],printo=1
   print list='Highway Toll (in $)                                               ',MW[1][J],printo=1
   print list='\nDrive alone disutility                                          ',MW[301][J](12.4),printo=1
   print list='\nDrive alone composite disutility (0 car)                        ',MW[401][J](12.4),printo=1
   print list='Drive alone composite disutility (1 car)                        ',MW[421][J](12.4),printo=1
   print list='Drive alone composite disutility (2 car)                        ',MW[441][J](12.4),printo=1
   print list='\nExponentiated drive alone composite disutility (0 car)        ',exp(MW[401][J])(14.12),printo=1
   print list='Exponentiated drive alone composite disutility (1 car)        ',exp(MW[421][J])(14.12),printo=1
   print list='Exponentiated drive alone composite disutility (2 car)        ',exp(MW[441][J])(14.12),printo=1

   print list='\n\n\nElements used in Shared Ride 2 disutility calculations: ***************************', printo=1
   print list='In-Vehicle time                                                   ',MW[3][J],printo=1
   print list='In-Vehicle time savings by using HOV lanes                        ',MAX((MW[7][J]-MW[3][J]), 0),printo=1
   print list='Out of Vehicle time (Terminal time at production-end)             ',ZI.1.TERMTIME[I],printo=1
   print list='Out of Vehicle time (Terminal time at attraction-end)             ',ZI.1.TERMTIME[J],printo=1
   print list='Parking Cost                                                      ',ZI.1.@PRK@[J],printo=1
   print list='Highway Distance (in miles)                                       ',MW[6][J],printo=1
   print list='Highway Toll (in $)                                               ',MW[1][J],printo=1
   print list='\nShared ride 2 disutility                                        ',MW[302][J](12.4),printo=1
   print list='\nShared ride 2 composite disutility (0 car)                      ',MW[402][J](12.4),printo=1
   print list='Shared ride 2 composite disutility (1 car)                      ',MW[422][J](12.4),printo=1
   print list='Shared ride 2 composite disutility (2 car)                      ',MW[442][J](12.4),printo=1
   print list='\nExponentiated shared ride composite disutility (0 car)        ',exp(MW[402][J])(14.12),printo=1
   print list='Exponentiated shared ride 2 composite disutility (1 car)      ',exp(MW[422][J])(14.12),printo=1
   print list='Exponentiated shared ride 2 composite disutility (2 car)      ',exp(MW[442][J])(14.12),printo=1

   print list='\n\n\nElements used in Shared Ride 2+ disutility calculations: ***************************', printo=1
   print list='In-Vehicle time                                                   ',MW[3][J],printo=1
   print list='In-Vehicle time savings by using HOV lanes                        ',MAX((MW[7][J]-MW[3][J]), 0),printo=1
   print list='Out of Vehicle time (Terminal time at production-end)             ',ZI.1.TERMTIME[I],printo=1
   print list='Out of Vehicle time (Terminal time at attraction-end)             ',ZI.1.TERMTIME[J],printo=1
   print list='Parking Cost                                                      ',ZI.1.@PRK@[J],printo=1
   print list='Highway Distance (in miles)                                       ',MW[6][J],printo=1
   print list='Occupancy rate for 2+ car market                                  ',@OCC3@,printo=1
   print list='Highway Toll (in $)                                               ',MW[1][J],printo=1
   print list='\nShared ride 2+ disutility                                       ',MW[303][J](12.4),printo=1
   print list='\nShared ride 2+ composite disutility (0 car)                     ',MW[403][J](12.4),printo=1
   print list='Shared ride 2+ composite disutility (1 car)                     ',MW[423][J](12.4),printo=1
   print list='Shared ride 2+ composite disutility (2 car)                     ',MW[443][J](12.4),printo=1
   print list='\nExponentiated shared ride 2+ composite disutility (0 car)     ',exp(MW[403][J])(14.12),printo=1
   print list='Exponentiated shared ride 2+ composite disutility (1 car)     ',exp(MW[423][J])(14.12),printo=1
   print list='Exponentiated shared ride 2+ composite disutility (2 car)     ',exp(MW[443][J])(14.12),printo=1

   print list='\n\n\nElements used in Walk Bus disutility calculations: ***************************', printo=1
   print list='In-Vehicle time (Bus)                                             ',MW[24][J],printo=1
   print list='In-Vehicle time (PRJ Bus)                                         ',MW[25][J],printo=1
   print list='In-Vehicle time (Circulator)                                      ',MW[26][J],printo=1
   print list='In-Vehicle time (Rail)                                            ',(MW[27][J]+MW[28][J]),printo=1
   print list='In-Vehicle time (Project Mode)                                    ',MW[30][J],printo=1
   print list='Out of Vehicle time (Walk)                                        ',MW[21][J],printo=1
   print list='Out of Vehicle time (Transfer Walk)                               ',MW[23][J],printo=1
   print list='Out of Vehicle time (IWAIT)                                       ',MW[32][J],printo=1
   print list='Out of Vehicle time (XWAIT)                                       ',MW[33][J],printo=1
   print list='Number of transfers                                               ',MW[31][J],printo=1
   print list='Transit Fare (in cents)                                           ',MW[34][J],printo=1
   print list='\nWalk Bus disutility                                             ',MW[304][J](12.4),printo=1
;   print list='Walk access dummy (added to the utility)                          ',walk_acc_dum,printo=1
   print list='\nWalk Bus composite disutility (0 car)                           ',MW[404][J](12.4),printo=1
   print list='Walk Bus composite disutility (1 car)                           ',MW[424][J](12.4),printo=1
   print list='Walk Bus composite disutility (2+ car)                          ',MW[444][J](12.4),printo=1
   print list='\nExponentiated walk bus composite disutility (0 car)           ',exp(MW[404][J])(14.12),printo=1
   print list='Exponentiated walk bus composite disutility (1 car)           ',exp(MW[424][J])(14.12),printo=1
   print list='Exponentiated walk bus composite disutility (2 car)           ',exp(MW[444][J])(14.12),printo=1

   print list='\n\n\nElements used in PNR Bus disutility calculations: ***************************', printo=1
   print list='In-Vehicle time (Bus)                                             ',MW[64][J],printo=1
   print list='In-Vehicle time (PRJ Bus)                                         ',MW[65][J],printo=1
   print list='In-Vehicle time (Circulator)                                      ',MW[66][J],printo=1
   print list='In-Vehicle time (Rail)                                            ',(MW[67][J]+MW[68][J]),printo=1
   print list='In-Vehicle time (Project Mode)                                    ',MW[70][J],printo=1
   print list='Out of Vehicle time (Walk)                                        ',MW[61][J],printo=1
   print list='Out of Vehicle time (Auto access time in IVT min - weighted)      ',MW[62][J],printo=1
   print list='Out of Vehicle time (Transfer Walk)                               ',MW[63][J],printo=1
   print list='Out of Vehicle time (IWAIT)                                       ',MW[72][J],printo=1
   print list='Out of Vehicle time (XWAIT)                                       ',MW[73][J],printo=1
   print list='Number of transfers                                               ',MW[71][J],printo=1
   print list='Transit Fare (in cents)                                           ',MW[74][J],printo=1
   print list='\nPNR Bus disutility                                              ',MW[305][J](12.4),printo=1
;   print list='Auto access dummy (added to the utility)                          ',auto_acc_dum,printo=1
   print list='\nPNR Bus composite disutility (0 car)                            ',MW[405][J](12.4),printo=1
   print list='PNR Bus composite disutility (1 car)                            ',MW[425][J](12.4),printo=1
   print list='PNR Bus composite disutility (2 car)                            ',MW[445][J](12.4),printo=1
   print list='\nExponentiated PNR Bus composite disutility (0 car)            ',exp(MW[405][J])(14.12),printo=1
   print list='Exponentiated PNR Bus composite disutility (1 car)            ',exp(MW[425][J])(14.12),printo=1
   print list='Exponentiated PNR Bus composite disutility (2 car)            ',exp(MW[445][J])(14.12),printo=1

   print list='\n\n\nElements used in KNR Bus disutility calculations: ***************************', printo=1
   print list='In-Vehicle time (Bus)                                             ',MW[64][J],printo=1
   print list='In-Vehicle time (Circulator)                                      ',MW[66][J],printo=1
   print list='In-Vehicle time (Rail)                                            ',(MW[67][J]+MW[108][J]),printo=1
   print list='In-Vehicle time (Project Mode)                                    ',MW[70][J],printo=1
   print list='Out of Vehicle time (Walk)                                        ',MW[61][J],printo=1
   print list='Out of Vehicle time (Auto access time in IVT min)                 ',MW[62][J],printo=1
   print list='Out of Vehicle time (Transfer Walk)                               ',MW[63][J],printo=1
   print list='Out of Vehicle time (IWAIT)                                       ',MW[62][J],printo=1
   print list='Out of Vehicle time (XWAIT)                                       ',MW[73][J],printo=1
   print list='Transit Fare (in cents)                                           ',MW[74][J],printo=1
   print list='\nKNR Bus disutility                                              ',MW[306][J](12.4),printo=1
;   print list='Auto access dummy (added to the utility)                          ',auto_acc_dum,printo=1
   print list='\nKNR Bus composite disutility (0 car)                            ',MW[406][J](12.4),printo=1
   print list='KNR Bus composite disutility (1 car)                            ',MW[426][J](12.4),printo=1
   print list='KNR Bus composite disutility (2 car)                            ',MW[446][J](12.4),printo=1
   print list='\nExponentiated KNR Bus composite disutility (0 car)            ',exp(MW[406][J])(14.12),printo=1
   print list='Exponentiated KNR Bus composite disutility (1 car)            ',exp(MW[426][J])(14.12),printo=1
   print list='Exponentiated KNR Bus composite disutility (2 car)            ',exp(MW[446][J])(14.12),printo=1
   print list="PNR parkcost                                                  ",(COEFF(1,4)/COEFF(1,1))*MW[292]
   print list="termtime difference betn PNR and KNR                          ",(COEFF(1,3)/COEFF(1,1))*(-mw[293]+mw[294])
   print list="see this",(MW[62]-({InflationFare}*(COEFF(1,4)/COEFF(1,1))*MW[292])+(COEFF(1,3)/COEFF(1,1))*(-mw[293]+mw[294]))

  endif

 ENDJLOOP

; ############################# XCHOICE SETUP ###################################

; AUTO0
      DMD=252
      XCHOICE ALTERNATIVES=da,hov2,hov3,walkbus,pnrbus,knrbus,
      UTILITIESMW=401,402,403,404,405,406,
      DEMANDMW=DMD,
      ODEMANDMW=501,502,503,504,505,506,
      STARTMW=900,
;     Model Structure
;     Top level nest
      SPLIT=Total NC_AUTO Auto NC_TRN Transit,
;     Auto nest
      SPLIT=Auto 1.0 da NC_AUTOSR hov, SPLITCOMP=36,
;     HOV nest
      SPLIT=hov 1.0 hov2 1.0 hov3,
;     Transit nest
      SPLIT= Transit 1.0 Walkbus 1.0 PNRbus 1.0 KNRbus

; AUTO1
      DMD=253
      XCHOICE ALTERNATIVES=da,hov2,hov3,walkbus,pnrbus,knrbus,
      UTILITIESMW=421,422,423,424,425,426,
      DEMANDMW=DMD,
      ODEMANDMW=521,522,523,524,525,526,
      STARTMW=900,
;     Model Structure
;     Top level nest
      SPLIT=Total NC_AUTO Auto NC_TRN Transit,
;     Auto nest
      SPLIT=Auto 1.0 da NC_AUTOSR hov, SPLITCOMP=37,
;     HOV nest
      SPLIT=hov 1.0 hov2 1.0 hov3,
;     Transit nest
      SPLIT= Transit 1.0 Walkbus 1.0 PNRbus 1.0 KNRbus


; AUTO2
      DMD=254
      XCHOICE ALTERNATIVES=da,hov2,hov3,walkbus,pnrbus,knrbus,
      UTILITIESMW=441,442,443,444,445,446,
      DEMANDMW=DMD,
      ODEMANDMW=541,542,543,544,545,546,
      STARTMW=900,
;     Model Structure
;     Top level nest
      SPLIT=Total NC_AUTO Auto NC_TRN Transit,
;     Auto nest
      SPLIT=Auto 1.0 da NC_AUTOSR hov, SPLITCOMP=38,
;     HOV nest
      SPLIT=hov 1.0 hov2 1.0 hov3,
;     Transit nest
      SPLIT= Transit 1.0 Walkbus 1.0 PNRbus 1.0 KNRbus

; user benefit table - exponentiated auto utility
      MW[56]=exp(NC_AUTOSR * MW[36])
      MW[57]=exp(NC_AUTOSR * MW[37])
      MW[58]=exp(NC_AUTOSR * MW[38])

; ############################################ END XCHOICE #################################

 ; now some debuging
   LOOP MARKET=1,3
    if (MARKET=1) MARKET_NAME='Zero Car HHs     '
    if (MARKET=2) MARKET_NAME='One Car HHs      '
    if (MARKET=3) MARKET_NAME='Two+ Car HHs     '

    _cnt=(MARKET-1)*20
    _cnt1=260+(MARKET-1)*3
    M1=501+_cnt,M2=502+_cnt,M3=503+_cnt,M4=504+_cnt,M5=505+_cnt,M6=506+_cnt;,M7=507+_cnt,M8=508+_cnt,M9=509+_cnt;,M12=512+_cnt

    IF ({DebugMC}>0)
     JLOOP
      if ((I={SelOrigin}) && (J={SelDest}))

; ################### SHARES (HAND CALCULATION - check it against XCHOICE RESULTS) ###########################################
       _konn = 0.0000000000000001

       print list='\n\nMARKET:           ',MARKET_NAME,printo=1
       print list='***************************************************',printo=1

       ; transit nest

       if ((exp(MW[404+_cnt][J])+exp(MW[405+_cnt][J])+exp(MW[406+_cnt][J]))>_konn)
         shrwkb=exp(MW[404+_cnt][J])/(exp(MW[404+_cnt][J])+exp(MW[405+_cnt][J])+exp(MW[406+_cnt][J]))
         shrpnrb=exp(MW[405+_cnt][J])/(exp(MW[404+_cnt][J])+exp(MW[405+_cnt][J])+exp(MW[406+_cnt][J]))
         shrknrb=exp(MW[406+_cnt][J])/(exp(MW[404+_cnt][J])+exp(MW[405+_cnt][J])+exp(MW[406+_cnt][J]))
         eutilt=exp(NC_TRNAC*ln(exp(MW[404+_cnt][J])+exp(MW[405+_cnt][J])+exp(MW[406+_cnt][J])))
       else
         shrwkb=0
         shrpnrb=0
         shrknrb=0
         eutilt=0
       endif
       print list='\nExponentiated transit nest disutility       ',eutilt(14.12),printo=1

       ; hov nest
       if ((exp(MW[402+_cnt][J])+exp(MW[403+_cnt][J]))>_konn)
         shrhov2sr=exp(MW[402+_cnt][J])/(exp(MW[402+_cnt][J])+exp(MW[403+_cnt][J]))
         shrhov3sr=exp(MW[403+_cnt][J])/(exp(MW[402+_cnt][J])+exp(MW[403+_cnt][J]))
         eutilsr=exp(NC_AUTOSR*ln(exp(MW[402+_cnt][J])+exp(MW[403+_cnt][J])))
       else
         shrhov2sr=0
         shrhov3sr=0
         eutilsr=0
       endif
       ; auto nest
       if ((exp(MW[401+_cnt][J])+eutilsr)>_konn)
         shrdaa=exp(MW[401+_cnt][J])/(exp(MW[401+_cnt][J])+eutilsr)
         shrsra=eutilsr/(exp(MW[401+_cnt][J])+eutilsr)
         eutila=exp(NC_AUTO*ln(exp(MW[401+_cnt][J])+eutilsr))
       else
         shrdaa=0
         shrsra=0
         eutila=0
       endif
       print list='\nExponentiated Auto disutility                 ',eutila(14.12),printo=1
       ; top nest
       if ((eutila+eutilt)>_konn)
         shra=eutila/(eutila+eutilt)
         shrt=eutilt/(eutila+eutilt)
         eutil=(eutila+eutilt)
       else
         shra=0
         shrt=0

         eutil=0
       endif
       print list='\nExponentiated total disutility                ',eutil(14.12),printo=1

       ; individual shares
       shrda=shrdaa*shra
       shrhov2=shrhov2sr*shrsra*shra
       shrhov3=shrhov3sr*shrsra*shra
       shrwalkbus=shrwkb*shrt

       shrpnrbus=shrpnrb*shrt

       shrknrbus=shrknrb*shrt

       ; print shares
       print list='\n\nTrips by mode:                       Share',printo=1
       print list='    Drive Alone                    ',shrda(7.8),printo=1
       print list='    HOV2                           ',shrhov2(7.8),printo=1
       print list='    HOV3                           ',shrhov3(7.8),printo=1
       print list='    Walk Bus                       ',shrwalkbus(7.8),printo=1

       print list='    PNR Bus                        ',shrpnrbus(7.8),printo=1

       print list='    KNR Bus                        ',shrknrbus(7.8),printo=1

       print list='\n\nCalculated shares using the utility equations:       ', printo=1
       print list='   Top Nest:                                         ', printo=1
       print list='        Auto Share               ',shra(6.4), printo=1
       print list='        Transit Share            ',shrt(6.4), printo=1

       print list='\n\n   Auto Nest:                                    ', printo=1
       print list='        Drive Alone Share        ',shrdaa(6.4), printo=1
       print list='        Shared Ride Share        ',shrsra(6.4), printo=1
       print list='\n\n   Shared Ride Nest:                             ', printo=1
       print list='        HOV2 Share               ',shrhov2sr(6.4), printo=1
       print list='        HOV3 Share               ',shrhov3sr(6.4), printo=1
       print list='\n\n   Transit Nest:                                 ', printo=1
       print list='        Walk Transit Share       ',shrwalkbus(6.4), printo=1
       print list='        PNR Transit Share        ',shrpnrbus(6.4), printo=1
       print list='        KNR Transit Share        ',shrknrbus(6.4), printo=1

; ######################### END HAND CALCULATIONS ########################################
; ##################### XCHOICE RESULTS###############################

       print list='\n\n\nCUBE UTILITY BASED XCHOICE MODEL\n',printo=1
       print list='*******************************************************************',printo=1
       print list='\nTotal demand between the interchange:  ',MW[251][J](10.2),printo=1
       print list='Demand from 0-car households           ',MW[_cnt1][J](10.2),printo=1
     IF ((MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J])>0)
       xcshrda  =(MW[M1][J]/(MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J]))
       xcshrhov2=(MW[M2][J]/(MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J]))
       xcshrhov3=(MW[M3][J]/(MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J]))
       xcshrwkb =(MW[M4][J]/(MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J]))
       xcshrpnrb=(MW[M5][J]/(MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J]))
       xcshrknrb=(MW[M6][J]/(MW[M1][J]+MW[M2][J]+MW[M3][J]+MW[M4][J]+MW[M5][J]+MW[M6][J]))

       xcshrtot=xcshrda+xcshrhov2+xcshrhov3+xcshrwkb+xcshrpnrb+xcshrknrb
       print list='\n\nTrips by mode:                          Trips      Share',printo=1
       print list='    Drive Alone                    ',MW[M1][J](10.2),'    ',xcshrda(7.4),printo=1
       print list='    HOV2                           ',MW[M2][J](10.2),'    ',xcshrhov2(7.4),printo=1
       print list='    HOV3                           ',MW[M3][J](10.2),'    ',xcshrhov3(7.4),printo=1
       print list='    Walk Bus                       ',MW[M4][J](10.2),'    ',xcshrwkb(7.4),printo=1

       print list='    PNR Bus                        ',MW[M5][J](10.2),'    ',xcshrpnrb(7.4),printo=1
       print list='    KNR Bus                        ',MW[M6][J](10.2),'    ',xcshrknrb(7.4),printo=1

       print list='\n\n\nOutput shares by XCHOICE:                             ', printo=1
       print list='   Top Nest:                                         ', printo=1
       print list='        Auto Share               ',(xcshrda+xcshrhov2+xcshrhov3)(7.4), printo=1
       print list='        Transit Share            ',(xcshrwkb+xcshrpnrb+xcshrknrb)(7.4), printo=1

       print list='\n\n   Auto Nest:                                    ', printo=1
       print list='        Drive Alone Share        ',(xcshrda/(xcshrda+xcshrhov2+xcshrhov3))(7.4), printo=1
       print list='        Shared Ride Share        ',((xcshrhov2+xcshrhov3)/(xcshrda+xcshrhov2+xcshrhov3))(7.4), printo=1
       print list='\n\n   Shared Ride Nest:                             ', printo=1
       print list='        HOV2 Share               ',(xcshrhov2/(xcshrhov2+xcshrhov3))(7.4), printo=1
       print list='        HOV3 Share               ',(xcshrhov3/(xcshrhov2+xcshrhov3))(7.4), printo=1
       print list='\n\n   Transit Nest:                                 ', printo=1
       print list='        Walk Transit Share       ',((xcshrwkb)/(xcshrwkb+xcshrpnrb+xcshrknrb))(7.4), printo=1

       print list='        PNR Transit Share        ',((xcshrpnrb)/(xcshrwkb+xcshrpnrb+xcshrknrb))(7.4), printo=1
       print list='        KNR Transit Share        ',((xcshrknrb)/(xcshrwkb+xcshrpnrb+xcshrknrb))(7.4), printo=1

       print list='\n\n',printo=1

       IF (MARKET=3)
        print list='\n\n\n','END TRACE @PURP@-@PERIOD@','\n',printo=1
        print list='========================================================================================',printo=1
       ENDIF

      ELSE
       print list='No demand (input trips) from this access type and market segment............... ',printo=1
       print list='\n\n',printo=1

      ENDIF

      ENDIF
     ENDJLOOP
    ENDIF

   ENDLOOP ; end market loop created for reporting purposes

; ###############################################################################

ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00Z.S"
; 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\MCMAT00A.PRN" MSG='Mode Choice Reporting'
FILEI MATI[7] = "{SCENARIO_DIR}\output\@PURP@Utility.mat"
FILEO MATO[1] = "{SCENARIO_DIR}\output\@PURP@_{alt}{year}.MAT",
MO=501-509,DEC=12*s,NAME=DA,SR2,SR3,WALKBUS,WALKPRJ,PNRBUS,PNRPRJ,KNRBUS,KNRPRJ;

FILEO MATO[2] = "{SCENARIO_DIR}\output\@PURP@UTIL_{alt}{year}.MAT",
MO=401-407,421-427,441-447, DEC=21*D,
NAME=PerTrip1,AutoTrnTrip1,AutoPrice1,WalkFrac1,WalkShare1,DriveFrac1,DriveShare1,
     PerTrip2,AutoTrnTrip2,AutoPrice2,WalkFrac2,WalkShare2,DriveFrac2,DriveShare2,
     PerTrip3,AutoTrnTrip3,AutoPrice3,WalkFrac3,WalkShare3,DriveFrac3,DriveShare3
FILEO PRINTO[4] = "{SCENARIO_DIR}\output\CBDSHARES.CSV",
append=t
FILEO PRINTO[3] = "{SCENARIO_DIR}\output\MODESHARES.CSV",
append=t
FILEO PRINTO[2] = "{SCENARIO_DIR}\output\ModeSum.TXT",
append=t
FILEO PRINTO[1] = "{SCENARIO_DIR}\output\ModeDebug.TXT",
append=t
FILEI LOOKUPI[1] = "{SCENARIO_DIR}\output\TRN_COEFFICIENTS.DBF"
FILEI ZDATI[1] = "{SCENARIO_DIR}\output\TAZDATA_{alt}{year}.DBF"
FILEI MATI[6] = "{SCENARIO_DIR}\output\expautoutil_tmp.mat"
FILEI MATI[5] = "{SCENARIO_DIR}\output\farexfer_tmp.mat"
FILEI MATI[4] = "{SCENARIO_DIR}\output\@PURP@Trip.MAT"
FILEI MATI[3] = "{SCENARIO_DIR}\output\@PURP@Trip_2CAR.MAT"
FILEI MATI[2] = "{SCENARIO_DIR}\output\@PURP@Trip_1CAR.MAT"
FILEI MATI[1] = "{SCENARIO_DIR}\output\@PURP@Trip_0CAR.MAT"

zonemsg=100

FILLMW MW[1]  =MI.1.1,2,3,4,5,6     ;  0 car
FILLMW MW[21] =MI.2.1,2,3,4,5,6     ;  1 car
FILLMW MW[41] =MI.3.1,2,3,4,5,6     ;  2 car
FILLMW MW[201]=MI.4.1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31   ; 0 car, 1 car, 2 car households
FILLMW MW[251]=MI.6.1,2,3
FILLMW MW[261]=MI.5.1,2,3,4,5,6                        ; fare and xfer matrix for the nine paths
fillmw mw[701]=mi.7.1,2,3,4,5,6                        ; 0 car utils
fillmw mw[721]=mi.7.7,8,9,10,11,12                     ; 1 car utils
fillmw mw[741]=mi.7.13,14,15,16,17,18                  ; 2 car utils

array _tripsDA=3, _tripsHOV2=3, _tripsHOV3=3, _tripsWalkbus=3, _tripsWalkprj=3, _tripsPNRbus=3,
      _tripsPNRprj=3, _tripsKNRbus=3, _tripsKNRprj=3, _SUMtrips=3,
      CBDA=4,TOTACC=4

if (i=FirstZone)
  loop xx=1,3
    _tripsDA[xx]=0
    _tripsHOV2[xx]=0
    _tripsHOV3[xx]=0
    _tripsWalkBus[xx]=0
    _tripsWalkPrj[xx]=0
    _tripsPNRBus[xx]=0
    _tripsPNRPrj[xx]=0
    _tripsKNRBus[xx]=0
    _tripsKNRPrj[xx]=0
    _SUMtrips[xx]=0
  endloop
endif

 ; Total person trips for each market segment
 COMP total = ROWADD(301,201,202,203,204,205,206)    ; Person trips Market Segment 1
 COMP total = ROWADD(302,207,208,209,210,211,212)   ; Person trips Market Segment 2
 COMP total = ROWADD(303,213,214,215,216,217,218)   ; Person trips Market Segment 3

 ; Person trip for assignment
 COMP total = ROWADD(501,1,21,41)             ; DA Person trips
 COMP total = ROWADD(502,2,22,42)             ; HOV2 Person trips
 COMP total = ROWADD(503,3,23,43)             ; HOV3 Person trips
 COMP total = ROWADD(504,4,24,44)             ; walk-bus Person trips
 COMP total = ROWADD(505,5,25,45)             ; walk-prj Person trips
 COMP total = ROWADD(506,6,26,46)             ; pnr-bus Person trips

; ********************************* BEGIN USER BENEFIT (UB) AND MODE CHOICE SUMMARY ********************************************
LOOP MARKET=1,3                    ; UB data generated for each market segment separately

 if (MARKET=1) MARKET_NAME='Zero Car HHs       '
 if (MARKET=2) MARKET_NAME='One Car HHs        '
 if (MARKET=3) MARKET_NAME='Two+ Car HHs       '

 _idx1 = (200 + 6*(MARKET-1))
 _idx2 = 20*(MARKET-1)

 JLOOP
  _tripsDA[MARKET] = _tripsDA[MARKET] + MW[_idx1+1]
  _tripsHOV2[MARKET] = _tripsHOV2[MARKET] + MW[_idx1+2]
  _tripsHOV3[MARKET] = _tripsHOV3[MARKET] + MW[_idx1+3]
  _tripsWalkBus[MARKET] = _tripsWalkBus[MARKET] + MW[_idx1+4]
  _tripsPNRBus[MARKET] = _tripsPNRBus[MARKET] + MW[_idx1+5]
  _tripsKNRBus[MARKET] = _tripsKNRBus[MARKET] + MW[_idx1+6]

  ; For reporting prods/attrs
  if (ZI.1.CBD_DUMMY[i]==1)
    _cbdp1 = _cbdp1 + MW[_idx1+1]
    _cbdp2 = _cbdp2 + MW[_idx1+2]
    _cbdp3 = _cbdp3 + MW[_idx1+3]
    _cbdp4 = _cbdp1 + _cbdp2 + _cbdp3
    _cbdp5 = _cbdp5 + MW[_idx1+4]
    _cbdp6 = _cbdp6 + MW[_idx1+5]
    _cbdp7 = _cbdp7 + MW[_idx1+6]
    _cbdp8= _cbdp5 + _cbdp6 + _cbdp7
    _cbdp9= _cbdp4 + _cbdp8
  elseif (ZI.1.EXU_DUMMY[i]==1)
    _exup1 = _exup1 + MW[_idx1+1]
    _exup2 = _exup2 + MW[_idx1+2]
    _exup3 = _exup3 + MW[_idx1+3]
    _exup4 = _exup1 + _exup2 + _exup3
    _exup5 = _exup5 + MW[_idx1+4]
    _exup6 = _exup6 + MW[_idx1+5]
    _exup7 = _exup7 + MW[_idx1+6]
    _exup8= _exup5 + _exup6 + _exup7
    _exup9= _exup4 + _exup8
  else
    _othp1 = _othp1 + MW[_idx1+1]
    _othp2 = _othp2 + MW[_idx1+2]
    _othp3 = _othp3 + MW[_idx1+3]
    _othp4 = _othp1 + _othp2 + _othp3
    _othp5 = _othp5 + MW[_idx1+4]
    _othp6 = _othp6 + MW[_idx1+5]
    _othp7 = _othp7 + MW[_idx1+6]
    _othp8= _othp5 + _othp6 + _othp7
    _othp9= _othp4 + _othp8
  endif
  if (ZI.1.CBD_DUMMY[j]==1)
    _cbda1 = _cbda1 + MW[_idx1+1]
    _cbda2 = _cbda2 + MW[_idx1+2]
    _cbda3 = _cbda3 + MW[_idx1+3]
    _cbda4 = _cbda1 + _cbda2 + _cbda3
    _cbda5 = _cbda5 + MW[_idx1+4]
    _cbda6 = _cbda6 + MW[_idx1+5]
    _cbda7 = _cbda7 + MW[_idx1+6]
    _cbda8= _cbda5 + _cbda6 + _cbda7
    _cbda9= _cbda4 + _cbda8
  elseif (ZI.1.EXU_DUMMY[j]==1)
    _exua1 = _exua1 + MW[_idx1+1]
    _exua2 = _exua2 + MW[_idx1+2]
    _exua3 = _exua3 + MW[_idx1+3]
    _exua4 = _exua1 + _exua2 + _exua3
    _exua5 = _exua5 + MW[_idx1+4]
    _exua6 = _exua6 + MW[_idx1+5]
    _exua7 = _exua7 + MW[_idx1+6]
    _exua8= _exua5 + _exua6 + _exua7
    _exua9= _exua4 + _exua8
  else
    _otha1 = _otha1 + MW[_idx1+1]
    _otha2 = _otha2 + MW[_idx1+2]
    _otha3 = _otha3 + MW[_idx1+3]
    _otha4 = _otha1 + _otha2 + _otha3
    _otha5 = _otha5 + MW[_idx1+4]
    _otha6 = _otha6 + MW[_idx1+5]
    _otha7 = _otha7 + MW[_idx1+6]
    _otha8= _otha5 + _otha6 + _otha7
    _otha9= _otha4 + _otha8
  endif
/*
  ; For reporting fare revenue
  _frev1 = _frev1 + ((MI.5.FareWB * MW[_idx1+4]) / 100.0)
  _frev2 = _frev2 + ((MI.5.FarepB * MW[_idx1+5]) / 100.0)
  _frev3 = _frev3 + ((MI.5.FarekB * MW[_idx1+6]) / 100.0)
  _frev7 = _frev1 + _frev2 + _frev3
  _avef1 = 0

*/
 if (MARKET=3)
  ; For reporting transfers
  if (MW[264]==0)                          ;walk bus
    _xfer0_1 = _xfer0_1 + MW[504]
  elseif (MW[267]==1)
    _xfer1_1 = _xfer1_1 + MW[504]
  elseif (MW[267]==2)
    _xfer2_1 = _xfer2_1 + MW[504]
  else
    _xfer3_1 = _xfer3_1+ MW[504]
  endif

  if (MW[265]==0)                          ;pnr bus
    _xfer0_3 = _xfer0_3 + MW[505]
  elseif (MW[269]==1)
    _xfer1_3 = _xfer1_3 + MW[505]
  elseif (MW[269]==2)
    _xfer2_3 = _xfer2_3 + MW[505]
  else
    _xfer3_3 = _xfer3_3 + MW[505]
  endif

  if (MW[266]==0)                          ;knr bus
    _xfer0_5 = _xfer0_5 + MW[506]
  elseif (MW[271]==1)
    _xfer1_5 = _xfer1_5 + MW[506]
  elseif (MW[271]==2)
    _xfer2_5 = _xfer2_5 + MW[506]
  else
    _xfer3_5 = _xfer3_5 + MW[506]
  endif

  _xfer0_10 = _xfer0_1 + _xfer0_3 + _xfer0_5
  _xfer1_10 = _xfer1_1 + _xfer1_3 + _xfer1_5
  _xfer2_10 = _xfer2_1 + _xfer2_3 + _xfer2_5
  _xfer3_10 = _xfer3_1 + _xfer3_3 + _xfer3_5

 endif

  ; User benefit calculations are done for each market segment
  ; Determine can walk & must drive percentages for matrix for Summit for each market segment (1 for auto0, 2 for auto1 and 3 for auto2)
  ; person trips
  IF ({RunUB}==1)

  T1=401+_idx2, T2=402+_idx2, T3=403+_idx2, T4=404+_idx2, T5=405+_idx2, T6=406+_idx2, T7=407+_idx2

  if (MW[300+MARKET]>0)
    MW[T1] = MW[300+MARKET]                                                                     ; person trips in market segment
    MW[T2] = MW[T1]                                                                             ; motorized trips in market segment
    MW[T3] = MW[250+MARKET]

if(mw[704+_idx2]>-9999.99)
    mw[t4]=0.99
    mw[t5]= (MW[204+(market-1)*6] + MW[205+(market-1)*6] +MW[206+(market-1)*6]) /mw[t1]
    mw[t6]=0.00
    mw[t7]=0.00
elseif((mw[705+_idx2]>-9999.99)||(mw[706+_idx2]>-9999.99))
    mw[t4]=0.00
    mw[t5]=0.00
    mw[t6]=0.99
    mw[t7]=(MW[205+(market-1)*6] +MW[206+(market-1)*6]) /mw[t1]
else
    mw[t4]=0.00
    mw[t5]=0.00
    mw[t6]=0.00
    mw[t7]=0.00
endif

  else
    MW[T1] = 0.00
    MW[T2] = 0.00
    MW[T3] = 0.00
    MW[T4] = 0.00
    MW[T5] = 0.00
    MW[T6] = 0.00
    MW[T7] = 0.00
  endif

 if (I={SelOrigin} && J={SelDest})
    print list="*UB Rec* ",i(5.0),j(5.0),MARKET(5.0),mw[T1](10.1),mw[T2](10.1),mw[T3](10.7),mw[T4](10.5),mw[T5](10.5),mw[T6](10.5),mw[T7](10.5)
  endif

  ENDIF

  ; MODE CHOICE SUMMARY
  IF (i=_zones & j=_zones)

   _SUMtrips[MARKET] = _tripsDA[MARKET] + _tripsHOV2[MARKET] + _tripsHOV3[MARKET] +
                       _tripsWalkBus[MARKET] +
                       _tripsPNRBus[MARKET] +
                       _tripsKNRBus[MARKET]
   ; Total trips by purpose
   if (_SUMtrips[MARKET]=0) _SUMtrips[MARKET]=1

   if (MARKET==1)
     PRINT FORM=8.OC, LIST='\f\n\n@PURP@ - @PERIOD@ MODE XCHOICE RESULTS',
     '\n**************************************************************',
     '\n','                 ','     Total','     Drive','       One','      Two+','     Total','      Walk','       PNR','       KNR','     Total',
     '\n','MARKET SEGMENT   ','    Person','     Alone','       Pax','       Pax','      Auto','       Bus','       Bus','       Bus','   Transit',
     '\n','--------------   ','   -------','    ------','    ------','    ------','    ------','    ------','   -------','    ------','   -------',PRINTO=2
   endif

   _aocp = _aocp + (_tripsDA[MARKET]+_tripsHOV2[MARKET]+_tripsHOV3[MARKET])
   _aocv = _aocv + (_tripsDA[MARKET]+_tripsHOV2[MARKET]/2.0+_tripsHOV3[MARKET]/@OCC3@)
   _trntemp = (_tripsWalkBus[MARKET]+_tripsPNRBus[MARKET]+_tripsKNRBus[MARKET])

   PRINT LIST='\n',MARKET_NAME(17),_SUMtrips[MARKET](10.1),
                   _tripsDA[MARKET](10.1),_tripsHOV2[MARKET](10.1),_tripsHOV3[MARKET](10.1),_tripsDA[MARKET]+_tripsHOV2[MARKET]+_tripsHOV3[MARKET](10.1),
                   _tripsWalkBus[MARKET](10.1),
                   _tripsPNRBus[MARKET](10.1),
                   _tripsKNRBus[MARKET](10.1),_trntemp(10.1),PRINTO=2

   if (MARKET==3)
 _tripsDAtot = _tripsDA[1] +_tripsDA[2]+_tripsDA[3]
 _tripsHOV2tot = _tripsHOV2[1] + _tripsHOV2[2]+_tripsHOV2[3]
 _tripsHOV3tot = _tripsHOV3[1] + _tripsHOV3[2]+_tripsHOV3[3]
 _tripsWalkBustot = _tripsWalkBus[1] + _tripsWalkBus[2]+_tripsWalkBus[3]
 _tripsPNRBustot = _tripsPNRBus[1] + _tripsPNRBus[2]+_tripsPNRBus[3]
 _tripsKNRBustot = _tripsKNRBus[1] + _tripsKNRBus[2]+_tripsKNRBus[3]
_total=_tripsDAtot + _tripsHOV2tot+ _tripsHOV3tot + _tripsWalkBustot +_tripsPNRBustot+ _tripsKNRBustot
_autototal=_tripsDAtot + _tripsHOV2tot+ _tripsHOV3tot
_transittotal=_tripsWalkBustot +_tripsPNRBustot+ _tripsKNRBustot

     PRINT LIST =  '\n\n',
                   'TOTAL            ',_total(10.1),_tripsDAtot(10.1),_tripsHOV2tot(10.1), _tripsHOV3tot(10.1),_autototal(10.1),
                     _tripsWalkBustot(10.1),_tripsPNRBustot(10.1),_tripsKNRBustot(10.1),_transittotal(10.1),'\n\n',PRINTO=2

     PRINT LIST =  '\n\n',
                   'Productions:     ','\n',
                   'CBD              ',_cbdp9(10.1),_cbdp1(10.1),_cbdp2(10.1),_cbdp3(10.1),_cbdp4(10.1),_cbdp5(10.1),_cbdp6(10.1),
                   _cbdp7(10.1),_cbdp8(10.1),'\n',
                   'Exurban          ',_exup9(10.1),_exup1(10.1),_exup2(10.1),_exup3(10.1),_exup4(10.1),_exup5(10.1),_exup6(10.1),
                   _exup7(10.1),_exup8(10.1),'\n',
                   'Other            ',_othp9(10.1),_othp1(10.1),_othp2(10.1),_othp3(10.1),_othp4(10.1),_othp5(10.1),_othp6(10.1),
                   _othp7(10.1),_othp8(10.1),PRINTO=2
     PRINT LIST =  '\n\n',
                   'Attractions:     ','\n',
                   'CBD              ',_cbda9(10.1),_cbda1(10.1),_cbda2(10.1),_cbda3(10.1),_cbda4(10.1),_cbda5(10.1),_cbda6(10.1),
                   _cbda7(10.1),_cbda8(10.1),'\n',
                   'Exurban          ',_exua9(10.1),_exua1(10.1),_exua2(10.1),_exua3(10.1),_exua4(10.1),_exua5(10.1),_exua6(10.1),
                   _exua7(10.1),_exua8(10.1),'\n',
                   'Other            ',_otha9(10.1),_otha1(10.1),_otha2(10.1),_otha3(10.1),_otha4(10.1),_otha5(10.1),_otha6(10.1),
                   _otha7(10.1),_otha8(10.1),PRINTO=2

     CBDA[1] = CBDA[1] + _cbda5
     CBDA[2] = CBDA[2] + _cbda6
     CBDA[3] = CBDA[3] + _cbda7

     TOTACC[1] = TOTACC[1] + _tripsWalkBustot
     TOTACC[2] = TOTACC[2] + _tripsPNRBustot
     TOTACC[3] = TOTACC[3] + _tripsKNRBustot

/*
     PRINT LIST =  '\n\n',
                   'Fare Revenue ($) '," "(50),_frev1(10.2),_frev2(10.2),_frev3(10.2),_frev4(10.2),_frev5(10.2),
                                               _frev6(10.2),_frev7(10.2)," "(10),'\n',
                   'Average Fare ($) '," "(50),_avef1(10.2),_avef2(10.2),_avef3(10.2),_avef4(10.2),_avef5(10.2),
                                               _avef6(10.2),_avef7(10.2)," "(10),PRINTO=2

     PRINT LIST =  '\n\n',
                   'Transfers:       ','\n',
                   'NONE             '," "(50),_xfer0_1(10.1),_xfer0_3(10.1),_xfer0_5(10.1),_xfer0_10(10.1),'\n',
                   'ONE              '," "(50),_xfer1_1(10.1),_xfer1_3(10.1),_xfer1_5(10.1),_xfer1_10(10.1),'\n',
                   'TWO              '," "(50),_xfer2_1(10.1),_xfer2_3(10.1),_xfer2_5(10.2),_xfer2_10(10.1),'\n',
                   'THREE +          '," "(50),_xfer3_1(10.1),_xfer3_3(10.1),_xfer3_5(10.1),_xfer3_10(10.1),PRINTO=2
*/
     PRINT LIST =  '\n\n',
                   'Average Auto Occupancy      =',_aocp/_aocv(8.3),
                   '\n\n**************************************************************',PRINTO=2
   endif
abcd1=0
abcd2=0
   ; OUTPUT MODEL SHARES TO A CSV FILE FOR CALIBRATION

    PRINT CSV=T, LIST= (18*(@PURPNO@-2)+6*(MARKET-1)+1),@PURPOSE@,MARKET,_tripsDA[MARKET]/_SUMtrips[MARKET](12.6), PRINTO=3
    PRINT CSV=T, LIST= (18*(@PURPNO@-2)+6*(MARKET-1)+2),@PURPOSE@,MARKET,_tripsHOV2[MARKET]/_SUMtrips[MARKET](12.6), PRINTO=3
    PRINT CSV=T, LIST= (18*(@PURPNO@-2)+6*(MARKET-1)+3),@PURPOSE@,MARKET,_tripsHOV3[MARKET]/_SUMtrips[MARKET](12.6), PRINTO=3
    PRINT CSV=T, LIST= (18*(@PURPNO@-2)+6*(MARKET-1)+4),@PURPOSE@,MARKET,_tripsWalkBus[MARKET]/_SUMtrips[MARKET](12.6), PRINTO=3
    PRINT CSV=T, LIST= (18*(@PURPNO@-2)+6*(MARKET-1)+5),@PURPOSE@,MARKET,_tripsPNRBus[MARKET]/_SUMtrips[MARKET](12.6), PRINTO=3
    PRINT CSV=T, LIST= (18*(@PURPNO@-2)+6*(MARKET-1)+6),@PURPOSE@,MARKET,_tripsKNRBus[MARKET]/_SUMtrips[MARKET](12.6), PRINTO=3

  ENDIF

 ENDJLOOP

ENDLOOP

IF (I == _ZONES)

if(totacc[1]=0)
  PRINT CSV=T, LIST=((@PURPNO@-2)*3+1),(@PURPNO@-1),abcd1(10.6), printo=4
else
  PRINT CSV=T, LIST=((@PURPNO@-2)*3+1),(@PURPNO@-1),(cbda[1]/totacc[1])(10.6), printo=4
endif

if(totacc[2]=0)
  PRINT CSV=T, LIST=((@PURPNO@-2)*3+2),(@PURPNO@-1),abcd1(10.6), printo=4
else
  PRINT CSV=T, LIST=((@PURPNO@-2)*3+2),(@PURPNO@-1),(cbda[2]/totacc[2])(10.6), printo=4
endif

if(totacc[3]=0)
  PRINT CSV=T, LIST=((@PURPNO@-2)*3+3),(@PURPNO@-1),abcd1(10.6), printo=4
else
  PRINT CSV=T, LIST=((@PURPNO@-2)*3+3),(@PURPNO@-1),(cbda[3]/totacc[3])(10.6), printo=4
endif

ENDIF
; ********************************* END USER BENEFIT AND MODE CHOICE SUMMARY ********************************************
ENDRUN


; PILOT Script


if ({RunUB,CheckBox,"Dummy variable to run (=1) or not run (=0) user benefits & generate user benefit files",0}==1)

; End of PILOT Script

; Script for program TPP2UB in file "D:\JOB\D3\Model\C\APPLICATIONS\MCTUB00B.S"
; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=TPP2UB PRNFILE="{SCENARIO_DIR}\output\MCMAT00F.PRN" MSG='Convert Voyager matrices to Summit Datafile'
FILEI MATI = "{SCENARIO_DIR}\output\@PURP@UTIL_{alt}{year}.MAT"
FILEI VARI = "{SCENARIO_DIR}\output\@PURP@HEAD.VAR"
FILEO MATO = "{SCENARIO_DIR}\output\@PURP@UTIL.UB"


ENDRUN


; PILOT Script



else
endif          ; RunUB flag
; End of PILOT Script

ENDLOOP

; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00G.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\MCMAT00C.PRN" MSG='Convert modeshares and CBD shares to DBF'
FILEI LOOKUPI[2] = "{SCENARIO_DIR}\output\CBDSHARES.CSV"
FILEI LOOKUPI[1] = "{SCENARIO_DIR}\output\MODESHARES.CSV"
FILEO RECO[2] = "{SCENARIO_DIR}\output\CBDSHARES.DBF",
FORM=10.6,FIELDS=N2,HBW1,HBNW1,NHB1
FILEO RECO[1] = "{SCENARIO_DIR}\output\MODESHARES.DBF",
FORM=10.6,FIELDS=N1,HBW,HBNW,NHB

zones=1
array Pshr=108, Pcbdshr=12

; LOOKUP for previuos run shares
LOOKUP NAME=SHARES, 
       LOOKUP[1]=1, 
       RESULT=4,
       FAIL=0,0,0,
       LIST=Y,
       INTERPOLATE=N, 
       LOOKUPI=1

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

; records 1 through 6 are for 6 choices for HBW purpose and 0 car households, next 6 is for HBW purpose and 1 car household....
LOOP _ww=1,54
 Pshr[_ww] = SHARES(1,_ww)
ENDLOOP

; 3 cbd targets (for walk, pnr,knr ) and 3 purposes
LOOP _ww=1,9
 Pcbdshr[_ww] = CBDSHARES(1,_ww)
ENDLOOP

; print REVISED modal constants
LOOP _ww=1,18
 _idx=_idx+1
 RO.N1=_ww
 RO.HBW=Pshr[_idx]
 RO.HBNW=Pshr[_idx+18]
 RO.NHB=Pshr[_idx+36]
 WRITE RECO=1
ENDLOOP

LOOP _xx=1,3
 RO.N2=_xx
 RO.HBW1=Pcbdshr[_xx]
 RO.HBNW1=Pcbdshr[_xx+3]
 RO.NHB1=Pcbdshr[_xx+7]
 WRITE RECO=2
ENDLOOP

ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00D.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\MCMAT00H.PRN" MSG='Combine trip tables for assignment'
FILEO MATO[2] = "{SCENARIO_DIR}\output\TRANSITTAB_{alt}{year}.MAT",
 MO=131-133,141-143, DEC=18*s,
 NAME=PKWlkBus,PKWlkPrj,PKPNRBus,PKPNRPrj,PKKNRBus,PKKNRPrj,
      OPWlkBus,OPWlkPrj,OPPNRBus,OPPNRPrj,OPKNRBus,OPKNRPrj
FILEO MATO[1] = "D:\JOB\D3\Model\C\BASE\OUTPUT\HWYTTAB_TEMP_{ALT}{YEAR}.MAT",
MO=121-124,NAME=HBW,HBO,NHB,TOTAL,DEC=6*S 
;MO=121-126, NAME=TRKIEII,SOVIEII,HOVIEII,TRKEE,SOVEE,HOVEE, DEC=6*s

FILEI MATI[3] = "{SCENARIO_DIR}\output\NHB_{alt}{year}.MAT"
FILEI MATI[2] = "{SCENARIO_DIR}\output\HBO_{alt}{year}.MAT"
FILEI MATI[1] = "{SCENARIO_DIR}\output\HBW_{alt}{year}.MAT"

PAR ZONEMSG=100
DistributeINTRASTEP ProcessID="FSUTMSDist", ProcessList=1-3

;DA
MW[50]=MI.1.1+MI.2.1+MI.3.1

;CARPOOL
MW[60]=(MI.1.2+MI.2.2+MI.3.2)*0.5+
        MI.1.3/{oc3vhbw}+
        MI.2.3/{oc3vhbnw}+
        MI.3.3/{oc3vnhb}

MW[124]=MW[50]+MW[60]
MW[121]=MI.1.1+MI.1.2*0.5+MI.1.3/{oc3vhbw}
MW[122]=MI.2.1+MI.2.2*0.5+MI.2.3/{oc3vhbnw}
MW[123]=MI.3.1+MI.3.2*0.5+MI.3.3/{oc3vnhb}


;FILLMW MW[1]  = MI.1.1,2,3,4,5,6   ; - HBW trips
;FILLMW MW[21] = MI.2.1,2,3,4,5,6  ; - HBNW trips
;FILLMW MW[41] = MI.3.1,2,3,4,5,6   ; - NHB trips

; PEAK TRANSIT
MW[131]=MI.1.4
MW[132]=MI.1.6
MW[133]=MI.1.8

; OFF-PEAK TRANSIT
MW[141]=MI.2.4 + MI.3.4
MW[142]=MI.2.6 + MI.3.6
MW[143]=MI.2.8 + MI.3.8

;MW[121]=MW[1]+MW[2]+MW[3]
;MW[122]=MW[21]+MW[22]+MW[23]
;MW[123]=MW[41]+MW[42]+MW[43]
;MW[124]=MW[121]+MW[122]+MW[123]


ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00E.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\MCMAT00A.PRN"
FILEI MATI[3] = "{Scenario_Dir}\OUTPUT\PSNTRIPS_{ALT}{YEAR}.MAT"
FILEO MATO[1] = "D:\JOB\D3\Model\C\BASE\OUTPUT\TEMP_HWYTTAB_{ALT}{YEAR}.MAT",
MO=1-7, NAME=HBW,HBO,NHB,TAX,IE,EE,TOTAL
FILEI MATI[2] = "{Scenario_Dir}\OUTPUT\EETRIPS_{ALT}{Year}.MAT"
FILEI MATI[1] = "D:\JOB\D3\Model\C\BASE\OUTPUT\HWYTTAB_TEMP_{ALT}{YEAR}.MAT"

;MW[11]=MI.1.4+MI.2.1+MI.3.4+MI.3.5
;mw[22]=MI.1.1.T+MI.1.2.t+MI.1.3.t+MI.2.1.t*0.5+mi.3.4.t+mi.3.5.t
MW[1]=MI.1.1*0.5
MW[2]=MI.1.2*0.5
MW[3]=MI.1.3*0.5
MW[4]=MI.3.4*0.5
;MW[5]=(MI.3.5+MI.3.5.T)*0.5*0.5
;MW[6]=MI.2.1*0.5
;MW[7]=MW[1]+MW[2]+MW[3]+MW[4]+MW[5]+MW[6]
;mw[1]=MW[11]*0.5

;if (i=2207) || (I=2208)
;mw[5]=(mi.3.1+mi.3.2)*0.5
;elseIF(I=2208)
;mw[5]=(mi.3.1+mi.3.2)*0.5
;ELSE
MW[5]=(MI.3.5+MI.3.5.T)*0.5*0.5
;endif

MW[6]=MI.2.1*0.5
MW[7]=MW[1]+MW[2]+MW[3]+MW[4]+MW[5]+MW[6]

;FILLMW MW[11]=MI.1.1,2,3,4,5             ;11/12/13/14/15: DA,SR2,SR3P,TRKS,SR2P   
;FILLMW MW[21]=MI.1.1.T,2.T,3.T,4.T,5.T   ;21/22/23/24/25: DA,SR2,SR3P,TRKS,SR2P-Transposed   


;Balanced Vehicle trips & Trucks are in PCE
;MW[1]= (MW[11]+MW[21])*0.5         ;24H - DA
;MW[2]= (MW[12]+MW[22])*0.5         ;24H - SR2
;MW[3]= (MW[13]+MW[23])*0.5         ;24H - SR3P
;MW[4]= (MW[14]+MW[24])*0.5         ;24H - TRKS
;MW[5]= (MW[15]+MW[25])*0.5         ;24H - SR2P




ENDRUN


; Script for program MATRIX in file "D:\JOB\D3\Model\C\APPLICATIONS\MCMAT00K.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\MCMAT00B.PRN"
FILEO MATO[1] = "{Scenario_Dir}\OUTPUT\HWYTTAB_{ALT}{Year}.MAT",
MO=1, NAME=TRIPS
FILEI MATI[1] = "D:\JOB\D3\Model\C\BASE\OUTPUT\TEMP_HWYTTAB_{ALT}{YEAR}.MAT"


PAR ZONEMSG=100

MW[1]=MI.1.7+MI.1.7.T



; 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


; PILOT Script
; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.


*if exist {SCENARIO_DIR}\output\new_trncoeff.csv del {SCENARIO_DIR}\output\new_trncoeff.csv
*if exist {SCENARIO_DIR}\output\new_mcconsts.csv del {SCENARIO_DIR}\output\new_mcconsts.csv.csv
; End of PILOT Script

