def find_optimal_box(box, display=False):
    actual_area = ACQUISITION_BOX_SIZE * ACQUISITION_BOX_SIZE # arcseconds^2
    
    scidata = box.get_data()

    best_box = find_optimal(scidata,
                            measure_box,
                            get_box_error_func(actual_area, box.unbinned_pixel_scale()))

    if display:
        plotting.put_plot(plot_box_measurement, (box, best_box))
    
    return MosaicTile(box, best_box)
Exemple #2
0
def find_optimal_slit(acqimage, slitxpos, slitypos, display):
    actual_width = acqimage.get_mask_width()
    
    scidata = acqimage.get_science_data()

    best_slit = find_optimal(scidata,
                             measure_slit,
                             get_error_func(actual_width, acqimage.unbinned_pixel_scale()),
                             slitxpos,
                             slitypos)

    if display:
        plotting.put_plot(plot_slit_measurement, (acqimage, best_slit))
    
    return best_slit
def find_fiber(fiberstamp, position, display_plot=False):
    fiber_data = fiberstamp.get_data()
    xmin, xmax, ymin, ymax = get_window(fiber_data, position, 10)
    debug("...searching for the fiber in the window = [%i:%i,%i:%i]" % (ymin, ymax, xmin, xmax))

    window = fiber_data[ymin:ymax,xmin:xmax]
    origin = np.array((xmin, ymin))

    cursor_point = position - origin

    debug("...contour must contain the point =", cursor_point)

    median = np.median(window)
    maximum = window.max()

    fibers = []
    detector_offset = fiberstamp.get_detector_offset()
    for contour_level in np.linspace(maximum, median):
        contours = measure.find_contours(window, contour_level)

        for contour in contours:
            if not is_closed_contour(contour):
                continue
            
            fiber = Fiber(detector_offset, origin, contour, contour_level)
            if not fiber.bounding_box_contains(cursor_point):
                continue

            fibers.append(fiber)

    def compare_fibers(fiber1, fiber2):
        diff1 = abs(fiber1.get_area() - FIBER_AREA)
        diff2 = abs(fiber2.get_area() - FIBER_AREA)
        return cmp(diff1, diff2)

    fibers.sort(cmp=compare_fibers)

    if display_plot:
        for fiber in fibers:
            plotting.put_plot(plot_fiber_measurement, (window, fiber, FIBER_AREA))
            break

    if fibers:
        return fibers[0]
    
    return None
Exemple #4
0
def get_selection_peak(input_data, curcrds, pixscale,
                       verbose=False, pixel_buffer=None, cursor_listener=None, radial=False):
    seeing_estimate = 0.8
    default_fwhm = seeing_estimate / pixscale

    if pixel_buffer is None:
        pixel_buffer = max(int(round(default_fwhm)), 1)

    best_selection = None
    prev_shape = (None, None)
    
    for i in range(10):
        debug("...using a pixel_buffer =", pixel_buffer)
        xmin, xmax, ymin, ymax = get_window(input_data, curcrds, pixel_buffer)

        stamp_data = input_data[ymin:ymax, xmin:xmax]
        window = (ymin, ymax, xmin, xmax)

        map(assert_int, window)

        debug("...moffat optimization for the data in the window [%i:%i,%i:%i]" % window)

        ydim, xdim = stamp_data.shape
        assert ydim != 0
        assert xdim != 0

        if prev_shape == stamp_data.shape:
            debug("...already attempted to fit a function to a window this size, terminating search.")
            break
        prev_shape = stamp_data.shape

        # fit the subset of data to a moffat function
        fitted_function = get_fitted_function(stamp_data, default_fwhm)
        
        center_x, center_y = fitted_function.get_center()
        # convert to 1-based indicing and original image
        measured_center = np.array([xmin + center_x + 1, ymin + center_y + 1])
        selection = Selection(measured_center, window, fitted_function)

        if is_debug_mode():
            plotting.put_plot(plot_selection, (selection, pixscale, cursor_listener))

        # note, the first time through the loop best_selection is None, this is ok!
        best_selection = max(best_selection, selection)

        if fitted_function.get_success() <= 3:
            debug("...moffat optimization converged.")
            break

        rsquared = fitted_function.get_rsquared()
        if rsquared >= 0.90:
            debug("...moffat optimization did not converge, but R^2 is greater than 0.9 (%.2f)" % rsquared)
            break

        debug("...moffat optimization did not converge.")
        mindim = min(input_data.shape)
        if mindim > 300:
            if pixel_buffer > int(float(mindim) * 0.1):
                debug("...window is already greater than 20% of the field of view, not going to try again.")
                break
        
        debug("...doubling the window size and trying again...")
        pixel_buffer *= 2

    if verbose and (not is_debug_mode() or radial):
        plotting.put_plot(plot_selection, (best_selection, pixscale, cursor_listener))
        if radial:
            plotting.put_plot(plot_radial, (best_selection, pixscale))

    return best_selection