esrf

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

#%TITLE%
# Wavelength Dispersive Xray spectrometer macros set
#%NAME%
# ID26WDX.MAC

#%DESCRIPTION%
#  set of macros to control the Wavelength Dispersive Xray spectrometer on ID26 beamline
#  and to get control of the WDX crystal position (in keV) as a spec
#  speudo motor. The WDX ratemeter can be associated to a spec pseudo
#  counter, see the wdxsetup macro.
#  WDX sotware controller unit (SCU) is driven through a serial connection (RS232), with
#  high communication speed (up to 115k baud). By default the SCU is set to 9600 baud.
#
#%SETUP%
#  Call first the wdxsetup macro to set the spec serial port as well as the pseudo
#  motor and counter to be used.
#  The pseudo devices (motor and counter) are activated within the call of the %B%wdxon%B%
#  macro and can be set off by calling the %B%wdxoff%B% macro.
#  You can call both %B%wdxsetup%B% and %B%wdxon%B% macros from the spec setup file.
#
#%EXAMPLE%
#   %B%wdx_getcrystal%B% return the current crystal %BR%
#   %B%wdx_getslitsize%B% return the slit size in mm
#%END%

# TO CLEAN :
# -merge all globals...
# -check variable names... w ....
# -prints -> wdx_msg()  wdx_err()  wdx_debug()
# -remove cdef in ON/OFF -> to be put in wdx_setup wdx_unsetup

global WDX_PARAM[]
global WDX_C

# ??? isn-t it a spec constant ??
WDX_C = 12.39842


global WDX_DEV WDX_MOT WDX_CNT WDX_ON  WDX_OLDPOS WDX_RUN
global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS

global WDX_ROW WDX_SIZE WDX_DSIZE WDX_HSIZE

#%UU% <serial device number> [<motor-mnemonic>] [<counter-mnemonic>]
#%MDESC%
# set up parameter to control the WDX. spec serial port has to be specified
# first, then optional parameters can be passed to configure pseudo motor and counter
# to get a useful control of the spectrometer. %BR%
# Use a direct serial connection by using the workstation serial port instead of the
# VME serial ports board (IBAM). You will get everything working smoothly.
def wdxsetup '{
    global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS

    if ($#) {
        WDX_DEV = $1
        WDX_MOT = "$2"
        WDX_CNT = "$3"
        WDX_SLIT_MOTS = "$4"
        WDX_SLIT_MOTP = "$5"
    }
    else {
        WDX_DEV = getval("Serial device from config", WDX_DEV)
        WDX_MOT = getval("Motor mnemonic to get/set keV position or 0 if unused", WDX_MOT)
        WDX_CNT = getval("Counter mnemonic to read ratemeter or 0 if unused", WDX_CNT)
        # cyril Wed 22 Nov 2006 11:21:32
        WDX_SLIT_MOTS =getval("Motor mnemonic to move slits size or 0 if unused", WDX_SLIT_MOTS)
        WDX_SLIT_MOTP =getval("Motor mnemonic to move slits position or 0 if unused", WDX_SLIT_MOTP)
    }

    if (cnt_num(WDX_CNT) == -1) {
        print "wdx: invalid counter mnemonic, " WDX_CNT
        exit
    }

    if (WDX_CNT != 0){
        if (motor_num(WDX_MOT) == -1) {
            print "wdx: invalid motor mnemonic, " WDX_CNT
            exit
        }
    }

    # cyril Wed 22 Nov 2006 11:21:20
    if (WDX_SLIT_MOTS != 0){
        if (motor_num(WDX_SLIT_MOTS) == -1) {
            print "wdx: invalid slit size motor mnemonic, " WDX_SLIT_MOTS
            exit
        }
    }

    if (WDX_SLIT_MOTP != 0){
        if (motor_num(WDX_SLIT_MOTP) == -1) {
            print "wdx: invalid slit position motor mnemonic, " WDX_SLIT_MOTP
            exit
        }
    }

    #get the current configuration of the spectrometer
    wdx_getconfig

    #read crystal in position
    _wdx_getcrystal()

}'


#%UU%
#%MDESC% Main menu to set the WDX up. play with it ...
def wdxmenu '{
    menu("\t\t\t\tWDX CONFIG MENU","_wdx_menu")
}'


