Пример #1
0
def photom_variable_star(x0,
                         y0,
                         params,
                         patch_half_width=15,
                         converge=True,
                         save_stamps=False,
                         stamp_prefix='mosaic',
                         locate=True,
                         locate_iterations=2,
                         locate_half_width=14,
                         q_sigma_threshold=1.0,
                         locate_date_range=None):

    from astropy.io import fits

    def save_mosaic(stack, nfiles, patch_size, name, diff_std, threshold):
        stamps_per_row = int(np.sqrt(nfiles))
        nrows = (nfiles - 1) / stamps_per_row + 1
        mx = stamps_per_row * (patch_size + 1) + 1
        my = nrows * (patch_size + 1) + 1
        mosaic = np.ones((my, mx)) * 1000.0
        for i in range(nfiles):
            mosaic[(i/stamps_per_row)*(patch_size+1)+1:(i/stamps_per_row+1)*(patch_size+1), \
                    (i%stamps_per_row)*(patch_size+1)+1:(i%stamps_per_row+1)*(patch_size+1)] \
                    = stack[i,:,:]
            if diff_std[i] > threshold:
                mosaic[(i/stamps_per_row)*(patch_size+1)+1:(i/stamps_per_row+1)*(patch_size+1), \
                      (i%stamps_per_row)*(patch_size+1)+1] = -1000.0
                mosaic[(i/stamps_per_row)*(patch_size+1)+1:(i/stamps_per_row+1)*(patch_size+1), \
                      (i%stamps_per_row+1)*(patch_size+1)-1] = -1000.0
                mosaic[(i/stamps_per_row)*(patch_size+1)+1, \
                      (i%stamps_per_row)*(patch_size+1)+1:(i%stamps_per_row+1)*(patch_size+1)] = -1000.0
                mosaic[(i/stamps_per_row+1)*(patch_size+1)-1, \
                      (i%stamps_per_row)*(patch_size+1)+1:(i%stamps_per_row+1)*(patch_size+1)] = -1000.0
        IO.write_image(mosaic, name)

    # Obtain a list of files

    all_files = os.listdir(params.loc_data)
    all_files.sort()
    filenames = []
    nfiles = 0

    print 'Searching in', params.loc_output, 'for', params.name_pattern

    for f in all_files:

        if fnmatch.fnmatch(f, params.name_pattern):

            basename = os.path.basename(f)
            dfile = params.loc_output + os.path.sep + 'd_' + basename
            ktable = params.loc_output + os.path.sep + 'k_' + basename

            if os.path.exists(dfile) and os.path.exists(ktable):

                nfiles += 1
                filenames.append(f)

    # Load the kernel tables
    # Load the difference images into a data cube

    print len(filenames), 'files found'

    dates = np.zeros(nfiles)
    seeing = np.zeros(nfiles)
    roundness = np.zeros(nfiles)
    bgnd = np.zeros(nfiles)
    signal = np.zeros(nfiles)
    norm_std = np.zeros(nfiles, dtype=np.float64)
    diff_std = np.zeros(nfiles, dtype=np.float64)
    n_kernel = np.zeros(nfiles, dtype=np.int32)
    n_coeffs = np.zeros(nfiles, dtype=np.int32)
    kindex_x = np.arange(0, dtype=np.int32)
    kindex_y = np.arange(0, dtype=np.int32)
    kindex_ext = np.arange(0, dtype=np.int32)
    coeffs = np.arange(0, dtype=np.float64)

    filenames.sort()

    if not converge:
        locate_iterations = 1

    threshold = -10
    for iteration in range(np.max([1, locate_iterations])):

        ix0 = np.int32(x0 + 0.5)
        iy0 = np.int32(y0 + 0.5)

        x_patch = x0 - ix0 + patch_half_width
        y_patch = y0 - iy0 + patch_half_width

        patch_size = 2 * patch_half_width + 1
        patch_slice = (ix0 - patch_half_width, ix0 + patch_half_width + 1,
                       iy0 - patch_half_width, iy0 + patch_half_width + 1)

        d_image_stack = np.zeros((nfiles, patch_size, patch_size),
                                 dtype=np.float64)
        inv_var_image_stack = np.zeros((nfiles, patch_size, patch_size),
                                       dtype=np.float64)

        for i, f in enumerate(filenames):

            basename = os.path.basename(f)
            ktable = params.loc_output + os.path.sep + 'k_' + basename
            kernelIndex, extendedBasis, c, params = IO.read_kernel_table(
                ktable, params)
            coeffs = np.hstack((coeffs, c))
            kindex_x = np.hstack((kindex_x, kernelIndex[:, 0].T))
            kindex_y = np.hstack((kindex_y, kernelIndex[:, 1].T))
            kindex_ext = np.hstack((kindex_ext, extendedBasis))
            n_kernel[i] = kernelIndex.shape[0]
            n_coeffs[i] = c.shape[0]
            dates[i] = IO.get_date(params.loc_data + os.path.sep + basename,
                                   key=params.datekey) - 2450000
            seeing[i], roundness[i], bgnd[i], signal[i] = IM.compute_fwhm(
                f, params, width=20, image_name=True)

            dfile = params.loc_output + os.path.sep + 'd_' + basename
            nfile = params.loc_output + os.path.sep + 'n_' + basename
            zfile = params.loc_output + os.path.sep + 'z_' + basename
            diff, _ = IO.read_fits_file(dfile)
            mask, _ = IO.read_fits_file(zfile)
            diff_sc = IM.undo_photometric_scale(diff, c, params.pdeg)
            diff_sc *= mask
            d_image_stack[i, :, :] = diff_sc[patch_slice[2]:patch_slice[3],
                                             patch_slice[0]:patch_slice[1]]
            norm, _ = IO.read_fits_file(nfile, slice=patch_slice)
            inv_var_image_stack[i, :, :] = (norm / d_image_stack[i, :, :])**2
            diff_std[i] = np.std(diff)
            d_image_stack[i, :, :] -= np.median(d_image_stack[i, :, :])

        if save_stamps:
            save_mosaic(
                d_image_stack, nfiles, patch_size,
                params.loc_output + os.path.sep + stamp_prefix + '.fits',
                diff_std, threshold)

        print 'kappa-clipping'
        qd1 = np.arange(len(filenames))
        #qd = np.where(diff_std[qd1]<10)
        #qd1 = qd1[qd]
        for iter in range(10):
            qd = np.where(diff_std[qd1] < np.mean(diff_std[qd1]) +
                          (4.0 - 1.5 * (iter / 9.0)) * np.std(diff_std[qd1]))
            qd1 = qd1[qd]
            print iter, np.mean(diff_std[qd1]), np.std(diff_std[qd1]), np.mean(
                diff_std[qd1]) + (4.0 - 3 *
                                  (iter / 9.0)) * np.std(diff_std[qd1])

        print 'mean(diff) :', np.mean(diff_std[qd1])
        print 'std(diff) :', np.std(diff_std[qd1])
        print '1-sig threshold:', np.mean(
            diff_std[qd1]) + 1 * np.std(diff_std[qd1])
        print '2-sig threshold:', np.mean(
            diff_std[qd1]) + 2 * np.std(diff_std[qd1])
        print '3-sig threshold:', np.mean(
            diff_std[qd1]) + 3 * np.std(diff_std[qd1])

        print '1-sig diff reject:', np.where(
            diff_std > np.mean(diff_std[qd1]) + 1 * np.std(diff_std[qd1]))
        print '2-sig diff reject:', np.where(
            diff_std > np.mean(diff_std[qd1]) + 2 * np.std(diff_std[qd1]))
        print '3-sig diff reject:', np.where(
            diff_std > np.mean(diff_std[qd1]) + 3 * np.std(diff_std[qd1]))

        threshold = np.mean(
            diff_std[qd1]) + q_sigma_threshold * np.std(diff_std[qd1])
        threshold2 = np.mean(diff_std[qd1]) + 2 * np.std(diff_std[qd1])
        threshold3 = np.mean(diff_std[qd1]) + 3 * np.std(diff_std[qd1])

        if locate_date_range is not None:
            diff_std_copy = diff_std.copy()
            diff_std = diff_std * 0.0 + 2 * threshold
            pp = np.where((dates > locate_date_range[0])
                          & (dates < locate_date_range[1]))[0]
            if pp.any():
                diff_std[pp] = diff_std_copy[pp]
            else:
                print 'Error: No images found in date range', locate_date_range
                print 'Reverting to all dates.'
                diff_std = diff_std_copy

        dsum = np.zeros((patch_size, patch_size), dtype=np.float64)
        for i in range(nfiles):
            if diff_std[i] < threshold3:
                dsum += d_image_stack[i, :, :]
        IO.write_image(
            dsum, params.loc_output + os.path.sep + 'dsum%d.fits' % iteration)
        dr = patch_half_width - int(locate_half_width)
        dsum[:dr, :] = 0.0
        dsum[-dr:, :] = 0.0
        dsum[:, :dr] = 0.0
        dsum[:, -dr:] = 0.0
        ind_dsum_max = np.unravel_index(dsum.argmax(), dsum.shape)
        print 'Iteration', iteration, ': dsum maximum located at ', ind_dsum_max

        if locate and converge:
            y0 += ind_dsum_max[0] - patch_half_width
            x0 += ind_dsum_max[1] - patch_half_width

    # Read the PSF

    psf_image = params.loc_output + os.path.sep + 'psf.fits'
    psf, psf_hdr = fits.getdata(psf_image, 0, header='true')
    psf_height = psf_hdr['PSFHEIGH']
    psf_sigma_x = psf_hdr['PAR1'] * 0.8493218
    psf_sigma_y = psf_hdr['PAR2'] * 0.8493218
    psf_x = psf_hdr['PSFX']
    psf_y = psf_hdr['PSFY']
    psf_size = psf.shape[1]
    psf_fit_rad = params.psf_fit_radius
    psf_parameters = np.array([
        psf_size, psf_height, psf_sigma_x, psf_sigma_y, psf_x, psf_y,
        psf_fit_rad, params.gain
    ]).astype(np.float64)

    if params.psf_profile_type == 'gaussian':
        psf_sigma_x = psf_hdr['PAR1'] * 0.8493218
        psf_sigma_y = psf_hdr['PAR2'] * 0.8493218
        psf_parameters = np.array([
            psf_size, psf_height, psf_sigma_x, psf_sigma_y, psf_x, psf_y,
            psf_fit_rad, params.gain
        ]).astype(np.float64)
        profile_type = 0
    elif params.psf_profile_type == 'moffat25':
        print 'params.psf_profile_type moffat25 not working yet. Exiting.'
        sys.exit(0)
        psf_sigma_x = psf_hdr['PAR1']
        psf_sigma_y = psf_hdr['PAR2']
        psf_sigma_xy = psf_hdr['PAR3']
        psf_parameters = np.array([
            psf_size, psf_height, psf_sigma_x, psf_sigma_y, psf_x, psf_y,
            psf_fit_rad, params.gain, psf_sigma_xy
        ]).astype(np.float64)
        profile_type = 1
    else:
        print 'params.psf_profile_type undefined'
        sys.exit(0)

    psf_0 = psf.astype(np.float64).copy()
    psf_xd = psf.astype(np.float64).copy() * 0.0
    psf_yd = psf.astype(np.float64).copy() * 0.0
    flux = np.zeros(nfiles, dtype=np.float64)
    dflux = np.zeros(nfiles, dtype=np.float64)

    x0_arr = np.atleast_1d(np.array([x0], dtype=np.float64))
    y0_arr = np.atleast_1d(np.array([y0], dtype=np.float64))

    cu_photom_converge(
        profile_type, patch_half_width, params.pdeg, params.sdeg, nfiles,
        n_kernel, kindex_x, kindex_y, kindex_ext, n_coeffs,
        coeffs.astype(np.float64), psf_parameters, psf_0, psf_xd, psf_yd,
        np.float64(d_image_stack.ravel()), inv_var_image_stack, diff_std,
        np.float64(threshold), x0_arr, y0_arr, x_patch, y_patch, diff.shape[1],
        diff.shape[0], 16, 16, flux, dflux, np.float64(params.gain),
        np.int32(converge), np.float64(2.5))

    if save_stamps:
        save_mosaic(
            d_image_stack, nfiles, patch_size,
            params.loc_output + os.path.sep + 'p' + stamp_prefix + '.fits',
            diff_std, threshold)

    if locate_date_range is not None:
        diff_std = diff_std_copy

    return dates, seeing, roundness, bgnd, signal, flux, dflux, diff_std / threshold, x0_arr[
        0], y0_arr[0]
