Print View

NERS/BIOE 580
Lab03 - Source Module A

Purpose:

In this module students will generate data files for the x-ray spectrum emerging directly from the target of an electron impact x-ray tube (i.e. before attenuation by the tube envelope or other filters). Spectra for both Molybdenum and Tungsten targets at different kVp will be observed. A simple script will be written to select kVp and observe the spectra as a plot on the screen.

Discussion:

X-rays are commonly generated by accelerating electrons that strike a small region on the surface of a target with high energy. These electrons penetrate a small distance into the target while continuously losing energy. Occasionally, the energetic electron will cause an inner shell electron of a target atom to be ejected. Subsequent filling of this vacant shell can result in the emission of an x-ray with an energy that is characteristic of the electron binding energy difference. Additionally, x-rays with a continuous spectrum of energies are generated when electrons are deflected by the nucleus of high atomic number atoms. These emissions are referred to as bremsstrahlung radiation.

The x-ray differential energy spectrum describes the number of x-rays emitted per steradian from the target focus point as a function of energy. The characteristics of this spectrum are important determinants of contrast and noise in radiation imaging. We consider in this laboratory module a computer model for the estimation of the x-ray spectrum.


Task Src-A1: X-ray spectrum program, 'spect_gen.exe'.

X-ray spectra are computed using a program called spect_gen. The program uses a model by Storm to generate the spectrum as was discussed in the NERS-481 class. Read the documentation for spect_gen in the directory _xspect3.5/_doc. Spectra generated by the spect_gen program are always output in a standard format in a file always named spectra.tmp. Read the documentation on this format in the Spectra.tmp.txt file. Note that the spectra may be stored in this file in different units. Later, we will use routines to convert the units of the spectra within these files.

Task Src-A2: Generate a spectrum from the console window.

Begin by making a folder in your NERS580 course directory with a name such as 'Lab03' for use with this module. Copy the 'link.tcl' script modified in Lab02 to this directory. Then execute the 'link.tcl' script (recall from Lab01 that files with *.tcl extension will be registered to open with the 'wish86.exe'Tcl/Tk interpreter). This will set the $BIN variable to the correct path and open a console window that can be used to interactively enter Tcl/Tk commands.

Then define the input variables and execute spect_gen using the following two commands;

  • set input "74 13 1 120 1 \n"
  • exec $BIN/spect_gen << $input
The first command sets the variable named 'input' to the quoted text string that has parameters read by the x-ray spectrum generation progrom. Note the inclusion of a newline, \n , at the end of the input definition This is needed to properly read the input variables. The second command executes the 'spect_gen.exe' program using the path stored in the $BIN variable (note that the .exe extension can be left off).

When run this way, the 'spect_gen.exe' program will generate a file in the 'Lab03' directory named 'spectra.tmp'. A file with a 'tmp' extension is used because this file is regularly modified by other XSPECT programs. Examine the contents of the spectra.tmp file with a text editor such as Windows Notepad. When opening this file for the first time, you may want to set Notepad as the default program for opening files with an *.tmp extension.

The format of the spectral data in this file makes it easy to plot using gnuplot. For now just run the interactive gnuplot window, use the ChDir button at the top of the window to move to the 'Lab03' directory, and plot the spectrum.

  • plot 'spectra.tmp' w l

Task Src-A3: Simple graphic application to plot a spectrum

Next write a simple script that will create a simple window with entry variables that we will use to define the radiographic technique. In the 'Lab03' directory, create a new text file, name it appropriately (i.e. something like 'Lab03.txt') and open it with a text editor such as Notepad. Enter the following comment lines and script commands and save the file. If you copy and paste from this web page, which is OK, you will probably have an extra space at the end of the second line. Remove this space so that the line ends with the backslash.

  • #!/bin/sh
  • # The next line restarts using tclsh8.0 \
  • exec tclsh $0 ${1+"$@"}
  • # ----------------------------------------
  • #     MODEL: ???
  • #     AUTHOR: ???     DATE: ???
  • # --------run link.tcl commands-----------
  • #
  • source $env(XSPECT_DIR)/_xspect3.5/_tcl/link.tcl
  • #
  • # ------define a simple procedure---------
  • # Note: add plot procedure later.
  • #
  • # ------assemble a simple window-----------
  • #
  • # .....set initial values
  • set kVp 100
  • #
  • # .....widget frame for kVp entry
  • frame .kVin
  • label .kVin.label -text "kVp = "
  • entry .kVin.entry -width 5 -textvariable kVp
  • pack .kVin.label .kVin.entry -side left -padx 05 -pady 10
  • #
  • # .....commands
  • # the .commands and .quit are defined in link.tcl
  • button .commands.plot -text "plot" -command {puts "$kVp"}
  • pack .commands.plot .commands.quit -side left
  • #
  • # .....put the frames in the window
  • pack .kVin .commands -side top

This introduces a simple script syntax to arrange widgets on a window. The first three lines are not needed when using Microsoft Windows, but are needed for Unix/Linux systems. Instead of adding commands to the link.tcl file, the commands in that file are executed using the 'source' command. If XSPECT_DIR has not be set as an environmental variable, it needs to be defined before the link.tcl file is sourced.

set env(XSPECT_DIR) "C:/../../../_Rad-Img"

The 'frame' command is used to create a box called .kVin that holds a label and the entry widget. The button and entry widgets belong to the frame by virtue of their name (i.e. names of .kVin.label and .kVin.entry). The widgets are then packed in the frame.

