#pragma TextEncoding = "UTF-8" #pragma rtGlobals=3 // Use modern global access method and strict wave access. #pragma moduleName=ScaleSub #pragma IgorVersion=7 #pragma version=1.0 #include // by__tony.withers@uwo.ca // GUI for subtraction of a reference wave scaled by some user-defined factor Menu "Macros" submenu "IR and Raman" "Wave Scale and Subtract", /Q, ScaleSub#MakeScaleSubPanel() end end static function MakeScaleSubPanel() dowindow /k ScaleSubPanel NewDataFolder /O root:Packages NewDataFolder /O root:Packages:ScaleSub make /o root:Packages:ScaleSub:W_preview=nan string /G root:Packages:ScaleSub:NewName="" NewPanel /K=1/N=ScaleSubPanel/W=(100,100,1100,600) as "Interactive subtraction" ModifyPanel /W=ScaleSubPanel fixedSize=1 SetDrawLayer UserBack SetDrawEnv fsize= 14 DrawText 25,30,"Choose a data wave:" SetDrawEnv fsize= 14 DrawText 255,30,"Choose a reference wave:" SetDrawEnv fsize= 14 DrawText 485,30,"Preview: Output = Data - [scale factor] x Reference + [offset]" ListBox listboxDataWave,pos={20, 40},size={200, 285} ListBox listboxRefWave,pos={250,40},size={200,285} MakeListIntoWaveSelector("ScaleSubPanel", "listboxDataWave",content=WMWS_Waves,selectionMode=WMWS_SelectionSingle, listoptions="DIMS:1,TEXT:0" ) MakeListIntoWaveSelector("ScaleSubPanel", "listboxRefWave",content=WMWS_Waves,selectionMode=WMWS_SelectionSingle, listoptions="DIMS:1,TEXT:0" ) if(datafolderexists("root:SingleChannelSpectra")) WS_OpenAFolder("ScaleSubPanel", "listboxDataWave", "root:SingleChannelSpectra") endif if(datafolderexists("root:BackgroundSpectra")) WS_OpenAFolder("ScaleSubPanel", "listboxRefWave", "root:BackgroundSpectra") endif WS_SetNotificationProc("ScaleSubPanel", "listboxDataWave", "ScaleSub#UpdateScaleSub" , isExtendedProc=1) WS_SetNotificationProc("ScaleSubPanel", "listboxRefWave", "ScaleSub#UpdateScaleSub" , isExtendedProc=1) Display/W=(480,40,970,440)/HOST=ScaleSubPanel /N=PreviewPlot root:Packages:ScaleSub:W_preview ModifyGraph /W=ScaleSubPanel#PreviewPlot frameStyle=5 Display/W=(20,330,220,440)/HOST=ScaleSubPanel /N=DataPlot root:Packages:ScaleSub:W_preview // temporarily put blank preview wave in here ModifyGraph /W=ScaleSubPanel#DataPlot margin(left)=1,margin(bottom)=1,margin(top)=1,margin(right)=1,frameStyle=5 ModifyGraph /W=ScaleSubPanel#DataPlot noLabel=2, axThick=0 SetAxis /W=ScaleSubPanel#DataPlot /A bottom SetAxis /W=ScaleSubPanel#DataPlot /A left Display/W=(250,330,450,440)/HOST=ScaleSubPanel /N=RefPlot root:Packages:ScaleSub:W_preview ModifyGraph /W=ScaleSubPanel#RefPlot margin(left)=1,margin(bottom)=1,margin(top)=1,margin(right)=1,frameStyle=5 ModifyGraph /W=ScaleSubPanel#RefPlot noLabel=2, axThick=0 SetAxis /W=ScaleSubPanel#RefPlot /A bottom SetAxis /W=ScaleSubPanel#RefPlot /A left SetActiveSubwindow ScaleSubPanel SetVariable setvarInc,pos={70,459},size={140,16},title="Increment:" SetVariable setvarInc,value=_NUM:1, fsize=14, proc=ScaleSub#ScaleSub_SetVar, limits={-2,inf,1} SetVariable setvarInc,userdata="1" SetVariable setvarFactor,pos={230,459},size={160,16},title="Scale factor:" SetVariable setvarFactor,value=_NUM:1, fsize=14, proc=ScaleSub#ScaleSub_SetVar SetVariable setvarOffset,pos={410,459},size={120,16},title="Offset:" SetVariable setvarOffset,value=_NUM:0, fsize=14, proc=ScaleSub#ScaleSub_SetVar SetVariable setvarNewName,pos={550,459},size={350,16},title="Create this output wave:" SetVariable setvarNewName,value=_STR:"new", fsize=14 Button buttonDoIt,pos={920,457},size={50,20},title="Do It", disable=2, proc=ScaleSub#ScaleSub_Buttons end static function UpdateScaleSub(SelectedItem, EventCode, OwningWindowName, ListboxControlName) String SelectedItem // string with full path to the item clicked on in the wave selector Variable EventCode // the ListBox event code that triggered this notification String OwningWindowName // String containing the name of the window containing the listbox String ListboxControlName // String containing the name of the listbox control string refStr, dataStr if(stringmatch(ListboxControlName, "listboxDataWave")) dataStr=SelectedItem wave /Z w_data=$removeending(dataStr, ";") RemoveFromGraph /Z/W=ScaleSubPanel#DataPlot $"#0" if (waveexists(w_data)) // replot wave in small subwindow AppendToGraph /W=ScaleSubPanel#DataPlot w_data ModifyGraph /W=ScaleSubPanel#DataPlot axThick=0 SetAxis /W=ScaleSubPanel#DataPlot /A bottom // reset name suggestion for output string tempStr=nameofwave(w_data)+"_n" if(CheckName(tempStr, 1)) tempStr=UniqueName(tempStr, 1, 0) endif else tempStr="new" endif SetVariable setvarNewName, win=ScaleSubPanel, value=_STR:tempStr endif if(stringmatch(ListboxControlName, "listboxRefWave")) wave /Z w_ref=$removeending(SelectedItem, ";") RemoveFromGraph /Z /W=ScaleSubPanel#RefPlot $"#0" if (waveexists(w_ref)) AppendToGraph /W=ScaleSubPanel#RefPlot w_ref ModifyGraph /W=ScaleSubPanel#RefPlot axThick=0 SetAxis /W=ScaleSubPanel#RefPlot /A bottom endif endif doSub() end static function doSub() string refStr, dataStr refStr=WS_SelectedObjectsList("ScaleSubPanel", "listboxRefWave") dataStr=WS_SelectedObjectsList("ScaleSubPanel", "listboxDataWave") refStr=removeending(refStr, ";") dataStr=removeending(dataStr, ";") wave w_preview=root:Packages:ScaleSub:w_preview wave /Z w_ref=$refStr wave /Z w_data=$dataStr string msg="" if (!waveexists(w_ref)) msg+="no reference wave selected\r" endif if (!waveexists(w_data)) msg+="no data wave selected\r" endif // if( ( strlen(msg)==0 ) && (dimsize(w_ref, 0)!=dimsize(w_data, 0)) ) // msg+="sample and background length mismatch\r" // endif textbox /C/F=0/W=ScaleSubPanel#PreviewPlot /N=text0/Z=1/X=5.00/Y=5.00/A=LT "\\K(26112,52224,0)"+msg if (strlen(msg)==0) duplicate /o w_data w_preview controlinfo /W=ScaleSubPanel setvarFactor variable scale=v_value controlinfo /W=ScaleSubPanel setvarOffset variable offset=v_value controlinfo /W=ScaleSubPanel setvarNewName string newStr=s_value w_preview=w_data-offset-scale*refValue(w_ref, x) Button buttonDoIt win=ScaleSubPanel , disable=0 if (offset) sprintf msg, "%s = %s % + g %+g * %s", newStr, nameofwave(w_data), offset, -scale, nameofwave(w_ref) else sprintf msg, "%s = %s %+g * %s", newStr, nameofwave(w_data), -scale, nameofwave(w_ref) endif textbox /C/F=0/W=ScaleSubPanel#PreviewPlot /N=text0/Z=1/X=5.00/Y=5.00/A=LT "\\K(26112,52224,0)"+msg note /k w_preview, msg return 1 endif w_preview=nan //SetVariable setvarNewName, win=ScaleSubPanel, value=_STR:"new" Button buttonDoIt win=ScaleSubPanel , disable=2 end static function refValue(w_ref, v_x) wave w_ref variable v_x make /free /n=2 w_range w_range[0]=leftx(w_ref) w_range[1]=pnt2x(w_ref,numpnts(w_ref)-1) if (v_x wavemax(w_range)) return nan endif return w_ref(v_x) end static function ScaleSub_Buttons(ctrlName) : ButtonControl String ctrlName wave w_preview=root:Packages:ScaleSub:W_preview controlinfo /W=ScaleSubPanel setvarNewName string newStr=s_value if (checkname(newStr, 1)!=0) doalert 1, ParseFilePath(0, newStr, ":", 1, 0)+" exists. Overwrite?" if (V_flag==2) return 0 endif endif duplicate /o w_preview $newStr // update wave selector listboxes so that new wave appears WS_UpdateWaveSelectorWidget("ScaleSubPanel", "listboxRefWave") WS_UpdateWaveSelectorWidget("ScaleSubPanel", "listboxDataWave") return 1 end static function ScaleSub_SetVar(s) : SetVariableControl STRUCT WMSetVariableAction &s if(s.eventCode<0) // excludes Igor8 eventcodes -2 and -3 return 0 endif if(stringmatch(s.ctrlName, "setvarInc")) controlinfo /W=ScaleSubPanel setvarInc variable old=str2num(S_UserData) variable new=s.dval if (abs(new-old)<1e-15) return 0 endif if (numtype(new)!=0) return 0 endif if (s.eventCode==1) // mouseup = increment button if(new