예제 #1
0
    def test_image_is_added_to_small_canvas(self, image_hdu_rect,
                                            image_hdu_square):
        im_hdu = image_hdu_rect
        im_hdu.header["CRVAL1"] -= 150 * u.arcsec.to(u.deg)
        im_hdu.header["CRVAL2"] += 40 * u.arcsec.to(u.deg)
        hdr = imp_utils.get_canvas_header([im_hdu, image_hdu_square])

        im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"]))
        canvas_hdu = fits.ImageHDU(header=hdr, data=im)
        canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(im_hdu, canvas_hdu)
        canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(
            image_hdu_square, canvas_hdu)

        flux = np.sum(im_hdu.data) + np.sum(image_hdu_square.data)
        assert np.sum(canvas_hdu.data) == approx(flux, rel=1e-2)

        if PLOTS:
            for im in [im_hdu, image_hdu_square]:
                x, y = imp_utils.calc_footprint(im.header)
                x, y = imp_utils.val2pix(canvas_hdu.header, x, y)
                plt.plot(x, y, "r-")

            x0, y0 = imp_utils.val2pix(canvas_hdu.header, 0, 0)
            plt.plot(x0, y0, "ro")
            plt.gca().set_aspect(1)

            plt.imshow(canvas_hdu.data, origin="lower")

            plt.show()
예제 #2
0
    def test_mm_image_and_tables_on_large_canvas(self, input_table_mm,
                                                 image_hdu_rect_mm,
                                                 image_hdu_square_mm):
        image_hdu_rect = image_hdu_rect_mm
        image_hdu_square = image_hdu_square_mm
        tbl1 = deepcopy(input_table_mm)
        tbl2 = deepcopy(input_table_mm)

        tbl1["y_mm"] -= 100
        tbl2["x_mm"] += 100
        tbl2["y_mm"] += 100

        im_hdu = image_hdu_rect
        im_hdu.header["CRVAL1D"] -= 150
        im_hdu.header["CRVAL2D"] += 20

        hdr = imp_utils.get_canvas_header(
            [im_hdu, image_hdu_square, tbl1, tbl2], pixel_scale=3 * u.mm)
        im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"]))
        canvas_hdu = fits.ImageHDU(header=hdr, data=im)

        canvas_hdu = imp_utils.add_table_to_imagehdu(tbl1,
                                                     canvas_hdu,
                                                     wcs_suffix="D")
        canvas_hdu = imp_utils.add_table_to_imagehdu(tbl2,
                                                     canvas_hdu,
                                                     wcs_suffix="D")
        canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(im_hdu,
                                                        canvas_hdu,
                                                        wcs_suffix="D")
        canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(image_hdu_square,
                                                        canvas_hdu,
                                                        wcs_suffix="D")

        total_flux = np.sum(tbl1["flux"]) + np.sum(tbl2["flux"]) + \
                     np.sum(im_hdu.data) + np.sum(image_hdu_square.data)
        assert np.sum(canvas_hdu.data) == approx(total_flux)

        if PLOTS:

            for im in [im_hdu, image_hdu_square]:
                x, y = imp_utils.calc_footprint(im, "D")
                x, y = imp_utils.val2pix(canvas_hdu, x, y, "D")
                plt.plot(x, y, "r-")

            x0, y0 = imp_utils.val2pix(canvas_hdu, 0, 0, "D")
            plt.plot(x0, y0, "ro")
            plt.gca().set_aspect(1)

            plt.imshow(canvas_hdu.data, origin="lower")

            plt.show()
예제 #3
0
    def test_mm_image_is_added_to_small_canvas(self, image_hdu_rect_mm,
                                               image_hdu_square_mm):
        im_hdu = image_hdu_rect_mm
        im_hdu.header["CRVAL1D"] -= 150
        im_hdu.header["CRVAL2D"] += 40
        hdr = imp_utils.get_canvas_header([im_hdu, image_hdu_square_mm],
                                          pixel_scale=1 * u.mm)

        im = np.zeros((hdr["NAXIS2"], hdr["NAXIS1"]))
        canvas_hdu = fits.ImageHDU(header=hdr, data=im)
        canvas_hdu = imp_utils.add_imagehdu_to_imagehdu(im_hdu,
                                                        canvas_hdu,
                                                        wcs_suffix="D")

        assert np.sum(canvas_hdu.data) == approx(np.sum(im_hdu.data))

        if PLOTS:
            for im in [im_hdu, image_hdu_square_mm]:
                x, y = imp_utils.calc_footprint(im.header, "D")
                x, y = imp_utils.val2pix(canvas_hdu.header, x, y, "D")
                plt.plot(x, y, "r-")

            x0, y0 = imp_utils.val2pix(canvas_hdu.header, 0, 0, "D")
            plt.plot(x0, y0, "ro")
            plt.gca().set_aspect(1)

            plt.imshow(canvas_hdu.data, origin="lower")

            plt.show()
