#! /bin/csh -f # # Move the sample into the beam # # set start_time = `echo "puts [clock seconds]" | tclsh` set path = ( /programs/kuim/bin $path ) #set debug set reso = 704x480 #set reso = 352x240 # decide which camera we can see through set camera = `stage.com |& awk '$(NF-1)~/[0-9]/{print 1+($(NF-1)<-10)}'` # fractional coordinates of beam center set center = `beam.com |& awk 'NF>1{print $(NF-1),$NF}' | tail -1` # microscope camera field of view (in microns) set fov = `fov.com |& awk 'NF>1{print $(NF-1),$NF}' | tail -1` # back-up plan if this fails if($#center != 2) set center = ( 0.5 0.5 ) if($#fov != 2 && $camera == 1) set fov = ( 0.586 -0.461 ) if($#fov != 2 && $camera == 2) set fov = ( 4.435 2.856 ) set delta = `echo $fov $reso | awk '{print 20*sqrt($1*$1)/$3}'` set blur = 0 set xsize = `echo $reso | awk -F "x" '{print $1}'` set ysize = `echo $reso | awk -F "x" '{print $2}'` set fftsize = `echo $reso | awk '{fft=1; while($1>2) {$1/=2;fft*=2} print fft}'` echo "first picture" echo "puts [expr [clock seconds] - $start_time]s" | tclsh # total number of images taken set n = 0 # current image number set i = 0 # "other" image number set j = 0 # difference image number set k = 0 rm -f snap?.jpg >& /dev/null pictures: @ n = ( $n + 1 ) snap.com snap${n}.jpg -nodisplay $reso >& /dev/null sample.com 0 0 -$delta echo "move complete" echo "puts [expr [clock seconds] - $start_time]s" | tclsh @ n = ( $n + 1 ) snap.com snap${n}.jpg -nodisplay $reso >& /dev/null sample.com 0 0 $delta & #animate -delay 20 snap?.jpg echo "images taken..." echo "puts [expr [clock seconds] - $start_time]s" | tclsh # process these images while ( $i < $n ) @ i = ( $i + 1 ) # convert the jpegs into KUIM format (maybe smooth them too) rm -f out.im type -f snap${i}.jpg out.im > /dev/null if($blur != 0) then mv out.im in.im interp -x $fftsize -y $fftsize in.im out.im > /dev/null mv out.im in.im gauss -s $blur in.im out.im > /dev/null # mv out.im in.im # ideal -f 10 100 in.im out.im mv out.im in.im interp -x $xsize -y $ysize in.im out.im > /dev/null echo "smoothing done..." rm -f blurred${i}.jpg ; type -j out.im blurred${i}.jpg echo "puts [expr [clock seconds] - $start_time]s" | tclsh endif mv out.im snap${i}.im # calculate the (n-1)^2 differences set j = 1 while ( $j < $i ) # subtract the two images rm -f out.im add -a 1 -b -1 snap${i}.im snap${j}.im out.im > /dev/null rm -f diff.jpg ; type -j out.im diff.jpg # square the difference mv out.im in.im mult -p in.im in.im out.im > /dev/null @ j = ( $j + 1 ) @ k = ( $k + 1 ) mv out.im sqr${k}.im rm -f sqr.jpg ; type -j sqr${k}.im sqr.jpg end end echo "differences calculated..." echo "puts [expr [clock seconds] - $start_time]s" | tclsh # make a "zero" feild first rm -f msq.im add -a 0 -b 0 sqr1.im sqr1.im msq.im > /dev/null set l = 0 while ( $l < $k ) @ l = ( $l + 1 ) # accumulate square deviates cp sqr${l}.im in.im add -a 1 -b 1 in.im msq.im out.im > /dev/null mv out.im msq.im end echo "differences summed..." echo "puts [expr [clock seconds] - $start_time]s" | tclsh greymap -s 0 10000 msq.im out.im > /dev/null mv out.im msq.im set newscale = `header msq.im | awk '/Mean/{print -$NF,10000-$NF}'` greymap -s $newscale msq.im out.im > /dev/null mv out.im msq.im if( $?debug ) then rm -f contours.im #type -f snap1.jpg contours.im > /dev/null add -a 0 -b 0 sqr1.im sqr1.im contours.im > /dev/null foreach sigma ( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) cp msq.im out.im mv out.im in.im set thresh = `header in.im | awk -v sigma=$sigma '/^Mean/{mean = $NF} /^StdDev/{sd = $NF} END{print mean+sigma*sd}'` thresh -t $thresh in.im out.im > /dev/null if ( `header out.im | awk '/Mean/{print ($NF+0==0)}'` ) break mv out.im in.im contour -d in.im out.im contour.txt > /dev/null rm -f over.im add contours.im out.im over.im > /dev/null mv over.im contours.im end rm -f contours.jpg type -j contours.im contours.jpg > /dev/null #view.com contours.im echo "contours.jpg done at $sigma sigma " echo "puts [expr [clock seconds] - $start_time]s" | tclsh endif # estimate signal produced by the move cp msq.im out.im mv out.im in.im header in.im set signal = `header in.im | awk '/^Image Range/{max=substr($NF,index($NF,"..")+2)+0} /^StdDev/{sd = $NF} END{printf "%d", max/sd}'` #set signal = `header in.im | awk '/^Mean/{mean = $NF} /^StdDev/{sd = $NF} END{printf "%d", (10000-mean)/sd}'` echo "signal/noise: $signal" #histo msq.im echo "puts [expr [clock seconds] - $start_time]s" | tclsh # identify tip as leftmost 2*sigma peak set thresh = `header in.im | awk '/^Mean/{mean = $NF} /^StdDev/{sd = $NF} END{print mean+2*sd}'` thresh -t $thresh in.im out.im > /dev/null header out.im rm -f show.jpg ; type -j out.im show.jpg >& /dev/null #thresh -o in.im out.im > /dev/null mv out.im in.im contour -d in.im out.im contour.txt > /dev/null rm -f over.im >& /dev/null add snap1.jpg out.im over.im > /dev/null rm -f over.jpg >& /dev/null type -j over.im over.jpg > /dev/null rm -f contours.im msq.im over.im # shave 10% off the edges of the image (avoid termination artefacts) cat contour.txt |\ awk -v xsize=$xsize -v ysize=$ysize '\ $1>xsize*0.1 && $1ysize*0.1 && $2! sorted.txt set tip = `awk '{print $0;exit}' sorted.txt` if("$tip" == "") set BAD echo "tip: $tip" set tip_X = `awk '{print $1;exit}' sorted.txt` set tip_Y = `awk -v X=$tip_X '{++n; sum+= $2} n+0>0 && $1!=X{printf "%d", sum/n; exit}' sorted.txt` echo "tip: $tip_X $tip_Y" set tip = `echo "$tip_X $tip_Y $xsize $ysize" | awk '{print $1/$3, $2/$4}'` echo "tip: $tip" echo "puts [expr [clock seconds] - $start_time]s" | tclsh # calculate needed move in mm set move = `echo " $center $tip $fov" | awk '{print $6*($2-$4), -$5*($1-$3)}'` #set move = `echo "$move $delta" | awk '{print $1,$2+$3}'` #set move = `echo "$move $delta" | awk '{print $1+$3,$2}'` set toobig = `echo $move $fov | awk '{printf (sqrt(($1)^2+($2)^2) > $3/2)}'` if($toobig) then set BAD = "calculated move is too big." goto exit endif echo "sample.com to "`sample.com | awk 'END{print $5,$6,$7}'` echo "sample.com 0 $move" wait if ($signal >= 5) then if(! $?debug) sample.com 0 $move else set delta = `echo $delta | awk '{print 2*$1}'` # if($n < 10) goto pictures set BAD = "unable to see crystal. signal/noise = $signal" goto exit endif echo "puts [expr [clock seconds] - $start_time]s" | tclsh exit: if ($?BAD) then echo "ERROR: $BAD" exit 9 endif #rm -f snap*.jpg sorted.txt contour.txt in.im out.im rm -f snap?.im sqr?.im >& /dev/null exit cat contour.txt |\ awk '/^START/,/^-1/{print} /^-1/{exit}' |\ ~/Develop/2D_plot.awk |\ cat >! shit.pgm display shit.pgm cat contour.txt |\ awk '$1>50 && $1<650 && $2>50 && $2<430 {print}' |\ sort -n |\ ~/Develop/2D_plot.awk |\ cat >! shit.pgm display shit.pgm end exit min_profile: # mimimize the cross-section of the centered sample by rotating phi set camera = 1 echo -n "" >! ${tempfile}.rot foreach phi ( `awk 'BEGIN{for (i=0;i<=180;i+=30) print i}'` ) phi.com $phi > /dev/null snap.com snap.jpg -nodisplay >& /dev/null echo -n "$phi " | tee -a ${tempfile}.rot convert -normalize -negative -threshold 32768 snap.jpg mask.jpg header mask.jpg |\ awk 'NR>6{printf " %s ", $NF}' |\ tee -a ${tempfile}.rot echo "" | tee -a ${tempfile}.rot end set min = `sort -n +1 ${tempfile}.rot | awk '{print $1; exit}'` echo -n "" >! ${tempfile}.rot foreach phi ( `echo $min | awk '{for (i=$1-20;i<=$1+20;i+=5) print i}'` ) phi.com $phi > /dev/null snap.com snap.jpg -nodisplay >& /dev/null echo -n "$phi " | tee -a ${tempfile}.rot convert -normalize -negative -threshold 32768 snap.jpg mask.jpg header mask.jpg |\ awk 'NR>6{printf " %s ", $NF}' |\ tee -a ${tempfile}.rot echo "" | tee -a ${tempfile}.rot end set min = `sort -n +1 ${tempfile}.rot | awk '{print $1; exit}'` phi.com $min exit set delta = 0.02 snap.com scooch2_${delta}_1.jpg -nodisplay >& /dev/null ~/beamline/sample.com 0 0.00 $delta snap.com scooch2_${delta}_2.jpg -nodisplay >& /dev/null ~/beamline/sample.com 0 $delta 0 snap.com scooch2_${delta}_3.jpg -nodisplay >& /dev/null ~/beamline/sample.com 0 0.00 -$delta snap.com scooch2_${delta}_4.jpg -nodisplay >& /dev/null ~/beamline/sample.com 0 -$delta 0