def make_mask(filename, ext, trail_coords, sublen=<NUM_LIT>, subwidth=<NUM_LIT:200>, order=<NUM_LIT:3>,<EOL>sigma=<NUM_LIT:4>, pad=<NUM_LIT:10>, plot=False, verbose=False):
if not HAS_OPDEP:<EOL><INDENT>raise ImportError('<STR_LIT>')<EOL><DEDENT>if verbose:<EOL><INDENT>t_beg = time.time()<EOL><DEDENT>fname = '<STR_LIT>'.format(filename, ext)<EOL>image = fits.getdata(filename, ext)<EOL>dx = image.max()<EOL>if dx <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>image = image / dx<EOL>image[image < <NUM_LIT:0>] = <NUM_LIT:0><EOL>(x0, y0), (x1, y1) = trail_coords  <EOL>rad = np.arctan2(y1 - y0, x1 - x0)<EOL>newrad = (np.pi * <NUM_LIT:2>) - rad<EOL>deg = np.degrees(rad)<EOL>if verbose:<EOL><INDENT>print('<STR_LIT>'.format(deg))<EOL><DEDENT>rotate = transform.rotate(image, deg, resize=True, order=order)<EOL>if plot and plt is not None:<EOL><INDENT>plt.ion()<EOL>mean = np.median(image)<EOL>stddev = image.std()<EOL>lower = mean - stddev<EOL>upper = mean + stddev<EOL>fig1, ax1 = plt.subplots()<EOL>ax1.imshow(image, vmin=lower, vmax=upper, cmap=plt.cm.gray)<EOL>ax1.set_title(fname)<EOL>fig2, ax2 = plt.subplots()<EOL>ax2.imshow(rotate, vmin=lower, vmax=upper, cmap=plt.cm.gray)<EOL>ax2.set_title('<STR_LIT>'.format(fname, deg))<EOL>plt.draw()<EOL><DEDENT>sx, sy = _rotate_point((x0, y0), newrad, image.shape, rotate.shape)<EOL>dx = int(subwidth / <NUM_LIT:2>)<EOL>ix0, ix1, iy0, iy1 = _get_valid_indices(<EOL>rotate.shape, sx - dx, sx + dx, sy - sublen, sy + sublen)<EOL>subr = rotate[iy0:iy1, ix0:ix1]<EOL>if len(subr) <= sublen:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(len(subr), sublen))<EOL><DEDENT>medarr = np.median(subr, axis=<NUM_LIT:1>)<EOL>flat = [medarr]<EOL>mean = sigma_clipped_stats(medarr)[<NUM_LIT:0>]<EOL>stddev = biweight_midvariance(medarr)<EOL>z = np.where(medarr > (mean + (sigma * stddev)))[<NUM_LIT:0>]<EOL>if plot and plt is not None:<EOL><INDENT>fig1, ax1 = plt.subplots()<EOL>ax1.plot(medarr, '<STR_LIT>')<EOL>ax1.plot(z, medarr[z], '<STR_LIT>')<EOL>ax1.set_xlabel('<STR_LIT>')<EOL>ax1.set_ylabel('<STR_LIT>')<EOL>ax1.set_title('<STR_LIT>')<EOL>plt.draw()<EOL><DEDENT>if len(z) < <NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(sigma))<EOL><DEDENT>lower = z.min()<EOL>upper = z.max()<EOL>diff = upper - lower<EOL>lower = lower - pad<EOL>upper = upper + pad<EOL>if plot and plt is not None:<EOL><INDENT>padind = np.arange(lower, upper)<EOL>ax1.plot(padind, medarr[padind], '<STR_LIT>')<EOL>plt.draw()<EOL><DEDENT>mask = np.zeros(rotate.shape)<EOL>lowerx, upperx, lowery, uppery = _get_valid_indices(<EOL>mask.shape, np.floor(sx - subwidth), np.ceil(sx + subwidth),<EOL>np.floor(sy - sublen + lower), np.ceil(sy - sublen + upper))<EOL>mask[lowery:uppery, lowerx:upperx] = <NUM_LIT:1><EOL>done = False<EOL>first = True<EOL>nextx = upperx  <EOL>centery = np.ceil(lowery + diff)  <EOL>counter = <NUM_LIT:0><EOL>while not done:<EOL><INDENT>ix0, ix1, iy0, iy1 = _get_valid_indices(<EOL>rotate.shape, nextx - dx, nextx + dx,<EOL>centery - sublen, centery + sublen)<EOL>subr = rotate[iy0:iy1, ix0:ix1]<EOL>if <NUM_LIT:0> in subr.shape:<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'.format(<EOL>subr.shape, first))<EOL><DEDENT>if first:<EOL><INDENT>first = False<EOL>centery = sy<EOL>nextx = sx<EOL><DEDENT>else:<EOL><INDENT>done = True<EOL><DEDENT>continue<EOL><DEDENT>medarr = np.median(subr, axis=<NUM_LIT:1>)<EOL>flat.append(medarr)<EOL>mean = sigma_clipped_stats(medarr, sigma=sigma)[<NUM_LIT:0>]<EOL>stddev = biweight_midvariance(medarr)<EOL>z = np.where(medarr > (mean + (sigma * stddev)))[<NUM_LIT:0>]<EOL>if len(z) < <NUM_LIT:1>:<EOL><INDENT>if first:<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'<EOL>'<STR_LIT>'.format(counter))<EOL><DEDENT>centery = sy<EOL>nextx = sx<EOL>first = False<EOL><DEDENT>else:<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'<EOL>'<STR_LIT>'.format(z, subr.shape))<EOL><DEDENT>done = True<EOL><DEDENT>continue<EOL><DEDENT>lower = z.min()<EOL>upper = z.max()<EOL>diff = upper - lower<EOL>lower = np.floor(lower - pad)<EOL>upper = np.ceil(upper + pad)<EOL>lowerx, upperx, lowery, uppery = _get_valid_indices(<EOL>mask.shape,<EOL>np.floor(nextx - subwidth),<EOL>np.ceil(nextx + subwidth),<EOL>np.floor(centery - sublen + lower),<EOL>np.ceil(centery - sublen + upper))<EOL>mask[lowery:uppery, lowerx:upperx] = <NUM_LIT:1><EOL>upper_p = (upperx, uppery)<EOL>upper_t = _rotate_point(<EOL>upper_p, newrad, image.shape, rotate.shape, reverse=True)<EOL>highy = np.ceil(upper_t[<NUM_LIT:1>])<EOL>highx = np.ceil(upper_t[<NUM_LIT:0>])<EOL>if first:<EOL><INDENT>nextx = nextx + dx<EOL>centery = lowery + diff  <EOL>if (nextx + subwidth) > rotate.shape[<NUM_LIT:1>]:<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'.format(counter))<EOL><DEDENT>first = False<EOL><DEDENT>elif (highy > image.shape[<NUM_LIT:0>]) or (highx > image.shape[<NUM_LIT:1>]):<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'.format(counter))<EOL><DEDENT>first = False<EOL><DEDENT>if not first:<EOL><INDENT>centery = sy<EOL>nextx = sx<EOL><DEDENT><DEDENT>else:<EOL><INDENT>nextx = nextx - dx<EOL>centery = lowery + diff  <EOL>if (nextx - subwidth) < <NUM_LIT:0>:<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'.format(counter))<EOL><DEDENT>done = True<EOL><DEDENT>elif (highy > image.shape[<NUM_LIT:0>]) or (highx > image.shape[<NUM_LIT:1>]):<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>'.format(counter))<EOL><DEDENT>done = True<EOL><DEDENT><DEDENT>counter += <NUM_LIT:1><EOL>if counter > <NUM_LIT>:<EOL><INDENT>if verbose:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>done = True<EOL><DEDENT><DEDENT>rot = transform.rotate(mask, -deg, resize=True, order=<NUM_LIT:1>)<EOL>ix0 = (rot.shape[<NUM_LIT:1>] - image.shape[<NUM_LIT:1>]) / <NUM_LIT:2><EOL>iy0 = (rot.shape[<NUM_LIT:0>] - image.shape[<NUM_LIT:0>]) / <NUM_LIT:2><EOL>lowerx, upperx, lowery, uppery = _get_valid_indices(<EOL>rot.shape, ix0, image.shape[<NUM_LIT:1>] + ix0, iy0, image.shape[<NUM_LIT:0>] + iy0)<EOL>mask = rot[lowery:uppery, lowerx:upperx]<EOL>if mask.shape != image.shape:<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(mask.shape, image.shape), AstropyUserWarning)<EOL><DEDENT>mask = mask.astype(np.bool)<EOL>if plot and plt is not None:<EOL><INDENT>test = image.copy()<EOL>test[mask] = <NUM_LIT:0><EOL>mean = np.median(test)<EOL>stddev = test.std()<EOL>lower = mean - stddev<EOL>upper = mean + stddev<EOL>fig1, ax1 = plt.subplots()<EOL>ax1.imshow(test, vmin=lower, vmax=upper, cmap=plt.cm.gray)<EOL>ax1.set_title('<STR_LIT>')<EOL>fig2, ax2 = plt.subplots()<EOL>ax2.imshow(mask, cmap=plt.cm.gray)<EOL>ax2.set_title('<STR_LIT>')<EOL>plt.draw()<EOL><DEDENT>if verbose:<EOL><INDENT>t_end = time.time()<EOL>print('<STR_LIT>'.format(t_end - t_beg))<EOL><DEDENT>return mask<EOL>
Create DQ mask for an image for a given satellite trail.
    This mask can be added to existing DQ data using :func:`update_dq`.

    .. note::

        Unlike :func:`detsat`, multiprocessing is not available for
        this function.

    Parameters
    ----------
    filename : str
        FITS image filename.

    ext : int, str, or tuple
        Extension for science data, as accepted by ``astropy.io.fits``.

    trail_coords : ndarray
        One of the trails returned by :func:`detsat`.
        This must be in the format of ``[[x0, y0], [x1, y1]]``.

    sublen : int, optional
        Length of strip to use as the fitting window for the trail.

    subwidth : int, optional
        Width of box to fit trail on.

    order : int, optional
        The order of the spline interpolation for image rotation.
        See :func:`skimage.transform.rotate`.

    sigma : float, optional
        Sigma of the satellite trail for detection. If points are
        a given sigma above the background in the subregion then it is
        marked as a satellite. This may need to be lowered for resolved
        trails.

    pad : int, optional
        Amount of extra padding in pixels to give the satellite mask.

    plot : bool, optional
        Plot the result.

    verbose : bool, optional
        Print extra information to the terminal, mostly for debugging.

    Returns
    -------
    mask : ndarray
        Boolean array marking the satellite trail with `True`.

    Raises
    ------
    ImportError
        Missing scipy or skimage>=0.11 packages.

    IndexError
        Invalid subarray indices.

    ValueError
        Image has no positive values, trail subarray too small, or
        trail profile not found.