Пример #2
0
def imsub_all_fits(params, reference='ref.fits'):

    #
    # Create the output directory if it doesn't exist
    #
    if not (os.path.exists(params.loc_output)):
        os.mkdir(params.loc_output)

    #
    # The degree of spatial shape changes has to be at least as
    # high as the degree of spatial photometric scale
    #
    if (params.sdeg < params.pdeg):
        print 'Increasing params.sdeg to ', params.pdeg
        params.sdeg = params.pdeg

    #
    # Print out the parameters for this run.
    #
    print 'Parameters:'
    for par in dir(params):
        print par, getattr(params, par)
    print

    #
    # Determine our list of images
    #
    all_files = os.listdir(params.loc_data)
    all_files.sort()
    files = []
    for f in all_files:
        print 'file', f
        if fnmatch.fnmatch(f, params.name_pattern):
            g = DS.Observation(params.loc_data + os.path.sep + f, params)
            del g.data
            del g.mask
            print 'fw', g.fw
            if g.fw > 0.0:
                files.append(g)
            print g.name, 'accepted'

    if len(files) < 3:
        print 'Only', len(files), 'files found matching', params.name_pattern
        print 'Exiting'
        sys.exit(0)

    #
    # Have we specified a registration template?
    #
    if params.registration_image:
        reg = DS.Observation(params.registration_image, params)
    else:
        reg = DS.EmptyBase()
        reg.fw = 999.0
        for f in files:
            if (f.fw < reg.fw) and (f.fw > params.reference_min_seeing) and (
                    f.sky < params.registration_max_background):
                reg = f

    print 'Registration image:', reg.name

    #
    # Register images
    #
    print 'Registering images'
    files_copy = [f for f in files]
    for f in files:
        print f.name
        if f == reg:
            f.image = f.data
            rf = params.loc_output + os.path.sep + 'r_' + f.name
            IO.write_image(f.image, rf)
        else:
            if not f.register(reg, params):
                files_copy.remove(f)
            # delete image arrays to save memory
            del f.image
            del f.mask
            del f.inv_variance
        del reg.data
        del reg.image
        del reg.mask
        del reg.inv_variance
    files = files_copy

    #
    # Write image names and dates to a file
    #
    if params.image_list_file:
        try:
            with open(params.loc_output + os.path.sep + params.image_list_file,
                      'w') as fid:
                for f in files:
                    date = None
                    if params.datekey:
                        date = IO.get_date(
                            params.loc_data + os.path.sep + f.name,
                            key=params.datekey) - 2450000
                    if date:
                        fid.write(f.name + '   %10.5f\n' % date)
                    else:
                        fid.write(f.name)
        except:
            raise

    #
    # Make the photometric reference image if we don't have it.
    # Find stamp positions if required.
    #
    if not (os.path.exists(params.loc_output + os.path.sep + reference)):
        print 'Reg = ', reg.name
        stamp_positions = make_reference(files,
                                         reg,
                                         params,
                                         reference_image=reference)
        ref = DS.Observation(params.loc_output + os.path.sep + reference,
                             params)
        mask, _ = IO.read_fits_file(params.loc_output + os.path.sep + 'mask_' +
                                    reference)
        variance, _ = IO.read_fits_file(params.loc_output + os.path.sep +
                                        'var_' + reference)
        ref.mask = mask
        ref.inv_variance = 1.0 / variance
        ref.register(reg, params)
    else:
        ref = DS.Observation(params.loc_output + os.path.sep + reference,
                             params)
        if os.path.exists(params.loc_output + os.path.sep + 'mask_' +
                          reference):
            mask, _ = IO.read_fits_file(params.loc_output + os.path.sep +
                                        'mask_' + reference)
        else:
            mask = np.ones_like(ref.data)
        ref.mask = mask
        ref.register(reg, params)
        stamp_positions = None
        if params.use_stamps:
            stamp_file = params.loc_output + os.path.sep + 'stamp_positions'
            if os.path.exists(stamp_file):
                stamp_positions = np.genfromtxt(stamp_file)
            else:
                stars = PF.choose_stamps(ref, params)
                stamp_positions = stars[:, 0:2]
                np.savetxt(stamp_file, stamp_positions)

    pm = params.pixel_max
    params.pixel_max *= 0.9
    ref.mask *= IM.compute_saturated_pixel_mask(ref.image, params)
    params.pixel_max = pm
    ref.blur = IM.boxcar_blur(ref.image)
    if params.mask_cluster:
        ref.mask *= IM.mask_cluster(ref.image, ref.mask, params)

    #
    # Detect stars and compute the PSF if we are doing photometry
    #
    star_positions = None
    sky = 0.0
    if params.do_photometry:
        star_file = params.loc_output + os.path.sep + 'star_positions'
        psf_file = params.loc_output + os.path.sep + 'psf.fits'
        if not (os.path.exists(psf_file)) or not (os.path.exists(star_file)):
            stars = PH.compute_psf_image(params, ref, psf_image=psf_file)
            star_positions = stars[:, 0:2]
            star_sky = stars[:, 4]
        if os.path.exists(star_file):
            star_positions = np.genfromtxt(star_file)
            star_sky = star_positions[:, 0] * 0.0
        else:
            np.savetxt(star_file, star_positions)

    print 'sky =', sky

    #
    # If we have pre-determined star positions
    #
    if params.star_file:
        stars = np.genfromtxt(params.star_file)
        star_positions = stars[:, 1:3]
        if params.star_reference_image:
            star_ref, h = IO.read_fits_file(params.star_reference_image)
            offset, _, _ = register_translation(star_ref, ref.image, 1000)
            dy, dx = offset
            #dy, dx = IM.positional_shift(ref.image,star_ref)
            print 'position shift =', dx, dy
            star_positions[:, 0] -= dx
            star_positions[:, 1] -= dy
        np.savetxt(star_file, star_positions)

    #
    # If we are using a CPU, group the stars by location
    #
    print 'Group_check'
    print 'params.do_photometry', params.do_photometry
    print 'params.use_GPU', params.use_GPU
    if params.do_photometry:
        star_group_boundaries = None
        detector_mean_positions_x = None
        detector_mean_positions_y = None
        star_unsort_index = None
        star_sort_index,star_group_boundaries,detector_mean_positions_x,detector_mean_positions_y = \
             PH.group_stars_ccd(params,star_positions,params.loc_output+os.path.sep+reference)
        star_positions = star_positions[star_sort_index]
        star_sky = star_sky[star_sort_index]
        star_unsort_index = np.argsort(star_sort_index)

    #
    # Do photometry of the reference image
    #
    if params.do_photometry:
        ref_flux_file = params.loc_output + os.path.sep + 'ref.flux'
        if not (os.path.exists(ref_flux_file)):
            result = difference_image(
                ref,
                ref,
                params,
                stamp_positions=stamp_positions,
                psf_image=psf_file,
                star_positions=star_positions,
                star_group_boundaries=star_group_boundaries,
                detector_mean_positions_x=detector_mean_positions_x,
                detector_mean_positions_y=detector_mean_positions_y,
                star_sky=star_sky)
            if isinstance(result.flux, np.ndarray):
                print 'ungrouping fluxes'
                result.flux = result.flux[star_unsort_index].copy()
                result.dflux = result.dflux[star_unsort_index].copy()
                np.savetxt(ref_flux_file,
                           np.vstack((result.flux, result.dflux)).T)

    #
    # Process images
    #

    if params.make_difference_images:

        if not (params.use_GPU) and (params.n_parallel > 1):

            pool = Pool(params.n_parallel)
            pool.map(
                process_image_helper,
                itertools.izip(
                    files,
                    itertools.repeat(
                        (ref, params, stamp_positions, star_positions,
                         star_group_boundaries, star_unsort_index,
                         detector_mean_positions_x,
                         detector_mean_positions_y))))

        else:

            for f in files:
                process_image(
                    f, (ref, params, stamp_positions, star_positions,
                        star_group_boundaries, star_unsort_index,
                        detector_mean_positions_x, detector_mean_positions_y))

    return files
