; 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\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
