esrf

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

#$Log: mxadsc.mac,v $
#Revision 1.25  2012/06/26 17:28:07  guijarro
#fixed frame for image filename
#
#Revision 1.24  2012/06/22 11:33:12  guijarro
#fix for darks
#
#Revision 1.23  2011/04/18 12:31:09  guijarro
#changed loop for polling detector state ;
#changed to use only 4 digits
#
#Revision 1.21  2008/02/20 08:24:31  guijarro
#use detector to generate jpegs
#
#Revision 1.20  2007/03/06 08:36:53  guijarro
#refactoring + lastimage=1 for darks + use adsc_taco_command as often as possible
#
#Revision 1.19  2007/02/13 15:30:15  spruce
#when doing an adsc_prep send a DevState command to the server
#in the hope that it will reconnect if the connection was lost
#due to the server being restarted
#
#Revision 1.18  2006/10/03 12:48:50  spruce
#a little clean up. This has been running for 4 days on ID14-4 including pipeline demos and users. No detector problems reported.
#
#Revision 1.17  2006/09/28 15:35:00  spruce
#detector errors treated
#waitmove before ccdstart to make sure nothing is moving
#set filename to notset just in case an erreonous image is written after an abort
#
#Revision 1.14  2006/09/12 12:46:51  guijarro
#added cleanup macros for macros with actions on the detector
#
#Revision 1.13  2006/08/29 14:07:14  spruce
#fix bug if mxCuBE does not set the binning mode it does not default to 0
#(due to mistake with whatis logic)
#
#Revision 1.12  2006/08/24 12:25:56  spruce
#adsc_prep now uses the collect_seq detector binning mode to determine what mxCuBE has decided. It should use the default is it does not exist.
#
#Revision 1.10  2006/06/14 08:55:11  blissadm
#correction for 4 digits- take start image number into account
#
#Revision 1.9  2006/06/13 09:42:24  blissadm
#still errors, fixed and tested this time on ID14-2
#
#Revision 1.8  2006/06/13 09:34:08  blissadm
#model is in mxbbcm_pars, not mx_spec_pars
#
#Revision 1.7  2006/06/13 09:29:40  blissadm
#detector type should be model in ccdprep. This must also correspond in mxlocal
#
#Revision 1.6  2006/06/06 13:27:50  spruce
#add suffix to adsc_filename (necessary for marccd)
#
#Revision 1.5  2006/05/15 12:41:01  spruce
#removed CURRENT_IMAGE in adsc_write- its already updated in mxcollect
#
#Revision 1.4  2006/05/11 11:56:38  spruce
#dark=1 was still forcing darks to always come on
#
#Revision 1.2  2005/11/08 17:22:17  beteva
#corrected the dark current calculations and unbinned mode parameters for Q210 & Q315 detectors
#
#Revision 1.1  2005/11/08 17:19:11  beteva
#Initial revision
#
#%TITLE% mxadsc.mac
#%NAME% %B%mxadsc.mac%B% - control ADSC detectors macro set
#for MX datacollection
#%END%

def adsc_on '{
  cdef("ccdstart","adsc_start","_pxccd",0x20)
  cdef("ccdread","adsc_read","_pxccd",0x20)
  cdef("ccdsave","adsc_write","_pxccd",0x20)
  cdef("ccdflush","adsc_flush","_pxccd",0x20)  
  cdef("ccdfile","adsc_filename","_pxccd",0x20)  
  cdef("ccdprep","adsc_prep","_pxccd",0x20)  
  cdef("ccdcleanup","adsc_cleanup","_pxccd",0x20)  
  cdef("taco_command","esrf_io","_tacolink",0x20)  
}'

def adsc_off '{
  cdef("","","_pxccd","delete")
}'

def adsc_cleanup '{  
  global MXSPEC_PARS
  adsc_taco_command("DevReset")
}'

def adsc_getfilepar(parstring) '{
  local pars[]
  local pctr
  local s
 
  split(parstring,pars,",")
  pctr=0
  for (par in pars) {
    if (pctr==0)
      s=sprintf("[%s:\"%s\"", pctr, pars[par])
    else
      s=sprintf("%s, %s:\"%s\"", s, pctr, pars[par])
    pctr++
  }
  s=sprintf("%s]", s)

  if (ADSC_DEBUG)
    adsc_taco_command("DevCCDGetFilePar", s, 1) 
  else
    adsc_taco_command("DevCCDGetFilePar", s)
}'

def adsc_setfilepar(parstring) '{
  global MXSPEC_PARS
  local aa[]
  local pars[]
  split(parstring,pars,",")
  for (par in pars) {
    aa[0] = ""
    aa[1] = ""
    split(pars[par],aa,"=")
    # aa should contain 2 arguments
    adsc_taco_command("DevCCDSetFilePar",sprintf("[0:\"%s\", 1:\"%s\"]", aa[0], aa[1]))
  }	

  if (ADSC_DEBUG) {
    adsc_taco_command("DevCCDGetFilePar","[0:\"all\"]",1)
  }
}'  

