esrf

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

#%TITLE% ccddarkflat.mac
#%NAME% 
#   Gets and handle image for dark image functionality
#
#%EXAMPLE%
#%DT% Taking images %DD%
#  %DL%
#    %DT% ccddark  %DD% Takes a number of images average them and save .
#                  %DD% with the suffix _dark. The image stays in the
#                  %DD% shared array ccd_dark*
#    %DT% ccdflat  %DD% Takes an image, substracts the dark and normalizes it
#                  %DD% with the average. It saves it to disk with the suffix
#                  %DD% _flat, and the image stays in the array ccd_flat*
#    %DT% ccdacq  %DD% Takes a number of images and substract the dark image
#                 %DD% in the array ccd_dark* , and divides by the number
#                 %DD% of images and the flat image in ccd_flat*
#  %XDL%
#
#%LOG%
#  $Revision: 1.4 $
#
#  $Log: ccddarkflat.mac,v $
#  Revision 1.4  2008/02/27 13:04:30  rey
#  documentation changes
#
#Revision 1.3  2004/10/04 06:25:32  claustre
#still a missuse of CCD_U instead of _ccd_u in *_show macro
#
#Revision 1.2  2004/06/25 12:00:14  claustre
#corrected bugs with use of CCD_U instead of _ccd_u for *_show macros
#
#Revision 1.1  2002/12/03 07:39:43  claustre
#Initial revision
#
#%END%

#%UU%  [<exp_time> <cycles>]
#%MDESC% Acquires the dark image and saves it to disk . If cycles is given
# it accumulates images and divides by number of cycles
def ccddark '{
        global DARKARRAY
        local _avg _xsize _ysize _exp_time _noc ii
        if ($# > 2 ) {
                p "usage: $0 [exp_time] [cycles]"
                exit
        }
        _exp_time =$1
        _noc= $2

        _xsize = array_op("rows",@ARRAYNAME)
        _ysize = array_op("cols",@ARRAYNAME)

        DARKARRAY=sprintf("dark_image%d",CCD_U)
        shared float array @DARKARRAY[_xsize][_ysize]
        p "Shared array for dark image:  " DARKARRAY

        if (_noc) {
                for (ii=1; ii<=_noc; ii++) {
                        printf("\rTaking image %4d", ii)
                        if (exp_time)
                                ccdtake _exp_time
                        else
                                ccdtake
                        @DARKARRAY += @ARRAYNAME
                }
                @DARKARRAY /=_noc ;
        } else {
                ccdtake $*
                @DARKARRAY = @ARRAYNAME
        }
        ccd_save(ccdfilename(CCD_DIR[CCD_U],CCD_PREFIX[CCD_U],CCD_N[CCD_U]++,sprintf("_dark%s",CCD_SUFFIX[CCD_U])),@DARKARRAY)

}'
#%UU%  [<exp_time> <cycles>]
#%MDESC% Acquires the flat image and saves it to disk . If cycles is given
# it accumulates images and divides by number of cycles
def ccdflat '{
        global FLATARRAY
        local _avg _xsize _ysize _noc _exp_time _min
        if ($# > 2 ) {
                p "usage: $0 [<exp_time> <cycles>]"
                exit
        }

        _exp_time =$1
        _noc= $2

        _xsize = array_op("rows",@ARRAYNAME)
        _ysize = array_op("cols",@ARRAYNAME)

        FLATARRAY=sprintf("flat_image%d",CCD_U)
        shared float array @FLATARRAY[_xsize][_ysize]
        p "Shared array for flat field:  " FLATARRAY

        if (!(whatis(DARKARRAY) & 0x4000000)){
                DARKARRAY=sprintf("dark_image%d",CCD_U)
                shared float array @DARKARRAY[_xsize][_ysize]
                p "Shared array for dark image created:  " DARKARRAY
                p "DON\'T FORGET TO TAKE A DARK IMAGE (use ccddark)!!!!"
        }
        if (_noc) {
                for (ii=1; ii<=_noc; ii++) {
                        printf("\rTaking image %4d", ii)
                        if (exp_time)
                                ccdtake _exp_time
                        else
                                ccdtake
                        @FLATARRAY += @ARRAYNAME
                }
                @FLATARRAY /=_noc ;
        } else {
                ccdtake $*
                @FLATARRAY = @ARRAYNAME
        }


        @FLATARRAY -= @DARKARRAY

        _min = array_op("min",@FLATARRAY)

        if (_min < 0 )
                @FLATARRAY -=  (_min - 1 ) # avoid dividing by 0 afterwards

        _avg = array_op("sum",@FLATARRAY)/ (_xsize * _ysize)
        @FLATARRAY /= _avg

        ccd_save(ccdfilename(CCD_DIR[CCD_U],CCD_PREFIX[CCD_U],CCD_N[CCD_U]++,sprintf("_flat%s",CCD_SUFFIX[CCD_U])),@FLATARRAY)

}'



