Exemplo n.º 1
0
def rebin_monitor(obj1, obj2, **kwargs):
    """
    This function takes objects (1st is the monitor, 2nd is the detector data)
    and rebins the data for obj1 onto the axis provided by obj2. The pixel ID
    can be transferred as the pixel ID of the rebinned monitor histogram. A
    prefix is placed on the bank ID to dilineate that this is a rebinned
    monitor spectrum for that pixel.

    @param obj1: Monitor object that will be rebinned
    @type obj1: C{SOM.SOM} or C{SOM.SO}
    
    @param obj2: Detector data object that will provide the axis for rebinning
    @type obj2: C{SOM.SOM} or C{SOM.SO}

    @param kwargs: A list of keyword arguments that the function accepts

    @keyword use_pix_id: A flag that specifies if the pixel ID is to be used
    in the rebinned monitor spectrum. The default is I{False}.
    @type use_pix_id: C{boolean}

    @keyword prefix: Allows one to specify a prefix for the bank ID. The
                     default is I{m}.
    @type prefix: C{string}

    @keyword rtype: A short string that defines the rebinning function to use.
                    The default is I{None} which uses
                    L{common_lib.rebin_axis_1D()}. The other possibilities are
                    I{linint} which uses L{common_lib.rebin_axis_1D_linint()}
                    and I{frac} which uses L{common_lib.rebin_axis_1D_frac()}.
    @type rtype: C{boolean}

    
    @return: Object that has been rebinned
    @rtype: C{SOM.SOM} or C{SOM.SO}


    @raise TypeError: The C{SOM}-C{SO} operation is attempted
    
    @raise TypeError: The C{SO}-C{SOM} operation is attempted
    
    @raise TypeError: obj1 not a C{SOM} or C{SO}
    
    @raise TypeError: obj2 not a C{SOM} or C{SO}
    """

    # import the helper functions
    import hlr_utils

    # Kickout if monitor object is None
    if obj1 is None:
        return obj1
    
    # set up for working through data
    (result, res_descr) = hlr_utils.empty_result(obj1, obj2)
    (o1_descr, o2_descr) = hlr_utils.get_descr(obj1, obj2)
    
    # error checking for types
    if o1_descr == "SOM" and o2_descr == "SO":
        raise TypeError, "SOM-SO operation not supported"
    elif o1_descr == "SO" and o2_descr == "SOM":
        raise TypeError("SO-SOM operation not supported")
    # Have the right object combination, go on
    else:
        pass

    # Check for keywords
    try:
        prefix = kwargs["prefix"]
    except KeyError:
        prefix = "m"

    try: 
        use_pix_id = kwargs["use_pix_id"]
    except KeyError:
        use_pix_id = False

    try: 
        rtype = kwargs["rtype"]
    except KeyError:
        rtype = None

    # Set the name of the rebinning function
    rebin_function_name = "rebin_axis_1D"
    if rtype is not None:
        rebin_function_name += "_" + str(rtype)

    # Get function pointer
    import common_lib
    rebin_function = common_lib.__getattribute__(rebin_function_name)

    result = hlr_utils.copy_som_attr(result, res_descr, obj1, o1_descr)

    # iterate through the values
    val1 = hlr_utils.get_value(obj1, 0, o1_descr, "all")

    # Cache length
    len_obj2 = hlr_utils.get_length(obj2)

    for i in xrange(len_obj2):
        val2 = hlr_utils.get_value(obj2, i, o2_descr, "x")

        value = rebin_function(val1, val2)

        if use_pix_id:
            # Set the pixel ID to the spectrum with modified bank ID
            try:
                value.id = (prefix+obj2[i].id[0],
                            (obj2[i].id[1][0], obj2[i].id[1][1]))
            except TypeError:
                value.id = prefix+str(obj2[i].id)
        
        hlr_utils.result_insert(result, res_descr, value, None, "all")

    return result
