esrf

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

#%TITLE% METEOR.MAC
#%NAME% 
# Macros to control non-standard cameras via the Matrox 
# Meteor2 frame grabber. 
#
#%CATEGORY% Detection, Ccd
#
#%DESCRIPTION%
#The meteor2 server supports the following cameras: %BR%
#%UL%
#%LI% 8500A54
#%LI% HR50
#%LI% XC55
#%LI% 
#%XUL%
#
#%LOG%
#$Revision: 2.9 $
#$Log: meteor.mac,v $
#Revision 2.9  2008/08/12 13:36:18  rey
#documentation changes
#
#Revision 2.8  2005/09/21 12:40:13  pepellin
#Use clscreen()
#
#Revision 2.7  2005/02/01 15:18:16  claustre
#fixed a bug in meteor_setthreshold().
#new macro meteor_pixelsize to set the pixel size using the DevStore/DevRestore commands of the meteor taco server.
#
#Revision 2.5  2004/11/29 15:58:39  claustre
#Hoops still a bug in meteor_counter_add macros
#
#Revision 2.4  2004/11/29 15:52:51  claustre
#corrected meteor_counter_add macro.
#
#Revision 2.3  2004/11/29 15:35:23  claustre
#meteorsetup takes now 4 paramaters instead of 5 !!
#
#Revision 2.2  2004/11/26 15:46:27  claustre
#meteor_ct_read definition, cdef, changed. now goes at the end
#after ccdread
#
#Revision 2.1  2004/06/25 15:37:18  claustre
#yet few bugs corrected.
#
#Revision 2.0  2004/06/25 12:01:46  claustre
#The definitive version 2.0 !
#
#Revision 1.3  2004/06/24 10:46:02  claustre
#This version is not compatible with the previous one, it can
#manage definition of multiple counters with multiple cameras.
#
#Revision 1.2  2003/07/01 08:17:24  claustre
#cleaned code up
#
#Revision 1.1  2002/12/03 07:39:36  claustre
#Initial revision
#
#
#
#%DESCRIPTION%
#   The Meteor2 device server can be accessed via SPEC as a standart CCD 
#   camera. All standart functionality is implemented.  
#   Several extra features are available which are accessible via this 
#   set of macros as well as ccdmenu.
#   The main features are:
#%UL%
#%LI% Counters on calculated beam parameters (spot center, size, ...)
#%LI% Manual and automatic image intensity calibration
#%LI% Live mode for monitoring
#%LI% Intensity threshold on image
#%XUL%
#
#%EXAMPLE%
#%DL%
#%DT%  meteorsetup device_name min_intensity max_intensity threshold %DD%
#    It will set default calibration parameters for the active meteor2 CCD camera.
#    You can add this setup into the SPEC setup.
# 
#%DT%  meteorshow %DD%
#    Shows all readable parameters for the active meteor2 camera with their index
#    and actual values. The actual meteor2 camera set-up is:
#%DL%
#%DD%	[index] : description : value
#%DD%	-----------------------------------------
#%DD%	[0]  : Exposure time             : 0.000000
#%DD%	[1]  : Threshold                 : 0
#%DD%	[9]  : Live mode                 : 0
#%DD%	[30] : Auto calibration mode     : 0
#%DD%	[2]  : Calibrated max. Intensity : 0
#%DD%	
#%DD%	[3]  : Full image size X         : 0
#%DD%	[4]  : Full image size Y         : 0
#%DD%	
#%DD%	[5]  : ROI start X               : 0
#%DD%	[6]  : ROI start Y               : 0
#%DD%	[7]  : ROI end X                 : 0
#%DD%	[8]  : ROI end Y                 : 0
#%DD%	
#%DD%	[10] : Number of beam spots      : 0
#%DD%	[11] : Beam intensity            : 0
#%DD%	[29] : Normalized beam intensity : 0
#%DD%	     : (Beam intensity/Exposure time)
#%DD%	
#%DD%	[12] : Beam center X             : 0.00
#%DD%	[13] : Beam center Y             : 0.00
#%DD%	[14] : Beam size FWHM X          : 0
#%DD%	[15] : Beam size FWHM Y          : 0
#%DD%	[16] : Beam size full X          : 0
#%DD%	[17] : Beam size full Y          : 0
#%DD%	
#%DD%	[18] : Max. pixel value          : 0
#%DD%	[19] : Max. pixel X              : 0
#%DD%	[20] : Max. pixel Y              : 0
#%DD%	
#%DD%	[21] : Beam spot start X         : 0
#%DD%	[22] : Beam spot start Y         : 0
#%DD%	[23] : Beam spot end X           : 0
#%DD%	[24] : Beam spot end Y           : 0
#%DD%	[25] : Beam spot FWHM start X    : 0
#%DD%	[26] : Beam spot FWHM start Y    : 0
#%DD%	[27] : Beam spot FWHM end X      : 0
#%DD%	[28] : Beam spot FWHM end Y      : 0
#%DD%
#%DT%  meteor_counter_add inten "d05/meteor2/eh1" 11 1 %DD%
#    Configures the counter inten with the beam intensity value (index = 11)
#
#%DT%  meteor_counter_add cenx  "d05/meteor2/eh1" 12 1 %DD%
#    Configures the counter cenx with the beam center X value (index = 12)
#
#%DT%  meteor_counter_add ceny  "d05/meteor2/eh1" 13 1 %DD%
#    Configures the counter ceny with the beam center Y value (index = 13)
#
#%DT%  meteoron %DD%
#    Switches on the configured counters during ct and scans.
#
#%DT%  meteoroff %DD%
#    Switches off the configured counters during ct and scans.
#    
#%DT% meteorcalibrate %DD%
#    Start a calibration of the camera exposure time.
# Apply the min and max intensity set by meteorsetup macro.
#
#%XDL%
#
#%END%
#
#
global CCD_METEOR_LIST
global	METEOR_CT_LIST METEOR_SIG_LIST

