def run(config): """ This method is where the data reduction process gets done. @param config: Object containing the data reduction configuration information. @type config: L{hlr_utils.Configure} """ import sys import dr_lib import DST import SOM banks = [("/entry/bank1", 1), ("/entry/bank2", 1)] max_ids = (64, 64) if config.vertical: tag = "v" size = max_ids[1] reps = max_ids[0] / config.pixel_group label = "Integrated pixel" else: tag = "h" size = max_ids[1] / config.pixel_group reps = max_ids[0] / config.sum_tubes label = "Tube Number" try: data_dst = DST.getInstance("application/x-NeXus", config.data) except SystemError: print "ERROR: Failed to data read file %s" % config.data sys.exit(-1) so_axis = "time_of_flight" for path in banks: bank = path[0].split('/')[-1] for i in range(size): tSOM = SOM.SOM() tSO = SOM.SO(construct=True) counter = 1 for j in range(reps): if config.vertical: starting_id = (i, config.pixel_group * j) ending_id = (i + 1, config.pixel_group * (j + 1)) else: if config.sum_tubes == 1: x1 = j x2 = j + 1 else: x1 = j * config.sum_tubes x2 = (j + 1) * config.sum_tubes starting_id = (x1, config.pixel_group * i) ending_id = (x2, config.pixel_group * (i + 1)) d_som1 = data_dst.getSOM(path, so_axis, start_id=starting_id, end_id=ending_id) d_som2 = dr_lib.sum_all_spectra(d_som1) d_som2[0].id = d_som1[0].id d_som1 = None del d_som1 value = dr_lib.integrate_axis(d_som2) if config.verbose: print "Sum", d_som2[0].id, ":", value[0], value[1] tSO.axis[0].val.append(counter) tSO.y.append(value[0]) tSO.var_y.append(value[1]) if counter == 1: tSO.id = d_som2[0].id counter += 1 tSOM.attr_list["filename"] = config.data tSOM.setTitle("TOF Pixel Summation") tSOM.setDataSetType("density") tSOM.setYLabel("Intensity Sum") tSOM.setYUnits("counts") tSOM.setAxisLabel(0, label) tSOM.setAxisUnits(0, "") tSOM.append(tSO) tag1 = str(i + 1) outfile = bank + "_" + tag + "_" + tag1 + ".tof" hlr_utils.write_file(outfile, "text/Spec", tSOM, verbose=config.verbose, message="intensity sum file", replace_ext=False) data_dst.release_resource()
def integrate_spectra(obj, **kwargs): """ This function takes a set of spectra and calculates the integration for the primary axis. If the integration range for a spectrum cannot be found, an error report will be generated with the following information: Range not found: pixel ID, start bin, end bin, length of data array A failing pixel will have the integration tuple set to C{(nan, nan)}. @param obj: Object containing spectra that will have the integration calculated from them. @type obj: C{SOM.SOM} or C{SOM.SO} @param kwargs: A list of keyword arguments that the function accepts: @keyword start: The start range for the integration. @type start: C{int} @keyword end: The end range for the integration. function. @type end: C{int} @keyword axis_pos: This is position of the axis in the axis array. If no argument is given, the default value is I{0}. @type axis_pos: C{int} @keyword norm: This is a flag to turn on the division of the individual spectrum integrations by the solid angle of the corresponding pixel. This also activates the multiplication of the individual spectrum bin values by their corresponding bin width via the I{width} flag in L{integrate_axis}. The default value of the flag is I{False}. @type norm: C{boolean} @keyword total: This is a flag to turn on the summation of all individual spectrum integrations. The default value of the flag is I{False}. @type total: C{boolean} @keyword width: This is a flag to turn on the removal of the individual bin width in the L{integrate_axis} function while doing the integrations. The default value of the flag is I{False}. @type width: C{boolean} @return: Object containing the integration and the uncertainty squared associated with the integration @rtype: C{SOM.SOM} or C{SOM.SO} """ # import the helper functions import hlr_utils if obj is None: return obj # set up for working through data (result, res_descr) = hlr_utils.empty_result(obj) o_descr = hlr_utils.get_descr(obj) result = hlr_utils.copy_som_attr(result, res_descr, obj, o_descr) # Check for axis_pos keyword argument try: axis_pos = kwargs["axis_pos"] except KeyError: axis_pos = 0 # Check for norm keyword argument try: norm = kwargs["norm"] if norm: if o_descr == "SO": raise RuntimeError("Cannot use norm keyword with SO!") width = True inst = obj.attr_list.instrument else: width = False except KeyError: norm = False width = False # Check for total keyword argument try: total = kwargs["total"] except KeyError: total = False # Check for width keyword argument only if norm isn't present if not norm: try: width = kwargs["width"] except KeyError: width = False # If the integration start bound is not given, set to infinity try: i_start = kwargs["start"] except KeyError: i_start = float("inf") # If the integration end bound is not given, set to infinity try: i_end = kwargs["end"] except KeyError: i_end = float("inf") # iterate through the values import dr_lib len_obj = hlr_utils.get_length(obj) for i in xrange(len_obj): obj1 = hlr_utils.get_value(obj, i, o_descr, "all") # If there's a NaN at the front and back, there are NaN's everywhere if str(obj1.axis[axis_pos].val[0]) == "nan" and \ str(obj1.axis[axis_pos].val[-1]) == "nan": print "Range not found:", obj1.id, i_start, i_end, len(obj1) value = (float('nan'), float('nan')) else: value = dr_lib.integrate_axis(obj1, start=i_start, end=i_end, width=width) if norm: if inst.get_name() == "BSS": map_so = hlr_utils.get_map_so(obj, None, i) dOmega = dr_lib.calc_BSS_solid_angle(map_so, inst) value1 = (value[0] / dOmega, value[1] / (dOmega * dOmega)) else: raise RuntimeError("Do not know how to get solid angle from "\ +"%s" % inst.get_name()) else: value1 = value hlr_utils.result_insert(result, res_descr, value1, obj1, "yonly") if not total: return result else: # Sum all integration counts total_counts = 0 total_err2 = 0 for j in xrange(hlr_utils.get_length(result)): total_counts += hlr_utils.get_value(result, j, res_descr, "y") total_err2 += hlr_utils.get_err2(result, j, res_descr, "y") # Create new result object (result2, res2_descr) = hlr_utils.empty_result(result) result2 = hlr_utils.copy_som_attr(result2, res2_descr, result, res_descr) res1 = hlr_utils.get_value(result, 0, res_descr, "all") hlr_utils.result_insert(result2, res2_descr, (total_counts, total_err2), res1, "yonly") return result2
def determine_time_indep_bkg(obj, tof_vals, **kwargs): """ This functions calculates the average counts at four given TOF channels for determining the time-independent background. @param obj: Object used for determining the time-independent background @type obj: C{SOM.SOM} or C{SOM.SO} @param tof_vals: The four TOF channels from which the average counts will be determined @type tof_vals: C{list} @param kwargs: A list of keyword arguments that the function accepts: @keyword is_range: A flag that tells the function that tof_vals is a range from which to determine the time-independent background. The default is I{False}. @type is_range: C{bool} @return: Object containing the time-independent background and the associated error @rtype: C{list} of C{tuple}s @raise TypeError: The incoming object is not a C{SOM} or C{SO}. """ # Kickout if tof_vals is NoneType if tof_vals is None: return None # import the helper functions import hlr_utils # Get keyword arguments is_range = kwargs.get("is_range", False) o_descr = hlr_utils.get_descr(obj) if o_descr != "SOM" and o_descr != "SO": raise TypeError("Incoming object must be a SOM or a SO") # Have a SOM or SO else: pass # set up for working through data (result, res_descr) = hlr_utils.empty_result(obj) result = hlr_utils.copy_som_attr(result, res_descr, obj, o_descr) if not is_range: num_tof_vals = float(len(tof_vals)) import bisect else: num_tof_vals = tof_vals[1] - tof_vals[0] import dr_lib # iterate through the values len_obj = hlr_utils.get_length(obj) for i in xrange(len_obj): obj1 = hlr_utils.get_value(obj, i, o_descr, "all") if not is_range: average = 0.0 ave_err2 = 0.0 for tof in tof_vals: index = bisect.bisect(obj1.axis[0].val, float(tof)) - 1 average += obj1.y[index] ave_err2 += obj1.var_y[index] else: (average, ave_err2) = dr_lib.integrate_axis(obj1, width=True, start=tof_vals[0], end=tof_vals[1]) average /= num_tof_vals ave_err2 /= num_tof_vals hlr_utils.result_insert(result, res_descr, (average, ave_err2), obj1, "yonly") return result