示例#1
0
def test_run_through_Telescope_methods():
    duet = Telescope()
    duet_offax = Telescope(config='reduced_baseline')
    duet.info()
    duet.update_bandpass()
    duet.calc_radial_profile()
    duet.calc_psf_fwhm()
    duet.update_effarea()
    duet.fluence_to_rate(1 * u.ph / u.s / u.cm**2)
    duet.psf_model()
    duet.compute_psf_norms()
示例#2
0
def imsim(**kwargs):
    """
    Simulate images of sources in galaxies with a range of surface brightnesses
    Warning! Takes several hours to run (~6 hours for 10 surface brightness levels, nmag=71 and nsrc=100)

    Will make a directory tree run/galaxy/zodi-level/duet[1,2] in directory where it's run.

    Parameters
    ----------
    tel: 'config' default is 'baseline'
        Sets telescope parameters

    run: string (date, as in '050719', or other identifying string)
        To track runs

    gal: 'spiral', 'elliptical', 'dwarf' or 'none', default is spiral
        Sets Sersic index and size

    zodi: 'low', 'med' or 'high', default is low
        Use the medium zodiacal background rate. Overrides low_zodi.

    sfb: [sfb_low, sfb_high], default is [20,30]
        List of lowest and highest surface brightness to simulate

    nmags: int, default is 71
        Number of source magnitudes to simulate, in 0.1 mag steps around 20.5

    nsrc: int, default is 100
        Number of sources to simulate at each source mag

    stack: int, default is 1
        Depth of science image (number of stacked 300s exposures)

    nref: list, default is [1,3,7,11]
        List of reference image depths

    Returns
    -------
    telescope_band_gal_sfb_zodi_src-mag.fits: fits file with simulated images.
    """

    # Deal with kwargs:
    tel = kwargs.pop('tel', 'baseline')
    gal = kwargs.pop('gal', 'spiral')
    zodi = kwargs.pop('zodi', 'low')
    sfb_lim = kwargs.pop('sfb', [20, 30])
    nmags = kwargs.pop('nmags', 71)
    nsrc = kwargs.pop('nsrc', 100)
    run = kwargs.pop('run')
    stack = kwargs.pop('stack', 1)
    ref_arr = kwargs.pop('nref', [1, 3, 7, 11])

    # set some telescope, instrument parameters
    duet = Telescope(config=tel)

    # Set/make directories:
    path1 = sim_path(run=run, gal=gal, zodi=zodi, band='duet1')
    path2 = sim_path(run=run, gal=gal, zodi=zodi, band='duet2')

    # Write telescope definition file if it doesn't exist yet for this run:
    if not os.path.exists('run_' + run + '/teldef'):
        teldef = duet.info()
        teldef_file = open('run_' + run + '/teldef', 'w+')
        teldef_file.write(teldef)
        teldef_file.close()

    # Define image simulation parameters
    exposure = 300 * u.s
    frame = np.array(
        [30, 30]
    )  # Dimensions of the image I'm simulating in DUET pixels (30x30 ~ 3x3 arcmin)
    oversample = 6  # Hardcoded in construct_image

    # Get backgrounds
    if zodi == 'low':
        [bgd_band1, bgd_band2] = background_pixel_rate(duet,
                                                       low_zodi=True,
                                                       diag=False)
    elif zodi == 'med':
        [bgd_band1, bgd_band2] = background_pixel_rate(duet,
                                                       med_zodi=True,
                                                       diag=False)
    elif zodi == 'high':
        [bgd_band1, bgd_band2] = background_pixel_rate(duet,
                                                       high_zodi=True,
                                                       diag=False)

    # Define galaxy: magnitude is placeholder. Sizes are typical at 100 Mpc
    if gal == 'spiral':
        reff = 16.5 * u.arcsec
        gal_params = {
            'magnitude': 1,
            'r_eff': reff / (duet.pixel / oversample),
            'n': 1,
            'x_0': 0,
            'y_0': 0
        }
    elif gal == 'elliptical':
        reff = 12.5 * u.arcsec
        gal_params = {
            'magnitude': 1,
            'r_eff': reff / (duet.pixel / oversample),
            'n': 4,
            'x_0': 0,
            'y_0': 0
        }
    elif gal == 'dwarf':
        reff = 7 * u.arcsec
        gal_params = {
            'magnitude': 1,
            'r_eff': reff / (duet.pixel / oversample),
            'n': 1,
            'x_0': 0,
            'y_0': 0
        }
    elif gal == 'none':
        gal_params = None

    # Make galaxy surface brightness array (if necessary)
    if gal != 'none':
        sfb_arr = np.arange(sfb_lim[0],
                            sfb_lim[1] + 1.)  # Now in steps of 1 mag

    # Make srcmag array:
    srcmag_arr = np.linspace(20.5 - 0.5 * (nmags - 1) * 0.1,
                             20.5 + 0.5 * (nmags - 1) * 0.1,
                             num=nmags,
                             endpoint=True)  # Currently in steps of 0.1 mag

    # No background galaxy:
    if gal == 'none':
        # Make reference images:
        ref_hdu_1, ref_hdu_2 = run_sim_ref(duet=duet,
                                           bkg1=bgd_band1,
                                           bkg2=bgd_band2,
                                           ref_arr=ref_arr,
                                           gal=False,
                                           exposure=exposure,
                                           frame=frame)
        # Update headers:
        ref_hdu_1 = update_header(ref_hdu_1,
                                  im_type='reference',
                                  zodi=zodi,
                                  gal=gal,
                                  band='DUET1',
                                  nframes=len(ref_arr),
                                  exptime=exposure.value)
        ref_hdu_2 = update_header(ref_hdu_2,
                                  im_type='reference',
                                  zodi=zodi,
                                  gal=gal,
                                  band='DUET2',
                                  nframes=len(ref_arr),
                                  exptime=exposure.value)

        # Write files
        ref_filename1 = run + '_duet1_zodi-' + zodi + '_reference.fits'
        ref_hdu_1.writeto(path1 + '/' + ref_filename1, overwrite=True)

        ref_filename2 = run + '_duet2_zodi-' + zodi + '_reference.fits'
        ref_hdu_2.writeto(path2 + '/' + ref_filename2, overwrite=True)

        # Make source images:
        for srcmag in srcmag_arr:
            src_hdu_1, src_hdu_2 = run_sim(duet=duet,
                                           bkg1=bgd_band1,
                                           bkg2=bgd_band2,
                                           stack=stack,
                                           srcmag=srcmag,
                                           nsrc=nsrc,
                                           gal=False,
                                           exposure=exposure,
                                           frame=frame)
            # Update headers
            src_hdu_1 = update_header(src_hdu_1,
                                      im_type='source',
                                      zodi=zodi,
                                      gal=gal,
                                      band='DUET1',
                                      srcmag=srcmag,
                                      nframes=nsrc,
                                      exptime=exposure.value)
            src_hdu_2 = update_header(src_hdu_2,
                                      im_type='source',
                                      zodi=zodi,
                                      gal=gal,
                                      band='DUET2',
                                      srcmag=srcmag,
                                      nframes=nsrc,
                                      exptime=exposure.value)

            # Write file
            filename1 = run + '_duet1_zodi-' + zodi + '_stack-' + str(
                stack) + '_src-' + "{:5.2f}".format(srcmag) + '.fits'
            src_hdu_1.writeto(path1 + '/' + filename1, overwrite=True)

            filename2 = run + '_duet2_zodi-' + zodi + '_stack-' + str(
                stack) + '_src-' + "{:5.2f}".format(srcmag) + '.fits'
            src_hdu_2.writeto(path2 + '/' + filename2, overwrite=True)

    # Yes background galaxy:
    else:
        for i, sfb in enumerate(sfb_arr):
            print('Surface brightness level ' + str(i + 1) + ' of ' +
                  str(len(sfb_arr)) + '...')
            # Calculate count rate:
            gal_params['magnitude'] = sfb
            # Make reference images:
            ref_hdu_1, ref_hdu_2 = run_sim_ref(duet=duet,
                                               bkg1=bgd_band1,
                                               bkg2=bgd_band2,
                                               ref_arr=ref_arr,
                                               gal=True,
                                               gal_params=gal_params,
                                               exposure=exposure,
                                               frame=frame)
            # Update headers:
            ref_hdu_1 = update_header(ref_hdu_1,
                                      im_type='reference',
                                      zodi=zodi,
                                      gal=gal,
                                      band='DUET1',
                                      nframes=len(ref_arr),
                                      exptime=exposure.value)
            ref_hdu_2 = update_header(ref_hdu_2,
                                      im_type='reference',
                                      zodi=zodi,
                                      gal=gal,
                                      band='DUET2',
                                      nframes=len(ref_arr),
                                      exptime=exposure.value)
            # Write files:
            ref_filename1 = run + '_duet1_' + gal + '_' + str(
                sfb) + '_zodi-' + zodi + '_reference.fits'
            ref_hdu_1.writeto(path1 + '/' + ref_filename1, overwrite=True)

            ref_filename2 = run + '_duet2_' + gal + '_' + str(
                sfb) + '_zodi-' + zodi + '_reference.fits'
            ref_hdu_2.writeto(path2 + '/' + ref_filename2, overwrite=True)

            # Make source images:
            for srcmag in srcmag_arr:
                src_hdu_1, src_hdu_2 = run_sim(duet=duet,
                                               bkg1=bgd_band1,
                                               bkg2=bgd_band2,
                                               stack=stack,
                                               srcmag=srcmag,
                                               nsrc=nsrc,
                                               gal=True,
                                               gal_params=gal_params,
                                               exposure=exposure,
                                               frame=frame)
                # Update headers:
                src_hdu_1 = update_header(src_hdu_1,
                                          im_type='source',
                                          zodi=zodi,
                                          gal=gal,
                                          band='DUET1',
                                          srcmag=srcmag,
                                          nframes=nsrc,
                                          exptime=exposure.value)
                src_hdu_2 = update_header(src_hdu_2,
                                          im_type='source',
                                          zodi=zodi,
                                          gal=gal,
                                          band='DUET2',
                                          srcmag=srcmag,
                                          nframes=nsrc,
                                          exptime=exposure.value)

                # Write files:
                filename1 = run + '_duet1_' + gal + '_' + str(
                    sfb) + '_zodi-' + zodi + '_stack-' + str(
                        stack) + '_src-' + "{:5.2f}".format(srcmag) + '.fits'
                src_hdu_1.writeto(path1 + '/' + filename1, overwrite=True)

                filename2 = run + '_duet2_' + gal + '_' + str(
                    sfb) + '_zodi-' + zodi + '_stack-' + str(
                        stack) + '_src-' + "{:5.2f}".format(srcmag) + '.fits'
                src_hdu_2.writeto(path2 + '/' + filename2, overwrite=True)