COMPILE FUNCTION Comm_RDS_Calculator(CtrlEntID,Event,Parm1,Parm2,Parm3,Parm4,Parm5,Parm6)

/*
	Commuter module for RDS_Calculator
	
	/11/2004 - John C. Henry, J.C. Henry & Co., Inc.
*/

DECLARE SUBROUTINE Set_Property, obj_AppWindow

DECLARE FUNCTION Get_Property, Get_Current_Event



$INSERT APPCOLORS


EQU CRLF$	TO \0D0A\
EQU Yes$	TO 1
EQU No$		TO 0


ErrTitle = 'Error in Comm_RDS_Calculator'
ErrorMsg = ''

Result = ''

IF NOT(Assigned(CtrlEntID))	THEN CtrlEntID = ''
IF NOT(Assigned(Event))		THEN Event = ''
IF NOT(Assigned(Parm1))		THEN Parm1 = ''
IF NOT(Assigned(Parm2))		THEN Parm2 = ''
IF NOT(Assigned(Parm3))		THEN Parm3 = ''
IF NOT(Assigned(Parm4))		THEN Parm4 = ''
IF NOT(Assigned(Parm5))		THEN Parm5 = ''
IF NOT(Assigned(Parm6))		THEN Parm6 = ''


Window		= CtrlEntID[1,'F*']
Control		= FIELD(CtrlEntID,'.',2,3)
IF Control	= '' THEN Control = Window

CtrlClass = Get_Property(CtrlEntID,'TYPE')
IF CtrlClass = 'OLECONTROL' THEN ProgID = Get_Property(CtrlEntID, "ORIG_TEXT")

EventType = Get_Current_Event()

IF EventType EQ "OLE" THEN

	Transfer Parm1  TO Event
	Transfer Parm2  TO Parm1
	Transfer Parm3  TO Parm2
	Transfer Parm4  TO Parm3
	Transfer Parm5  TO Parm4
	Transfer Parm6  TO Parm5
END


BEGIN CASE
	CASE Control = Window
		BEGIN CASE
			CASE Event = 'CREATE'		; GOSUB Create
		END CASE
		
	CASE Control = 'THICKNESS_READINGS'
		BEGIN CASE
			CASE Event = 'POSCHANGED'	; GOSUB Recalc
			CASE Event = 'LOSTFOCUS'	; GOSUB ClearSelPos
		END CASE
		
	CASE Control = 'SHEETRHO_READINGS'
		BEGIN CASE
			CASE Event = 'POSCHANGED'	; GOSUB Recalc
			CASE Event = 'LOSTFOCUS'	; GOSUB ClearSelPos
		END CASE
		
	CASE Control = 'OLE_EDT_RDS'
		
		BEGIN CASE
			CASE Event = 'AfterUpdate'		; *GOSUB AfterUpdate.OLE_EDT_RDS
			
		END CASE
		
	CASE Event = 'CLICK'
		BEGIN CASE
			CASE Control = 'CALC_BUTTON'			; GOSUB Calc
			CASE Control = 'OK'						; GOSUB OK
		END CASE
	
	
END CASE
	

RETURN Result



* * * * * * *
Create:
* * * * * * *

obj_AppWindow('Create',@WINDOW)

PosList = ''
FOR I = 80 TO 10 STEP -10
	PosList<-1> = I:'mm'
NEXT I
PosList<-1> = 'Center'
FOR I = 10 TO 80 STEP 10
	PosList<-1> = I:'mm'
NEXT I

Set_Property(@WINDOW:'.POSITION','LIST',PosList)

RETURN


* * * * * * *
ClearSelPos:
* * * * * * *

Set_Property(CtrlEntID,'SELPOS',0:@FM:0)

Result = 1

RETURN


* * * * * * *
Recalc:
* * * * * * *

SheetRhoReads	= Get_Property(@WINDOW:'.SHEETRHO_READINGS','LIST')
ThicknessReads	= Get_Property(@WINDOW:'.THICKNESS_READINGS','LIST')


ReadCnt = COUNT(SheetRhoReads,@FM) + (SheetRhoReads NE '')
IF ReadCnt = '' THEN
	ReadCnt = COUNT(ThicknessReads,@FM) + (ThicknessReads NE '')
END

