示例#1
0
def geo_corr(ds, enabled):
    global DEFAULT_SAMPLE_TO_DETECTOR_DISTANCE
    global DEGREE_RAD_COEFFICIENT
    sdd = DEFAULT_SAMPLE_TO_DETECTOR_DISTANCE
    try:
        sdd = ds.sample_to_detector_distance
    except:
        pass
    sdds = sdd ** 2
    stth = ds.stth
    if not hasattr(stth, '__len__') :
        stth = [stth]
    is_fixed_stth = True
    if math.fabs(stth[0] - stth[-1]) > 1e-3:
        is_fixed_stth = False
    
    currentStth = stth[0] / DEGREE_RAD_COEFFICIENT
    sinStth = math.sin(currentStth)
    cosStth = math.cos(currentStth)
    
    y_len = ds.axes[-2].size - 1
    x_len = ds.axes[-1].size - 1
    
    if enabled:
        d_shape = [y_len, x_len]
        pixel_2theta = array.instance([y_len, x_len + 1], dtype = float)
        
        if is_fixed_stth :
            res = dataset.instance(ds.shape, dtype = float)
            
            y_bounds = ds.axes[-2].__iArray__
            y_centres = AxisRecord.createCentres(y_bounds)
            x_bounds = ds.axes[-1].__iArray__
            GeometryCorrection.calculateTwoThetaPixel(sdd, sdds, x_bounds, cosStth, sinStth, \
                                                 y_centres, pixel_2theta.__iArray__)
            axis_2theta = array.instance([x_len + 1], dtype = float)
            GeometryCorrection.calculateTwoThetaAxis(sdd, sdds, x_bounds, cosStth, \
                                                 sinStth, axis_2theta.__iArray__)
            relocateLeftIndexArray = array.instance(d_shape, dtype = int)
            relocateRightIndexArray = array.instance(d_shape, dtype = int)
            relocateLeftRateArray = array.instance(d_shape, dtype = float)
            relocateRightRateArray = array.instance(d_shape, dtype = float)
            relocateCounterArray = array.instance(d_shape, dtype = float)
            
            for i in xrange(ds.shape[0]):
                GeometryCorrection.rebinWithTwoTheta(ds[i].__iArray__, ds[i].var.__iArray__, \
                            res[i].__iArray__, res[i].var.__iArray__, pixel_2theta.__iArray__, \
                            axis_2theta.__iArray__, relocateLeftIndexArray.__iArray__, \
                            relocateRightIndexArray.__iArray__, relocateLeftRateArray.__iArray__, \
                            relocateRightRateArray.__iArray__, relocateCounterArray.__iArray__, \
                            i == 0)
            res.axes[0] = ds.axes[0]
            res.axes[1] = ds.axes[1]
            res.axes[2] = axis_2theta
            res.axes[2].title = 'two theta'
            res.copy_metadata_shallow(ds)
            res.append_log('Processed with: geometry curve correction')
            res.append_log('Processed with: calculating two theta on LDS=%.1f mm' % sdd)
        else:
            res = dataset.instance(ds.shape, dtype = float)
            
            y_bounds = ds.axes[-2].__iArray__
            y_centres = AxisRecord.createCentres(y_bounds)
            x_bounds = ds.axes[-1].__iArray__
            two_theta_axes = array.instance([ds.shape[0], x_len + 1], dtype = float)
            for i in xrange(ds.shape[0]):
                currentStth = stth[i] / DEGREE_RAD_COEFFICIENT
                sinStth = math.sin(currentStth)
                cosStth = math.cos(currentStth)
                pixel_2theta = array.instance([y_len, x_len + 1], dtype = float)
                GeometryCorrection.calculateTwoThetaPixel(sdd, sdds, x_bounds, cosStth, sinStth, \
                                                     y_centres, pixel_2theta.__iArray__)
                axis_2theta = array.instance([x_len + 1], dtype = float)
                GeometryCorrection.calculateTwoThetaAxis(sdd, sdds, x_bounds, cosStth, \
                                                     sinStth, axis_2theta.__iArray__)
                two_theta_axes[i] = axis_2theta
                relocateLeftIndexArray = array.instance(d_shape, dtype = int)
                relocateRightIndexArray = array.instance(d_shape, dtype = int)
                relocateLeftRateArray = array.instance(d_shape, dtype = float)
                relocateRightRateArray = array.instance(d_shape, dtype = float)
                relocateCounterArray = array.instance(d_shape, dtype = float)
                
                GeometryCorrection.rebinWithTwoTheta(ds[i].__iArray__, ds[i].var.__iArray__, \
                            res[i].__iArray__, res[i].var.__iArray__, pixel_2theta.__iArray__, \
                            axis_2theta.__iArray__, relocateLeftIndexArray.__iArray__, \
                            relocateRightIndexArray.__iArray__, relocateLeftRateArray.__iArray__, \
                            relocateRightRateArray.__iArray__, relocateCounterArray.__iArray__, \
                            True)
            res.axes[0] = ds.axes[0]
            res.axes[1] = ds.axes[1]
            res.axes[2] = two_theta_axes[0]
            res.axes[2].title = 'two theta'
            res.copy_metadata_shallow(ds)
            res.two_theta_axes = two_theta_axes
            res.append_log('Processed with: geometry curve correction')
            res.append_log('Processed with: calculating two theta on LDS=%.1f mm' % sdd)
        return res
    else:
        x_bounds = ds.axes[2].__iArray__
        two_theta_axes = array.instance([ds.shape[0], x_len + 1], dtype = float)
        for i in xrange(ds.shape[0]):
                currentStth = stth[i] / DEGREE_RAD_COEFFICIENT
                sinStth = math.sin(currentStth)
                cosStth = math.cos(currentStth)
                axis_2theta = array.instance([x_len + 1], dtype = float)
                GeometryCorrection.calculateTwoThetaAxis(sdd, sdds, x_bounds, cosStth, \
                                                     sinStth, axis_2theta.__iArray__)
                two_theta_axes[i] = axis_2theta
            
        ds.axes[2] = two_theta_axes[0]
        ds.axes[2].title = 'two theta'
        ds.two_theta_axes = two_theta_axes
        ds.append_log('Processed with: calculating two theta on LDS=%.1f mm' % sdd)
        return ds
