Esempio n. 1
0
def compute_oblique_section(item, obj):
    """Return oblique averaged cross section"""
    global TEMP_ITEM
    
    xa, ya, xb, yb = obj.get_bounding_rect_coords()
    x0, y0, x1, y1, x2, y2, x3, y3 = obj.get_rect()

    getcpi = item.get_closest_pixel_indexes
    ixa, iya = getcpi(xa, ya)
    ixb, iyb = getcpi(xb, yb)
    ix0, iy0 = getcpi(x0, y0)
    ix1, iy1 = getcpi(x1, y1)
    ix3, iy3 = getcpi(x3, y3)
    
    destw = vector_norm(ix0, iy0, ix1, iy1)
    desth = vector_norm(ix0, iy0, ix3, iy3)
    ysign = -1 if obj.plot().get_axis_direction('left') else 1
    angle = vector_angle(ix1-ix0, (iy1-iy0)*ysign)
    
    dst_rect = (0, 0, int(destw), int(desth))
    dst_image = np.empty((desth, destw), dtype=np.float64)
    
    if isinstance(item.data, np.ma.MaskedArray):
        if item.data.dtype in (np.float32, np.float64):
            item_data = item.data
        else:
            item_data = np.ma.array(item.data, dtype=np.float32, copy=True)
        data = np.ma.filled(item_data, np.nan)
    else:
        data = item.data
    
    ixr = .5*(ixb+ixa)
    iyr = .5*(iyb+iya)
    mat = translate(ixr, iyr)*rotate(-angle)*translate(-.5*destw, -.5*desth)
    _scale_tr(data, mat, dst_image, dst_rect,
              (1., 0., np.nan), (INTERP_LINEAR,))

    if DEBUG:
        plot = obj.plot()
        if TEMP_ITEM is None:
            from guiqwt.builder import make
            TEMP_ITEM = make.image(dst_image)
            plot.add_item(TEMP_ITEM)
        else:
            TEMP_ITEM.set_data(dst_image)
        if False:
            TEMP_ITEM.imageparam.alpha_mask = True
            xmin, ymin = ixa, iya
            xmax, ymax = xmin+destw, ymin+desth
            TEMP_ITEM.imageparam.xmin = xmin
            TEMP_ITEM.imageparam.xmax = xmax
            TEMP_ITEM.imageparam.ymin = ymin
            TEMP_ITEM.imageparam.ymax = ymax
            TEMP_ITEM.imageparam.update_image(TEMP_ITEM)
        plot.replot()
    
    ydata = np.ma.fix_invalid(dst_image, copy=DEBUG).mean(axis=1)
    xdata = item.get_x_values(0, ydata.size)[:ydata.size]
    try:
        xdata -= xdata[0]
    except IndexError:
        print(xdata, ydata)
    return xdata, ydata
Esempio n. 2
0
def compute_oblique_section(item, obj):
    """Return oblique averaged cross section"""
    global TEMP_ITEM

    xa, ya, xb, yb = obj.get_bounding_rect_coords()
    x0, y0, x1, y1, x2, y2, x3, y3 = obj.get_rect()

    getcpi = item.get_closest_pixel_indexes
    ixa, iya = getcpi(xa, ya)
    ixb, iyb = getcpi(xb, yb)
    ix0, iy0 = getcpi(x0, y0)
    ix1, iy1 = getcpi(x1, y1)
    ix3, iy3 = getcpi(x3, y3)

    destw = vector_norm(ix0, iy0, ix1, iy1)
    desth = vector_norm(ix0, iy0, ix3, iy3)
    ysign = -1 if obj.plot().get_axis_direction('left') else 1
    angle = vector_angle(ix1 - ix0, (iy1 - iy0) * ysign)

    dst_rect = (0, 0, int(destw), int(desth))
    dst_image = np.empty((desth, destw), dtype=np.float64)

    if isinstance(item.data, np.ma.MaskedArray):
        if item.data.dtype in (np.float32, np.float64):
            item_data = item.data
        else:
            item_data = np.ma.array(item.data, dtype=np.float32, copy=True)
        data = np.ma.filled(item_data, np.nan)
    else:
        data = item.data

    ixr = .5 * (ixb + ixa)
    iyr = .5 * (iyb + iya)
    mat = translate(ixr, iyr) * rotate(-angle) * translate(
        -.5 * destw, -.5 * desth)
    _scale_tr(data, mat, dst_image, dst_rect, (1., 0., np.nan),
              (INTERP_LINEAR, ))

    if DEBUG:
        plot = obj.plot()
        if TEMP_ITEM is None:
            from guiqwt.builder import make
            TEMP_ITEM = make.image(dst_image)
            plot.add_item(TEMP_ITEM)
        else:
            TEMP_ITEM.set_data(dst_image)
        if False:
            TEMP_ITEM.imageparam.alpha_mask = True
            xmin, ymin = ixa, iya
            xmax, ymax = xmin + destw, ymin + desth
            TEMP_ITEM.imageparam.xmin = xmin
            TEMP_ITEM.imageparam.xmax = xmax
            TEMP_ITEM.imageparam.ymin = ymin
            TEMP_ITEM.imageparam.ymax = ymax
            TEMP_ITEM.imageparam.update_image(TEMP_ITEM)
        plot.replot()

    ydata = np.ma.fix_invalid(dst_image, copy=DEBUG).mean(axis=1)
    xdata = item.get_x_values(0, ydata.size)[:ydata.size]
    try:
        xdata -= xdata[0]
    except IndexError:
        print(xdata, ydata)
    return xdata, ydata