def test_image_is_visible(self):
        cmd = sim.UserCommands(use_instrument="METIS", set_modes=["img_n"])
        metis = sim.OpticalTrain(cmd)
        for eff in ["skycalc_atmosphere",   # Adds ~58000 ph/s/pix
                    "telescope_reflection", # Adds ~20 ph/s/pix
                    "common_fore_optics",   # EntrWindow alone adds ~14700 ph/s/pix
                    "chop_nod",
                    # "metis_img_lm_mirror_list",           # Adds ~0 ph/s/pix
                    # "quantum_efficiency",
                    "psf"
                    ]:
            metis[eff].include = False

        hdu = fits.open(r"F:\temp\scopesim_metis_workshop\data\sd0490_image_l12_i090_p000.fits")
        hdu[0].header["CDELT1"] *= 10
        hdu[0].header["CDELT2"] *= 10
        hdu[0].header["CUNIT1"] = "deg"
        hdu[0].header["CUNIT2"] = "deg"
        hdu[0].header["CRVAL1"] = 0
        hdu[0].header["CRVAL2"] = 0
        src = sim.Source(image_hdu=hdu[0], flux=1*u.Jy)
        # src = empty_sky()

        metis.observe(src)
        img = metis.image_planes[0].data

        if PLOTS:
            plt.imshow(img)
            plt.show()
def run_metis_lss():
    # src = sim.source.source_templates.empty_sky()
    spec = source_templates.ab_spectrum()
    src = sim.Source(x=[-1, 0, 1],
                     y=[0, 0, 0],
                     ref=[0, 0, 0],
                     weight=[1, 1, 1],
                     spectra=[spec])
    cmds = sim.UserCommands(use_instrument="METIS", set_modes=["lss_m"])
    metis = sim.OpticalTrain(cmds)
    metis["metis_psf_img"].include = False

    pr = cProfile.Profile()
    pr.enable()
    metis.observe(src)
    pr.disable()
    pr.print_stats(sort="cumulative")

    hdus = metis.readout()

    plt.subplot(122)
    plt.imshow(hdus[0][1].data, origin="lower", norm=LogNorm())
    plt.title("Detctor Plane (with noise)")
    plt.colorbar()

    plt.subplot(121)
    plt.imshow(metis.image_planes[0].data, origin="lower", norm=LogNorm())
    plt.title("Image Plane (noiseless)")
    plt.colorbar()
    plt.show()
    def test_image_source_is_as_expected(self):
        im = face(True).astype(float)
        hdu = fits.ImageHDU(data=im)
        hdu.header.update({"CDELT1": 0.00547, "CDELT2": 0.00547,
                           "CUNIT1": "arcsec", "CUNIT2": "arcsec"})
        src = sim.Source(image_hdu=hdu, flux=1*u.mJy)

        cmd = sim.UserCommands(use_instrument="METIS", set_modes=["img_n"])
        metis = sim.OpticalTrain(cmd)
        metis["detector_linearity"].include = False

        # for eff in ["skycalc_atmosphere",  # Adds ~58000 ph/s/pix
        #             "telescope_reflection",  # Adds ~20 ph/s/pix
        #             "common_fore_optice",  # EntrWindow alone adds ~14700 ph/s/pix
        #             "metis_img_lm_mirror_list",  # Adds ~0 ph/s/pix
        #             "quantum_efficiency",
        #             "psf"
        #             ]:
        #     metis[eff].include = False

        metis.observe(src)
        hdus = metis.readout()

        n = 1024
        img = metis.image_planes[0].data
        img = hdus[0][1].data
        img_sum = np.sum(img[1024 - n:1024 + n, 1024 - n:1024 + n])
        img_med = np.median(img[n:3 * n, n:3 * n])
        print(f"Sum star: {img_sum}, Median top-left: {img_med}")

        sys_trans = metis.optics_manager.system_transmission
        one_jy_phs = hmbp.in_one_jansky(sys_trans).value * 978

        if PLOTS:
            plt.imshow(img[1024 - n:1024 + n, 1024 - n:1024 + n])  # norm=LogNorm()
            plt.show()

        assert img_sum == approx(one_jy_phs, rel=0.05)
