 COMPILE FUNCTION obj_RDS2(Method,Parms)

/*
	Methods for RDS table		;Original object (obj_RDS) is full with methods for original data structures
    
	05/12/2005 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Find()												;* Lookup RDS record
    Delete(RdsNos)										;* Delete RDS records (@VM'd list of keys)
    LockSet(RdsNos)										;* Returns TableVar if all records were locked
    UnlockSet(RdsNos,TableVar)							;* Uses TableVar returned from LockSet call
    ChangeWOStepProps(RSNos,CommitDts,HotLots,MUBoxes)	;* Changes RDS stuff 'en-mass' from WOStep window
	
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey,  obj_Prod_Spec, Dialog_Box, obj_RDS_Test
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, obj_WO_Step, obj_RDS_Layer, obj_RDS_Test

$INSERT MSG_EQUATES
$INSERT WO_VERIFY_EQU
$INSERT WO_LOG_EQU
$INSERT RDS_EQU
$INSERT RDS_MAKEUP_EQU
$INSERT RDS_LAYER_EQUATES
$INSERT RDS_TEST_EQUATES
$INSERT RDS_LAYER_INFO_EQU
$INSERT QUOTE_SPEC_EQU
$INSERT PROD_SPEC_EQU
$INSERT SCHEDULE_EQU
$INSERT WO_STEP_EQU

$INSERT PRS_LAYER_EQU	;* Used to return obj_Prod_Spec values


EQU SHEETRHO_SPEC_UNITS$	TO 'ê/Ü'
EQU RES_SPEC_UNITS$			TO \EA2D636D\


ErrTitle = 'Error in Stored Procedure "obj_RDS2"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Find'				; GOSUB Find
	CASE Method = 'Delete'				; GOSUB Delete
	CASE Method = 'LockSet'				; GOSUB LockSet
	CASE Method = 'UnlockSet'			; GOSUB UnlockSet
	CASE Method = 'ChangeWOStepProps'	; GOSUB ChangeWOStepProps
	CASE Method = 'AdjStepWafersIn'		; GOSUB AdjStepWafersIn
	CASE Method = 'GetMetKeys'			; GOSUB GetMetKeys
	CASE Method = 'MetConversion'		; GOSUB MetConversion
	CASE Method = 'LoadEpi'				; GOSUB LoadEpi
	CASE Method = 'RemovePocketNCR'		; GOSUB RemovePocketNCR
	CASE 1

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
END

RETURN Result


* * * * * * *
Find:
* * * * * * *


RDSKeys = Dialog_Box('DIALOG_RDS_QUERY',@WINDOW,'')

Result = RDSKeys
		
RETURN


* * * * * * *
Delete:
* * * * * * *

RdsNos		= Parms[1,@RM]
TableVar	= Parms[COL2()+1,@RM]

IF RdsNos = '' THEN RETURN

RdsDateIns = XLATE('RDS',RdsNos,35,'X')	;* Field 35 = RDS_DATE_IN$

CONVERT @FM TO @VM IN RdsDateIns

IF @USERNAME = 'BRYCE_BARB' THEN DEBUG

TestString = RdsDateIns

CONVERT @VM TO '' IN TestString

IF TestString NE '' THEN
	FOR I = 1 TO COUNT(RdsDateIns,@VM) + (RdsDateIns NE '')
		IF RdsDateIns<1,I> NE '' THEN
			Result<1,-1> = RdsNos<1,I>			;* Return list of RdsNos that have started processing and cannot be deleted
		END
	NEXT I
	ErrorMsg = 'Production started on one or more RDS No(s).'
	RETURN
END

IF TableVar = '' THEN
	* This section of code should be removed after all calls to obj_RDS('Delete'....) have been checked
	Set_Status(0)
	RTParms = 'RDS'
	LockedRDSNos = ''

	FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
		RdsNo = RdsNos<1,I>
		RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)
		obj_Tables('LockRec',RTParms)
		IF Get_Status(errCode) THEN
			FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
				RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
				obj_Tables('UnlockRec',RTParms)									;* Unlock everything locked up to here
			NEXT N
			ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):' for delete.'
			RETURN
		END ELSE
			LockedRDSNos<1,I> = RdsNo
		END
	NEXT I

	TableVar = FIELD(RTParms,@RM,3,1)
	
END

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo = RdsNos<1,I>
	LSKeys = XLATE('RDS',RdsNo,RDS_RDS_LAYER_KEYS$,'X')
	MetKeys = XLATE('RDS_LAYER',LSKeys,RDS_LAYER_RDS_TEST_KEYS$,'X')
	
	RTParms = 'RDS':@RM:RdsNo:@RM:TableVar:@RM
	obj_Tables('UnlockRec',RTParms)
	
	obj_RDS_Test('Delete',MetKeys)
	
	obj_RDS_Layer('Delete',LSKeys)
	
	obj_Tables('DeleteRec','RDS':RTParms)				;* Deletes and removes the lock
NEXT I

RETURN


* * * * * * * 
LockSet:
* * * * * * * 

RdsNos 		= Parms[1,@RM]

IF RdsNos	= '' THEN RETURN

Set_Status(0)
RTParms = 'RDS'
LockedRDSNos = ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo = RdsNos<1,I>
	RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)
	obj_Tables('LockRec',RTParms)
	IF Get_Status(errCode) THEN
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)									;* Unlock everything locked up to here
		NEXT N
		ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):'.'
		RETURN
	END ELSE
		LockedRDSNos<1,I> = RdsNo
	END
NEXT I

TableVar = FIELD(RTParms,@RM,3,1)

Result = TableVar

RETURN


* * * * * * * 
UnlockSet:
* * * * * * * 

RdsNos 		= Parms[1,@RM]
TableVar	= Parms[COL2()+1,@RM]

IF RdsNos	= '' THEN RETURN
IF TableVar = '' THEN RETURN

Set_Status(0)
RTParms = 'RDS':@RM:@RM:TableVar
LockedRDSNos = ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo = RdsNos<1,I>
	RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)
	obj_Tables('UnlockRec',RTParms)
NEXT I

RETURN



* * * * * * *
ChangeWOStepProps:
* * * * * * *

RDSNos		= Parms[1,@RM]
CommitDts	= Parms[COL2()+1,@RM]
HotLots		= Parms[COL2()+1,@RM]
MUBoxes		= Parms[COL2()+1,@RM]

IF NOT(ASSIGNED(RDSNos))		THEN ErrorMsg = 'Unassigned Parm "RDSNos" passed to routine. (':Method:')'
IF NOT(ASSIGNED(CommitDts))		THEN CommitDts = ''
IF NOT(ASSIGNED(HotLots))		THEN HotLots = ''
IF NOT(ASSIGNED(MUBoxes))		THEN MUBoxes = ''

IF ErrorMsg NE '' THEN RETURN

IF RDSNos = '' THEN RETURN

Set_Status(0)
RTParms = 'RDS'
LockedRDSNos = ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	
	RdsNo = RdsNos<1,I>
	RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)

	obj_Tables('LockRec',RTParms)
	IF Get_Status(errCode) THEN
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
		NEXT N
		ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):' for update.'
		RETURN
	END ELSE
		LockedRDSNos<1,I> = RdsNo
	END
NEXT I

CommitDts	= ICONV(CommitDts,'D')		;* Move to internal format
HotLots		= ICONV(HotLots,'B')
MUBoxes		= ICONV(MUBoxes,'B')

RDSTableVar		= FIELD(RTParms,@RM,3)

FOR I = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
	LockedRDSNo = LockedRDSNos<1,I>
	READ RDSRec FROM RDSTableVar,LockedRDSNo THEN
		CommitDt	= CommitDts<1,I>
		HotLot		= HotLots<1,I>
		MUBox		= MUBoxes<1,I>
		
		IF CommitDt NE RDSRec<RDS_COMMIT_DATE_FINAL$>	THEN RDSRec<RDS_COMMIT_DATE_FINAL$>	= CommitDt
		IF HotLot	NE RDSRec<RDS_HOT_LOT$>				THEN RDSRec<RDS_HOT_LOT$>			= HotLot
		IF MUBox NE RDSRec<RDS_MAKEUP_BOX$>				THEN RDSRec<RDS_MAKEUP_BOX$> 		= MUBox
		
		RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNo)
		RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
		obj_Tables('WriteRec',RTParms)								;* Write and unlock RDS records
	END
NEXT I

RETURN


* * * * * * *
AdjStepWafersIn:
* * * * * * *

RETURN

* Dead as of 11/5/2008 JCH

StepCompRdsNos  = Parms[1,@RM]
AdjRdsNos		= Parms[COL2()+1,@RM]

IF StepCompRdsNos = ''	THEN ErrorMsg = 'Null parameter "StepCompRdsNos" passed to routine. (':Method:')'
IF AdjRdsNos = ''		THEN ErrorMsg = 'Null parameter "AdjRdsNos" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

Set_Status(0)
RTParms = 'RDS'
LockedRDSNos = ''

FOR I = 1 TO COUNT(AdjRdsNos,@VM) + (AdjRdsNos NE '')
	
	AdjRdsNo = AdjRdsNos<1,I>
		
	RTParms = FieldStore(RTParms, @RM, 2, 1, AdjRdsNo)

	obj_Tables('LockRec',RTParms)
	IF Get_Status(errCode) THEN
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
		NEXT N
		ErrorMsg = 'Unable to lock RDS ':QUOTE(AdjRdsNo):' for update.'
		RETURN
	END ELSE
		LockedRDSNos<1,I> = AdjRdsNo
	END
NEXT I

RDSTableVar		= FIELD(RTParms,@RM,3)

FOR I = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
	LockedRDSNo = LockedRDSNos<1,I>
	
	READ RDSRec FROM RDSTableVar,LockedRDSNo THEN

		SCRdsNo						= StepCompRdsNos<1,I>
		SCWafersOut					= XLATE('RDS',SCRdsNo,'WFRS_OUT','X')		;* Wafers out from previous step
		RDSRec<RDS_CASS_WAFER_QTY$>	= SCWafersOut
			
		RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNo)
		RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
		obj_Tables('WriteRec',RTParms)											;* Write and unlock RDS records
	END ELSE
		status = Set_FSError()
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
		NEXT N
		
	END
NEXT I

RETURN


* * * * * * *
GetMetKeys:
* * * * * * *

* Returns data structure of 'LSID*ZONE' - @VM in first column associated RDS_TEST numbers in 2nd Column.

RdsNo  = Parms[1,@RM]

IF RdsNo = '' THEN RETURN

SortedLSKeys = XLATE('RDS',RdsNo,'RDS_LS_SORTED','X')
CONVERT @VM TO @FM IN SortedLSKeys

IF SortedLSKeys NE '' THEN
	LSMetKeys = XLATE('RDS_LAYER',SortedLSKeys,RDS_LAYER_RDS_TEST_KEYS$,'X')
	RDSMetZones = XLATE('RDS_TEST',LSMetKeys,RDS_TEST_ZONE$,'X')
	ResultLine = 0
	FOR I = 1 TO COUNT(SortedLSKeys,@FM) + (SortedLSKeys NE '')
		SortedLSKey = SortedLSKeys<I>
		LSMetKey = LSMetKeys<I>
		
		FOR N = 1 TO COUNT(LSMetKey,@VM) + (LSMetKey NE '')
			ResultLine += 1
			Result<1,ResultLine> = FIELD(SortedLSKey,'*',2):'*':RDSMetZones<I,N>
			Result<2,ResultLine> = LSMetKeys<I,N>
		NEXT N
	NEXT I
END

RETURN


* * * * * * *
LoadEpi:
* * * * * * * 

RdsNo			= Parms[1,@RM]
Pockets			= Parms[COL2()+1,@RM]
PocketChars		= Parms[COL2()+1,@RM]
Zones			= Parms[COL2()+1,@RM]
InCassettes		= Parms[COL2()+1,@RM]
InSlots			= Parms[COL2()+1,@RM]
WaferChars		= Parms[COL2()+1,@RM]
OutCassettes	= Parms[COL2()+1,@RM]
OutSlots		= Parms[COL2()+1,@RM]
OutNCRs			= Parms[COL2()+1,@RM]

IF RdsNo = '' THEN RETURN

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for LoadEpi update.'
	RETURN
END


RDSRec<RDS_POCKET$>			= Pockets
RDSRec<RDS_POCKET_CHAR$>	= PocketChars
RDSRec<RDS_ZONE$>			= Zones
RDSRec<RDS_IN_CASS_NO$>		= InCassettes
RDSRec<RDS_IN_SLOT_NO$>		= Inslots
RDSRec<RDS_OUT_CASS_NO$>	= OutCassettes
RDSRec<RDS_OUT_CASS_SLOT$>	= OutSlots
RDSRec<RDS_WAFER_CHAR$>		= WaferChars
RDSRec<RDS_OUT_NCR$>		= OutNCRs


LoadCnt = 0
FOR I = 1 TO COUNT(WaferChars,@VM) + (WaferChars NE '')
	IF WaferChars<1,I> = 'PROD' THEN LoadCnt += 1
NEXT I

RDSRec<RDS_WAFERS_IN$>		= LoadCnt
RDSRec<RDS_VERIFY_QTY$>		= LoadCnt
RDSRec<RDS_CASS_WAFER_QTY$>	= LoadCnt

RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
obj_Tables('WriteRec',RTParms)		


RETURN


* * * * * * *
RemovePocketNCR:
* * * * * * *

RDSNo	= Parms[1,@RM]
NCRNo	= Parms[COL2()+1,@RM]

IF RDSNo = '' THEN RETURN
IF NCRNo = '' THEN RETURN

IF RDSNo = ''	THEN ErrorMsg = 'Null parameter "RDSNo" passed to routine. (':Method:')'
IF NCRNo = ''	THEN ErrorMsg = 'Null parameter "NCRNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for LoadEpi update.'
	RETURN
END

OutNCRs = RDSRec<RDS_OUT_NCR$>

Done = 0
LOOP
	LOCATE NCRNo IN RDSRec<RDS_OUT_NCR$> USING @VM SETTING Pos THEN
		RDSRec<RDS_OUT_NCR$,Pos> = ''
	END ELSE
		Done = 1
	END
UNTIL Done
REPEAT

RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
obj_Tables('WriteRec',RTParms)		

RETURN


* * * * * * *
MetConversion:
* * * * * * * 

* Updates entered data into RDS_LAYER and RDS_TEST records from RDS record


RETURN		;* Dead 4/16/2006 with installation of metrology and NCR updates

RDSNo 	= Parms[1,@RM]


IF RDSNo = '' THEN RETURN

SortedLSKeys	= XLATE('RDS',RDSNo,'RDS_LS_SORTED','X')
RDSRec			= XLATE('RDS',RDSNo,'','X')
PSNId			= RDSRec<RDS_PROD_SPEC_ID$>


ResUnits	= RDSRec<RDS_RES_UNITS$>
LayerMess	= RDSRec<RDS_LAYER_INFO$>

CONVERT CHAR(248) TO @FM IN LayerMess

LayerCnt 	= COUNT(LayerMess,@FM) + (LayerMess NE '')

IF SortedLSKeys = '' THEN
	SortedLSKeys = 'L1'
	FOR N = 1 TO COUNT(LayerMess,@FM) + (LayerMess NE '')
	
	IF N = LayerCnt THEN
			LS_ID = N
		END ELSE
			LS_ID = 'L':N
		END
		SortedLSKeys<1,-1> = LS_ID
	NEXT N
END

FOR I = 1 TO COUNT(SortedLSKeys,@VM) + (SortedLSKeys NE '')
	LSKey = SortedLSKeys<1,I>
	LS_ID = FIELD(LSKey,'*',2)
	
	RLParms = 'RDS_LAYER':@RM:LSKey
	RDSLayerRec = obj_Tables('ReadRec',RLParms)				;* Try reading record
	
	IF RDSLayerRec = '' THEN
		obj_RDS_Layer('Create',RDSNo:@RM:LS_ID:@RM:PSNId)	;* Wasn't there - create it
		RDSLayerRec = obj_Tables('ReadRec',RLParms)			;* Read it again
	END
	
	WaferSize		= XLATE( 'PROD_SPEC', PSNId, 'SUB_WAFER_SIZE', 'X' )
	SubOrientation	= XLATE('PROD_SPEC',PSNId,'SUB_ORIENTATION','X')
	ResUnits		= RDSRec<RDS_RES_UNITS$>
	
	IF I = 1 THEN
		RDSLayerRec<RDS_LAYER_RECIPE_NO$>			= RDSRec<RDS_RECIPE_NO$>
		RDSLayerRec<RDS_LAYER_EPI_TIME$>			= RDSRec<RDS_BAKE_TIME$>
		RDSLayerRec<RDS_LAYER_DILUENT_ADJ_PARAM$>	= RDSRec<RDS_DILUENT_ADJ_PARAM$>
		RDSLayerRec<RDS_LAYER_DOPANT_FLOW$>			= RDSRec<RDS_DOPANT_FLOW$>
		RDSLayerRec<RDS_LAYER_HCL_FLOW$>			= RDSRec<RDS_HCL_FLOW$>
		RDSLayerRec<RDS_LAYER_BAKE_TIME$>			= RDSRec<RDS_BAKE_TIME$>
		RDSLayerRec<RDS_LAYER_EPI_H2_FLOW$>			= RDSRec<RDS_EPI_H2_FLOW$>
		RDSLayerRec<RDS_LAYER_TCS_FLOW$>			= RDSRec<RDS_TCS_FLOW$>
		RDSLayerRec<RDS_LAYER_DCS_FLOW$>			= RDSRec<RDS_DCS_FLOW$>
		RDSLayerRec<RDS_LAYER_AUX1$>				= RDSRec<RDS_AUX1$>
		RDSLayerRec<RDS_LAYER_AUX2$>				= RDSRec<RDS_AUX2$>
		RDSLayerRec<RDS_LAYER_F_OFFSET$>			= RDSRec<RDS_F_OFFSET$>
		RDSLayerRec<RDS_LAYER_S_OFFSET$>			= RDSRec<RDS_S_OFFSET$>
		RDSLayerRec<RDS_LAYER_R_OFFSET$>			= RDSRec<RDS_R_OFFSET$>
		RDSLayerRec<RDS_LAYER_ETCH1$>				= RDSRec<RDS_ETCH1$>
		RDSLayerRec<RDS_LAYER_ETCH2$>				= RDSRec<RDS_ETCH2$>
		
		IF RDSLayerRec<RDS_LAYER_RDS_TEST_KEYS$> = '' THEN
			
			RDSTestRec = ''
			RDSTestRec<RDS_TEST_RDS_NO$> = RDSNo
			RDSTestRec<RDS_TEST_LS_ID$> = 'L1'
			RDSTestRec<RDS_TEST_WAFER_SIZE$>			= WaferSize
			RDSTestRec<RDS_TEST_SUB_ORIENTATION$>		= SubOrientation
			
			RDSTestRec<RDS_TEST_SPEC_THICK_MIN$>		= RDSRec<RDS_THICK_MIN$>
			RDSTestRec<RDS_TEST_SPEC_THICK_TARGET$>		= RDSRec<RDS_THICK_TARGET$>
			RDSTestRec<RDS_TEST_SPEC_THICK_MAX$>		= RDSRec<RDS_THICK_MAX$>
			RDSTestRec<RDS_TEST_SPEC_THICK_UNITS$>		= RDSRec<RDS_THICK_UNITS$>
			RDSTestRec<RDS_TEST_SPEC_RES_MIN$>			= RDSRec<RDS_RES_MIN$>
			RDSTestRec<RDS_TEST_SPEC_RES_TARGET$>		= RDSRec<RDS_RES_TARGET$>
			RDSTestRec<RDS_TEST_SPEC_RES_MAX$>			= RDSRec<RDS_RES_MAX$>
			RDSTestRec<RDS_TEST_SPEC_RES_UNITS$>		= RDSRec<RDS_RES_UNITS$>
			RDSTestRec<RDS_TEST_SPEC_CON_MIN$>			= RDSRec<RDS_CON_MIN$>
			RDSTestRec<RDS_TEST_SPEC_CON_TARGET$>		= RDSRec<RDS_CON_TARGET$>
			RDSTestRec<RDS_TEST_SPEC_CON_MAX$>			= RDSRec<RDS_CON_MAX$>
			RDSTestRec<RDS_TEST_SPEC_CON_UNITS$>		= RDSRec<RDS_CON_UNITS$>
			
			* Now load the specification stuff that was not copied in the org system
			
			SpecLayerData	= obj_Prod_Spec('GetLayerProp',PSNId:@RM:LS_ID:@RM:1)	;* Gets 1st layer WO/Conversiion
			SpecLayerID		= SpecLayerData[1,@FM]
			SpecLayerData	= FIELD(SpecLayerData,@FM,2,999)
					
			RDSTestRec<RDS_TEST_SPEC_DOPANT$>		= SpecLayerData<PRS_LAYER_DOPANT$>
			RDSTestRec<RDS_TEST_SPEC_RECIPE$>		= SpecLayerData<PRS_LAYER_RECIPE$>
			RDSTestRec<RDS_TEST_SPEC_RECIPE_NAME$>	= SpecLayerData<PRS_LAYER_RECIPE_NAME$>
			
			FOR A = 11 TO 19
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_THICK_MEASUREMENT$,A-10>
			NEXT A
				
			FOR A = 24 TO 32
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_RES_MEASUREMENT$,A-23>
			NEXT A
			
			FOR A = 37 TO 45
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_CONC_MEASUREMENT$,A-36>
			NEXT A
			
			FOR A = 50 to 58
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_STRESS_MEASUREMENT$,A-49>
			NEXT A

			RDSTestRec<RDS_TEST_SPEC_TRANS$>				= SpecLayerData<PRS_LAYER_TRANS_SPEC$>
			RDSTestRec<RDS_TEST_SPEC_TRANS_MEASUREMENT$>	= SpecLayerData<PRS_LAYER_TRANS_MEASUREMENT$>
			
			RTKey = NextKey('RDS_TEST')
			RTParms = 'RDS_TEST':@RM:RTKey
		END ELSE
			RTKey = RDSLayerRec<RDS_LAYER_RDS_TEST_KEYS$,1>
			RTParms = 'RDS_TEST':@RM:RTKey
			RDSTestRec = obj_Tables('ReadRec',RTParms)				;* Try reading record
		END
		
		RDSTestRec<RDS_TEST_READ_SHEET_RHO$>		= ICONV(OCONV(RDSRec<RDS_SHEETRHO_READ$>,'MD2'),'MD3')
		
		RDSTestRec<RDS_TEST_READ_THICK$>			= RDSRec<RDS_THICK_READ$>
		
		RDSTestRec<RDS_TEST_THICK_AVG$>				= RDSRec<RDS_TTHICK_AVG$>
		RDSTestRec<RDS_TEST_THICK_STDV$>			= RDSRec<RDS_TTHICK_STDEV$>
		RDSTestRec<RDS_TEST_THICK_UNIF$>			= RDSRec<RDS_TTHICK_UNIF$>
		RDSTestRec<RDS_TEST_THICK_MIN$>				= RDSRec<RDS_TTHICK_MIN$>
		RDSTestRec<RDS_TEST_THICK_MAX$>				= RDSRec<RDS_TTHICK_MAX$>
		
		IF ResUnits = SHEETRHO_SPEC_UNITS$ THEN
			RDSTestRec<RDS_TEST_SHEETRHO_AVG$>			= RDSRec<RDS_TRES_AVG$>
			RDSTestRec<RDS_TEST_SHEETRHO_STDV$>			= RDSRec<RDS_TRES_STDEV$>
			RDSTestRec<RDS_TEST_SHEETRHO_UNIF$>			= RDSRec<RDS_TRES_UNIF$>
			RDSTestRec<RDS_TEST_SHEETRHO_MIN$>			= RDSRec<RDS_TRES_MIN$>
			RDSTestRec<RDS_TEST_SHEETRHO_MAX$>			= RDSRec<RDS_TRES_MAX$>
			RDSTestRec<RDS_TEST_RES_AVG$>				= ''
			RDSTestRec<RDS_TEST_RES_STDV$>				= ''
			RDSTestRec<RDS_TEST_RES_UNIF$>				= ''
			RDSTestRec<RDS_TEST_RES_MIN$>				= ''
			RDSTestRec<RDS_TEST_RES_MAX$>				= ''
		END ELSE
			RDSTestRec<RDS_TEST_SHEETRHO_AVG$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_STDV$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_UNIF$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_MIN$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_MAX$>			= ''
			RDSTestRec<RDS_TEST_RES_AVG$>				= RDSRec<RDS_TRES_AVG$>
			RDSTestRec<RDS_TEST_RES_STDV$>				= RDSRec<RDS_TRES_STDEV$>
			RDSTestRec<RDS_TEST_RES_UNIF$>				= RDSRec<RDS_TRES_UNIF$>
			RDSTestRec<RDS_TEST_RES_MIN$>				= RDSRec<RDS_TRES_MIN$>
			RDSTestRec<RDS_TEST_RES_MAX$>				= RDSRec<RDS_TRES_MAX$>
		END
		
		RDSTestRec<RDS_TEST_THICK_OVERGROW_AVG$>	= RDSRec<RDS_THICK_OVR_GROWTH_AVG$>
		RDSTestRec<RDS_TEST_SRP_PROFILE_PATH$>		= RDSRec<RDS_SRP_PROFILE_PATH$>

		GOSUB LoadTestWafers
			
		RDSTestRec = obj_RDS_Test('CalcMissing',RDSTestRec)

	END ELSE
		
		RecipeNo = LayerMess<I-1,RLRecipeNo$>
					
		IF INDEX(LS_ID,'L',1) THEN
			RDSLayerRec<RDS_LAYER_RECIPE_NO$>			= RecipeNo
			RDSLayerRec<RDS_LAYER_EPI_TIME$>			= LayerMess<I-1,RLEpiTime$>
			RDSLayerRec<RDS_LAYER_DOPANT_FLOW$>			= LayerMess<I-1,RLDopantFlow$>
		END

		IF RDSLayerRec<RDS_LAYER_RDS_TEST_KEYS$> = '' THEN
		
			PSNLayerData = obj_Prod_Spec('GetLayerProp',PSNId:@RM:LS_ID)
			PSNLayerData = FIELD(PSNLayerData,@FM,2,999)	;* Layer ID is returned in the 1st field position
			 				
			
			RDSTestRec = ''
			RDSTestRec<RDS_TEST_RDS_NO$>					= RDSNo
			RDSTestRec<RDS_TEST_LS_ID$>						= LS_ID
			RDSTestRec<RDS_TEST_SPEC_THICK_MIN$>			= LayerMess<I-1,RLThickMin$>
			RDSTestRec<RDS_TEST_SPEC_THICK_TARGET$>			= LayerMess<I-1,RLThickTarget$>
			RDSTestRec<RDS_TEST_SPEC_THICK_MAX$>			= LayerMess<I-1,RLThickMax$>
			RDSTestRec<RDS_TEST_SPEC_THICK_UNITS$>			= LayerMess<I-1,RLThickUnits$>
			
			RDSTestRec<RDS_TEST_SPEC_RES_MIN$>				= LayerMess<I-1,RLResMin$>
			RDSTestRec<RDS_TEST_SPEC_RES_TARGET$>			= LayerMess<I-1,RLResTarget$>
			RDSTestRec<RDS_TEST_SPEC_RES_MAX$>				= LayerMess<I-1,RLResMax$>
			RDSTestRec<RDS_TEST_SPEC_RES_UNITS$>			= LayerMess<I-1,RLResUnits$>
			
			RDSTestRec<RDS_TEST_SPEC_CON_MIN$>				= LayerMess<I-1,RLConMin$>
			RDSTestRec<RDS_TEST_SPEC_CON_TARGET$>			= LayerMess<I-1,RLConTarget$>
			RDSTestRec<RDS_TEST_SPEC_CON_MAX$>				= LayerMess<I-1,RLConMax$>
			RDSTestRec<RDS_TEST_SPEC_CON_UNITS$>			= LayerMess<I-1,RLConUnits$>
			
			RDSTestRec<RDS_TEST_SPEC_STRESS_MIN$>			= PSNLayerData<PRS_LAYER_STRESS_MIN$>
			RDSTestRec<RDS_TEST_SPEC_STRESS_MAX$>			= PSNLayerData<PRS_LAYER_STRESS_MAX$>
			
			RDSTestRec<RDS_TEST_SPEC_TRANS$>				= PSNLayerData<PRS_LAYER_TRANS_SPEC$>
			RDSTestRec<RDS_TEST_SPEC_TRANS_MEASUREMENT$>	= PSNLayerData<PRS_LAYER_TRANS_MEASUREMENT$>
			
			RDSTestRec<RDS_TEST_WAFER_SIZE$>				= WaferSize
			RDSTestRec<RDS_TEST_SUB_ORIENTATION$>			= SubOrientation
			
			
			FOR A = 11 TO 19
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_THICK_MEASUREMENT$,A-10>
			NEXT A
				
			FOR A = 24 TO 32
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_RES_MEASUREMENT$,A-23>
			NEXT A
			
			FOR A = 37 TO 45
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_CONC_MEASUREMENT$,A-36>
			NEXT A
			
			FOR A = 50 to 58
				RDSTestRec<A> = SpecLayerData<PRS_LAYER_STRESS_MEASUREMENT$,A-49>
			NEXT A
			
			RTKey = NextKey('RDS_TEST')
			RTParms = 'RDS_TEST':@RM:RTKey
			
		END ELSE
		 
			RTKey = RDSLayerRec<RDS_LAYER_RDS_TEST_KEYS$>
			RTParms = 'RDS_TEST':@RM:RTKey
			RDSTestRec = obj_Tables('ReadRec',RTParms)				;* Try reading record
			
		END
	
		* Update results data in Metrology record
		
		
		SheetRHOReads  = LayerMess<I-1,RLSheetRHORead$>
		ThicknessReads = LayerMess<I-1,RLThickRead$>
		
		CONVERT @SVM TO @VM IN SheetRHOReads
		CONVERT @SVM TO @VM IN ThicknessReads
		
		A1 = SheetRHOReads
		
		RDSTestRec<RDS_TEST_READ_SHEET_RHO$>			= ICONV(OCONV(SheetRHOReads,'MD2'),'MD3')
		RDSTestRec<RDS_TEST_READ_THICK$>				= ThicknessReads
		
		
		RDSTestRec<RDS_TEST_THICK_AVG$>					= LayerMess<I-1,RLTThickAvg$>
		RDSTestRec<RDS_TEST_THICK_STDV$>				= LayerMess<I-1,RLTThickStdev$>
		RDSTestRec<RDS_TEST_THICK_UNIF$>				= LayerMess<I-1,RLTThickUnif$>
		RDSTestRec<RDS_TEST_THICK_MIN$>					= LayerMess<I-1,RLTThickMin$>
		RDSTestRec<RDS_TEST_THICK_MAX$>					= LayerMess<I-1,RLTThickMax$>
		
		
		IF ResUnits = SHEETRHO_SPEC_UNITS$ THEN
			RDSTestRec<RDS_TEST_SHEETRHO_AVG$>			= LayerMess<I-1,RLTResAvg$>
			RDSTestRec<RDS_TEST_SHEETRHO_STDV$>			= LayerMess<I-1,RLTResStdev$>
			RDSTestRec<RDS_TEST_SHEETRHO_UNIF$>			= LayerMess<I-1,RLTResUnif$>
			RDSTestRec<RDS_TEST_SHEETRHO_MIN$>			= LayerMess<I-1,RLTResMin$>
			RDSTestRec<RDS_TEST_SHEETRHO_MAX$>			= LayerMess<I-1,RLTResMax$>
			RDSTestRec<RDS_TEST_RES_AVG$>				= ''
			RDSTestRec<RDS_TEST_RES_STDV$>				= ''
			RDSTestRec<RDS_TEST_RES_UNIF$>				= ''
			RDSTestRec<RDS_TEST_RES_MIN$>				= ''
			RDSTestRec<RDS_TEST_RES_MAX$>				= ''
		END ELSE
			RDSTestRec<RDS_TEST_SHEETRHO_AVG$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_STDV$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_UNIF$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_MIN$>			= ''
			RDSTestRec<RDS_TEST_SHEETRHO_MAX$>			= ''
			RDSTestRec<RDS_TEST_RES_AVG$>				= LayerMess<I-1,RLTResAvg$>
			RDSTestRec<RDS_TEST_RES_STDV$>				= LayerMess<I-1,RLTResStdev$>
			RDSTestRec<RDS_TEST_RES_UNIF$>				= LayerMess<I-1,RLTResUnif$>
			RDSTestRec<RDS_TEST_RES_MIN$>				= LayerMess<I-1,RLTResMin$>
			RDSTestRec<RDS_TEST_RES_MAX$>				= LayerMess<I-1,RLTResMax$>
		END
												
		* GOSUB LoadTestWafers
			
		RDSTestRec = obj_RDS_Test('CalcMissing',RDSTestRec)
		
	END
	
	
	* Write and unlock the RDS_Layer Record
	
	RLParms = FieldStore(RLParms, @RM, 4, 1, RDSLayerRec)
	obj_Tables('WriteRec',RLParms)
	
	* Write and unlock the RDS_Test Record
	
	RTParms = FieldStore(RTParms, @RM, 4, 1, RDSTestRec)
	obj_Tables('WriteRec',RTParms)

NEXT I
RETURN



* * * * * * * 
LoadTestWafers:
* * * * * * *

TWLine = 0

IF RDSRec<RDS_TW_NMINUS_CNT$> NE '' THEN
	TWType = 'N-'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_NMINUS_CODE$>,@VM) + (RDSRec<RDS_TW_NMINUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_NMINUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_NMINUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_NMINUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_NMINUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_NMINUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_NMINUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_NMINUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_NMINUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_NMINUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_NPLUS_CNT$> NE '' THEN
	TWType = 'N+'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_NPLUS_CODE$>,@VM) + (RDSRec<RDS_TW_NPLUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_NPLUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_NPLUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_NPLUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_NPLUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_NPLUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_NPLUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_NPLUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_NPLUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_NPLUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_PMINUS_CNT$> NE '' THEN
	TWType = 'P-'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_PMINUS_CODE$>,@VM) + (RDSRec<RDS_TW_PMINUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_PMINUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_PMINUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_PMINUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_PMINUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_PMINUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_PMINUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_PMINUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_PMINUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_PMINUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_PPLUS_CNT$> NE '' THEN
	TWType = 'P+'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_PPLUS_CODE$>,@VM) + (RDSRec<RDS_TW_PPLUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_PPLUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_PPLUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_PPLUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_PPLUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_PPLUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_PPLUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_PPLUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_PPLUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_PPLUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_PROD_CNT$> NE '' THEN
	TWType = 'Prod'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_PROD_CODE$>,@VM) + (RDSRec<RDS_TW_PROD_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_PROD_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_PROD_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_PROD_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_PROD_CNT$,T>
		TWCon		= RDSRec<RDS_TW_PROD_CON$,T>
		TWRes		= RDSRec<RDS_TW_PROD_RES$,T>
		TWRho		= RDSRec<RDS_TW_PROD_RHO$,T>
		TWStress	= RDSRec<RDS_TW_PROD_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_PROD_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_RECLAIM_CNT$> NE '' THEN
	TWType = 'Reclaim'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_RECLAIM_CODE$>,@VM) + (RDSRec<RDS_TW_RECLAIM_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_RECLAIM_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_RECLAIM_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_RECLAIM_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_RECLAIM_CNT$,T>
		TWCon		= RDSRec<RDS_TW_RECLAIM_CON$,T>
		TWRes		= RDSRec<RDS_TW_RECLAIM_RES$,T>
		TWRho		= RDSRec<RDS_TW_RECLAIM_RHO$,T>
		TWStress	= RDSRec<RDS_TW_RECLAIM_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_RECLAIM_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

RETURN


* * * * * * *
LoadTWLine:
* * * * * * *

TWLine += 1

RDSTestRec<RDS_TEST_TW_TYPE$,TWLine>	= TWType
RDSTestRec<RDS_TEST_TW_TOOL$,TWLine>	= TWTool
RDSTestRec<RDS_TEST_TW_CODE$,TWLine>	= TWCode
RDSTestRec<RDS_TEST_TW_TRANS$,TWLine>	= TWTrans
RDSTestRec<RDS_TEST_TW_CNT$,TWLine>		= TWCnt
RDSTestRec<RDS_TEST_TW_CON$,TWLine>		= TWCon
RDSTestRec<RDS_TEST_TW_RES$,TWLine>		= TWRes
RDSTestRec<RDS_TEST_TW_RHO$,TWLine>		= TWRho
RDSTestRec<RDS_TEST_TW_STRESS$,TWLine>	= TWStress
RDSTestRec<RDS_TEST_TW_THICK$,TWLine>	= TWThick

RETURN



