
// example for reading a bunch of data 
[Main]

path$=system.path.program$ + "Samples\Data Manipulation\";
fn$="tcal3";// base name of our files that will be enumerated later
// we will later put the raw data into a sub folder
pe_cd /; // goto root path in Project Explorer
// create new book with no sheet so we can add later
newbook s:=0 result:=bkn$;
//@XFR=0;
// import file into new book
newsheet bkn$ "" result:=rawDataSheet$;
filename$= path$ + fn$ + ".dsc";
impfile filename$ dst:=rawDataSheet$;
pe_mkdir RawData;
pe_move bkn$ /RawData;// move into subfolder
//@XFR=1;

// find the custom path
cd result:=cwd$;
aa$=rawDataSheet$;
aa$+="(1,2)";
//aa$=;
%A=cwd$;
run.section(%Apfm.ogs,PFMFindPeaks, "aa$" );




//////////////////////////////////////////////////////
// For given wks XYdata range, 
// 1. construct automatic baseline
// 2. subtract baseline from data
// 3. find peaks
// 4. create peak plot for fitting
// 5. change the peak func with peak id
// 6. call nlsf dlg and ready for fit the whold curve
// The following wkbooks and Sheets are used
// [PkBaseline]
// each sheet will be the name of source data column
// (1,2) will be the baseline, (3,4) will be the subtracted data
// [PkInfo]
// only one sheet called "Peaks" with the following columns
// 1=source data range
// 2=
/////////////////////////////////////////////////////////
[PFMFindPeaks]
//	type "arg =%1";
sr$ = %1;// source XYRange, assume to be coming from a wks
getXYName yi:=sr$ yname:=yn$;

// all baseline related XF requires a plot, so we need to plot it first
win -t plot; pltbk$=%H;
plotxy sr$ plot:=200;
// next to create XYRange string for the plot and plot baseline on the same graph
blauto yi:=[pltbk$]!1 yo:=[PkBaseline]yn$!(1,2) method:=2;

// create a new plot to show raw data and baseline as they will be replaced by
// subtracted data later by the blsubtract function
win -t plot; spltbk$=%H;
plotxy sr$ plot:=200;
plotxy [PkBaseline]yn$!(1,2) plot:=200;

//subtract the baseline form the original dataplot, delete the baseline plot
blsubtract yi:=[pltbk$]!1 yo:=[PkBaseline]yn$!(3,4) bl:=2;

pkfind1 yi:=[pltbk$]!1 dir:=2 num:=3 result:=[PkInfo]Peak!;

pkcreate yi:=[pltbk$]!1 dir:=2 minp:=5 result:=[PkInfo]FitEachPeak!; 

pkfunc func:=7 pid:=3;//change the third peak's name to Lorentzian

return;