esrf

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

# SAVESCAN.MAC macros to write a scan to a datafile 
#
#    Saves the last scan to a file. This macro can be used if
#    datafile is switched off and you want to keep the last scan done.
#    If there is an datafile the macro will work, too
#MACROS
# savescan [filename] : If the filename is not given the user will be asked to
#     provide one.
#
#JORG KLORA 15.2.93

#plotting is not switched on
if ((whatis("PL_G")>>16)&0x0800) {
  global PL_G
  PL_G = 0
  data_grp(0,4096,COUNTERS+2)
  rdef scan_plot '{
    local i
    data_put(PL_G, NPTS, 0, A[_m[0]]*UN) ;
    data_put(PL_G, NPTS, 1, S[2]) ;
    data_put(PL_G, NPTS, 2, S[1]) ;
    for (i=3;i<COUNTERS;i++)
      data_put(PL_G, NPTS, i, S[i]) ;
    _plot
    }'
  }


def _savescan '
	HEADING=_nm>1? sprintf("a%dscan ", _nm):"ascan "
	for (i=0;i<_nm;i++) {
		HEADING=sprintf("%s%s",HEADING,sprintf(" %s %g%s %g%s ",\
			motor_mne(_m[i]),_s[i]*UN,UL,_f[i]*UN,UL))
		_d[i] = (_f[i] - _s[i]) / _n1
	}
	HEADING=sprintf("%s %g %g",HEADING,_n1,_ctime)
	_cols=_nm+_hkl_col
	X_L = motor_name(_m[0])
	Y_L = cnt_name(DET)
	_sx = _s[0]*UN; _fx = _f[0]*UN
	_stype = 1|(_nm<<8)
	FPRNT=PPRNT=VPRNT=""
	for (i=0;i<_nm;i++) {
	     FPRNT=sprintf("%s%s  ",FPRNT,motor_name(_m[i]))
	     PPRNT=sprintf("%s%8.8s ",PPRNT,motor_name(_m[i]))
	     VPRNT=sprintf("%s%9.9s ",VPRNT,motor_name(_m[i]))
	}
	FPRNT=sprintf("%s%s  ",FPRNT,_hkl_sym)
	_savescan_head
	PFMT=sprintf("%%s%%8.%df ",UP)
	VFMT=sprintf("%%s%%9.%df ",UP)
	def _scan_on \'
	 for (; NPTS < _n1; NPTS++) {
		for (i=0;i<_nm;i++)
			A[_m[i]] = _s[i] + NPTS * _d[i]
		FPRNT=PPRNT=VPRNT=""
		for (i=0;i<_nm;i++) {
		     FPRNT=sprintf("%s%g ",FPRNT,A[_m[i]]*UN)
		     PPRNT=sprintf(PFMT,PPRNT,A[_m[i]]*UN)
		     VPRNT=sprintf(VFMT,VPRNT,A[_m[i]]*UN)
		}
		FPRNT=sprintf("%s%s ",FPRNT,_hkl_val)
		{
 		  local _i
                  S[1]=data_get(PL_G,NPTS,2)
		  S[2]=data_get(PL_G,NPTS,1)
                  for (_i=3;_i<COUNTERS;_i++) {
		    S[_i]=data_get(PL_G,NPTS,_i)
 		    }
 		}
		_savescan_loop
	 }
	 _savescan_tail
	\'
	_scan_on
'
def _savescan_head '
	NPTS = T_AV = MT_AV = 0
	DATE = date()
	TIME = TIME_END = time()
	_cp = A[_pmot]

	# DATA FILE HEADER
	ond; offt
	printf("\n#S %d  %s\n#D %s\n",SCAN_N,HEADING,DATE)
	if (_ctime < 0)
		printf("#M %g  (%s)\n", -_ctime, cnt_name(MON))
	else
		printf("#T %g  (%s)\n", _ctime, cnt_name(sec))

	_head_par G 0
	_head_par U 1
	_head_par M 2

	printf("#Q %s\n", _hkl_val)
	{
		local i j k
		for (i = 0, k = MOTORS; i < k; i += 8) {
			printf("#P%d ", i/8)
			_mo_loop .6g "A[mA[j]]"
		}
	}
	Fheader
	{
		local i j z
		z = _ctime < 0? sec:MON
		for (i=j=0;i<COUNTERS;i++)
			if (i != z && i != DET && cnt_name(i) != "unused")
				j++
		printf("#N %d\n", _cols + 3 + j)
		printf("#L %s%sEpoch",FPRNT,Flabel)
		for (i=0;i<COUNTERS;i++)
			if (i != z && i != DET && cnt_name(i) != "unused")
				printf("  %s",cnt_name(i))
		if (z >= 0)
			printf("  %s",cnt_name(z))
		printf("  %s\n",cnt_name(DET))
	}
	offd