list_test METEOR_CT_LIST
list_test CCD_METEOR_LIST
#%UU%
#%MDESC%
# Setup meteor CCD control, add sub-private menu to the ccdmenu macro.%BR%
# Call the meteorsetup macro to setup Camera Sony control default parameters too.
#def meteor_setup '
#  cdef ("user_getcounts", "meteor_ct_read\n", "meteor_ct", 0x10)
#  cdef("ccd_other_setup","meteor_private_setup\n","meteor",0x20)
#  cdef("ccd_other_show","meteor_private_show\n","meteor",0x20)
#'  

#%UU%
#%MDESC% unsetup the meteor CCD control
#def meteor_unsetup '
#  cdef("ccd_other_setup","","meteor","delete")
#  cdef("ccd_other_show","","meteor","delete")
#  cdef ("","","meteor_ct","delete")
#'

#%IU%
#%MDESC%
# 
def meteor_private_show '
  local controller
  controller = image_par(_ccd_u,"controller") 
  meteor_initpar(CCD_DS[_ccd_u])
  if (controller == "CCD_PC" && CCD_DS_CONTR[_ccd_u]=="METEOR") {
    printf("        61 -  Meteor Setup . . . . . . . . . . . . :")
    printf("\n            Live Mode                   -> ")
    tty_cntl("md")
    printf("%s\n",(list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"live"))? "ON": "OFF")
    tty_cntl("me")
    printf("            Calibration                 -> ")
    tty_cntl("md")
    printf("%s\n",(list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"auto"))? "Auto": "Manual")
    tty_cntl("me")
    printf("            Threshold                   -> ")
    tty_cntl("md")
    printf("%d\n", list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"thres"))
    tty_cntl("me")
    printf("            Calibration min. Intensity  -> ")
    tty_cntl("md")
    printf("%d\n", list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"min"))
    tty_cntl("me")
    printf("            Calibration max. Intensity  -> ")
    tty_cntl("md")
    printf("%d\n", list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"max"))
    tty_cntl("me")
    printf("\n        62 -  Define Meteor Counters . . . . . . . :")
    tty_cntl("md")
    printf("< %s >\n",(list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"on"))? "YES": "NO")
    tty_cntl("me")
    if (list_getpar(CCD_METEOR_LIST,CCD_DS[_ccd_u],"on")) {        
      printf("           %8.8s:   %16.16s:   %8.8s (counter=signal/factor):\n", "Counter", "Signal", "Factor")
      local _counters _ct_num _index _mne _factor _ccdds
      _counters = list_n (METEOR_CT_LIST)
      for (i=1; i <= _counters; i++) {
        _mne = list_item (METEOR_CT_LIST, i)
        _ccdds =  list_getpar (METEOR_CT_LIST, _mne, "ccd_ds")
        if (_ccdds != CCD_DS[_ccd_u]) continue
        _index = list_getpar (METEOR_CT_LIST, _mne, "index")
        _factor = list_getpar (METEOR_CT_LIST, _mne, "factor")
        printf ("           %8.8s    %16.16s    %8.8s\n", \
        _mne,list_item(METEOR_SIG_LIST, _index), _factor)
      }
    }
  }   
'

