; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=MATRIX PRNFILE="X:\Projects\CRTPA\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