예제 #4
0
def combine_imagehdu_fields(fov_header, src, fields_indexes, wave_min, wave_max,
                            area, wcs_suffix=""):
    """
    Combines a list of ImageHDUs into a single one bounded by the Header WCS

    Parameters
    ----------
    fov_header : fits.Header
        Header from the FieldOfView
    src : Source object
    fields_indexes : list of ints
        Which indexes from <Source>.fields to use
    wave_min : float
        [deg] Blue spectral border
    wave_max : float
        [deg] Red spectral border
    area : float
        [m2] Area of the primary aperture
    wcs_suffix : str
        Which coordinate system to use
        - "" for the on-sky coordinate system
        - "D" for the image-plane coordinate system

    Returns
    -------
    canvas_hdu : fits.ImageHDU

    """

    image = np.zeros((fov_header["NAXIS2"], fov_header["NAXIS1"]))
    canvas_hdu = fits.ImageHDU(header=fov_header, data=image)
    spline_order = utils.from_currsys("!SIM.computing.spline_order")
    pixel_area = fov_header["CDELT1"] * fov_header["CDELT2"] * \
                 u.Unit(fov_header["CUNIT1"]).to(u.arcsec) ** 2

    for ii in fields_indexes:
        field = src.fields[ii]
        if isinstance(field, fits.ImageHDU):
            ref = field.header["SPEC_REF"]
            flux = src.photons_in_range(wave_min, wave_max, area, indexes=[ref])
            image = np.zeros((fov_header["NAXIS2"], fov_header["NAXIS1"]))
            temp_hdu = fits.ImageHDU(header=fov_header, data=image)

            if field.header.get("BG_SRC", False) and \
                    field.header["NAXIS1"] <= 1 and \
                    field.header["NAXIS2"] <= 1:
                # .. todo: check if we need to take pixel_scale into account
                temp_hdu.data += flux[0].value * pixel_area
            else:
                temp_hdu = imp_utils.add_imagehdu_to_imagehdu(
                    field, temp_hdu, spline_order, wcs_suffix)
                temp_hdu.data *= flux[0].value

            canvas_hdu.data += temp_hdu.data

    return canvas_hdu
예제 #5
0
    def test_larger_hdu_encompases_smaller_hdu(self):
        """monochrome box"""
        big, small = self.big_small_hdus()
        big_sum, small_sum = np.sum(big.data), np.sum(small.data)

        new = imp_utils.add_imagehdu_to_imagehdu(big, small)

        if PLOTS:
            plt.imshow(new.data, origin="lower")
            plt.show()

        assert np.sum(new.data) == 2 * small_sum
예제 #6
0
    def test_smaller_hdu_is_fully_in_larger_hdu(self):
        """yellow box in box"""
        big, small = self.big_small_hdus()
        big_sum, small_sum = np.sum(big.data), np.sum(small.data)

        new = imp_utils.add_imagehdu_to_imagehdu(small, big)

        if PLOTS:
            plt.imshow(new.data, origin="lower")
            plt.show()

        assert np.sum(new.data) == big_sum + small_sum
예제 #7
0
    def test_larger_hdu_is_fully_outside_smaller_hdu(self):
        """monochrome box"""
        big, small = self.big_small_hdus(small_offsets=(15, 0))
        big_sum, small_sum = np.sum(big.data), np.sum(small.data)

        new = imp_utils.add_imagehdu_to_imagehdu(big, small)

        if PLOTS:
            plt.imshow(new.data, origin="lower")
            plt.show()

        assert np.sum(new.data) == small_sum
