esrf

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

#%TITLE% SPECFITSERVER.MAC
#%NAME%
#   Macros to interface SpecfitServer (Device server interface to Specfit).
#%DESCRIPTION%
# This macro set allows Spec to call the Specfit modules for peak search
# and fit.
#%SETUP%
# The name of the device server has to be given to specfitserversetup
# In database less mode, the server can be started as:
# python SpecfitServer.py
# That will create a TACO device server of the form:
#         //machine.esrf.fr/SpecfitServer?5000003
# %BR%
#  To get peak search working, play first with the following configuration:%BR%
# fitcon["AutoFwhm"] = 0 %BR%
# fitcon["AutoScaling"] = 1 %BR%
# fitcon["Sensitivity"] = 2.5 %BR%
# fitcon["Yscaling"] = 1 %BR%
# fitcon["fitbkg"] = "Linear" %BR%
# fitcon["fittheory"] = "Area Gaussians" %BR%
# %BR%
# Supported theories are : "Gaussians","Lorentz","Area Gaussians",
# "Area Lorentz","Pseudo-Voigt Line","Area Pseudo-Voigt","Step Down",
# "Step Up","Slit","Hypermet"
# %BR%
#Supported backgrounds are : "Constant","Exponential","Linear","Internal"
#%END%

#%UU% [specfitdevicename]
#%MDESC%
# Set the variable SPECFIT_DEV and set the device server in mode "tcp"
def specfitserversetup '
    global SPECFIT_DEV SPECFIT_CONFIG
    if ($# < 1){
        print "Usage: specfitserversetup deviceservername"
    }else{
        SPECFIT_DEV="$1"
    }
    esrf_io(SPECFIT_DEV,"tcp")
    esrf_io(SPECFIT_DEV,"timeout",10)
    SPECFIT_CONFIG = specfitservergetconf(SPECFIT_DEV)
'

#%UU% (specfitdevicename)
#%MDESC%
# Give back an associative array with the FULL specfit configuration
def specfitservergetconf(specfitdevice) '{
    local conf[]
    local n
    local i
    local configuration
    configuration[0] = "-1"
    n=esrf_io(specfitdevice,"SpecfitGetConfig",conf)
    if (conf[0] == "-1"){
        printf("Error getting %s configuration\n",specfitdevice)
    }else{
        for (i=0;i<(n/2);i++){
            configuration[conf[2*i]] = conf[2 * i + 1]
        }
        delete configuration[0]
    }
    return(configuration)
}'

#%UU% (specfitdevicename,configuration)
#%MDESC%
# Configure the keys of the associative array configuration recognized
# by the device server.
def specfitserversetconf(specfitdevice,parameters) '{
    local i
    local key
    local conf[]
    local errors
    i = 0
    for (key in parameters) {
        conf[i] = key
        i++
        conf[i] = sprintf("%s",parameters[key])
        i++
    }   
    errors=esrf_io(specfitdevice,"SpecfitSetConfig",conf)
    if (errors == -1){
        print "Error in configuration"
    }
    return(errors)
}'

#%UU% (specfitdevicename,two_col_array)
#%MDESC%
# Perform a quick estimation of two_col_array and gives back
# an associative array with the form:%BR%
# tmp[0] = Number of peaks%BR%
# tmp[1] = Position_1%BR%
# tmp[2] = Position_2%BR%
# ...%BR%
# tmp[n] = Position_n
def specfitserversearch(specfitdevice,yourdata) '{
    local argout
    argout[0] = argout[0]
    esrf_io(specfitdevice,"SpecfitSearchArray",yourdata,argout)
    if (argout[0] < 0) {
        print "Exception occurred"
    }
    return(argout)
}'

#%UU% (specfitdevicename,two_col_array)
#%MDESC%
# Perform a quick estimation of two_col_array and gives back
# an associative array with the form:%BR%
# tmp[0] = Number of parameters %BR%
# tmp[1] = Name  of parameter 1 %BR%
# tmp[2] = Value of parameter 1 %BR%
# ... %BR%
# tmp[n] = Value of parameter n 
def specfitserverfit(specfitdevice,yourdata) '{
    local argout,i
    local finalargout
    argout[0]      = -1
    finalargout[0] = -1
    esrf_io(specfitdevice,"SpecfitFitArray",yourdata,argout)
    if (argout[0] < 0) {
        print "Exception occurred"
        return finalargout        
    }
    i = 0
    finalargout[0] = argout[0] * 1.0
    for (i=1;i<finalargout[0]*2+1;i++){
        if ((i % 2) == 0){
            finalargout[i] = argout[i] * 1.0
        }else{
            finalargout[i] = argout[i]
        }
    }
    return(finalargout)
}'

#%UU% (specfitdevice,counter,parameters)
#%MDESC%
# fit the last scan on "counter" and return the fit result.
# If no fit parameters are given the default values are: %BR%
# fitcon["AutoFwhm"] = 0 %BR%
# fitcon["AutoScaling"] = 1 %BR%
# fitcon["Sensitivity"] = 2.5 %BR%
# fitcon["fitbkg"] = "Linear" %BR%
# fitcon["fittheory"] = "Area Gaussians"

