esrf

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

#%TITLE% ccdconfigparser.mac
#%NAME%
#   Macros to parse ccdconfig files
#%CATEGORY% Detection, ccd
#
#%LOG%
# DFC 11/04/2002.
#     added shutter output control and shutter closing time for frelon
#     the selection of the input channels was also changed
#
# DFC 04/12/2003
# 	esrf_io(... "timeout" , 7  ), otherwise CcdStop looses the connection
# DFC 03/9/2004
#	FRELON2000 flag added (needed on ID19 for the pixel size)
#	Different behaviour with the device server
#	
#%END%

ONLINE=0
if (whatis("ccd_postconfig")==0)
	def ccd_postconfig ''
if (whatis("ccd_preconfig")==0)
	def ccd_preconfig ''

def globals '{
	global CCDCONFIG

	global CCD_U CCD_ON SOFT_PRESET CCD_TIME CCD_ADD_TIME
	global CCDBIN ROI_ACTIVE CCD_DS CCD_DS_CONTR CCD_INCH CCD_DEPTH
	global CCDSAVE CCDSAVE_MODE NO_CCDS
	global CCD_DIR CCD_PREFIX CCD_SUFFIX CCD_N ARRAYNAME
	 
	global CCD_INTEG CCD_SUM CCD_MNE CCD_MNE2 CCD_NORM
	global ROI_GRAIN
	global FRELON2000
				  
	global CCD_EXT_HEADER MY_EXT_HEADER                                     
}'
def ccdconfig '{

	globals

	if ($#) {
		CCDCONFIG = "$1"
	} else {
		CCDCONFIG = sprintf("%s/%s/userfiles/ccdconfig",SPECD,SPEC)
	}

	if (!file_info(CCDCONFIG, "-e")) {
		print "File " CCDCONFIG " does not exist. Creating a new one"
	}
	if (!file_info(CCDCONFIG, "-w")) {
		print "File " CCDCONFIG " does not have write permissions !!!"
	}
	
	unix(sprintf("ccdconf 0 %s",CCDCONFIG))
	#unix(sprintf("%s/ccdconf 0 %s", SPECD, CCDCONFIG))


	if (ONLINE) {
		SERVER

	} else {
		ccdconfigparser CCDCONFIG
		ccdparser	
	}
}'

def ccdreconfig '{
	globals
	if (whatis(CCDCONFIG) == 0 ) 
		CCDCONFIG = sprintf("%s/%s/userfiles/ccdconfig",SPECD,SPEC)
	ccdconfigparser CCDCONFIG
	ccdparser
}'

def ccdparser '{
	if (image_par(CCD_U,"type") > 2000) {
		# this is for the new Frelon 2k + ESPIA cards
		ccdparser_F2K
	} else {
		# this is for the old device servers + old frelon2000
		# + EDT PDV card
		ccdparser_F2000
	}
}'
def ccdparser_F2K '{

}'