def adsc_hwpar(parstring) '{
  global MXSPEC_PARS
  local pars[]
  local aa[]
  pars[0] = ""
  split(parstring,pars,",")
  for (par in pars) {
    aa[0] = ""
    aa[1] = ""
    split(pars[par],aa,"=")
    # aa should contain 2 arguments
    adsc_taco_command("DevCCDSetHwPar",sprintf("[0:\"%s\", 1:\"%s\"]", aa[0], aa[1]))
  }
	
  if (ADSC_DEBUG) {
    adsc_taco_command("DevCCDGetHwPar","[0:\"all\"]",1)
  }
}'  


def adsc_taco_command(cmd,inp,outp) '{
  local ESRF_ERR ESRF_ERR_MSG
  local aa
  local bb[]
  ESRF_ERR = -1 
  ESRF_ERR_MSG=""

  if ((inp == 0)&&(outp == 0)) {
    retval = taco_command(MXSPEC_PARS["detector"]["TacoDeviceName"],cmd)
  } else if (outp==0) {
    aa  = eval(inp)
    retval = taco_command(MXSPEC_PARS["detector"]["TacoDeviceName"],cmd,aa)
  } else {
    aa = eval(inp)
    retval = taco_command(MXSPEC_PARS["detector"]["TacoDeviceName"], cmd, aa, bb)
  }

  if (ESRF_ERR > 0) {
    egui_fatal(sprintf("detector device server error : %s",ESRF_ERR_MSG))
    exit
  }

  if (( retval == -1 )||(retval=="")) {
    egui_fatal("Error communicating with detector software")
    exit
  }

  if (outp == 0) {
    return retval
  } else {
    p bb
    return retval 
  }
}'

def adsc_start '{
  global MXSPEC_PARS

  cdef("cleanup_once","adsc_cleanup;","_adsc_start_cleanup") 

  if (MXSPEC_PARS["detector"]["TakeDark"]==1) {
    adsc_setfilepar(sprintf("kind=0,lastimage=1,filename=%s/%s_%d",COLLECT_SEQ["fileinfo"]["directory"],COLLECT_SEQ["fileinfo"]["prefix"],COLLECT_SEQ["fileinfo"]["run_number"]))

    darktime = MXSPEC_PARS["detector"]["DarkTime"]
    egui_logmsg(sprintf("Taking 1st dark image of %s seconds",darktime))
    # there should be nothing moving here this check only takes 0.5msec
    waitmove
    adsc_taco_command("DevCCDStartExposure")
    detwait(1)
    sleep(darktime)
    adsc_stop
    detwait(0)
    adsc_taco_command("DevCCDWriteImage")
    detwait(0)

    # Second dark
    adsc_setfilepar(sprintf("kind=1,lastimage=1,filename=%s/%s_%d",COLLECT_SEQ["fileinfo"]["directory"],COLLECT_SEQ["fileinfo"]["prefix"],COLLECT_SEQ["fileinfo"]["run_number"]))
    egui_logmsg(sprintf("Taking 2nd dark image of %s seconds",darktime))
    adsc_taco_command("DevCCDStartExposure")
    detwait(1)
    sleep(darktime)
    adsc_stop
    detwait(0)
    adsc_taco_command("DevCCDWriteImage")
    detwait(0)

    MXSPEC_PARS["detector"]["TakeDark"]=0
  }
  # You have to specify the file name before you start
  adsc_setfilepar(sprintf("axis=1,kind=5,lastimage=0,filename=%s,phi=%f,jpeg_name1=%s,jpeg_size1=1024x1024,jpeg_name2=%s,jpeg_size2=250x250",adsc_imagefile,getMxCollectPars("current_phi"),adsc_jpeg1,adsc_jpeg2))

  adsc_taco_command("DevCCDStartExposure")
  detwait(1)
}'

def adsc_stop '{
  global MXSPEC_PARS
  adsc_taco_command("DevCCDStopExposure")
}'

def adsc_read '{
  adsc_stop
}'

def adsc_flush '{
  adsc_write 1
}'

def adsc_write '{
  global MXSPEC_PARS MXCOLLECT_PARS
  global adsc_imagefile
  
  cdef("cleanup_once","adsc_cleanup;","_adsc_write_cleanup")

  adsc_setfilepar(sprintf("lastimage=%d",$1))
  detwait(0)
  adsc_taco_command("DevCCDWriteImage")
  MXCOLLECT_PARS["TakeDarkFlag"]=0
}'

