esrf

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

#%TITLE% MXAUTOCHOOCH.MAC
#%NAME% 
#  Runs the autochooch script
#
#%CATEGORY% MX, Tools
#
#%DESCRIPTION%
#  Runs the autochooch script for the analysis of energy
#  scans for MX beamlines
#
#%LOG%
#$Log: mxautochooch.mac,v $
#Revision 1.7  2008/08/12 14:39:32  rey
#documentation changes
#
#Revision 1.6  2006/11/20 09:55:27  spruce
#put back order of energies for returning to mxcube.
#That is pk,ip,rm instead of ip,pk,rm.
#This is to remain compatible with the other local macros.
#
#Revision 1.5  2006/11/13 14:23:13  spruce
#swapped ip and pk energies in print chooch. Seems like mxCuBE wants the normal order.
#
#Revision 1.4  2006/09/12 12:37:22  guijarro
#added timeout when waiting for chooch results
#
#Revision 1.3  2006/02/15 08:52:12  beteva
#moved the creation of the chooch files from the macro to the script. Cleanup.
#
#Revision 1.2  2005/11/10 15:55:03  beteva
#changed the search for the chooch script to be in ~blissadm/local/bin"
#
#Revision 1.1  2005/11/08 17:23:58  beteva
#Initial revision
#
#%END%

#%UU% [prefix element edge]
#%MDESC% Macro to run the autochooch script. Print the fp and fpp energies
#[keV].
def auto_chooch '{
local prefix elem edge
local ret
  if ($# < 1) {
    dir = getval("directory: ",dir)
    prefix = getval("file prefix: ",prefix)
    elem = getval("element: ",elem)
    edge = getval("edge (K/L): ",edge)
  } else {
    dir = "$1"
    prefix = "$2"
    elem = "$3"
    edge = "$4"
  }

  ret = do_autochooch (dir,prefix, elem, edge)
  tty_cntl("md")
  if (ret == 0)
    printf ("\nfp = %2.5f keV, fpp = %2.5f keV\n", En[0]/1000, En[1]/1000)
  else if (ret == -2) {
    eprintf ("\nWrong fp/fpp output (fp = %2.2f keV, fpp = %2.2f keV)\n",\
	En[0]/1000, En[1]/1000)
  } else
    eprintf ("\nChooch gives no output\n")
  tty_cntl("me")
}'

#%UU% (prefix, elem, edge)
#%MDESC% Function to run the autochooch script for %B%element%B% and
#%B%edge%B% and store the result if %B%prefix%B%_* .raw, .inf and .efs files.
#Return 0 if sucess, -1 if empty output files, -2 if either fp ot fpp is zero.
def do_autochooch (dir, prefix, elem, edge) '{
global En[] Fp[] Fpp[]
local script dir1
local cmd
local fname
local line ftemp dat dtime
local t0 chooch_timeout

  script =  "/users/blissadm/local/bin/prodcdochooch.sh"

  # copy the data file from the beamline saved location
  # to the user directory and execute chooch on the user file
  if (file_info(dir) == 0)
    dir = getMxCollectPars("escan_dir")
  cmd = sprintf("cp %s %s/data.raw",AE_CHOOCH_FILE,dir)
  printf ("copying %s to %s/data.raw\n", AE_CHOOCH_FILE, dir)
  unix(cmd)
  if (AE_DEBUG == 1)
    printf ("dir %s, prefix %s, elem %s, edge %s\n", dir, prefix, elem, edge)

  dir1 = AE_ROOT_DIR
  edge = _edge(edge)
  cmd = sprintf ("%s %s %s %s %s", script, prefix, elem, edge, dir1)
  printf (" --- %s ----\n", cmd)
  unix(cmd)
  fname = sprintf("%s/data.inf", dir1)
  dtime = file_info(fname,"atime")
  t0 = time()
  chooch_timeout = 60
  while (file_info(fname,"size") == 0) {
    if (file_info(fname,"atime") != dtime) {
      break
    }
    sleep(0.5)
    if ((time() - t0) > chooch_timeout )  {
        printf("Chooch timeout after %d seconds\n", chooch_timeout)
        break
    }
  }
  if(file_info(fname,"size") > 0) {
    line = getline(fname)
    while (line != -1) {
      dat =  split(line, ftemp)
      if (ftemp[2] == "minimum") {
        En[0]= ftemp[4]
        Fp[0] = ftemp[6]
        Fpp[0] = ftemp[8]
      } else if (ftemp[2] == "maximum") {
        En[1]= ftemp[4]
        Fp[1] = ftemp[6]
        Fpp[1] = ftemp[8]
      }
      line = getline(fname)
    }
    getline(fname,"close")
    if ((En[0] == 0) || (En[1] == 0))
      return (-2)
    return(0)
  } else {
    En[0]= -1
    En[1]= -1
    return(-1)
  }
}'

#%UU% [prefix element edge]
#%MDESC% Macro to run the autochooch script. Print the fp and fpp energies
#[keV].
def print_chooch(dir,prefix, elem, edge) '{
local ret msg

  eprodc_energy_scan_status("Running chooch, please wait")
  ret = do_autochooch (dir,prefix, elem, edge)
  if (ret == 0) {
    msg = sprintf("ip = %2.5f keV (fp = %3.2f, fpp = %3.2f)", \
	En[0]/1000, Fp[0], Fpp[0])
    msg = sprintf("%s  pk = %2.5f keV (fp = %3.2f, fpp = %3.2f)", \
	msg, En[1]/1000, Fp[1], Fpp[1])
    msg = sprintf ("%s remote = %2.5f keV", msg, (AE_E+50)/1000)
  } else if (ret == -2) {
    msg = sprintf("Wrong ip/pk output (ip = %2.2f keV, pk = %2.2f keV)",\
	En[0]/1000, En[1]/1000)
  } else
    msg = sprintf("Chooch gives no output")
  eprodc_energy_scan_status(msg)
  egui_logmsg(msg)
  return (sprintf ("%2.5f %2.5f %2.5f", En[1]/1000, En[0]/1000, (AE_E+50)/1000))
}'

#%IU% (edge)
#%MDESC% parse the L edge
def _edge(edge) '{
  if (edge == "L")
    edge = "L3"
  return (edge)
}'

#%MACROS%
#%IMACROS%
#%TOC%
#%DEPENDENCIES% %B%/users/blissadm/local/bin/prodcdochooch.sh%B%
#%AUTHOR% A.Beteva
#$Revision: 1.7 $$Date: 2008/08/12 14:39:32 $