FUNCTION Calc_WO_Stats(RDSNos)
#pragma precomp SRP_PreCompiler

$INSERT RDS_EQU
$INSERT RDS_LAYER_EQUATES
$INSERT RDS_TEST_EQUATES

* 	Must pass keys in the same work order and they must be delimited by @VM

IF RDSNos = '' THEN RETURN

RdsCnt = COUNT(RDSNos,@VM) + (RDSNos NE '')

WOStats = ''

* Get Readings from RDS_TEST records for each RDS

MetThickAvgs = ''
MetResRHOAvgs = ''

ThickAvgLow = ''
ThickAvgHigh = ''
ThickAvgCount = 0
ThickAvgSum	= ''

ResRHOAvgLow = ''
ResRHOAvgHigh = ''
ResRHOAvgCount = 0
ResRHOAvgSum = ''



FOR I = 1 to RdsCnt

	RDSNo = RDSNos<1,I>
	
	FirstLayerKey	= RDSNo:'*L1'
	TestKeys		= XLATE('RDS_LAYER',FirstLayerKey,RDS_LAYER_RDS_TEST_KEYS$,'X')
	
	TestKey			= TestKeys<1,1>								;* There may be multiple keys for each Epi Pro reactor zone - Take just the first for now
	MetrologyRec	= XLATE('RDS_TEST',TestKey,'','X')
	
	IF I = 1 THEN
		
		SpecThickMin    = MetrologyRec<RDS_TEST_SPEC_THICK_MIN$>
		SpecThickMax    = MetrologyRec<RDS_TEST_SPEC_THICK_MAX$>
		SpecThickTarget = MetrologyRec<RDS_TEST_SPEC_THICK_TARGET$>
		
		SpecResUnits	= MetrologyRec<RDS_TEST_SPEC_RES_UNITS$>
		
		SpecResMin      = MetrologyRec<RDS_TEST_SPEC_RES_MIN$>
		SpecResMax      = MetrologyRec<RDS_TEST_SPEC_RES_MAX$>
		SpecResTarget   = MetrologyRec<RDS_TEST_SPEC_RES_TARGET$>
		
	END
	
	MetThickAvg = MetrologyRec<RDS_TEST_THICK_AVG$>
	
	IF MetThickAvg <> "" THEN
	
		MetThickAvgs<I> = MetThickAvg
		
		ThickAvgCount	+= 1
		ThickAvgSum		+= MetThickAvg
		
		IF ThickAvgLow = ''		THEN ThickAvgLow = MetThickAvg
		IF ThickAvgHigh	= ''	THEN ThickAvgHigh = MetThickAvg
		
		IF MetThickAvg < ThickAvgLow	THEN ThickAvgLow = MetThickAvg
		IF MetThickAvg > ThickAvgHigh	THEN ThickAvgHigh = MetThickAvg
	END
	
	IF SpecResUnits = 'Ãª/Ãœ' THEN
		SpecResLabel = 'Sheet Rho'		
		MetResRHOAvg = MetrologyRec<RDS_TEST_SHEETRHO_AVG$>
	END ELSE
		SpecResLabel = 'Resistivity'
		MetResRHOAvg = MetrologyRec<RDS_TEST_RES_AVG$>
	END
	
	MetResRHOAvgs<I> = MetResRhoAvg
	
    IF MetResRHOAvg <> ""  THEN
        ResRHOAvgCount	+= 1
        ResRHOAvgSum	+= MetResRHOAvg
        
        IF ResRHOAvgLow = ''	THEN ResRHOAvgLow = MetResRHOAvg
        IF ResRHOAvgHigh = ''	THEN ResRHOAvgHigh = MetResRHOAvg
        
        IF MetResRHOAvg < ResRHOAvgLow	THEN ResRHOAvgLow = MetResRHOAvg
        IF MetResRHOAvg > ResRHOAvgHigh	THEN ResRHOAvgHigh = MetResRHOAvg
    END
    
NEXT I

//DPC - 3/11/2020 - hack to account for formatting/conversion issue in Thick Vals from RDS_Test
IF ThickAvgCount > 0 THEN
	StatThickAvg  = (ThickAvgSum/ThickAvgCount)*10
	WOStats<1,1> = OCONV( StatThickAvg, 'MD3' )
END ELSE
	StatThickAvg = 0
END


* * *   Standard Deviation   * * * *

SumDiffSq = 0
ThickMean = ''
ThickStDev = 0

IF ThickAvgSum and ThickAvgCount THEN
	ThickMean = ThickAvgSum/ThickAvgCount
END ELSE
	ThickMean = 0
END

FOR I = 1 TO RdsCnt
	MetThickAvg = MetThickAvgs<I>
	IF MetThickAvg <> "" THEN
		SumDiffSq += ( MetThickAvg - ThickMean ) * ( MetThickAvg - ThickMean )
	END  
NEXT I

IF SumDiffSq AND ThickAvgCount THEN
	ThickStDev = (SQRT( SumDiffSq/(ThickAvgCount-1) )) * 10
	WOStats<1,3> = OCONV( ThickStDev, 'MD3' )
END

*  Minimum and Maximum

