; Do not change filenames or add or remove FILEI/FILEO statements using an editor. Use Cube/Application Manager.
RUN PGM=NETWORK PRNFILE="{Scenario_Dir}\OUTPUT\RPNET00C.PRN" MSG='RMSE'
FILEI LINKI[1] = "{SCENARIO_DIR}\Output\HWYLOAD_{ALT}{Year}.NET"
FILEO PRINTO[1] = "{Scenario_Dir}\OUTPUT\RMSE_{Year}{ALT}.PRN",
 APPEND=T 

; =========================================================
; LINKMERGE PHASE
; =========================================================
PHASE=LINKMERGE

   ft=int((LI.1.FACILITY_TYPE)/10)
  ; at=INT((LI.1.ATYPE)/10)
   ;at=AREA_TYPE
   ;AREA_TYPE=AREA_TYPE*10
   at=int((li.1.area_type)/10)
   LNS=LI.1.NUM_LANES
   
   TOTAL_VOL=ROUND(v1_1) ; directional volume
   COUNT_07 = COUNT   
  ; count=COMB_CNT
  ; IF (FT=7 ) COUNT=0

; initialize arrays and variables
   ARRAY _err=13, _cns=13, _cnt=13, _RGP=13, _volbyft=100, _cntbyft=100
   ARRAY _volbyat=100, _cntbyat=100, _lnkbyft=100, _lnkbyat=100
   _group=(0.0*FACILITY_TYPE)
   _RGP[1]=1, _RGP[2]=5000, _RGP[3]=10000, _RGP[4]=20000, _RGP[5]=30000, _RGP[6]=40000, _RGP[7]=50000,
   _RGP[8]=60000, _RGP[9]=70000, _RGP[10]=80000, _RGP[11]=90000, _RGP[12]=100000, _RGP[13]=500000
   IF (A=1)
     LOOP _iter=1,13
           _err[_iter]=0,_cnt[_iter]=0,_cns[_iter]=0
     ENDLOOP
     LOOP _iter=1,99
           _volbyft[_iter]=0, _cntbyft[_iter]=0, _lnkbyft[_iter]=0
           _volbyat[_iter]=0, _cntbyat[_iter]=0, _lnkbyat[_iter]=0
     ENDLOOP
   ENDIF
;   FNAME='ROADNAME'
; establish some English sounding variables for the output network
   links=1
   lanemiles=lns*distance
; calculate and compartmentalize 
   IF(COUNT>0) VOLCNT=TOTAL_VOL/COUNT, NETDIFF=TOTAL_VOL-COUNT, ABSDIFF=ABS(NETDIFF), ERRORSQ=NETDIFF^2, PCTDIFF=100*NETDIFF/COUNT _group=1
   IF (COUNT>5000) _group=2
   IF (COUNT>10000) _group=3
   IF (COUNT>20000) _group=4
   IF (COUNT>30000) _group=5
   IF (COUNT>40000) _group=6
   IF (COUNT>50000) _group=7
   IF (COUNT>60000) _group=8
   IF (COUNT>70000) _group=9
   IF (COUNT>80000) _group=10
   IF (COUNT>90000) _group=11
   IF (COUNT>100000) _group=12
   IF (COUNT>0) 
   _ERR[_group]=ERRORSQ+_ERR[_group], _CNS[_group]=COUNT+_CNS[_group], _CNT[_group]=_CNT[_group]+1
   _ERR[13]=ERRORSQ+_ERR[13], _CNS[13]=COUNT+_CNS[13], _CNT[13]=_CNT[13]+1
   ENDIF
IF (COUNT>0)  
   _volbyft[ft]=_volbyft[ft]+TOTAL_VOL
   _cntbyft[ft]=_cntbyft[ft]+count
   _lnkbyft[ft]=_lnkbyft[ft]+1
   _volbyat[at]=_volbyat[at]+TOTAL_VOL
   _cntbyat[at]=_cntbyat[at]+count
   _lnkbyat[at]=_lnkbyat[at]+1
   _volbyft[100]=_volbyft[100]+TOTAL_VOL
   _cntbyft[100]=_cntbyft[100]+count
   _lnkbyft[100]=_lnkbyft[100]+1
   _volbyat[100]=_volbyat[100]+TOTAL_VOL
   _cntbyat[100]=_cntbyat[100]+count
   _lnkbyat[100]=_lnkbyat[100]+1
endif
CROSSTAB  VAR= LINKS LANEMILES, form=12.0c,
    row=FT, range=10-99-10,
    col=LNS, range=1-5-1,6-9
