MCA Utility Routines

This page was created by the IDL library routine mk_html_help. For more information on this routine, refer to the IDL Online Help Navigator or type:

     ? mk_html_help

at the IDL command line prompt.

Last modified: Thu Apr 18 11:52:38 2013.


List of Routines


Routine Descriptions

ATOMIC_NUMBER

[Next Routine] [List of Routines]
 NAME:
       ATOMIC_NUMBER

 PURPOSE:
       This function returns the atomic number of an element 

 CATEGORY:
       IDL data analysis

 CALLING SEQUENCE:
       Result = ATOMIC_NUMBER(Symbol)

 INPUTS:
       Symbol: The atomic symbol of the element whose atomic number is being
               requested.  This is a 1 or 2 character string, e.g. 'H', 'Si',
               etc.  It is case insensitive and leading or trailing blanks
               are ignorred.

 OUTPUTS:
       This function returns the atomic number of the input element.  If an
       Symbol is an invalid atomic symbol then the function returns 0.

 COMMON BLOCKS:
       ATOMIC_SYMBOL_COMMON:  Holds an array of atomic_symbols.  Initialized
       the first time this function or function ATOMIC_SYMBOL is called.

 EXAMPLE:
       IDL> print, atomic_number('ag')
       IDL>      47

 MODIFICATION HISTORY:
       Written by:     Mark Rivers, October 23, 1998

(See atomic_number.pro)


ATOMIC_SYMBOL

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       ATOMIC_SYMBOL

 PURPOSE:
       This function returns the atomic symbol of an element 

 CATEGORY:
       IDL data analysis

 CALLING SEQUENCE:
       Result = ATOMIC_SYMBOL(Z)

 INPUTS:
       Z:  The atomic number of the element whose atomic symbol is being
           requested.  

 OUTPUTS:
       This function returns the atomic symbol of the input element as a
       string.  If Z is an invalid atomic number then the function returns 
       a null string.

 COMMON BLOCKS:
       ATOMIC_SYMBOL_COMMON:  Holds an array of atomic_symbols.  Initialized
       the first time this function or function ATOMIC_NUMBER is called.

 EXAMPLE:
       IDL> print, atomic_symbol(47)
       IDL> Ag

 MODIFICATION HISTORY:
       Written by:     Mark Rivers, October 23, 1998

(See atomic_symbol.pro)


EXTRACT_SPECTRA_SCANS

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
	EXTRACT_SPECTRA_SCANS

 PURPOSE:
	This function extracts the net counts in regions of interest from spectra scan
   files.

 CATEGORY:
	IDL MCA utilities

 CALLING SEQUENCE:

	Result = EXTRACT_SPECTRA_SCANS(Roi_template_file, Nx, Ny, First_file, Rois, $
                                  Background_width=Background_width)

 INPUTS:
	Roi_template_file:
 	 	The name of an MCA file that contains the ROI definitions to be
       used when extracting the data. This is typically one of the spectra files that
       has been mofified using MCA_DISPLAY to define the ROIs to be extracted, and
       then perhaps saved under a different name.
  Nx:
       The number of points in the fast scan direction.
  Ny:
       The number of points in the slow scan direction.  May be 1 for a 1-D scan.
  First_file:
       The full name of the first file in the scan.  Subsequent files are assumed to be
       constructed from this file name with "increment_filename".

 KEYWORD PARAMETERS:
	BACKGROUND_WIDTH:
       The number of points of background on each side of the ROIs to be used in
       calculating	the net counts in each ROI.

 OUTPUTS:
	This function returns a floating point array [Nx, Ny, Nrois] containing the net
   counts in each ROI at each spectrum in the scan. Nrois is the  number of ROIs
   defined in the Roi_template_file.

 OPTIONAL OUTPUTS:
   Rois:
       An array of structures of type MCA_ROI[Nrois].  These structures can be used,
       for example to get the labels to put on plots of the

 EXAMPLE:
   IDL> data = extract_spectra_scans('roi_template.dat', 33, 1, '501.49', back=1, rois)
   IDL> plot, data[*,0,4], psym=-1, title=rois[4].label

 MODIFICATION HISTORY:
 	Written by:	Mark Rivers, June 29, 2001

(See extract_spectra_scans.pro)


