-- This tutorial calculates the 2p to 1s x-ray emission spectra of Ni in NiO using
-- ligand field theory. We assume that the decay of the 1s core hole is fast such
-- that the 3d state is unchanged by the 1s core hole.

-- The basis

NF=28
NB=0
IndexDn_1s={0}
IndexUp_1s={1}
IndexDn_2p={2,4,6}
IndexUp_2p={3,5,7}
IndexDn_3d={8,10,12,14,16}
IndexUp_3d={9,11,13,15,17}
IndexDn_Ld={18,20,22,24,26}
IndexUp_Ld={19,21,23,25,27}

-- angular momentum operators on the d-shell
 
OppSx_3d   =NewOperator("Sx"   ,NF, IndexUp_3d, IndexDn_3d)
OppSy_3d   =NewOperator("Sy"   ,NF, IndexUp_3d, IndexDn_3d)
OppSz_3d   =NewOperator("Sz"   ,NF, IndexUp_3d, IndexDn_3d)
OppSsqr_3d =NewOperator("Ssqr" ,NF, IndexUp_3d, IndexDn_3d)
OppSplus_3d=NewOperator("Splus",NF, IndexUp_3d, IndexDn_3d)
OppSmin_3d =NewOperator("Smin" ,NF, IndexUp_3d, IndexDn_3d)
 
OppLx_3d   =NewOperator("Lx"   ,NF, IndexUp_3d, IndexDn_3d)
OppLy_3d   =NewOperator("Ly"   ,NF, IndexUp_3d, IndexDn_3d)
OppLz_3d   =NewOperator("Lz"   ,NF, IndexUp_3d, IndexDn_3d)
OppLsqr_3d =NewOperator("Lsqr" ,NF, IndexUp_3d, IndexDn_3d)
OppLplus_3d=NewOperator("Lplus",NF, IndexUp_3d, IndexDn_3d)
OppLmin_3d =NewOperator("Lmin" ,NF, IndexUp_3d, IndexDn_3d)
 
OppJx_3d   =NewOperator("Jx"   ,NF, IndexUp_3d, IndexDn_3d)
OppJy_3d   =NewOperator("Jy"   ,NF, IndexUp_3d, IndexDn_3d)
OppJz_3d   =NewOperator("Jz"   ,NF, IndexUp_3d, IndexDn_3d)
OppJsqr_3d =NewOperator("Jsqr" ,NF, IndexUp_3d, IndexDn_3d)
OppJplus_3d=NewOperator("Jplus",NF, IndexUp_3d, IndexDn_3d)
OppJmin_3d =NewOperator("Jmin" ,NF, IndexUp_3d, IndexDn_3d)
 
Oppldots_3d=NewOperator("ldots",NF, IndexUp_3d, IndexDn_3d)
 
-- Angular momentum operators on the Ligand shell
 
