Compile function GaNMetrics(ReactRunNo)

#pragma precomp SRP_PreCompiler

$Insert REACT_RUN_EQUATES
$Insert RUN_STAGE_WFR_EQUATES
$Insert GAN_CYCLE_TIME_EQUATES
$Insert WO_LOG_EQUATES
$Insert PROD_VER_EQUATES

Declare Subroutine Obj_Tables

//ReactRunNo = 345711
Recs = ''
RecToWrite = ''



//Add in SQL checks for exist. If exists we'll goto update, otherwise we'll write a new record.

ReactRunRec = Xlate('REACT_RUN',ReactRunNo, '', 'X')
Carr_wfr_ids = ReactRunRec<REACT_RUN_CARR_WFR_ID$>
//Wfr_stgs = ReactRunRec<REACT_RUN_WFR_STAGE$>
Wfr_stgs = ReactRunRec<107>

SubPartNo = XLATE('WO_MAT', ReactRunRec<REACT_RUN_WO_NO$>, 4, 'X')
waferFlags = ReactRunRec<109>
WO_NO = ReactRunRec<REACT_RUN_WO_NO$>
PartNo = XLATE('WO_LOG', WO_NO,WO_LOG_WAFER_TRACK_PART$,'X')
ProdVerNo = XLATE('WO_LOG', WO_NO, WO_LOG_PROD_VER_NO$,'X')
PSNno = Xlate('PROD_VER',ProdVerNo,PROD_VER_PROC_STEP_PSN$,'X')

//Epi Deposition Step
RecToWrite<WO_NO$> =               WO_NO
RecToWrite<RUN_ID$> =           ReactRunRec<REACT_RUN_GAN_RUN_ID$>
RecToWrite<RDS_No$> =           ReactRunNo
RecToWrite<RECIPE$> =           ReactRunRec<REACT_RUN_GAN_RECIPE$>
RecToWrite<Part_NO$> =          PartNo
RecToWrite<PSN_NO$> =           PSNno
RecToWrite<STAGE_NAME$> =       'Epi_Deposition'
RecToWrite<TOOL_CLASS$> =       'Reactor'
RecToWrite<TOOL$> =             ReactRunRec<REACT_RUN_REACTOR$>
RecToWrite<START_DTM$> =        OCONV(ReactRunRec<REACT_RUN_ENTER_DTM$>,'DT4/^HS')
RecToWrite<STOP_DTM$> =         OCONV(ReactRunRec<REACT_RUN_UNLOAD_SIG_DTM$>,'DT4/^HS')
RecToWrite<OPERATOR_START$> =   ReactRunRec<REACT_RUN_ENTER_BY$>
RecToWrite<OPERATOR_STOP$> =    ReactRunRec<REACT_RUN_ENTER_BY$>
//Recs<1,14> = wfrIDfmt
//Recs<1,14> = ReactRunRec<71,a>
                    RecKey = RecToWrite<RDS_No$> : '-' : RecToWrite<STAGE_NAME$>: '-' :  ReactRunRec<REACT_RUN_UNLOAD_SIG_DTM$>

                    Obj_Tables('WriteRec', 'GAN_CYCLE_TIME':@RM:RecKey:@RM:@RM:RecToWrite)

i=1
a=1
b=1;//
c=Count(Wfr_stgs,@VM)


wfrIDfmt = ''

