def _slice_from_path(x, y, data, attribute, slc): """ Extract a PV-like slice from a cube :param x: An array of x values to extract (pixel units) :param y: An array of y values to extract (pixel units) :param data: :class:`~glue.core.data.Data` :param attribute: :claass:`~glue.core.data.Component` :param slc: orientation of the image widget that `pts` are defined on :returns: (slice, x, y) slice is a 2D Numpy array, corresponding to a "PV ribbon" cutout from the cube x and y are the resampled points along which the ribbon is extracted :note: For >3D cubes, the "V-axis" of the PV slice is the longest cube axis ignoring the x/y axes of `slc` """ from glue.external.pvextractor import Path, extract_pv_slice p = Path(list(zip(x, y))) cube = data[attribute] dims = list(range(data.ndim)) s = list(slc) ind = _slice_index(data, slc) cube_wcs = getattr(data.coords, 'wcs', None) # transpose cube to (z, y, x, <whatever>) def _swap(x, s, i, j): x[i], x[j] = x[j], x[i] s[i], s[j] = s[j], s[i] _swap(dims, s, ind, 0) _swap(dims, s, s.index('y'), 1) _swap(dims, s, s.index('x'), 2) cube = cube.transpose(dims) # slice down from >3D to 3D if needed s = [slice(None)] * 3 + [slc[d] for d in dims[3:]] cube = cube[s] # sample cube spacing = 1 # pixel x, y = [np.round(_x).astype(int) for _x in p.sample_points(spacing)] try: result = extract_pv_slice(cube, path=p, wcs=cube_wcs, order=0) except: # sometimes pvextractor complains due to wcs. Try to recover result = extract_pv_slice(cube, path=p, wcs=None, order=0) from astropy.wcs import WCS data = result.data wcs = WCS(result.header) return data, x, y, wcs