////////////////////////////////////////////////////////////////////////////////
// File:	Profiler.OGS
// Purpose:	Create and plot horizontal, vertical, and line segment profiles
// Author:	Joseph M. Przechocki
// Date:	August 9, 2001
// Updated:
//		JMP 11/10/01 Entire file re-written to work with updated 7.0 template
//		GRD 2002.01.22	Preserve user's SYSTEM.MATH.ANGULARUNITS
//		ML 3/20/2002 QA70-1173 CHECKING_IF_CLOSING_ALL_WINDOWS
//		SDB: 9/5/2003 ADD_MENU_FOR_CONTOUR new menu to plot profile with Contour
//		Annia:9/20/2003 QA70-5210 ADD_ZLEVEL_LINE
//		SDB: 9/29/2003 REMOVE_Z_LEVEL_IMAGE Do not add Z level to Image Profile
//		SDB: 10/3/2003 QA5280 @V=7.5712 CHECK_WINDOW_CLOSE_HIDE
//		SY: 01-13-2004 QA70-5381 v7.0800 SAVE_AS_WORKSPACE_SHOULD_NOT_REMOVE_FILES_IN_TEMP_FOLDER
////////////////////////////////////////////////////////////////////////////////


[main]
	if (run.section(,CurrentMatrix, %1))
		return 1;
	/* if (run.section(,SelectMatrix))
		return 1;
	*/	
	
	run.section(,CreatePlot, %1);
	run.section(,CreateWks);
	
	run.section(,Setup,(%M) %1);
	run.section(,SwitchCursors);
	
	return 0;

[SwitchCursors]

	%A=radio.l3$;
	clearworksheet %A;						//Clear Horizontal worksheet
	
	%A=radio.l4$;
	clearworksheet %A;						//Clear Vertical worksheet
	
		
	switch (radio.choice)
	{
	case 1:
		sline.show=0;
		hline.show=1;
		vline.show=1;
		intersect.show=1;
	
		hline.run();
		vline.run();
		break;
	case 2:
		hline.show=0;
		h_readout.show=0;	
	
		vline.show=0;
		v_readout.show=0;
		
		intersect.show=0;
		
		sline.show=1;
		sline.run();
		break;
	
	};
	return 0;

[CurrentMatrix]
	if (exist(%H)!=5)
	{
		type -b;							//BEEP!
		type -b "Error! Must select a matrix.";
		return 1;
	};
	%M=%H;
	matrix -id;
	if(!%1)
	{
		run.section(,SetMatrixToImage);
	}
	return 0;

[SetMatrixToImage]
	matrix -ii;	//show as image
	return 0;
	