#%IU% [parma]
#%MDESC%
#    
def _wdx_menu '{
    menuoptbutton(0,"\t\t\t","Initialize WDX","I")
    menuaction("wdx_init")
    menuoptbutton(0," \n\n","Print Configuration","C")
    menuaction("print WDX_PARAM;input()")

    global SETKEV

    SETKEV = _wdx_readkev()

    menuoptval (0,"KeV\t\t","WDX Energy : ",SETKEV,"E")
    menuvargetv ("SETKEV")
    menuaction("print \"Changing energy ... \";local _w; _w=WDX_C/SETKEV; wdx_setwave _w")
    menuprint(0,"\n", "and Crystal in position is ", WDX_PARAM["crys"][WDX_PARAM["setcrys"]])

    print MENU_SEP
}'


#%UU%
#%MDESC% define chained macros for wdx pseudo counter and motor
# This should be moved in wdx_setup !!!
def wdxon '{
    global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS


    # this cdef should be in setup
    cdef("cleanup_always", "wdx_cleanup", "wdx")

    if (WDX_MOT != 0) {
        cdef("user_checkall", "wdx_move\n", motor_mne(motor_num(WDX_MOT)),0x01)
        cdef("user_getpangles", "wdx_getangle\n", motor_mne(motor_num(WDX_MOT)),0x01)

        # cyril Wed 22 Nov 2006 11:13:06  add this 2 lines for pseudo slit with wdx.
        cdef("user_checkall",   "wdx_slit_s_move\n",     motor_mne(motor_num(WDX_SLIT_MOTS)), 0x01)
        cdef("user_getpangles", "wdx_slit_s_getangle\n", motor_mne(motor_num(WDX_SLIT_MOTS)), 0x01)
        cdef("user_checkall",   "wdx_slit_p_move\n",     motor_mne(motor_num(WDX_SLIT_MOTP)), 0x01)
        cdef("user_getpangles", "wdx_slit_p_getangle\n", motor_mne(motor_num(WDX_SLIT_MOTP)), 0x01)

    }
    if (WDX_CNT != 0) {
        cdef("user_getcounts", "wdx_getcounts\n", cnt_mne(cnt_num(WDX_CNT)),0x02)
    }
    WDX_ON = 1
}'


#%UU%
#%MDESC% undefine chained macros for wdx pseudo counter and motor
# This should be moved in wdx_unsetup !!!
def wdxoff '{

    if (WDX_MOT != 0) {
        # these cdef should be in unsetup
        cdef("", "", motor_mne(motor_num(WDX_MOT)), "delete")
        cdef("", "", cnt_mne(cnt_num(WDX_CNT)), "delete")
    }

    # cyril Wed 22 Nov 2006 11:14:42
    if (WDX_SLIT_MOTS != 0) {
        cdef("", "", motor_mne(motor_num(WDX_SLIT_MOTS)), "delete")
    }

    # cyril Wed 22 Nov 2006 11:14:42
    if (WDX_SLIT_MOTP != 0) {
        cdef("", "", motor_mne(motor_num(WDX_SLIT_MOTP)), "delete")
        cdef("","", "wdx","delete")
    }

    WDX_ON = 0

}'


#%IU%
#%MDESC%
# macro chained to Ctrl-C to finish properly the communicatin with the SCU.
# It clears the serial  buffer until the last command finishes.
# A started command cannot be stopped but just finished !!
def wdx_cleanup '{
    local str str2[]

    #first clear serial port buffer if communication was interrupted
    if (WDX_RUN) {
        printf ("Wait a moment, last WDX command is finishing ... ")
        ser_put(WDX_DEV,"WDX_STATUS\n")
        while ((str=ser_get(WDX_DEV,"\n"))!="") {
            print str
            split(str,str2)
            if (str2[0]=="busy") {
                ser_put(WDX_DEV,"WDX_STATUS\n")
            }
            else if (str2[0] == "status") {
                ser_get(WDX_DEV,"\n"); break
            }
            _show_wait()
        }
        print ""
        WDX_RUN = 0
    }
}'


#%IU%
#%MDESC%
def wdx_move '{
  local w
  if (A[motor_num(WDX_MOT)] != WDX_OLDPOS) {
    w=WDX_C/A[motor_num(WDX_MOT)]
    wdx_setwave w
    WDX_OLDPOS = A[motor_num(WDX_MOT)]
  }
}'


