esrf

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

#%TITLE% ATTFACTOR.MAC
#%NAME%
# monochromatic beam attenuators
#%CATEGORY% other hardware, MX
#%LOG%
#$Log: attfactor.mac,v $
#Revision 1.2  2007/06/25 15:26:09  rey
#just documentation
#
#Revision 1.1  2005/04/05 12:26:34  beteva
#Initial revision
#
#%DESCRIPTION%
#Additional macros to calculate the attenuation factor as function of the
#attenuators or the attenuators to be set as function of the transmission.
#%END%

#%UU% [device_name]
#%MDESC% Set the calculation server %B%device_name%B%
def transmission_setup '{
global CALCSERVER_DEVICE MATT_INVERSED
  if ($#) {
    CALCSERVER_DEVICE = "$1"
    MATT_INVERSED = $2
  } else {
    CALCSERVER_DEVICE = getval("CalcServer device name:",CALCSERVER_DEVICE)
    MATT_INVERSED = yesno("Inversed attenuators cabling", 0)
  }
}'

#%UU% [trm_factor]
#%MDESC% Set the attenuators as function of the current energy [keV] and the
#transmission factor %B%trm_factor%B% [percent].
def transmission '{
local mattnb

  if ($#)
    transm = $1
  else {
    if (transm == 0) {
      print "Use command transmission [transm_factor_in_percent]"
    }
  }

  ener = A[energy]

  mattnb = calc_att(ener,transm)
  _mattset(mattnb)
}'

#%UU% [trm]
#%MDESC%Calculate which attenuators to be set as function of the current
#energy [keV] and the transmission %B%trm%B% [percent]. Print the attenuators
#to be set.
def calc_attenuation '{
local mattnb

  if ($#)
    transm = $1
  else {
    if (transm == 0) {
      print "Use command calc_attenuation [transm_factor_in_percent]"
    }
  }

  ener = A[energy]

  mattnb = calc_att(ener,transm)
  tty_cntl("md")
  printf("\nCalculated attenuation factor: %g\n", ATT_FACTOR)
  tty_cntl("me")
  printf("   #\t")
  for (i=1;i<=MATT_NUMBER; i++) {
    if (i<10)
      printf("F%d  ",i)
    else
      printf("F%d ",i)
  }
  printf("\n")
  printf ("%4d\t", mattnb)
  for (i=0;i<MATT_NUMBER;i++)
    printf ("%s ",(mattnb&(1<<i))?"IN ":"OUT")
}'

#%UU% [matt_state]
#%MDESC%Calculate the transmission factor as function of the current energy
#[keV] and the attenuators %B%matt_state%B% set.
def calc_transmission '{
local ener matt_state
global ATT_FACTOR
local max_att

  max_att = (1<<MATT_NUMBER) - 1
  if ($#) {
    matt_state = $1
    if (matt_state > max_att)
      matt_state = max_att
  } else {
    matt_state = _matt_get()
    MATT_STATE = matt_state
  }

  ATT_FACTOR = calc_transm(A[energy],matt_state)
  
}'

#%IU% [filterno]
#%MDESC% Set the %B%filterno%B% filter in and calculate the new transmission
#factor. 
def attio '{
local filterno val

  if (MATT_INVERSED)
    filterno = pow(2, MATT_NUMBER-$1)
  else
    filterno = 1<<($1 -1)
  val = _matt_get()

  if ((val & filterno) == 0)
    val += filterno
  else
    val &= ~filterno

  _mattset(val)
  if (MATT_STATE != (val))
    eprintf ("Attenuators not set\n")
  ATT_FACTOR = calc_transm(A[energy], MATT_STATE)
}'

#%IU% (nrj, trm)
#%MDESC% Calculate which attenuators to be set as function of the
#energy %B%nrj%B% [keV] and the transmission %B%trm%B% [percent]. Return the
#number to be used by the mattset macro.
def calc_att(nrj, trm) '{
local cmd ret res[] nb
global ATT_FACTOR

  ATT_FACTOR = -1 
 
  cmd = sprintf("AttMonoSetup.getAttenuation(%f,%f)",nrj,trm)
  nb = esrf_io(CALCSERVER_DEVICE,"DevRemoteDoubleArr",cmd,res)
  if (nb != -1) {
    ATT_FACTOR = res[0]
    ret = 0
    for (i=1; i<nb; i++) {
      if (res[i] != 0)
        ret += 1<<res[i]
    }
  }
  return (ret)
}'

#%IU% (nrj,matt_state)
#%MDESC% Calculate the transmission as function of the energy %B%nrj%B%
#[keV] and the attenuators %B%matt_state%B% set. Return the attenuation factor
#if susess, -1 if error.
def calc_transm(nrj,matt_state) '{
local att_factor
local fnum cmd i

  att_factor = -1

  if (matt_state != 0) {
    for (i=0;i<MATT_NUMBER;i++)
      (matt_state&(1<<i))?(fnum = sprintf("%s %d",fnum,i)):0
  } else
    fnum = 0

  cmd = sprintf("AttMonoSetup.getAttenuationFactor(%g,\'%s\')",nrj,fnum)
  att_factor = esrf_io(CALCSERVER_DEVICE,"DevRemoteDouble",cmd)
  return(att_factor)
}'

#%MACROS%
#%IMACROS%
#%TOC%
#%DEPENDENCIES% matt.mac, CalcServer.py, AttMonoSetup.py, percent.arr
#%AUTHOR% A.Beteva, BLISS%BR%
#$Revision: 1.2 $,$Date: 2007/06/25 15:26:09 $