def vary_exposure_times(plot=False):
    '''Adjusting exposure times'''
    print("------ Beginning of vary_exposure_times() ----------")

    # Create a new source
    # import simmetis
    # dummycmd = simmetis.UserCommands("metis_image_LM.config",
    #                                  sim_data_dir="../data")
    # lam, spec = simmetis.source.flat_spectrum(17, "TC_filter_Lp.dat")

    lam = np.arange(1, 20, 0.001)
    spec = np.ones(len(lam)) * 1e7 * 2.5*np.log10(-30)     # 0 mag spectrum

    src = sim.Source(lam=lam * u.um, spectra=np.array([spec]),
                          ref=[0], x=[0], y=[0])

    # Load the configuration for the METIS LM-band imaging mode.
    cmd = sim.UserCommands(use_instrument="METIS",
                           set_modes=["img_lm"])

    # build the optical train and adjust
    metis = sim.OpticalTrain(cmd)
    metis['detector_linearity'].include = False

    # Observe the source
    metis.observe(src, update=True)

    # Readout with a range of DITs and NDITs
    dit = np.array([1, 1, 1, 10, 10, 10, 100, 100, 100])
    ndit = np.array([3, 10, 30, 3, 10, 30, 3, 10, 30])
    hdus = list()
    for i in range(9):
        metis.cmds["!OBS.dit"] = dit[i]
        metis.cmds["!OBS.ndit"] = ndit[i]
        thehdu = metis.readout()[0]
        hdus.append(thehdu)
        print(i, "DIT =", rc.__currsys__["!OBS.dit"],
              "   NDIT =", rc.__currsys__["!OBS.ndit"])
        print("min =", thehdu[1].data.min(), "    max =", thehdu[1].data.max())

    if plot:
        fig, axes = plt.subplots(3, 3, figsize=(12, 12),
                                 sharex=True, sharey=True)
        for i in range(9):
            theax = axes.flat[i]
            frame = hdus[i][1].data[960:1090, 960:1090]
            theplot = theax.imshow(frame, origin='lower')
            fig.colorbar(theplot, ax=theax)
            theax.set_title("DIT={}, NDIT={}, INTTIME={}".format(
                dit[i], ndit[i], dit[i] * ndit[i]))

        plt.show()


    # Perform photometry
    aperture = CircularAperture([(1024., 1024.)], r=10.)

    bglevel = np.zeros(9)
    bgnoise = np.zeros(9)
    starsum = np.zeros(9)
    starnoise = np.zeros(9)

    for i, thehdu in enumerate(hdus):
        # background stats
        bglevel[i] = np.mean(thehdu[1].data[0:800, 0:800])
        bgnoise[i] = np.std(thehdu[1].data[0:800, 0:800])
        # total signal of star
        phot_table = aperture_photometry(thehdu[1].data - bglevel[i], aperture,
                                         error=(np.ones_like(thehdu[1].data)
                                                * bgnoise[i]))
        starsum[i] = phot_table['aperture_sum'][0]
        starnoise[i] = phot_table['aperture_sum_err'][0]

    table = Table([dit, ndit, dit * ndit, bglevel, bgnoise, starsum,
                   starsum / starnoise],
                  names=["DIT", "NDIT", "INTTIME", "bg level", "bg noise",
                         "Star counts", "S/N"])
    table["bg level"].format = ".0f"
    table["bg noise"].format = ".1f"
    table["Star counts"].format = ".1f"
    table["S/N"].format = ".2f"

    table.pprint()

    if plot:
        plt.plot(table["INTTIME"], table["S/N"], "o")
        plt.xlabel("Integration time")
        plt.ylabel("Signal-to-noise ratio")
        plt.show()

    print("--------- vary_exposure_times() -------------")