# Wed 22 Nov 2006 11:06:02 cyril
def wdx_slit_s_move '{
global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS
    local w

    if (A[motor_num(WDX_SLIT_MOTS)] != WDX_SLIT_S_OLDPOS) {
        wdx_setslitsize A[motor_num(WDX_SLIT_MOTS)]
        print " wdx_setslitsize " A[motor_num(WDX_SLIT_MOTS)]
        WDX_SLIT_S_OLDPOS = A[motor_num(WDX_SLIT_MOTS)]
    }
}'


# Wed 22 Nov 2006 11:06:02 cyril
def wdx_slit_p_move '{
    global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS
    local w

    if (A[motor_num(WDX_SLIT_MOTP)] != WDX_SLIT_P_OLDPOS) {
        wdx_setslitpos A[motor_num(WDX_SLIT_MOTP)]
        print " wdx_setslitpos " A[motor_num(WDX_SLIT_MOTP)]
        WDX_SLIT_P_OLDPOS = A[motor_num(WDX_SLIT_MOTP)]
    }
}'


# cyril Wed 22 Nov 2006 11:12:45
#%IU%
#%MDESC%
def wdx_slit_s_getangle '{
    global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS
    A[motor_num(WDX_SLIT_MOTS)] = _wdx_getslitsize()
}'


# cyril Wed 22 Nov 2006 11:12:45
def wdx_slit_p_getangle '{
    global WDX_SLIT_MOTS WDX_SLIT_S_OLDPOS WDX_SLIT_MOTP WDX_SLIT_P_OLDPOS
    A[motor_num(WDX_SLIT_MOTP)] = _wdx_getslitpos()
}'


#%IU%
#%MDESC%
def wdx_getangle '{
  A[motor_num(WDX_MOT)] = _wdx_readkev()
}'


#%IU%
#%MDESC%
def wdx_getcounts '{
  S[cnt_num(WDX_CNT)] = _wdx_getcps()
}'


#%UU%
#%MDESC% Intialize the spectrometer, take a while
def wdx_init '{
  if (_wdx_testcom()==-1) {
    print "WDX doesn\'t respond, check serial connection"
    exit
  }
  wdx_datacmd("WDX_INITWDX",1)
  _wdx_getcrystal()
}'


#%IU%
#%MDESC% get the WDX configuation parameters and store them into the
# WDX_PAR[] array.
def wdx_getconfig '{
  local str str2 param[] i val
  str = wdx_datacmd("WDX_GETCONFIG",1)
  if (str != -1) {
    split(str,param,"\n")
    WDX_PARAM["nb"] = 0
    for (i=0;i!=6;i++) {

      sscanf(param[4+(i*6)],"%*s %s", str2)
      if (str2 != "None") {
        WDX_PARAM["nb"]++
        WDX_PARAM["crys"][i] = str2
	WDX_PARAM[str2]["pos"] = i
        sscanf(param[8+(i*6)],"%*s %8g",val); WDX_PARAM[i]["trhi"]=WDX_C/val
        sscanf(param[9+(i*6)],"%*s %8g",val); WDX_PARAM[i]["trlo"]=WDX_C/val
      }
    }
  }
}'


#%UU%
#%MDESC% Test if the serial communication with WDX SCU is ready.
def wdx_testcom '{
  if (_wdx_testcom()==-1) print "Test of serial communication failed !"
  else print "Test of serial comm. successed !"
}'


#%IU%
#%MDESC% test the serial communication
def _wdx_testcom() '{
  if(wdx_datacmd("WDX_ECHO TEST SERIAL COM",0)!= "TEST SERIAL COM\n") return -1
  else return 0
}'


#%UU% <energy-keV>
#%MDESC% change the energy in position
def wdx_setkev '{
  local str
  if (!$#) {
    print "usage: " "$0" " <kev>"
    exit
  }
  if (wdx_checkenergy($1)!=-1){
    str = sprintf("WDX_SETWAVE %f", WDX_C/$1)
    wdx_ackcmd(str,1)
  }
}'


#%UU% <Ang.>
#%MDESC% change the wavelength in position
def wdx_setwave '{
  local str
  if (!$#) {
    print "usage: " "$0" " <Ang.>"
    exit
  }
  if (wdx_checkenergy(WDX_C/$1)!=-1){
    str = sprintf("WDX_SETWAVE %f", $1)
    wdx_ackcmd(str,1)
  }
}'


