//////////////////////////////////////////////////////////////////
// Filename:    PickPeak.ogs					//				
// Creation:    by Ree Migliozzi, 7/27/95  DLL by Marko Ledvij	//
// Purpose:     Peak picking					//
//////////////////////////////////////////////////////////////////
// Modifications:
//
// SDB 09/25/2003 ER7.5705 QA3875 USE_COPY_TO_FIND_NEG_PEAKS
//////////////////////////////////////////////////////////////////

[OnInit]
	PickPeak!Positive=1;
	PickPeak!Negative=1;
	PickPeak!Width.v1=5;
	PickPeak!Height.v1=5;
	PickPeak!MinHeight.v1=5;
	PickPeak!ShowCenter=1;
	PickPeak!ShowLabel=1;

[Initialize]
	/* Initialize                                                   */
	/* -------------------------------------------------------------*/
	if (curve.error==(0/0)) 
		return 1;					//Error! No DLL
	curve.reset();
	return 0;

[Find.OnClick]
	if (exist(%H)==3 && Layer.is3D==1)
	{
		type -b $general.2DGraphsOnly;
		return 1;
	}

	if(run.section(,Initialize)!=0)
	{
		type -b $pickpeak.dllLoadFailure;
		return 1;
	}

	

	if (run.section(,OptionsCk)==0) 
	{	

		numP=0; 	    	 
		numV=0;
		
		create Index -wd 10 Positive Negative;
		
		/* Find Peaks */	
		if(PickPeak!Positive==1) 
		{	
			/* type -b peaktype is peak; */
			curve.data$=%C;				// set data for peak search		
			curve.peakindex$ = Index_Positive;		//set output into Index_Positive
			run.section(,Pick);
			get Index_Positive -e numP;		//set number of peaks found
				
		}

		/* Find "Negative Peaks" */	
		if (PickPeak!Negative==1) 
                { 
			/* type -b peaktype is Negative; */
			copy %C NegData;			//create NegData and copy %C to it
			// SDB 09/25/2003 ER7.5705 QA3875 USE_COPY_TO_FIND_NEG_PEAKS
			NegData*=-1;
			curve.data$=NegData;				//set data for Negative search
			curve.peakindex$ = Index_Negative;	//set output
			run.section(,Pick);
			get Index_Negative -e numV;		//set number of Neg peaks found
			del NegData;
			curve.data$=%C;				//re-set data
		
		}
			
		/*total number of Positive and Negative peaks found */

		if((numP+numV)!=0)
		{
			run.section(,MakeWks);		
			run.section(,Fill);	
			run.section(,Display);
		}

	}
	else
	{
		type -b;				//BEEP!
		type -b $pickpeak.BadOptions;
		type -q "";				//Clear up Status Bar
		return 1;
	} 
		


[OptionsCk]
	/* check user settings                                          */
	/* -------------------------------------------------------------*/
	/* Check the users data selection and settings                  */
	if (exist(%H)==3)
	{
		if (%C=="")
			return 1;				// Return ERROR
		else
		{
			if(PickPeak!Width.v1<0 || PickPeak!Width.v1>100) 
			{
				type -b $pickpeak.widtherr;	// the minimum width must be between 0 and 100
				return 1;
			};
			if(PickPeak!Height.v1<0 || PickPeak!Height.v1>100 || PickPeak!MinHeight.v1<0 || PickPeak!MinHeight.v1>100) 
			{
				type -b $pickpeak.heighterr;			// maximum width must be between 0 and 50
				return 1;
			};
		
			curve.pickpeaks.rectwidth=PickPeak!Width.v1/100;
			curve.pickpeaks.rectheight=PickPeak!Height.v1/100;
			curve.pickpeaks.minheight=PickPeak!MinHeight.v1/100;
			
		}

		/* type -b Options OK;  */
		return 0;
	};
	
[MakeWks]
	/* type -b make wks; */
	%B=%C;	
	%A="";
	doc -e W {
		if(page.label$=="Peaks for %B")
		{
			%A=%H;
			Break;
		}
	}

	if (exist(%A)==2)
	{
		win -o %A
		{		
			create.wksName$=%A;
			create.enumwks=0;
			loop(ii,1,%A!wks.ncols)
			{
				del col(1);
			}
			if(page.extinfo==1)
			{
				type -b $Baseline.NoExcelWks;
				return 1;
			}
		}
	}
	else
	{
		create.wksName$="Peaks";
		create.enumwks=1;
	}
	create.wkslabel$="Peaks for %B";
	create.template$="CREATE";
	create.type1=2;
	create.type2=2;
	create.type3=4;
	create.type4=1;
	create.type5=5;
	create.type6=5;
	create.wks(PkIndex PkType Pkx Pky PosPkL NegPkL);
	%A=create.wksname$;
	return 0;

[Pick]
	/* type -b pick; */
	curve.pickpeaks(1);			// call DLL function...fills curve.peakindex$
[Fill]
	/* type -b fill; */

	/*fill %A_PkIndex*/
	for(i=1;i<=numP;i+=1)
	{
		%A_PkIndex[i] = Index_Positive[i]; 
	}
	for(i=1;i<=numV;i+=1)
	{
		%A_PkIndex[numP+i] = Index_Negative[i]; 
	}

	/* delete temporary data sets */
	del Index_Positive;
	del Index_Negative;

	/* Operate following script on window %A */
	win -o %A					
	{
		for(i=1;i<=(numP+numV);i+=1)
	 	{
	
	 		%B=curve.data$;				// %B is current active dataset
			index=%A_pkindex[i];			// get the peak index	
			%A_Pkx[i]=xvalue(index,%B);		// Set the corresponding x value 
			%A_Pky[i]=%B[index];			// Set the corresponding y value
			%B=($(col(Pkx)[i]));	
	

			if(i<=numP)
			{
				col(PosPkL)[i]$=%B;		// Set the corresponding label
				%A_PkType[i]$=POS;
			}
			else
			{
				col(NegPkL)[i]$=%B;		// Set the corresponding label
				%A_PkType[i]$=NEG;
			}
		}
	
	}; 

	del -v numP;
	del -v numV;

[Display]

	/* type -b display; */

	layer -e %A_Pky;
	layer -e %A_PosPkL;

	if(PickPeak!ShowCenter)
	{
		%B=%A_Pky;			
		layer -i201 %B;				//add center markers to the graph
		set %B -c 2;				//Set the color to red
		set %B -k 6;				//set the data point symbol
		set %B -z 8;				//point size to 15	
		set %B -l 0;				//line connection to none
	} 

	if(PickPeak!ShowLabel)
	{
		%B=%A_PosPkL;				//add peak labels to the graph
		layer -i %B;
		set %B -ty 70;				//Offset in the y direction by 70 percent
		set %B -tj 1;				//Left Alignment
		set %B -c 1;
		set %B -tf font(Arial);

		%B=%A_NegPkL;				//add peak labels to the graph
		layer -i %B;
		set %B -ty -70;				//Offset in the y direction by 70 percent
		set %B -tj 1;				//Left Alignment
		set %B -c 1;
		set %B -tf font(Arial);
	} 
	//Remove empty label columns
	if(PickPeak!Positive==1 && PickPeak!Negative==0)
		delete %A_NegPkl;
	if(PickPeak!Positive==0 && PickPeak!Negative==1)
		delete %A_PosPkl;

[Positive.OnClick]
	PickPeak!Negative=PickPeak!Positive==1?PickPeak!Negative:1;


[Negative.OnClick]
	PickPeak!Positive=PickPeak!Negative==1?PickPeak!Positive:1;
