예제 #1
0
def align(object_table):
    """
    Align images from image_list.

    Args:
        object_table: a list of file names

    Returns:
        a table of objects that have matching field of view
    """

    # use file list to make data objects
    object_table = makedata(object_table)
    data = object_table['data']

    # sort table by date
    object_table.sort('date')

    # pick reference image
    ref_index = len(data) // 2
    ref_img = data[ref_index]

    # align images to reference image
    aligned = []
    for item in object_table['data']:
        aligned.append(aa.align_image(ref_img, item))
    object_table['aligned'] = aligned

    return object_table
예제 #2
0
def _align_for_coadd(imglist):
    """
    Function to align a group of images for coadding, it uses
    the astroalign `align_image` tool.
    """
    ref = imglist[0]
    new_list = [ref]
    for animg in imglist[1:]:
        new_img = aa.align_image(
            ref.data.astype(float), animg.data.astype(float)
        )
        new_list.append(type(animg)(new_img))
    return new_list
예제 #3
0
def _align_for_diff_crop(refpath, newpath, bordersize=50):
    """Function to align two images using their paths,
    and returning newpaths for differencing.
    We will allways rotate and align the new image to the reference,
    so it is easier to compare differences along time series.

    This special function differs from aligh_for_diff since it
    crops the images, so they do not have borders with problems.
    """
    ref = fits.getdata(refpath)
    hdr_ref = fits.getheader(refpath)

    dest_file_ref = "cropped_" + os.path.basename(refpath)
    dest_file_ref = os.path.join(os.path.dirname(refpath), dest_file_ref)

    hdr_ref.set("comment", "cropped img " + refpath + " to " + newpath)
    ref2 = ref[bordersize:-bordersize, bordersize:-bordersize]
    fits.writeto(dest_file_ref, ref2, hdr_ref, overwrite=True)

    new = fits.getdata(newpath)
    hdr_new = fits.getheader(newpath)

    dest_file_new = "aligned_" + os.path.basename(newpath)
    dest_file_new = os.path.join(os.path.dirname(newpath), dest_file_new)

    try:
        new2 = aa.align_image(ref, new)
    except ValueError:
        ref = ref.astype(float)
        new = new.astype(float)
        new2 = aa.align_image(ref, new)

    hdr_new.set("comment", "aligned img " + newpath + " to " + refpath)
    new2 = new2[bordersize:-bordersize, bordersize:-bordersize]
    fits.writeto(dest_file_new, new2, hdr_new, overwrite=True)

    return [dest_file_new, dest_file_ref]
예제 #4
0
def align_for_diff(refpath, newpath):
    """Function to align two images using their paths,
    and returning newpaths for differencing.
    We will allways rotate and align the new image to the reference,
    so it is easier to compare differences along time series.
    """
    ref = fits.getdata(refpath)
    new = fits.getdata(newpath)
    hdr = fits.getheader(newpath)

    dest_file = 'aligned_'+os.path.basename(newpath)
    dest_file = os.path.join(os.path.dirname(newpath), dest_file)

    try:
        new2 = aa.align_image(ref, new)
    except ValueError:
        ref = ref.astype(float)
        new = new.astype(float)
        new2 = aa.align_image(ref, new)

    hdr.set('comment', 'aligned img '+newpath+' to '+refpath)
    fits.writeto(dest_file, new2, hdr, overwrite=True)

    return dest_file
예제 #5
0
 def testalignment(image_input, ref_input):
     image_aligned = astroalign.align_image(ref_input, image_input)
     self.assertIs(type(image_aligned), type(ref_input))
     fraction = compare_image(image_aligned)
     self.assertGreater(fraction, 0.85)
예제 #6
0
    def test_align_image(self):
        def compare_image(the_image):
            """Return the fraction of sources found in the original image"""
            # pixel comparison is not good, doesn't work. Compare catalogs.
            if isinstance(the_image, np.ma.MaskedArray):
                full_algn = the_image.filled(fill_value=np.median(the_image))\
                    .astype('float32')
            else:
                full_algn = the_image.astype('float32')
            full_algn[the_image == 0] = np.median(the_image)
            import sep
            bkg = sep.Background(full_algn)
            thresh = 3.0 * bkg.globalrms
            allobjs = sep.extract(full_algn - bkg.back(), thresh)
            allxy = np.array([[obj['x'], obj['y']] for obj in allobjs])

            from scipy.spatial import KDTree
            ref_coordtree = KDTree(self.star_new_pos)

            # Compare here srcs list with self.star_ref_pos
            num_sources = 0
            for asrc in allxy:
                found_source = ref_coordtree.query_ball_point(asrc, 3)
                if found_source:
                    num_sources += 1
            fraction_found = float(num_sources) / float(len(allxy))
            return fraction_found

        image_aligned = astroalign.align_image(self.image_ref, self.image)
        # Test that image returned is not masked
        self.assertIs(type(image_aligned), np.ndarray)
        fraction = compare_image(image_aligned)
        self.assertGreater(fraction, 0.85)

        # Test masked arrays
        # Make some masks...
        mask = np.zeros(self.image.shape, dtype='bool')
        mask[self.h / 10:self.h / 10 + 10, :] = True
        mask_ref = np.zeros(self.image_ref.shape, dtype='bool')
        mask_ref[:, self.w / 10:self.w / 10 + 10] = True
        image_masked = np.ma.array(self.image, mask=mask)
        image_ref_masked = np.ma.array(self.image_ref, mask=mask_ref)

        def testalignment(image_input, ref_input):
            image_aligned = astroalign.align_image(ref_input, image_input)
            self.assertIs(type(image_aligned), type(ref_input))
            fraction = compare_image(image_aligned)
            self.assertGreater(fraction, 0.85)

        # Test it works with masked image:
        testalignment(image_masked, self.image_ref)

        # Test it works with masked ref:
        testalignment(self.image, image_ref_masked)

        # Test it works with both masked image and masked ref:
        testalignment(image_masked, image_ref_masked)

        # Test it works when given a masked array with no mask set
        testalignment(np.ma.array(self.image), self.image_ref)

        # Test it works when given a reference masked array with no mask set
        testalignment(self.image, np.ma.array(self.image_ref))

        # Test if it works when both images are masked, but with no mask set
        testalignment(np.ma.array(self.image), np.ma.array(self.image_ref))