esrf

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

#%TITLE% camera.mac
#%NAME%
#  General features for images acquisition.  OBSOLETE: use ccd.mac instead
#%CATEGORY% Detection, obsolete
#%WARNING% 
#   A different file, called also camera.mac exists in local/spec/macros at MX
#   beamlines
#%DESCRIPTION%
# Controls a camera in different ways described below. More particularly, those macros allow images taking during standard scans, handle images saving to files and are able to start the EXPG Image Display program.
#%SETUP%
#%DL% SPEC config must declare:
#%DD% CCD-like device type used.
#%XDL%
#%ATTENTION%
#%DL% File names are like:
#%DT% prefix001_002_003
#%DD% when saved from scans
#%DT% %DD% 001 is the camera run number never incremented when scanning.
#%DT% %DD% 002 is the scan number during which the image was taken.
#%DT% %DD% 003 the scan point number at which the image was taken.
#%XDL%
#%DT% prefix001 
#%DD% in other cases
#%DT% %DD% 001 is the camera run number incremented at each saving.
#%XDL%
#%XDL%
#%END%


#%UU% [file_prefix] [start_exposure_macro] [stop_exposure_macro]
#%MDESC% Set up macros to work with a given camera. [start_exposure_macro] and [stop_exposure_macro] must be defined separately. The file prefix serves to build a name like described above.
def camsetup '{
  local mac0 mac1
  if ($#) {
    CAM_PREFIX = "$1"
    mac0="$2"
    mac1="$3"
  } else {
    CAM_PREFIX = getval("File Prefix",CAM_PREFIX) 
    mac0 = getval("Macro that starts the image taking",substr(CAM_M0,1,length(CAM_M0)-1))
    mac1 = getval("Macro that stops it",substr(CAM_M1,1,length(CAM_M1)-1))
  }
  if (mac0) cam_define(CAM_PREFIX,mac0,mac1)
  else cam_define(CAM_PREFIX)
}' 

#%IU% (file_prefix,start_exposure_macro,stop_exposure_macro)
#%MDESC% Set up macro and variables.
def cam_define (prefix,startexp,stopexp) '{
global CAM_RUN CAM_PREFIX
global CAM_M0 CAM_M1

CAM_PREFIX = prefix
CAM_RUN = 0
CAM_M0=sprintf("%s\n",startexp)
CAM_M1=sprintf("%s\n",stopexp)

while (unix(sprintf("test -r %s%03d", CAM_PREFIX,CAM_RUN)) == 0) {
  printf("CAMERA File %s%03d exists \n",CAM_PREFIX,CAM_RUN)
  CAM_RUN = getval("Next run number", 0)
} 
shared ushort array image_data[image_par("rows")][image_par("cols")]

camon
}'

#%UU%
#%MDESC% Sets a new file prefix and initialises the run number.
def camnewfile '
  CAM_PREFIX = getval("File Prefix",CAM_PREFIX)
  CAM_RUN = 0
  while (unix(sprintf("test -r %s%03d", CAM_PREFIX,CAM_RUN)) == 0)  {
    printf("CAMERA File %s%03d exists \n",CAM_PREFIX,CAM_RUN)
    CAM_RUN = getval("Next run number", CAM_RUN) 
  } 
'

#%IU%
#%MDESC% Reads in an image and save it to disk.
#%BR% Hooked to %B%measure1%B%.
def cam_takesave '{
  image_par("start",0)
  camera_stop
  image_get(0,image_data)
  if (image_par("lasterror")) {
    print "CAMERA TIMEOUT ; Image",sprintf("%s%03d_%03d_%03d", \
           CAM_PREFIX, CAM_RUN, SCAN_N, NPTS)," not saved !!!!!!!"
    image_data = 0
  } else {
    cam_save(sprintf("%s%03d_%03d_%03d", CAM_PREFIX, CAM_RUN, SCAN_N, NPTS),                image_data)
    CAM_RUN--
  }
}'

#%IU% (file_name,image_array)
#%MDESC% Saves an image to the specified file.
def cam_save (file,image) '{
  if (file != "0") {
    if (unix(sprintf("test -r %s", file)) != 0) {
      image_par("format","esrf")
      image_par("file",file)
      if (CAM_USEROI) {
        image_roi = image[CAM_ROI[0]:CAM_ROI[1]][CAM_ROI[2]:CAM_ROI[3]]
        image_par("save",image_roi)
        printf("ROI [%d:%d][%d:%d] saved to file %s",\
		CAM_ROI[0],CAM_ROI[1],CAM_ROI[2],CAM_ROI[3],file)
      } else {
        image_par("save",image)
        printf("Image saved to file %s",file)
      }
    } else {
      printf("CAMERA File %s already exists, cannot overwrite, exit !!",file)
      exit
    }
  } else {
    print "CAMERA Image not saved"
  }
  CAM_RUN++
}'