示例#2
0
def v_intg(ds, masks):
    if len(masks) > 0 :
        is_fixed_stth = True
        if len(ds) > 1 and math.fabs(ds.stth[0] - ds.stth[-1]) > 1e-3:
            is_fixed_stth = False
        if is_fixed_stth:
            x_axis = ds.axes[-1]
            y_axis = ds.axes[-2]
            res = dataset.instance([ds.shape[0], ds.shape[2]], float('NaN'), dtype=float)
            for mask in masks:
                y_iMin = int((mask.minY - y_axis[0]) / (y_axis[-1] - y_axis[0]) \
                             * (y_axis.size - 1))
                if y_iMin < 0 :
                    y_iMin = 0
                if y_iMin >= y_axis.size:
                    continue
                y_iMax = int((mask.maxY - y_axis[0]) / (y_axis[-1] - y_axis[0]) \
                             * (y_axis.size - 1)) + 1
                if y_iMax < 0:
                    continue
                x_iMin = int((mask.minX - x_axis[0]) / (x_axis[-1] - x_axis[0]) \
                             * (x_axis.size - 1))
                if x_iMin < 0:
                    x_iMin = 0;
                if x_iMin >= x_axis.size:
                    continue
                x_iMax = int((mask.maxX - x_axis[0]) / (x_axis[-1] - x_axis[0]) \
                             * (x_axis.size - 1)) + 1
                if x_iMax < 0:
                    continue
                res[:, x_iMin : x_iMax] = ds[:, y_iMin : y_iMax, x_iMin : x_iMax].intg(1)
            res.axes[0] = ds.axes[0]
            res.axes[1] = ds.axes[2]
            mask = masks[0]
            res.copy_metadata_shallow(ds)
            res.append_log('Processed with: apply mask y in [' + str(mask.minY) + ',' \
                           + str(mask.maxY) + '], x in [' + str(mask.minX) + ',' + str(mask.maxX) + ']')
            return res
        else :
            y_axis = ds.axes[-2]
            res = dataset.instance([ds.shape[0], ds.shape[2]], float('NaN'), dtype=float)
            for i in xrange(len(ds)):
                x_axis = ds.two_theta_axes[i]
                for mask in masks:
                    y_iMin = int((mask.minY - y_axis[0]) / (y_axis[-1] - y_axis[0]) \
                                 * (y_axis.size - 1))
                    if y_iMin < 0 :
                        y_iMin = 0
                    if y_iMin >= y_axis.size:
                        continue
                    y_iMax = int((mask.maxY - y_axis[0]) / (y_axis[-1] - y_axis[0]) \
                                 * (y_axis.size - 1)) + 1
                    if y_iMax < 0:
                        continue
                    x_iMin = int((mask.minX - x_axis[0]) / (x_axis[-1] - x_axis[0]) \
                                 * (x_axis.size - 1))
                    if x_iMin < 0:
                        x_iMin = 0;
                    if x_iMin >= x_axis.size:
                        continue
                    x_iMax = int((mask.maxX - x_axis[0]) / (x_axis[-1] - x_axis[0]) \
                                 * (x_axis.size - 1)) + 1
                    if x_iMax < 0:
                        continue
                    res[i, x_iMin : x_iMax] = ds[i, y_iMin : y_iMax, x_iMin : x_iMax].intg(0)
            res.axes[0] = ds.axes[0]
            res.axes[1] = ds.axes[2]
            mask = masks[0]
            if hasattr(ds, 'two_theta_axes'):
                res.two_theta_axes = ds.two_theta_axes
            res.copy_metadata_shallow(ds)
            res.append_log('Processed with: apply mask y in [' + str(mask.minY) + ',' \
                           + str(mask.maxY) + '], x in [' + str(mask.minX) + ',' + str(mask.maxX) + ']')
            return res
    else:
        return ds.intg(1)