#%IU%
#%MDESC%
# 
def meteor_private_setup '
  local controller __ccd_u cam
  controller = image_par(CCD_U,"controller") 
  if (controller == "CCD_PC" && CCD_DS_CONTR[CCD_U]=="METEOR") {
    if ($#)
      __ccd_u=$1
    else
      __ccd_u=CCD_U
    
    meteor_initpar(CCD_DS[__ccd_u])
    if (option == 61) {
      (yesno("            Start live",list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"live")))? meteor_live_on(CCD_U): meteor_live_off(CCD_U)

      (yesno("            Automatic calibration",list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"auto")))? meteor_autocal_on(): meteor_autocal_off()

      list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"thres",getval ("            Threshold        :", list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"thres")))
      meteor_setthreshold(list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"thres"))
      list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"min",getval ("            Min. Intensity   :", list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"min")))     
      list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"max",getval ("            Max. Intensity   :", list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"max")))     
    }  
    else if (option == 62) {
      if (yesno("            Enable Meteor Counters during ct/scans",list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"on"))){
        _meteor_on(CCD_U)
        local _opt
        for(;;) {
          printf("\t")
          tty_cntl("md"); printf("a"); tty_cntl("me")
          printf ("dd / ")
          tty_cntl("md"); printf("d"); tty_cntl("me")
          printf("elete counter or ")
          tty_cntl("md"); printf("q"); tty_cntl("me")
          printf ("uit:")
          _opt = getval("(a/d/q)", "q")
          if (_opt == "q") break
          else if (_opt == "a") {
            local _cnt _pos _factor _ccdds
     
            if(CCD_DS[CCD_U]=="?") {
              printf ("CCD unit %d is unusable, you cannot add new counters\n",CCD_U)
              input("\tHit \"Enter\" to continue: ")
              continue
            }

            _cnt = getval ("\tCounter mnemonic",0)
            if (cnt_num(_cnt) ==-1) {
              print "\tUndefined counter mnemonic !"
              input("\tHit \"Enter\" to continue: ")
              continue
            }
            _ccdds = list_getpar(METEOR_CT_LIST,_cnt,"ccd_ds")
            if (list_item (METEOR_CT_LIST, _cnt)!=-1 && CCD_DS[CCD_U]!=_ccdds) {
               tty_cntl("md")
               printf ("\tCounter \"%s\" is already used by the CCD device \"%s\" !\n",_cnt,_ccdds)
                tty_cntl("me")
               if (!yesno("\tDo you want to redefine it",1)) continue
            }
            printf("\tSignal number :\n\t\t")
            tty_cntl("us");tty_cntl("md")
            printf ("0");tty_cntl("me");tty_cntl("ue")
            printf(" y_beam_center   ")
            tty_cntl("us");tty_cntl("md")
            printf ("1");tty_cntl("me");tty_cntl("ue")
            printf(" z_beam_center\n\t\t")
            tty_cntl("us");tty_cntl("md")
            printf ("2");tty_cntl("me");tty_cntl("ue")
            printf(" beam_intensity  ")
            tty_cntl("us");tty_cntl("md")
            printf ("3");tty_cntl("me");tty_cntl("ue")
            printf(" More...\n")
            _pos = getval ("",0)
            if (_pos!=3) {
              if (_pos == 0) _pos = list_item(METEOR_SIG_LIST,"y_beam_center")
              else if (_pos == 1) _pos = list_item(METEOR_SIG_LIST,"z_beam_center")
              else if (_pos == 2) _pos = list_item(METEOR_SIG_LIST,"beam_intensity")
            } else _pos = _meteor_signal_menu()
            _factor = getval ("            Factor (counter=signal/factor)",1)
            
            meteor_ct_add(_cnt, CCD_DS[CCD_U], _pos, _factor)
          }
          else if (_opt == "d") {
            local _counters _mne i _ccdds _cnt
            _counters = list_n (METEOR_CT_LIST)
            printf("\tDefined counter(s) = ")
            for (i=1; i <= _counters; i++) {
              _mne = list_item (METEOR_CT_LIST, i)
              _ccdds = list_getpar(METEOR_CT_LIST, _mne,"ccd_ds")
              if (_ccdds != CCD_DS[CCD_U]) continue
              printf("%s ",_mne)
            }
            print ""
            _cnt = getval ("\tCounter mnemonic",_mne)
            meteor_ct_remove(_cnt)
          }
        }
      }
      else {
         _meteor_off(CCD_U)
      }      
    }
  }
'

#################
#
# Here start Meteor special macros
#
#################

#%UU% <device-name> <minimum_intensity> <maximum_intensity> <threshold>
#%MDESC% 
# Setup default parameters of the meteor camera.One can add this setup into the SPEC setup to save
# the default setting of the camera calibration.
#
def meteorsetup '
  local param
  if ($#!=4) {
    print "usage: $0 <device-name> <minimum_intensity> <maximum_intensity> <threshold>"
  } else {   
    #first get current setting from the camera
    meteor_initpar("$1")
    list_setpar(CCD_METEOR_LIST,"$1","min",$2)
    list_setpar(CCD_METEOR_LIST,"$1","max", $3)
    list_setpar(CCD_METEOR_LIST,"$1","thres", $4)
    #then apply the new values
    meteor_setthreshold($4,"$1")

  }