def specfitserver_fitscan(specfitdevice,counter,parameters) '{
  local fitcon[]
  if (cnt_num(counter)==-1) {
    print "Invalid counter mnemonic"
    exit
  }

  if (whatis("parameters") & 0x01000000)
      specfitserversetconf(specfitdevice,parameters)  
  else {
    fitcon["AutoFwhm"] = 0
    fitcon["AutoScaling"] = 1
    fitcon["Sensitivity"] = 2.5
    fitcon["fitbkg"] = "Linear"
    fitcon["fittheory"] = "Area Gaussians"
    specfitserversetconf(specfitdevice,fitcon)  
  }

  return specfitserverfit(specfitdevice,SCAN_D[0:NPTS-1][0,cnt_num(counter)+1])
}'


#%UU% (specfitdevice,counter,parameters)
#%MDESC%
# fit the last scan on "counter" plot the fitted data then return the fit result.
# If no fit parameters are given the default values are: %BR%
# fitcon["AutoFwhm"] = 0 %BR%
# fitcon["AutoScaling"] = 1 %BR%
# fitcon["Sensitivity"] = 2.5 %BR%
# fitcon["fitbkg"] = "Linear" %BR%
# fitcon["fittheory"] = "Area Gaussians"
def specfitserver_plotfitscan(specfitdevice, counter, parameters) '{
  #first fit the scan
  #local fitres[]
  local t
  splot
  array gendata[NPTS][2]
  fitres[0]=-1
  fitres = specfitserver_fitscan(specfitdevice,counter,parameters)
  p fitres
  if (fitres[0] < 3) {print "fit not found "; exit}
  gendata[:][0]=SCAN_D[0:NPTS-1][0]
  esrf_io(specfitdevice,"SpecfitGenData",gendata[:][0],gendata[:][1])
  plot_cntl("addline")
  symbcolor = plot_cntl("colors[4]")
  plot_cntl("colors=::::3") #use magenta color for the fit plot 
  array_plot(gendata)
  plot_cntl(t="colors=::::"symbcolor)
  return fitres 
}'


#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitnow 'specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0])'

def fitgaussian 'fitgaussians'
def fitgauss    'fitgaussians'
def fitvoigt    'fitpvoigt'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitgaussians ' {
local parameters[]
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Linear"
parameters["fittheory"] = "Gaussians"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitlorentz ' {
local parameters[]
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Linear"
parameters["fittheory"] = "Lorentz"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitpvoigt ' {
local parameters[]
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Linear"
parameters["fittheory"] = "Pseudo-Voigt Line"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitslit ' {
local parameters[]
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Constant"
parameters["fittheory"] = "Slit"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitstepup ' {
local parameters[]
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Constant"
parameters["fittheory"] = "Step Up"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fitstepdown ' {
local parameters[]
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Constant"
parameters["fittheory"] = "Step Down"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'

#%UU% 
#%MDESC% fit the last scan, return the fit data and plot the fitting curve.
def fithypermet ' {
local parameters[]
local flag
flag = 15
if ($# > 0){
    flag = int($1)
}
parameters["AutoFwhm"] = 0
parameters["AutoScaling"] = 1
parameters["Sensitivity"] = 2.5
parameters["fitbkg"] = "Linear"
parameters["fittheory"] = "Hypermet"
parameters["PositionFlag"] = "1"
parameters["QuotedPositionFlag"] = "1"
if ((flag > 0) && (flag < 16)){
    parameters["HypermetTails"]=flag
}else{
    print "0 < HypermetFlag < 16"
    exit
}
specfitserver_plotfitscan(SPECFIT_DEV,PLOT_SEL[0],parameters)
}'


 
#%MDESC% Force a single gaussian fit taken SPEC parameters as starting parameters.
#Return the fit data and plot the fitting curve.
def fitforcedgaussian '{
  local counter
  local height
  local position
  local fwhm
  local array gendata[NPTS][2]
  local fitres
  local parameters
  local constraints[]
  counter  = PLOT_SEL[0]
  height   = pl_MAX - pl_MIN
  position = pl_xMAX
  fwhm     = pl_FWHM
  parameters["Height1"]   = height
  parameters["Position1"] = position
  parameters["FWHM1"]     = fwhm

  constraints["Height1"]  ["code"] = "POSITIVE"
  constraints["Position1"]["code"] = "QUOTED"
  constraints["Position1"]["cons1"] = pl_MINX
  constraints["Position1"]["cons2"] = pl_MAXX
  constraints["FWHM1"]    ["code"] = "POSITIVE"


  fitres = _fitforcedgaussian(SPECFIT_DEV,                         \
                           SCAN_D[0:NPTS-1][0,cnt_num(counter)+1],\
                           parameters,constraints)
  print fitres
  if (fitres[0]==-1) {print "fit not found "; exit}
  gendata[:][0]=SCAN_D[0:NPTS-1][0]
  esrf_io(SPECFIT_DEV,"SpecfitGenData",gendata[:][0],gendata[:][1])
  plot_cntl("addline")
  symbcolor = plot_cntl("colors[4]")
  plot_cntl("colors=::::3") #use magenta color for the fit plot 
  array_plot(gendata)
  plot_cntl(t="colors=::::"symbcolor)
}'
 