//b to c: Ensures we group tools into a group. Makes it easier for parsing. b is the index, c is the amount of different tools
For b=1 To c
    
    //For each wafer
    For Each Carr_wfr_id in Carr_wfr_ids using @VM
        
        //For each wafer at each stage
        For Each Wfr_stg in Wfr_stgs using @VM
            
            
            
            wfrIDfmt = Carr_wfr_id
            swap '*' with '.' in wfrIDfmt
            Wfr_stg_rec_key = ReactRunNo:'*':Wfr_stg:'*':wfrIDfmt
            Wfr_stg_rec = XLATE('RUN_STAGE_WFR',Wfr_stg_rec_key, '', 'X')
            StopCompare = OConv(Wfr_stg_rec<RUN_STAGE_WFR_SIA_COMP_DTM$,2>,'DT4/^HS') 
            
            //Makes sure that the Wafer Stages are grouped together AND that we don't add an entry for the same tool
            //That also has the same Stop timestamp
            IF Wfr_stg EQ Wfr_stgs<1,b> AND StopCompare NE Recs<i-1,STOP_DTM$> then
                
                
                Dtms = Wfr_stg_rec<RUN_STAGE_WFR_SIA_COMP_DTM$>
                Operators = Wfr_stg_rec<RUN_STAGE_WFR_SIA_COMP_BY$>
                InvActions = Wfr_stg_rec<RUN_STAGE_WFR_SPEC_INV_ACTION$>
                Stg_Start = OCONV(Dtms<1,1>, 'DT4/^HS')
                Stg_Stop = OCONV(Dtms<1,2>, 'DT4/^HS')
                Stg_Start_Op = Operators<1,1>
                Stg_Stop_Op = Operators<1,2>
                
                
                
                //Add to list to track
                IF Stg_Start NE '' Then
                    Recs<i,WO_NO$> =               WO_NO
                    Recs<i,RUN_ID$> =           ReactRunRec<REACT_RUN_GAN_RUN_ID$>
                    Recs<i,RDS_No$> =           ReactRunNo
                    Recs<i,RECIPE$> =           ''
                    Recs<i,Part_NO$> =          PartNo
                    Recs<i,PSN_NO$> =           PSNno
                    Recs<i,STAGE_NAME$> =       Wfr_stg
                    Recs<i,TOOL_CLASS$> =       Wfr_stg_rec<RUN_STAGE_WFR_SPEC_TOOL_CLASS$>
                    Recs<i,TOOL$> =             Wfr_stg_rec<RUN_STAGE_WFR_TOOL_ID$>
                    Recs<i,START_DTM$> =        Stg_Start
                    Recs<i,STOP_DTM$> =         Stg_Stop
                    Recs<i,OPERATOR_START$> =   Stg_Start_Op
                    Recs<i,OPERATOR_STOP$> =    Stg_Stop_Op
                    Recs<i,14> = ReactRunRec<71,a>
                    
                    
                    RecToWrite = ''
                    RecToWrite<WO_NO$> =               WO_NO
                    RecToWrite<RUN_ID$> =           ReactRunRec<REACT_RUN_GAN_RUN_ID$>
                    RecToWrite<RDS_No$> =           ReactRunNo
                    RecToWrite<RECIPE$> =           ''
                    RecToWrite<Part_NO$> =          PartNo
                    RecToWrite<PSN_NO$> =           PSNno
                    RecToWrite<STAGE_NAME$> =       Wfr_stg
                    RecToWrite<TOOL_CLASS$> =       Wfr_stg_rec<RUN_STAGE_WFR_SPEC_TOOL_CLASS$>
                    RecToWrite<TOOL$> =             Wfr_stg_rec<RUN_STAGE_WFR_TOOL_ID$>
                    RecToWrite<START_DTM$> =        Stg_Start
                    RecToWrite<STOP_DTM$> =         Stg_Stop
                    RecToWrite<OPERATOR_START$> =   Stg_Start_Op
                    RecToWrite<OPERATOR_STOP$> =    Stg_Stop_Op
                    RecToWrite<14> = ReactRunRec<71>
                    
                    RecKey = RecToWrite<RDS_No$> : '-' : RecToWrite<STAGE_NAME$>: '-' : Dtms<1,2>

                    Obj_Tables('WriteRec', 'GAN_CYCLE_TIME':@RM:RecKey:@RM:@RM:RecToWrite)
                    
                    
                    i = i + 1
                end
                
            end
            
        Next Wfr_stg
        
        a = a + 1
    Next Carr_wfr_id
Next b