def ccdparser_F2000 '{
	
	local _myccdno _dsmode _inp_ch _channel_code
	_myccdno = CCD_U
	_channel_code[0]=15; _channel_code[1]=1; _channel_code[2]=2
	_channel_code[3]=4;  _channel_code[4]=8; _channel_code[5]=3
	_channel_code[6]=12; _channel_code[7]=5; _channel_code[8]=6

	ccd_preconfig

	image_par(_myccdno,"kinwinsize",XKIN_WINSIZE)		
	if (XKINETICS) {
		if (XKIN_MODE)
			image_par(_myccdno,"kinetics",2)
		else
			image_par(_myccdno,"kinetics",1)

	} else {
		image_par(_myccdno,"kinetics",1)
	}

	# connerie . waiting for  solution from the server
	# it is not reset ok in the server  
	#
	if (!XKINETICS) {
		image_par(_myccdno,"kinetics",0)
	}		
	if (XUSE_BIN)  {
		image_par(CCD_U,"row_bin",XROW_BIN) 
		image_par(CCD_U,"col_bin",XCOL_BIN)
		
	} else {
		image_par(CCD_U,"row_bin",1) 
		image_par(CCD_U,"col_bin",1)
	}
	
	ccdroi XUSE_ROI XROI_ROW[0] XROI_ROW[1] XROI_COL[0] XROI_COL[1]

	if (XCCD_ON) {
		ccdon _myccdno
	}else {
		ccdoff _myccdno
	}
	
	#CCD_INTEG[_myccdno]
	#CCD_NORM[_myccdno]

	if ( cnt_num(X_MNE[0]) != -1) {
		CCD_MNE[_myccdno] = X_MNE[0]
	} else {
		ccdintegroff
	}
	if ( cnt_num(X_MNE[1])!= -1) {
		CCD_NORM[_myccdno] = 1
		CCD_MNE[_myccdno] = X_MNE[1]
		#avg . Overwrites the other counter to match ccdintegr
		if ( cnt_num(X_MNE[2])!= -1) {
			CCD_NORM[_myccdno] = 2
			CCD_MNE2[_myccdno] = X_MNE[2]
			# ivg&Std
		}
		ccdintegron
	}else {
		CCD_NORM[_myccdno] = 0
	}

	if (XCCD_EXT_TRIG==1) {
		image_par(_myccdno,"ext_trig",1)
		image_par(_myccdno,"soft_preset",1)
	} else if (XCCD_EXT_TRIG==2) {
		image_par(_myccdno,"ext_trig",1)		
		image_par(_myccdno,"soft_preset",0)		
	} else {
		image_par(_myccdno,"ext_trig",0)		
		image_par(_myccdno,"soft_preset",1)		
	}
	

	if (XCCD_TRIG_MODE && XCCD_EXT_TRIG) {
		image_par(_myccdno,"ext_trig",2)
	}


#	ccdnewfile  XAUTO_SAVE XCCD_DIR XCCD_PREFIX XCCD_SUFFIX XCCD_N

	CCDSAVE[_myccdno] 	= XAUTO_SAVE
	CCDSAVE_MODE[_myccdno] = XREMOTE_SAVE

	if (!XFORCE_CCDN) {
		XCCD_N = CCD_N[_myccdno]
	}
	if (CCDSAVE_MODE[_myccdno] == 1) {
		local _myvar
		myvar=".edf"
		_ccd_remote_file_setup CCD_U XCCD_DIR XCCD_PREFIX myvar XCCD_N

	} else {
		CCD_DIR[_myccdno] 	= XCCD_DIR
		CCD_PREFIX[_myccdno] 	= XCCD_PREFIX
		CCD_SUFFIX[_myccdno] 	= XCCD_SUFFIX?".gif":".edf"
		CCD_N[_myccdno] 	= XCCD_N
	}

	image_par(_myccdno,"hw_par",sprintf("%d %d %d %d",XFLIP, XKIN_BEGIN, XKIN_STRIPES, X_OVIEWLN))		

	#f3send "SLB"XKIN_BEGIN
	
	_dsmode = X_DSMODE[0] + X_DSMODE[1]*4 + X_DSMODE[2]*8 + X_DSMODE[3]*32 + X_DSMODE[4]*16 + X_DSMODE[5]*128 + X_DSMODE[6]*256
	image_par(_myccdno,"mode",_dsmode)

	#_inp_ch =  XCHANNEL[0] + XCHANNEL[1]*2 + XCHANNEL[2]*4 + XCHANNEL[3]*8

	_inp_ch =  _channel_code[XCHANNELS]
	
	image_par(_myccdno,"input_channel",_inp_ch)

	# put here all image_par( "talk") to do a single f3_dump
	# Didnt work so i did single f3send dump after each comand
	# (Still dont know why it didnt work

	image_par(_myccdno,"talk",sprintf("F%d\r\n",XSHUTTER_TIME))
	f3_dump
	image_par(_myccdno,"talk", sprintf("U%d\r\n",XOUT_SHUTTER))
	f3_dump

	SHUTTER_CLOSING_TIME = XSHUTTER_TIME

	while (res != -1) {
		sleep (.05) 
                if ((res = image_par(CCD_U,"talk")) == "") {
                    sleep (.1)
                    if ((res = image_par(CCD_U,"talk")) == "")
                        break
                }
                printf("%s",res)
	}
	esrf_io(image_par(CCD_U,"device_id"),"timeout",7);

	if (image_par(CCD_U,"cols") * image_par(CCD_U,"col_bin") > 1024 )
		FRELON2000=1
	else
		FRELON2000=0

	ccd_postconfig

}'