def adsc_filename(cpars,frame) '{
  global adsc_imagefile adsc_image_number adsc_jpeg1 adsc_jpeg2
  adsc_image_number = frame       
  formatstring = sprintf("%s%d%s","%s/%s_%d_%0",4,"d.%s")
  adsc_imagefile =  sprintf(formatstring,cpars["fileinfo"]["directory"],\
                                            cpars["fileinfo"]["prefix"],\
                                            cpars["fileinfo"]["run_number"],\
                                            adsc_image_number,getMxDetectorFileSuffix())
  adsc_jpeg1 = sprintf(formatstring,cpars["archive_dir"],\
                                            cpars["fileinfo"]["prefix"],\
                                            cpars["fileinfo"]["run_number"],\
                                            adsc_image_number,"jpeg")
  adsc_jpeg2 =  sprintf(formatstring,cpars["archive_dir"],\
                                            cpars["fileinfo"]["prefix"],\
                                            cpars["fileinfo"]["run_number"],\
                                            adsc_image_number,"thumb.jpeg")
  print "*filename*** " adsc_imagefile
  unix(sprintf("mkdir -p \`dirname %s\`", adsc_jpeg1))
  print "*jpeg files*** " adsc_jpeg1 " " adsc_jpeg2
  return(adsc_imagefile)
}'


def detwait(untilstate) '{
  global MXSPEC_PARS ABORT
  local status
  status = adsc_taco_command("DevStatus")
  local t0

  t0=time() 
  while (adsc_taco_command("DevState") != untilstate ) {
    sleep(0.1)
    printf("Waiting for detector to be ready; elapsed time=%f ms.\n", 1000*(time()-t0))
    if (time()-t0 > 20) {
      egui_fatal("Detector software is not responding. Try to restart it")
      exit
    }
    cstate = adsc_taco_command("DevState")
    if (cstate==3 || cstate == -1) {
      status = adsc_taco_command("DevStatus")
      egui_fatal(sprintf("%s. Try to restart detector software", status))
      exit
    }
 }
}'


# Prepare detector for data collection in the right hw modes and with the right header info
def adsc_prep(dark,start_angle,range,exp_time,number_of_passes,comment) '{
  global MXSPEC_PARS
  local darktime adc bin
  
  # send a command to trigger a reconnection if necessary
  taco_command(MXSPEC_PARS["detector"]["TacoDeviceName"],"DevState")
 
  print ">>>>>>>>>>>>>>" dark 
  MXSPEC_PARS["detector"]["TakeDark"]=dark
  #dark = MXCOLLECT_PARS["TakeDarkFlag"]

  darktime = MXSPEC_PARS["detector"]["DarkTime"]

  taco_command(MXSPEC_PARS["detector"]["TacoDeviceName"],"timeout",15)
  taco_command(MXSPEC_PARS["detector"]["TacoDeviceName"],"tcp")

  if (whatis("COLLECT_SEQ[\"detector_mode\"]")>>16&0x8000) {
    print "Binning set by data collection parameters"
  } else {
    print "No binning set by data collection, use default"
    COLLECT_SEQ["detector_mode"] = MXSPEC_PARS["detector"]["binning"]
  }
  if ( COLLECT_SEQ["detector_mode"]==0) {
    print " SOFTWARE BINNED"
    adc=0;bin=2
  } else if (COLLECT_SEQ["detector_mode"]==1) {
    print " Q315/210 UNBINNED"
    adc=0;bin=1
    # special case for q4 detectors
    if ( index(MXBCM_PARS["detector"]["model"],"q4") == 1) adc=1
  } else if (COLLECT_SEQ["detector_mode"]==2) {
    print " HARDWARE BINNED"
    adc=1;bin=2
  }
  if (MXSPEC_PARS["detector"]["UseDefaultDarkTime"] == 0) {
    darktime = exp_time + MXSPEC_PARS["detector"]["overheadtime"] + MXSPEC_PARS["detector"]["overpasstime"] * number_of_passes
    MXSPEC_PARS["detector"]["DarkTime"] = darktime
    printf("Using dark time = %f s.\n", darktime)
  }
  # switch on dark images if the darktime was changed
  #if (MXSPEC_PARS["detector"]["DarkTime"] != darktime || dark || COLLECT_SEQ["number_of_images"] > 4)
  #  MXSPEC_PARS["detector"]["TakeDark"]=1
  
  adsc_hwpar(sprintf("adc=%d,bin=%d",adc,bin))
  adsc_hwpar(sprintf("save_raw=%d,no_xform=%d",MXSPEC_PARS["detector"]["save_raw"],MXSPEC_PARS["detector"]["no_xform"]))

  distance=getMxCurrentDistance()
  wavelength=getMxWavelength()

  # set the filepars in order to write the correct header to the image files
  adsc_filepar_s=sprintf("filename=%s/notset,phi=%f,distance=%f,wavelength=%f,osc_range=%f,time=%f,beam_x=%f,beam_y=%f,comment=%s",\
  	COLLECT_SEQ["fileinfo"]["directory"],start_angle,	distance, wavelength,	range,	exp_time,	getMxBeamCentreX(), getMxBeamCentreY(), 	comment)
  adsc_setfilepar(adsc_filepar_s)
}'

#%MACROS%
#%IMACROS%
#%TOC%
#%AUTHOR% BLISS
#$Revision: 1.25 $$Date: 2012/06/26 17:28:07 $