#%IU%
#%MDESC% check energy is in range and then change crystal if necessary
def wdx_checkenergy (kev) '{
  local lkev hkev str ret
  global _C
  hkev = WDX_PARAM[0]["trhi"]
  lkev = WDX_PARAM[WDX_PARAM["nb"]-1]["trlo"]
  if (kev<lkev || kev>hkev) {
    printf ("Energy ,%.4f, out of range [%.4f - %.4f] keV", kev, lkev, hkev)
    return -1
  }
  #select the crystal if necessary
  if (kev < WDX_PARAM[WDX_PARAM["setcrys"]]["trlo"] || \
  	kev > WDX_PARAM[WDX_PARAM["setcrys"]]["trhi"]){
	  print"Wait, changing crystal ..."
   str = sprintf("WDX_SELECTXTAL %f", WDX_C/kev)
   ret = wdx_datacmd(str,1)
   if(ret!=-1) sscanf(ret,"%d",WDX_PARAM["setcrys"])
	   return ret
  }
  return 0
}'


#%UU%
#%MDESC% print the wdx energy position in keV
def wdx_readkev '{
  printf ("Wdx energy position : %.2f keV\n", _wdx_readkev())
}'


#%IU%
#%MDESC% return the energy position
def _wdx_readkev() '{
  local _e
  sscanf(wdx_datacmd("WDX_GETENERGY",0),"%*s %f",_e)
  return _e
}'


#%UU% [crystal-number (1-6)]
#%MDESC% change the crystal in position
def wdx_setcrystal '{
  local crystal str i

  _wdx_getcrystal()

  if ($#) {
    crystal = "$1"
  }else{
    str="Crystals "
    for (i=0;i!=WDX_PARAM["nb"];i++ ) str= sprintf("%s, %s (%d) ", str, WDX_PARAM["crys"][i],i+1)
    if ((crystal = getval(str,WDX_PARAM["setcrys"]+1))==-1) exit
  }
  if (crystal<1 || crystal>WDX_PARAM["nb"]+1) {
    print "wrong crystal number"
    exit
  }
  if (wdx_ackcmd(n="WDX_SETCRYSTAL " WDX_PARAM["crys"][crystal-1],0)!=-1)
    WDX_PARAM["setcrys"] = crystal-1
}'


#%UU%
#%MDESC% return the crystal name, positionned behind the window
def wdx_getcrystal ' _wdx_getcrystal(); print WDX_PARAM["crys"][WDX_PARAM["setcrys"]]'


#%IU%
#%MDESC% get set crystal in position
def _wdx_getcrystal() '{
  local str crys
  str = wdx_datacmd("WDX_GETCRYSTAL",0)
  sscanf(str,"%*s %s",crys)
  WDX_PARAM["setcrys"] = WDX_PARAM[crys]["pos"]
}'








#%IU%
#%MDESC% get 
def _wdx_gethv() '{
  local str crys
  str = wdx_datacmd("WDX_DETGETHV", 0)
  print str
  # sscanf(str, "%*s %s", crys)
  # WDX_PARAM["setcrys"] = WDX_PARAM[crys]["pos"]
}'












#%UU%[0-1]
#%MDESC%  set on/off amplifier of the SPC (sealed proportional counter).
def wdx_setdetspc '{
  local stat cmd
  if ($#){
    if ($1<0 && $1>1) {
      print "usage: $0 [0 - 1]"
      exit
    }
    stat = $1
  }else
    stat = getval("Set SPC on(1) or off(0):",0)

  cmd = (stat)?"WDX_DETSPCON":"WDX_DETSPCOFF"
  wdx_ackcmd(cmd,0)
}'


#%UU%[0-1]
#%MDESC% set on/off amplifier of the FPC (flow proportinal counter).
def wdx_setdetfpc '{
  local stat cmd
  if ($#){
    if ($1<0 && $1>1) {
      print "usage: $0 [0 - 1]"
      exit
    }
    stat = $1
  }else
    stat = getval("Set FPC on(1) or off(0):",0)

  cmd = (stat)?"WDX_DETFPCON":"WDX_DETFPCOFF"
  wdx_ackcmd(cmd,0)
}'


#%UU%
#%MDESC% read state of the SPC (sealed proportinal counter) amplifier.
def wdx_getdetspc '{
 print wdx_datacmd("WDX_DETGETSPC",0)
}'


#%UU%
#%MDESC% read state of the FPC (flow proportinal counter) amplifier.
def wdx_getdetfpc '{
 print wdx_datacmd("WDX_DETGETFPC",0)
}'


