; 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