'
def _savescan_loop '
	local z
	if (DATAFILE != "") {
		local i
		z = _ctime < 0? sec:MON
		fprintf(DATAFILE,"%s%s%d",FPRNT,Fout,(TIME_END=time())-EPOCH)
		for (i=0;i<COUNTERS;i++)
			if (i != z && i != DET && cnt_name(i) != "unused")
				fprintf(DATAFILE," %g",S[i])
		if (z >= 0)
			fprintf(DATAFILE," %g",S[z])
		fprintf(DATAFILE," %g\n",S[DET]);
	}
'
def _savescan_tail '
	TIME_END = time()
	if (!(_stype&8)) {
		ond; offt
		Ftail
		offd; ont
		plot
	}
'

def savescan '

global SAVESCANFILE OLDDATAFILE
if ($#==0) {
  SAVESCANFILE=getval("Write the current scan to which file?",SAVESCANFILE)
  }
else {
  SAVESCANFILE="$1"
  }
 
if (DATAFILE != "") {
  OLDDATAFILE=DATAFILE 
  }
else {
  OLDDATAFILE="null"
  }

_savescannewfile SAVESCANFILE SCAN_N
_savescan
_savescannewfile OLDDATAFILE SCAN_N
'

def _savescannewfile '
	_3 = DATAFILE
	if ($# == 0) {
		_1 = getval("\nData file", DATAFILE)
		_2 = 0
	} else {
		_1 = $1; _2 = $2
	}
	if (_1 == "")
		exit
	if (_1 == "null")
		_1 = "/dev/null"
	if (!index(_1, "/") && !unix(sprintf("test -d %s",DATA_DIR)))
		_1 = sprintf("%s/%s",DATA_DIR,_1)
	if (unix(sprintf("test -r %s",_1))) {
		if (_1 == DATAFILE) {
			printf(\
	"Warning:  Same name as before, but file isn\'t in %s directory.\n",\
					     index(_1, "/")? "same":"current")
			printf("A new version of the file will be created.\n")
		}
		DATAFILE="/dev/null"
	} else if (_1 != "/dev/null") {
		local s
		s = sprintf("%s/chk_file", SPECD)
		if (unix(sprintf("test -x %s && %s %s",s,s,_1)))
			print "Note:  file already exits."
	}
	;
	if (open(_1)) {
		DATAFILE="/dev/null"
		exit
	}
	if (_1 != "tty" && _1 != "/dev/tty")
		close(_1)
	if (_1 == DATAFILE) {
		if (open(_1)) {
			DATAFILE="/dev/null"
			exit
		}
		if ($# == 2 && SCAN_N != $2)
			SCAN_N = $2
	} else {
		if ($# == 0)
			_2 = getval("Last scan number",_2)
		else if ($# == 1)
			_2 = 0
		if (_3 != "" && _3 != "0" && _3 != "tty" && _3 != "/dev/tty")
			close(_3)
		if (open(_1)) {
			DATAFILE="/dev/null"
			exit
		}
		DATAFILE = _1
		SCAN_N = _2
		ond; offt
		if (SCAN_N == 0) {
			constant EPOCH time()
			printf("#F %s\n", DATAFILE)
		}
		printf("#E %d\n#D %s\n#C %s  User = %s\n",\
			EPOCH,date(),TITLE,USER)
		local i j k
		for (i = 0, k = MOTORS; i < k; i += 8) {
			printf("#O%d ", i/8)
			_mo_loop "s " "motor_name(mA[j])"
		}
		offd; ont
	}
	printf("Using \"%s\".  Next scan is number %d.\n",DATAFILE,SCAN_N+1)
'