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

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set args $argv
# example: makeSkewQuadknobs -nuy 36.1 -nux 95.1
set usage "makeSkewQuadKnobs -lattice <lattice> {-harmonic <number>|-nuy <integer> -nux <integer>}"

set lattice default
set harmonic ""
set nux ""
set nuy ""
set debug 0
set output skew.cokn
APSParseArguments {lattice harmonic nux nuy debug output}
if ![string length $harmonic] {
    if {![string length $nux] || ![string length $nux] } {
        puts stderr $usage
        exit
    }
} elseif {[string length $nux] || [string length $nux] } {
    puts stderr $usage
    exit
}

if ![string length $harmonic] {
    set harmonic [expr int($nux - $nuy + 0.5)]
}

set baseDir /home/helios/oagData/sr/knobs/skew/lattices
if ![file exists $baseDir] {
    puts stderr "Can't find directory $baseDir"
}
if ![file exists $baseDir/$lattice] {
    puts stderr "Can't find directory $baseDir/$lattice"
}

cd $baseDir/$lattice

# let's just break  down our inhibitions and use the aps.twi file as
# the basis for element names and phase advance (or simlpy s)
set twissFile /home/helios/oagData/sr/lattices/default/aps.twi

if [catch {exec sddsprocess $twissFile -pipe=out \
               -proc=s,last,Circumference \
               -match=col,ElementName=*:SQ*,ElementType=MULT \
               -edit=col,ControlName,ElementName,ei/:PS:SetCurrentC/ \
               | sddsconvert -pipe=in skewQuads.twi \
               -retain=para,nu?,Circumference -retain=col,ControlName,s,eta?,beta?,psi? \
	   } result ] {
    puts stderr $result
}

lappend filesToPurgeOnExit  skewQuads.twi

if [catch {exec sddsprocess skewQuads.twi template.SQ.knob \
               -def=para,Gain,1.0,type=double \
               -print=para,ControlType,pv \
               "-print=para,ControlUnits,1.0 A/click" \
               -define=parameter,StepSize,1.0,type=float \
	   } result ] {
    puts stderr $result
}


lappend filesToPurgeOnExit template.SQ.knob

if [catch {exec sddsprocess \
	       template.SQ.knob sineKnob.sdds \
	       "-print=para,KnobDescription,Harmonic $harmonic Sine Knob" \
               "-print=para,ControlName,Sn:SQ:${harmonic}thSin" \
               "-print=parameter,SetValuePV,Sn:SQ:Sine:SetValueC,type=string" \
	       "-print=parameter,SetRbvPV,Sn:SQ:Sine:ReadbackValueM,type=string" \
	       "-print=parameter,Mode,add,type=string" \
	       "-print=parameter,SetStatusPV,Sn:SQ:Sine:Status,type=string" \
	       "-define=col,Weight,s Circumference / $harmonic * 2 * pi * sin" \
	   } result ] {
    puts stderr $result
}


if [catch {exec sddsprocess \
	       template.SQ.knob cosineKnob.sdds \
	       "-print=para,KnobDescription,Harmonic $harmonic Sine Knob" \
               "-print=para,ControlName,Sn:SQ2:${harmonic}thCos" \
               "-print=parameter,SetValuePV,Sn:SQ:Cosine:SetValueC,type=string" \
	       "-print=parameter,SetRbvPV,Sn:SQ:Cosine:ReadbackValueM,type=string" \
	       "-print=parameter,Mode,add,type=string" \
	       "-print=parameter,SetStatusPV,Sn:SQ:Cosine:Status,type=string" \
	       "-define=col,Weight,s Circumference / $harmonic * 2 * pi * sin" \
	   } result ] {
    puts stderr $result
}
lappend filesToPurgeOnExit sineKnob.sdds cosineKnob.sdds

# the 36th harmonic is for correcting etay. Use SQ2 which has non-zero etax
if [catch {exec sddsprocess \
	       template.SQ.knob 36thHarmonic-cosine.sdds \
               -match=col,ControlName=*SQ2* \
	       "-print=para,KnobDescription,Harmonic 36 Knob" \
	       "-print=para,ControlName,Sn:SQ2:36thCos" \
	       "-print=parameter,SetValuePV,Sn:SQ2:36thCos:SetValueC,type=string" \
	       "-print=parameter,SetRbvPV,Sn:SQ2:36thCos:ReadbackValueM,type=string" \
	       "-print=parameter,Mode,add,type=string" \
	       "-print=parameter,SetStatusPV,Sn:SQ2:36thCos:Status,type=string" \
	       "-define=col,Weight,s Circumference / 36 * 2 * pi * cos" \
	   } result ] {
    puts stderr $result
}
if [catch {exec sddsprocess \
	       template.SQ.knob 36thHarmonic-sine.sdds \
               -match=col,ControlName=*SQ2* \
	       "-print=para,KnobDescription,Harmonic 36 Knob" \
	       "-print=para,ControlName,Sn:SQ2:36thSin" \
	       "-print=parameter,SetValuePV,Sn:SQ2:36thSin:SetValueC,type=string" \
	       "-print=parameter,SetRbvPV,Sn:SQ2:36thSin:ReadbackValueM,type=string" \
	       "-print=parameter,Mode,add,type=string" \
	       "-print=parameter,SetStatusPV,Sn:SQ2:36thSin:Status,type=string" \
	       "-define=col,Weight,s Circumference / 36 * 2 * pi * sin" \
	   } result ] {
    puts stderr $result
}
lappend filesToPurgeOnExit 36thHarmonic-cosine.sdds 36thHarmonic-sine.sdds

# for creating etay "wave"
if [catch {exec sddsprocess \
	       template.SQ.knob 40thHarmonic.sdds \
	       -match=col,ControlName=*SQ2* \
	       "-print=para,KnobDescription,Harmonic 40 Knob" \
	       "-print=para,ControlName,Sn:SQ2:40th" \
	       "-print=parameter,SetValuePV,Sn:SQ2:40th:SetValueC,type=string" \
	       "-print=parameter,SetRbvPV,Sn:SQ2:40th:ReadbackValueM,type=string" \
	       "-print=parameter,Mode,add,type=string" \
	       "-print=parameter,SetStatusPV,Sn:SQ2:40th:Status,type=string" \
	       "-define=col,Weight,1" \
	   } result ] {
    puts stderr $result
}
if [catch {exec sddsprocess \
	       template.SQ.knob 20thHarmonic.sdds \
	       -match=col,ControlName=*SQ2* \
	       "-print=para,KnobDescription,Harmonic 20 Knob" \
	       "-print=para,ControlName,Sn:SQ2:20th" \
	       "-print=parameter,SetValuePV,Sn:SQ2:20th:SetValueC,type=string" \
	       "-print=parameter,SetRbvPV,Sn:SQ2:20th:ReadbackValueM,type=string" \
	       "-print=parameter,Mode,add,type=string" \
	       "-print=parameter,SetStatusPV,Sn:SQ2:20th:Status,type=string" \
	       "-define=col,Weight,s Circumference / 20 * 2 * pi * sin" \
	   } result ] {
    puts stderr $result
}

lappend filesToPurgeOnExit 40thHarmonic.sdds 20thHarmonic.sdds

if [catch {exec sddscombine -overWrite\
             sineKnob.sdds cosineKnob.sdds 36thHarmonic-sine.sdds 36thHarmonic-cosine.sdds 40thHarmonic.sdds 20thHarmonic.sdds $output \
         } result ] {
    puts stderr $result
}

if !${debug} {
    eval file delete $filesToPurgeOnExit
}

exit

# Local Variables:
# mode: tcl
# End:
