esrf

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

#$Log: xfescan.mac,v $
#Revision 1.17  2012/05/11 13:35:25  beteva
#changed shstate to shstatus to cope with the new beamshutter software
#
#Revision 1.16  2009/11/04 15:42:01  beteva
#added more XFE_CONFIG parameters to be used for the ISpyB database
#
#Revision 1.15  2008/11/25 16:37:53  beteva
#changed the procedures to use transmission factor instead of attenuators set
#
#Revision 1.14  2008/06/23 11:03:14  guijarro
#filename must be specified since the beginning in eprodc_xfespectrum
#
#Revision 1.13  2008/04/09 09:32:58  spruce
#restore the current transmission at the end of the scan
#
#Revision 1.12  2008/03/11 16:17:33  spruce
#added flag LOCAL_SETUP_NEEDED to run local macros if necessary
#set the variable XFE_LOCAL_SETUP_NEEDED to 1 if necessary and generate
#the code in local_xfe_setup and local_xfe_cleanup macros
#
#Revision 1.11  2008/02/14 16:18:07  guijarro
#put time information in XFE_CONFIG
#
#Revision 1.10  2008/01/10 14:21:28  gabadinh
#-minor changes in the setup
#-added a new entry in the XFE_CONFIG to cut the spectrum before showing it to the user
#
#Revision 1.9  2007/10/16 09:05:32  gabadinh
#minor fixes
#
#Revision 1.8  2007/09/07 10:40:56  beteva
#added backstop in; fixed bug when aborting the sequence
#
#Revision 1.7  2007/09/04 12:11:35  beteva
#added xfespectrum_setup to replace xfescan_setup
#
#Revision 1.6  2007/05/02 18:15:29  beteva
#added eprodc_xfespectrum to be used by the XfeScan brick
#
#Revision 1.5  2007/02/26 13:25:31  beteva
# changed xfescan to xfespectrum (requested by the scietists)
#
#Revision 1.4  2006/12/13 13:09:00  beteva
#removed _xfescan_save, few cosmetic changes in _xfescan
#
#Revision 1.3  2006/10/23 14:31:02  beteva
#changed conditions in _estimateE
#
#Revision 1.2  2006/10/23 08:57:31  beteva
#changes to cope with new multimca.mac and SpecfitServer
#
#Revision 1.1  2004/07/06 15:47:41  beteva
#Initial revision
#
#%TITLE% XFESCAN.MAC
#%NAME% %B%xfescan.mac%B% - X-ray fluorescence emission scan
#%END%

#%UU% [emot_mne [und_move]]
#%MDESC% Set the energy mototr mnemonic %B%emet_mne%B% and evetually if the
#undulator(s) should be moved %B%und_move%B% (yes=1, no=0).
def xfespectrum_setup '{
global XFE_MOT XFE_UNDUSE XFE_CONFIG[] XFE_DEBUG
local conf[] ret
  if ($# == 0) {
    XFE_MOT[0] = getval("Energy motor mnemonic", XFE_MOT[0])
    XFE_UNDUSE = yesno("Should the undulator be moved", XFE_UNDUSE)
  } else {
    XFE_MOT[0] = "$1"
    XFE_UNDUSE = $2
  }

  if ((XFE_MOT[1] = motor_num(XFE_MOT[0])) == -1) {
    eprintf ("Motor %s not configured.\n", XFE_MOT[0])
    exit
  }
}'

#%UU% [time file]
#%MDESC% Do the emission acquisition for %B%time%B% seconds, save the data
#in %B%file%B%.
def xfespectrum '{
local tim
global XFE_FILE XFE_CONFIG ATT_OLD

  if ($# == 0) {
    COUNT_TIME = getval("Acquisition time [s]", COUNT_TIME)
    XFE_FILE = getval("File name to save the spectra (full path)", XFE_FILE)
  } else {
    COUNT_TIME = $1
    XFE_FILE = "$2"
  }
  if (file_info(XFE_FILE, "-e") == 0) {
    printf ("Creating %s\n", XFE_FILE)
    if (open(XFE_FILE) != 0) {
      eprintf ("cannot create %s file, procedure aborted.\n",XFE_FILE)
      exit
    }
  }
  get_angles
  tty_cntl("md")
  printf ("Current X-ray energy is %2.4f keV.\n", A[XFE_MOT[1]])
  tty_cntl("me")
  if (yesno("Should I continue", 0) == 0)
    exit
  _xfespectrum(COUNT_TIME, XFE_FILE)
}'

def eprodc_xfespectrum(tim,file,edge) '{
local stat inp[]
global XFE_CONFIG

  eprodc_energy_scan_status("")
  if (edge) {
    split(edge,inp)
    eprodc_energy_scan_status("Look up Edge")
    stat = _ae_getedge(inp[0],inp[1])
    if (stat == -1) {
      msg = sprintf("Cannot calculate the edge parameters, xfespectrum aborted!")
      eprodc_energy_scan_status(msg)
      return(-1)
    }

    if (XFE_UNDUSE) {
      eprodc_energy_scan_status("Moving undulators")
      stat = _ae_moveundulators()
      if (stat == -1) {
        msg = sprintf ("Cannot move undulators, scan aborted!")
        eprodc_energy_scan_status(msg)
        return(-1)
      }
    }
  }
  get_angles
  if (XFE_LOCAL_SETUP_NEEDED==1) local_xfe_setup
  msg = sprintf("Current X-ray energy is %2.4f keV.", A[XFE_MOT[1]])
  tty_cntl("md")
  printf ("%s\n",msg)
  tty_cntl("me")
  eprodc_energy_scan_status(msg)
  return(_xfespectrum(tim, file))
}'

#%IU% (tim, file)
#%MDESC% Get the energy, open the shutters, prepare the mca, search for
#appropriate attenuation, do mcaacq for %B%tim%B% seconds, close the
#shutters, save the spectra in the %B%file%B%, unset the mca, if needed.
#Find the peaks in the spectra. Suggest elements, corresponding to the
#energy of the peaks.
def _xfespectrum(tim, file) '{
shared ulong array XFE_DATA[4096][2]
shared double array XFE_CALIB[3]
local stat cmd msg current_transmission
local fn[]

  if (!tim)
    tim = 5
  if (!file) {
    split(date(),fn)
    file = sprintf("%s/%s_%s_%s_%s.scan", getMxCollectPars("escan_dir"), \
	getMxCollectPars("escan_prefix"), fn[2], fn[1], fn[4])
  }

  XFE_FILE = file

  current_transmission = ATT_FACTOR

  if (XFE_DEBUG==0) {
    # Do not waste time if the hutch not searched
    stat = shstatus("OPEN")
    if ((stat != "OPEN") && (stat != "CLOSED")) {
      msg = sprintf("Safety shutter disabled, xfespectrum aborted!")
      eprintf("%s\n", msg)
      eprodc_energy_scan_status(msg)
      return(-1)
    }

    if (_check_fe(0) == -1) {
      msg = sprintf ("Front End not open, xfespectrum aborted!")
      eprintf("%s\n", msg)
      eprodc_energy_scan_status(msg)
      return(-1)
    }
  }

  cdef ("cleanup_once",sprintf(";_xfescan_cleanup(%d,%d,%d);",\
	current_transmission, MCAON_OLD, MCA_GUI_OLD), _xfescan_)

  #beamstop in
  backin

  #get the energy for the database
  XFE_CONFIG["energy"]= A[XFE_MOT[1]]

  #get the AE_E value - for the attenuation scan
  AE_E = A[XFE_MOT[1]]*1000 - 50
  AE_RCM[AE_RCM_DISCLO] = 2200
  AE_RCM[AE_RCM_DISCHI] = A[XFE_MOT[1]]*1000 - 600
  _ae_shutters_open()
  stat = ae_attscan()
  if (stat == -1) {
    msg = sprintf("Cannot find appropriate attenuation, procedure aborted!")
    eprintf ("%s\n", msg)
    eprodc_energy_scan_status(msg)
    _xfescan_cleanup(current_transmission, MCAON_OLD, MCA_GUI_OLD)
    return(-1)
  }

  #setup the MCA
  _xfescan_mcaset(file)

  msg = sprintf("xfespectrum - mcaacq %g s",tim)
  eprodc_energy_scan_status(msg)
  msopen
  eval (sprintf ("mcaacq %s %s", tim, msg))

  XFE_DATA = MCA_DATA0
  XFE_CALIB= MCA_DATA0_PARAM
  XFE_CONFIG["file"] = _get_config_filename(A[XFE_MOT[1]])
  XFE_CONFIG["time"] = tim
  XFE_CONFIG["att"] = ATT_FACTOR
  XFE_CONFIG["bsX"] = getMxBeamSizeX()
  XFE_CONFIG["bsY"] = getMxBeamSizeY()

  #_xfescan_ps()
  _xfescan_cleanup(current_transmission, MCAON_OLD, MCA_GUI_OLD)
  return(0)
}'

#%IU% ()
#%MDESC% Set the MCA parameters. Set the acquisition on full spectra (roi 0).
def _xfescan_mcaset(file) '{
global MCA_MCAON_OLD MCA_GUI_OLD
local ch
  MCAON_OLD = MCAON
  MCA_GUI_OLD = MCA_GUI
  #MCAROI_OLD = MCA_ROIACTIVE
  _mcaon(AE_MCAUSE)
  #_mcaguion(AE_MCAUSE)
  #2 is the minimum energy possible on the beamline
  XFE_CONFIG["min"] = _mcagetch(AE_MCAUSE, 2)
  #XFE_CONFIG["max"] = _mcagetch(AE_MCAUSE, A[XFE_MOT[1]]-0.4)
  XFE_CONFIG["max"] = _mcagetch(AE_MCAUSE, A[XFE_MOT[1]]+0.2)
  XFE_CONFIG["max_user"] = _mcagetch(AE_MCAUSE, A[XFE_MOT[1]]-1.0)
  _mcaroiadd(AE_MCAROI[0],AE_MCAUSE,XFE_CONFIG["min"],XFE_CONFIG["max"])
  MCA_SAVE[AE_MCAUSE]["acq"] = 1
  MCA_SAVE[AE_MCAUSE]["std"] = 0
  MCA_SAVE[AE_MCAUSE]["onefile"] = 0
  MCA_SAVE[AE_MCAUSE]["prefix"] = file
  MCA_SUFFIX = ""
}'

#%IU% (att,mcaon,gui)
#%MDESC% Reset the attenuators and the MCA with the before the scan values.
def _xfescan_cleanup(att, mcaon, gui) '{
  _ae_shutters_close()
  fldetout
  transmission att
  if (XFE_LOCAL_SETUP_NEEDED==1) local_xfe_cleanup
  MCA_GUI = gui
  MCAON = mcaon
  MCA_ROIACTIVE = roi
  if (gui == 0)
    _mcaguiclose(AE_MCAUSE)
  if (mcaon == 0)
    _mcaoff(AE_MCAUSE)
  close(XFE_FILE)
}'

def _get_config_filename(en) '{
local fname
  fdir = "/users/blissadm/local/spec/userconf"
  if (en > 12.)
    fname = "13keV.cfg"
  else if (en > 10.)
    fname = "12keV.cfg"
  else if (en > 7)
    fname = "10keV.cfg"
  else
    fname = "7keV.cfg"
  fname = sprintf ("%s/%s", fdir,fname)
  return(fname)
}'