#%UU%
#%MDESC% return the slit position
def wdx_getslitpos '{
  printf ("Slit position : %.2f mm\n", _wdx_getslitpos())
}'


#%IU%
#%MDESC% return the slit position
def _wdx_getslitpos() '{
  local sz
  sscanf(wdx_datacmd("WDX_GETSLITPOS",0),"%*s %f",sz)
  return sz
}'


#%UU%
#%MDESC% return the ratemeter count rate
def wdx_getcps '{
  printf ("Wdx count rate: %.2f cts/s\n", _wdx_getcps())
}'


#%IU%
#%MDESC% return the slit position
def _wdx_getcps() '{
  local _cps
  sscanf(wdx_datacmd("WDX_RATGETCPS",0),"%*s %f",_cps)
  return _cps
}'


#%UU%
#%MDESC% print the slit size
def wdx_getslitsize '{
  printf ("Slit size : %.2f mm", _wdx_getslitsize())
}'


#%IU%
#%MDESC% return the slit position
def _wdx_getslitsize() '{
  local sz
  sscanf(wdx_datacmd("WDX_GETSLITSIZE",0),"%*s %f",sz)
  return sz
}'


#%UU%
#%MDESC% change the slit position
def wdx_setslitpos '{
  local pos
  if($#) {
    pos = $1
  } else {
    pos = getval("Slit position (mm):",_wdx_getslitpos())
  }
  wdx_ackcmd(n= "WDX_SETSLITPOS " pos,0)
}'


#%UU%
#%MDESC% return the slit position
def wdx_setslitsize '{
 local pos
  if($#) {
    pos = $1
  } else {
    pos = getval("Slit size (mm):",_wdx_getslitsize())
  }
  wdx_ackcmd(n= "WDX_SETSLITSIZE " pos,0)
}'


#%IU% ("cmdstring")
#%MDESC% send a command to the spectrometer, with "ACK" status if success.
#-1 is return if it failed.
def wdx_ackcmd(cmd,f) '{
  local ret ret_val[] n
  WDX_RUN = 1
  ser_put(WDX_DEV, n= cmd "\n")
  ret = ser_get(WDX_DEV,"\n")
  if (ret!="ACK\n") {
    split(ret,ret_val)
    if (ret_val[0] == "E100") ret = "unknown command !"
    print "WDX command (" cmd ") failed, " ret
    #still string into buffer
    ser_get(WDX_DEV,"\n");ser_get(WDX_DEV,"\n")
    WDX_RUN=0
    return -1
  }
  #wait until "READY" or an error (Exxx) is return
  ret = ser_get(WDX_DEV,"\n")
  while (ret!="READY\n" && asc(ret)!=69)  {
    ret = ser_get(WDX_DEV,"\n")
    if (f) _show_wait()
  }
  #manage now why cmd failed
  if(asc(ret)== 69) {
    #still string into buffer
    ser_get(WDX_DEV,"\n")
    ret = wdx_datacmd("WDX_LASTERROR",0)
    split(ret,ret_val,"lerror"); split(ret_val[1],ret_val,"\n")
    print"WDX command(" cmd ") failed: "ret_val[0]"."
    WDX_RUN=0
    return -1
  }
  WDX_RUN=0
  return 0
}'


#%IU% ("cmdstring")
#%MDESC% send a data command to the spectrometer, and data are return if sucess
# otherwise -1
def wdx_datacmd(cmd,f) '{
  local ret ret_val i n
  WDX_RUN=1
  ser_put(WDX_DEV, n = cmd "\n")
  ret = ser_get(WDX_DEV,"\n")
  if (ret!="ACK\n") {
    split(ret,ret_val)
    if (ret_val[0] == "E100") ret = "unknown command !"
    print "WDX command (" cmd ") failed, " ret
    #still string into buffer
    ser_get(WDX_DEV,"\n");ser_get(WDX_DEV,"\n")
    WDX_RUN=0
    return -1
  }

  ret_val=""
  while ((ret = ser_get(WDX_DEV,"\n"))!="READY\n"){
    ret_val = ret_val ret
    if (f) _show_wait()
  }
  WDX_RUN=0
  return ret_val
}'


#%IU%
#%MDESC% draw a bar while action goin on
global _WDX_M
def _show_wait() '{
  printf("\b%s", substr("-\\|/", (_WDX_M=++_WDX_M%4)+1, 1))
  sleep(.07)
}'


