esrf

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

#%TITLE% MXOSCSCAN.MAC
#%NAME% %B%mxoscscan.mac%B% - Do different type of "spec" scans with an
#oscillation for each point
#%DESCRIPTION%
#%CATEGORY% MX
#%END%

global MX_OSCSCAN

cdef("user_prepcount", "if (MX_OSCSCAN[\"on\"]) {_mxdetstart};", "_mxoscscan")
cdef("user_getcounts", "if (MX_OSCSCAN[\"on\"]) {_mxdetread};", "_mxoscscan",0x10)

#%UU% ()
#%MDESC% Set the scan motors default names. If differen, use
#mx_oscscan_localmot macro hook. Return 0 if OK, -1 if any of the motors is not
#configured in SPEC.
def mx_oscscan_setup() '{
global MX_OSCSCAN_MNE[] MX_OSCSCAN_MOT[] MX_OSCSCAN_OLD[]
local mmne

  MX_OSCSCAN_MNE["phi"] = "phi"
  MX_OSCSCAN_MNE["phix"] = "phix"
  MX_OSCSCAN_MNE["phiy"] = "phiy"
  MX_OSCSCAN_MNE["phiz"] = "phiz"

  mx_oscscan_localmot

  for (mmne in MX_OSCSCAN_MNE) {
    MX_OSCSCAN_MOT[mmne]=motor_num(MX_OSCSCAN_MNE[mmne])
    if (MX_OSCSCAN_MOT[mmne] == -1) {
      eprintf ("wrong motor mnemonic %s, check your mx_oscscan_localmot macro\n", MX_OSCSCAN_MNE[mmne])
      MX_OSCSCAN["setup"] = -1
      return(-1)
    }
  }

  if (MXBCM_PARS["detector"]["type"] == "adsc") {
    if (motor_par(MX_OSCSCAN_MOT["phi"],"device_id") == "udiff_mot")
      cdef ("mx_oscscan_localprep", "id29_adsc_prepare();", "_mxoscscan")
  } else if (MXBCM_PARS["detector"]["type"] == "marccd") {
    cdef ("mx_oscscan_localprep", "id232_mccd_prepare();", "_mxoscscan")
  }

  MX_OSCSCAN["setup"] = 1
  return(0)
}'

#%IU% ()
#%MDESC% Save current motor positions. Return -1 if no motors set 0 if OK.
def mx_oscscan_save() '{
local mmne

  if ( MX_OSCSCAN["setup"] != 1)
    return(-1)
  waitmove; get_angles
  for (mmne in MX_OSCSCAN_MOT) {
    MX_OSCSCAN_OLD[mmne] = A[MX_OSCSCAN_MOT[mmne]]
  }
  return(0)
}'

#%IU% ()
#%MDESC% Restore previously saved motor positions and move the motors to them.
#Return -1 if no motors set 0 if OK.
def mx_oscscan_restore() '{
local mmne

  if (MX_OSCSCAN["setup"] != 1)
    return(-1)
  for (mmne in MX_OSCSCAN_MOT) {
    A[MX_OSCSCAN_MOT[mmne]] = MX_OSCSCAN_OLD[mmne]
  }
  move_em; waitmove; get_angles
  return(0)
}'

#%IU% ()
#%MDESC% What is to be done before each scan - save the motor positions
#open the safety shutter, close the fast shutter, prepare the detector,
#set the acquisition flag on.
def mx_oscscan_prepare() '{

   if (mx_oscscan_save() == -1)
     exit
   msclose; shopen
   mx_oscscan_localprep
   local_deton
}'

#%IU% ()
#%MDESC% What is to be done after each scan - set the acquisition flag off,
#close the fast shutter, close the safety shutter, move the motorts to
#their before the scan position.
def mx_oscscan_finish() '{

  local_detoff
  msclose; shclose
  mx_oscscan_restore()
}'

#%IU%
#%MDESC% Set the acquisition flag on.
def local_deton '{
global  MX_OSCSCAN
  MX_OSCSCAN["on"] = 1
}'

