#!/bin/sh  
# \
exec tclsh "$0" "$@"

# make a file with a sequence of values of delta
exec sddssequence deltaValues.sdds \
    "-define=delta,type=double,symbol=\$gd\$r" \
    -sequence=begin=-0.020,end=0.020,delta=0.005

# do tracking for each value of delta

if ![file exists sp8Track.log] {
    catch {exec elegant sp8Track.ele -macro=deltaFile=deltaValues.sdds > sp8Track.log}
}


# add the twiss file parameter data to the aperture output
exec sddsxref sp8Track.aper sp8Track.twi -leave=* -transfer=param,* -pipe=out \
 | sddsprocess -pipe=in sp8Track.aper1 \
    "-print=parameter,Label1,\$gd\$r: %.4f    \$gn\$r\$bx\$n: %.4f    \$gn\$r\$bx\$n: %.4f,MAL.DP,nux,nuy"

# plot DA
exec sddsplot -split=page -separate=page -groupby=page -layout=3,3 \
    -column=x,y sp8Track.aper1 -graph=symbol,type=0 -title=@Label1 \
    -column=x,y sp8Track.abnd -graph=symbol,type=1,connect -omnipresent &

# add the delta variation parameter data to the twiss output
exec sddsxref sp8Track.twi sp8Track.aper -leave=* -transfer=param,* -pipe=out \
 | sddsprocess -pipe=in sp8Track.twi1 \
    "-print=parameter,Label1,\$gd\$r: %.4f    \$gn\$r\$bx\$n: %.4f    \$gn\$r\$bx\$n: %.4f,MAL.DP,nux,nuy"

# plot orbit and beta functions
exec sddsplot -split=page -separate=request -groupby=page,request -layout=1,2 \
    -column=s,x sp8Track.clo -same=y "-topline=closed orbit" \
    -column=s,beta? sp8Track.twi1 -title=@Label1 -graph=line,vary -same=y -unsup=y \
    "-topline=beta functions" \
    -column=s,Profile sp8Track.mag -omnipresent -overlay=xmode=norm,yfact=0.04 &
 
# list chromaticity
exec sdds2stream sp8Track.twi -param=dnux/dp &