def convert_single_to_list(funcname, number, som, **kwargs):
    """
    This function retrieves a function object from the L{common_lib} set of
    functions that provide axis transformations and converts the provided
    number based on that function. Instrument geometry information needs to be
    provided via the C{SOM}. The following is the list of functions supported
    by this one.

      - d_spacing_to_tof_focused_det
      - energy_to_wavelength
      - frequency_to_energy
      - initial_wavelength_igs_lin_time_zero_to_tof
      - init_scatt_wavevector_to_scalar_Q
      - tof_to_initial_wavelength_igs_lin_time_zero
      - tof_to_initial_wavelength_igs
      - tof_to_scalar_Q
      - tof_to_wavelength_lin_time_zero
      - tof_to_wavelength
      - wavelength_to_d_spacing
      - wavelength_to_energy
      - wavelength_to_scalar_k
      - wavelength_to_scalar_Q

    @param funcname: The name of the axis conversion function to use
    @type funcname: C{string}

    @param number: The value and error^2 to convert
    @type number: C{tuple}

    @param som: The object containing geometry and other special information
    @type som: C{SOM.SOM}

    @param kwargs: A list of keyword arguments that the function accepts:

    @keyword inst_param: The type of parameter requested from an associated
                         instrument. For this function the acceptable
                         parameters are I{primary}, I{secondary} and I{total}.
                         Default is I{primary}.
    @type inst_param: C{string}

    @keyword pixel_id: The pixel ID from which the geometry information will
                       be retrieved from the instrument
    @type pixel_id: C{tuple}=(\"bankN\", (x, y))


    @return: A converted number for every unique spectrum
    @rtype: C{list} of C{tuple}s


    @raise AttributeError: The requested function is not in the approved
                           list
    """
    # Setup supported function list and check to see if funcname is available
    function_list = []
    function_list.append("d_spacing_to_tof_focused_det")
    function_list.append("energy_to_wavelength")
    function_list.append("frequency_to_energy")
    function_list.append("initial_wavelength_igs_lin_time_zero_to_tof")
    function_list.append("init_scatt_wavevector_to_scalar_Q")
    function_list.append("tof_to_initial_wavelength_igs_lin_time_zero")
    function_list.append("tof_to_initial_wavelength_igs")
    function_list.append("tof_to_scalar_Q")
    function_list.append("tof_to_wavelength_lin_time_zero")
    function_list.append("tof_to_wavelength")
    function_list.append("wavelength_to_d_spacing")
    function_list.append("wavelength_to_energy")
    function_list.append("wavelength_to_scalar_k")
    function_list.append("wavelength_to_scalar_Q")

    if funcname not in function_list:
        raise AttributeError("Function %s is not supported by "\
                             +"convert_single_to_list" % funcname)

    import common_lib

    # Get the common_lib function object
    func = common_lib.__getattribute__(funcname)

    # Setup inclusive dictionary containing the requested keywords for all
    # common_lib axis conversion functions

    fkwds = {}
    fkwds["pathlength"] = ()
    fkwds["polar"] = ()
    fkwds["lambda_f"] = ()
    try:
        lambda_final = som.attr_list["Wavelength_final"]
    except KeyError:
        lambda_final = None
    try:
        fkwds["time_zero_slope"] = som.attr_list["Time_zero_slope"]
    except KeyError:
        pass
    try:
        fkwds["time_zero_offset"] = som.attr_list["Time_zero_offset"]
    except KeyError:
        pass
    try:
        fkwds["time_zero"] = som.attr_list["Time_zero"]
    except KeyError:
        pass    
    fkwds["dist_source_sample"] = ()
    fkwds["dist_sample_detector"] = ()
    try:
        fkwds["inst_param"] = kwargs["inst_param"]
    except KeyError:
        fkwds["inst_param"]  = "primary"
    try:
        fkwds["pixel_id"] = kwargs["pixel_id"]
    except KeyError:
        fkwds["pixel_id"]  = None
    fkwds["run_filter"] = False

    # Set up for working through data
    # This time highest object in the hierarchy is NOT what we need
    result = []
    res_descr = "list"

    inst = som.attr_list.instrument
    
    import hlr_utils

    # iterate through the values
    for i in xrange(hlr_utils.get_length(som)):
        map_so = hlr_utils.get_map_so(som, None, i)
        
        fkwds["pathlength"] = hlr_utils.get_parameter(fkwds["inst_param"],
                                                      map_so, inst)
        
        fkwds["dist_source_sample"] = hlr_utils.get_parameter("primary",
                                                              map_so, inst)

        fkwds["dist_sample_detector"] = hlr_utils.get_parameter("secondary",
                                                                map_so, inst)

        fkwds["polar"] = hlr_utils.get_parameter("polar", map_so, inst)

        fkwds["lambda_f"] = hlr_utils.get_special(lambda_final, map_so)

        value = tuple(func(number, **fkwds))

        hlr_utils.result_insert(result, res_descr, value, None, "all")

    return result
Exemplo n.º 3
0
def rebin_monitor(obj1, obj2, **kwargs):
    """
    This function takes objects (1st is the monitor, 2nd is the detector data)
    and rebins the data for obj1 onto the axis provided by obj2. The pixel ID
    can be transferred as the pixel ID of the rebinned monitor histogram. A
    prefix is placed on the bank ID to dilineate that this is a rebinned
    monitor spectrum for that pixel.

    @param obj1: Monitor object that will be rebinned
    @type obj1: C{SOM.SOM} or C{SOM.SO}
    
    @param obj2: Detector data object that will provide the axis for rebinning
    @type obj2: C{SOM.SOM} or C{SOM.SO}

    @param kwargs: A list of keyword arguments that the function accepts

    @keyword use_pix_id: A flag that specifies if the pixel ID is to be used
    in the rebinned monitor spectrum. The default is I{False}.
    @type use_pix_id: C{boolean}

    @keyword prefix: Allows one to specify a prefix for the bank ID. The
                     default is I{m}.
    @type prefix: C{string}

    @keyword rtype: A short string that defines the rebinning function to use.
                    The default is I{None} which uses
                    L{common_lib.rebin_axis_1D()}. The other possibilities are
                    I{linint} which uses L{common_lib.rebin_axis_1D_linint()}
                    and I{frac} which uses L{common_lib.rebin_axis_1D_frac()}.
    @type rtype: C{boolean}

    
    @return: Object that has been rebinned
    @rtype: C{SOM.SOM} or C{SOM.SO}


    @raise TypeError: The C{SOM}-C{SO} operation is attempted
    
    @raise TypeError: The C{SO}-C{SOM} operation is attempted
    
    @raise TypeError: obj1 not a C{SOM} or C{SO}
    
    @raise TypeError: obj2 not a C{SOM} or C{SO}
    """

    # import the helper functions
    import hlr_utils

    # Kickout if monitor object is None
    if obj1 is None:
        return obj1

    # set up for working through data
    (result, res_descr) = hlr_utils.empty_result(obj1, obj2)
    (o1_descr, o2_descr) = hlr_utils.get_descr(obj1, obj2)

    # error checking for types
    if o1_descr == "SOM" and o2_descr == "SO":
        raise TypeError, "SOM-SO operation not supported"
    elif o1_descr == "SO" and o2_descr == "SOM":
        raise TypeError("SO-SOM operation not supported")
    # Have the right object combination, go on
    else:
        pass

    # Check for keywords
    try:
        prefix = kwargs["prefix"]
    except KeyError:
        prefix = "m"

    try:
        use_pix_id = kwargs["use_pix_id"]
    except KeyError:
        use_pix_id = False

    try:
        rtype = kwargs["rtype"]
    except KeyError:
        rtype = None

    # Set the name of the rebinning function
    rebin_function_name = "rebin_axis_1D"
    if rtype is not None:
        rebin_function_name += "_" + str(rtype)

    # Get function pointer
    import common_lib
    rebin_function = common_lib.__getattribute__(rebin_function_name)

    result = hlr_utils.copy_som_attr(result, res_descr, obj1, o1_descr)

    # iterate through the values
    val1 = hlr_utils.get_value(obj1, 0, o1_descr, "all")

    # Cache length
    len_obj2 = hlr_utils.get_length(obj2)

    for i in xrange(len_obj2):
        val2 = hlr_utils.get_value(obj2, i, o2_descr, "x")

        value = rebin_function(val1, val2)

        if use_pix_id:
            # Set the pixel ID to the spectrum with modified bank ID
            try:
                value.id = (prefix + obj2[i].id[0], (obj2[i].id[1][0],
                                                     obj2[i].id[1][1]))
            except TypeError:
                value.id = prefix + str(obj2[i].id)

        hlr_utils.result_insert(result, res_descr, value, None, "all")

    return result
