// Filename:	wksAnaly.ogs
// Creation:	by CP Yang, 7/27/95
// Purpose:	Worksheet Analysis Menu scripts
//**********************************************
// Modification Log:
//		11/12/96 RKM    Took out [StatCol] [StatRow] [Freqcount] [Sort]
//				and [Normalize]  These are in wks.ogs and should
//				be using Context Sensitive menu id.
//		05/29/97 JMP	Implement Correlation: Cross Correlation if 2 columns, Auto if 1 column
//**********************************************




[Convolute]
	//SDB Added Novice dialog
	ini.get.filename$="Origin.ini";
	if(ini.OptionalPrompt.$(menu.lastexeid)!=0)
	{
		ii=0;
		getn
		($Deconvolution.PromptMessege) ii:2s
		($Deconvolution.SymmetricalData);
		if(ii==1)
			ini.OptionalPrompt.$(menu.lastexeid)=0;
	}

	if (run.section(,ConvSelectionCK)!=0)
		return 1;
	wks.multiX=1;					//Enable Multiple X columns
	wks.AddCol(Index);
	wks.col$(wks.ncols).type=4;					//Set as X
	fft.conv.indexdata$=%(%H,wks.ncols);
	wks.AddCol(Conv);
	wks.col$(wks.ncols).type=1;					//Set as Y
	fft.conv.convdata$=%(%H,wks.ncols);

	if (fft.conv(%(%H,wks.c1),%(%H,wks.c1+%(%H,@O,1)))!=0)
	{
		doc -uw;				//Refresh Worksheet
		type -b;
		type -b $General.BadCalculation;
	}
	set %H -ue;
	doc -uw;
	run.section(,ConvCleanUp);
	return 0;
	
[Deconvolute]
	//SDB Added Novice dialog
	ini.get.filename$="Origin.ini";
	if(ini.OptionalPrompt.$(menu.lastexeid)!=0)
	{
		ii=0;
		getn
		($Deconvolution.PromptMessege) ii:2s
		($Deconvolution.SymmetricalData);
		if(ii==1)
			ini.OptionalPrompt.$(menu.lastexeid)=0;
	}

	if (run.section(,ConvSelectionCK)!=0)
		return 1;
	wks.multiX=1;					//Enable Multiple X columns
	wks.AddCol(Index);
	wks.col$(wks.ncols).type=4;					//Set as X
	fft.deconv.indexdata$=%(%H,wks.ncols);
	wks.AddCol(Deconv);
	wks.col$(wks.ncols).type=1;					//Set as Y
	fft.deconv.deconvdata$=%(%H,wks.ncols);

	if (fft.deconv(%(%H,wks.c1),%(%H,wks.c1+%(%H,@O,1)))!=0)
	{
		doc -uw;				//Refresh Worksheet
		type -b;
		type -b $General.BadCalculation;
	}
	set %H -ue;
	doc -uw;
	run.section(,ConvCleanUp);
	return 0;

[ConvSelectionCK]
	////////////////////////////////////////////////////////////////////
	// Two columns must be selected
	////////////////////////////////////////////////////////////////////
	if ((wks.sel&2==2) && (wks.iscolsel()==2))
	{
		////////////////////////////////////////////////////////////
		// Count rows in Signal dataset
		////////////////////////////////////////////////////////////
		get %(%H,wks.c1) -e SigRows;

		////////////////////////////////////////////////////////////
		// Count rows in Response dataset
		////////////////////////////////////////////////////////////
		get %(%H,wks.c1+%(%H,@O,1)) -e RespRows;

		////////////////////////////////////////////////////////////
		// Response dataset size must be odd and less than half of signal dataset
		////////////////////////////////////////////////////////////
		if((int(RespRows)>SigRows/2) || (int(RespRows/2)==RespRows/2)) 
		{
			run.section(,ConvError);
			return 1;
		}
	}
	else
	{
		type -b;
		type -b $general.Only2Columns;
		return 1;
	}
	return 0;


[ConvCleanUP]
	delete -v SigRows;
	delete -v RespRows;
	return 0;


[ConvError]
	%A=%(%H,wks.c1);
	%B=%(%H,wks.c1+%(%H,@O,1));
	if (int(RespRows/2)==RespRows/2) 
		type -b $Deconvolution.DataNotOdd;
	if (int(SigRows/2)<RespRows)
		type -b $Deconvolution.ResponseTooBig;
	return 0;

[Correlate]
	if (run.section(,CorrSelectionCK)!=0)
		return 1;
	wks.multiX=1;					//Enable Multiple X columns
	wks.AddCol(Lag);
	wks.col$(wks.ncols).type=4;					//Set as X
	fft.corr.lagdata$=%(%H,wks.ncols);
	wks.AddCol(Corr);
	wks.col$(wks.ncols).type=1;					//Set as Y
	fft.corr.corrdata$=%(%H,wks.ncols);
	switch(wks.iscolsel())
	{
		case 1:
			fft.corr.poslag=1;
			if (fft.corr(%(%H,wks.c1),%(%H,wks.c1))!=0)
			{
				doc -uw;				//Refresh Worksheet
				type -b;
				type -b $General.BadCalculation;
			}
			break;
		case 2:
			fft.corr.poslag=0;
			if (fft.corr(%(%H,wks.c1),%(%H,wks.c1+%(%H,@O,1)))!=0)
			{
				doc -uw;				//Refresh Worksheet
				type -b;
				type -b $General.BadCalculation;
			}
			break;
	}			
	set %H -ue;
	doc -uw;
	return 0;

[CorrSelectionCk]
	////////////////////////////////////////////////////////////////////
	// Two columns must be selected or 1 Y column for Autocorrelation
	////////////////////////////////////////////////////////////////////
	if ((wks.sel&2==2) && (wks.iscolsel()<=2)) 
		return 0;			
	else
	{
		type -b;
		type -b $general.Only2Columns;
		return 1;
	}

[SFFT]
	if(run.section(DBRoutines.OGS,LoadSFFTWorkspace))      // Build SFFT OC Workspace
		return 1;                                          // If not successful return error code
	else                                                   // Else continue...
	{
		run.section(DBRoutines.OGS,SaveAndSetAtOC);        // Enable calling OC functions from LabTalk
		SFFT();                                            // Launch SFFT tool
		run.section(DBRoutines.OGS,RestoreAtOC);           // Restore original @OC setting 
	}
	
	return 0;