#%IU%
#%MDESC% Set the acquisition flag off.
def local_detoff '{
  MX_OSCSCAN["on"] = 0
  if (MXBCM_PARS["detector"]["type"] == "marccd")
    MCCD_AUTOSAV = 0
}'

#%IU%
#%MDESC% Actions taken if ^C pressed.
def _mx_oscscan_cleanup '{
  mx_oscscan_finish()
}'

#%IU%
#%MDESC% Do a2scan with scan parameters coming from the GUI. Set the dark
#current flag if needed. Prepare the acquisition. Do required actions after
#the scan.
def mx_oscscan_a2scan '{
local nb lpar[]

  if (MXBCM_PARS["detector"]["type"] == "adsc") {
    nb = split("$*", lpar)
    if (fabs(lpar[nb-1] - _ctime) < 0.001)
      MX_OSCSCAN["dark"] = 0
    else {
      _ctime = lpar[nb-1]
      MX_OSCSCAN["dark"] = 1
    }
  }

  cdef("cleanup_once", "_mx_oscscan_cleanup\n", "_mx_oscscan")
  mx_oscscan_prepare()
  a2scan $*
  mx_oscscan_finish()
}'

#%IU%
#%MDESC% Do mesh with scan parameters coming from the GUI. Set the dark
#current flag if needed. Prepare the acquisition. Do required actions after
#the scan.
def mx_oscscan_mesh '{
local lpar[] nb

  if (MXBCM_PARS["detector"]["type"] == "adsc") {
    nb = split("$*", lpar)
    if (fabs(lpar[nb-1] - _ctime) < 0.001)
      MX_OSCSCAN["dark"] = 0
    else {
      _ctime = lpar[nb-1]
      MX_OSCSCAN["dark"] = 1
    }
  }
  cdef("cleanup_once", "_mx_oscscan_cleanup\n", "_mx_oscscan")
  mx_oscscan_prepare()
  mesh $*
  mx_oscscan_finish()
}'

#%IU%
#%MDESC% Do ltscan with scan parameters coming from the GUI. Set the dark
#current flag if needed. Prepare the acquisition. Do required actions after
#the scan.
def mx_oscscan_ltscan '{

  if (MXBCM_PARS["detector"]["type"] == "adsc") {
    nb = split("$*", lpar)
    if (fabs(lpar[nb-1] - _ctime) < 0.001)
      MX_OSCSCAN["dark"] = 0
    else {
      _ctime = lpar[nb-1]
      MX_OSCSCAN["dark"] = 1
    }
  }

  cdef("cleanup_once", "_mx_oscscan_cleanup\n", "_mx_oscscan")
  mx_oscscan_prepare()
  ltscan $*
  mx_oscscan_finish()
}'

#%IU%
#%MDESC% For each scan point: set the file and the collection parameters, start
#the detector acquisition; do an oscillation for 1 degree, 1 pass, starting
#from the phi current position and the scan counting time.
def _mxdetstart '{

  if (mx_oscscan_collectpars() == -1)
    exit
  ccdstart
  osc_scan MX_OSCSCAN_OLD["phi"] MX_OSCSCAN_OLD["phi"]+1 _ctime 1 
}'

#%IU%
#%MDESC% For each scan point: read the detector, save the data. Flush the
#detector at the end of the scan.
def _mxdetread '{
  ccdread
  if (_stype&8 <= MX_OSCSCAN["inum"])
    ccdsave
  else
    ccdflush
  MX_OSCSCAN["inum"] += 1
}'