OppSx_Ld   =NewOperator("Sx"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppSy_Ld   =NewOperator("Sy"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppSz_Ld   =NewOperator("Sz"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppSsqr_Ld =NewOperator("Ssqr" ,NF, IndexUp_Ld, IndexDn_Ld)
OppSplus_Ld=NewOperator("Splus",NF, IndexUp_Ld, IndexDn_Ld)
OppSmin_Ld =NewOperator("Smin" ,NF, IndexUp_Ld, IndexDn_Ld)
 
OppLx_Ld   =NewOperator("Lx"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppLy_Ld   =NewOperator("Ly"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppLz_Ld   =NewOperator("Lz"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppLsqr_Ld =NewOperator("Lsqr" ,NF, IndexUp_Ld, IndexDn_Ld)
OppLplus_Ld=NewOperator("Lplus",NF, IndexUp_Ld, IndexDn_Ld)
OppLmin_Ld =NewOperator("Lmin" ,NF, IndexUp_Ld, IndexDn_Ld)
 
OppJx_Ld   =NewOperator("Jx"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppJy_Ld   =NewOperator("Jy"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppJz_Ld   =NewOperator("Jz"   ,NF, IndexUp_Ld, IndexDn_Ld)
OppJsqr_Ld =NewOperator("Jsqr" ,NF, IndexUp_Ld, IndexDn_Ld)
OppJplus_Ld=NewOperator("Jplus",NF, IndexUp_Ld, IndexDn_Ld)
OppJmin_Ld =NewOperator("Jmin" ,NF, IndexUp_Ld, IndexDn_Ld)
 
-- total angular momentum
OppSx = OppSx_3d + OppSx_Ld
OppSy = OppSy_3d + OppSy_Ld
OppSz = OppSz_3d + OppSz_Ld
OppSsqr = OppSx * OppSx + OppSy * OppSy + OppSz * OppSz
OppSsqr.Name="S2"
OppLx = OppLx_3d + OppLx_Ld
OppLy = OppLy_3d + OppLy_Ld
OppLz = OppLz_3d + OppLz_Ld
OppLsqr = OppLx * OppLx + OppLy * OppLy + OppLz * OppLz
OppLsqr.Name="L2"
OppJx = OppJx_3d + OppJx_Ld
OppJy = OppJy_3d + OppJy_Ld
OppJz = OppJz_3d + OppJz_Ld
OppJsqr = OppJx * OppJx + OppJy * OppJy + OppJz * OppJz
OppJsqr.Name="J2"
 
-- define the coulomb operator
-- we here define the part depending on F0 seperately from the part depending on F2
-- when summing we can put in the numerical values of the slater integrals
 
OppF0_3d =NewOperator("U", NF, IndexUp_3d, IndexDn_3d, {1,0,0})
OppF0_3d.Name="F0dd"
OppF2_3d =NewOperator("U", NF, IndexUp_3d, IndexDn_3d, {0,1,0})
OppF2_3d.Name="F2dd"
OppF4_3d =NewOperator("U", NF, IndexUp_3d, IndexDn_3d, {0,0,1})
OppF4_3d.Name="F4dd"
 
-- define onsite energies - crystal field
-- Akm = {{k1,m1,Akm1},{k2,m2,Akm2}, ... }
 
Akm = PotentialExpandedOnClm("Oh", 2, {0.6,-0.4})
OpptenDq_3d = NewOperator("CF", NF, IndexUp_3d, IndexDn_3d, Akm)
OpptenDq_Ld = NewOperator("CF", NF, IndexUp_Ld, IndexDn_Ld, Akm)
 
Akm = PotentialExpandedOnClm("Oh", 2, {1,0})
OppNeg_3d = NewOperator("CF", NF, IndexUp_3d, IndexDn_3d, Akm)
OppNeg_3d.Name="Neg3d"
OppNeg_Ld = NewOperator("CF", NF, IndexUp_Ld, IndexDn_Ld, Akm)
Akm = PotentialExpandedOnClm("Oh", 2, {0,1})
OppNt2g_3d = NewOperator("CF", NF, IndexUp_3d, IndexDn_3d, Akm)
OppNt2g_3d.Name="Nt2g3d"
OppNt2g_Ld = NewOperator("CF", NF, IndexUp_Ld, IndexDn_Ld, Akm)
 
OppNUp_1s = NewOperator("Number", NF, IndexUp_1s, IndexUp_1s, {1})
OppNDn_1s = NewOperator("Number", NF, IndexDn_1s, IndexDn_1s, {1})
OppN_1s = OppNUp_1s + OppNDn_1s
OppNUp_2p = NewOperator("Number", NF, IndexUp_2p, IndexUp_2p, {1,1,1})
OppNDn_2p = NewOperator("Number", NF, IndexDn_2p, IndexDn_2p, {1,1,1})
OppN_2p = OppNUp_2p + OppNDn_2p
OppNUp_3d = NewOperator("Number", NF, IndexUp_3d, IndexUp_3d, {1,1,1,1,1})
OppNDn_3d = NewOperator("Number", NF, IndexDn_3d, IndexDn_3d, {1,1,1,1,1})
OppN_3d = OppNUp_3d + OppNDn_3d
OppNUp_Ld = NewOperator("Number", NF, IndexUp_Ld, IndexUp_Ld, {1,1,1,1,1})
OppNDn_Ld = NewOperator("Number", NF, IndexDn_Ld, IndexDn_Ld, {1,1,1,1,1})
OppN_Ld = OppNUp_Ld + OppNDn_Ld
 
-- define L-d interaction
 
Akm = PotentialExpandedOnClm("Oh", 2, {1,0})
OppVeg  = NewOperator("CF", NF, IndexUp_3d, IndexDn_3d, IndexUp_Ld, IndexDn_Ld,Akm) +  NewOperator("CF", NF, IndexUp_Ld, IndexDn_Ld, IndexUp_3d, IndexDn_3d, Akm)
Akm = PotentialExpandedOnClm("Oh", 2, {0,1})
OppVt2g = NewOperator("CF", NF, IndexUp_3d, IndexDn_3d, IndexUp_Ld, IndexDn_Ld,Akm) +  NewOperator("CF", NF, IndexUp_Ld, IndexDn_Ld, IndexUp_3d, IndexDn_3d, Akm)
 
-- core valence interaction
 
OppUsdF0 = NewOperator("U", NF, IndexUp_1s, IndexDn_1s, IndexUp_3d, IndexDn_3d, {1}, {0})
OppUsdG2 = NewOperator("U", NF, IndexUp_1s, IndexDn_1s, IndexUp_3d, IndexDn_3d, {0}, {1})

Oppcldots= NewOperator("ldots", NF, IndexUp_2p, IndexDn_2p)
OppUpdF0 = NewOperator("U", NF, IndexUp_2p, IndexDn_2p, IndexUp_3d, IndexDn_3d, {1,0}, {0,0})
OppUpdF2 = NewOperator("U", NF, IndexUp_2p, IndexDn_2p, IndexUp_3d, IndexDn_3d, {0,1}, {0,0})
OppUpdG1 = NewOperator("U", NF, IndexUp_2p, IndexDn_2p, IndexUp_3d, IndexDn_3d, {0,0}, {1,0})
OppUpdG3 = NewOperator("U", NF, IndexUp_2p, IndexDn_2p, IndexUp_3d, IndexDn_3d, {0,0}, {0,1})



-- dipole transition
 
t=math.sqrt(1/2)
 
Akm = {{1,-1,t},{1, 1,-t}}
TXESx = NewOperator("CF", NF, IndexUp_1s, IndexDn_1s, IndexUp_2p, IndexDn_2p, Akm)
Akm = {{1,-1,t*I},{1, 1,t*I}}
TXESy = NewOperator("CF", NF, IndexUp_1s, IndexDn_1s, IndexUp_2p, IndexDn_2p, Akm)
Akm = {{1,0,1}}
TXESz = NewOperator("CF", NF, IndexUp_1s, IndexDn_1s, IndexUp_2p, IndexDn_2p, Akm)


-- and the annihilation operator on the 1s orbital.

An1sUp = NewOperator("An", NF, IndexUp_1s,{1})
An1sDn = NewOperator("An", NF, IndexDn_1s,{1})


-- once all operators are defined we can set some parameter values.

-- We follow the energy definitions as introduced in the group of G.A. Sawatzky (Groningen)
-- J. Zaanen, G.A. Sawatzky, and J.W. Allen PRL 55, 418 (1985)
-- for parameters of specific materials see
-- A.E. Bockquet et al. PRB 55, 1161 (1996)
-- After some initial discussion the energies U and Delta refer to the center of a configuration
-- The L^10 d^n   configuration has an energy 0
-- The L^9  d^n+1 configuration has an energy Delta
-- The L^8  d^n+2 configuration has an energy 2*Delta+Udd
--
-- If we relate this to the onsite energy of the L and d orbitals we find
-- 10 eL +  n    ed + n(n-1)     U/2 == 0
--  9 eL + (n+1) ed + (n+1)n     U/2 == Delta
--  8 eL + (n+2) ed + (n+1)(n+2) U/2 == 2*Delta+U
-- 3 equations with 2 unknowns, but with interdependence yield:
-- ed = (10*Delta-nd*(19+nd)*U/2)/(10+nd)
-- eL = nd*((1+nd)*Udd/2-Delta)/(10+nd)
--
-- For the intermediate state we/they defined
-- The 1s^1 L^10 d^n   configuration has an energy 0
-- The 1s^1 L^9  d^n+1 configuration has an energy Delta - Usd
-- The 1s^1 L^8  d^n+2 configuration has an energy 2*Delta + Udd - 2*Usd
--
-- If we relate this to the onsite energy of the s and d orbitals we find
-- 2 es + 10 eL +  n    ed + n(n-1)     Udd/2 + 2 n     Usd == 0
-- 2 es +  9 eL + (n+1) ed + (n+1)n     Udd/2 + 2 (n+1) Usd == Delta
-- 2 es +  8 eL + (n+2) ed + (n+1)(n+2) Udd/2 + 2 (n+2) Usd == 2*Delta+Udd
-- 1 es + 10 eL + n   ) ed + n(n-1)     Udd/2 + 1 n     Usd == 0
-- 1 es +  9 eL + (n+1) ed + (n+1)n     Udd/2 + 1 (n+1) Usd == Delta-Usd
-- 1 es +  8 eL + (n+2) ed + (n+1)(n+2) Udd/2 + 1 (n+2) Usd == 2*Delta+Udd-2*Usd
-- 6 equations with 3 unknowns, but with interdependence yield:
-- esintermediate = -n Usd
-- edintermediate = -((-20 Delta + 19 n Udd + n^2 Udd + 40 Usd)/(2 (10 + n)))
-- eLintermediate = -((2 Delta n - n Udd - n^2 Udd - 4 n Usd)/(2 (10 + n)))
--
-- For the final state we define
--
-- The 2p^5 L^10 d^n configuration has an energy 0
-- The 2p^5 L^9  d^n configuration has an energy Delta + Udd - Upd
-- The 2p^5 L^8  d^n configuration has an energy 2*Delta + 3*Udd - 2*Upd
--
-- If we relate this to the onsite energy of the p and d orbitals we find
-- 6 ep + 10 eL +  n    ed + n(n-1)     Udd/2 + 6 n     Upd == 0
-- 6 ep +  9 eL + (n+1) ed + (n+1)n     Udd/2 + 6 (n+1) Upd == Delta
-- 6 ep +  8 eL + (n+2) ed + (n+1)(n+2) Udd/2 + 6 (n+2) Upd == 2*Delta+Udd
-- 5 ep + 10 eL + n     ed + n(n-1)     Udd/2 + 5 n     Upd == 0
-- 5 ep +  9 eL + (n+1) ed + (n+1)(n)   Udd/2 + 5 (n+1) Upd == Delta-Upd
-- 5 ep +  8 eL + (n+2) ed + (n+2)(n+1) Udd/2 + 5 (n+2) Upd == 2*Delta+Udd-2*Upd
-- 6 equations with 3 unknowns, but with interdependence yield:
-- epfinal = -nd * Upd
-- edfinal = (     20 * Delta - 19 * nd * Udd - nd^2 * Udd -     120 * Upd) / (2*(10+nd))
-- eLfinal = (-2 * nd * Delta +      nd * Udd + nd^2 * Udd + 12 * nd * Upd) / (2*(10+nd))
-- 
-- note that ed-ep = Delta - nd * U and not Delta
-- note furthermore that ep and ed here are defined for the onsite energy if the system had
-- locally nd electrons in the d-shell. In DFT or Hartree Fock the d occupation is in the end not
-- nd and thus the onsite energy of the Kohn-Sham orbitals is not equal to ep and ed in model
-- calculations.
--
-- note furthermore that ep and eL actually should be different for most systems. We happily ignore this fact
-- 
-- We normally take U and Delta as experimentally determined parameters

-- number of electrons (formal valence)
nd = 8
-- parameters from experiment (core level PES)
Udd     =  7.3
Upd     =  8.5
Usd     =  7.5
Delta   =  4.7
-- parameters obtained from DFT (PRB 85, 165113 (2012))
F2dd    = 11.14 
F4dd    =  6.87
F2pd    =  6.67
G1pd    =  4.92
G3pd    =  2.80
G2sd    =  0.057
tenDq   =  0.56
tenDqL  =  1.44
Veg     =  2.06
Vt2g    =  1.21
zeta_3d =  0.081
zeta_2p = 11.51
Bz      =  0.000001
Hz      =  0.120

F0dd    = Udd + (F2dd+F4dd) * 2/63
F0pd    = Upd + (1/15)*G1pd + (3/70)*G3pd
F0sd    = Usd + G2sd*1/10
 
ed      = (10*Delta-nd*(19+nd)*Udd/2)/(10+nd)
eL      = nd*((1+nd)*Udd/2-Delta)/(10+nd)
 
esintermediate = -nd * Usd
edintermediate = ((     20 * Delta - 19 * nd * Udd - nd^2 * Udd -     40 * Usd) / (2 * (10 + nd)))
eLintermediate = ((-2 * nd * Delta +      nd * Udd + nd^2 * Udd + 4 * nd * Usd) / (2 * (10 + nd)))

epfinal = -nd * Upd
edfinal = (     20 * Delta - 19 * nd * Udd - nd^2 * Udd -     120 * Upd) / (2*(10+nd))
eLfinal = (-2 * nd * Delta +      nd * Udd + nd^2 * Udd + 12 * nd * Upd) / (2*(10+nd))
 
 
Hamiltonian             = F0dd*OppF0_3d + F2dd*OppF2_3d + F4dd*OppF4_3d + zeta_3d*Oppldots_3d + Bz*(2*OppSz_3d + OppLz_3d) + Hz * OppSz_3d + tenDq*OpptenDq_3d + tenDqL*OpptenDq_Ld + Veg * OppVeg + Vt2g * OppVt2g 
                        + ed * OppN_3d + eL * OppN_Ld
Hamiltonian.Name="H"
 
IntermediateHamiltonian = F0dd*OppF0_3d + F2dd*OppF2_3d + F4dd*OppF4_3d + zeta_3d*Oppldots_3d + Bz*(2*OppSz_3d + OppLz_3d) + Hz * OppSz_3d + tenDq*OpptenDq_3d + tenDqL*OpptenDq_Ld + Veg * OppVeg + Vt2g * OppVt2g 
                        + esintermediate * OppN_1s + edintermediate * OppN_3d + eLintermediate * OppN_Ld 
                        + F0sd * OppUsdF0 + G2sd * OppUsdG2
 
FinalHamiltonian        = F0dd*OppF0_3d + F2dd*OppF2_3d + F4dd*OppF4_3d + zeta_3d*Oppldots_3d + Bz*(2*OppSz_3d + OppLz_3d) + Hz * OppSz_3d + tenDq*OpptenDq_3d + tenDqL*OpptenDq_Ld + Veg * OppVeg + Vt2g * OppVt2g 
                        + epfinal * OppN_2p + edfinal * OppN_3d + eLfinal * OppN_Ld
                        + zeta_2p * Oppcldots + F0pd * OppUpdF0 + F2pd * OppUpdF2 + G1pd * OppUpdG1 + G3pd * OppUpdG3  

-- We saw in the previous example that NiO has a ground-state triplet with occupation 
-- t2g^6 eg^2 and S=1 (S^2=S(S+1)=2). The next state is 1.1 eV higher in energy and thus
-- unimportant for the ground-state upto temperatures of 10 000 Kelvin. We thus restrict 
-- the calculation to the lowest 3 eigenstates.
Npsi=3
-- in order to make sure we have a filling of 8
-- electrons we need to define some restrictions
-- We need to restrict the occupation of the Ni-1s shell to 2 for the ground state and for
-- the Ni 3d-shell to 8.
StartRestrictions = {NF, NB, {"11 111111 0000000000 1111111111",18,18}, {"00 000000 1111111111 0000000000",8,8}}

-- And calculate the lowest 3 eigenfunctions
psiList = Eigensystem(Hamiltonian, StartRestrictions, Npsi)

-- In order to get some information on these eigenstates it is good to plot expectation values
-- We first define a list of all the operators we would like to calculate the expectation value of
oppList={Hamiltonian, OppSsqr, OppLsqr, OppJsqr, OppSz, OppLz, Oppldots_3d, OppF2_3d, OppF4_3d, OppNeg_3d, OppNt2g_3d};

-- we print the expectation values
PrintExpectationValues(psiList,oppList,{{"zero",0.001}})

-- we here assume that we can create the 1s core hole and that afterwards this core hole 
-- directly decays via the 2p to 1s FY decay. There is no internal relaxation. 
psiListIntermediate = {An1sDn * psiList[1], An1sUp * psiList[1],
                       An1sDn * psiList[2], An1sUp * psiList[2],
                       An1sDn * psiList[3], An1sUp * psiList[3]}


-- calculating the spectra is simple and single line once all operators and wave-functions
-- are defined.
XESSpectra = CreateSpectra(FinalHamiltonian, {TXESx,TXESy,TXESz}, psiListIntermediate, {{"Emin",-20}, {"Emax",25}, {"NE",4500}, {"Gamma",0.2}})
-- XASSpectra will contain 15 spectra in the end. We have 3 functions in psiList and 5 
-- polarizations. We furthermore define the energy range to be from -5 to +10
-- using 3000 energy points (delta E = 10 meV) and an internal lifetime during the calculation
-- of 100 meV (Full width Half maximum)

-- we can use several operations on a spectrum object. For one we can sum several spectra
-- the function Spectra.Sum sums the spectra with the prefactors given in the list afterwards
-- sum the spectra of the ground-state for different polarizations in order to get the
-- isotropic spectrum
XESIsoSpectra = Spectra.Sum(XESSpectra,{1,1,0,0,0,0,  1,1,0,0,0,0, 1,1,0,0,0,0})
-- broaden the spectra, note that a method changes the spectrum object, here we broaden by
-- a Gaussian of FWHM 0.4 and Lorenzian of FWHM 1.0
XESIsoSpectra.Broaden(0.4,1.0)

-- in order to plot the spectra we write them to file in ASCII format
-- note that the calculated object, i.e. <psi | T^dag 1/(w-H+IG/2) T | psi>
-- is complex. Minus the imaginary part is the absorption.
XESSpectra.Print({{"file","XESSpec.dat"}})
XESIsoSpectra.Print({{"file","XESIsoSpec.dat"}})

-- in order to plot we use gnuplot. You can use any program, but gnuplot is nice as it can
-- be called from within Quanty. We first define the gnuplot script as a string inside Quanty
gnuplotInput = [[
set autoscale 
set xtic auto 
set ytic auto 
set style line  1 lt 1 lw 1 lc rgb "#000000"
set style line  2 lt 1 lw 1 lc rgb "#FF0000"
set style line  3 lt 1 lw 3 lc rgb "#000000"

set xlabel "E (eV)" font "Times,12"
set ylabel "Intensity (arb. units)" font "Times,8"

set out 'XESSpec.ps'
set size 1.0, 1.0
set terminal postscript landscape enhanced color  "Times" 8

energyshift=7475

set multiplot layout 6, 3

plot "XESSpec.dat"  u (-$1+energyshift):(-$3 ) title 'x-polarized dn Sz=-1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$5 ) title 'x-polarized up Sz=-1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$7 ) title 'x-polarized dn Sz= 0' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$9 ) title 'x-polarized up Sz= 0' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$11) title 'x-polarized dn Sz= 1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$13) title 'x-polarized up Sz= 1' with lines ls  1

plot "XESSpec.dat"  u (-$1+energyshift):(-$15) title 'y-polarized dn Sz=-1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$17) title 'y-polarized up Sz=-1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$19) title 'y-polarized dn Sz= 0' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$21) title 'y-polarized up Sz= 0' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$23) title 'y-polarized dn Sz= 1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$25) title 'y-polarized up Sz= 1' with lines ls  1

plot "XESSpec.dat"  u (-$1+energyshift):(-$27) title 'z-polarized dn Sz=-1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$29) title 'z-polarized up Sz=-1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$31) title 'z-polarized dn Sz= 0' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$33) title 'z-polarized up Sz= 0' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$35) title 'z-polarized dn Sz= 1' with lines ls  1
plot "XESSpec.dat"  u (-$1+energyshift):(-$37) title 'z-polarized up Sz= 1' with lines ls  1


unset multiplot


set size 1.0, 0.6
intensityscale=48
set out 'XESIsoSpec.ps'

plot "XESIsoSpec.dat" using (-$1+energyshift):((-$3) * intensityscale) title 'isotropic theory' with lines ls 3

]]

-- next we save this string to a file
file = io.open("XESSpec.gnuplot", "w")
file:write(gnuplotInput)
file:close()

-- and finally call gnuplot to execute the script
os.execute("gnuplot XESSpec.gnuplot")
-- as I like pdf to view I transform the format
os.execute(" ps2pdf XESSpec.ps ; ps2pdf XESIsoSpec.ps")