esrf

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

#$Log: transmission.mac,v $
#Revision 1.1  2006/04/11 07:04:27  guijarro
#Initial revision
#
#Revision 1.1  2005/03/22 10:09:14  blissadm
#Initial revision
#
#%TITLE% ATTFACTOR.MAC
#%NAME%
# %B%attfactor.mac%B% - monochromatic beam attenuators
#%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%

global ATT_FACTOR MATT_INVERSED CALCSERVER_DEVICE

#%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 ener

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

  ener = getMxEnergy()

  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 ener

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

  ener = getMxEnergy()

  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
  }

  ener = getMxEnergy()

  ATT_FACTOR = calc_transm(ener,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")

  ener = getMxEnergy()

  ATT_FACTOR = calc_transm(ener, 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]-1)
    }
  }
  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+1)):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.1 $,$Date: 2006/04/11 07:04:27 $