'
#%UU% [ccd-unit]
#%MDESC%
# Start the camera live mode
# The "Take an image during ct/scan" feature of ccdmenu is disabled because
# it is incompatible with the live mode.
#

def meteor_liveon '
  local __ccd_u
  if(!$#) __ccd_u = CCD_U
  else __ccd_U = $1
  meteor_live_on(__ccd_u)
'
#%IU%
#%MDESC%
#
def meteor_live_on (u) '{
  esrf_io(CCD_DS[u], "DevCcdLive", 1)
  list_setpar(CCD_METEOR_LIST,CCD_DS[u],"live",1)
  if (CCD_ON[u]) {
    ccdoff u
    tty_cntl("md")
    printf ("            \"Take image during ct/scans\" is now disabled\n")
    tty_cntl("me")
  }
}'

#%UU% [ccd-unit]
#%MDESC%
# Stop the camera live mode
#
def meteor_liveoff '
  local __ccd_u
  if(!$#) __ccd_u = CCD_U
  else __ccd_U = $1
  meteor_live_off(__ccd_u)
'

#%IU%
#%MDESC%
#
def meteor_live_off (u) '{
  esrf_io(CCD_DS[u], "DevCcdLive", 0)
  list_setpar(CCD_METEOR_LIST,CCD_DS[u],"live",0)

  while( meteor_is_running(CCD_DS[u]) ) {
    sleep(.1)
  }
}'

#%UU%  ()
#%MDESC%
# Switch on the intensity calibration for every image aquired.
# The camera calibrates the image intensity using the specified range
# before taking an image by changing the exposure time.
# see meteorsetup.
#
def meteor_autocal_on () '{
  local argin
  argin[0] = 1
  argin[1] = list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"min")
  argin[2] = list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"max")
  esrf_io (CCD_DS[CCD_U], "DevCalibration", argin)
  list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"auto",1)
}'

#%UU% ()
#%MDESC%
# Switch off the intensity calibration for every image aquired.
#
def meteor_autocal_off ()'{
  local argin
  argin[0] = 0
  esrf_io (CCD_DS[CCD_U], "DevCalibration", argin)
  list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"auto",0)
}'

#%UU% [ccd unit]
#%MDESC%
# Switches on the configured counters during ct and scans.
#
def meteoron '
  local __ccd_u
  if ($#) __ccd_u = $1 
  else __ccd_u=CCD_U
  _meteor_on(__ccd_u)
'

cdef ("user_getcounts", "meteor_ct_read\n", "meteor_ct_", 0x20)

def _meteor_on (u)'{
  local str1 str2
  if (CCD_DS_CONTR[u] !="METEOR") {
    print "Cannot switch on this CCD unit !!"
    print "CCD unit "u" is not of type METEOR."
  } else
    list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"on",1)
}'

#%UU% [ccd unit]
#%MDESC%
# Disables the configured counters during ct and scans.
#
def meteoroff '
  local __ccd_u
  if ($#) __ccd_u = $1 
  else __ccd_u=CCD_U
  _meteor_off(__ccd_u)
'

def _meteor_off (u) '{
  local	_mne
  local	_ct_num
  local _counters str1
  if (CCD_DS_CONTR[u] !="METEOR") {
    print "Cannot switch off this CCD unit !!"
    print "CCD unit "u" is not of type METEOR."
  } else {
    list_setpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"on",0)
    	
    _counters = list_n (METEOR_CT_LIST)
    for (i=1; i <= _counters; i++) {
      _ct_num = cnt_num (_mne = list_item (METEOR_CT_LIST, i))			
      S[_ct_num] = 0
    }	
  }
}'

#%UU%
#%MDESC%
#    Shows all readable parameters for the camera with their index
#	  and actual values.
#

def meteorshow '{
  local _val idx n
	
  _val = meteor_getsignals()
  n = list_n(METEOR_SIG_LIST)
  printf ("The actual METEOR camera set-up is:\n\n")
  tty_cntl("md")
  printf ("[%16.16s]   : %31.31s : %s\n","Name","Description","Value")  
  tty_cntl("me")
  for (idx=1; idx!=n+1;idx++) {
    str=sprintf("[%16.16s]   : %31.31s : %s\n", \
    list_item(METEOR_SIG_LIST,idx), \
    list_getpar(METEOR_SIG_LIST,idx,"descr"),\
    list_getpar(METEOR_SIG_LIST,idx,"format"))
    printf(str, _val[idx-1])
  }
}'