////////////////////////////////////////////////////////////////////////////////
// This section is not used by default
// User can change script so that instead of plotting active matrix, the user is prompted
////////////////////////////////////////////////////////////////////////////////
[SelectMatrix]
	ii=1;%Z="";
	doc -e M {%Z=%Z %H};
	getnumber (Matrix) ii:Z (Select Data);
	%M=%[%Z,#ii];					//Set %M to matrix name
	return 0;
	
[CreatePlot]
	///SDB: 9/5/2003 ADD_MENU_FOR_CONTOUR
	///use contour profiles template if menu item was selected
	if(%1)
		getenumwin Profile2;
	else
		getenumwin Profiles;
	%W=%H;						//Set %W to be Graph window name
	return 0;

[Setup]	
	limit (%1);
	//---- CPY 9/28/03 PROFILE_PLOT_Z_LEVEL_CONNECT_NOT_SEG_2
	// move this to later
	/*
	//Setup non-linked axes for child layers
	layer -o 3
	{
		layer -i %(%B,2);
		layer.y.from=limit.ymin;
		layer.y.to=limit.ymax;
	};
	layer -o 2
	{
		layer -i %(%A,2);
		layer.y.from=limit.ymin;
		layer.y.to=limit.ymax;
	};
	*/
	//----
	///SDB: 9/5/2003 ADD_MENU_FOR_CONTOUR
	///use id for contour if selected 
	
	if(%2)
	{
		layer -o 1
		{
			layer -i226 %M;
			layer -a;
			///Annia: 9/20/2003 QA70-5210 ADD_ZLEVEL_LINE
			///add Z-level lines into X-Z and Y-Z layers.
			///SDB: 9/29/2003 REMOVE_Z_LEVEL_IMAGE Do not add Z level to Image Profile
			err = run.LoadOC(originlab\ProfilePlot);
			if(0==err)
   				ProfilePlotSetup %H;
				///End ADD_ZLEVEL_LINE
		}
	}
	else
		layer -o 1 {layer -i220 %M;layer -a}
	
	//---- CPY 9/28/03 PROFILE_PLOT_Z_LEVEL_CONNECT_NOT_SEG_2
	layer -o 3
	{
		layer -i %(%B,2);
		layer.y.from=limit.ymin;
		layer.y.to=limit.ymax;
	};
	layer -o 2
	{
		layer -i %(%A,2);
		layer.y.from=limit.ymin;
		layer.y.to=limit.ymax;
	};
	//----
	
	//Setup Layer 1 objects, only if desired to be the same all the time, otherwise the template will dictate cursor line location
	/* 
	layer -o 1 
	{
		hline.y=(layer.y.to-layer.y.from)/3+layer.y.from;
	
		vline.x=(layer.x.to-layer.x.from)/3+layer.x.from;
		
		sline.x1=layer.x.from;								//Initialize segment cursor position
		sline.x2=layer.x.to;
		sline.y1=layer.y.from;
		sline.y2=layer.y.to;
	};
	*/

[CreateWks]
	////////////////////////////////////////////////////////////////////////////////
	// Assume Profiler Graph is already created
	// Store names of related worksheets into the hidden values of the radio object
	////////////////////////////////////////////////////////////////////////////////
	
	create.template$="CREATE";
	create.npts=30;
	create.enumwks=1;				//Enumerate the worksheet
	create.numtypes=2;			
	create.type1=4;				//Only explicitly specify 1st type as X
	create.type2=1;				//Set Type2=Y column type

	create.wksLabel$="";
	create.wksname$="Horizontal";
	create.wks(A B);
	%W!radio.l3$=create.wksName$;
	%A=create.wksName$;			//Store for later Setup section use
	
	create.wksLabel$="";
	create.wksname$="Vertical";
	create.wks(A B);
	%W!radio.l4$=create.wksName$;
	%B=create.wksName$;			//Store for later Setup section use
	
	return 0;

[horizontal]
	SYSMATHANGUNIT = SYSTEM.MATH.ANGULARUNITS;
	SYSTEM.MATH.ANGULARUNITS=0;
	
	mat.reset();
	
	mat.matname$=%(1,@D);
	
	%A=radio.l3$;
	clearworksheet %A;
	
	mat.profile.hwksname$=%A;
	mat.profile.hxcol=1;
	mat.profile.hzcol=2;
	
	mat.profile(this.x,this.y);
	
	h_readout.show=1;
	h_readout.v1=this.y;
	h_readout.y=this.y;
	h_readout.x=layer.x.from-1.2*abs(h_readout.dx/2);	//use correction factor of 1.2 for placement
	
	intersect.v1= $(%(%A,2)(vline.x));
	
	SYSTEM.MATH.ANGULARUNITS = SYSMATHANGUNIT;
	return 0;

[vertical]
	SYSMATHANGUNIT = SYSTEM.MATH.ANGULARUNITS;
	SYSTEM.MATH.ANGULARUNITS=0;
		
	mat.reset();
	mat.matname$=%(1,@D);
	
	%A=radio.l4$;
	clearworksheet %A;

	mat.profile.vwksname$=%A;
	mat.profile.vycol=1;
	mat.profile.vzcol=2;
	
	mat.profile(this.x,this.y);
	
	v_readout.show=1;
	v_readout.v1=this.x;
	v_readout.x=this.x;
	v_readout.y=layer.y.from-0.7*abs(v_readout.dy/2);	//use correction factor of 0.7 for placement
	
	intersect.v1= $(%(%A,2)(hline.y));
	
	SYSTEM.MATH.ANGULARUNITS = SYSMATHANGUNIT;
	return 0;

[section]
	SYSMATHANGUNIT = SYSTEM.MATH.ANGULARUNITS;
	SYSTEM.MATH.ANGULARUNITS=0;
	
	mat.reset();
	mat.matname$=%(1,@D);
	
	%A=radio.l3$;
	clearworksheet %A;

	mat.wksname$=%A;
	mat.xcol=1;
	mat.zcol=2;
	
	mat.profile.angle=atan(this.dy/this.dx);
	win -o %(1,@D)
	{
		matrix -pg X xmin xmax;
		matrix -pg Y ymin ymax;
	}
	this.x1=this.x1<xmin?xmin:this.x1;
	this.x1=this.x1>xmax?xmax:this.x1;
	this.x2=this.x2<xmin?xmin:this.x2;
	this.x2=this.x2>xmax?xmax:this.x2;
	this.y1=this.y1<ymin?ymin:this.y1;
	this.y1=this.y1>ymax?ymax:this.y1;
	this.y2=this.y2<ymin?ymin:this.y2;
	this.y2=this.y2>ymax?ymax:this.y2;
	mat.segment(this.x1,this.y1,this.x2,this.y2);
	
	mat.matname$=%(1,@D);

	%A=radio.l4$;
	clearworksheet %A;

	mat.wksname$=%A;
	mat.ycol=1;
	mat.zcol=2;
	mat.segment(this.x1,this.y1,this.x2,this.y2);
	
	SYSTEM.MATH.ANGULARUNITS = SYSMATHANGUNIT;
	delete -v xmin;
	delete -v xmax;
	delete -v ymin;
	delete -v ymax;
	return 0;

[WinClose]
	///SDB: 10/3/2003 QA5280 @V=7.5712 CHECK_WINDOW_CLOSE_HIDE
	///do not delete the data if window is hidden
	if(@FS == 0)
	{
		%A=radio.l3$;							//Get name for Horiztonal worksheet
		//if exist(%A)==2
		if (0 == @CW)							/// ML 3/20/2002 QA70-1173 CHECKING_IF_CLOSING_ALL_WINDOWS
			run.section(,WinDelete,%A);			//Delete it
		
		%A=radio.l4$;							//Get name for Vertical worksheet
		//if exist(%A)==2
		if (0 == @CW)							/// ML 3/20/2002 QA70-1173 CHECKING_IF_CLOSING_ALL_WINDOWS
			run.section(,WinDelete,%A);			//Deleted it
	
		///Annia: 9/20/2003 QA70-5210 ADD_ZLEVEL_LINE
		///delete worksheet containing Z-level values when the profile graph is deleted or closed.
		/// SY 01-13-2004 QA70-5381 v7.0800 SAVE_AS_WORKSPACE_SHOULD_NOT_REMOVE_FILES_IN_TEMP_FOLDER
		if( 0 == @CW )	// if not closing all windows	
		/// end SAVE_AS_WORKSPACE_SHOULD_NOT_REMOVE_FILES_IN_TEMP_FOLDER
		{
			err = run.LoadOC(originlab\ProfilePlot);
			if(0==err)
	   			ProfilePlotOnClose %H;
		}
		///End ADD_ZLEVEL_LINE
	}
	return 0;

[WinDelete]
	win -o %1
	{
		win -ca %1;
	}
	return 0;

[edit_readout]
	if (this.v1>layer.%1.to)
		this.v1=layer.%1.to;
	if (this.v1<layer.%1.from)
		this.v1=layer.%1.from;
	if (%1=="y")
		hline.y=this.v1;
	if (%1=="x")
		vline.x=this.v1;

///Annia: 9/20/2003 QA70-5210 ADD_ZLEVEL_LINE
///update Z-level lines when level value chabges.
[onZlevelChange]
	// delay execution of following labtalk script
	;{
		err = run.LoadOC(originlab\ProfilePlot);
		if(0==err)
	   		ProfilePlotUpdate %H;
	}
///End ADD_ZLEVEL_LINE