FOR I = 1 TO ReadCnt
	SheetRhoRead	= SheetRhoReads<I>
	ThicknessRead	= ThicknessReads<I>
	
	IF SheetRhoRead = '' OR ThicknessRead = '' THEN
		Result<I> = ''
	END ELSE
		IF SheetRhoRead NE 0 AND ThicknessRead NE 0 THEN	
			Result<I> = OCONV(ICONV((SheetRhoRead * ThicknessRead/10000),'MD4'),'MD4')
		END ELSE
			Result<1,I> = ''
		END
	END
NEXT I

Set_Property(@WINDOW:'.RES_READINGS','LIST',Result)

RETURN


* * * * * * *
Calc:
* * * * * * *

Ctrls  = @WINDOW:'.TTHICK_AVG':@RM			; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TTHICK_STDEV':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TTHICK_MAX':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TTHICK_MIN':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TTHICK_UNIF'				; Props := 'DEFPROP'

StatReadings = Get_Property(@WINDOW:'.THICKNESS_READINGS','LIST')
StatConversion = 'MD3'

GOSUB CalcStats

Set_Property(Ctrls,Props,Stats)


Ctrls  = @WINDOW:'.SHEETRHO_AVG':@RM		; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.SHEETRHO_STDEV':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.SHEETRHO_MAX':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.SHEETRHO_MIN':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.SHEETRHO_UNIF'			; Props := 'DEFPROP'

StatReadings = Get_Property(@WINDOW:'.SHEETRHO_READINGS','LIST')
StatConversion = 'MD3'

GOSUB CalcStats

Set_Property(Ctrls,Props,Stats)

Ctrls  = @WINDOW:'.TRES_AVG':@RM			; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TRES_STDEV':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TRES_MAX':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TRES_MIN':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TRES_UNIF'				; Props := 'DEFPROP'

StatReadings = Get_Property(@WINDOW:'.RES_READINGS','LIST')
StatConversion = 'MD4'

GOSUB CalcStats

Set_Property(Ctrls,Props,Stats)

RETURN



* * * * * * *
CalcStats:
* * * * * * *

thisReadings = StatReadings
Conversion = StatConversion

StatAvg 	= ''
StatStdv	= ''
StatUnif	= ''
StatMin		= ''
StatMax		= ''

AvgCount	= 0
AvgTotal	= 0

ReadCount = COUNT(thisReadings,@FM) + (thisReadings NE '')

FOR I = 1 TO ReadCount
	thisReading = ThisReadings<I>
	IF thisReading NE '' AND thisReading NE 0.00 THEN
		AvgCount += 1
		AvgTotal += thisReading
		
		IF thisReading _LEX StatMin OR StatMin = '' THEN StatMin = thisReading
		IF thisReading _GEX StatMax OR StatMax = '' THEN StatMax = thisReading
	END
NEXT I

IF AvgCount > 0 THEN
	StatAvg = AvgTotal/AvgCount
	Mean	= AvgTotal/AvgCount

	SumDiffSq = 0

	FOR I = 1 TO ReadCount
		thisReading = thisReadings<I>
		IF thisReading NE '' THEN
*			SumDiffSq += ( thisReading - Mean )**2
			SumDiffSq += ( thisReading - Mean ) * ( thisReading - Mean )
		END
	NEXT I


	IF SumDiffSq _GEX 0.00 AND AvgCount > 1 THEN
		StatStdv = Sqrt( SumDiffSq/(AvgCount - 1) )	
	END
	IF SumDiffSq _EQX 0 AND AvgCount > 1 THEN StatStdv = 0
END

IF StatMin NE '' AND StatMax NE '' AND StatMin _GEX 0 AND StatMin _GEX 0 THEN

	StatUnif = OCONV(((StatMax - StatMin)/(StatMax + StatMin))*10000, 'MD2' )
END


StatAvg		= ICONV(StatAvg,Conversion)
StatStdv	= ICONV(StatStdv,'MD4')
StatMin		= ICONV(StatMin,Conversion)
StatMax		= ICONV(StatMax,Conversion)


Stats = OCONV(StatAvg,Conversion):@RM:OCONV(StatStdv,'MD4'):@RM:OCONV(StatMax,Conversion):@RM:OCONV(StatMin,Conversion):@RM:StatUnif


RETURN










* * * * * * *
OK:
* * * * * * *

RETURN