#%IU%
#%MDESC%
#
def _meteor_signal_menu ()'{
  local idx n choice
	
  n = list_n(METEOR_SIG_LIST)
  while (1) {

    clscreen()
    tty_move(5,0,"\[us]\[md]METEOR camera Signal list:\[me]\[ue]")
  
    for (idx=1; idx!=n+3;idx+=3) {
      tty_move(1,2+idx/3,sprintf("\[us]\[md]%d\[me]\[ue]:",idx))
      tty_move(6,2+idx/3,sprintf("%s",list_item(METEOR_SIG_LIST,idx)))
      if (idx+2 <=n) {
        tty_move(26,2+idx/3,sprintf("\[us]\[md]%d\[me]\[ue]:",idx+1))
        tty_move(31,2+idx/3,sprintf("%s",list_item(METEOR_SIG_LIST,idx+1)))
        if (idx+3 <=n) {
          tty_move(52,2+idx/3,sprintf("\[us]\[md]%d\[me]\[ue]:",idx+2))
          tty_move(57,2+idx/3,sprintf("%s",list_item(METEOR_SIG_LIST,idx+2)))
        }
      }
    }
    printf("\n\nChoose a signal number: "); choice = input()
    if (list_item (METEOR_SIG_LIST, choice)!=-1) break 
  }
  
  return choice 
}'

