#! /bin/tcsh -f
#
# Scan M2 Tilt motor and do a curve fit to find the maximum Iend
# or: steer beam up and down to put it back through the pinhole
#
#
set M2_Tilt = ""
set Iend = ""
if(! $?plot) set plot = ""
if(! $?beamline) then
# default to beamline this computer belongs to
setenv beamline `beamline.com`
if($status) then
echo "ERROR: $beamline"
exit 9
endif
endif
#if(! $?beamline) setenv beamline 831
# lower limit before beam is considered "off" (nA)
set min_Izero = 0.001
set min_Iend = 50
set min_Iend = 20
# user-specified starting point?
set manual = `echo "$1" | awk '$1~/^[0-9-]/ && $1+0>-4000 && $1+0<45000{print $1+0}'`
if("$1" == "debug") set debug
if("$1" == "slow") set SLOW
# find a writable temporary file name
if(! $?home) set home
foreach tempfile ( ./tune_M2_tmp ~/tune_M2_tmp /tmp/tune_M2_tmp$$ )
touch ${tempfile} >& /dev/null
if(! $status) break
end
rm -f ${tempfile}
# decide how to print output
test -t 1
if($status) set AUTO
if($?CGI) set AUTO
diode.com in
shutter.com open
if("$manual" != "") then
# use a user-specified value as the "center" of the scan
set M2_Tilt = "$manual"
set force_Iend
blmotor.com M2 Tilt $M2_Tilt > /dev/null
sleep 1
else
set use_local
endif
# got multilayers?
if( `blmotor.com Mono Type |& awk '{print $NF+0}' | tail -1` ) then
# multilayers are engaged
set deltas = ( -40 -20 -10 0 10 20 40 )
else
set deltas = ( -40 -20 -10 0 10 20 40 )
endif
try_again:
##########################################################################################################
autoscale.com
if($status) then
set BAD = "no beam"
goto finish
endif
set M2_Tilt = `blmotor.com M2 Tilt | awk '! /unknown/{print $NF+0}'`
set orig_M2_Tilt = $M2_Tilt
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
set Izero = `blmotor.com Izero | awk '! /unknown/{print $NF+0}'`
set lastgood = `tac /data/log/change.log | awk '/M2_Tilt/ && $NF>3000{print $(NF-2)}' | head -1`
echo "M2 Tilt = $M2_Tilt Iend = $Iend"
echo ""
# make sure beam goes into the hutch!
set weak = `echo $Izero $min_Izero | awk '{print ($1<$2)}'`
if(("$weak")&&(! $?force_Izero)) then
echo "Flux into hutch is insignificant... "
echo ""
echo "Please push the PHOTONS ON button on the hutch."
if($?AUTO) then
echo "and run this again."
set BAD = "no beam in hutch"
goto finish
endif
echo 'press "Enter" when you have done this:'
set nutn = ( $< )
set force_Izero
goto try_again
endif
# check if M2 is just too far off
set weak = `echo $Iend $min_Iend | awk '{print ($1<$2)}'`
if(($weak) && (! $?force_Iend)) then
# check if M2 Tilt is just WAY off
if ( `echo $M2_Tilt $lastgood | awk '{print (sqrt(($1-$2)^2) > 10)}'` ) then
echo "resetting M2 Tilt to $lastgood"
set M2_Tilt = $lastgood
blmotor.com M2 Tilt $M2_Tilt
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
set weak = `echo $Iend $min_Iend | awk '{print ($1<$2)}'`
endif
endif
if(($weak) && (! $?force_Iend)) then
# check if M2 Tilt is just WAY off
if ( `echo $M2_Tilt $lastgood | awk '{print (sqrt(($1-$2)^2) > 100)}'` ) then
echo "resetting M2 Tilt to $lastgood"
blmotor.com M2 Tilt $lastgood
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
endif
endif
if("$weak") unset use_local
# make sure the PIN diode is in the beam!
set weak = `echo $Iend $min_Iend | awk '{print ($1<$2)}'`
if(("$weak")&&(! $?force_Iend)) then
echo "Flux into diode is insignificant... "
echo ""
echo "Please insert the PIN diode and open all shutters"
if($?AUTO) then
echo "and run this again."
exit
endif
echo 'press "Enter" when you have done this:'
set nutn = ( $< )
set force_Iend
goto try_again
endif
# make sure no autoscales occur during run
set test = `echo "autooff\r" | sock_exchange.tcl beamline 10001 1 | grep Local`
if("$test" != "") then
set BAD = 'unable to control beamline. please switch the beamline control computer (bl'${beamline}'b) to "Remote Control"'
goto finish
endif
# register this with the change log
change.com "M2_Tilt $M2_Tilt Iend: $Iend"
if($?use_local) then
echo -n "doing Ed's tuneup"
autoscale.com on > /dev/null
if($status) then
set BAD = "no beam"
goto finish
endif
blmotor.com Tune M2 Tilt 0
autoscale.com > /dev/null
if($status) then
set BAD = "no beam"
goto finish
endif
sleep 1
set M2_Tilt = `blmotor.com M2 Tilt | awk '! /unknown/{print $NF+0}'`
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
echo "M2 Tilt: $M2_Tilt Iend = $Iend"
echo ""
endif
if($?HURRY) goto finish
echo -n "" >! ${tempfile}.plot
foreach delta ( $deltas )
set goal = `echo "$M2_Tilt $delta" | awk '{printf "%d", $1+$2}'`
echo -n "$goal " | tee -a ${tempfile}.plot
blmotor.com M2 Tilt $goal > /dev/null
if($status) then
echo "unable to move motor."
echo 'please switch the beamline control computer (bl'${beamline}'b) to "Remote Control"'
#exit 2
endif
usleep 500000
if($?SLOW) sleep 2
set result = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
if("$result" == "") goto try_again
echo "$result" | tee -a ${tempfile}.plot
end
# go back to original position
blmotor.com M2 Tilt $M2_Tilt > /dev/null
cat << EOF | gnuplot -persist >&! ${tempfile}.log
f(x) = w*(x-x0)**2 + c
x0=$M2_Tilt;c=$Iend;
fit f(x) '${tempfile}.plot' using 1:2 via w
fit f(x) '${tempfile}.plot' using 1:2 via x0
fit f(x) '${tempfile}.plot' using 1:2 via c
fit f(x) '${tempfile}.plot' using 1:2 via w
fit f(x) '${tempfile}.plot' using 1:2 via x0
fit f(x) '${tempfile}.plot' using 1:2 via c
fit f(x) '${tempfile}.plot' using 1:2 via w
fit f(x) '${tempfile}.plot' using 1:2 via x0
fit f(x) '${tempfile}.plot' using 1:2 via c
fit f(x) '${tempfile}.plot' using 1:2 via w,x0,c
set title "flux vs vertical beam position"
set timestamp
set nokey
${plot}plot '${tempfile}.plot' using 1:2, f(x)
#set terminal png small color
#set output "flux_vs_M2.png"
#plot '${tempfile}.plot' using 1:2, f(x)
EOF
if($?CGI) echo '
'
date
if($?CGI) echo '
'
rm -f fit.log >& /dev/null
set new_M2_Tilt = `awk '/^x0/ && $4=="+/-"{printf "%d\n", $3}' ${tempfile}.log | tail -1`
set curvature = `awk '/^w / && $4=="+/-"{print $3/sqrt($3*$3)}' ${tempfile}.log | tail -1`
set quality = `awk '/^final sum of squares of residuals/{printf "%d\n", $NF+0}' ${tempfile}.log | tail -1`
set delta = `echo $M2_Tilt $new_M2_Tilt | awk '{printf "%d", sqrt(($1-$2)^2)}'`
echo "quality: $delta $curvature $quality"
set test = `echo $delta $deltas | awk '{print ($1 < $NF)}'`
if(($test)&&($curvature == -1)) then
set M2_Tilt = $new_M2_Tilt
else
set M2_Tilt = `sort -n +1 ${tempfile}.plot | awk '{print $1}' | tail -1`
set BAD_FIT
endif
# go to new position
if(! $?debug) blmotor.com M2 Tilt $M2_Tilt > /dev/null
usleep 200000
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
if("$Iend" == "") set BAD_FIT
echo ""
echo "new M2 Tilt: $M2_Tilt"
if(! $?trials) set trials = 1
if(($?BAD_FIT)&&($trials < 5)) then
unset BAD_FIT
@ trials = ( $trials + 1 )
echo "lets try that again..."
goto try_again
endif
if($?BAD_FIT) set BAD = "bad fit"
finish:
# did something bad happen?
if($?BAD) then
echo "putting M2 Tilt back to $orig_M2_Tilt"
blmotor.com M2 Tilt $orig_M2_Tilt
echo "ERROR: $BAD"
exit 9
endif
# register this with the change log
change.com "M2_Tilt $M2_Tilt Iend: $Iend"
# clean up
if(! $?debug) rm -f ${tempfile}* >& /dev/null
exit