// Filename:	GRIDDING.OGS
// Creation:	by Laurie A. Shea,  9/5/01
// Purpose:		Random XYZ Gridding tool

// Modifications Required:

// Create a new template so that the user can resave it for their plot?
// Keep calling [init] section yet my tool is modal???
// Add error message to OMG file
// Add error messages when calling the C functions
// My tool needs to be put in the German and Japanese versions

[OnOpen]
	CheckWksSelection;  //dialog will not open if a Z column is not selected
	dialog.domodal(Otools,gridding);
	return 0;


[OnInit]
 	Gridding!Method=1;  //set to Renka-Cline
	Gridding!Flag=1;    //controls whether the routine needs to run again
	
	Gridding!Dataset.v1$="%(%H,wks.c2)";	
	
	get %C -e end;			// get number of scatter points
	Gridding!NumCols.v1=int(sqrt(end));  // set grid x,y size accordingly
	Gridding!NumRows.v1=int(sqrt(end)); 
	
	Gridding!SR.v1=2;
	Gridding!Smooth.v1=.8;  

	Gridding!nq.v1=1;
	Gridding!nw.v1=1;
		
	Gridding!ShowPlot=1;

	return 0;

	
[OnOK]

	if(Gridding!Flag)
		run.section(,ConvertXYZ);
	
	return 0;
	
[ONCANCEL]

	if (exist(%H)==3)
	{
		win -cd %(1);
		win -cd %H;
	}
	else
		return 0;

	
[Apply.OnClick]
	
	Gridding!Flag=0;
	run.section(,ConvertXYZ);  

	return 0;

	
[ConvertXYZ]

	if(run.section(,SortInit)==1)
	{
		type -b $GriddingXYZ.ErrSort;
		win -cd %A; //close TempWks
		return 1;
	}
	
	switch (Gridding!Method.choice)
	{
		case 1 2:
			if (run.section(,ConvRCandS) != 0) return 1;
			break;
		case 3 4 5:
			run.section(,ConvRandom);
			break;
	}
	
	if(Gridding!ShowPlot)
	{		
		worksheet -p 242 ConvRandom;  //Create a 3D wire frame plot
		%A=Gridding!Dataset$;
		layer -i240 %A;
		layer -a;  //rescale
	}
	
	return 0;

		
