esrf

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

#%TITLE% IMAGEPRO.MAC
#
#%NAME%
# Macros for using ImagePro CCD image acquisition software
#
#%CATEGORY% Detection, Ccd
#
#%OVERVIEW%
#  This macro set can be used to trigger CCD image acquisitions by 
#  ImagePro running on a Windows PC. The macros allow a user to
#  configure the image acquisition basic setup (device, file prefix, exposure
#  time). Triggering is switched on/off using the ipon/ipoff macro
#  macros. When triggering is on image acquisition is triggered at
#  every ct command i.e. during scans. Single shot images can be taking
#  using the ipsnap command.
#
#%EXAMPLE%
#
#  %DL% 
#  %DT%ipsetup id18/imagepro/1  "c:\\temp" "img" 
#  %DD%Will setup the ImagePro macros to trigger an acquisition
#  from the device id18/imagepro/1 with file prefix "img" (exposure time
#  is set by the count time specified to "ct").
#  The file names created will be of the form img_scan_i
#  where scan is the scan number, i is the point number inside a
#  scan.
#  %DT%ipoff
#  %DD%Switch triggering off at ct command
#  %DT%ipon
#  %DD%Switch triggering on at every ct command
#  %DT%iptake "test" 10
#  %DD%Take a single shot image exposure 10 s and store it in the
#  file name "test" (NOTE: if the file exists already ImagePro will
#  open a dialog box which has to be acknowledged) 
#  %DT%ipsnap 10
#  %DD%Take a single shot image exposure 10 s and display it but
#  don't save it in a file
#  %DT%ipsave "C:\\mypath\\myfile"
#  %DD%Save the current workspace (e.g. an image taken with ipsnap)
#  in the specified file
#  %XDL%
#
# imagepro needs dbase.mac now - ONLY on ID11
#
#jtdo("id11/dbase")
#
# ipsetup
def ipsetup '{
   global IP_DEVICE, IP_PREFIX, IP_DIRECTORY, IP_scan_n, iP_NUMBER
   global IP_SUFFIX, IP_FILE_FORMAT, IP_DESCRIPTION, IP_COUNTER
   global IP_STATS, IP_AUTOSAVE, IP_NO_SCAN_N

   IP_STATS[0] = -1

   if ($# < 1) {
      print "Usage: $0 device [directory] [prefix] [file_no] [file format]"
   } else {
     IP_DEVICE = "$1"
     if ($# > 1) IP_DIRECTORY = "$2"
     if ($# > 2) 
     {
	if (IP_PREFIX != "$3") IP_NO_SCAN_N = 0
        IP_PREFIX = "$3"
     }
     if ($# >= 4) {
        SCAN_N = $4;
        IP_SCAN_N = SCAN_N;
     }
     IP_NUMBER = 0;
     IP_SUFFIX = "tif"
     IP_FILE_FORMAT = "tif"
     if ($# >= 5) {
        IP_SUFFIX = "$5";
        IP_FILE_FORMAT = IP_SUFFIX;
     }
     ipoff
     esrf_io(IP_DEVICE,"tcp")
     IP_COUNTER = -1
     ipautoon
     ipcloseon
   }
}'

# ipoff
def ipoff '{
  cdef("user_prepcount","","imagepro","delete")
  cdef("user_getcounts","","imagepro","delete")
}'

# ipon
def ipon '{
  cdef("user_prepcount","ipsnap COUNT_TIME;","imagepro",0) 
  if (IP_AUTOSAVE == 1) {
     cdef("user_getcounts","ipwait();ipautosave;","imagepro",0)
  }
  if (IP_COUNTER > -1) {
     cdef("user_getcounts",sprintf("ip_get_cnt_stats %s;", IP_CNTMNE), \
          IP_CNTMNE,0x02)
  }

}'

# ipctoff
def ipctoff '{
  cdef("user_getcounts","",IP_CNTMNE,"delete")
}'

# ipautooff
def ipautooff '{
  IP_AUTOSAVE = 0
}'

