#!/bin/bash

sddsprocess pls3.twi -nowarning -ifnot=param,C -process=s,max,C

# Select out the skew quads by looking at the TILT
# Set the K1 values to the coupling harmonic
# 0.01/sqrt(betax*betay)*sin(psix-psiy - (nux - nuy - q)*2*Pi*s/C), where
# q=44 is integer tune difference
# See section 3.1.4.4 of the Handbook of Accelerator Physics and Engineering

sddsprocess pls3.param -pipe=out \
    -match=col,ElementParameter=TILT -match=col,ElementType=KQUAD \
    -filter=col,ParameterValue,`rpnl pi 4 / .01 -`,`rpnl pi 4 / .01 +` \
    -reprint=col,ElementParameter,K1 \
    | sddsxref -pipe -match=ElementName -take=s,beta?,psi?  pls3.twi -transfer=param,nu?,C \
    | sddsprocess -pipe=in pls3-skewWave.param \
    "-define=param,q,nux nuy - 0.5 + int,type=short" \
    -redefine=col,ParameterValue,"betax betay * sqrt rec psix psiy - nux nuy - q - 2 * pi * s * C / - sin 100 /" 

sddsplot -col=s,ParameterValue pls3-skewWave.param -graph=sym,fill
