#! /bin/tcsh -f # # Scan Theta2 motor and do a curve fit to find the maximum Imono Out # # # set Theta2 = "" set Imono = "" ###################################################################### 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 #################################################################### # user-specified starting point? set manual = `echo "$1" | awk '$1+0>1.0 && $1+0<1.5{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_rock_tmp ~/tune_rock_tmp /tmp/tune_rock_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 if("$manual" != "") then # use a user-specified value as the "center" of the scan set Theta2 = "$manual" blmotor.com Theta2 $Theta2 > /dev/null else set use_local endif try_again: ########################################################################################################## autoscale.com set Theta2 = `blmotor.com Theta2 | awk '! /unknown/{print $NF+0}'` set Imono = `blmotor.com Imono Out | awk '! /unknown/{print $NF+0}'` if("$Imono" == "") goto try_again set Mono_Type = `blmotor.com Mono Type |& awk '{print $NF+0}'` set orig_Theta2 = "$Theta2" set deltas = ( -0.0004 -0.0003 -0.0002 -0.0001 0 0.0001 0.0002 0.0003 0.0004 ) if( $Mono_Type == 1 ) then set deltas = ( -0.004 -0.003 -0.002 -0.001 0 0.001 0.002 0.003 0.004 ) endif echo "Theta2: $Theta2 Imono = $Imono" echo "" # make sure data collection is stopped? # no need for PIN diode here set weak = `echo $Imono | awk '{print ($1<0.005)}'` if("$weak") then echo "x-ray flux in beamline is insignificant... ($Imono)" echo "" echo 'Please wait for the fill to finish' echo 'or dial "RING" on the phone to get the beamline keyed back on.' if($?AUTO) then set BAD = "no beam" goto finish endif echo 'press "Enter" when you have done this:' set nutn = ( $< ) #goto try_again endif if("$weak") unset use_local # 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 (beamline) to "Remote Control"' goto finish endif # register this with the change log change.com "Theta2 $Theta2" if($?use_local) then echo -n "doing Ed's tuneup" autoscale.com on > /dev/null blmotor.com Tune Rocking Curve 0 autoscale.com > /dev/null if($status) then set BAD = "no beam" goto finish endif sleep 1 set Theta2 = `blmotor.com Theta2 | awk '! /unknown/{print $NF+0}'` set Imono = `blmotor.com Imono Out | awk '! /unknown/{print $NF+0}'` echo "Theta2: $Theta2 Imono = $Imono" echo "" endif if($?HURRY) goto finish echo -n "" >! ${tempfile}.data # back up first #set goal = `echo "$Theta2 $deltas[1]" | awk '{print $1+$2}'` #blmotor.com Theta2 $goal > /dev/null foreach delta ( $deltas ) set goal = `echo "$Theta2 $delta" | awk '{print $1+$2}'` echo -n "$goal " | tee -a ${tempfile}.data blmotor.com Theta2 $goal > /dev/null if($status) then set BAD = "error moving Theta2" goto finish endif sleep 1 if($?SLOW) sleep 2 set result = `blmotor.com Imono Out | awk '! /unknown/{print $NF+0}'` echo "$result" | tee -a ${tempfile}.data if("$result" == "") goto try_again end # go back to original position blmotor.com Theta2 $goal > /dev/null # reasonable starting value for fit set Imono = `sort -n +1 ${tempfile}.data | awk '{print $2}' | tail -1` set x0 = `sort -n +1 ${tempfile}.data | awk '{print $1}' | tail -1` set w = `awk -v Theta2=$Theta2 -v Imono=$Imono '{++n; dT += ($1-Theta2)^2; dI += ($2-Imono)^2} END{if(n) print 1/(sqrt(dT/n) * sqrt(dI/n))}' ${tempfile}.data` cat << EOF | gnuplot -persist >&! ${tempfile}.log f(x) = w*(x-x0)**2 + c w=-$w;x0=$x0;c=$Imono; fit f(x) '${tempfile}.data' using 1:2 via w fit f(x) '${tempfile}.data' using 1:2 via x0 fit f(x) '${tempfile}.data' using 1:2 via c fit f(x) '${tempfile}.data' using 1:2 via x0 fit f(x) '${tempfile}.data' using 1:2 via w fit f(x) '${tempfile}.data' using 1:2 via x0 fit f(x) '${tempfile}.data' using 1:2 via w,x0,c set title "Flux vs 2nd xtal Tilt" set timestamp set nokey ${plot}plot '${tempfile}.data' using 1:2, f(x) #set terminal jpeg #set terminal png small color #set output "flux_vs_theta2.png" #plot '${tempfile}.data' using 1:2, f(x) EOF if($?CGI) echo '

' date rm -f fit.log >& /dev/null set newTheta2 = `awk '/^x0/ && $4=="+/-"{print $3}' ${tempfile}.log | tail -1` echo "new Theta2 = $newTheta2" set toofar = `echo $newTheta2 $Theta2 $deltas | awk '{print (sqrt(($1-$2)^2) > $(NF-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` if( ! $toofar &&($curvature == -1)) then set Theta2 = $newTheta2 else # just pick highest value seen set Theta2 = `sort -n +1 ${tempfile}.data | awk '{print $1}' | tail -1` set BAD_FIT endif # move to beginning of scan first (backlash?) set goal = `echo "$Theta2 $deltas[1]" | awk '{print $1+$2}'` #blmotor.com Theta2 $goal > /dev/null # move to optimal value of Theta2 blmotor.com Theta2 $Theta2 > /dev/null sleep 1 set Imono = `blmotor.com Imono Out | awk '! /unknown/{print $NF+0}'` if("$Imono" == "") set BAD_FIT echo "" echo "new Theta2: $Theta2 Imono = $Imono" if(! $?trials) set trials = 1 if(($?BAD_FIT)&&($trials < 3)) 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 Theta2 back to $orig_Theta2" blmotor.com Theta2 $orig_Theta2 exit 9 endif # register this with the change log change.com "Theta2 $Theta2" # clean up if(! $?debug) rm -f ${tempfile}* >& /dev/null