esrf

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

#%TITLE% monocalib.mac
#%NAME% monocalib.mac
#%DESCRIPTION%
# Procedures to calibrate the monochromator
#%END%

need getbeam_presets.mac
need getbeam.mac

#%UU% ()
#%MDESC% Get the nesessary parameters (pslit motors, undulator motors...)
def mono_calib_parameters() '{
global GB_USE[]

  if (gb_pslit() < 0) {
    if (GB_USE["pslit"] == 0)
      return(-1)
  }
  if (gb_u() < 0) {
    if (GB_USE["undulator"] == 0)
      return(-1)
  }
  if (gb_en() < 0) {
    return(-1)
  }
  if (gb_calibfoils() < 0) {
    if (GB_USE["foil"] == 0)
      return(-1)
  }
}'

#%UU% ()
#%MDESC% Calibrate the monochromator.
def mono_calib() '{
global GB_ENERGY GB_MONO_COARSE
local i cmd foils entmp nb inflval abb abc nn msg

  tty_cntl("md")
  printf ("Calibrating the monochromator\n")
  tty_cntl("me")
  #set the default & specific parameters
  if (mono_calib_parameters() < 0)
    return(-1)

  #save the slits positions
  gb_save_positions()

  #check if the datafile set
  if (DATAFILE == "/dev/null") {
    printf ("No data file selected. Try newsession and start again.\n")
    return(-1)
  }

  cdef("cleanup_once","gb_monocalib_cleanup\n","_gb_")

  #getting the scan parameters
  gb_parameter("mono")

  tty_cntl("md")
  printf ("Monochromator calibration start\n")
  tty_cntl("me")

  eval(sprintf("plotselect %s", GB_CALIB_CTMNE))
  local_ct_set "mono"

  nb = split(GB_CALIB_LIST, entmp)
  if (nb > 1) {   
    printf ("Available energies:\n")
    for (i=0; i<nb; i++) {
      split(entmp[i],foils,":")
      msg = sprintf ("%s\n %d - %s: %f", msg, (i+1), foils[1], foils[0])
    }
    printf ("%s\n", msg)
    choise = input(sprintf ("Please choose form 1 to %d, ^C to exit:\n", nb))
   } else
    choise = 1

  if (GB_USE["filter"] != -1)
    gb_setfilters("mono")

  if (GB_USE["pslit"] != -1)
    gb_setslits("mono")

  split(entmp[choise-1],foils,":")
  printf ("Moving %s to %g\n", GB_EN_MNE, foils[0])
  A[GB_EN_MOT] = foils[0]
  GB_ENERGY = foils[0]
  if (GB_USE["undulator"] != -1) {
    GB_U_SAFETY = -1
    printf ("Moving %s to gap %g\n", GB_U_MNE, GB_GAP)
    A[GB_U_MOT] = GB_GAP
    if (GB_U_SAFETY != -1)
      A[GB_U_SAFETY] = GB_POS
  }
  printf ("Moving %s to %s foil (position %g)\n", \
	GB_CALIB_MNE, foils[1], foils[2])
  A[GB_CALIB_MOT] = foils[2]
  move_em; waitmove; get_angles

  #open the front end
  feopen

  if (GB_MONO_COARSE) {
    cmd = sprintf("dscan %s %g %g %d %g", GB_EN_MNE, \
	GB_PARAMS["coarse"]["start"], GB_PARAMS["coarse"]["stop"], \
	GB_PARAMS["coarse"]["pts"], GB_PARAMS["coarse"]["ctime"])
    eval (cmd)

    printf ("Getting the foils out of the beam\n")
    gb_filters("empty")
    A[GB_CALIB_MOT] = GB_FILTERS["empty"][GB_CALIB_MNE]
    move_em; waitmove; get_angles

    #do the second scan
    eval (cmd)

    abb = getinfl(DATAFILE, SCAN_N-1, SCAN_N)
    if (abb) {
      nn = split(abb,abc)
      local float array inflp[nn]
      for (i=0; i<nn; i++)
        inflp[i]=abc[i]
      inflval = array_op("min",inflp)
      printf ("Moving %s motor to calculated inflection point at %g keV\n", \
	GB_EN_MNE, inflval)
      A[GB_EN_MOT] = inflval
      move_em; waitmove; get_angles     
    }
    printf ("Moving %s to position %g (foil %s)\n", \
	GB_CALIB_MNE, foils[2], foils[1])
    A[GB_CALIB_MOT] = foils[2]
      move_em; waitmove; get_angles
  }
  cmd = sprintf ("dscan %s %g %g %d %g", GB_EN_MNE, \
	GB_PARAMS["normal"]["start"], GB_PARAMS["normal"]["stop"], \
	GB_PARAMS["normal"]["pts"], GB_PARAMS["normal"]["ctime"])
  eval (cmd)

  printf ("Getting the foil out of the beam\n")
  gb_filters("empty")
  A[GB_CALIB_MOT] = GB_FILTERS["empty"][GB_CALIB_MNE]
  move_em; waitmove; get_angles

  #do the second scan
  eval (cmd)

  inflval = getinfl(DATAFILE, SCAN_N-1, SCAN_N)
  if (inflval == -1 )
    printf ("Error when calculating inflection point\n")
  else {
    nn = split(inflval,abc)
    local float array inflp[nn]
    for (i=0; i<nn; i++)
      inflp[i]=abc[i]
    inflval = array_op("min",inflp)
    printf ("Moving %s motor to calculated inflection point at %g keV\n", \
	GB_EN_MNE, inflval)
    A[GB_EN_MOT] = inflval
    move_em; waitmove; get_angles
    printf ("The energy will be reset from %f to %f",inflval, GB_ENERGY)
    if (yesno(" ",0) == 1) {
      eval(sprintf("set %s %f", GB_EN_MNE,GB_ENERGY))
      eval(sprintf("set_dial %s %f", GB_EN_MNE,GB_ENERGY))
    }
    infl_save(inflval, foils[1], SCAN_N-1, inflval, DATAFILE)
  }
  gb_monocalib_cleanup

  tty_cntl("md")
  printf ("Monochromator calibration finished\n")
  tty_cntl("me")  
}'