FIT_BACKGROUND

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       FIT_BACKGROUND

 PURPOSE:
       This function fits a background to an MCA spectrum.
       The background is fitted using an enhanced version of the algorithm
       published by Kajfosz, J. and Kwiatek, W .M. (1987)  "Non-polynomial
       approximation of background in x-ray spectra." Nucl. Instrum. Methods
       B22, 78-81.

 CATEGORY:
       Spectra data fitting

 CALLING SEQUENCE:
       Result = FIT_BACKGROUND(Data, Slope)

 INPUTS:
       Data:   A 1-D array which contains the input spectrum to be fitted.

       Slope:  The energy calibration slope, in user units/channel.  This
               value is needed because the widths are specified in user units.

 KEYWORD PARAMETERS:
       TOP_WIDTH:
           Specifies the width of the polynomials which are concave upward.
           The top_width is the full width in energy units at which the
           magnitude of the polynomial is 100 counts. The default is 0, which
           means that concave upward polynomials are not used.

       BOTTOM_WIDTH:
           Specifies the width of the polynomials which are concave downward.
           The bottom_width is the full width in energy units at which the
           magnitude of the polynomial is 100 counts. The default is 4.

       EXPONENT:
           Specifies the power of polynomial which is used. The power must be
           an integer. The default is 2, i.e. parabolas. Higher exponents,
           for example EXPONENT=4, results in polynomials with flatter tops
           and steeper sides, which can better fit spectra with steeply
           sloping backgrounds.

       TANGENT
           Specifies that the polynomials are to be tangent to the slope of the
           spectrum. The default is vertical polynomials. This option works
           best on steeply sloping spectra. It has trouble in spectra with
           big peaks because the polynomials are very tilted up inside the
           peaks.

       COMPRESS:
           Compression factor to apply before fitting the background.
           Default=4, which means, for example, that a 2048 channel spectrum
           will be rebinned to 512 channels before fitting.
           The compression is done on a temporary copy of the input spectrum,
           so the input spectrum itself is unchanged.
           The algorithm works best if the spectrum is compressed before it
           is fitted. There are two reasons for this. First, the background
           is constrained to never be larger than the data itself. If the
           spectrum has negative noise spikes they will cause the fit to be
           too low. Compression will smooth out such noise spikes.
           Second, the algorithm requires about 3*N^2 operations, so the time
           required grows rapidly with the size of the input spectrum. On a
           200 MHz Pentium it takes about 3 seconds to fit a 2048 channel
           spectrum with COMPRESS=1 (no compression), but only 0.2 seconds
           with COMPRESS=4 (the default).

 OUTPUTS:
       This function returns a 1-D array which contains the background 
       fitted to the input data.

 PROCEDURE:
       1) At each channel "i" an n'th degree polynomial which is concave up
       is fitted. Its equation is

                                     n
                        (e(i) - e(j))
       f(j,i) = y(i) + --------------
                                  n
                         top_width

       where f(j,i) is the fitted counts in channel j for the polynomial
       centered in channel i. y(i) is the input counts in channel "i", e(i) is
       the energy of channel i, e(j) is the energy of channel j, and
       "top_width" and "n" are user-specified parameters. The background count
       in channel "j", b(j) is defined as

       b(j) = min ((f(j,i), y(j))
               i

       b(j) is thus the smallest fitted polynomial in channel j, or the raw
       data, whichever is smaller.

       2) After the concave up polynomials have been fitted, a series of
       concave down polynomials are constructed. At each channel "i" an n'th
       degree polynomial which is concave up is fitted. The polynomial is slid
       up from below until it "just touches" some channel of the spectrum. Call
       this channel "i". The maximum height of the polynomial is thus

                                                n
                                   (e(i) - e(j))
       height(j) = max ( b(j) +  --------------  )
                    i                          n
                                   bottom_width

       where bottom_width is a user_specified parameter.

       3) Once the value of height(i) is known the polynomial is fitted. The
       background counts in each channel are then determined from:

                                                n
                                   (e(i) - e(j))
       bgd(j) = max ( height(i) + --------------
                 i                             n
                                   bottom_width

       bgd(j) is thus the maximum counts for any of the concave down
       polynomials passing though channel j.

       Before the concave-down polynomials are fitted the spectrum at each
       channel it is possible to subtract out a straight line which is
       tangent to the spectrum at that channel. Use the /TANGENT qualifier to
       do this. This is equivalent to fitting a "tilted" polynomial whose
       apex is tangent to the spectrum at that channel. By fitting
       polynomials which are tangent rather than vertical the background fit
       is much improved on spectra with steep slopes.

 EXAMPLE:
       mca = obj_new('MCA')
       mca->read_file, 'my_file.dat'
       data = mca->get_data()
       cal = mca->get_calibration()
       slope = cal.slope
       back = fit_background(data, slope, bottom=6, exponent=4)
       plot, data
       oplot, back

       Note:  This example is provided for reference.  In practice it is
              simpler to use the MCA object method MCA::FIT_BACKGROUND.
              because is handles much of the tedious bookkeeping the above 
              example.  However, it is important to recognize that
              FIT_BACKGROUND is independent of the MCA class, and
              can be used without the MCA class library

 MODIFICATION HISTORY:
       Written by:     Mark Rivers, October 19, 1998

(See fit_background.pro)


FIT_PEAKS

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       FIT_PEAKS

 PURPOSE:
       This function fits spectra with a set of Gaussian peaks.

 CATEGORY:
       Spectral data fitting

 CALLING SEQUENCE:
       Result = FIT_PEAKS(Fit, Data, Peaks)

 INPUTS:
       Fit:    
           A structure of type {MCA_FIT}.  This structure is used to control 
           the global fitting parameters and options. The exact definition of 
           this structure is subject to change.  However, the following 
           "input" fields will always exist and can be used to control the fit
           process.  Function MCA::FIT_INITIALIZE() can be used to create
           this structure with reasonable default values for each field.
           Further information on many of these fields can be found in the
           procedure description below.
               .initial_energy_offset ; The initial energy calibration offset. 
                                      ; FIT_INITIALIZE sets this to the 
                                      ; calibration offset for the MCA object
               .initial_energy_slope  ; The initial energy calibration slope. 
                                      ; FIT_INITIALIZE sets this to the 
                                      ; calibration slope for the MCA object
               .energy_flag           ; Energy flag
                                      ; 0 = Fix energy calibration coeffs
                                      ; 1 = Optimize energy calibration coeffs
                                      ; FIT_INITIALIZE sets this to 1
               .initial_fwhm_offset   ; The initial FWHM calibration offset
                                      ; FIT_INITIALIZE sets this to 150 eV
               .initial_fwhm_slope    ; The initial FWHM calibration slope
                                      ; FIT_INITIALIZE sets this to 0.
               .fwhm_flag             ; FWHM flag
                                      ;   0 = Fix FWHM coefficients
                                      ;   1 = Optimize FWHM coefficients
                                      ; FIT_INITIALIZE sets this to 1
               .chi_exp               ; Exponent of chi
                                      ; FIT_INITIALIZE sets this to 0.
               .max_eval              ; Maximum # function evaluations
                                      ; FIT_INITIALIZE sets this to 0 which
                                      ; does not limit the number of function 
                                      ; evaluations
               .max_iter              ; Maximum number of iterations
                                      ; FIT_INITIALIZE sets this to 20
               .tolerance             ; Convergence tolerance. The fitting 
                                      ; process will stop when the value of 
                                      ; chi^2 changes by a relative amount
                                      ; less than tolerance on two successive 
                                      ; iterations. 
                                      ; FIT_INITIALIZE sets this to 1.e-4

       Data:   
           The input spectrum to be fit.  Note that this spectrum typically 
           will have previously had the background fitted using 
           FIT_BACKGROUND and this background 
           subtracted from Data before passing it to this function.

       Peaks:  
           An array of structures of type {MCA_PEAKS} which contains the
           parameters for each peak to be fitted. The exact definition of 
           this structure is subject to change.  However, the following 
           "input" fields will always exist and can be used to control the fit
           process.  Function READ_PEAKS can be used 
           to read a disk file into this structure.
           Further information on many of these fields can be found in the
           procedure description below.
               .label          ; A string describing the peak
               .energy_flag    ; Flag for fitting energy of this peak
                                   ; 0 = Fix energy 
                                   ; 1 = Optimize energy
               .fwhm_flag      ; Flag for fitting FWHM of this peak
                                   ; 0 = Fix FWHM to global curve
                                   ; 1 = Optimize FWHM
                                   ; 2 = Fix FWHM to input value
               .ampl_factor    ; Flag for fitting amplitude of this peak
                                   ; 0.0  = Optimize amplitude of this peak
                                   ; >0.0 = Fix amplitude to this value 
                                   ;        relative to amplitude of 
                                   ;        previous unconstrained peak
                                   ; -1.0 = Fix amplitude at 0.0
               .initial_energy ; Initial value for peak energy
               .initial_fwhm   ; Initial value for FWHM.  This can be zero if
                                   ;   .fwhm_flag is 0
               .initial_ampl   ; Initial value of peak amplitude.  
                                   ; If .ampl_factor is 0.0 then this function
                                   ; will automaticailly determine a value for
                                   ; .initial_ampl

 OUTPUTS:
       This function returns the fitted spectrum as the function return value.
       It also returns output information in the Fit and Peaks parameters.

       Fit:    
           A structure of type {MCA_FIT} which contains the global fit 
           parameters. This is the same structure which is also an input 
           parameter.  The exact definition of this structure is subject to 
           change.  However, the following "output" fields will always exist 
           and contain the results of the fit. Further information on many 
           of these fields can be found in the procedure description below.
               .energy_offset  ; Fitted energy calibration offset
               .energy_slope   ; Fitted energy calibration slope
               .fwhm_offset    ; Fitted FWHM offset
               .fwhm_slope     ; FWHM slope
               .n_eval         ; Actual number of function evalutions
               .n_iter         ; Actual number of iterations
               .chisqr         ; Chi-squared on output

       Peaks:  
           An array of structures of type {MCA_PEAKS} which contains the
           parameters for each peak to be fitted. This is the same array which
           is also an input parameter.  The exact definition of 
           this structure is subject to change.  However, the following 
           "output" fields will always exist and contain the results of the
           fit. Further information on many of these fields can be found in the
           procedure description below.
               .energy         ; The fitted peak energy
               .fwhm           ; The fitted peak FWHM
               .ampl           ; The fitted peak amplitude
               .area           ; The fitted area of the peak

 COMMON BLOCKS:
       FIT_PEAKS_COMMON:  This common block is used to communicate between the
               various routines in this file.  It is required because of the
               way MPFITFUN calls the procedure to evaluate the residuals.

 RESTRICTIONS:
       This function is presently limited to fitting Gaussian peaks.  It may
       be extended in the future to fit other peak shapes.

 PROCEDURE:
       In general a Gaussian peak has 3 adjustable parameters: position 
       (or energy), sigma (or FWHM), and amplitude (or area).  For many
       applications, however, not all of these parameters should be
       adjustable during the fit.  For example, in XRF analysis the energy of
       the peaks is known, and should not be optimized.  However, the overall
       energy calibration coefficients for the entire spectrum, which relate
       channel number to energy, might well be optimized during the fit.
       Similarly, the FWHM of XRF peaks are not independent, but rather
       typically follow a predictable detector response function:
           FWHM = A + B*sqrt(energy)
       Finally, even the amplitude of an XRF peak might not be a free
       parameter, since, for example one might want to constrain the K-beta 
       peak to be a fixed fraction of the K-alpha.  Such constraints allow 
       one to fit overlapping K-alpha/K-beta peaks with much better accuracy.

       This procedure is designed to be very flexible in terms of which
       parameters are fixed and which ones are optimized.  The constraints are
       communicated via the Fit and Peaks structures.

       The energy of each channel is assumed to obey the relation: 
           energy = energy_offset + (channel * energy_slope)

       These parameters control the fit for peaks whose energy is fixed, 
       rather than being a fit parameter.
       If Fit.energy_flag is 1 then these energy calibration coefficients
       will be optimized during the fitting process. If it is 0 then these
       energy calibration coefficients are assumed to be correct and are not 
       optimized.  Not optimizing the energy calibration coefficients can 
       both speed up the fitting process and lead to more stable results when 
       fitting small peaks.  This function does a sanity check and will not
       optimize these energy calibration coefficients unless at least 2 peaks
       have their .energy_flag field set to 0, so that they use these global
       calibration coefficients.

       The FWHM of the peaks is assumed to obey the relation:
           fwhm = fwhm_offset + (fwhm_slope * sqrt(energy))
       These parameters control the fit for peaks whose FWHM is neither fixed 
       nor a fit parameter.
       If Fit.fwhm_flag is 1 then these coefficients will be optimized during
       the fitting process. If it is 0 then the specified coefficients are 
       assumed to be correct and are not optimized. Not optimizing the FWHM
       coeffcients can both speed up the fitting process and lead to more 
       stable results when fitting very small peaks. This function does a 
       sanity check and will not optimize these FWHM calibration coefficients 
       unless at least 2 peaks have their .fwhm_flag field set to 0, so that 
       they use these global calibration coefficients.

       This function also optimizes the following parameters:
           - The amplitudes of all peaks whose .ampl_factor field is 0
           - The energies of all peaks whose .energy_flag field is 1
           - The FWHM of all peaks whose .fwhm_flag field is 1

       The parameter which is the minimized during the fitting process is 
       chi^2, defined as:
                                                    2
          2            y_obs[i]    -     y_pred[i]
       chi  = sum (  ---------------------------- )
               i              sigma[i]

       where y_obs[i] is the observed counts in channel i, y_pred is the
       predicted counts in channel i, and sigma[i] is the standard deviation
       of y_obs[i].

       This function assumes that:

       sigma[i] = y_obs[i] ** chi_exponent

       e.g. that the standard deviation in each channel is equal to the counts
       in the channel to some power. For photon counting spectra where Poisson
       statistics apply chi_exponent=0.5, and this is the default. Setting
       chi_exponent=0. will set all of the sigma[i] values to 1., and the fit
       would then be minimizing the sum of the squares of the residuals. This
       should tend to result in a better fit for the large peaks in a spectrum
       and a poorer fit for the smaller peaks. Setting chi_exponent=1.0 will
       result in a minimization of the sum of the squares of the relative error
       in each channel. This should tend to weight the fit more strongly toward
       the small peaks.

       If .ampl_factor for a peak is 0., then the amplitude of the peak is a 
       fit parameter. If the amplitude_factor is non-zero then the amplitude 
       of this peak is not a fit parameter, but rather is constrained to
       be equal to the amplitude of the last previous peak in the array which 
       had an amplitude factor of zero, times the amplitude_factor. This can 
       be used, for instance, fit K-alpha and K-beta x-ray lines when the 
       alpha/beta ratio is known, and one wants to add this known constraint 
       to the fitting process.
       For example:
           peaks = replicate({mca_peak}, 3)
           ; Fe Ka is the "reference" peak
           peaks[0].initial_energy=6.40 & peaks[0].ampl_factor=0.0 
           ; Si-Ka escape peak is 3% of Fe Ka at 4.66 keV
           peaks[1].initial_energy=4.66 & peaks[1].ampl_factor=0.03
           ; Fe-Kb is 23% of Fe Ka
           peaks[2].initial_energy=7.06 & peaks[2].ampl_factor=0.23
       In this example the amplitude of the Fe-Ka peak will be fitted, but the
       amplitudes of the escape peak and the Fe-Kb peak are constrained to
       be fixed fractions of the Fe-Ka peak.  The reference peak is always the
       closest preceding peak in the array for which ampl_factor is 0.

 EXAMPLE:
       mca = obj_new('mca')
       mca->read_file, 'myspect.dat'
       data = mca->get_data()
       cal = mca->get_calibration()
       slope = cal.slope
       back = fit_background(data, slope)
       peaks = read_peaks('mypeaks.pks')
       fit = fit_initialize()
       result = fit_peaks(fit, data-back, peaks)
       plot, data
       oplot, back
       oplot, result+back

       Note:  This example is provided for reference.  In practice it is
              simpler to use the MCA object methods MCA::FIT_BACKGROUND and
              MCA::FIT_PEAKS, because they handle much of the tedious
              bookkeeping the above example.  However, it is important to
              recognize that FIT_PEAKS is independent of the MCA class, and
              can be used without the MCA class library

 MODIFICATION HISTORY:
       Written by:     Mark Rivers, October 21, 1998.  This is the latest
                       re-write of a routine which has a long history, begun 
                       at X-26 at the NSLS.  The original version was written 
                       in a program called SPCALC, and was then ported to IDL.
                       These early versions used IMSL for the least-squares
                       routine.  The port to CURVEFIT, so that no external
                       software package is required, was done in 1998.
       Mark Rivers, Nov. 9, 1998.  Added sanity check for nchans
       Mark Rivers, Nov. 12, 1998.  Significant re-write to use MPFITFUN
                                    in place of CURVEFIT
       Mark Rivers, Feb. 1, 2001.  Changed amplitude ratio calculation so that the
                                   AREA of the two peaks has the specified ratio,
                                   rather than the AMPLITUDE.  This is done by
                                   adjusting the constrained ratio by the relative
                                   peak widths.

(See fit_peaks.pro)


LOOKUP_GAMMA_LINE

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       LOOKUP_GAMMA_LINE

 PURPOSE:
       This function returns the energy in keV for a particular gamma
       emmission line.

 CATEGORY:
       X-ray analysis routines

 CALLING SEQUENCE:
       Result = LOOKUP_GAMMA_LINE(Gamma_Line)

 INPUTS:
       Gamma_Line: A string of the form 'Isotope Line', where Isotope is a
       the symbol for a radioactive isotope, and Line is an index of the form
       g1, g2, ... gn.
       Both Isotope and Line are case insensitive.  There must be a space
       between Isotope and Line.

       Examples of Gamma_Line:
           'Cd109 g1'  - 88 keV line of Cd-109
           'co57 g2'   - 122 keV line of Co-57

 OUTPUTS:
       This function returns the gamma energy of the specified line.
       If the input is invalid, e.g. non-existent isotope or line, then the
       function returns 0.

 RESTRICTIONS:
       This function only knows about a few isotopes at present.  It is
       intended for use with common radioactive check sources.  It is easy to
       add additional isotopes and gamma lines to this function as needed.
       The current library is:
           'CO57 G1' = 14.413
           'CO57 G2' = 122.0614
           'CO57 G3' = 136.4743
           'CD109 G1'= 88.04

 EXAMPLE:
       energy = LOOKUP_GAMMA_LINE('Co57 g1')  ; Look up 14 keV line of Co-57

 MODIFICATION HISTORY:
       Written by:     Mark Rivers.  October 10, 1998

(See lookup_gamma_line.pro)


LOOKUP_JCPDS_LINE

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       LOOKUP_JCPDS_LINE

 PURPOSE:
       This function returns the d-spacing in Angstroms for a particular
       lattice plane.

 CATEGORY:
       X-ray analysis routines

 CALLING SEQUENCE:
       Result = LOOKUP_JCPDS_LINE(Diffraction_plane)

 INPUTS:
       Diffaction_plane: A string of the form 'Compound HKL', where Compound
       is the name of a material (e.g. 'gold', and HKL is the diffraction
       plane (e.g. 220).
       There must be a space between Compound and HKL.

       Examples of Diffraction_plane:
           'gold 111' - Gold 111 plane
           'si 220'   - Silicon 220 plane

 KEYWORD PARAMETERS:
       PATH:
           The path in which to look for the file 'Compound.jcpds'.  The
           default is to search in the directory pointed to by the
           environment variable JCPDS_PATH.

       PRESSURE:
           The pressure at which to compute the d-spacing.  Not yet
           implemented, zero pressure d-spacing is always returned.

       TEMPERATURE:
           The temperature at which to compute the d-spacing.  Not yet
           implemented.  Room-temperature d-spacing is always returned.

 OUTPUTS:
       This function returns the d-spacing of the specified lattice plane.
       If the input is invalid, e.g. non-existent compound or plane, then the
       function returns 0.

 RESTRICTIONS:
       This function attempts to locate the file 'Compound.jcpds', where
       'Compound' is the name of the material specified in the input parameter
       'Diffraction_plane'.  For example:
           d = LOOKUP_JCPDS_LINE('gold 220')
       will look for the file gold.jcpds.  It will either look in the file
       specified in the PATH keyword parameter to this function, or in the
       the directory pointed to by the environtment variable JCPDS_PATH
       if the PATH keyword is not specified.  Note that the filename will be
       case sensitive on Unix systems, but not on Windows or VMS systems.

       This function is currently only able to handle HKL values from 0-9.
       The parser will need to be improved to handle 2-digit values of H,
       K or L.

 PROCEDURE:
       This function calls READ_JCPDS and searches for the specified HKL plane
       and returns its d-spacing;

 EXAMPLE:
       d = LOOKUP_JCPDS_LINE('gold 111')  ; Look up gold 111 line
       d = LOOKUP_JCPDS_LINE('quartz 220') ; Look up the quartz 220 line

 MODIFICATION HISTORY:
       Written by:     Mark Rivers.  October 10, 1998

(See lookup_jcpds_line.pro)


LOOKUP_XRF_LINE

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       LOOKUP_XRF_LINE

 PURPOSE:
       This function returns the energy in keV for a particular x-ray
       fluorescence line.

 CATEGORY:
       X-ray analysis routines

 CALLING SEQUENCE:
       Result = LOOKUP_XRF_LINE(XRF_Line)

 INPUTS:
       XRF_LINE: A string of the form 'Element Line', where Element is an
       atomic symbol, and Line is an acronym for a fluorescence line.
       Both Element and Line are case insensitive.  There must be a space
       between Element and Line.
       The valid lines are
           ka  - K-alpha (weighted average of ka1 and ka2)
           ka1 - K-alpha 1
           ka2 - K-alpha 2
           kb  - K-beta (weighted average of kb1 and kb2)
           kb1 - K-beta 1
           kb2 - K-beta 2
           la1 - L-alpha 1
           lb1 - L-beta 1
           lb2 - L-beta 2
           lg1 - L-gamma 1
           lg2 - L-gamma 2
           lg3 - L-gamma 3
           lg4 - L-gamma 4
           ll  - L-eta

       Examples of XRF_Line:
           'Fe Ka'  - Fe k-alpha
           'sr kb2' - Sr K-beta 2
           'pb lg2' - Pb L-gamma 2

 OUTPUTS:
       This function returns the fluoresence energy of the specified line.
       If the input is invalid, e.g. non-existent element or line, then the
       function returns 0.

 COMMON BLOCKS:
       LOOKUP_XRF_COM: This common block is used to store the element line
       table after reading it in.

 RESTRICTIONS:
       This function uses the environment variable XRF_PEAK_LIBRARY to locate
       the file containing the database of XRF lines.  This environment
       variable must be defined to use this function.  On Unix systems
       this is typically done with a command like
       setenv XRF_PEAK_LIBRARY
                   /usr/local/idl_user/epics/mca/xrf_peak_library.txt
       On Windows NT systems this is typically done with the
               Settings/Control Panel/System/Environment control.

 PROCEDURE:
       The first time this function is called it reads in the table from the
       file pointed to by the environment variable.  On all calls it looks
       up the element using function ATOMIC_NUMBER and searches for the
       specified line in the table.  It returns 0. if the element or line
       are invalid.

 EXAMPLE:
       energy = LOOKUP_XRF_LINE('Fe Ka')  ; Look up iron k-alpha line
       energy = LOOKUP_XRF_LINE('Pb lb1') ; Look up lead l-beta 1 line

 MODIFICATION HISTORY:
       Written by:     Mark Rivers.  October 10, 1998

(See lookup_xrf_line.pro)


READ_PEAKS

[Previous Routine] [Next Routine] [List of Routines]
 NAME:
       READ_PEAKS

 PURPOSE:
       This function reads a disk file into an array of structures of type 
       {MCA_PEAK}.  This routine is typically called before calling 
       FIT_PEAKS or 
       MCA::FIT_PEAKS.
       The routine also returns a structure of type {MCA_BACKGROUND}.  This structure
       may or may not actually be defined in the file (older peaks files lacked it),
       but a reasonable default value will always be returned.

 CATEGORY:
       MCA data analysis

 CALLING SEQUENCE:
       Result = READ_PEAKS(File, Background)

 INPUTS:
       File:  
           The name of a disk file containing the peak definitions.

 OUTPUTS:
       This function returns an array of structures of type {MCA_PEAK}.

       Background:
           A structure of type {MCA_BACKGROUND} containing the background fitting
           parameters.

 PROCEDURE:
       The input file is used to construct an array of structures of type
       {MCA_PEAK}, which contains the parameters for peaks to be fitted. 
       The exact definition of this structure is subject to change.  However, 
       the following fields will always exist and are the fields which are set
       by this function. 
       Further information on many of these fields can be found in the
       documentation for FIT_PEAKS.
               .label          ; A string describing the peak
               .energy_flag    ; Flag for fitting energy of this peak
                                   ; 0 = Fix energy 
                                   ; 1 = Optimize energy
               .fwhm_flag      ; Flag for fitting FWHM of this peak
                                   ; 0 = Fix FWHM to global curve
                                   ; 1 = Optimize FWHM
                                   ; 2 = Fix FWHM to input value
               .ampl_factor    ; Flag for fitting amplitude of this peak
                                   ; 0.0  = Optimize amplitude of this peak
                                   ; >0.0 = Fix amplitude to this value 
                                   ;        relative to amplitude of 
                                   ;        previous unconstrained peak
                                   ; -1.0 = Fix amplitude at 0.0
               .initial_energy ; Initial value for peak energy
               .initial_fwhm   ; Initial value for FWHM.  This can be zero if
                                   ;   .fwhm_flag is 0
               .initial_ampl   ; Initial value of peak amplitude.  
                                   ; If .ampl_factor is 0.0 then this function
                                   ; will automaticailly determine a value for
                                   ; .initial_ampl

       The format of the disk file is as follows:
           - Lines containing the parameters for the background (fields in
             {MCA_BACKGROUND} structure) have the following format:
                   Background_exponent, 4
                   Background_top_width, 0
                   Background_bottom_width, 4
                   Background_tangent, 0
                   Background_compress, 8

           - There is one line in the file for each peak

           - Each line consists of the following fields, separated by commas:
               energy, energy_flag, fwhm, fwhm_flag, ampl_factor, label

           - All fields except the first, "energy", are optional and default
             values of 0 or blank.
           - Field descriptions
               - energy:       This field can either be an energy in keV or a
                               string which can be parsed by 
                                LOOKUP_XRF_LINE 
                               or LOOKUP_GAMMA_LINE.
                               The energy in keV of the peak is put in the 
                               .energy value for the peak.
               - energy_flag:  The .energy_flag value for the peak. 0 or 1.
               - fwhm:         The .initial_fwhm value for the peak.  This 
                               can be 0 if the .fwhm_flag is 0.
               - fwhm_flag:    The .fwhm_flag value for the peak. 0, 1 or 2.
               - ampl_factor:  The .ampl_factor for the peak.
               - label:        The .label string for the peak.  If the energy
                               field is a string, and the label field is blank
                               then the energy string will be put in .label.

       The following is an example peak file. Most peaks in this example use 
       the default (0) values for the energy_flag, fwhm_flag and 
       amplitude_factor fields.

           4.660,,,,,Fe escape  ! This peak uses a numeric energy value
           Fe Ka                ! This peak uses a string energy value
           Fe Kb,,,,.5          ! Fe Kb is constrained to be 0.5 of Fe Ka
           Ni Ka                ! These peaks energies are found with
           Ni Kb                !  LOOKUP_XRF_LINE
           Co57 G1              ! These peak energies are found with
           Cd109 G2             !  LOOKUP_GAMMA_LINE
           15.9,1,.3,1,,Diffraction ! Fit both the energy and fwhm of this peak
           17.443,1,,,,Unknown  ! Fit the energy, but not fwhm, of this peak



 EXAMPLE:
       Peaks = READ_PEAKS('mypeaks.pks')

 MODIFICATION HISTORY:
       Written by:     Mark Rivers, October 23, 1998
       Feb 17, 2000:   Matt Newville.  Separated parse_peaks function to
                       separate file -- I wanted direct access to that function
       Jan 9, 2001:    Mark Rivers.  Added Background output parameter since files
                       can now contain background fitting parameters as well as
                       peaks.

(See read_peaks.pro)


WRITE_PEAKS

[Previous Routine] [List of Routines]
 NAME:
       WRITE_PEAKS

 PURPOSE:
       This procedure writes an array of structures of type
       {MCA_PEAK} to a disk file.  If the Background parameter is present is also
       writes the background structure to the file.

 CATEGORY:
       MCA data analysis

 CALLING SEQUENCE:
       WRITE_PEAKS, Peaks, File, Background

 INPUTS:
       Peaks:
           An array of structures of type {MCA_PEAK}.
       File:
           The name of a disk file containing the peak definitions.

 OPTIONAL INPUTS:
       Background:
           A structure of type {MCA_BACKGROUND}.

 EXAMPLE:
       Peaks = READ_PEAKS('my_peaks.pks')
       Peaks[1].initial_energy = 6.4
       WRITE_PEAKS, Peaks, 'mypeaks.pks'

 MODIFICATION HISTORY:
       Written by:     Mark Rivers, November 1, 1998
       9-Jan-2001, MLR, Added background parameter for saving background structure

(See write_peaks.pro)