// Script function: Remove the background of an EELS profile
// Requirement: Open an EELS profile on the front-most.

// 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

if (!ContinueCancelDialog("Please select the background of the front-most EELS image")) exit (0)

image GlobalSinoImage:=Getfrontimage()
number EMxsize, EMysize
Getsize(GlobalSinoImage, EMxsize, EMysize)

// Count the number of slices in the image and get some info

number EMnumber, EMsliceNumber, EMexistingSlice
Imagedisplay GlobalSinoEM=GlobalSinoImage.Imagegetimagedisplay(0)
EMsliceNumber=GlobalSinoEM.Imagedisplaycountslices()
string EMimageNames=Getname(GlobalSinoImage)

// Loop through the slices in the spectrum, displaying each slice as a stand-alone spectrum

for(EMnumber=0; EMnumber<EMsliceNumber; EMnumber++)
{
// Get the slice
object EMsliceID=GlobalSinoEM.Imagedisplaygetsliceidbyindex(EMnumber)
string EMsliceName=GlobalSinoEM.Imagedisplaygetslicelabelbyid(EMsliceID)
// Extract it as an image and display it
image EMslice:=Imageclone(GlobalSinoImage{EMnumber})// clone the actual slice to pick up all the image calibrations, tags etc

if ( EMnumber > 1 ) // Remove or disable this line if you want to extract all the slices
Showimage(EMslice) // Show images with their Legend

// label and display the component spectrum
Setname(EMslice, EMimageNames+" ("+EMsliceName+")") // Display "Legend"
Documentwindow EMWindows=Getdocumentwindow(0)
EMWindows.Windowsetframeposition(EMexistingSlice*30, EMexistingSlice*30)
imagecopycalibrationfrom(EMslice, GlobalSinoImage)
EMexistingSlice=EMexistingSlice+1
result ("\n Slice " + EMexistingSlice + ": "  + GetName(EMslice))
}