The link.tcl commands that were sourced have a frame, .commands, that is used for application buttons. At the end, a button to plot the results is added to the .commands frame, which already has a quit button, and both the .commands and .kVin are packed into the final window. The padx and pady options add space around the widgets when they are packed. The side options indicate the direction that the widgets are packed.

Change the extension on this file from *.txt to *.tcl and execute this script. When the button labeled plot is pushed the value in the entry box will be printed on the console window. The command 'console show' should be used for windows systems to insure that a console window is open to print standard output.

This 'Lab03.tcl' file can be further editing by using the Windows 'open with' feature to open the file with Notepad rather that executing it with the Tcl/Tk interpreter. Edit the file by adding a section as shown below that defines a procedure to compute a spectrum and plot it. Note where selective indentation has been used to group statement lines contained between left brackets, '{' , and right brackets, '}'. This makes the script easier to read and debug.

  • # ------define a simple procedure---------
  • # This computes a spectra and plots it
  • proc plotSpectra {kV} {
  •   # allow access to a global variable
  •   global BIN gnuplot ;# both defined in link.tcl
  •   # ------run the fortran program---------
  •   set input "74 12 1 $kV 1.0 \n" ;#see Tclsh.doc
  •   set message [exec $BIN/spect_gen << $input]
  •   # ------set up a plot and wait---------
  •   set fileID [open Lab.gpl w] ;#open a file for writing
  •   puts $fileID { ;#send some gnuplot commands
  •     set xlabel "keV"
  •     plot "spectra.tmp" w l
  •     pause -1 ;#pause waits for a return
  •   }
  •   close $fileID ;#close the file
  •   exec $gnuplot Lab.gpl ;#gnuplot defined in link.tcl
  • }

These commands should be inserted in the script where a comment was made to insert a simple procedure. Secondly, the command option of the plot button widget should be changed from {puts "$kVp"} to {plotSpectra $kVp}.

The procedure is defined using the 'proc' command. The indented lines within { .. } are the commands to be executed. They are executed with local scope and so the BIN variable must be declared as a global variable. Note that comment lines begin with a #. Lines can be ended with a ; followed by a # to add comments at the end of a line.

In the lines concerning plotting, a temporary file with plot instructions is created and those instructions are used when gnuplot is executed. See the gnuplot help file for details on 'pause -1'. It allows us to run this in a script and hold the plot on the screen until we enter a carriage return on the keyboard. Later we will see another construction to run gnuplot and change the command instruction from the script. Note that the gnuplot program was defined as a variable in link.tcl so that $gnuplot is valid whether you are using Windows or Linux.

Otherwise, this script uses the constructions described in 'Tcl4xspect.pdf' to execute the fortran program.

Finally, add to this script application an additional input variable to enter the target atomic number, Z. Use the entry widget for kV to add an additional entry widget for Z. At the end, be sure to pack this new entry widget. The procedure should be modified to accept this arguement, {kV Z}, and the button command modified to include Z as an argument, i.e. -command {plotSpectra $kVp $Z}. Additionally, the input arguements for the spect_gen program need to be modified to set the target Z to this variable value (i.e. $Z 12 1 $kV 1.0).

Task Src-A4: Comparison of of spectra from tungsten and molybdenum targets.

Using this application, produce spectra for tungsten targets (Z=74) with a kVp of 60 and 100. Note the difference in the characteristic x-ray peaks for the different kV values. To produce graphic PNG files, the plot procedure can be modified by adding the following lines after the 'pause -1' line written to the gpl file.

  •     ....
  •     set terminal png size 800,500 font "Times New Roman, 14"
  •     set output "L03-A4_Z_kV.png"
  •     replot
  •     exit
  •     ....
After generating a plot, the filename should be edited using the Z and kV values to save the result.

Note: Alternatively, the export icon at the upper left of the gnuplot terminal window can be used to save the plot as a PNG file.

Also produce spectra for molybdenum targets (Z=42) with kVp of 26 and 40. You may want to add the target Z as a variable to the plot procedure and specify that as an input to spect_gen similar to how kV is passed to the procedure. Then an additional entry frame can be added to set the target Z to either 74 for tungsten or to 42 for molybdenum. Otherwise, edit the script to set the input parameter to 74 or 42. For both note the emissions at very low energies and the discontinuities caused by self absorbtion around the L edge of the target material.

Note: The spect_gen program only supports Z values of 74 (tungsten), 45 (Rhodium), and 42 (molybdenum). A better widget for specifying Z would therefore be a set of three radiobuttons. The Tk demo application illustrates this widget and shows the script. The following could be added to the script just after the section where the .kVin frame is made and then .zin added to the pack command at the end;
  • # .....widget frame for Z selection
  • set tarZ 74 ;# sets which radiobutton will be activated
  • frame .zin
  • foreach i {42 45 74} {
  •     radiobutton .zin.b$i -text "Z = $i" -value $i -variable Z
  •     pack .zin.b$i
  • }
  • ....
  • ....
  • # .....put the frames in the window
  • pack .kVin .zin .commands -side top

Lab03 Results:

At the end of this module, turn in a copy of script file written as a part of this module and files for the 4 x-ray spectra plots.

  • L03_Src-A4.tcl : tcl script for this lab module.
  • L03-A4_74_60.png : Tungsten spectra for 60 kVp.
  • L03-A4_74_100.png : Tungsten spectra for 100 kVp.
  • L03-A4_42_26.png : Molybdenum spectra for 26 kVp.
  • L03-A4_42_40.png : Molybdenum spectra for 40 kVp.

At the beginning of the script file, add a comment line with your name and the date. This will be taken as an indication that you are the sole author of the script.

L03 Reference script: L03_Src-A4.tcl

Next

The next module is 04-Source-B.