# ipautoon
def ipautoon '{
  IP_AUTOSAVE = 1
}'
# ipcounter
def ipcounter '{
   global IP_COUNTER IP_CNTMNE
   if ($# < 1) {
      print "ipcounter: count_mne [sum|mean|stddev|max|min]"
      return -1
   }
   if (cnt_num("$1") < 0) {
      print "imagepro counting switched off: $1 is not a counter"
      cdef("user_getcounts","",IP_CNTMNE,"delete")
      IP_COUNTER = -1
   }
   IP_CNTMNE = "$1"
   IP_COUNTER = 2 # default is sum
   if ($# > 1) {
      if ("$2" == "mean") IP_COUNTER = 0
      if ("$2" == "stddev") IP_COUNTER = 1
      if ("$2" == "max") IP_COUNTER = 4
      if ("$2" == "min") IP_COUNTER = 3
   }

   cdef("user_getcounts",sprintf("ip_get_cnt_stats %s;", IP_CNTMNE), \
        IP_CNTMNE,0x02) 
}' 

def ip_get_cnt_stats '{
   ipwait()
   ipstats
   S[$1]=IP_STATS[IP_COUNTER] 
}'

#ipstats
def ipstats '{
   _status = esrf_io(IP_DEVICE,"DevIpGetStats",IP_STATS)
}'

#ipscan
def ipscan '{
   if ($# < 5) {
      print "ipscan: motor start finish intervals exposure"
      return -1
   }

   _ip_start = $2
   _ip_finish = $3
   _ip_interval = $4
   _ip_exposure = $5

# check if motor ($1) is really a motor
   _check0 "$1"

   for (_ip_i=0; _ip_i < $4; _ip_i++) {
      getangles
      A[$1] = _ip_start + (_ip_finish-_ip_start)/_ip_interval*_ip_i
      move_em
      waitmove
      print "ipscan $1 position " _ip_start+(_ip_finish-_ip_start)/_ip_interval*_ip_i " taking exposure ..."
      ipsnap $5
      ipwait()
      ipautosave
   }
}'

# ipwait
def ipwait() '{

   local _ip_i

   sleep(.1)
   ESRF_ERR = -1
#   print "wait for camera ready ..."
   _ip_i = 0;
   while ((esrf_io(IP_DEVICE,"DevState") != 2) && (_ip_i < 65000)) {
      if (ESRF_ERR != 0) {
         print "error with ImagePro device server, aborting"
         return -1
      }
      sleep(.01) 
      _ip_i++
   }
#   if (esrf_io(IP_DEVICE,"DevState") != 2)
#   {
#         print "ImagePro device server still busy, try again"
#         return -1
#   }
   return 0
}'

# ipsnap
def ipsnap '{
   global IP_DESCRIPTION

   local ip_acq_timed, _ip_wait
   if( $# != 1 ) {
	printf("usage %s time\n", "$0")
	exit
   }

   _ip_wait = ipwait()

   if (_ip_wait == 0) {
#      print "OK ... trigger acquisition"
      ip_acq_timed["0"] = ""
      ip_acq_timed["1"] = "NEW"
      ip_acq_timed["2"] = 0
      ip_acq_timed["3"] = 1
      ip_acq_timed["4"] = 0

      if ($1 >= 0) ip_exposure $1

      esrf_io(IP_DEVICE,"DevIpAcqTimed",ip_acq_timed)

   }
#sleep(.25)
}'

def ipsave(filename) '{
   local my_arr mytmp
   global IP_DIRECTORY IP_PREFIX IP_SCAN_N IP_SUFFIX IP_FILE_FORMAT

   if (filename == "") {
      p "ipsave(filename) : please specify a file name !"
   }
   else {
      my_arr[0]=sprintf("%s.%s",filename,IP_SUFFIX)
      my_arr[1]=IP_FILE_FORMAT  
      printf("Image saved as %s \n", my_arr[0])
      esrf_io(IP_DEVICE,"DevIpWsSaveAs",my_arr)
#     db_update_setting("ip_filename",my_arr[0])
   }

}'

