// Script function: Extract a selected energy window from a EDS spectrum and sum it.
// Note: In this example, the energy range of the entire EDS spectrum is 20 keV and 2048 channels.
// Requirement: Open an EDS spectrum.

// Unless named starting with GlobalSino, EM, My, Self, the function commands are defined 
//by Gatan DM, please see at link: http://www.globalsino.com/EM/page2597.html


void GlobalSinoPrint( ROI GlobalSinoROI, number MyCalculation )
{      
image GlobalSinoInput, GlobalSinoMask1, GlobalSinoMask2, GlobalSinoMask, GlobalSinoMasked

TagGroup GlobalSino, EMItems
GlobalSino = DLGCreateDialog( "Please confirm your channels", EMItems )
TagGroup MyStart, MyWidth
EMItems.DLGAddElement( DLGCreateRealField( "Window Start (eV) :", MyStart, 3000, 8, 3 ) )  // Start energy
EMItems.DLGAddElement( DLGCreateRealField( "Window Width (eV) :", MyWidth, 1000, 8, 3 ) )  // Energy width
   
if ( !Alloc( UIframe ).Init( GlobalSino ).Pose() ) exit (0)
number GlobalSinoStart = MyStart.DLGGetValue()
number GlobalSinoWidth = MyWidth.DLGGetValue()

if ( !GetFrontImage( GlobalSinoInput ) )
Throw( "No image loaded." )
GlobalSinoMask1 := ImageClone(GlobalSinoInput)
GlobalSinoMask1 = icol  < GlobalSinoStart/10+20 ? 1 : 0   // ===

GlobalSinoMask2 := ImageClone(GlobalSinoInput)
GlobalSinoMask2 = icol  < (GlobalSinoStart + GlobalSinoWidth)/10 +20 ? 1 : 0   // ===

GlobalSinoMask := ImageClone(GlobalSinoInput) // Carry the units and calibration of Image GlobalSinoInput
GlobalSinoMask = 0<1 ? (GlobalSinoMask2 - GlobalSinoMask1) : 0 

GlobalSinoMasked := ImageClone(GlobalSinoInput)
GlobalSinoMasked = GlobalSinoMask * GlobalSinoInput
SetName( GlobalSinoMasked, GetName( GlobalSinoInput ) + " masked" )
ShowImage( GlobalSinoMasked )      

result ("\n Start Energy and Energy Window: " + GlobalSinoStart + " eV and " + GlobalSinoWidth + " eV")
result ("\n Sum of energy window: " + sum (GlobalSinoMasked))

}
image GlobalSinoImage := GetFrontImage()
imageDisplay MyDisplay = GlobalSinoImage.ImageGetImageDisplay( 0 )
number MyCalculation = TwoButtonDialog( "Do you want to compute it?", "Yes", "No" )
 GlobalSinoPrint( MyDisplay.ImageDisplayGetROI( 0 ), MyCalculation )