def _ccdacqct '{
	global SH_DELAY_OFF
	local _ct_time _no_strips 
	local _was_on _was_external _was_soft_preset

	if ($# != 1 ) {
        	p "This should never happen: usage: $0 <exp_time>"
        	exit
  	}

	_was_on = CCD_ON[CCD_U]
		ccdon

	if (image_par(CCD_U,"ext_trig"))
		_was_external = 1
	else 
		image_par(CCD_U,"ext_trig",1)

	if (image_par(CCD_U,"soft_preset")){ 
		_was_soft_preset = 1
		# Dont preset the camera with the COUNTing time
		image_par(CCD_U,"soft_preset",0)
	}
	image_par(0,"preset",$1)

	if (image_par(CCD_U,"kinetics")) {
		_no_strips = image_par(CCD_U,"rows") / \
					image_par(CCD_U,"kinwinsize") 
			
	} else 
		_no_strips = 1

	_ct_time = $1 *	_no_strips +  SH_DELAY_OFF[CCD_U]/1000 + \
					SH_DELAY_BEG[CCD_U]/1000 
 
	p "Counting for " _ct_time " seconds"
	ct _ct_time 
	
	if (_was_soft_preset)	
		image_par(CCD_U,"soft_preset",1)
	if (!_was_external)	
		image_par(CCD_U,"ext_trig",0)
	if (!_was_on) 
		ccdoff
}'
#%UU%  [<exp_time> <cycles>] 
#%MDESC%  Acquires an image and does the background substraction and 
#flat field correction from shared memories. You need to to first ccddark and 
#ccdflat and  then configure the options with ccdmenu (6)
def ccdacq '{
  	global CORARRAY CCD_DARK CCD_FLAT CCD_BACKGROUND CCD_OFFSET_SUBS
  	local _avg _xsize _ysize _exp_time _noc ii _min 
	local _add_time 
  	if ($# > 4  || $# <1) {
        	p "usage: $0 <exp_time> [<cycles>]"
        	exit
  	}
  	_exp_time =$1
  	_noc= $2

  	_xsize = array_op("rows",@ARRAYNAME)
  	_ysize = array_op("cols",@ARRAYNAME)

  	CORARRAY=sprintf("cor_image%d",CCD_U)
  	shared float array @CORARRAY[_xsize][_ysize]
  	p "Shared array for corrected image:  " CORARRAY

  	if (_noc == 0)
        	_noc=1
  	for (ii=1; ii<=_noc; ii++) {
        	printf("\rTaking image %4d\n", ii)
		if (image_par(CCD_U,"ext_trig")) {
			 _ccdacqct _exp_time
		} else {
        		if (_exp_time)
                		ccdtake _exp_time
        		else
                		ccdtake
		}
        	@CORARRAY += @ARRAYNAME
  	}
  	@CORARRAY /= _noc

  	if (CCD_DARK[CCD_U]) {
        	p "Substrating dark image " DARKARRAY
        	@CORARRAY -= @DARKARRAY
  	}

  	if (CCD_FLAT[CCD_U]) {
        	p "Dividing by Flat image image " FLATARRAY
        	@CORARRAY /= @FLATARRAY
  	}

  	if ( CCD_BACKGROUND[CCD_U] ) {
        	@CORARRAY += CCD_BACKGROUND[CCD_U]
        	p "Adding bg value : " CCD_BACKGROUND[CCD_U]
        	CCD_OFFSET_SUBS[CCD_U]= CCD_BACKGROUND[CCD_U]
  	}else {
          	_min = array_op("min",@CORARRAY)
        	p "min value : " _min

        	if (_min < 0 ){
                	@CORARRAY -=  _min
                	p "image = image - (" _min ") "
                	CCD_OFFSET_SUBS[CCD_U]= _min
        	} else {
                	CCD_OFFSET_SUBS[CCD_U]= 0
        	}
  	}

	if (CCDSAVE[__ccd_u] == 1) {

  		ccd_save(ccdfilename(CCD_DIR[CCD_U],CCD_PREFIX[CCD_U],CCD_N[CCD_U]++,sprintf("_corr%s",CCD_SUFFIX[CCD_U])),@CORARRAY)
        }

}'
# This macro should be put in the setup file 
# Settings are overwitten by reconfig
def ccd_ext_gate_cfg '{
	
	global SH_DELAY_BEG
	local _dev_id _bad_counter _shutterg _ccdg _delay

	if ($# != 3 ) {

		p "usage: $0 <shutter_gate> <ccd_gate> <delay_ms> "
		exit
	
	}

	_shutterg ="$1"
	_ccdg ="$2"
	_delay =$3
	SH_DELAY_BEG[CCD_U] = _delay

	if (cnt_num(_shutterg) == -1 ) {

		p "Counter " _shutterg " does not exist!!"
		p
		_bad_counter = 1
	}
	if (cnt_num(_ccdg) == -1 ) {

		p "Counter " _ccd " does not exist!!"
		_bad_counter = 1
	}
	if (_bad_counter) {

		p " CAUTION: In order to trigger the shutter and the camera"
		p "          from the VCT6 you have to configure two counters"
		p "          one for the shutter shutter and one delayed for"
		p "          the camera"

		exit
	}

 	_delay = counter_par(sec,"scale")*_delay/1000
	_dev_id = counter_par(cnt_num(_ccdg),"device_id")

	p "You are using " counter_par(cnt_num(_shutterg),"device_id") " for shutter"
	p "              " _dev_id " for Camera. Delay= $3 (" _delay " edges)"

	esrf_io(_dev_id,"DevCntEdgeSet",_delay)

}'