Пример #3
0
def photom_variable_star(x0,y0,params,patch_half_width=15,converge=True,save_stamps=False,stamp_prefix='mosaic',locate=True,locate_iterations=2,
						  locate_half_width=14,q_sigma_threshold=1.0,locate_date_range=None):

	from astropy.io import fits
	from scipy.ndimage.filters import median_filter

	outer_radius = 15
	inner_radius = 12
	diameter = 2*outer_radius + 1
	x = np.arange(diameter)-outer_radius
	xx,yy = np.meshgrid(x,x)
	filter_kernel = np.zeros((diameter,diameter))
	filter_kernel[xx**2+yy**2<=outer_radius**2] = 1
	filter_kernel[xx**2+yy**2<=inner_radius**2] = 0


	def save_mosaic(stack,nfiles,patch_size,name,diff_std,threshold):
		stamps_per_row = int(np.sqrt(nfiles))
		nrows = (nfiles-1)/stamps_per_row+1;
		mx = stamps_per_row*(patch_size+1)+1
		my = nrows*(patch_size+1)+1
		mosaic = np.ones((my,mx))*1000.0
		for i in range(nfiles):
		  mosaic[(i/stamps_per_row)*(patch_size+1)+1:(i/stamps_per_row+1)*(patch_size+1), \
				  (i%stamps_per_row)*(patch_size+1)+1:(i%stamps_per_row+1)*(patch_size+1)] \
				  = stack[i,:,:]
		  if diff_std[i] > threshold:
			mosaic[(i/stamps_per_row)*(patch_size+1)+1:(i/stamps_per_row+1)*(patch_size+1), \
				  (i%stamps_per_row)*(patch_size+1)+1] = -1000.0
			mosaic[(i/stamps_per_row)*(patch_size+1)+1:(i/stamps_per_row+1)*(patch_size+1), \
				  (i%stamps_per_row+1)*(patch_size+1)-1] = -1000.0
			mosaic[(i/stamps_per_row)*(patch_size+1)+1, \
				  (i%stamps_per_row)*(patch_size+1)+1:(i%stamps_per_row+1)*(patch_size+1)] = -1000.0                  
			mosaic[(i/stamps_per_row+1)*(patch_size+1)-1, \
				  (i%stamps_per_row)*(patch_size+1)+1:(i%stamps_per_row+1)*(patch_size+1)] = -1000.0                  
		IO.write_image(mosaic,name)

	# Obtain a list of files

	all_files = os.listdir(params.loc_data)
	all_files.sort()
	filenames = []
	nfiles = 0

	print 'Searching in', params.loc_output, 'for', params.name_pattern

	for f in all_files:

		if fnmatch.fnmatch(f,params.name_pattern):

			basename = os.path.basename(f)
			dfile = params.loc_output+os.path.sep+'d_'+basename
			ktable = params.loc_output+os.path.sep+'k_'+basename

			if os.path.exists(dfile) and os.path.exists(ktable):

				nfiles += 1
				filenames.append(f)

	# Load the kernel tables
	# Load the difference images into a data cube

	print len(filenames), 'files found'


	dates = np.zeros(nfiles)
	seeing = np.zeros(nfiles)
	roundness = np.zeros(nfiles)
	bgnd = np.zeros(nfiles)
	signal = np.zeros(nfiles)
	norm_std = np.zeros(nfiles,dtype=np.float64)
	diff_std = np.zeros(nfiles,dtype=np.float64)
	n_kernel = np.zeros(nfiles,dtype=np.int32)
	n_coeffs = np.zeros(nfiles,dtype=np.int32)
	kindex_x = np.arange(0,dtype=np.int32)
	kindex_y = np.arange(0,dtype=np.int32)
	kindex_ext = np.arange(0,dtype=np.int32)
	coeffs = np.arange(0,dtype=np.float64)

	filenames.sort()

	if not converge:
	 	locate_iterations = 1

	threshold = -10
	for iteration in range(np.max([1,locate_iterations])):

	 	#ix0 = np.int32(x0+0.5)
	 	#iy0 = np.int32(y0+0.5)
		ix0 = np.int32(x0)
		iy0 = np.int32(y0)

		x_patch = x0 - ix0 + patch_half_width
		y_patch = y0 - iy0 + patch_half_width

		patch_size = 2*patch_half_width+1
		patch_slice = np.array([ix0-patch_half_width, ix0+patch_half_width+1, iy0-patch_half_width, iy0+patch_half_width+1])
		print 'patch_slice:', patch_slice

		# check that patch doesn't overlap the edge of the image
		f = filenames[0]
		diff, _ = IO.read_fits_file(params.loc_output+os.path.sep+'d_'+os.path.basename(f))
		nx = diff.shape[1]
		ny = diff.shape[0]
		delta_patch_x = 0
		delta_patch_y = 0
		if patch_slice[0] < 0:
			delta_patch_x = -patch_slice[0]
		elif patch_slice[1] >= nx:
			delta_patch_x = nx - patch_slice[1] - 1
		if patch_slice[2] < 0:
			delta_patch_y = -patch_slice[2]
		elif patch_slice[3] >= ny:
			delta_patch_y = ny - patch_slice[3] - 1

		print 'delta_patch_x, delta_patch_y:', delta_patch_x, delta_patch_y

		patch_slice += np.array([delta_patch_x,delta_patch_x,delta_patch_y,delta_patch_y])
		print 'patch_slice:', patch_slice

		x_patch -= delta_patch_x
		y_patch -= delta_patch_y

		d_image_stack = np.zeros((nfiles,patch_size,patch_size),dtype=np.float64)
		inv_var_image_stack = np.zeros((nfiles,patch_size,patch_size),dtype=np.float64)

		dmask = np.ones([patch_size,patch_size],dtype=np.bool)
		dmask_rad = 8.0
		dmix = np.linspace(-patch_half_width,patch_half_width,patch_size) - delta_patch_x
		dmiy = np.linspace(-patch_half_width,patch_half_width,patch_size) - delta_patch_y
		dmx, dmy = np.meshgrid(dmix,dmiy,indexing='ij')
		dmask[dmx**2 + dmy**2 < dmask_rad**2] = False

		for i, f in enumerate(filenames):

			basename = os.path.basename(f)
			ktable = params.loc_output+os.path.sep+'k_'+basename
			kernelIndex, extendedBasis, c, params = IO.read_kernel_table(ktable,params)
			coeffs = np.hstack((coeffs,c))
			kindex_x = np.hstack((kindex_x,kernelIndex[:,0].T))
			kindex_y = np.hstack((kindex_y,kernelIndex[:,1].T))
			kindex_ext = np.hstack((kindex_ext,extendedBasis))
			n_kernel[i] = kernelIndex.shape[0]
			n_coeffs[i] = c.shape[0]
			dates[i] = IO.get_date(params.loc_data+os.path.sep+basename,key=params.datekey)
			if dates[i] > 2450000:
				dates[i] -= 2450000
			seeing[i], roundness[i], bgnd[i], signal[i] = IM.compute_fwhm(f,params,width=20,image_name=True)

			dfile = params.loc_output+os.path.sep+'d_'+basename
			nfile = params.loc_output+os.path.sep+'n_'+basename
			zfile = params.loc_output+os.path.sep+'sm_'+basename
			ivfile = params.loc_output+os.path.sep+'iv_'+basename
			diff, _ = IO.read_fits_file(dfile)
			mask, _ = IO.read_fits_file(zfile)
			iv, _ = IO.read_fits_file(ivfile)
			diff_sc = IM.undo_photometric_scale(diff,c,params.pdeg)
			#diff_sc = diff
			#diff_sc -= median_filter(diff_sc,footprint=filter_kernel)
			diff_sc *= mask
			d_image_stack[i,:,:] = diff_sc[patch_slice[2]:patch_slice[3],patch_slice[0]:patch_slice[1]]
			inv_var_image_stack[i,:,:], _ = IO.read_fits_file(ivfile,slice=patch_slice)
			#inv_var_image_stack[i,:,:] = (norm / d_image_stack[i,:,:])**2
			#diff_std[i] = np.std(diff)
			diff_std[i] = np.std(d_image_stack[i,:,:][dmask])
			d_image_stack[i,:,:] -= np.median(d_image_stack[i,:,:])

		print 'kappa-clipping'
		qd = np.arange(len(filenames))
		qd1 = np.where(np.isfinite(diff_std))[0]
		for iter in range(10):
			qd = np.where(diff_std[qd1]<np.mean(diff_std[qd1])+(4.0-1.5*(iter/9.0))*np.std(diff_std[qd1]))[0]
			qd1 = qd1[qd]
			print iter, np.mean(diff_std[qd1]), np.std(diff_std[qd1]), np.mean(diff_std[qd1])+(4.0-3*(iter/9.0))*np.std(diff_std[qd1])

		print 'mean(diff) :',np.mean(diff_std[qd1])
		print 'std(diff) :',np.std(diff_std[qd1])
		print '1-sig threshold:', np.mean(diff_std[qd1])+1*np.std(diff_std[qd1])
		print '2-sig threshold:', np.mean(diff_std[qd1])+2*np.std(diff_std[qd1])
		print '3-sig threshold:', np.mean(diff_std[qd1])+3*np.std(diff_std[qd1])

		print '1-sig diff reject:',np.where(diff_std>np.mean(diff_std[qd1])+1*np.std(diff_std[qd1]))
		print '2-sig diff reject:',np.where(diff_std>np.mean(diff_std[qd1])+2*np.std(diff_std[qd1]))
		print '3-sig diff reject:',np.where(diff_std>np.mean(diff_std[qd1])+3*np.std(diff_std[qd1]))

		threshold = np.mean(diff_std[qd1])+q_sigma_threshold*np.std(diff_std[qd1])
		threshold2 = np.mean(diff_std[qd1])+2*np.std(diff_std[qd1])
		threshold3 = np.mean(diff_std[qd1])+3*np.std(diff_std[qd1])

		if locate_date_range is not None:
			diff_std_copy = diff_std.copy()
			diff_std = diff_std*0.0 + 100.0*threshold
			pp = np.where((dates>locate_date_range[0]) & (dates<locate_date_range[1]))[0]
			if pp.any():
				print 'Using images ',pp
				diff_std[pp] = diff_std_copy[pp]
			else:
				print 'Error: No images found in date range',locate_date_range
				print 'Reverting to all dates.'
				diff_std = diff_std_copy

		print 'zeros:'
		for i in range(nfiles):
			print i, np.sum(np.abs(d_image_stack[i,:,:]) < 1.e-6)
			if np.isnan(inv_var_image_stack[i,:,:]).any() or np.sum(np.abs(d_image_stack[i,:,:]) < 1.e-6) > 5:
				diff_std[i] = 100.0*threshold
				inv_var_image_stack[i,:,:] = inv_var_image_stack[i,:,:]*0.0


		if save_stamps:
			save_mosaic(d_image_stack,nfiles,patch_size,params.loc_output+os.path.sep+stamp_prefix+'.fits',diff_std,threshold)

		dsum = np.zeros((patch_size,patch_size),dtype=np.float64)
		for i in range(nfiles):
			if diff_std[i] < threshold3:
				dsum += d_image_stack[i,:,:]
		IO.write_image(dsum,params.loc_output+os.path.sep+'dsum%d.fits'%iteration)
		dr = patch_half_width-int(locate_half_width)
		print 'dr:', dr
		dsum[:dr-delta_patch_y,:] = 0.0
		dsum[-dr-delta_patch_y:,:] = 0.0
		dsum[:,:dr-delta_patch_x] = 0.0
		dsum[:,-dr-delta_patch_x:] = 0.0
		IO.write_image(dsum,params.loc_output+os.path.sep+'dsum_m%d.fits'%iteration)
		ind_dsum_max = np.unravel_index(dsum.argmax(),dsum.shape)
		print 'Iteration',iteration,': dsum maximum located at ',ind_dsum_max

		if locate and converge:
			y0 += ind_dsum_max[0] - patch_half_width + delta_patch_y
			x0 += ind_dsum_max[1] - patch_half_width + delta_patch_x


	# Read the PSF

	psf_image = params.loc_output+os.path.sep+'psf.fits'
	psf,psf_hdr = fits.getdata(psf_image,0,header='true')
	psf_height = psf_hdr['PSFHEIGH']
	psf_sigma_x = psf_hdr['PAR1']*0.8493218
	psf_sigma_y = psf_hdr['PAR2']*0.8493218
	psf_x = psf_hdr['PSFX']
	psf_y = psf_hdr['PSFY']
	psf_size = psf.shape[1]
	psf_fit_rad = params.psf_fit_radius
	psf_parameters = np.array([psf_size,psf_height,psf_sigma_x,psf_sigma_y,psf_x,
							   psf_y,psf_fit_rad,params.gain]).astype(np.float64)

	if params.psf_profile_type == 'gaussian':
		psf_sigma_x = psf_hdr['PAR1']*0.8493218
		psf_sigma_y = psf_hdr['PAR2']*0.8493218
		psf_parameters = np.array([psf_size,psf_height,psf_sigma_x,psf_sigma_y,psf_x,
								   psf_y,psf_fit_rad,params.gain]).astype(np.float64)
		profile_type = 0
	elif params.psf_profile_type == 'moffat25':
		print 'params.psf_profile_type moffat25 not working yet. Exiting.'
		sys.exit(0)
		psf_sigma_x = psf_hdr['PAR1']
		psf_sigma_y = psf_hdr['PAR2']
		psf_sigma_xy = psf_hdr['PAR3']
		psf_parameters = np.array([psf_size,psf_height,psf_sigma_x,psf_sigma_y,psf_x,
								   psf_y,
								   psf_fit_rad,params.gain,psf_sigma_xy]).astype(np.float64)
		profile_type = 1
	else:
		print 'params.psf_profile_type undefined'
		sys.exit(0)

	psf_0 = psf.astype(np.float64).copy()
	psf_xd = psf.astype(np.float64).copy()*0.0
	psf_yd = psf.astype(np.float64).copy()*0.0
	flux = np.zeros(nfiles,dtype=np.float64)
	dflux = np.zeros(nfiles,dtype=np.float64)

	x0_arr = np.atleast_1d(np.array([x0],dtype=np.float64))
	y0_arr = np.atleast_1d(np.array([y0],dtype=np.float64))

	print 'Converging photometry'
	print 'x0, y0:', x0, y0
	print 'x_patch, y_patch:', x_patch, y_patch
	good_images = np.where(diff_std < threshold)[0]
	print 'using images', good_images
	print 'threshold', threshold
	for i, f in enumerate(filenames):
		if i in good_images:
			print i, 'd_'+f, diff_std[i] 

	cu_photom_converge(profile_type, patch_half_width, params.pdeg, params.sdeg, nfiles, 
						n_kernel, kindex_x, kindex_y, kindex_ext, n_coeffs, coeffs.astype(np.float64),
						psf_parameters, psf_0, psf_xd, psf_yd,
						np.float64(d_image_stack.ravel()), np.float64(inv_var_image_stack.ravel()), diff_std, np.float64(threshold),
						x0_arr, y0_arr, x_patch, y_patch, diff.shape[1], diff.shape[0], 16, 16, flux, dflux, 
						np.float64(params.gain),np.int32(converge),np.float64(2.5))

	if save_stamps:
		save_mosaic(d_image_stack,nfiles,patch_size,params.loc_output+os.path.sep+'p'+stamp_prefix+'.fits',diff_std,threshold)

	if locate_date_range is not None:
	  diff_std = diff_std_copy

	return dates, seeing, roundness, bgnd, signal, flux, dflux, diff_std/threshold, x0_arr[0], y0_arr[0]