예제 #8
0
def combine_imagehdu_fields(fov_header,
                            src,
                            fields_indexes,
                            wave_min,
                            wave_max,
                            area,
                            wcs_suffix=""):
    """
    Combines a list of ImageHDUs into a single one bounded by the Header WCS

    Parameters
    ----------
    fov_header : fits.Header
        Header from the FieldOfView
    src : Source object
    fields_indexes : list of ints
        Which indexes from <Source>.fields to use
    wave_min : float
        [deg] Blue spectral border
    wave_max : float
        [deg] Red spectral border
    area : float
        [m2] Area of the primary aperture
    wcs_suffix : str
        Which coordinate system to use
        - "" for the on-sky coordinate system
        - "D" for the image-plane coordinate system

    Returns
    -------
    canvas_hdu : fits.ImageHDU

    """

    image = np.zeros((fov_header["NAXIS2"], fov_header["NAXIS1"]))
    canvas_hdu = fits.ImageHDU(header=fov_header, data=image)
    order = int(rc.__config__["!SIM.computing.spline_order"])

    for ii in fields_indexes:
        if isinstance(src.fields[ii], fits.ImageHDU):
            ref = src.fields[ii].header["SPEC_REF"]
            flux = src.photons_in_range(wave_min,
                                        wave_max,
                                        area,
                                        indexes=[ref])
            image = np.zeros((fov_header["NAXIS2"], fov_header["NAXIS1"]))
            temp_hdu = fits.ImageHDU(header=fov_header, data=image)
            temp_hdu = imp_utils.add_imagehdu_to_imagehdu(
                src.fields[ii], temp_hdu, order=order, wcs_suffix=wcs_suffix)
            canvas_hdu.data += temp_hdu.data * flux[0].value

    return canvas_hdu
예제 #9
0
    def test_smaller_hdu_is_partially_in_larger_hdu(self):
        """yellow quarter top-right"""
        big, small = self.big_small_hdus(small_wh=(20, 10),
                                         small_offsets=(10, 5))
        big_sum, small_sum = np.sum(big.data), np.sum(small.data)

        new = imp_utils.add_imagehdu_to_imagehdu(small, big)

        if PLOTS:
            plt.imshow(new.data, origin="lower")
            plt.show()

        assert np.sum(new.data) == 1.25 * big_sum
예제 #10
0
    def test_smaller_cube_is_fully_inside_larger_cube(self):
        """yellow box in box"""
        big, small = self.big_small_hdus()
        big.data = big.data[None, :, :] * np.ones(3)[:, None, None]
        small.data = small.data[None, :, :] * np.ones(3)[:, None, None]

        big_sum, small_sum = np.sum(big.data), np.sum(small.data)

        new = imp_utils.add_imagehdu_to_imagehdu(small, big)

        if PLOTS:
            plt.imshow(new.data[1, :, :], origin="lower")
            plt.show()

        assert np.sum(new.data) == big_sum + small_sum
예제 #11
0
    def test_image_added_conserves_flux(self, angle, image_hdu_square):
        canvas = deepcopy(image_hdu_square)
        canvas.data = np.zeros((200, 200))
        canvas.header["CRPIX1"] *= 2
        canvas.header["CRPIX2"] *= 2

        angle = np.deg2rad(angle)
        image_hdu_square.data = np.ones((100, 100))
        image_hdu_square.header["PC1_1"] = np.cos(angle)
        image_hdu_square.header["PC1_2"] = np.sin(angle)
        image_hdu_square.header["PC2_1"] = -np.sin(angle)
        image_hdu_square.header["PC2_2"] = np.cos(angle)

        canvas = imp_utils.add_imagehdu_to_imagehdu(image_hdu_square, canvas)
        assert np.isclose(np.sum(canvas.data), np.sum(image_hdu_square.data))
예제 #12
0
    def test_larger_cube_is_partially_in_smaller_cube(self):
        """yellow quarter bottom-left"""
        big, small = self.big_small_hdus(small_wh=(20, 10),
                                         small_offsets=(10, 5))
        big.data = big.data[None, :, :] * np.ones(3)[:, None, None]
        small.data = small.data[None, :, :] * np.ones(3)[:, None, None]

        big_sum, small_sum = np.sum(big.data), np.sum(small.data)
        new = imp_utils.add_imagehdu_to_imagehdu(big, small)

        if PLOTS:

            plt.imshow(new.data[1, :, :], origin="lower")
            plt.show()

        assert np.sum(new.data) == 1.25 * big_sum
예제 #13
0
    def test_add_one_image_to_another_in_the_right_position(self):

        big = imp_utils.header_from_list_of_xy(x=np.array([-20, -20, 20, 20]),
                                               y=np.array([10, -10, -10, 10]),
                                               pixel_scale=0.1)
        im = np.zeros([big["NAXIS2"], big["NAXIS1"]])
        big = fits.ImageHDU(header=big, data=im)

        small = imp_utils.header_from_list_of_xy(
            x=np.array([-3, -3, 3, 3]) + 10,
            y=np.array([1, -1, -1, 1]) - 5,
            pixel_scale=0.1)
        im = np.ones([small["NAXIS2"], small["NAXIS1"]])
        small = fits.ImageHDU(header=small, data=im)

        big = imp_utils.add_imagehdu_to_imagehdu(small, big)
        ycen, xcen = np.array(big.data.shape) // 2
        assert np.sum(big.data[:ycen, xcen:]) == np.sum(small.data)

        if PLOTS:
            plt.imshow(big.data, origin="lower")
            plt.show()