IF (COUNT>0)
CROSSTAB  VAR= TOTAL_VOL,COUNT, form=12.0c,
    row=FT, range=10-99-10,
    col=AT, range=10-59-10
ENDIF
;if (count=0) delete

ENDPHASE

; =========================================================
; SUMMARY REPORTING
; =========================================================


PHASE=SUMMARY
; a little loop to write out the Percent Root Mean Square Error

LOOP _iter=1,12
if (_iter=1) _limit='45 - 55'
if (_iter=2) _limit='35 - 45'
if (_iter=3) _limit='27 - 35'
if (_iter=4) _limit='24 - 27'
if (_iter=5) _limit='22 - 24'
if (_iter=6) _limit='20 - 22'
if (_iter=7) _limit='18 - 20'
if (_iter=8) _limit='17 - 18'
if (_iter=9) _limit='16 - 17'
if (_iter=10) _limit='15 - 16'
if (_iter=11) _limit='14 - 15'
if (_iter=12) _limit='LT 14  '

  if (_iter=1) print list="*************************** ROOT MEAN SQUARE ERROR SUMMARY *****************************", printo=1
  if (_cnt[_iter]>0) print,
 list="  Percent RMSE for Volume Group ",_iter(2.0c)," ",_RGP[_iter](7.0c),"-",_RGP[_iter+1](7.0c),": ",sqrt(_err[_iter]/(_cnt[_iter]-1))/(_cns[_iter]/_cnt[_iter])*100(5.1),"%", " Allowable RMSE = ", _limit,"%    N=",_cnt[_iter](5.0CL), PRINTO=1



;ASR
;LOOP _iter=1,12
;  if (_iter=1) print list="*************************** ROOT MEAN SQUARE ERROR SUMMARY *****************************", printo=1
;  if (_cnt[_iter]>0) print,
; list="  Percent RMSE for Volume Group ",_iter(2.0c)," ",_RGP[_iter](7.0c),"-",_RGP[_iter+1](7.0c),": ",sqrt(_err[_iter]/_cnt[_iter])/(_cns[_iter]/_cnt[_iter])*100(5.1),"%,                      N=",_cnt[_iter](5.0CL), PRINTO=1

ENDLOOP
;
;*echo Mode Choice Done = %date%   %time%   >>{SCENARIO_DIR}\output\runtime.prn

;print list="Scenario  ",{ALT},"   Done by   ",%date%,%time%,printo=1 

if (_iter=13) _limit='32 - 39'
 list="  Percent RMSE for Volume Group ",_iter(2.0c)," ",_RGP[1](7.0c),"-",_RGP[_iter](7.0c),": ",sqrt(_err[_iter]/(_cnt[_iter]-1))/(_cns[_iter]/_cnt[_iter])*100(5.1),"%", " Allowable RMSE = ", _limit,"%    N=",,_cnt[_iter](5.0CL), PRINTO=1

;_iter=13
; list="  Percent RMSE for Volume Group ",_iter(2.0c)," ",_RGP[1](7.0c),"-",_RGP[_iter](7.0c),": ",sqrt(_err[_iter]/_cnt[_iter])/(_cns[_iter]/_cnt[_iter])*100(5.1),"%,                      N=",_cnt[_iter](5.0CL), PRINTO=1

; one for Vol/Cnt by FT 
_iter=0
LOOP _iter=1,100
  if (_iter=1) print list="\n","\n ********************** VOLUME AND COUNT SUMMARY BY FACILITY TYPE **********************", PRINTO=1
if (_cntbyft[_iter]>0) print,
 list="Facility Type Summary for FT=",_iter(3.0c),
 " VOL=",_volbyft[_iter](11.0c),
 " CNT=",_cntbyft[_iter](11.0c),
 " VOL/CNT=",(_volbyft[_iter]/_cntbyft[_iter])(5.2c),
 "   N=",_lnkbyft[_iter](5.0c), PRINTO=1
ENDLOOP




; one for Vol/Cnt by AT
_iter=0
LOOP _iter=1,100
  if (_iter=1) print list="\n","\n ************************* VOLUME AND COUNT SUMMARY BY AREA TYPE ************************", PRINTO=1
if (_cntbyat[_iter]>0) print,
 list="    Area Type Summary for AT=",_iter(3.0c),
 " VOL=",_volbyat[_iter](11.0c),
 " CNT=",_cntbyat[_iter](11.0c),
 " VOL/CNT=",(_volbyat[_iter]/_cntbyat[_iter])(5.2c),
 "   N=",_lnkbyat[_iter](5.0c), PRINTO=1
ENDLOOP


ENDPHASE

ENDRUN