#%UU%
#%MDESC% Saves an image to disk. The file name is automatically processed out of the setup prefix and the run number.
def camsave '{
  cam_save(sprintf("%s%03d", CAM_PREFIX, CAM_RUN),image_data)
}'


#%UU% [exposure_time]
#%MDESC% Does an acquisition using SPEC configured timer for gating the exposure.
def camtake '{
  global CAMERA_TIME

  if ($#) {
    CAMERA_TIME = $1		
  } else {
    CAMERA_TIME = getval ("Exposure time [s]",CAMERA_TIME)
  }
  
  camera_online			
  camera_start			
  if (CAMERA_TIME>0) {
    camera_expose CAMERA_TIME	
  }
  image_par("start",0)		
  camera_stop			
  image_get(0,image_data)
  if (image_par("lasterror")) {
    printf("\n\nTIMEOUT\n"  )
  }
}'

#%IU%
#%MDESC% Checks wether the Image Display X program is running, and starts it if need it.
def camera_online '
{
  global F3_PID
  if (!F3_PID || unix(sprintf("kill -0 %d 2>/dev/null",F3_PID))) {
    F3_PID =  cam_display()
  }
}'

#%IU%
#%MDESC% Starts the Image Display X program.
def cam_display () '{
  local pid file guicmd
  file = sprintf("/tmp/disgui_%s_%s.pid",USER,SPEC)
  guicmd = sprintf("onze -ver %s -shm image_data -scale 50",SPEC)
 
  unix(sprintf("/bin/rm -f %s",file))
  unix(sprintf("%s >/dev/null 2>&1 & echo \$! > %s",guicmd,file))
  pid = getline(file)
  getline(file,"close")  
  return (pid)
}'
 

#%IU% <exposure_time>
#%MDESC% Starts the SPEC configured timer with the exposure time value.
def camera_expose '
  count_em $1
  waitcount
'


#%UU%
#%MDESC% Hook camera commands to standard scans commands.
def camon '{
  cdef("camera_start", CAM_M0, "Camera", 0)
  cdef("camera_stop" , CAM_M1, "Camera", 0)
  cdef("measure0", "camera_online\n", "camera", 0)
  cdef("measure0", "camera_start\n", "camera", 0)
  cdef("measure1", "cam_takesave\n", "camera", 0)
}'

#%UU%
#%MDESC% Get camera commands out of standard scans.
def camoff '{
  cdef("", "", "camera", "delete")
}'

#%UU% <exposure_time>
#%MDESC%  Unfold acquisitions using SPEC configured timer for gating the exposure. Exit with control-C. 
def camlive ' {
 local oldtime expo

 if (!$#) expo = getval("Exposure time (s)",CAMERA_TIME)
 for (;;) {
   oldtime = time()
   camtake $*
   printf("\rImages/sec: %5.2f",1/(time()-oldtime))
 }
}'


#%UU% <exposure_time> <no>
#%MDESC% Takes and sums the specified number of images. Saves the resulting image to disk.
def camsum '{
if ($# != 2 ) { 
  p "usage: $0 <time> <no of cycles>"
  exit
}
ulong array sum_image[image_par("rows")][image_par("cols")]
sum_image = 0
for (ii=0;ii<$2;ii++) {
  camtake $1
  sum_image += image_data
}
cam_save(sprintf("%s%03d", CAM_PREFIX, CAM_RUN),sum_image)
}'

#%UU% <flag - use roi> <xmin> <xmax> <ymin> <ymax>
#%MDESC% Defines a roi.
def camroi '{
  global CAM_ROI CAM_USEROI
  local xsize ysize
  if ($#) {
    CAM_USEROI = $1
    CAM_ROI[0] = $2
    CAM_ROI[1] = $3
    CAM_ROI[2] = $4
    CAM_ROI[3] = $5
  } else {
    CAM_USEROI = yesno ("Use a ROI for saving and display",CAM_USEROI)
    if (CAM_USEROI) {
      CAM_ROI[0] = getval("ROI: from X-pixel",CAM_ROI[0])
      CAM_ROI[1] = getval("ROI: to   X-pixel",CAM_ROI[1])
      CAM_ROI[2] = getval("ROI: from Y-pixel",CAM_ROI[2])
      CAM_ROI[3] = getval("ROI: to   Y-pixel",CAM_ROI[3])
    }
  }
  xsize = CAM_ROI[1]-CAM_ROI[0]+1
  ysize = CAM_ROI[3]-CAM_ROI[2]+1
  ushort array image_roi[xsize][ysize]
}'



#%MACROS%
#%IMACROS%
#%ATTENTION%
#%DEPENDENCIES%
#%PRE%
#  - The file %B%camera.mac%B% has to be read in 
#%PRE%
#%AUTHOR%
#  CAMERA.MAC - ICNTL - 96/03/27
#%TOC%