def _basic_fov():
    src = _image_source()
    fov = FieldOfView(_basic_fov_header(),
                      waverange=[1, 2] * u.um,
                      area=1 * u.m**2)
    fov.extract_from(src)

    return fov
    def test_ignores_fields_outside_fov_boundary(self, basic_fov_header):

        src = _combined_source(dx=[200, 200, 200])
        src.fields[0]["x"] += 200

        the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=1 * u.m**2)
        the_fov.extract_from(src)

        assert len(the_fov.fields) == 0
    def test_contains_cube_when_passed_a_cube_source(self, basic_fov_header,
                                                     cube_source):
        src = cube_source
        the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=1 * u.m**2)
        the_fov.extract_from(src)

        assert len(the_fov.fields) == 1
        assert len(the_fov.spectra) == 0
        assert len(the_fov.fields[0].data.shape) == 3
 def test_contains_all_fields_inside_fov(self, basic_fov_header,
                                         cube_source, image_source,
                                         table_source):
     src = image_source + cube_source + table_source
     the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=1 * u.m**2)
     the_fov.extract_from(src)
     assert len(the_fov.fields) == 3
     assert isinstance(the_fov.fields[0], fits.ImageHDU)
     assert isinstance(the_fov.fields[1], fits.ImageHDU)
     assert the_fov.fields[1].header["NAXIS"] == 3
     assert isinstance(the_fov.fields[2], Table)
    def test_views_with_only_table(self, basic_fov_header):
        src = _table_source()
        fluxes = src.photons_in_range(1 * u.um, 2 * u.um)
        phs = fluxes[src.fields[0]["ref"]] * src.fields[0]["weight"]

        the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=1 * u.m**2)
        the_fov.extract_from(src)
        view = the_fov.view()

        assert np.sum(view) == np.sum(phs).value - 4

        if PLOTS:
            plt.imshow(view.T, origin="lower", norm=LogNorm())
            plt.colorbar()
            plt.show()
    def test_can_extract_the_source_in_a_fov(self, fov_hdr, comb_src,
                                             implane_hdr):

        from scipy.ndimage.interpolation import zoom
        comb_src.fields[3].data = zoom(comb_src.fields[3].data, (1.5, 1),
                                       order=1)
        print(comb_src.fields[3].data.shape)
        print(dict(comb_src.fields[3].header))

        # angle = 30
        # deg2rad = 3.1415926 / 180
        # comb_src.fields[3].header["PC1_1"] = np.cos(angle * deg2rad)
        # comb_src.fields[3].header["PC1_2"] = np.sin(angle * deg2rad)
        # comb_src.fields[3].header["PC2_1"] = -np.sin(angle * deg2rad)
        # comb_src.fields[3].header["PC2_2"] = np.cos(angle * deg2rad)

        as2deg = u.arcsec.to(u.deg)

        fov = FieldOfView(fov_hdr, waverange=[0.5, 2.5] * u.um)
        fov.hdu.header["CRVAL1"] += 1.5 * as2deg
        fov.hdu.header["CRVAL2"] -= 1.5 * as2deg
        fov.hdu.header["CRVAL1D"] += 30
        fov.hdu.header["CRVAL2D"] -= 30

        imp = ImagePlane(implane_hdr)
        fov.extract_from(comb_src)
        fov.view()
        imp.add(fov.hdu, wcs_suffix="D")

        ipt = np.sum(fov.fields[0]["flux"]) + np.sum(fov.fields[1].data)
        opt = np.sum(imp.image)

        assert ipt == approx(opt)

        if PLOTS:
            plt.subplot(131)
            plt.imshow(comb_src.fields[3].data.T,
                       origin="lower",
                       norm=LogNorm())

            plt.subplot(132)
            plt.imshow(fov.image.T, origin="lower", norm=LogNorm())

            plt.subplot(133)
            plt.imshow(imp.image.T, origin="lower", norm=LogNorm())
            plt.show()
    def test_views_with_only_image(self, basic_fov_header):
        src = _image_source()
        flux = src.photons_in_range(1 * u.um, 2 * u.um).value
        orig_sum = np.sum(src.fields[0].data * flux)

        the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=1 * u.m**2)
        the_fov.extract_from(src)
        the_fov.view()

        assert np.isclose(np.sum(the_fov.hdu.data), orig_sum)

        if PLOTS:
            plt.imshow(src.fields[0].data, origin="lower", norm=LogNorm())
            plt.colorbar()
            plt.show()

            plt.imshow(the_fov.hdu.data, origin="lower", norm=LogNorm())
            plt.colorbar()
            plt.show()
    def test_views_with_tables_and_images(self, basic_fov_header):
        src = _combined_source(im_angle=0,
                               weight=[1, 1, 1],
                               dx=[-2, 0, 0],
                               dy=[2, 2, 0])
        ii = src.fields[3].header["SPEC_REF"]
        flux = src.photons_in_range(1 * u.um, 2 * u.um, indexes=[ii]).value
        orig_sum = np.sum(src.fields[3].data) * flux

        the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=1 * u.m**2)
        the_fov.extract_from(src)
        view = the_fov.view()

        assert np.isclose(np.sum(the_fov.fields[1].data), orig_sum)
        assert np.isclose(np.sum(view), orig_sum + 36)
        assert np.sum(the_fov.fields[0]["flux"]) == approx(36)

        if PLOTS:
            plt.imshow(view.T, origin="lower", norm=LogNorm())
            plt.colorbar()
            plt.show()
    def test_views_with_rotated_image(self, basic_fov_header):
        area = 1 * u.m**2

        # works for angle=0, not working for angle!=0
        src = _image_source(angle=30)
        flux = src.photons_in_range(1 * u.um, 2 * u.um, area=area).value
        in_sum = np.sum(src.fields[0].data) * flux

        the_fov = FieldOfView(basic_fov_header, (1, 2) * u.um, area=area)
        the_fov.extract_from(src)
        view = the_fov.view()
        out_sum = np.sum(view)

        assert out_sum == approx(in_sum, rel=1e-3)

        if PLOTS:
            plt.subplot(121)
            plt.imshow(src.fields[0].data, origin="lower", norm=LogNorm())
            plt.colorbar()

            plt.subplot(122)
            plt.imshow(view, origin="lower", norm=LogNorm())
            plt.colorbar()
            plt.show()
    def test_can_extract_the_source_in_a_fov(self, fov_hdr, comb_src,
                                             implane_hdr):

        fov = FieldOfView(fov_hdr,
                          waverange=[0.5, 2.5] * u.um,
                          area=1 * u.m**2)
        imp = ImagePlane(implane_hdr)

        fov.extract_from(comb_src)
        fov.view()
        imp.add(fov.hdu, wcs_suffix="D")

        assert np.sum(imp.image) > 0

        if PLOTS:
            plt.subplot(131)
            plt.imshow(comb_src.fields[3].data, origin="lower", norm=LogNorm())

            plt.subplot(132)
            plt.imshow(fov.hdu.data, origin="lower", norm=LogNorm())

            plt.subplot(133)
            plt.imshow(imp.hdu.data, origin="lower", norm=LogNorm())
            plt.show()