esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

#$Log:$
#%TITLE% getbeam_aux.mac
#%NAME% getbeam_aux.mac
#%DESCRIPTION%
# Auxilary procedures used to find the beam.
#%END%

#%IU%(sfor,cnt)
#%MDESC%Find the zero, using the %B%cnt%B% counter data and %B%sfor%B%
#treshold.
def slit_find_zero(sfor,cnt) '{
local min max idxmin
local retpos seachfor
local float array avarr[(NPTS-1)/3][1]

  # contract the array by three to get averaging on every three points
  avarr = array_op("contract", SCAN_D[0:NPTS][cnt+1],5)
  max   = array_op("max", avarr)
  min   = array_op("min", avarr)
  seachfor = (max - min) * sfor + min
array_op("min", avarr)
  idxmin = array_op("i_<=_value", SCAN_D[0:NPTS-1][cnt+1],seachfor)
  retpos = SCAN_D[idxmin-1][0]
  return(retpos)
}'

#%IU% (cnt)
#%MDESC% Find the centre of the beam, using the %B%cnt%B% counter data.
def slit_find_centre(cnt) '{
local lhmx uhmx
local head_pts tail_pts
local cen max

  if (NPTS < 8) {
    # not enough points
    return (GB_WRONG_CENTRE)
  }

  head_pts = array_op("sum",SCAN_D[0:4][cnt+1])
  tail_pts = array_op("sum",SCAN_D[NPTS-6:NPTS-1][cnt+1])
  printf("%g, %g\n", head_pts, tail_pts)

  max = array_op("max",SCAN_D[0:NPTS-1][cnt+1])

  printf ("Head average: %g; Tail average: %g; Max is %g\n", \
	head_pts/5, tail_pts/5, max)

  lhmx = array_op("lhmx",SCAN_D[0:NPTS-1][0], SCAN_D[0:NPTS-1][cnt+1])
  uhmx = array_op("uhmx",SCAN_D[0:NPTS-1][0], SCAN_D[0:NPTS-1][cnt+1])
  cen = (lhmx + uhmx)/2
  return (cen)
}'

#%IU%()
#%MDESC% Check if there is beam. Return -1 if no beam, 0 if OK.
def gb_beampresence() '{
global GB_ISBEAM

  GB_ISBEAM = 0

  gb_beampresence_local

  return(GB_ISBEAM)
}'

#%IU% (mnum,spos,fpos,npts,tim,cnum)
#%MDESC% Align the gap and offset to the centre of the beam.
def scan_slitpseudo(mnum,spos,fpos,npts,tim,cnum) '{
local cen

  printf ("\t - scanning %s\n", motor_mne(mnum))
  _m[0] = mnum; _s[0] = A[mnum]+ spos; _f[0] = A[mnum]+fpos;
  _n1 = npts; _ctime = tim; _nm = 1
  _ascan

  #cen = array_op("x_at_max",SCAN_D[0:NPTS-1][0],SCAN_D[0:NPTS-1][cnum+1])
  cen = slit_find_centre(cnum)
  if (cen == GB_WRONG_CENTER) {
     eprintf ("STOP: cant find center\n")
     return(GB_WRONG_CENTER)
  }
  printf ("\t - moving %s to centre %g\n", motor_mne(mnum), cen)
  A[mnum] = cen
  move_em; waitmove; get_angles

  return(cen)
}'

#%IU% (mnum,spos,fpos,npts,tim,cnum,sfor,offset)
#%MDESC% Find the zero of the real slit motors.
def scan_slitreal(mnum,spos,fpos,npts,tim,cnum,sfor) '{
local zeropos mmne str

  mmne = motor_mne(mnum)
  printf("Looking for zero real motor %s\n", mmne)

  _f[0] = fpos;
  _m[0] = mnum; _s[0] = spos; 
  _n1 = npts; _ctime = tim; _nm = 1
  printf ("\t - scanning %s, %g, %g\n", mmne, _s[0], _f[0])
  _ascan

  zeropos = slit_find_zero(sfor,cnum)
  printf ("\t - moving %s to zero position %g\n", mmne, zeropos)
  A[mnum] = zeropos
  move_em; waitmove; get_angles

  return(nobeam)
}'