#%UU%  <file_name>
#%MDESC%  Reads an image file into flat_array* array (float)
def ccd_open_flat '{
        local _flat_file _header

        if ($# != 1 ) {
                p "usage: $0 <flat_file_name> "
                exit
        }
        _flat_file="$1"
        p "Reading file " _flat_file " in array " FLATARRAY
        fmt_read(_flat_file, "ESRF", @FLATARRAY, _header)

        #p "HEADER"
        #p
        #p _header
}'


def ccd_cor_setup '{
  global CORARRAY CCD_DARK CCD_FLAT CCD_BACKGROUND
  local controller
  controller = image_par(CCD_U,"controller") 
  if (controller=="FRELON" || (controller=="CCD_PC" && CCD_DS_CONTR[CCD_U]=="FRELON")) {
        if ($#==3) {
                CCD_DARK[CCD_U] = $1
                CCD_FLAT[CCD_U] = $2
                CCD_BACKGROUND[CCD_U] = $3
        } else {

                CCD_DARK[CCD_U] = \
                        yesno ("Substract dark image (use ccddark)",\
                                                        CCD_DARK[CCD_U])
                if (CCD_DARK[CCD_U]) {
                        CCD_FLAT[CCD_U] = \
                                yesno ("Divide by Flat image (use ccdflat)",\
                                                        CCD_FLAT[CCD_U])
                        CCD_BACKGROUND[CCD_U] = \
                                getval("Background value (0 => Auto)", \
                                                        CCD_BACKGROUND[CCD_U])
                        p
                        p "Use \'ccdacq [exp_time] [cycles]\' "
                        p "                     to acq and correct images !!!!"
                }
        }

  }
}'
def ccd_cor_show '{
  local controller
  controller = image_par(_ccd_u,"controller") 
  if (controller=="FRELON" || (controller=="CCD_PC" && CCD_DS_CONTR[_ccd_u]=="FRELON")) {
        if (CCD_DARK[_ccd_u]) {
                tty_cntl("me")
                printf("        Correction:. . .Dark ")
                tty_cntl("md")
                printf("%s ", CCD_DARK[_ccd_u]?"ON":"OFF")
                tty_cntl("me")
                printf("| Flat ")
                tty_cntl("md")
                printf("%s ", CCD_FLAT[_ccd_u]?"ON":"OFF")
                tty_cntl("me")
                printf("| BG = ")
                tty_cntl("md")
                printf("%s \n", CCD_BACKGROUND[_ccd_u]? \
                                sprintf("%s",CCD_BACKGROUND[_ccd_u]):"Auto")
                tty_cntl("me")
        } else {
                tty_cntl("me")
                printf("        CORRECTION . . . . . . . . . . . . . . . . .:")
                tty_cntl("md")
                printf("<OFF>")
                tty_cntl("me")
                printf("\n")
        }
  }
}'
def ccd_cor_on '{
        cdef("ccd_other_setup","ccd_cor_setup\n","corr","0x20")
        cdef("ccd_other_show","ccd_cor_show\n","corr","0x20")

}'
# by default we set the correction menu on
# for ID10
ccd_cor_on
def ccd_cor_off '{
        cdef("ccd_other_setup","","corr","delete")
        cdef("ccd_other_show","","corr","delete")
}'