IF ThickAvgHigh <> 0	THEN WOStats<1,5> = OCONV( ThickAvgHigh, 'MD2' ) ELSE WOStats<1,5> = ''
IF ThickAvgLow <> 0		THEN WOStats<1,7> = OCONV( ThickAvgLow, 'MD2') ELSE WOStats<1,7> = ''

* Uniformity

IF ThickAvgHigh and ThickAvgLow THEN
    WOStats<1,9> = OCONV(((ThickAvgHigh - ThickAvgLow)/(ThickAvgHigh + ThickAvgLow))*10000, 'MD2' )
END

ThickCp   = 0
TThickCPK = 0

* Thickness Cp

IF SpecThickMin and SpecThickMax and ThickStdev THEN
    ThickCp = ((SpecThickMax-SpecThickMin)/(6*ThickStdev))*1000
    WOStats<1,11> = OCONV( ThickCp, 'MD3' )
END

* Thickness CpK

IF ThickCp AND (ABS(SpecThickTarget-StatThickAvg)) AND (SpecThickMax-SpecThickMin) THEN
    TThickCPK = ThickCp*(1-((ABS(SpecThickTarget-StatThickAvg))/((SpecThickMax-SpecThickMin)/2)))
    WOStats<1,13> = OCONV( TThickCPK, 'MD3' )
END

* Sigma Capabilities

WOStats<1,15> = OCONV( ThickCP*3, 'MD3' )    ;* WOStats<1,11>*3

* Sigma Level

WOStats<1,17> = OCONV( TThickCPK*3, 'MD3' )  ;* WOStats<1,13>*3


* * * * * * *
* Resistivity Calculations
* * * * * * * 

* * *   Average   * * *

IF ResRHOAvgCount THEN
    StatResRHOAvg = ResRHOAvgSum/ResRHOAvgCount 
    IF SpecResUnits = 'Ãª/Ãœ' THEN
		WOStats<1,2> = OCONV( StatResRHOAvg, 'MD3' )
	END ELSE
		WOStats<1,2> = OCONV( StatResRHOAvg, 'MD4' )
	END
END ELSE
	StatResRHOAvg = 0
END


* * *   StDev   * * *

SumDiffSq = 0
ResRHOStDev = 0

IF ResRHOAvgSum AND ResRHOAvgCount THEN
    ResMean = ResRHOAvgSum/ResRHOAvgCount
END ELSE
	ResMean = 0
END

FOR I = 1 to RdsCnt
    MetResRHOAvg = MetResRHOAvgs<I>
    IF MetResRHOAvg <> "" AND MetResRHOAvg <> 0 THEN
        *SumDiffSq += ( MetResRHOAvg - ResMean )**2
        SumDiffSq += ( MetResRHOAvg - ResMean ) * ( MetResRHOAvg - ResMean )
    END
NEXT I

IF SumDiffSq AND ResRHOAvgCount THEN
   ResRHOStDev = SQRT( SumDiffSq/(ResRHOAvgCount-1) )
   WOStats<1,4> = OCONV( ResRHOStDev, 'MD3' )
END

* * *   Minimum and Maximum   * * *

IF ResRHOAvgHigh <> 0 THEN
	IF SpecResUnits = 'Ãª/Ãœ' THEN
		WOStats<1,6> = OCONV( ResRHOAvgHigh, 'MD3' )
	END ELSE
		WOStats<1,6> = OCONV( ResRHOAvgHigh, 'MD4' )
	END
END ELSE
	WOStats<1,6> = ''
END
IF ResRHOAvgLow <> 0 THEN
	IF SpecResUnits = 'Ãª/Ãœ' THEN
		WOStats<1,8> = OCONV( ResRHOAvgLow, 'MD3' )
	END ELSE
		WOStats<1,8> = OCONV( ResRHOAvgLow, 'MD4' )
	END
END ELSE
	WOStats<1,8> = ''
END


* * *   uniformity   * * *

IF ( ResRHOAvgHigh - ResRHOAvgLow ) and ( ResRHOAvgHigh + ResRHOAvgLow ) THEN
    WOStats<1,10>= OCONV(((ResRHOAvgHigh - ResRHOAvgLow)/(ResRHOAvgHigh + ResRHOAvgLow))*10000, 'MD2' )
END

ResRHOCp  = 0
TResCPK   = 0

* * *   Cp   * * *

IF SpecResMin and SpecResMax and ResRHOStdev THEN
    ResRHOCp = ((SpecResMax-SpecResMin)/(6*ResRHOStdev))*1000
    WOStats<1,12> = OCONV( ResRHOCp, 'MD3' )
END   

* * *   CpK   * * *

IF (ABS(SpecResTarget-StatResRHOAvg)) AND ( SpecResMax-SpecResMin ) THEN
    TResCPK = ( ResRHOCp*(1-((ABS(SpecResTarget-StatResRHOAvg))/((SpecResMax-SpecResMin)/2))) )
    IF TResCPK <> 0 THEN
       WOStats<1,14> = OCONV( TResCPK, 'MD3' )
    END ELSE
       WOStats<1,14> = ''
    END 
END

* resistivity sigma capabilities

WOStats<1,16> = OCONV( ResRHOCp*3, 'MD3' )   ;* WOStats<1,12>*3


* thickness and resistivity sigma level

WOStats<1,18> = OCONV( TResCPK*3, 'MD3' )    ;* WOStats<1,14>*3


RETURN WOStats