#%MACROS%
#%IMACROS%
#%TOC%
#%AUTHOR% A.Beteva
#%DEPENDENCIES% id.mac MAD.mac safshut.mac matt.mac multimca.mac transmission.mac  SpecfitServer%BR%
#$Revision: 1.17 $ $Date: 2012/05/11 13:35:25 $

######## obsolete ########
#%UU% [servname en_mot und_use]
#%MDESC% Set the parameters for the %B%servname%B% Specfit device server.
def xfescan_setup '{
global XFE_PSDEV  XFE_MOT XFE_UNDUSE
local conf[] ret
  if ($# == 0) {
    XFE_PSDEV = getval("Peakserch device (e.g. id29/specfit/1)", XFE_PSDEV)
    XFE_MOT[0] = getval("Energy motor mnemonic", XFE_MOT[0])
    XFE_UNDUSE = yesno("Should the undulator be moved", XFE_UNDUSE)
  } else {
    XFE_PSDEV = "$1"
    XFE_MOT[0] = "$2"
    XFE_UNDUSE = $3
  }

  if ((XFE_MOT[1] = motor_num(XFE_MOT[0])) == -1) {
    eprintf ("Motor %s not configured.\n", XFE_MOT[0])
    exit
  }

  ret = esrf_io(XFE_PSDEV,"SpecfitGetConfig",conf)
  if (ret == -1) {
    eprintf ("Specfit server not responding, exiting ... \n")
    exit
  } else {
    esrf_io(XFE_PSDEV,"tcp")
    esrf_io(XFE_PSDEV,"timeout",40)
    conf["FwhmPoints"] = "14"
    conf["Yscaling"] = "1"
    conf["McaMode"] = "0"
    conf["Sensitivity"] = "3.25"
    conf["AutoFwhm"] = "0"
    esrf_io(XFE_PSDEV,"SpecfitSetConfig",conf)
  }
}'

#%IU% ()
#%MDESC% Search for peaks in the MCA_DATA array.
def _xfescan_ps() '{
local ret
local _picks[] i
local _en[]
  ret = esrf_io(XFE_PSDEV,"SpecfitSearchArray",MCA_DATA0,_picks)
  if (ret == -1)
    return(ret)

  if (_picks[0] < 1) {
   printf ("No peaks found\n")
   return (0)
  } else {
   for (i=1; i<=_picks[0]; i++) {
     #convert the channel into energy
     _en[i] = MCA_DATA_PARAM[0] + MCA_DATA_PARAM[1] * _picks[i] +\
	MCA_DATA_PARAM[2] * pow(_picks[i],2)
     tty_cntl("md")
     printf ("Peak #%d, ch %d - %2.4f keV (%d counts)\n",i,\
	int(_picks[i]), _en[i], MCA_DATA[int(_picks[i])][1])
     tty_cntl("me")
     printf("\tPossible elements (+/- 5 percent from the peak):\n")
     printf ("\tEnergy(keV)	Element	Line	Relative_intensity\n")
     _estimateE(_en[i])
   }
   return (_picks[0])
  }
}'


#%IU% (en)
#%MDESC% Get the elements with energy close to the inserted %B%en%B% [keV]
#elements from the file %B%/users/blissadm/local/spec/userconf/PhotonEn.dat%B%
def _estimateE (en) '{
local line ftemp nb delta
local _fname

  nb = 0
  _fname = "/users/blissadm/local/spec/userconf/PhotonEn.dat"
  line = getline(_fname)
  if (line == -1)
    return (-1)
  while (line != -1) {
    split(line, ftemp,"\t")
    delta = pow((en*0.02),2) + 0.1
    if ((ftemp[0] >= en - delta) && (ftemp[0] <= en + delta))  {
      printf("\t%s",line)
      if (substr(ftemp[3],0,2) == "Kb")
        printf ("%s only present if Ka line observed as well!\n", ftemp[2])
      nb++
    }
    line=getline(_fname)
  }
  # close file
  getline(_fname, "close")
  return(nb)
}'

######## end obsolete ########