#%IU%
#%MDESC%
#
def _meteor_init_siglist () '{
  if (list_check (METEOR_SIG_LIST,"is_auto_calibration") == -1) {
    list_init METEOR_SIG_LIST

    list_add(METEOR_SIG_LIST,"exposure_time")
    list_setpar(METEOR_SIG_LIST, "exposure_time", "descr", "Exposure time (sec.)")
    list_setpar(METEOR_SIG_LIST, "exposure_time", "format", "%4.5f")
    
    list_add(METEOR_SIG_LIST, "threshold")
    list_setpar(METEOR_SIG_LIST, "threshold", "descr", "Pixel Threshold")
    list_setpar(METEOR_SIG_LIST, "threshold", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "calib_intensity")
    list_setpar(METEOR_SIG_LIST, "calib_intensity", "descr", "Calibrated max. Intensity")
    list_setpar(METEOR_SIG_LIST, "calib_intensity", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_image_size")
    list_setpar(METEOR_SIG_LIST, "y_image_size", "descr", "Full image size in Y")
    list_setpar(METEOR_SIG_LIST, "y_image_size", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_image_size")
    list_setpar(METEOR_SIG_LIST, "z_image_size", "descr", "Full image size in Z")
    list_setpar(METEOR_SIG_LIST, "z_image_size", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_roi_start")
    list_setpar(METEOR_SIG_LIST, "y_roi_start", "descr", "ROI start in Y")
    list_setpar(METEOR_SIG_LIST, "y_roi_start", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_roi_start")
    list_setpar(METEOR_SIG_LIST, "z_roi_start", "descr", "ROI start in Z")
    list_setpar(METEOR_SIG_LIST, "z_roi_start", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_roi_end")
    list_setpar(METEOR_SIG_LIST, "y_roi_end", "descr", "ROI end in Y")
    list_setpar(METEOR_SIG_LIST, "y_roi_end", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_roi_end")
    list_setpar(METEOR_SIG_LIST, "z_roi_end", "descr", "Roi end in Z")
    list_setpar(METEOR_SIG_LIST, "z_roi_end", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "is_live")
    list_setpar(METEOR_SIG_LIST, "is_live", "descr", "Live mode (1-on/0-off)")
    list_setpar(METEOR_SIG_LIST, "is_live", "format", "%4d")
    
    list_add(METEOR_SIG_LIST, "num_of_beam_spots")
    list_setpar(METEOR_SIG_LIST, "num_of_beam_spots", "descr", "Number of beam spots")
    list_setpar(METEOR_SIG_LIST, "num_of_beam_spots", "format", "%4d")
    
    list_add(METEOR_SIG_LIST, "beam_intensity")
    list_setpar(METEOR_SIG_LIST, "beam_intensity", "descr", "Beam intensity")
    list_setpar(METEOR_SIG_LIST, "beam_intensity", "format", "%g")
    
    list_add(METEOR_SIG_LIST, "y_beam_center")
    list_setpar(METEOR_SIG_LIST, "y_beam_center", "descr", "Beam center in Y")
    list_setpar(METEOR_SIG_LIST, "y_beam_center", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_beam_center")
    list_setpar(METEOR_SIG_LIST, "z_beam_center", "descr", "Beam cernter  in Z")
    list_setpar(METEOR_SIG_LIST, "z_beam_center", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_size_fwhm")
    list_setpar(METEOR_SIG_LIST, "y_size_fwhm", "descr", "Beam size FWHM in Y")
    list_setpar(METEOR_SIG_LIST, "y_size_fwhm", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_size_fwhm")
    list_setpar(METEOR_SIG_LIST, "z_size_fwhm", "descr", "Beam size FWHM in Z")
    list_setpar(METEOR_SIG_LIST, "z_size_fwhm", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_size")
    list_setpar(METEOR_SIG_LIST, "y_size", "descr", "Beam size full in Y")
    list_setpar(METEOR_SIG_LIST, "y_size", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_size")
    list_setpar(METEOR_SIG_LIST, "z_size", "descr", "Beam size full in Z")
    list_setpar(METEOR_SIG_LIST, "z_size", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "max_pixel_value")
    list_setpar(METEOR_SIG_LIST, "max_pixel_value", "descr", "Maxe pixel value")
    list_setpar(METEOR_SIG_LIST, "max_pixel_value", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_max_pixel")
    list_setpar(METEOR_SIG_LIST, "y_max_pixel", "descr", "Max pixel in Y")
    list_setpar(METEOR_SIG_LIST, "y_max_pixel", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_max_pixel")
    list_setpar(METEOR_SIG_LIST, "z_max_pixel", "descr", "Max pixel in Z")
    list_setpar(METEOR_SIG_LIST, "z_max_pixel", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_spot_start")
    list_setpar(METEOR_SIG_LIST, "y_spot_start", "descr", "Beam spot start in Y")
    list_setpar(METEOR_SIG_LIST, "y_spot_start", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_spot_start")
    list_setpar(METEOR_SIG_LIST, "z_spot_start", "descr", "Beam spot start in Z")
    list_setpar(METEOR_SIG_LIST, "z_spot_start", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_spot_end")
    list_setpar(METEOR_SIG_LIST, "y_spot_end", "descr", "Beam spot end in Y")
    list_setpar(METEOR_SIG_LIST, "y_spot_end", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_spot_end")
    list_setpar(METEOR_SIG_LIST, "z_spot_end", "descr", "Beam spot end in Z")
    list_setpar(METEOR_SIG_LIST, "z_spot_end", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_fwhm_start")
    list_setpar(METEOR_SIG_LIST, "y_fwhm_start", "descr", "Beam spot FWHM start in Y")
    list_setpar(METEOR_SIG_LIST, "y_fwhm_start", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_fwhm_start")
    list_setpar(METEOR_SIG_LIST, "z_fwhm_start", "descr", "Beam spot FWHM start in Z")
    list_setpar(METEOR_SIG_LIST, "z_fwhm_start", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "y_fwhm_end")
    list_setpar(METEOR_SIG_LIST, "y_fwhm_end", "descr", "Beam spot FWHM end in Y")
    list_setpar(METEOR_SIG_LIST, "y_fwhm_end", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "z_fwhm_end")
    list_setpar(METEOR_SIG_LIST, "z_fwhm_end", "descr", "Beam spot FWHM end in Z")
    list_setpar(METEOR_SIG_LIST, "z_fwhm_end", "format", "%4.0f")
    
    list_add(METEOR_SIG_LIST, "norm_beam_intensity")
    list_setpar(METEOR_SIG_LIST, "norm_beam_intensity", "descr", "Normalized beam intensity (I/T)")
    list_setpar(METEOR_SIG_LIST, "norm_beam_intensity", "format", "%g")
    
    list_add(METEOR_SIG_LIST, "is_auto_calibration")
    list_setpar(METEOR_SIG_LIST, "is_auto_calibration", "descr", "Auto calibration (0-off/1-on)")
    list_setpar(METEOR_SIG_LIST, "is_auto_calibration", "format", "%4d")
  }
}'


_meteor_init_siglist()


#%UU% [ counter_mnemonic ccd_ds value_index factor ]
#%MDESC%
# Adds the counter with the given mnemonic which reads the ccd_ds device value 
# specified by the index. The index can be found by executing "meteorshow"!
#
def meteor_counter_add '{
  local factor

  if (($# == 3) || ($# == 4)) {
    factor = ($# == 4) ? $4 : 1
    meteor_ct_add  ("$1", "$2", $3 , factor)
  } else {
    printf("    Usage: ");
    tty_cntl("md");print "meteor_counter_add  counter_mnemonic ccd_ds signal_index signal_factor";tty_cntl("me")
    exit
  }
}'

#%UU% [ counter_mnemonic ]
#%MDESC%
# Removes the counter with the given mnemonic.
#
def meteor_counter_remove '{

  if ($# == 1) {
    meteor_ct_remove  ("$1")
  } else {
    printf("    Usage: ");
    tty_cntl("md");print "meteor_counter_remove counter_mnemonic";tty_cntl("me")
    exit
  }
}'

#%IU% (counter_mnemonic ccd_ds signal_index signal_factor)
#%MDESC%
# Adds the counter with the given mnemonic which reads the ccd_unit camera value 
# specified by the index. The index can be found executing "meteorshow"!
#
def meteor_ct_add (counter_mnemonic, ccd_ds, signal_index, signal_factor) '{
  list_add (METEOR_CT_LIST, counter_mnemonic)
  list_setpar(METEOR_CT_LIST, counter_mnemonic, "ccd_ds", ccd_ds)
  list_setpar(METEOR_CT_LIST, counter_mnemonic, "index", signal_index)
  list_setpar(METEOR_CT_LIST, counter_mnemonic, "factor", signal_factor)
}'

#%IU% (counter_mnemonic)
#%MDESC%
# Removes the counter with the given mnemonic.
#
def meteor_ct_remove (counter_mnemonic) '{
  local	_ct_num
	
  list_remove(METEOR_CT_LIST, counter_mnemonic)
	
  _ct_num = cnt_num (counter_mnemonic)
  S[_ct_num] = 0
}'

float array meteor_sig_arr[10][31]
#%IU%
#%MDESC%
# Reads all counters specified for the camera if the reading was eanabled
# by "meteoron"!
#
def meteor_ct_read '{
  local	_mne _ndev _item
  local	_index _factor
  local	_ct_num i
  local	_counters _factor _ccdds _met_ds
  local devs_stat[] devs_num[]
  
  _ndev = list_n(CCD_METEOR_LIST)
  for(_item=1;_item!=_ndev+1;_item++) {    
    local ds
    ds =  list_item(CCD_METEOR_LIST,_item)
    devs_num[ds]=_item
    if (list_getpar(CCD_METEOR_LIST,_item, "on")) {
      devs_stat[ds]=1
      esrf_io (ds, "DevReadSigValues", meteor_sig_arr[_item])
    } else
      devs_stat[ds]=0
  }
  _counters = list_n (METEOR_CT_LIST)
  for (i=1; i <= _counters; i++) {
    _ct_num = cnt_num (_mne = list_item (METEOR_CT_LIST, i))
    _ccdds = list_getpar (METEOR_CT_LIST, _mne, "ccd_ds")
    if (devs_stat[_ccdds]==0) continue
    _item = list_getpar (METEOR_CT_LIST, _mne, "index")
    _index = list_check(METEOR_SIG_LIST,_item)
    _factor = list_getpar (METEOR_CT_LIST, _mne, "factor")
  
    if ((_factor == -1) || (_factor == 0))  _factor=1			        
    S[_ct_num] = meteor_sig_arr[devs_num[_ccdds]][_index-1]/_factor
  }
    
}'


#%UU%
#%MDESC%
# start calibration of the Meteor2 CCD camera exposure time.
# Apply the min and max intensity set by meteorsetup macro.
#
def meteorcalibrate '
  local min max
  min = list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"min")
  max = list_getpar(CCD_METEOR_LIST,CCD_DS[CCD_U],"max")
  meteor_calibrate_intensity min max
'

#%UU% minimum_intensity maximum_intensity
#%MDESC%
# start calibration of the Meteor2 CCD camera exposure time then return 
# the calibrated maximum intensity and the calibrated exposure time.
#
def meteor_calibrate_intensity '{
  local ret_values 	

  if ($# != 2) {
    printf ("Usage: calibrate_intensity minimum_intensity maximum_intensity\n")
    exit
  }

  ret_values = _meteor_calibrate_intensity ($1, $2)

  printf ("Maximum Intensity = %3.0f\n", ret_values[0])
  printf ("Exposure Time     = %f\n",    ret_values[1])
}'

#%IU% (minimum_intensity, maximum_intensity)
#%MDESC%
# start calibration of the Meteor2 CCD camera exposure time.
#
def _meteor_calibrate_intensity (min, max, dev) '{
  local	inp
  local	state
  local calib_values
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
  
  calib_values[0] = -1.0
  calib_values[1] = -1.0
  inp[0] = min
  inp[1] = max 

  esrf_io (CCD_DS[CCD_U],"DevSetCalib", inp)

  # state == DevCcdTaking 
  while ( meteor_is_running() ) {
    sleep(0.2)
  }

  # state == DevCcdReady 
  if (meteor_is_ready()) {
    esrf_io(CCD_DS[CCD_U], "DevGetCalib", calib_values)
  } else {
    p "Calibration did not finish correctly!"
    p esrf_io(CCD_DS[CCD_U], "DevCcdGetLstErrMsg")
  }

  return (calib_values)
}'

#%IU% (threshold)
#%MDESC%
# set the threshold.
#
def meteor_setthreshold (threshold, dev) '{
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
    
  esrf_io(_dev, "DevCcdSetThreshold", threshold)    
}'

#%IU% 
#%MDESC%
# Return the threshold.
#
def meteor_getthreshold (dev) '{
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
    
  return esrf_io(_dev, "DevCcdGetThreshold")    
}'


#%IU% ()
#%MDESC%
# return Y and Z beam position + Y Fwhm and Z Fwhm
#
def meteor_read_beam_center (dev) '{
  local	state
  local center
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
  
  center[0] = -1.0
  center[1] = -1.0
  
  param_values[0] = -1

  esrf_io(_dev, "DevReadValues", param_values)
 
  if ( param_values[0] == 1 ) {
    center[0] = param_values[2]
    center[1] = param_values[3]
    center[2] = param_values[4]
    center[3] = param_values[5]
  } 
  
  return (center)
}'


#%IU%
#%MDESC%
# 
def meteor_read_intensity (dev) '{
  local param_values
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]

  intensity = -1.0
  
  param_values[0] = -1 
    
  esrf_io(_dev, "DevReadValues", param_values)    
  
  if ( param_values[0] == 1 ) {
    intensity = param_values[1]
  }
  
  return (intensity)
}'
    
#%IU%
#%MDESC%
# 
def meteor_is_running(dev) '{
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
  #state == DevCcdTaking 
  return (esrf_io (_dev,"DevState") == 202899458)
}'

#%IU%
#%MDESC%
# 
def meteor_is_ready(dev) '{
  local _dev
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
  return (esrf_io (_dev,"DevState") == 202899457)
}'

#%IU%
#%MDESC%
# 
def meteor_getsignals(dev) '{
  local _param _dev
  _param[0]=0
  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
  esrf_io (_dev, "DevReadSigValues", _param)
  return (_param)
}'

#%IU%
#%MDESC%
# Initialise the local parameters with the current camera setting.
def meteor_initpar(dev) '{
  local _param _dev  _thr

  if (dev) _dev = dev
  else _dev = CCD_DS[CCD_U]
  if (_dev =="?") return
  _param =  meteor_getsignals(_dev)
  _thr = meteor_getthreshold(_dev)
  list_add(CCD_METEOR_LIST,_dev)
  list_setpar(CCD_METEOR_LIST,_dev,"live",_param[9])
  list_setpar(CCD_METEOR_LIST,_dev,"auto",_param[30])
  list_setpar(CCD_METEOR_LIST,_dev,"thres",_thr)
  list_setpar(CCD_METEOR_LIST,_dev,"min",10)
  list_setpar(CCD_METEOR_LIST,_dev,"max",250)
  if (list_getpar(CCD_METEOR_LIST,_dev,"on")==-1)
    list_setpar(CCD_METEOR_LIST,_dev,"on",0)
}'

#%UU% <Y pixelsize> <Z pixelsize>
#%MDESC%
# store the pixel size in Y and Z into the database through the device server
def meteor_pixelsize '{
  local data[] controller
  data[0]=1; data[1]=1
  if ($# != 0 && $# != 2) {
    print "Usage: $0 <Y pixelsize> <Z pixelsize>"
    print "       um/pixel as unit."
    exit
  }
	controller = image_par(CCD_U,"controller")
	
  if (controller != "CCD_PC" || CCD_DS_CONTR[CCD_U]!="METEOR") {
	  beep; sleep(.2); beep
		print "\nSorry but the Active CCD is not a valid Meteor CCD camera !"
    print "Please, set active a Meteor CCD camera within the \"ccdmenu\" macro."
		exit
	}
  esrf_io(CCD_DS[CCD_U],"DevRestore", data)
  if(!$#) {
    printf("Actual pixel-size is (Y,Z): (%.2f, %.2f) um/pixel\n", 1000*data[0],1000*data[1])
    data[0] = getval("Y pixel-size (um/pixel)", 1000*data[0]) 
    data[1] = getval("Z pixel-size (um/pixel)", 1000*data[1]) 
  } else {
    data[0] = $1; data[1] = $2
  }
  if (data[0]==0 || data[1]==0){
    print "Pixelsize must be not null, exit !!"
    exit
  }
	data[0]/=1000; data[1]/=1000
  esrf_io(CCD_DS[CCD_U],"DevStore", data)
}'

#%MACROS%
#%IMACROS%
#%AUTHOR% BLISS (L.Claustre) / ESRF 2004
#%TOC%