#%IU% ()
#%MDESC% Generate acquisition file with the parameters from the GUI and
#oscilation parametsrs 1 degree, 1 pass, starting from the phi current
#position. 
def mx_oscscan_collectpars() '{
local lfname lstr
global MX_OSCSCAN

  #return(0)
  COLLECT_SEQ["fileinfo"]["directory"] = MX_OSCSCAN["dir"]
  if (length(COLLECT_SEQ["fileinfo"]["directory"]) == 0) {
    tty_cntl("md")
    eprintf ("No data directory specified, scan aborted\n")
    tty_cntl("me")
    return(-1)
  } else {
    split(COLLECT_SEQ["fileinfo"]["directory"], lstr, "/")
    if (lstr[1] != "data") {
      tty_cntl("md")
      eprintf ("Directory chosen is not \/data , scan aborted\n")
      tty_cntl("me")
      return(-1)
    }
  }
  COLLECT_SEQ["fileinfo"]["prefix"] = MX_OSCSCAN["prefix"]
  COLLECT_SEQ["fileinfo"]["run_number"] = MX_OSCSCAN["inum"]
  COLLECT_SEQ["start"] = MX_OSCSCAN_OLD["phi"]
  COLLECT_SEQ["range"] = 1
  COLLECT_SEQ["exposure_time"] = _ctime
  COLLECT_SEQ["number_of_passes"] = 1
  COLLECT_SEQ["start_image_number"] = 0
  lfname = ccdfile(COLLECT_SEQ,1)
  #lfname = tstccdfile(COLLECT_SEQ,1)
  jpeg_files()
  setMxCurrentFilename(lfname)
  p COLLECT_SEQ
  
}'

cdef ("mx_oscscan_localmot","","_mxoscscan")

#%IU% ()
#%MDESC% Actions to be executed with the microdiff and the ADSC Q315 detector
#to prepare the acquisition.
def id29_adsc_prepare() '{
local msg

  if (mx_oscscan_collectpars() == -1)
    exit

  # force darks if needed
  setMxCollectPars("TakeDarkFlag",MX_OSCSCAN["dark"])
  COLLECT_SEQ["dark"] = MX_OSCSCAN["dark"]

  #this is to set the dark current time
  if (microdiff_preparecollect() == -1)
    exit

  #force the mode to hwbinned
  setMxSpecPars([0:"detector", 1:"binning"], 2)
  msg = sprintf ("---- procedure started on %s ----", date())
  printf ("%s\n", msg)
  ccdprep(getMxCollectPars("dark"),MX_OSCSCAN_OLD["phi"],1,_ctime,1)
  
}'

#%MDESC% Actions to be executed with the minidiff and the MAR555 detector
#to prepare the acquisition.
def id232_mccd_prepare() '{
  lightout
  camout
  backin
  MCCD_AUTOSAV=1
}'


def tstccdfile(cpars,frame) '{
local  adsc_imagefile adsc_image_number ndigits formatstring

  adsc_image_number = cpars["start_image_number"] + frame      
  ndigits = cpars["start_image_number"] + cpars["number_of_images"] > 1000?4:3
  formatstring = sprintf("%s%d%s","%s/%s_%d_%0",ndigits,"d.%s")
  adsc_imagefile = sprintf(formatstring,cpars["fileinfo"]["directory"],\
                           cpars["fileinfo"]["prefix"],\
                           cpars["fileinfo"]["run_number"],\
                           adsc_image_number, getMxDetectorFileSuffix())
  printf ("*filename*** %s\n", adsc_imagefile)
  return(adsc_imagefile)
}'

def jpeg_files() '{
global adsc_jpeg1 adsc_jpeg2

  adsc_jpeg1 = sprintf(formatstring,COLLECT_SEQ["fileinfo"]["directory"] ,\
                       COLLECT_SEQ["fileinfo"]["prefix"],\
                       COLLECT_SEQ["fileinfo"]["run_number"],\
                       adsc_image_number,"jpeg")
  adsc_jpeg2 =  sprintf(formatstring,COLLECT_SEQ["fileinfo"]["directory"],\
                 	COLLECT_SEQ["fileinfo"]["prefix"],\
                        COLLECT_SEQ["fileinfo"]["run_number"],\
                        adsc_image_number,"thumb.jpeg")

  print "*jpeg files*** " adsc_jpeg1 " " adsc_jpeg2

}'

#%MACROS%
#%IMACROS%
#%DEPENDENCIES%
#%TOC%
#%AUTHOR% BLISS%BR%
#$Revision: 1.1 $ $Date: 2010/07/01 12:48:47 $%BR%
#%END%
#%LOG%
#$Log: mxoscscan.mac,v $
#Revision 1.1  2010/07/01 12:48:47  beteva
#Initial revision
#