def slitbox_realign() '{
global SB_FESTAT
local cnt

  SB_FESTAT = _check_fe(1)
  if (SB_FESTAT == -1) {
    msg =  sprintf ("cannot open the Front End, slitbox realignment aborted")
    printf("%s\n", msg)
    gui_logmsg(msg)
    return(-1)
  }
  cdef("cleanup_once","slitbox_realign_cleanup\n","_slitbox_realign_")

  close_ms_shutter
  scoff
  #bsout
  if (mattset0() < 0)
    return (-1)

  eval(sprintf("plotselect %s", GB_SB_CTMNE))
  cnt = cnt_num(GB_SB_CTMNE)
  splot
  lupsetup COM NO
  mcaoff

  gain_set_local

  GB_SB_MOT["s1h"] = 0.1; GB_SB_MOT["s1v"] = 0.1
  GB_SB_MOT["s2h"] = 0.12; GB_SB_MOT["s2v"] = 0.12
  move_em; waitmove; get_angles

  prodc_open_safety_shutter
  tcount(1); wait(2); getcounts
  msg = sprintf ("Before the slitbox realignment: %s=%g", GB_SB_CTMNE, S[cnt])
  printf("%s\n", msg)
  gui_logmsg(msg)
  if (S[cnt] == 0)
    return(-1)
  GB_SB_MOT["s2h"] = 1.5; GB_SB_MOT["s2v"] = 1.5
  move_em; waitmove; get_angles

  #### first pair of slits
  scan_slitreal(GB_SB_MOT["s1h"],0.7,1,80,0.1,GB_SB_CTMNE,0.1)
  GB_SB_MOT["s1h"] = 0.1; move_em; waitmove; get_angles
  scan_slitreal(GB_SB_MOT["s1v"],0.7,1,80,0.1,GB_SB_CTMNE,0.1)

  GB_SB_MOT["s1h"] = 1; GB_SB_MOT["s1v"] = 1
  printf ("%s and %s slits finished\n", GB_SB_MNE["s1h"], GB_SB_MNE["s1v"])

  lineup t1h -0.5 0.5 100 0.1
  lineup t1v -0.5 0.5 100 0.1

  ### second pair of slits
  scan_slitreal(GB_SB_MOT["s2h"],0.6,1.3,80,0.1,GB_SB_CTMNE,0.1)
  GB_SB_MOT["s2h"] = 0.12; move_em; waitmove; get_angles
  scan_slitreal(GB_SB_MOT["s2v"],0.6,1.3,80,0.1,GB_SB_CTMNE,0.1)
  GB_SB_MOT["s2v"] = 0.12; move_em; waitmove; get_angles

  GB_SB_MOT["s1h"] = 0.1; GB_SB_MOT["s1v"] = 0.1
  printf ("%s and %s slits finished\n", GB_SB_MNE["s2h"], GB_SB_MNE["s2v"])

  lineup t2h -0.5 0.5 100 0.1
  lineup t2v -0.5 0.5 100 0.1

  # set the values of motor positions of all slit offsets to zero
  # this should avoid problem with slit scon stuff. GAL 24/8/2005

  for (ii = 1; ii<3; ii++) {
    chg_dial(GB_SB_MOT[sprinf("t%1dh%",ii)],0)
    chg_offset(GB_SB_MOT[sprinf("t%1dh%",ii)],0)
    chg_dial(GB_SB_MOT[sprinf("t%1dv%",ii)],0)
    chg_offset(GB_SB_MOT[sprinf("t%1dv%",ii)],0)
  }

  open_ms_shutter
  tcount(1); wait(2); getcounts
  msg = sprintf ("After the slitbox realignment: %s=%g", GB_SB_CTMNE, S[cnt])
  close_ms_shutter
  printf("%s\n", msg)
  gui_logmsg(msg)

  slitbox_realign_cleanup
}'

def slit_setlim(mnum, offset) '{
local pos mmne

  mmne =  motor_mne(mnum)
  if (motor_par(mnum,"step_size") < 0) {
    printf ("\t - looking for %s negative limit\n", mmne)
    str = "lim-"
  } else {
    printf ("\t - looking for %s positive limit\n", mmne)
    str = "lim+"
  }
  chg_dial(mnum, str)
  _show_waitf()
  get_angles

  pos = A[mnum] - offset
  A[mnum] = pos
  move_em; waitmove; get_angles
  
  eval(sprintf("set_lm %s -10 %g", mmne, pos+0.05))
  return(pos)
}'

def search_max_gaps(key,pos) '{
local mmot i lims str
global max_gap
  if (key == "pslit") {
    mmot[0] = GB_PSLIT_MOT["psu"]
    mmot[1] = GB_PSLIT_MOT["psd"]
    mmot[2] = GB_PSLIT_MOT["psl"]
    mmot[3] = GB_PSLIT_MOT["psr"]
    mmot["vg"] = GB_PSLIT_MOT["psvg"]
    mmot["hg"] = GB_PSLIT_MOT["pshg"]
  }
  for (i=0; i<4; i++) {
    eval (sprintf("set_lm %s -100 100",mmot[i]))
    A[mmot[i]] = 98
  }
  move_em; waitmove; get_angles
  for (i=0; i<4; i++) {
    A[mmot[i]] -= 0.1
  }
  move_em; waitmove; get_angles

  max_gap["vg"] = A[mmot["vg"]]
  max_gap["hg"] = A[mmot["hg"]]
  eval (sprintf("set_lm %s %g %g",mmot["vg"], -A[mmot["vg"]], A[mmot["vg"]]))
  eval (sprintf("set_lm %s %g %g",mmot["hg"], -A[mmot["hg"]], A[mmot["hg"]]))
}'

def slit_search_limits(mmot) '{
local mmne

  mmne = motor_mne(mmot)
  eval (sprintf("set_lm %s -40 40", mmne))
  if (motor_par(mmot,"step_size") > 0) {
    printf ("\t - looking for %s negative limit\n", mmne)
    str = "lim-"
  } else {
    printf ("\t - looking for %s positive limit\n", mmne)
    str = "lim+"
  }
  chg_dial(mmot, str)
  _show_waitf()
  get_angles
  
  A[mmot] += 0.1
  move_em; waitmove; get_angles  
  chg_offset(mmot,0)
}'
#%MACROS%
#%IMACROS%
#%DEPENDENCIES% %B%id.mac%B%
#%TOC%
#%AUTHOR% A.Beteva%BR%
#$Revision: 1.2 $$Date: 2005/09/15 14:08:18 $