function GetNewScanDataAvg(CINo, CurrentMax, NCRNo, CurrentAvg)
/*
This function will return the defect data while excluding data for wafers that were NCR'ed out.
*/
#pragma precomp SRP_PreCompiler
$INSERT REACT_RUN_EQUATES

Declare function DCount
Declare function SRP_Math
*Some wafers must get a reclean and thus they're read twice. We'll take the last number and assign it as the max.
CurrentMax = CurrentMax<1,1>
CurrentAvg = CurrentAvg<1,1>
OldMax = CurrentMax

*Pulls in the values of all slots read
Reads =  XLATE('CLEAN_INSP',CINo,86,'X')

slots = 25

*Pulls up the NCR Records and looks for what failed wafers exist in each NCR
FailedSlots = ''
failedCount = 0

    FailedSlots = FailedSlots:XLATE('NCR', NCRNo, 57, 'X')


failedCount = Count(FailedSlots, @VM)


swap @SVM with @VM in Reads
ReadsCount = DCOUNT(Reads,@VM)
NewMax = 0
DefSpecs = XLATE('CLEAN_INSP',CINo, 15, 'X')
DefSpec = DefSpecs<1,1>

NewReads = 0
TotalWafers = 0
    if FailedCount GT 0 Then
        For slot = 1 to slots
                
                IF Reads<1,slot> NE '' then
                        If Reads<1,slot> LE DefSpec then
                        	NewReads += Reads<1,slot>
                        	TotalWafers += 1
                        	If Reads<1,slot> GE NewMax then
                        		NewMax = Reads<1,slot>
                        	end
                        end
                        /*
                            failed = 0
                            Loop
                                For failedWafers = 1 to failedCount
                                    if FailedSlots<1, failedWafers> EQ slot then
					                    failed = 1
					                end else
					                     failed = failed + 0
					                end
					                
					            Next failedWafers
                            Until failed = 1 OR failedWafers GE failedCount
                            Repeat
                            
                            if failed = 0 AND Reads<1,slot> LE DefSpec then
                                IF Reads<1,slot> GE NewMax AND Reads<1,slot> NE OldMax then
					                NewMax = Reads<1,slot>
					                
                                end
                                NewReads += Reads<1,slot>
                                TotalWafers = TotalWafers + 1
					        end
					       */ 
                        
                end

        Next slot
        

		RecalcCount = ReadsCount - FailedCount
		
		//Catches a possible division by zero JRO 5/28/2020
		if (NewReads EQ 0) OR (TotalWafers EQ 0) then
			Ans = 0
		end else
			Ans = SRP_Math("ROUND", NewReads / TotalWafers, 3)
		end
			
    end else
        Ans = OCONV(CurrentAvg,'MD3')
    end

Return Ans