def ccd_kin_setup '{
  global CORARRAY CCD_DARK CCD_FLAT CCD_BACKGROUND SH_DELAY_OFF
  local _kinetics _kinwinsize _kin_trigger
  local controller
  controller = image_par(CCD_U,"controller") 
  if (controller=="FRELON" || (controller=="CCD_PC" && CCD_DS_CONTR[CCD_U]=="FRELON")) {

        if ($#==3) {
                image_par(CCD_U,"kinetics",$1)
                image_par(CCD_U,"kinwinsize",$2)
                image_par(CCD_U,"ext_trig",$3)
        } else {

                _kinetics =  image_par(CCD_U,"kinetics")

                _kinetics = image_par(CCD_U,"kinetics", \
                                        yesno ("Use Kinetics",_kinetics))


                if (_kinetics) {
                        _kinwinsize =  image_par(CCD_U,"kinwinsize")

                        image_par(CCD_U,"kinwinsize", \
                                getval ("Kinetics Window Size",_kinwinsize))

                        _kin_trigger =  image_par(CCD_U,"ext_trig")

                        image_par(CCD_U,"ext_trig", \
                                getval (\
                                "Kinetics Trigger 0:none 1:single 2:multiple",\
                                                                _kin_trigger))
			SH_DELAY_OFF[CCD_U] = \
				 getval("Total transfer delay time (ms)",0)
                }
                #input()
        }

  }
}'

def ccd_kin_show '{
  local controller
  controller = image_par(CCD_U,"controller") 
  if (controller=="FRELON" || (controller=="CCD_PC" && CCD_DS_CONTR[_ccd_u]=="FRELON")) {
        tty_cntl("me")
        if (!image_par(_ccd_u,"kinetics")) {
                printf("        KINETICS . . . . . . . . . . . . . . . . . .:")
                tty_cntl("md")
                printf("<OFF>")
                tty_cntl("me")
        } else {
                printf("        KINETICS:.size: ")
                tty_cntl("md")
                printf("%s ", image_par(_ccd_u,"kinwinsize"))
                tty_cntl("me")
                printf("| Trig: ")
                tty_cntl("md")
                printf("%s ", image_par(_ccd_u,"ext_trig")?"ON":"NONE")
                tty_cntl("me")
                printf("| Sh.add time(ms): ")
                tty_cntl("md")
                printf("%d ",SH_DELAY_OFF[_ccd_u])
                tty_cntl("me")
		
        }
        printf("\n")
  }
}'
def ccd_kin_on '{
        cdef("ccd_other_setup","ccd_kin_setup\n","kin","0x20")
        cdef("ccd_other_show","ccd_kin_show\n","kin","0x20")

}'
# by default we want the kinetics menu to appear
ccd_kin_on
def ccd_kin_off '{
        cdef("ccd_other_setup","","kin","delete")
        cdef("ccd_other_show","","kin","delete")
}'

def ccd_del_shm '{
        global FLATARRAY DARKARRAY CORARRAY
        FLATARRAY=sprintf("flat_image%d",CCD_U)
        DARKARRAY=sprintf("dark_image%d",CCD_U)
        CORARRAY=sprintf("cor_image%d",CCD_U)
        unglobal @FLATARRAY
        unglobal @DARKARRAY
        unglobal @CORARRAY
}'

#%MACROS%
#%IMACROS%
#%TOC%