#!/bin/csh -f
# Safe turn-on of SR septa
# $Log: not supported by cvs2svn $

if ("$2" == "") then
	echo "usage: $0 IS1|IS2 <snapshotFile>"
	exit
	endif
set septum = $1
set file = $2
if (! -e $file) then
	echo "file $file does not exist"
	exit
	endif

switch ($septum)
	case IS1:
	case IS2:
		breaksw
	default:
		echo "unknown septum"
		exit
		breaksw
	endsw

caput S:${septum}:ResetSEQ 1
echo "waiting for supply to turn on..."
	while( (`caget -t S:${septum}:StatusCALC`) != "0")
		sleep 2
	end

echo "disabling charge/discharge..."
switch ($septum)
	case IS1:
		caput Mt:Ddg3chan0.GATE 0	
		caput Mt:Ddg3chan2.GATE 0
		breaksw
	case IS2:
		caput Mt:Ddg3chan1.GATE 0
		caput Mt:Ddg3chan3.GATE 0
		breaksw
	endsw


set PV = S:${septum}:VoltageSetSendAO
set value = `sddsprocess $file -pipe=out -match=col,ControlName=$PV | sdds2stream -pipe=in -col=ValueString`

echo "setting $PV to $value..."
caput $PV $value

echo "waiting for ramping of supply..."
set DACval = ""
set DACval = `caget -t S:${septum}:DacAI`
while (`rpnl "${value} ${DACval} - abs 0.1 > ? 1 : 0 $ "` == 1 )
	set DACval = ""
	set DACval = `caget -t S:${septum}:DacAI`
	echo "waiting for DAC to reach setpoint (now at ${DACval})..."
	sleep 10
    end

echo "enabling discharge pulse"
switch ($septum)
	case IS1:
		caput Mt:Ddg3chan2.GATE 1
		breaksw
	case IS2:
		caput Mt:Ddg3chan3.GATE 1
		breaksw
	endsw

sleep 5
echo "enabling charge pulse"
switch ($septum)
	case IS1:
		caput Mt:Ddg3chan0.GATE 1	
		breaksw
	case IS2:
		caput Mt:Ddg3chan1.GATE 1
		breaksw
	endsw

echo "done"

exit