def simulate_point_source(plot=False):
    '''Create a simulation of a point source'''

    print("------ Beginning of simulate_point_source() ----------")
    # Create the Source object, this is currently a hack. We create a
    # spectrum that is flat in lambda with magnitude 0 in the Lp
    # filter. For this purpose we are currently using a function in
    # SimMETIS, pending its inclusion in ScopeSim-Templates.

    # import simmetis
    # dummycmd = simmetis.UserCommands("metis_image_LM.config",
    #                                  sim_data_dir="../data")
    # dummycmd["INST_FILTER_TC"] = "TC_filter_Lp.dat"
    #
    # lam, spec = simmetis.source.flat_spectrum(0,
    #                                           dummycmd["INST_FILTER_TC"])

    lam = np.linspace(1, 20, 0.001)
    spec = np.ones(len(lam)) * 1e7      # 0 mag spectrum

    if plot:
        plt.plot(lam, spec)
        plt.xlabel(r"$\lambda$ [um]")
        plt.ylabel("relative flux")
        plt.title("Spectrum of input source")
        plt.show()

    # Create two source objects for two dither positions
    dither_offset = 1
    src = sim.Source(lam=lam * u.um, spectra=np.array([spec]),
                          ref=[0], x=[0], y=[0])
    src_dither = sim.Source(lam=lam * u.um, spectra=np.array([spec]),
                                 ref=[0], x=[0], y=[dither_offset])

    # Load the configuration for the METIS LM-band imaging mode.
    cmd = sim.UserCommands(use_instrument="METIS",
                                set_modes=["img_lm"])

    # build the optical train and adjust
    metis = sim.OpticalTrain(cmd)
    metis['detector_linearity'].include = False

    # Set the DIT to 1 second
    metis.cmds["!OBS.dit"] = 1.

    # Perform an observation.
    metis.observe(src, update=True)
    hdus = metis.readout()

    metis.observe(src_dither, update=True)
    hdus_dither = metis.readout()

    frame1 = hdus[0][1].data
    frame2 = hdus_dither[0][1].data
    if plot:
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 6),
                                       sharey=True)
        f1_plot = ax1.imshow(frame1[900:1400, 940:1100], origin='lower')
        fig.colorbar(f1_plot, ax=ax1)

        f2_plot = ax2.imshow(frame2[900:1400, 940:1100], origin='lower')
        fig.colorbar(f2_plot, ax=ax2)

        plt.show()

    # Shift the dithered image back to the original position and combine
    # the two images
    pixscale = metis.cmds['INST']['pixel_scale']
    frame_sum = frame1 + np.roll(frame2,
                                 np.int(np.round(-dither_offset / pixscale)),
                                 axis=0)
    if plot:
        plt.imshow(frame_sum[940:1100, 940:1100])
        plt.colorbar()
        plt.show()

    print("Writing test_LM_framesum.fits")
    fits.writeto("test_LM_framesum.fits", frame_sum, overwrite=True)

    print("--------- End of simulate_point_source() -------------")
Exemple #6
0
def sim_heeps():
    """Do the simulation"""

    # Prepare the source
    cube = fits.open(FNAME)
    imwcs = WCS(cube[0].header).sub([1, 2])
    naxis1, naxis2, naxis3 = (cube[0].header['NAXIS1'],
                              cube[0].header['NAXIS2'],
                              cube[0].header['NAXIS3'])

    # Set up the instrument
    cmd = sim.UserCommands(use_instrument='METIS', set_modes='img_lm')

    # Set up detector size to match the input cube - using the full
    # METIS detector of 2048 x 2048 pixels would require too much memory
    cmd["!DET.width"] = naxis1
    cmd["!DET.height"] = naxis2

    metis = sim.OpticalTrain(cmd)

    # Set included and excluded effects
    metis['armazones_atmo_skycalc_ter_curve'].include = True
    metis['armazones_atmo_default_ter_curve'].include = False

    metis['scope_surface_list'].include = False
    metis['eso_combined_reflection'].include = True

    metis['detector_array_list'].include = False
    metis['detector_window'].include = True

    metis['scope_vibration'].include = False
    metis['detector_linearity'].include = False
    metis['metis_psf_img'].include = False

    # We attach the spectrum of Vega to the image
    spec = SourceSpectrum.from_file("alpha_lyr_stis_008.fits")

    # Exposure parameters
    dit = cube[0].header['CDELT3']
    ndit = 1

    # Loop over the input cube
    nplanes = 100
    # nplanes = naxis3  # for full cube

    nplanes = min(nplanes, naxis3)

    for i in range(nplanes):
        print("Plane", i + 1, "/", nplanes)
        imhdu = fits.ImageHDU(header=imwcs.to_header(),
                              data=cube[0].data[i, :, :] * FLUX_SCALE)
        src = sim.Source(spectra=[spec], image_hdu=imhdu)

        metis.observe(src)
        outhdu = metis.readout(dit=dit, ndit=ndit)[0]
        cube[0].data[i, :, :] = outhdu[1].data.astype(np.float32)

    # We have filled up the original cube with the simulated data
    # Write out only the simulated planes
    fits.writeto(OUTFILE,
                 data=cube[0].data[:nplanes, :, :],
                 header=cube[0].header,
                 overwrite=True)