#%IU% 
def _fitforcedgaussian(specfitdev,yourdata,parameters,constraints0)'{
  local fitcon[]
  local estimate[]
  local constraints[]
  local argout[]
  local i
  local key
  #configure a gaussian fit plus linear background
  fitcon["AutoFwhm"] = 0
  fitcon["AutoScaling"] = 1
  fitcon["Sensitivity"] = 2.5
  fitcon["fitbkg"] = "Linear"
  fitcon["fittheory"] = "Gaussians"
  specfitserversetconf(specfitdev,fitcon)
  esrf_io(specfitdev,"SpecfitSetData",yourdata)
  estimate=specfitservergetestimate(specfitdev)
  for (key in parameters){
    estimate[key] = parameters[key]
  }
  specfitserversetestimate(specfitdev,estimate)
  constraints = specfitservergetconstraints(specfitdev)
  if (whatis("constraints0") != 1211105284){
    for (key in constraints0){
        constraints[key] = constraints0[key]        
    }
  }
  specfitserversetconstraints(specfitdev, constraints)
  #print "Estimate prior to fit= ",specfitservergetestimate(SPECFIT_DEV)
  esrf_io(specfitdev,"SpecfitFit",argout)
  if (argout[0] < 0) {
      print "Exception occurred"
      return -1        
  }
  i = 0
  for (i=0;i<argout[0];i++){
    finalargout[argout[2*i+1]] = argout[2*i + 2]
  }
  return finalargout
}'



#%UU% (specfitdevicename)
#%MDESC%
# Give back an associative array with the FULL specfit configuration
def specfitservergetestimate(specfitdevice) '{
    local conf[]
    local n
    local i
    local configuration
    configuration[0] = "-1"
    n=esrf_io(specfitdevice,"SpecfitGetEstimate",conf)
    if (conf[0] == "-1"){
        printf("Error getting %s estimate\n",specfitdevice)
    }else{
        for (i=1;i<(n/2);i++){
            configuration[conf[2*(i-1)+1]] = conf[2 * i]
        }
        configuration[0]="0"
    }
    return(configuration)
}'

#%UU% (specfitdevicename,configuration)
#%MDESC%
# Configure the keys of the associative array configuration recognized
# by the device server.
def specfitserversetestimate(specfitdevice,parameters) '{
    local i
    local key
    local conf[]
    local errors
    parameters[0] = parameters[0]
    delete parameters[0]
    i = 0
    for (key in parameters) {
        conf[i] = key
        i++
        conf[i] = sprintf("%s",parameters[key])
        i++
    }   
    errors=esrf_io(specfitdevice,"SpecfitSetEstimate",conf)
    if (errors == -1){
        print "Error setting estimate"
    }
    return(errors)
}'

#%UU% (specfitdevicename)
#%MDESC%
# Give back an associative array with the FULL specfit configuration
def specfitservergetconstraints(specfitdevice) '{
    local conf[]
    local n
    local i
    local j
    local configuration
    configuration[0] = "-1"
    n=esrf_io(specfitdevice,"SpecfitGetConstraints",conf)
    if (conf[0] == "-1"){
        printf("Error getting %s constraints\n",specfitdevice)
    }else{
        for (i=0;i<=(n-1)/5;i++){
            j = 4 * i +1
            configuration[conf[j]]["code"]  = conf[j + 1]
            configuration[conf[j]]["cons1"] = conf[j + 2]
            configuration[conf[j]]["cons2"] = conf[j + 3]
        }
        configuration[0] = "0"
    }
    return(configuration)
}'

#%UU% (specfitdevicename,configuration)
#%MDESC%
# Configure the keys of the associative array configuration recognized
# by the device server.
def specfitserversetconstraints(specfitdevice,parameters) '{
    local i
    local n
    local key, key0
    local key1, key2
    local conf[]
    local temp[]
    local errors
    i = 0
    key0 = "0"
    for (key in parameters) {    
        if ((key != "0") && (key != 0)){ 
            n=split(key,temp,"\034")
            key1=temp[0]
            key2=temp[1]
            if (key1 != key0){
                key0=key1
                conf[i] = key0
                i++
                conf[i] = sprintf("%s",parameters[key0]["code"])
                i++
                conf[i] = sprintf("%.6g",parameters[key0]["cons1"])
                i++
                conf[i] = sprintf("%.6g",parameters[key0]["cons2"])
                i++
            }
        }
    }
    errors=esrf_io(specfitdevice,"SpecfitSetConstraints",conf)
    if (errors == -1){
        print "Error setting constraints"
    }
    return(errors)
}'


#%MACROS%
#%IMACROS%
#%AUTHOR% V.A.Sole - BLISS Group - ESRF - Copyright 2003
#%TOC%