def ipautosave '{
   local my_arr mytmp  _ip_in_scan
   global IP_DIRECTORY IP_PREFIX IP_SCAN_N IP_SUFFIX IP_FILE_FORMATi IP_OLD_SCAN_N

   #
   # special case for mesh scan
   #

   _ip_in_scan = 0
   if (_stype&8) {
      if (_g2 < _n2) _ip_in_scan = 1
   }
   else {
      if(NPTS<_n1) _ip_in_scan = 1
   }

   if(_ip_in_scan) {
       # if within a scan then take the scan number (SCAN_N) into account
       if (SCAN_N != IP_OLD_SCAN_N) {
           IP_SCAN_N = 0
           IP_OLD_SCAN_N = SCAN_N
       }
       else {
           IP_SCAN_N++
       }
       my_arr[0]=sprintf("%s\\%s%03d_%04d.%s",IP_DIRECTORY,IP_PREFIX,SCAN_N,IP_SCAN_N,IP_SUFFIX)
   }
   else {
       # if outside a scan (ct typed from command line) then
       # do not take SCAN_N into account
      IP_NO_SCAN_N++
      my_arr[0]=sprintf("%s\\%s%04d.%s",IP_DIRECTORY,IP_PREFIX,IP_NO_SCAN_N,IP_SUFFIX)
   }
   my_arr[1]=IP_FILE_FORMAT
   printf("Image saved as %s\n", my_arr[0])
   esrf_io(IP_DEVICE,"DevIpWsSaveAs",my_arr)
#  db_update_setting("ip_filename",my_arr[0])

}'

# Note that ipautosave now in addition needs the fileformat
# which is defined through the set_fileformat function below. 


def fipautosave '{
   local my_arr mytmp
   global IP_DIRECTORY IP_PREFIX IP_SCAN_N IP_SUFFIX IP_FILE_FORMAT

   IP_SCAN_N++
   my_arr[0]=sprintf("%s\\%s%04d.%s",IP_DIRECTORY,IP_PREFIX,IP_SCAN_N,IP_SUFFIX)
   my_arr[1]=IP_FILE_FORMAT
  
   esrf_io(IP_DEVICE,"DevIpWsSaveAs",my_arr)

}'


def ipnewfile '{
   global IP_FILE_FORMAT IP_DIRECTORY IP_SUFFIX, IP_SUFFIX, IP_SCAN_N
   local file_par

   if ($# < 4) {
      IP_DIRECTORY = getval("directory",IP_DIRECTORY)
      IP_PREFIX = getval("prefix",IP_PREFIX)
      IP_FILE_FORMAT = getval("file format [tif|edf]",IP_FILE_FORMAT)
      IP_SUFFIX = IP_FILE_FORMAT
      IP_SUFFIX = getval("file extension",IP_SUFFIX)
      IP_SCAN_N = getval("image number ",IP_SCAN_N)
      ipnewfile_fn(IP_DIRECTORY,IP_PREFIX,IP_SUFFIX,IP_FILE_FORMAT,IP_SCAN_N)
   }
   else {

      ipnewfile_fn("$1","$2","$3","$4",$5)
   }
}'

def ipnewfile_fn(_dir,_prefix,_suffix,_format,_file_no) '{
   global IP_FILE_FORMAT IP_DIRECTORY IP_SUFFIX, IP_SUFFIX, IP_SCAN_N
   local file_par

   IP_DIRECTORY=_dir
   if (IP_PREFIX != _prefix) IP_NO_SCAN_N = 0
   IP_PREFIX=_prefix
   IP_SUFFIX=_suffix
   IP_FILE_FORMAT=_format
   IP_SCAN_N=_file_no
   file_par[0] = _dir
   file_par[1] = _prefix
   file_par[2] = _suffix
   file_par[3] = _file_no
   file_par[4] = "%04d"
   file_par[5] = "false"
   esrf_io(IP_DEVICE,"DevCcdSetFilePar",file_par)
}'

# ipseqsave
def ipseqsave'{
   for (_i=0; _i<$1; _i++) {
   	esrf_io(IP_DEVICE,"DevCcdWriteFile",_i)
        p "saving image number " _i
   }
}'

def set_fileformat '{
   global FILEFORMAT FILEEXT
   FILEFORMAT="$1"
   FILEEXT="$2"
   IP_FILE_FORMAT=FILEFORMAT
   IP_SUFFIX=FILEEXT
}'

def set_fileformat_edf '{
   set_fileformat "edf"  "edf"
}'

def set_fileformat_eda '{
   set_fileformat "Flat(EDA)"  "eda"
}'

def set_fileformat_tif '{
   set_fileformat "TIFF(TIF)" "tif"
}'



# iptake

def iptake '{

   if ("$1" == "" || $2 <= 0) {
      printf("usage: iptake file exposure\n")
   }
   else {
      ipsnap $2
      _ip_wait = ipwait()
      filename = sprintf("%s\\%s",IP_DIRECTORY,"$1")
      ipsave(filename)
   }
}'