def ccdconfigparser '{
	local _ccdcf _myline _myarray
	
	if ($#) {
		_ccdcf = $1
	}else {
		_ccdcf = CCDCONFIG
	}

	_myline = getline (_ccdcf,0)
	if (_myline == -1) {
		printf("Can\'t open file \"%s\"\n",_ccdcf);
	} else {
		p _myline
		while ((_myline = getline (_ccdcf)) != -1) {
			local _myvar  _t _arr_nonewline
			split( _myline, _myarray, " = ")
			split(_myarray[1], _arr_nonewline, "\n")
			_myvar = sprintf("%s",_myarray[0])
			_myvar = strtoupper(_myvar)	
			#if (substr(_myarray[1],0,1) =="[")  {
			if (index(_arr_nonewline[0],"["))  {
				local _myresult
				_process_subarray(_arr_nonewline[0])
				_myvar = sprintf("%s",_myarray[0])
				_myvar = strtoupper(_myvar)	
				global @_myvar
				for (i in _myresult) {
					@_myvar[i]= _myresult[i]
				}
				if (DEBUG == 192)
					p @_myvar
			}else {
				global  @_myvar
				@_myvar = _arr_nonewline[0]
				if (DEBUG == 192)
					p _myvar" = " @_myvar
			}
		}
	}
}'
#%UU%
#%MDESC%
# Takes a list from the file and puts it into an associative array
#
def _process_subarray '{
	local _i

	split(_arr_nonewline[0], _myresult, ", ")
	
	# takes out ] and [ to get values inside a list
	for (_i in _myresult) {
		local _beg _len 

		_beg = index(_myresult[_i],"[")
		_len = length(_myresult[_i])
		_myresult[_i] = substr(_myresult[_i], _beg+1, _len)
		
		_end = index(_myresult[_i],"]")
		if (_end) {
			_myresult[_i] = substr(_myresult[_i], 1, _end-1)
		}
		# takes out \' ... we use \" for strings
		while (index(_myresult[_i],"\'")==1) { 
			_len = length(_myresult[_i])
			_myresult[_i] = substr(_myresult[_i], 2, _len)	
		}
		while ((_end=index(_myresult[_i],"\'"))>1) { 
			_myresult[_i] = substr(_myresult[_i], 1, _end-1)	
		}
	}
}'

#%UU%
#%MDESC%
# Crypts a string moving the ascii codes N values
#
def strcrypt(_mystr,_value, _ldown, _lup) '{
	local _ii _my_substr _my_chr _my_result

	for (_ii=0 ; _ii < length(_mystr) ; _ii++) {
		_my_substr = substr(_mystr,_ii+1,1)
		_my_chr = asc(_my_substr)
		if (_my_chr>=_ldown && _my_chr<=_lup)
			_my_chr += _value

		_my_result = sprintf("%s%c",_my_result,_my_chr)
	}
	return _my_result
}'
#%UU%
#%MDESC%
# Converts a string to upper case
#
def strtoupper(_mystr) '{
	return strcrypt(_mystr,-32, 97, 122)
}'
#%UU%
#%MDESC%
# Converts a string to lower case
#
def strtolower(_mystr) '{
	return strcrypt(_mystr,32, 65, 90)
}'