[SortInit]

	if (exist(%H)==3)
	{
		win -cd %(1);
		win -cd %H;
	}
	else 
	if (exist(%H)==5)
		win -cd %H;
		
	%W = %H;  //store the name of the original worksheet
		
	create.wksName$="TempWks";
	create.enumwks=1;
	create.template$="CREATE";
	create.type1=4;
	create.type2=1;
	create.type3=6;
	create.wks(A B C);
	%A=create.wksname$;  //wks to hold sorted and duplicated results
	if(run.LoadOC("OriginLab\ConvertRandomXYZ.OCW")!=0)
	{
		//run.section(DBRoutines.OGS,ReportError,$OStatCommon.OStatCnotLoaded);
		type -b $GriddingXYZ.LoadGridErr;      
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	ii=RemoveDuplicates(%W,nXcol-1,nYcol-1,nZCol-1,%A);
	run.section(DBRoutines,RestoreAtOC);
	
	if(ii>0)
		type -b $GriddingXYZ.Duplicates;
	if(ii<0)
		return 1;  //break if the RemoveDuplicates function fails
	
	return 0;
 			
///////////////////////////////////////////////////////////////////////////
//
// These are the routines that we have always supported, but duplicates 
// will be replaced with the mean value.
//
///////////////////////////////////////////////////////////////////////////

[ConvRandom]
 	sum(%(%A,1));
	xmax=sum.max;
	xmin=sum.min;
	limit -r xmin xmax 8;
 	sum(%(%A,2));
	ymax=sum.max;
	ymin=sum.min;
	limit -r ymin ymax 8;
	mat.wksname$=%A;
	mat.xcol=1;
	mat.ycol=2;
	mat.zcol=3; 
	mat.Grid.nMatCols=Gridding!NumCols.v1;
	mat.Grid.nMatRows=Gridding!NumRows.v1;
	mat.Grid.radius=Gridding!SR.v1;
	mat.Grid.smooth=Gridding!Smooth.v1;
	checkvar GridMode 1;
	checkvar UseLog 0;
	val1=mat.Grid.nMatCols;
	val2=mat.Grid.nMatRows;
	limit -r xmin xmax (val1-1);
	run.section(,CreateMatrix);
	%M = %H; 
	mat.matname$=%M;
	matrix -ps D val1 val2; 
	matrix -ps x xmin xmax;
	matrix -ps y ymin ymax;
	if(GRIDDING!LOGSPACE) 
	{
		win -o %A {
			copy -x wcol(mat.zcol) dummy;
			wcol(mat.zcol)=ln(wcol(mat.zcol));
			#!wcol(A)=wcol(mat.zcol)
		}
	};
	mat.grid(check); 
	if(Gridding!Method.choice==5)
	{ 
		err=mat.grid(ave);
	}
	if(Gridding!Method.choice==4) 
	{
		mat.grid.average=1;	
		err=mat.grid(coo);
	} 
	if(Gridding!Method.choice==3)
	{
		mat.grid.average=0;	
		err=mat.grid(coo);
	}; 
	if(GRIDDING!LOGSPACE) 
	{
		win -o %A
		{
			copy dummy wcol(mat.zcol);
			(%M)=exp(%M);
			del dummy;
			#!type dummy is deleted.
		}
	}
	if(err)
		type -a $Wksheet.ConvRandGridError;
		
	win -cd %A;  //delete the temporary wks which held the sorted duplicated values	
		
	return 0;
		
	
////////////////////////////////////////////////////////////////////////
//
//  New NAG conversion routines supported:  Renka Cline and Shepard	
//
////////////////////////////////////////////////////////////////////////
	
[ConvRCandS]
	
	%W=%A;  //sorted wks
	create.wksName$="TempWks";
	create.enumwks=1;
	create.template$="CREATE";
	create.type1=4;
	create.type2=1;
	create.type3=6;
	create.wks(A B C);
	%A=create.wksname$;
	if(run.LoadOC("OriginLab\ConvertRandomXYZ.OCW")!=0)
	{
		//run.section(DBRoutines.OGS,ReportError,$OStatCommon.OStatCnotLoaded);
		type -b $GriddingXYZ.LoadGridErr;      
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	if(Gridding!Method.choice==1)
	{
		if(Grid2D(%W,%A,0) != 0) return 1; 
		
	}
	else
	{
		if (Grid2D(%W,%A,1,Gridding!nq.v1,Gridding!nw.v1) != 0) return 1;
	
	}

	run.section(DBRoutines,RestoreAtOC);
	win -cd %W;  //delete the sorted wks
	
	// Results from the NAG computation are in the temp worksheet 
	// We will use LabTalk commands to convert this worksheet to a matrix

	run.section(,CreateMatrix);
	
	mat.wksname$=%A;		// point to worksheet to be converted
	mat.xcol=1;				// point to x,y,z columns
	mat.ycol=2;
	mat.zcol=3;
	mat.matname$=%H;		// point to resulting matrix worksheet
	mat.xyz2m();			// perform conversion

	win -cd %A;  			//delete the wks used to convert regular data

	return 0;

		
[CreateMatrix]
	
	%B="Matrix";
	for(ii=1;1==1;ii++) 
	{
		if(exist(%B$(ii))!=5)
			break;
	};	

	%L = System.Matrix.DefTemplate$;
	win -T Matrix %L "%B$(ii)";
	
	page.title=3;
	%L=Gridding!Method$;
	%W=Gridding!Dataset.v1$;
	page.label$=$GriddingXYZ.RanMatrixLabel;	
	
	return 0;	

		
[Method.OnSelChange]
	
	if (Gridding!Method.choice>=3)
	{
		get %C -e end;			// get number of scatter points
		Gridding!NumCols.v1=int(sqrt(end));  // set grid x,y size accordingly
		Gridding!NumRows.v1=int(sqrt(end));  
	}
	
	Gridding!Flag=1;
	return 0;

	
[NumCols.OnEditChange]
	
	Gridding!Flag=1;
	return 0;
	
	
[NumRows.OnEditChange]
	
	Gridding!Flag=1;
	return 0;

	
[SR.OnEditChange]
	
	Gridding!Flag=1;
	return 0;

	
[Smooth.OnEditChange]
	
	Gridding!Flag=1;
	return 0;

	
[NQ.OnEditChange]
	
	Gridding!Flag=1;
	return 0;

	
[NW.OnEditChange]
	
	Gridding!Flag=1;
	return 0;

	
[RNQ.OnEditChange]
	
	Gridding!Flag=1;
	return 0;

	
[ShowPlot.OnClick]
	
	Gridding!Flag=1;
	return 0;

	
[LogSpace.OnClick]
	
	Gridding!Flag=1;
	return 0;	