|
Methods defined here:
- __init__(self, record_name, environment_file=None)
- Creates a new epicsMca.
Inputs:
record_Name:
The name of the EPICS MCA record for the MCA object being created, without
a trailing period or field name.
Keywords:
environment_file:
This keyword can be used to specify the name of a file which
contains the names of EPICS process variables which should be saved
in the header of files written with Mca.write_file().
If this keyword is not specified then this function will attempt the
following:
- If the system environment variable MCA_ENVIRONMENT is set then
it will attempt to open that file
- If this fails, it will attempt to open a file called
'catch1d.env' in the current directory.
This is done to be compatible with the data catcher program.
This is an ASCII file with each line containing a process variable
name, followed by a space and a description field.
Example:
>>> from epicsMca import *
>>> mca = epicsMca('13IDC:mca1')
>>> print mca.data
- add_roi(self, roi, energy=0)
- Adds a new ROI to the epicsMca.
Inputs:
roi:
An McaROI object.
Keywords:
energy:
Set this flag if the .left and .right fields in the mcaROI are in energy units.
By default these fields are assumed to be in channels.
- delete_roi(self, index)
- Deletes an ROI from the epicsMca.
Inputs:
index:
The index number of the ROI to be deleted (0-31).
- erase(self)
- Erases the EPICS mca
- get_acquire_status(self, update=0)
- Returns the acquisition status. 1 if the mca is acquiring, 0 if it is not.
Keywords:
update:
Set this keyword to force the routine to process the record, and thus read
the hardware.
By default update=0 and this routine does not process the record.
The acquire status returned will be the status when the record
was last processed.
- get_calibration(self)
- Reads the calibration information from the EPICS mca record. Stores this information
in the epicsMca object, and returns an McaCalibration object with this information.
- get_data(self)
- Reads the data from the EPICS mca record. Returns the data with Mca.get_data().
- get_elapsed(self)
- Reads the elapsed information from the EPICS mca record. Stores this information
in the epicsMca object, and returns an McaElapsed object with this information.
- get_environment(self)
- Reads the current values of the environment PVs. Returns a list of
McaEnvironment objects with Mca.get_environment().
- get_presets(self)
- Reads the preset information from the EPICS mca record. Stores this information
in the epicsMca object, and returns an McaPresets object with this information.
- get_roi_counts(self)
- Reads the ROI counts from the EPICS mca record. Returns a tuple containing two lists,
(total, net), containing the total and net counts in each ROI.
- get_rois(self, energy=0)
- Reads the ROI information from the EPICS mca record. Stores this information
in the epicsMca object, and returns a list of McaROI objects with this information.
- get_sequence(self)
- Reads the current "sequence" from the EPICS mca record. The sequence is typically
used for fast data collection where multiple spectra are stored in the hardware.
- new_acquire_status(self)
- Returns a flag to indicate if the acquisition status (acquiring or done) for the mca
record has changed since it was last read. Returns 1 if the status has changed,
0 if it has not.
- new_data(self)
- Returns a flag to indicate if the data (counts) for the mca record have changed
since they were last read. Returns 1 if there are new values, 0 if there are not.
- new_elapsed(self)
- Returns a flag to indicate if the elapsed parameters for the mca record have changed
since they were last read. Returns 1 if there are new values, 0 if there are not.
- read_environment_file(self, file)
- Reads a file containing the "environment" PVs. The values and desriptions of these
PVs are stored in the data files written by Mca.write_file().
Inputs:
file:
The name of the file containing the environment PVs. This is an ASCII file
with each line containing a process variable name, followed by a
space and a description field.
- set_calibration(self, calibration)
- Writes the calibration information from an McaCalibration object to the EPICS mca
record.
Inputs:
calibration:
An McaCalibration instance containing the calibration information.
- set_data(self, data)
- Writes data to the EPICS mca record. The .VAL and .NUSE fields are written to.
Inputs:
data:
The array of counts to be written to the mca record.
- set_elapsed(self, elapsed)
- Writes the elapsed information from an McaElapsed object to the EPICS mca
record.
Inputs:
elapsed:
An McaElapsed instance containing the elapsed information.
- set_presets(self, presets)
- Writes the presets information from an McaPresets object to the EPICS mca
record.
Inputs:
presets:
An McaPresets instance containing the presets information.
- set_rois(self, rois, energy=0)
- Writes the ROI information from a list of mcaROI objects to the EPICS mca
record.
Inputs:
rois:
A list of mcaROI objects.
Keywords:
energy:
Set this flag if the .left and .right fields of the ROIs are in energy units.
By default these fields are assumed to be in channels.
- set_sequence(self, sequence)
- Sets the current "sequence" from the EPICS mca record. The sequence is typically
used for fast data collection where multiple spectra are stored in the hardware.
Inputs:
sequence:
The sequence number for data collection.
- spectra_scan(self, first_file, scan_record)
- Collects Mca spectra and saves them to disk in conjunction with an EPICS scan record.
Inputs:
first_file:
The name of the first spectrum file to save. Subsequent files
will be named using the Xrf.increment_filename()function. The
filename must end in a numeric extension for this to work.
scan_record:
The name of the EPICS scan record which is controlling the scan.
This scan record must be configure to start epicsMca data collection
by writing "1" into the ERST field if the EPICS MCA.
Procedure:
1) Waits for scan.EXSC = 1, meaning scan has started
2) Waits for ClientWait=1, meaning acquisition has started
3) Waits for Acquiring=0, meaning acquisition has completed
4) Writes data to disk with epicsMca::write_file, increment file name
5) Resets ClientWait to 0 so scan will continue
6) If scan.EXSC is still 1 go to 2.
- start(self, erase=0)
- Starts acquisition of the EPICS mca.
Keywords:
erase:
Set this flag to erase the mca before starting acquisition. The default
is not to erase first.
- stop(self)
- Stops acquisition of the EPICS mca.
- wait(self, delay=0.10000000000000001, start=0, stop=1)
- Waits for acquisition of the MCA to start and/or complete.
Keywords:
delay: The time between polling. Default=0.1 seconds.
start:
Set this flag to wait for acquisition to start.
stop:
Set this flag to wait for acquisition to stop. This is the default.
If both the "start" and "stop" keywords are given then the routine
will wait first for acquisition to start and then for acquistion to
stop. If only start=1 is given then it will not wait for acquisition
to stop.
- write_file(self, file)
- Invokes Mca.write_file to write the mca to a disk file.
After the file is written resets the client wait flag PV, if it exists.
This flag is typically used by the EPICS scan record to wait for a client application
to save the file before it goes on to the next point in the scan.
Inputs:
file:
The name of the file to write the mca to.
Data and non-method functions defined here:
- __doc__ = None
- __module__ = 'epicsMca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
Methods inherited from Mca.Mca:
- __copy__(self)
- Makes a "shallow" copy of an Mca instance, using copy.copy() on all of
the attributes of the Mca instance. The .rois and .environment attributes
will still point to the same values, because they are lists.
- __deepcopy__(self, visit)
- Makes a "deep" copy of an Mca instance, using copy.deepcopy() on all of
the attributes of the Mca instance. All of the attribute will point to
new objects.
- channel_to_d(self, channels)
- Converts channels to "d-spacing" using the current calibration values for
the Mca. This routine can convert a single channel number or an array of
channel numbers. Users are strongly encouraged to use this function
rather than implement the conversion calculation themselves, since it
will be updated if additional calibration parameters are added. This
routine is useful for energy dispersive diffraction experiments. It uses
both the energy calibration parameters and the "two-theta" calibration
parameter.
Inputs:
channels:
The channel numbers to be converted to "d-spacing".
This can be a single number or a list of channel numbers.
Outputs:
This function returns the equivalent "d-spacing" for the input channels.
The output units are in Angstroms.
Restrictions:
This function assumes that the units of the energy calibration are keV
and that the units of "two-theta" are degrees.
Example:
mca = Mca('mca.001')
channels = [100,200,300]
d = mca.channel_to_d(channels) # Get the "d-spacing" of these
- channel_to_energy(self, channels)
- Converts channels to energy using the current calibration values for the
Mca. This routine can convert a single channel number or an array of
channel numbers. Users are strongly encouraged to use this function
rather than implement the conversion calculation themselves, since it
will be updated if additional calibration parameters (cubic, etc.) are
added.
Inputs:
channels:
The channel numbers to be converted to energy. This can be
a single number or a sequence of channel numbers.
Outputs:
This function returns the equivalent energy for the input channels.
Example:
mca = Mca('mca.001')
channels = [100, 200, 300]
energy = mca.channel_to_energy(channels) # Get the energy of these
- d_to_channel(self, d, clip=0)
- Converts "d-spacing" to channels using the current calibration values
for the Mca. This routine can convert a single "d-spacing" or an array
of "d-spacings". Users are strongly encouraged to use this function
rather than implement the conversion calculation themselves, since it
will be updated if additional calibration parameters are added.
This routine is useful for energy dispersive diffraction experiments.
It uses both the energy calibration parameters and the "two-theta"
calibration parameter.
Inputs:
d:
The "d-spacing" values to be converted to channels.
This can be a single number or an array of values.
Keywords:
clip:
Set this flag to 1 to clip the returned values to be between
0 and nchans-1. The default is not to clip.
Outputs:
This function returns the closest equivalent channel for the input
"d-spacing". Note that it does not generate an error if the channel
number is outside the range 0 to (nchans-1), which will happen if the
"d-spacing" is outside the range for the calibration values of the Mca.
Example:
mca = Mca('mca.001')
channel = mca.d_to_chan(1.598)
- energy_to_channel(self, energy, clip=0)
- Converts energy to channels using the current calibration values for the
Mca. This routine can convert a single energy or an array of energy
values. Users are strongly encouraged to use this function rather than
implement the conversion calculation themselves, since it will be updated
if additional calibration parameters are added.
Inputs:
energy:
The energy values to be converted to channels. This can be a
single number or a sequence energy values.
Keywords:
clip:
Set this flag to 1 to clip the returned values to be between
0 and nchans-1. The default is not to clip.
Outputs:
This function returns the closest equivalent channel for the input
energy. Note that it does not generate an error if the channel number
is outside the range 0 to (nchans-1), which will happen if the energy
is outside the range for the calibration values of the Mca.
Example:
mca = Mca('mca.001')
channel = mca.energy_to_channel(5.985)
- find_roi(self, left, right, energy=0)
- This procedure finds the index number of the ROI with a specified
left and right channel number.
Inputs:
left:
Left channel number (or energy) of this ROI
right:
Right channel number (or energy) of this ROI
Keywords:
energy:
Set this flag to 1 to indicate that Left and Right are in units
of energy rather than channel number.
Output:
Returns the index of the specified ROI, -1 if the ROI was not found.
Example:
mca = Mca('mca.001')
index = mca.find_roi(100, 200)
- fit_background(self, bottom_width=4.0, top_width=0.0, exponent=2, tangent=0, compress=4)
- 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.
Keywords:
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).
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.
Outputs:
This function returns an MCA object which is identical to the calling
object, except that the data have been replaced by the background fit.
Example:
mca = Mca()
mca.read_file('mca.001')
bgd = mca.fit_background(mca, bottom=6, exponent=4)
- fit_peaks(self, peaks, fit=None, background=None, output='', spreadsheet=None, append=1, **background_kw)
- Fits the peaks in the MCA spectrum. It provides a convenient interface to
the fitPeaks() function.
Inputs:
peaks: A list of McaPeak objects. See fitPeaks() and read_peaks() for
more information.
Keywords:
fit:
An object of type McaFit which can be used to control the
peak fitting. If this keyword is omitted then the fit structure
is created with McaFit()
background:
An Mca object containing the fitted background. If this keyword
is omitted then this function will call Mca.fit_background() before
calling fitPeaks().
output:
The name of an output file to receive the ASCII printout of the
fit results. This keyword is simply passed to fit_peaks_report().
spreadsheet:
The name of an output file to receive the ASCII output of the
fit results in spreadsheet format. This keyword is simply passed to
fit_peaks_report().
append:
Flag indicating whether the output and spreadsheet files should be
appended to or overwritten. This keyword is simply passed to
fit_peaks_report9).
In addition to these keywords, all keywords accepted by the
fit_background() function are accepted if the background keyword is
not present, i.e. if this function will be calling fit_background().
Outputs:
This function returns an Mca object which is identical to the calling
object, except that the data have been replaced by the peak fit.
Procedure:
The function does the following:
- Creates the Fit structure with mca->FIT_INITIALIZE() if Fit
was not passed as a keyword parameter.
- Fits the background using fit_background() if background
was not passed as a keyword parameter.
- Extracts the data from the input spectrum and the background
spectrum.
- Calls fitPeaks() with the background subtracted data.
- Calls fit_peaks_report()
- Creates a new Mca object using Mca.deepcopy and stores the output
of fitPeaks() in this new object with set_data(). It then
returns this new MCA object as the function return value.
Example:
mca = Mca()
mca.read_file('mca.001')
peaks = read_peaks('mypeaks.pks')
fit = mca.fit_peaks(peaks, bottom=6, exponent=4)
- fit_peaks_report(self, fit, peaks, background, output='', spreadsheet=None, append=1, time=None)
- Prints out the results from <A HREF="mca_utility_routines.html#FIT_PEAKS">FIT_PEAKS</A>
Inputs:
fit:
An McaFit object with the global fitting parameters.
peaks:
A list of McaPeak objects with the fit results for each peak.
See fit_peaks for more information on fit and peaks.
background:
An Mca object containing the fitted background spectrum.
Keywords:
output:
The name of an output file to receive the ASCII printout of the
fit results. If this keyword is omitted then the output will be
written to stdout, i.e. the IDL output window. If the Output file
already exists then the new information will (by default) be appended
to the file.
spreadsheet:
The name of an output file to receive the ASCII output of the
fit results in a format easily imported into a spreadsheet. If this
keyword is omitted then no spreadsheet output will be generated.
written to stdout, i.e. the IDL output window.
If the spreadhseet file already exists then the new information will
(by default) be appended to the file.
append:
Set this keyword to 0 to overwrite the output and spreadsheet files
rather than to append to them, which is the default behavior.
Example:
mca = Mca(file='mca.001')
peaks = read_peaks('mypeaks.pks')
[fit, peaks, predicted] = mca.fit_peaks(peaks, fit,
bottom=6, exponent=4)
mca.fit_peaks_report(fit, peaks, background, output='mca.001_out')
- get_energy(self)
- Returns a list containing the energy of each channel in the MCA spectrum.
Procedure:
Simply returns mca.channel_to_energy() for each channel
Example:
from Mca import *
mca = Mca('mca.001')
energy = mca.get_energy()
- get_name(self)
- Returns the Mca name as a string
- initial_calibration(self, energy)
- Performs an initial coarse energy calibration of the Mca, setting only
the slope, and setting the offset parameter to 0.
Inputs:
energy: The energy of the biggest peak in the MCA spectrum.
Procedure:
This routine does the following:
1) Sets the offset coefficient to 0.0
2) Sets the quadratic coefficient to 0.0
3) Determines which channel contains the most counts, PEAK_CHAN
4) Sets the slope equal to the input energy divided by PEAK_CHAN
Example:
from Mca import *
mca = Mca('mca.001')
mca.initial_calibration(20.1)
- read_file(self, file, netcdf=0, detector=0)
- Reads a disk file into an MCA object. If the netcdf=1 flag is set it
reads a netcdf file, else it assumes the file is ASCII.
If the data file has multiple detectors then the detector keyword can be
used to specify which detector data to return.
Inputs:
file:
The name of the disk file to read.
Keywords:
netcdf:
Set this flag to read files written in netCDF format, otherwise
the routine assumes that the file is in ASCII format.
See the documentation for Mca.write_ascii_file and
Mca.write_netcdf_file for information on the formats.
detector:
Specifies which detector to read if the file has multiple detectors.
Example:
mca = Mca()
mca.read_file('mca.001')
- set_environment(self, environment)
- Copies a list of McaEnvironment objects to the Mca object.
Inputs:
environment:
A list of McaEnvironment objects.
- set_name(self, name)
- Sets the Mca name.
Inputs:
name:
A string
|