def iptake_old '{
   local ip_acq_timed, _ip_wait

   _ip_wait = ipwait()

   if (_ip_wait == 0) {

      if ($2 > 0) ip_exposure $2

      print "OK ... trigger acquisition"
      ip_acq_timed["0"] = IP_DIRECTORY
      ip_acq_timed["1"] = "$1"
      ip_acq_timed["2"] = 0
      ip_acq_timed["3"] = 1
      ip_acq_timed["4"] = 0

      esrf_io(IP_DEVICE,"DevIpAcqTimed",ip_acq_timed)
   }
}'

# ipautotake
def ipautotake '{
   local ip_acq_timed, _ip_wait

   _ip_wait = ipwait()

   if (_ip_wait == 0) {

      if ($3 > 0) ip_exposure $3

      print "OK ... trigger acquisition"
      ip_acq_timed["0"] = $1
      ip_acq_timed["1"] = $2
      ip_acq_timed["2"] = 0
      ip_acq_timed["3"] = 1
      ip_acq_timed["4"] = 0

      esrf_io(IP_DEVICE,"DevIpAcqTimed",ip_acq_timed)
   }
}'

# ip_exposure
def ip_exposure '{
   local ip_acq_control, _exposure
 
   ip_acq_control["0"] = 49
   ip_acq_control["1"] = 1
   _exposure = $1*1000
   if (_exposure > 65535) {
      ip_acq_control["2"] = _exposure - 65536
      ip_acq_control["3"] = _exposure / 65536
   }
   else {
      ip_acq_control["2"] = _exposure
      ip_acq_control["3"] = 0
   }
   esrf_io(IP_DEVICE,"DevIpAcqControl",ip_acq_control)
}'

# ip_trigger
def ip_trigger'{
   local ip_acq_timed, _ip_wait

   _ip_wait = ipwait()

   if (_ip_wait == 0) {
      if (COUNT_TIME > 0) {
         print "set acquisition time " COUNT_TIME "..."
         ip_exposure COUNT_TIME
      }

      print "OK ... trigger acquisition"

      if (IP_SCAN_N != SCAN_N) {IP_NUMBER = 0; IP_SCAN_N = SCAN_N}
      ip_acq_timed["0"] = IP_DIRECTORY
      ip_acq_timed["1"] = sprintf("%s_%s_%s",IP_PREFIX,SCAN_N,IP_NUMBER++)
      ip_acq_timed["2"] = 0
      ip_acq_timed["3"] = 1
      ip_acq_timed["4"] = 0

      esrf_io(IP_DEVICE,"DevIpAcqTimed",ip_acq_timed)
   }
}' 

# ipcloseall
def ipcloseall'{
   ip_macro_arg = "closeall"
   esrf_io(IP_DEVICE,"DevIpMacroRun",ip_macro_arg)
}'

# ipcloseon
def ipcloseon'{
   ip_macro_arg = "closeall"
   esrf_io(IP_DEVICE,"DevIpPreSnapMacro",ip_macro_arg)
}'

# ipcloseoff
def ipcloseoff'{
   esrf_io(IP_DEVICE,"DevIpPreSnapMacro","")
}'

# ipmacro
def ipmacro'{
   ip_macro_arg = "$1"
   esrf_io(IP_DEVICE,"DevIpMacroRun",ip_macro_arg)
}'

# ippresnapmacro
def ippresnapmacro'{
   ip_macro_arg = "$1"
   esrf_io(IP_DEVICE,"DevIpPreSnapMacro",ip_macro_arg)
}'

# ippostsnapmacro
def ippostsnapmacro'{
   ip_macro_arg = "$1"
   esrf_io(IP_DEVICE,"DevIpPostSnapMacro",ip_macro_arg)
}'

# ipsetdescription
def ipsetdescription(text)'{
   ip_desc_arg["0"] = "description"
   ip_desc_arg["1"] = text
   esrf_io(IP_DEVICE,"DevIpWsChangeDescription",ip_desc_arg)
}'

# ipreset
def ipreset'{
   esrf_io(IP_DEVICE,"DevReset")
}'
#%MACROS%
#%IMACROS%
#%AUTHOR% A.Gotz,  (original 10/99, last changed 4/3/2003).
#%TOC%