// Script to compute the High Frequency Dielectric Constant
// based on equation 9 in Gass et al., Phys. Rev. B73, (2006) 035312.

// This image takes as its input the front-most image which must be 
// Epsilon2 data from Kramers Kronig analysis (KKA). This data can be 
// in the form of a 1D intensity profile, a 2D spectrum image or a 3D spectrum image.

// EELS data must have plural scattering and low loss removed prior to
// carrying out KKA. Have the Epsilon2 data file shown front-most and then
// run this script.

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this address work)
// version:20200814, v1.0, August 2020

// Acknowledgements: The integration code is based on some Gatan code, by an anonymous author.



// Source the front-most image

number nodocs=countdocumentwindowsoftype(5)
if(nodocs<1)
	{
		showalert("Ensure Epsilon2 data from KKA is displayed.\n\nData can be in the form of : an intensity profile, a 2D SI or a 3D SI.",2)
		exit(0)
	}
	
image front:=getfrontimage()
image efDC


// Get some data from it

number xsize, ysize, zsize
string imgname=getname(front)

try
	{
		get3dsize(front, xsize, ysize, zsize)
	}
catch
	{
		getsize(front, xsize, ysize)
		break
	}


// Decide what kind of image it is : spectrum, 2D SI, or 3D SI

number sizeflag=0

if(zsize>1) sizeflag=3 // 3D SI
if(zsize==1 && ysize>1) sizeflag=2 // 2D SI
if(xsize>1 && ysize<=1) sizeflag=1	// Spectrum

if(sizeflag==0)
	{
		showalert("The front-most image must be Espilon2 data from Kramers-Kronig Analysis. The image can be a spectrum, a 2D SI or a 3D SI.",2)
		exit(0)
	}


// Main processing section

if(sizeflag==3) // it is a 3D spectrum image
	{
		// Create images to store the individual spectra and output
		
		image clone=imageclone(front)
		image output=realimage("", 4, xsize, ysize)


		// Get calibration info
		
		number origin, scale, dimension=2 // z axis is the energy axis
		string units
		origin=front.imagegetdimensionorigin(dimension)
		scale=front.imagegetdimensionscale(dimension)
		
		
		// Create a 'spectrum' in which each channel contains the energy value of that channel
		
		image energyloss=realimage("", 4, zsize, 1)
		energyloss=icol*scale+origin
			
			
		// Create a clone of the spectrum image and divide the SI by the energy of each channel
		
		image energy:=imageclone(front)
		energy/=energyloss[iplane, icol, irow]
		
		
		//Remove information below 0 eV - ch0eV is the channel at which the energy is 0eV

		number ch0eV = ceil(-origin/scale)
		energy[0,0,0,xsize,ysize,ch0eV+1]=0


		// Do the integration
		
		image integral:=imageclone(energy)
		integral = integral[icol,irow,iplane] + integral[icol,irow,iplane-1]


		// Calculate the effective dielectric constant
		
		efDC=imageclone(front)*0
		efDC=1+2/pi()*integral*scale
	}
	
if(sizeflag==2) // 2D SI
	{
		// Create images to store the individual spectra and output
		
		image clone=imageclone(front)
		image output=realimage("", 4, xsize, ysize)


		// Get calibration info
		
		number origin, scale, dimension=0 // x axis is the energy axis
		string units
		origin=front.imagegetdimensionorigin(dimension)
		scale=front.imagegetdimensionscale(dimension)
		
		
		// Create a 2D 'spectrum  image' in which each channel contains the energy value of that channel
		
		image energyloss=realimage("", 4, xsize, ysize)
		energyloss=icol*scale+origin
		
				
		// Create a clone of the spectrum image and divide the SI by the energy of each channel
		
		image energy:=imageclone(front)
		energy=energy/energyloss
		
		
		//Remove information up to 0 eV
		
		number ch0eV = ceil(-origin/scale)
		energy[0,0,ysize, ch0eV]=0
		

		// Do the integration

		image integral:= imageclone(energy)
		integral= integral[icol, irow]+integral[icol-1, irow]
		
		efDC=imageclone(integral)*0
		efDC=1+2/pi()*integral*scale
	}


if(sizeflag==1) // Spectrum
	{
		// Define some variables and source the calibration of the image
		
		number i, Eeff
		number scale, origin, dimension=0 // x axis is the energy axis
		scale=front.imagegetdimensionscale(dimension)
		origin=front.imagegetdimensionorigin(dimension)
		
		
		// Create a 'spectrum' to store the energy of each channel
		
		image energyLoss = RealImage("Energy loss",4,xsize,1)
		energyLoss = icol*scale+origin
		
		
		// Divide the 1D spectrum by the energy of each channel
		
		image eps2spectrum:=imageclone(front)
		eps2spectrum=eps2spectrum/energyloss
		
		
		//Remove information up to 0 eV
		
		number ch0eV = ceil(-origin/scale)
		eps2spectrum=tert(icol<(ch0eV+1), 0, eps2spectrum)


		// Do the integration

		image integral:= imageclone(eps2spectrum)
		integral = integral[icol, irow]+integral[icol-1, irow]
		
		efDC=imageclone(integral)*0
		efDC=1+2/pi()*integral*scale
	}


		// Copy across calibrations and tags
		
		imagecopycalibrationfrom(efDC, front)
		taggroup efDCtags=efDC.imagegettaggroup()
		taggroup fronttags=front.imagegettaggroup()
		taggroupcopytagsfrom(efDCtags, fronttags)
		

		// Get the size and position of the original spectrum image
		
		showimage(front)
		documentwindow frontwin=getdocumentwindow(0)
		number top, left, bottom, right
		frontwin.windowgetframebounds(top, left, bottom, right)
		number displayheight=bottom-top
		number displaywidth=right-left
		

		// Display the output at the same size and directly to the right of the original.
		// If that would put it outside the screen area, display below the original.
		// If that too would put it off the screen area - display top left.
		
		number screenwidth, screenheight
		getscreensize(screenwidth, screenheight)

		showimage(efDC)
		setname(efDC, "Effective Dielectric Constant")

		documentwindow outputwin=getdocumentwindow(0)
		number posflag=0
		if(right>=(screenwidth*0.9)) posflag=1 // 90% of the screen width is the limit chosen
		if(bottom>=(screenheight*0.9)) posflag=posflag+2 // ditto bottom of the screen

		if(posflag==1)outputwin.windowsetframebounds(bottom, left, bottom+displayheight, right)
		if(posflag==0 || posflag==2)outputwin.windowsetframebounds(top, right, bottom, right+displaywidth)
		if(posflag==3) windowsetframebounds(outputwin, 0,0, displayheight, displaywidth)