////////////////////////////////////////////////////////////////////////////////
// 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
// EJP 2006-04-10 v8.0387 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
// CPY 5/12/06 MULTI_SHEET_MULTI_MAT_OBJ_SUPPORT
// EJP 2006-12-08 v8.0520 QA70-5388 DO_NOT_CHANGE_MATRIX_VIEW_MODE
// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
// EJP 2007-10-09 v8.0721 QA70-10298 INIT_ARBITRARY_LINE_TO_CENTER_OF_PROFILE_PLOT
// EJP 2008-02-26 v8.013 QA80-10944 AVOID_LABTALK_EXPRESSION_ERR
// EJP 2009-01-06 v8.0995 QA80-12863 PREVENT_UNDEFINED_VAR_ERROR
// EJP 2009-11-25 QA81-14728 PROFILE_PLOTS_NOT_UPDATING
// Kyle 05/18/2010 ORG-98 NEW_IMAGE_PROFILE_IN_85
////////////////////////////////////////////////////////////////////////////////


///Kyle 05/18/2010 ORG-98 NEW_IMAGE_PROFILE_IN_85
[main85]
	if( Exist(%H)==3 )	// graph
	{
		return 1;
	}

	//int nContourPlot = %1;
	int nContourPlot = 0;
	if(macro.narg >= 1)
		nContourPlot = %1;

	if( nContourPlot )
		run.section(Plot3D,ContourColor);
	else
	{
		matrix -ig;	///Sophy 5/31/2010 ORG-98-S11 REMINDER_MESSAGE_TO_CONVERT_GRAY_ON_IMAGE_PROFILE
		run.section(Plot3D,ImagePlot);
	}

	if( Exist(%H) == 3 )	// graph
	{
		imageprofile layout:=0 protect:=0;
	}
///End NEW_IMAGE_PROFILE_IN_85



[main]
//--- CPY 5/12/06 MULTI_SHEET_MULTI_MAT_OBJ_SUPPORT
// this ogs is run as
// profile.main
// profile.main 1
// so %1 can be empty of 1
//%A="%1";
//%A=;
//string strArg1 = "%1";
//---
	/// EJP 2006-04-10 v8.0387 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	run.LoadOC(OriginLab\image_utils.c);
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE

	/// EJP 2006-12-08 v8.0520 QA70-5388 DO_NOT_CHANGE_MATRIX_VIEW_MODE
	///if (run.section(,CurrentMatrix, %1))
	if (run.section(,CurrentMatrix, 1)) // non-zero to keep current view mode
	/// end DO_NOT_CHANGE_MATRIX_VIEW_MODE
		return 1;
	
	run.section(,CreatePlot, %1);
	run.section(,CreateWks);
	
	run.section(,Setup,(%M) %1);
	win -o %W {
		layer -o 1 {
			run.section(,SwitchCursors);
		}
	}
	
	/// EJP 2007-10-09 v8.0721 QA70-10298 INIT_ARBITRARY_LINE_TO_CENTER_OF_PROFILE_PLOT
	win -o %M {matrix -pg X xmin xmax; matrix -pg Y ymin ymax;}
	offset=(xmax-xmin)/4; sline.x1=xmin+offset; sline.x2=xmax-offset;
	offset=(ymax-ymin)/4; sline.y1=ymin+offset; sline.y2=ymax-offset;
	delete -v xmin;	delete -v xmax;
	delete -v ymin; delete -v ymax;
	delete -v offset;
	/// end INIT_ARBITRARY_LINE_TO_CENTER_OF_PROFILE_PLOT

	return 0;

[SwitchCursors]
	/// EJP 2009-11-25 QA81-14728 PROFILE_PLOTS_NOT_UPDATING
	curActiveLayer = page.active;
	page.active = 1; // activate layer 1
	/// end PROFILE_PLOTS_NOT_UPDATING
	
	%A=page.info.profiler.wks.Horz$;//radio.l3$;
	clearworksheet %A;						//Clear Horizontal worksheet
	
	%A=page.info.profiler.wks.Vert$;//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;
	
	};

	/// EJP 2009-11-25 QA81-14728 PROFILE_PLOTS_NOT_UPDATING
	page.active = curActiveLayer;
	/// end PROFILE_PLOTS_NOT_UPDATING

	return 0;