#%IU% ("filename")
#%MDESC% read an .spm (wdx data format) file and return 1 if it successes
# otherwise -1
def _wdx_read(filename) '{
  local dummy_header i n

  if(file_info(filename,"-e")!=1) {
    print "file "filename " doesn\'t exist"
    return 0
  }
  if(file_info(filename,"-r")!=1) {
    print "file "filename " isn\'t readable"
    return 0
  }

  WDX_SIZE = file_info(filename,"size")
  string array wdx_buff1[WDX_SIZE]
  fmt_read(filename,"raw",wdx_buff1,dummy_header)

  for (i=0;i!=WDX_SIZE;i++)
    if (wdx_buff1[i]==26 && wdx_buff1[i+1]==13&&wdx_buff1[i+2]==10) break
  if (i==WDX_SIZE) {
    print "problem reading WDX data file, is "filename" valid WDX file ?"
    return 0
  }

  WDX_HSIZE = i+3
  WDX_DSIZE = (WDX_SIZE-WDX_HSIZE)
  WDX_ROW = WDX_DSIZE/8

  ubyte array wdx_buff2[WDX_DSIZE]
  wdx_buff2 = wdx_buff1[WDX_HSIZE:WDX_SIZE-1]

  fmt_write(n="tmpfile_"WDX_SIZE,"raw",wdx_buff2)

  ulong array wdx_buff2[WDX_ROW][2]

  fmt_read(n="tmpfile_"WDX_SIZE,"raw", wdx_buff2,dummy_header)
  unix(n="/bin/rm -rf tmpfile_"WDX_SIZE)

  return 1
}'


#%IU% ("inputfile", "outputfile")
#%MDESC% write in text format the data previously read with the _wdx_read()
#function, return 1 if it successes otherwise -1
def _wdx_write(inf,outf) '{
  local i n str

  if(file_info(outf,"-e")) {
    if(!yesno(n="file "outf " exists, overwrite it",1)) return
    else
      unix(n="/bin/rm -rf "outf)
  }

  fprintf(outf,"# WDX .spm file converted using wdx_export spec macro (BLISS, ESRF)\n")
  fprintf(outf,"# %s\n\n", date())
  fprintf(outf,"# Converted .spm file is \"%s\"\n", inf)
  fprintf(outf,"# .spm Header :\n")
  str=sprintf("%s", wdx_buff1[0:WDX_HSIZE-4])
  fprintf(outf,"%s",str)
  for(i=0;i!=WDX_ROW;i++){
    fprintf(outf,"%d,%d\n", wdx_buff2[i][0], wdx_buff2[i][1])
  }
  close(outf)
}'


#%UU% "spm file"
#%MDESC% Read the spm file (WDX data format) and plot the spectrum.
#
def wdx_import '{
  if (!$#) {
    print "usage: wdx_import spm-filename"
    exit
  }

  if (_wdx_read("$1")!=1) exit
  array_plot(wdx_buff2)
}'


#%UU% "spm file"
#%MDESC% Read the spm file (WDX data format) and plot the spectrum,
# then save the data in a text format file, same prefix and suffix = .txt
def wdx_export '{
    local str[] outf

    if ($#!=1) {
        print "usage: wdx_export <input-filename>"
        exit
    }

    if (_wdx_read("$1")!=1){
        exit
    }

    array_plot(wdx_buff2)
    split("$1",str,".")
    outf = str[0]".txt"
    _wdx_write("$1",outf)
}'


#%UU% <directory-path>
#%MDESC% import and export all the files from the specified directory.
def wdx_exportall '{
    local n filename str[] inf outf

    if (!$#) {
        print "usage: wdx_importall <spm-directory>"
        exit
    }

    if (!file_info("$1","-d") && !file_info("$1","-w")) {
        print "Directory $1 doesn\'t exit or is not writable"
        exit
    }

    if (file_info("tmpfile","-e")){
        unix("/bin/rm -f tmpfile")
    }

    unix("ls $1/*.spm > tmpfile")

    for (;;) {
        if ((filename = getline("tmpfile")) !=-1){
            split(filename,str,"\n")
            inf = str[0]
            split(inf,str,".")
            outf = str[0]".txt"
            _wdx_read(inf)
            array_plot(wdx_buff2)
            _wdx_write(inf,outf)
        }else break
    }

    close("tmpfile")

}'


#%MACROS%
#%IMACROS%
#%AUTHOR% L.Claustre - BLISS - ESRF - Nov. 2001
#%TOC%