Exemplo n.º 4
0
def run(config, tim):
    """
    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}

    @param tim: Object that will allow the method to perform timing
                evaluations.
    @type tim: C{sns_time.DiffTime}    
    """
    import common_lib
    import dr_lib

    if tim is not None:
        tim.getTime(False)
        old_time = tim.getOldTime()

    if config.data1 is None or config.data2 is None:
        raise RuntimeError("Need to pass a data filename(s) to the driver "\
                           +"script.")

    dst_type1 = hlr_utils.file_peeker(config.data1[0])

    if config.verbose:
        print "Initial file type (data set 1):", dst_type1

    d_som1 = dr_lib.add_files(config.data1,
                              dst_type=dst_type1,
                              Verbose=config.verbose,
                              Timer=tim)

    dst_type2 = hlr_utils.file_peeker(config.data2[0])

    if config.verbose:
        print "Initial file type (data set 2):", dst_type2

    d_som2 = dr_lib.add_files(config.data2,
                              dst_type=dst_type2,
                              Verbose=config.verbose,
                              Timer=tim)

    # Get requested simple math operation
    func = common_lib.__getattribute__(config.operation)

    d_som3 = func(d_som1, d_som2)

    del d_som1, d_som2

    # Rescale data if necessary
    if config.rescale is not None:
        d_som4 = common_lib.mult_ncerr(d_som3, (config.rescale, 0.0))
    else:
        d_som4 = d_som3

    del d_som3

    # Write out file after simple math operation
    hlr_utils.write_file(config.output,
                         dst_type1,
                         d_som4,
                         verbose=config.verbose,
                         replace_ext=False,
                         path_replacement=config.path_replacement,
                         axis_ok=True,
                         message="operated file")

    if tim is not None:
        tim.setOldTime(old_time)
        tim.getTime(msg="Total Running Time")
Exemplo n.º 5
0
def run(config, tim):
    """
    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}

    @param tim: Object that will allow the method to perform timing
                evaluations.
    @type tim: C{sns_time.DiffTime}    
    """
    import common_lib
    import dr_lib
    
    if tim is not None:
        tim.getTime(False)
        old_time = tim.getOldTime()

    if config.data1 is None or config.data2 is None:
        raise RuntimeError("Need to pass a data filename(s) to the driver "\
                           +"script.")

    dst_type1 = hlr_utils.file_peeker(config.data1[0])

    if config.verbose:
        print "Initial file type (data set 1):", dst_type1

    d_som1 = dr_lib.add_files(config.data1, dst_type=dst_type1,
                              Verbose=config.verbose,
                              Timer=tim)

    dst_type2 = hlr_utils.file_peeker(config.data2[0])

    if config.verbose:
        print "Initial file type (data set 2):", dst_type2

    d_som2 = dr_lib.add_files(config.data2, dst_type=dst_type2,
                              Verbose=config.verbose,
                              Timer=tim)

    # Get requested simple math operation
    func = common_lib.__getattribute__(config.operation)

    d_som3 = func(d_som1, d_som2)

    del d_som1, d_som2

    # Rescale data if necessary
    if config.rescale is not None:
        d_som4 = common_lib.mult_ncerr(d_som3, (config.rescale, 0.0))
    else:
        d_som4 = d_som3

    del d_som3

    # Write out file after simple math operation
    hlr_utils.write_file(config.output, dst_type1, d_som4,
                         verbose=config.verbose,
                         replace_ext=False,
                         path_replacement=config.path_replacement,
                         axis_ok=True,
                         message="operated file")

    if tim is not None:
        tim.setOldTime(old_time)
        tim.getTime(msg="Total Running Time")
def convert_single_to_list(funcname, number, som, **kwargs):
    """
    This function retrieves a function object from the L{common_lib} set of
    functions that provide axis transformations and converts the provided
    number based on that function. Instrument geometry information needs to be
    provided via the C{SOM}. The following is the list of functions supported
    by this one.

      - d_spacing_to_tof_focused_det
      - energy_to_wavelength
      - frequency_to_energy
      - initial_wavelength_igs_lin_time_zero_to_tof
      - init_scatt_wavevector_to_scalar_Q
      - tof_to_initial_wavelength_igs_lin_time_zero
      - tof_to_initial_wavelength_igs
      - tof_to_scalar_Q
      - tof_to_wavelength_lin_time_zero
      - tof_to_wavelength
      - wavelength_to_d_spacing
      - wavelength_to_energy
      - wavelength_to_scalar_k
      - wavelength_to_scalar_Q

    @param funcname: The name of the axis conversion function to use
    @type funcname: C{string}

    @param number: The value and error^2 to convert
    @type number: C{tuple}

    @param som: The object containing geometry and other special information
    @type som: C{SOM.SOM}

    @param kwargs: A list of keyword arguments that the function accepts:

    @keyword inst_param: The type of parameter requested from an associated
                         instrument. For this function the acceptable
                         parameters are I{primary}, I{secondary} and I{total}.
                         Default is I{primary}.
    @type inst_param: C{string}

    @keyword pixel_id: The pixel ID from which the geometry information will
                       be retrieved from the instrument
    @type pixel_id: C{tuple}=(\"bankN\", (x, y))


    @return: A converted number for every unique spectrum
    @rtype: C{list} of C{tuple}s


    @raise AttributeError: The requested function is not in the approved
                           list
    """
    # Setup supported function list and check to see if funcname is available
    function_list = []
    function_list.append("d_spacing_to_tof_focused_det")
    function_list.append("energy_to_wavelength")
    function_list.append("frequency_to_energy")
    function_list.append("initial_wavelength_igs_lin_time_zero_to_tof")
    function_list.append("init_scatt_wavevector_to_scalar_Q")
    function_list.append("tof_to_initial_wavelength_igs_lin_time_zero")
    function_list.append("tof_to_initial_wavelength_igs")
    function_list.append("tof_to_scalar_Q")
    function_list.append("tof_to_wavelength_lin_time_zero")
    function_list.append("tof_to_wavelength")
    function_list.append("wavelength_to_d_spacing")
    function_list.append("wavelength_to_energy")
    function_list.append("wavelength_to_scalar_k")
    function_list.append("wavelength_to_scalar_Q")

    if funcname not in function_list:
        raise AttributeError("Function %s is not supported by "\
                             +"convert_single_to_list" % funcname)

    import common_lib

    # Get the common_lib function object
    func = common_lib.__getattribute__(funcname)

    # Setup inclusive dictionary containing the requested keywords for all
    # common_lib axis conversion functions

    fkwds = {}
    fkwds["pathlength"] = ()
    fkwds["polar"] = ()
    fkwds["lambda_f"] = ()
    try:
        lambda_final = som.attr_list["Wavelength_final"]
    except KeyError:
        lambda_final = None
    try:
        fkwds["time_zero_slope"] = som.attr_list["Time_zero_slope"]
    except KeyError:
        pass
    try:
        fkwds["time_zero_offset"] = som.attr_list["Time_zero_offset"]
    except KeyError:
        pass
    try:
        fkwds["time_zero"] = som.attr_list["Time_zero"]
    except KeyError:
        pass
    fkwds["dist_source_sample"] = ()
    fkwds["dist_sample_detector"] = ()
    try:
        fkwds["inst_param"] = kwargs["inst_param"]
    except KeyError:
        fkwds["inst_param"] = "primary"
    try:
        fkwds["pixel_id"] = kwargs["pixel_id"]
    except KeyError:
        fkwds["pixel_id"] = None
    fkwds["run_filter"] = False

    # Set up for working through data
    # This time highest object in the hierarchy is NOT what we need
    result = []
    res_descr = "list"

    inst = som.attr_list.instrument

    import hlr_utils

    # iterate through the values
    for i in xrange(hlr_utils.get_length(som)):
        map_so = hlr_utils.get_map_so(som, None, i)

        fkwds["pathlength"] = hlr_utils.get_parameter(fkwds["inst_param"],
                                                      map_so, inst)

        fkwds["dist_source_sample"] = hlr_utils.get_parameter(
            "primary", map_so, inst)

        fkwds["dist_sample_detector"] = hlr_utils.get_parameter(
            "secondary", map_so, inst)

        fkwds["polar"] = hlr_utils.get_parameter("polar", map_so, inst)

        fkwds["lambda_f"] = hlr_utils.get_special(lambda_final, map_so)

        value = tuple(func(number, **fkwds))

        hlr_utils.result_insert(result, res_descr, value, None, "all")

    return result