[CurrentMatrix]
	if (exist(%H)!=5)
	{
		type -b;							//BEEP!
		type -b "Error! Must select a matrix.";
		return 1;
	};
	%M=%H;
	/// EJP 2006-04-10 v8.0387 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	///matrix -id;
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE
	if(!%1)
	{
		run.section(,SetMatrixToImage);
	}
	/// EJP 2006-04-10 v8.0387 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	matrix -ig;
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE
	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
	//----- CPY 12/4/06 USE_PAGE_INFO_TO_STORE_WKS_NAME_INSTEAD_OF_HIDDEN_LABEL
	//page.info.profiler.wks
	page.info.add(profiler);
	page.info.profiler.addsection(wks);
	//-----
	return 0;

[Setup]	
//--- CPY 5/12/06 MULTI_SHEET_MULTI_MAT_OBJ_SUPPORT
//	limit (%1);
// %1=MatrixWindow name
// %2=1 if contour, empty if plot as image
string strM$ = "%1"; /// EJP 2009-11-25 QA81-14728 PROFILE_PLOTS_NOT_UPDATING, added $ to fix J cmd err
int isContour = $("%2"); // caller passed in %1 into 2nd arg
range [%1] mm = <active>;
//
	//--- CPY 5/12/06 MULTI_SHEET_MULTI_MAT_OBJ_SUPPORT
	// I have updated other codes in this section without marking, mainly to use mm to replace %M etc
	limit mm;

	///SDB: 9/5/2003 ADD_MENU_FOR_CONTOUR
	///use id for contour if selected 
	
	if(isContour > 0)
	{
		layer -o 1
		{
			layer -i226 mm;
			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 mm;layer -a}
	
	//---- CPY 9/28/03 PROFILE_PLOT_Z_LEVEL_CONNECT_NOT_SEG_2
	%A=page.info.profiler.wks.Horz$;
	%B=page.info.profiler.wks.Vert$;
	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 page.info.profiler.wks.Horz, and Vert variables
	////////////////////////////////////////////////////////////////////////////////
	
	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$;
	%W!page.info.profiler.wks.Horz$=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
	%W!page.info.profiler.wks.Vert$=create.wksName$;
	
	return 0;

[horizontal]

/// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
/// Move "horizontal" section to "horizontal_update" to allow call from "edit_readout" section.
	%A=radio.l3$;
	run.section(, horizontal_update, $(this.y));
	return 0;

[horizontal8]
	%A=page.info.profiler.wks.Horz$;
	run.section(, horizontal_update, $(this.y));
	return 0;

[horizontal_update]
/// end UPDATE_DISPLAY_ON_EDIT_VALUE

//	SYSMATHANGUNIT = SYSTEM.MATH.ANGULARUNITS;
//	SYSTEM.MATH.ANGULARUNITS=0;
	
	mat.reset();

	mat.matname$=%(1,@D);
	
	/// EJP 2006-05-04 v8.0405 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	/*
	%A=page.info.profiler.wks.Horz$;//radio.l3$;
	clearworksheet %A;
	mat.profile.hwksname$=%A;
	mat.profile.hxcol=1;
	mat.profile.hzcol=2;

	mat.profile(this.x,this.y);
	*/
	clearworksheet %A;
	%Z = %(1,@D);
	run.LoadOC(OriginLab\image_utils.c); /// EJP 2007-08-08 v8.0678 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	profiler(1, %Z, hline.x1, hline.y1, hline.x2, hline.y2, %A, 0, 1);
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE

	h_readout.show=1;

	/// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	///h_readout.v1=this.y;
	///h_readout.y=this.y;
	h_readout.v1=%1;
	h_readout.y=%1;
	/// end UPDATE_DISPLAY_ON_EDIT_VALUE

	h_readout.x=layer.x.from-1.2*abs(h_readout.dx/2);	//use correction factor of 1.2 for placement

	if( %(%A,2)(vline.x) != 0/0 ) /// EJP 2008-02-26 v8.013 QA80-10944 AVOID_LABTALK_EXPRESSION_ERR
		intersect.v1= $(%(%A,2)(vline.x));

//	SYSTEM.MATH.ANGULARUNITS = SYSMATHANGUNIT;
	return 0;

[vertical]

/// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
/// Move "vertical" section to "vertical_update" to allow call from "edit_readout" section.
	%A=radio.l4$;
	run.section(, vertical_update, $(this.x));
	return 0;

[vertical8]
	%A=page.info.profiler.wks.Vert$;
	run.section(, vertical_update, $(this.x));
	return 0;

[vertical_update]
/// end UPDATE_DISPLAY_ON_EDIT_VALUE

//	SYSMATHANGUNIT = SYSTEM.MATH.ANGULARUNITS;
//	SYSTEM.MATH.ANGULARUNITS=0;
		
	mat.reset();
	mat.matname$=%(1,@D);

	/// EJP 2006-05-04 v8.0405 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	/*
	%A=page.info.profiler.wks.Vert$;//radio.l4$;
	clearworksheet %A;
	mat.profile.vwksname$=%A;
	mat.profile.vycol=1;
	mat.profile.vzcol=2;
	
	mat.profile(this.x,this.y);
	*/
	clearworksheet %A;
	%Z = %(1,@D);
	run.LoadOC(OriginLab\image_utils.c); /// EJP 2007-08-08 v8.0678 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	profiler(0, %Z, vline.x1, vline.y1, vline.x2, vline.y2, %A, 0, 1);
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE
	
	v_readout.show=1;

	/// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	///v_readout.v1=this.x;
	///v_readout.x=this.x;
	v_readout.v1=%1;
	v_readout.x=%1;
	/// end UPDATE_DISPLAY_ON_EDIT_VALUE

	v_readout.y=layer.y.from-0.7*abs(v_readout.dy/2);	//use correction factor of 0.7 for placement
	
	if( %(%A,2)(hline.y) != 0/0 ) /// EJP 2008-02-26 v8.013 QA80-10944 AVOID_LABTALK_EXPRESSION_ERR
		intersect.v1= $(%(%A,2)(hline.y));
	
//	SYSTEM.MATH.ANGULARUNITS = SYSMATHANGUNIT;
	return 0;

[section]
	SYSMATHANGUNIT = SYSTEM.MATH.ANGULARUNITS;
	SYSTEM.MATH.ANGULARUNITS=0;

	/// EJP 2006-05-04 v8.0405 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	/*
	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)
	*/
	win -o %(1,@W)
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE
	{
		matrix -pg X xmin xmax;
		matrix -pg Y ymin ymax;
	}

	/// EJP 2009-01-06 v8.0995 QA80-12863 PREVENT_UNDEFINED_VAR_ERROR
	// If the variables are not defined then the win -o script did not run.
	if(xmin==0/0) 
		return 1;
	/// end PREVENT_UNDEFINED_VAR_ERROR

	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;

	/// EJP 2006-05-04 v8.0405 QA70-8187 GET_DATA_VALUES_DIRECT_FROM_IMAGE
	/*
	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);
	*/
	%Z = %(1,@D);
	run.LoadOC(OriginLab\image_utils.c); /// EJP 2007-08-08 v8.0678 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	
	// Horizontal profile
	%A=page.info.profiler.wks.Horz$;//radio.l3$;
	clearworksheet %A;
	profiler(1, %Z, this.x1, this.y1, this.x2, this.y2, %A, 0, 1);
	
	// Vertical profile
	%A=page.info.profiler.wks.Vert$;//radio.l4$;
	clearworksheet %A;
	profiler(0, %Z, this.x1, this.y1, this.x2, this.y2, %A, 0, 1);
	/// end GET_DATA_VALUES_DIRECT_FROM_IMAGE
	
	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=page.info.profiler.wks.Horz$;//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=page.info.profiler.wks.Vert$;//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]
/// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	if(%1=="y")
		%A=radio.l3$;
	else if(%1=="x")
		%A=radio.l4$;
	run.section(, edit_readout_update, %1);
	return 0;

[edit_readout8]
	if(%1=="y")
		%A=page.info.profiler.wks.Horz$;
	else if(%1=="x")
		%A=page.info.profiler.wks.Vert$;
	run.section(, edit_readout_update, %1);
	return 0;

[edit_readout_update]
/// end UPDATE_DISPLAY_ON_EDIT_VALUE
	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;
		run.section(, horizontal_update, $(this.v1)); /// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	}
	if (%1=="x")
	{
		vline.x=this.v1;
		run.section(, vertical_update, $(this.v1)); /// EJP 2007-07-31 v8.0672 QA70-10129 UPDATE_DISPLAY_ON_EDIT_VALUE
	}

///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