def getinfl(datfile, scan_n1, scan_n2) '{
local tmpfile pyfile nb

  tmpfile = "/tmp/spec_getinfl.tmp"
  pyfile  = sprintf("%s/local/CalculationServer/infl.py",BLISSADM)

  if ( file_info(pyfile,"-e") == 0 ) {
     printf ("Cannot find python script for inflexion point calculations\n")
     return(-1)
  }

  cmd = sprintf("%s %s %s %s > %s", pyfile, datfile, scan_n1, scan_n2, tmpfile)
  unix(cmd)
  value = getline(tmpfile)
  while (line != -1) {
    line = getline(tmpfile)
    if (line != -1)
      value = line
  }
  getline(tmpfile,"close")
  return(value)
}'

def infl_save(nrj, elem, scan_n, inflval, datfile) '{

  infl_file  = sprintf("%s.infl", datfile)
  on(infl_file)
  printf ("%s - Scan #%d and %d\n", date(), scan_n-1, scan_n)
  msg = sprintf("ENERGY: %s, ELEM: %s, INFL found at: %s", nrj, elem,inflval)
  printf("%s\n",msg)
  close(infl_file)
  fprintf(datfile, "#C %s, %s\n", date(), msg ) 
}'

def mono_scan_inverted() '{
global GB_ENERGY GB_MONO_COARSE
local i cmd foils entmp nb inflval abb abc nn msg

  tty_cntl("md")
  printf ("Doing Inverdet Scan\n")
  tty_cntl("me")
  #set the default & specific parameters
  if (mono_calib_parameters() < 0)
    return(-1)

  #save the slits positions
  gb_save_positions()

  #check if the datafile set
  if (DATAFILE == "/dev/null") {
    printf ("No data file selected. Try newsession and start again.\n")
    return(-1)
  }

  cdef("cleanup_once","gb_monocalib_cleanup\n","_gb_")

  #getting the scan parameters
  gb_parameter("mono")

  tty_cntl("md")
  printf ("Monochromator Inverted Scan start\n")
  tty_cntl("me")

  eval(sprintf("plotselect %s", GB_CALIB_CTMNE))
  local_ct_set "mono"

  nb = split(GB_CALIB_LIST, entmp)
  if (nb > 1) {   
    printf ("Available energies:\n")
    for (i=0; i<nb; i++) {
      split(entmp[i],foils,":")
      msg = sprintf ("%s\n %d - %s: %f", msg, (i+1), foils[1], foils[0])
    }
    printf ("%s\n", msg)
    choise = input(sprintf ("Please choose form 1 to %d, ^C to exit:\n", nb)) 
  } else
    choise = 1

  if (GB_USE["filter"] != -1)
    gb_setfilters("mono")

  if (GB_USE["pslit"] != -1)
    gb_setslits("mono")

  split(entmp[choise-1],foils,":")
  printf ("Moving %s to %g\n", GB_EN_MNE, foils[0])
  A[GB_EN_MOT] = foils[0]
  GB_ENERGY = foils[0]
  if (GB_USE["undulator"] != -1) {
    GB_U_SAFETY = -1
    printf ("Moving %s to gap %g\n", GB_U_MNE, GB_GAP)
    A[GB_U_MOT] = GB_GAP
    if (GB_U_SAFETY != -1)
      A[GB_U_SAFETY] = GB_POS
  }
  printf ("Moving %s to %s foil (position %g)\n", \
	GB_CALIB_MNE, foils[1], foils[2])
  A[GB_CALIB_MOT] = foils[2]
  move_em; waitmove; get_angles

  #open the front end
  feopen

  printf ("Doind forward scan\n")
  cmd = sprintf ("dscan %s %g %g %d %g", GB_EN_MNE, \
	GB_PARAMS["normal"]["start"], GB_PARAMS["normal"]["stop"], \
	GB_PARAMS["normal"]["pts"], GB_PARAMS["normal"]["ctime"])
  eval (cmd)

  printf ("Doind backwards scan\n")
  cmd = sprintf ("dscan %s %g %g %d %g", GB_EN_MNE, \
	GB_PARAMS["normal"]["stop"], GB_PARAMS["normal"]["start"], \
	GB_PARAMS["normal"]["pts"], GB_PARAMS["normal"]["ctime"])
  eval (cmd)

  gb_monocalib_cleanup

  tty_cntl("md")
  printf ("Monochromator Inverted Scan finished\n")
  tty_cntl("me")  
}'

if (whatis("local_ct_set") == 0)
  eval("def local_ct_set\'{ }\'")

#%MACROS%
#%IMACROS%
#%DEPENDENCIES% %B%machinfo.mac%B% %B%getbeam.mac%B% %B%getbeam_presets.mac%B%
#%TOC%
#%AUTHOR% A.Beteva BLISS%BR%
#$Revision: 1.1 $$Date: 2013/05/31 08:35:27 $