Пример #1
0
 def __init__(self, fname, min_z=1e-2, max_z=0.5):
     self.data = pd.HDFStore(fname, mode='r').data
     self.data_description = pd.HDFStore(fname, mode='r').description
     self.data = self.data[self.data.z > min_z]
     self.data = self.data[self.data.z < max_z]
     self.coords = self.get_sky_coords(self.data)
     self.arcsec_per_kpc = Planck15.arcsec_per_kpc_proper(self.data.z)
Пример #2
0
def angular_distance_vs_redshift(size):
    """
	Plots the anuglar distance vs redshift function as a function of the physical
	size of the radio source
	"""

    tdata = '/data1/osinga/value_added_catalog_1_1b_thesis/SN_10_value_added_sources.fits'
    tdata = Table(fits.open(tdata)[1].data)
    tdata = tdata[np.invert(np.isnan(tdata['z_best']))]
    z_best = tdata['z_best']

    print('redshift:')
    print('min: %f, max: %f, mean %f, median: %f' %
          (np.min(z_best), np.max(z_best), np.mean(z_best), np.median(z_best)))
    domain_z = np.linspace(0, np.max(z_best), 5000)

    arcsec_per_kpc = Planck15.arcsec_per_kpc_proper(domain_z)

    # arcsec_per_kpc = Planck15.arcsec_per_kpc_comoving(domain_z)

    # plt.plot(domain_z, arcsec_per_kpc,label='1 kpc')
    plt.plot(domain_z, 10 * arcsec_per_kpc, label='10 kpc')
    plt.plot(domain_z, 100 * arcsec_per_kpc, label='100 kpc')
    plt.plot(domain_z, 200 * arcsec_per_kpc, label='200 kpc')
    plt.plot(domain_z, 300 * arcsec_per_kpc, label='300 kpc')
    plt.plot(domain_z, 400 * arcsec_per_kpc, label='400 kpc')
    plt.hlines(41.4, 0, np.max(z_best), label="0.69' cutoff")
    plt.ylabel('Angular size (arcseconds)', fontsize=12)
    plt.ylim(0, 150)
    plt.xlim(0, np.max(z_best))
    plt.xlabel('Redshift', fontsize=12)
    plt.legend(title='Radio source physical size')
    plt.grid()
    plt.show()
Пример #3
0
 def setup_galaxy(self):
     self.sersic_index = 1.0
     self.position_angle = np.random.uniform(0., np.pi)  # radians
     self.redshift = z_at_value(cosmo.luminosity_distance,
                                self.distance * u.Mpc)
     self.re_arcsec = self.re_kpc * cosmo.arcsec_per_kpc_proper(
         self.redshift).value
     self.re_pixels = self.re_arcsec / self.arcsec_per_pixel
    def generate_blurred_map(self, kernel_size, band='H'):
        self.blrcube = self.cube.copy() * nan
        self.kernel_size = kernel_size
        self.kernel = Gaussian2DKernel(self.kernel_size)

        print 'Convolving spatially...'
        for i in arange(self.zsize):
            self.blrcube[i] = convolve_fft(self.cube[i], self.kernel)
        #self.blrcube += np.random.normal(0, max(self.blrcube.ravel())/100., shape(self.blrcube))

        #KMOS reaches a point source 5-sigma sensitvity in 8 hr of
        #of (J, H, K) = (22, 21.0, 20.5) AB magnitudes
        #for R ~ (3380, 3800, 3750)
        #citation: http://www2011.mpe.mpg.de/Highlights/FB2004/exp13_bender.pdf
        if band == 'H': sens, R = 21.0, 3800
        elif band == 'J': sens, R = 22.0, 3380
        elif band == 'K': sens, R = 20.5, 3750
        else:
            sens = 21.0
            print 'Bad input band, setting sensitivty to H-band value, %s AB mag' % (
                sens)

        print 'Convolving spectrally...'

        print 'Adding noise...'

        sens_si_fd = (sens * u.ABmag).to(
            u.Watt / (u.meter * u.meter) /
            (u.Hz)) * astropy.constants.c / (self.lam[0] * u.meter)**2.
        print sens_si_fd

        #The pixel values in our cube are W/m/m^2/Sr (surface brightness). To get to units of
        #flux density per pixel
        #check to make sure this pixel scale is in proper coordinates
        pix_scale_kpc = self.cube_hdr['CD1_1'] * u.kpc
        print pix_scale_kpc, 'per pixel'
        pix_scale_arc = pix_scale_kpc * cosmo.arcsec_per_kpc_proper(
            1. / self.ascale - 1)
        print pix_scale_arc, 'per pixel'
        pix_scale_str = (pix_scale_arc**2.).to(u.steradian)
        print pix_scale_str, 'per square pixel'

        #Currently in m, want to get in terms of hz^-1. F_v = (F_lam)*lam^2/c
        #Ths units of this factor are 1/(str*m). Let's now consider an 'aperture' equal to
        #1 spatial fwhm * 1 spectral fwhm. We want to add noise equal to 1/5th the sensitivity over this aperture.
        #In steradians, the PSF is:
        psf_str = pi * (self.kernel_size**2. * u.arcsec**2.).to(u.steradian)
        print psf_str, 'is the seeing'

        #The spectral lsf fwhm (in pixels) is:
        lsf_pix = (3.e5 / R) / (self.vscale[1] - self.vscale[0])
        print lsf_pix

        sens_noise = sens_si_fd / psf_str / lsf_pix
        print sens_noise
        self.cube += np.random.normal(0, sens_noise.value, self.cube.shape)
def main(minimal_file=True, SExtractor_command='source-extractor'):
    # Open the file with the name of the fields that the simulation
    # is going to be run for.
    f = open(parameters['list_of_fields'])
    k = f.readlines()
    f.close()
    cat = [str(line.split()[0]) for line in k]

    m_output = np.linspace(parameters['min_mag'], parameters['max_mag'],
                           parameters['mag_bins'])  #or observed magnitudes
    mbinsize = (parameters['max_mag']-parameters['min_mag'])/\
        (parameters['mag_bins']-1.)
    nmagbins_out = parameters['mag_bins']

    z_total = np.linspace(parameters['min_z'], parameters['max_z'],
                          parameters['z_bins'])  #redshifts
    nzbins = parameters['z_bins']

    #intrinsic/input absolute magnitudes
    #calculated by roughly converting the output magnitude back to intrinsic
    #and round to single digit. Assuming that the observed magnitude (m) of
    #the detection band is m at the UV reference wavelength.

    if parameters['Minput_min'] is None:
        maxlumdis = cosmo.luminosity_distance(parameters['max_z']).to(
            u.parsec).value
        M_input_min = np.around(parameters['min_mag'] - 2.5 * np.log10(
            (maxlumdis / 10.)**2 / (1. + parameters['max_z'])),
                                decimals=1)  #brightest absolute magnitude
        M_input_min = M_input_min - mbinsize  #make it one magbin lower
    else:
        M_input_min = parameters['Minput_min']

    if parameters['Minput_max'] is None:
        minlumdis = cosmo.luminosity_distance(parameters['min_z']).to(
            u.parsec).value
        M_input_max = np.around(parameters['max_mag'] - 2.5 * np.log10(
            (minlumdis / 10.)**2 / (1. + parameters['min_z'])),
                                decimals=1)  #faintest
        M_input_max = M_input_max + mbinsize
    else:
        M_input_max = parameters['Minput_max']

    nmagbins_in = np.ceil((M_input_max - M_input_min) / mbinsize).astype(int)
    M_input = np.arange(nmagbins_in) * mbinsize + M_input_min

    nLF = len(parameters['LF_shape'])

    print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
    print('\n')
    print('LF:', parameters['LF_shape'])
    print('redshift:', z_total)
    print('m_out:', m_output)
    print('M_input:', M_input)
    print('\n')
    print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')

    margin = np.round(parameters['margin'] /
                      parameters['size_pix']).astype(int)

    # Creates the directories that will contain the results if they
    # don't already exist.
    if not os.path.exists(parameters['path_to_results']):
        os.makedirs(parameters['path_to_results'])
    if not os.path.exists(parameters['path_to_results'] + 'Results'):
        os.makedirs(parameters['path_to_results'] + 'Results')
    if not os.path.exists(parameters['path_to_results'] + 'SciImages'):
        os.makedirs(parameters['path_to_results'] + 'SciImages')
    if not os.path.exists(parameters['path_to_results'] + 'Results'
                          '/SegmentationMaps'):
        os.makedirs(parameters['path_to_results'] + 'Results/SegmentationMaps')
    if not os.path.exists(parameters['path_to_results'] + 'Results/Plots'):
        os.makedirs(parameters['path_to_results'] + 'Results/Plots')
    if not os.path.exists(parameters['path_to_results'] + 'Results/images/'):
        os.makedirs(parameters['path_to_results'] + 'Results/images/')
    if not os.path.exists(parameters['path_to_results'] + '/Results/Catalogs'):
        os.makedirs(parameters['path_to_results'] + '/Results/Catalogs')

    #copy parameter and Sextractor files to the Results folder
    copyfile(parameter_file, parameters['path_to_results'] + parameter_file)
    copyfile('SExtractor_files/parameters.sex',
             parameters['path_to_results'] + 'parameters.sex')
    print('Results will be in %s' % parameters['path_to_results'])

    #Check if the provided throughputs are normalized.
    creation_of_galaxy.normalize_throughputs(parameters['bands'])

    #copy Schechter parameter if LF shape is specified as Schechter
    for ilf in range(nLF):
        parameters['LF_shape'][ilf] = parameters['LF_shape'][ilf].lower()
        if ((parameters['LF_shape'][ilf] == 'schechter')
                or (parameters['LF_shape'][ilf] == 'schechter_full')):
            copyfile('Files/LF_Schechter_params.txt',
                     parameters['path_to_results'] + 'LF_Schechter_params.txt')

    #loop for each field
    for ic in range(len(cat)):
        print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
        print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
        print('running field %s \n' % cat[ic])

        # Empty array to save the simulation results (for pickles)
        nout_completeness = np.zeros((nmagbins_out, nmagbins_in, nzbins, nLF))
        nout_dropouts = np.zeros((nmagbins_out, nmagbins_in, nzbins, nLF))
        Nin = np.zeros((nmagbins_in, nzbins, nLF))
        total_completeness = np.zeros((nmagbins_in, nzbins, nLF))
        total_dropouts = np.zeros((nmagbins_in, nzbins, nLF))

        #science image names (begining)
        name_hdu_list1 = (parameters['path_to_images'] +
                          parameters['image_name'] + cat[ic] + '_')
        #run SExtractor on science images
        run_sextractor.science_image(parameters['bands'],
                                     parameters['detection_band'],
                                     parameters['zeropoints'],
                                     parameters['gain_values'],
                                     parameters['path_to_images'],
                                     parameters['path_to_results'],
                                     parameters['image_name'],
                                     cat[ic],
                                     imfits_end=parameters['imfits_end'],
                                     rmsfits_end=parameters['rmsfits_end'],
                                     SExtractor_command=SExtractor_command)

        # Define segmentation map from the original science image.
        segm_maps_old_file = parameters['path_to_results'] +\
            'SciImages/sources_'+cat[ic]+'_' +parameters['detection_band'] +\
                '_segm.fits'
        # Open SExtractor catalogue with the identified sources from the
        # original science image and save the following information
        # about the sources on the detection band, or the chosen main detection band

        if parameters['detection_band'] == 'det':
            ibmain = parameters['bands'].index(
                parameters['det_combination'][0])
            f_old_hdu = fits.open('%sSciImages/sources_%s_%s_cat.fits' %
                                  (parameters['path_to_results'], cat[ic],
                                   parameters['det_combination'][0]),
                                  ignore_missing_end=True)
        else:
            f_old_hdu = fits.open('%sSciImages/sources_%s_%s_cat.fits' %
                                  (parameters['path_to_results'], cat[ic],
                                   parameters['detection_band']),
                                  ignore_missing_end=True)
            ibmain = 0

        id_old_cat = f_old_hdu[1].data['NUMBER']
        m_old_cat = f_old_hdu[1].data['MAG_AUTO']
        f_old_cat = f_old_hdu[1].data['FLUX_ISO']
        xpos_old_cat = f_old_hdu[1].data['X_IMAGE']
        ypos_old_cat = f_old_hdu[1].data['Y_IMAGE']
        f_old_hdu.close()

        # Create main overall output files and write headers to save
        # the detection stats and detection/dropout array for each field
        for ilf in range(nLF):
            curLF = parameters['LF_shape'][ilf]
            fws = open(
                parameters['path_to_results'] + 'Results/' + curLF +
                '_RecoveredStats_cat' + cat[ic] + '.cat', 'w')
            header = ('  z   # M_in # N(M_in) # St = 0 # St = 1  # st = 2'
                      ' # St = -1 # St = -2 # St = -3 #St = -4 # N_Dropouts'
                      ' # N_Recovered  # Dropout_frac  # Recov_frac\n')
            fws.writelines(header)
            fws.close()

            fws2 = open(
                parameters['path_to_results'] + 'Results/' + curLF +
                '_RecoveredStats_cat' + cat[ic] + '_nout.cat', 'w')
            header = ('  z   #M_in #N_objects #n_out@m_out  #' +
                      ' #'.join('%.1f' % x for x in m_output) + '\n')
            fws2.writelines(header)
            fws2.close()

            if parameters['dropouts'] is True:
                fws3 = open(
                    parameters['path_to_results'] + 'Results/' + curLF +
                    '_RecoveredStats_cat' + cat[ic] + '_dout.cat', 'w')
                header = ('  z   #M_in #N_objects #d_out@m_out  #' +
                          ' #'.join('%.1f' % x for x in m_output) + '\n')
                fws3.writelines(header)
                fws3.close()

        # Start the mega loops
        # Run for all required redshift bins.
        for iz in range(parameters['z_bins']):
            print(datetime.datetime.now())
            redshift = z_total[iz]  # Redshift of the galaxies
            print('****************************')
            print('Redshift z = %.1f' % redshift)
            # Effective radius in pixels. Equal to 'R_eff' kpc at z = 6
            # rescaled to the new redshift
            Re = (parameters['R_eff']*(6+1)/(redshift+1.0)) * \
                cosmo.arcsec_per_kpc_proper(redshift).value / \
                    parameters['size_pix']

            #Set the stamp diameter to 1.8 arcsecs or at least 5 Re
            stampsize = int(
                np.round(np.ceil(1.8 / parameters['size_pix'] / 2)) *
                2)  #in pixels, and is even number
            if stampsize < int(Re) * 5: stampsize = int(Re) * 5
            stamp_radius = stampsize / 2  # Radius of the galaxy stamp.
            print('creating stamps with size %d x %d pixels' %
                  (stampsize, stampsize))
            #Make galaxy stamps with specified Re, and stamp size, and various
            #ellipticity and inclinations.
            galaxy_grid, galaxy_grid_n4 = create_stamps(
                parameters['sersic_indices'], stampsize, Re,
                int(parameters['types_galaxies']), parameters['ebins'],
                parameters['ibins'])

            #Run for all LF shapes
            for ilf in range(nLF):
                curLF = parameters['LF_shape'][ilf]

                #Open the main overall output files to append
                fws = open(
                    parameters['path_to_results'] + 'Results/' + curLF +
                    '_RecoveredStats_cat' + cat[ic] + '.cat', 'a')

                fws2 = open(
                    parameters['path_to_results'] + 'Results/' + curLF +
                    '_RecoveredStats_cat' + cat[ic] + '_nout.cat', 'a')

                if parameters['dropouts'] is True:
                    fws3 = open(
                        parameters['path_to_results'] + 'Results/' + curLF +
                        '_RecoveredStats_cat' + cat[ic] + '_dout.cat', 'a')

                #Calculate number of galaxies to simulate according to curLF
                ngals, M_iter, refwl = creation_of_galaxy.calphi_marr(
                    M_input,
                    curLF,
                    redshift,
                    msample=parameters['n_iterations'],
                    minngal=parameters['n_galaxies'],
                    slope=parameters['lin_slope'],
                    expbase=parameters['exp_base'])

                #Note that refwl is only returned for Schechter function only
                #(specified in Schechter param file). For other LF, use the
                #specified wavelength from param.yaml
                if refwl is None:
                    refwl = parameters['ref_uv_wl']

                print('%s LF' % curLF)
                print(
                    'Number of galaxies to be created '
                    'per iteration (injecting maximum %d galaxies each time):'
                    % (parameters['n_inject_max']))
                print(ngals)
                print('\n')
                Nin[:, iz, ilf] = ngals

                #File to save the recovery info of the simulated galaxies.
                fwg = open(
                    parameters['path_to_results'] + 'Results/' + curLF +
                    'RecoveredGalaxies_' + cat[ic] + '_z%.2f' % redshift +
                    '.cat', 'w')
                if parameters['Jaguar_spec'] is True:
                    sixthcol = 'jaguar_id'
                else:
                    sixthcol = 'beta'
                headline = (
                    '# Mbin, Minput, niter, round_number, sexcat_id, ' +
                    sixthcol + ', input_%s' % (parameters['bands'][ibmain]) +
                    ', det_status, drop_status' + ''.join([
                        ', %s_AUTO, %s_errAUTO' % (x, x)
                        for x in parameters['bands']
                    ]) + ''.join([
                        ', %s_ISO, %s_snISO' % (x, x)
                        for x in parameters['bands']
                    ]) + ''.join([
                        ', %s_APER1, %s_snAPER1' % (x, x)
                        for x in parameters['bands']
                    ]) + ''.join([
                        ', %s_APER2, %s_snAPER2' % (x, x)
                        for x in parameters['bands']
                    ]) + ''.join([
                        ', %s_APER3, %s_snAPER3' % (x, x)
                        for x in parameters['bands']
                    ]) + ', Kron_radius, r90, xpos, ypos \n')
                fwg.writelines(headline)
                fwg.close()

                # Empty arrays to save the stats.
                total = np.zeros(nmagbins_in)
                identified = np.zeros(nmagbins_in)
                blended_f1 = np.zeros(nmagbins_in)
                blended_f2 = np.zeros(nmagbins_in)
                blended_b = np.zeros(nmagbins_in)
                blended_l = np.zeros(nmagbins_in)
                not_indentified_sn = np.zeros(nmagbins_in)
                not_indentified = np.zeros(nmagbins_in)
                drops = np.zeros(nmagbins_in)
                identified_total = np.zeros(nmagbins_in)
                identified_fraction = np.zeros(nmagbins_in)
                dropouts_fraction = np.zeros(nmagbins_in)

                nout = np.zeros([nmagbins_out, nmagbins_in])
                dout = np.zeros([nmagbins_out, nmagbins_in])

                # Run for all the required magnitudes.
                for iM in range(nmagbins_in):
                    print('redshift %d/%d, LF %d/%d, M_input %d/%d' %
                          (iz + 1, nzbins, ilf + 1, nLF, iM + 1, nmagbins_in))
                    print('   Input Magnitude bin M = %.1f mag:' % M_input[iM])
                    print('       simulate galaxies with M =', M_iter[iM])
                    for iit in range(parameters['n_iterations']):
                        niter = iit + 1  # Iteration number

                        Magnitude = M_iter[iM][
                            iit]  #absolute intrinsic mag for the current iteration

                        # All galaxies in each iteration will have the same spectrum
                        # Assign a random beta
                        m = np.zeros(parameters['n_bands'])
                        flux = np.zeros(parameters['n_bands'])

                        if parameters['Jaguar_spec'] is True:
                            beta = creation_of_galaxy.get_Jaguar(
                                Magnitude, redshift,
                                parameters['path_to_results'], refwl)
                            #beta here is the index number in the Jaguar catalog
                        else:
                            beta = random.gauss(parameters['beta_mean'],
                                                parameters['beta_sd'])
                            # Create spectrum
                            creation_of_galaxy.write_spectrum(
                                Magnitude,
                                beta,
                                redshift,
                                parameters['path_to_results'],
                                absmag=True,
                                refwl=refwl)

                        # Calculate input (theoretical observed) AB mag in each band
                        for ib in range(parameters['n_bands']):
                            m[ib] = creation_of_galaxy.mag_band(
                                parameters['bands'][ib], redshift,
                                parameters['path_to_results'])
                            flux[ib] = (10**(
                                (parameters['zeropoints'][ib] - m[ib]) / 2.5))

                        print('   iteration %d/%d:' %
                              (iit + 1, parameters['n_iterations']))
                        print('       theoretical m =', m, end='')

                        # Calculate how many rounds of injections for this
                        # iteration. Inject total of ngals[iM] galaxies with
                        # maximum of parameters['n_inject_max'] galaxies per injection
                        nrounds = np.ceil(
                            ngals[iM] / parameters['n_inject_max']).astype(int)
                        totinject = 0  #tally

                        # Start injecting
                        for ir in range(nrounds):
                            if ir == nrounds - 1:
                                ninject = ngals[iM] - totinject
                            else:
                                ninject = parameters['n_inject_max']

                            totinject += ninject

                            # Assign (xpos, ypos) = the position of the center of the
                            # galaxy in the image in pixels.
                            xpos, ypos = creation_of_galaxy.galaxies_positions(
                                name_hdu_list1 + parameters['detection_band'] +
                                parameters['imfits_end'], ninject, stampsize,
                                Re)

                            # Assign a random incl, and elip.
                            i_rand = np.random.randint(0,
                                                       parameters['ibins'],
                                                       size=ninject)
                            e_rand = np.random.randint(0,
                                                       parameters['ebins'],
                                                       size=ninject)
                            # Place galaxies and make fits files in all bands.
                            for ib in range(parameters['n_bands']):
                                create_synthetic_fits(
                                    name_hdu_list1,
                                    parameters['bands'][ib],
                                    parameters['imfits_end'],
                                    m[ib],
                                    parameters['sersic_indices'],
                                    ninject,
                                    parameters['fraction_type'],
                                    e_rand,
                                    i_rand,
                                    flux[ib],
                                    xpos,
                                    ypos,
                                    stamp_radius,
                                    galaxy_grid,
                                    galaxy_grid_n4,
                                    parameters['path_to_results'],
                                    cat[ic],
                                    fixed_psf=parameters['fixed_psf'])
                            # Check if there is a separate detection band,
                            # then create the detection image
                            if parameters['detection_band'] == 'det':
                                coadd_images(parameters['path_to_results'],
                                             cat[ic],
                                             parameters['det_combination'],
                                             parameters['detection_band'],
                                             parameters['path_to_images'],
                                             parameters['image_name'],
                                             parameters['rmsfits_end'],
                                             parameters['coadd_type'])

                            # Run SExtractor on the new images and generate
                            # catalogues of the detected sources.
                            for ib in range(parameters['n_bands']):
                                do_segmentation = True if (ib == 0) else False
                                run_sextractor.main(
                                    parameters['bands'][ib],
                                    parameters['detection_band'],
                                    parameters['zeropoints'][ib],
                                    parameters['gain_values'][ib],
                                    parameters['path_to_images'],
                                    parameters['path_to_results'],
                                    niter,
                                    parameters['image_name'],
                                    cat[ic],
                                    M_input[iM],
                                    redshift,
                                    rmsfits_end=parameters['rmsfits_end'],
                                    roundnum=ir,
                                    do_segmentation=do_segmentation,
                                    SExtractor_command=SExtractor_command)
                                print('.', end='')
                            print('.')

                            #remove the newly created images to save space (except detection images)
                            if minimal_file is True:
                                for ib in range(parameters['n_bands']):
                                    os.remove(
                                        parameters['path_to_results'] +
                                        'Results/images/sersic_sources_' +
                                        '%s_%s.fits' %
                                        (cat[ic], parameters['bands'][ib]))
                            else:
                                for ib in range(parameters['n_bands']):
                                    os.rename(
                                        parameters['path_to_results'] +
                                        'Results/images/sersic_sources_' +
                                        '%s_%s.fits' %
                                        (cat[ic], parameters['bands'][ib]),
                                        parameters['path_to_results'] +
                                        'Results/images/sersic_sources_' +
                                        '%s_%s_mag%.1f_z%.2f_i%d.%d_%s.fits' %
                                        (parameters['path_to_results'],
                                         cat[ic], M_input[iM], redshift, niter,
                                         ir, parameters['bands'][ib]))

                            # Find the number of sources for the different categories
                            # of detection status.
                            fwg = open(
                                parameters['path_to_results'] + 'Results/' +
                                curLF + 'RecoveredGalaxies_' + cat[ic] +
                                '_z%.2f' % redshift + '.cat', 'a')
                            identified_aux, blended_f1_aux, blended_f2_aux,\
                                blended_b_aux, blended_l_aux,\
                                not_indentified_sn_aux, not_indentified_aux,\
                                nout_c, nout_d, id_nmbr = blending.main(
                                    parameters['path_to_results'],
                                    niter, ir, parameters['detection_band'],
                                    cat[ic], M_input[iM], Magnitude, beta, redshift,
                                    xpos, ypos, xpos_old_cat, ypos_old_cat,
                                    segm_maps_old_file, m_old_cat, f_old_cat,
                                    id_old_cat, m[ibmain],
                                    parameters['zeropoints'], parameters['bands'],
                                    parameters['min_sn'], parameters['dropouts'],
                                    margin, m_output,
                                    fwg, parameters['det_combination'])
                            fwg.close()
                            # Find the input magnitude bin of m_in and add the
                            # number of sources in each detection status for each bin.
                            # This is to combine the results from different iterations.
                            for v in range(nmagbins_in):
                                # Check where the calculated input magnitude lies and
                                # add them to the respective counter.
                                if ((Magnitude > (M_input[v] - 0.5 * mbinsize))
                                        and (Magnitude <=
                                             (M_input[v] + 0.5 * mbinsize))):
                                    if v != iM:
                                        warnings.warn(
                                            "Something is wrong, v and iM should be equal"
                                        )
                                    total[v] = total[v] + ninject
                                    identified[
                                        v] = identified[v] + identified_aux
                                    blended_f1[
                                        v] = blended_f1[v] + blended_f1_aux
                                    blended_f2[
                                        v] = blended_f2[v] + blended_f2_aux
                                    blended_b[v] = blended_b[v] + blended_b_aux
                                    blended_l[v] = blended_l[v] + blended_l_aux
                                    not_indentified_sn[v] = (
                                        not_indentified_sn[v] +
                                        not_indentified_sn_aux)

                                    not_indentified[v] = (not_indentified[v] +
                                                          not_indentified_aux)
                                    drops[v] = drops[v] + np.sum(nout_d)
                                    nout[:, v] = nout[:, v] + nout_c
                                    dout[:, v] = dout[:, v] + nout_d
                            print(
                                '      round %d/%d, inject %d, detect %d, dropout %d (in magbins)'
                                % (ir + 1, nrounds, ninject, identified_aux +
                                   blended_f1_aux + blended_f2_aux,
                                   np.sum(nout_d)))

                            #delete big files
                            if minimal_file is True:
                                os.remove(
                                    parameters['path_to_results'] +
                                    'Results/SegmentationMaps/Segmentation_maps_i'
                                    + '%d.%d' % (niter, ir) + '_' +
                                    parameters['detection_band'] + '.fits')
                                #delete small but can contribute to large number of files
                                #Can uncomment if the computer has limit on the number of files.
                                if niter > 1:
                                    os.remove(
                                        parameters['path_to_results'] +
                                        'Results/SegmentationMaps/region_' +
                                        '%s_mag%.1f_z%.2f_i%d.%d.reg' %
                                        (cat[ic], M_input[iM], redshift, niter,
                                         ir))
                                    for ib in range(parameters['n_bands']):
                                        os.remove(
                                            '%sResults/Catalogs/source_%s_mag%.1f_z%.2f_i%d.%d_%s_cat.fits'
                                            % (parameters['path_to_results'],
                                               cat[ic], M_input[iM], redshift,
                                               niter, ir,
                                               parameters['bands'][ib]))

                            # Only keep sextracted catalog of injected galaxies.
                            # Delete real sources that are originally there
                            for ib in range(parameters['n_bands']):
                                delete_id_fits(
                                    '%sResults/Catalogs/source_%s_mag%.1f_z%.2f_i%d.%d_%s_cat.fits'
                                    % (parameters['path_to_results'], cat[ic],
                                       M_input[iM], redshift, niter, ir,
                                       parameters['bands'][ib]),
                                    id_nmbr,
                                    id_keep=True)
                        #finish looping over rounds
                        if totinject != ngals[iM]:
                            print(
                                'Number of injected galaxies is not equal to what expected'
                            )
                            breakpoint()
                    #finish looping over iterations
                #finish looping over input Magnitude bins

                #write results to the overall output files
                for s in range(nmagbins_in):
                    # Calculate fractions of different statuses.
                    identified_total[s] = (np.array(identified[s]) +
                                           np.array(blended_f1[s]) +
                                           np.array(blended_f2[s]))
                    identified_fraction[s] = float(identified_total[s]) /\
                        np.array(total[s])
                    dropouts_fraction[s] = float(drops[s]) / np.array(total[s])

                    total_completeness[s, iz, ilf] = identified_fraction[s]
                    total_dropouts[s, iz, ilf] = dropouts_fraction[s]

                    line = ('%.2f\t%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d'
                            '\t%d\t%f\t%f\n' %
                            (redshift, M_input[s], total[s], identified[s],
                             blended_f1[s], blended_f2[s], blended_b[s],
                             blended_l[s], not_indentified_sn[s],
                             not_indentified[s], drops[s], identified_total[s],
                             dropouts_fraction[s], identified_fraction[s]))
                    fws.writelines(line)

                    line = ('%.2f\t%.2f\t%d' %
                            (redshift, M_input[s], total[s]) +
                            ''.join('\t%d' % x for x in nout[:, s]) + '\n')
                    fws2.writelines(line)

                    if parameters['dropouts'] is True:
                        line = ('%.2f\t%.2f\t%d' %
                                (redshift, M_input[s], total[s]) +
                                ''.join('\t%d' % x for x in dout[:, s]) + '\n')
                        fws3.writelines(line)

                fws.close()
                fws2.close()
                if parameters['dropouts'] is True: fws3.close()

                nout_completeness[:, :, iz, ilf] = nout
                nout_dropouts[:, :, iz, ilf] = dout
            #finish looping over LF
        #finish looping over redshift
        #save overall output results with pickles
        output_dict = {
            'Nin': Nin,
            'Nout_detect': nout_completeness,
            'Nout_dropout': nout_dropouts,
            'magout': m_output,
            'Magin': M_input,
            'redshift': z_total,
            'LF_shape': parameters['LF_shape'],
            'Niteration': parameters['n_iterations']
        }
        outpicklefile = open(
            parameters['path_to_results'] + 'Results/GLACiAR_output_' +
            cat[ic] + '.pickle', 'wb')
        pickle.dump(output_dict, outpicklefile)
        outpicklefile.close()

        #Generate plots (Completeness as a function of input magnitude)
        plot_completeness.main(parameters['path_to_results'],
                               parameters['LF_shape'], M_input, z_total,
                               cat[ic], total_completeness, total_dropouts)
Пример #6
0
def jetpath(t,
            i=1.6,
            psi=0.3,
            theta=0,
            pp=1,
            s_jet=1,
            beta=0.99,
            z=1,
            alpha=0,
            core=None):
    # version 3 also returns a Boolean parameter which is True, if the jet
    # starts out

    # plot solution of precessing jet aberration problem
    # this version implements a callable function
    # model of Gower et al. 1982 ApJ 262,478
    # Reproduced their Figure 2 (compare V01).

    # this version: origin = 0
    # output: list of jet (counterjet) position vectors in degrees

    # inputs:
    # dt: np array of times in Myr
    # i: inclination cone angle to line of sight / rad
    # psi: prec. cone 1/2-opening angle / rad
    # theta: phase of precession
    # pp: precession period / myr
    # s_jet: 1=jet (rightwards, x) -1=counterjet (leftwards, -x)
    # gamma: bulk Lorentz factor
    # beta = jet speed (max 0.99 c)
    # z: source redshift, if negative it is the distance in Mpc
    # alpha: position angle on sky from right
    # core: array, RA and DEC of jet origin in degree

    from scipy.constants import year, c, pi
    from astropy.cosmology import Planck15 as cosmo
    from astropy import units as u

    if z > 0:
        as_per_kpc = cosmo.arcsec_per_kpc_proper(z)
        as_per_kpc *= u.kpc
        as_per_kpc /= u.arcsec
        as_per_kpc = float(as_per_kpc)
    if z < 0:  # now z is the distance in Mpc
        as_per_kpc = 360. * 60. * 60. / (2. * pi * (-z) * 1000)

    # cgs units
    kpc = 3.0856e21
    myr = 1.e6 * year
    ccgs = c * 100.

    dt = t * myr
    # adjust i so 0 corresponds to "jet towards observer"
    # i = i-pi/2

    d = 1.  # distance to object, scale-free:1.
    omega = 2. * pi / (pp * myr)  # precession frequency
    # scale = 1. / ( (180./pi)*60 )  # arcmin

    # Lorentz factor of jet
    c = ccgs  # speed of light
    gamma = 1 / (np.sqrt(1 - (beta**2)))
    vj = beta * c

    # precession argument, Gower et al. eq (6)
    prec = theta - omega * dt

    # X-velocity, Gower et al. eq (1)
    vx = s_jet * beta * c
    vx *= (np.sin(psi) * np.sin(i) * np.cos(prec) + np.cos(psi) * np.cos(i))

    # Y-velocity, Gower et al. eq (2), remember prec from eq (6)
    vy = s_jet * beta * c
    vy *= np.sin(psi) * np.sin(prec)

    # Z-velocity, Gower et al. eq (3), remember prec from eq (6)
    vz = s_jet * beta * c
    vz *= (np.cos(psi) * np.sin(i) - np.sin(psi) * np.cos(i) * np.cos(prec))

    # calculate the sky pattern / units of cm
    y = vy * dt / (1 - vx / c)
    z = vz * dt / (1 - vx / c)

    # convert to kpc
    ykpc = y / kpc
    zkpc = z / kpc

    # convert to arcsec
    yas = ykpc * as_per_kpc
    zas = zkpc * as_per_kpc

    # rotate on the sky
    yrot = np.cos(alpha) * yas - np.sin(alpha) * zas
    zrot = np.sin(alpha) * yas + np.cos(alpha) * zas

    # convert to degree
    ydeg = yrot / 60. / 60.
    zdeg = zrot / 60. / 60.

    # adjust to core position
    if core is not None:
        ydeg += core[0]
        zdeg += core[1]

    return ydeg, zdeg
Пример #7
0
import pyfits
import glob
import astropy
from astropy import units
from astropy.cosmology import Planck15 as cosmo
from astropy.convolution import Gaussian2DKernel, convolve_fft, Gaussian1DKernel
plt.close('all')
plt.ioff()

files = glob.glob('../data/jwst/*340*cam1.fits')

a = array([float(file.rsplit('_')[1].replace('a', '')) for file in files])
z = 1. / a - 1.

arc_per_kpc = cosmo.arcsec_per_kpc_proper(z)
"""
Nirspec design
0.2 x 0.5" shutters
R ~2700

"""
ysize = 1.5
xsize = 0.2

msa_arc_pix = 0.065
mos_arc_pix = 0.18
msa_fwhm_arc = 0.1
mos_fwhm_arc = 0.7


def rebin(a, new_shape):
Пример #8
0
def addWcsRtModel(inp,
                  ref,
                  outfile,
                  spatial_scale=0.2,
                  zgal=0.6942,
                  center=('12 36 19.811', '+62 12 51.831'),
                  PA=27):

    ### Load in the model file
    inpdat = fits.open(inp)
    rtcube = DataCube(inp, include_wcs=False)
    rtcube.wavelength = inpdat[2].data
    rtcube.data = inpdat[1].data
    wavearr = rtcube.wavelength

    ### Load in the reference file if necessary
    if not isinstance(ref, DataCube):
        refcube = DataCube(ref)
    else:
        refcube = inp
    refdat = refcube.data
    refwave = refcube.wavelength
    nbhdu = transform.narrowband(refcube, 4000., 5000.)
    refnb = nbhdu.data
    refwcs = WCS(nbhdu.header)

    ### Rearrange the cube
    newdat = np.transpose(rtcube.data, axes=[2, 1, 0])

    ### Get the pixel scale in the spectral dimension
    diff = wavearr[1:] - wavearr[:-1]
    if not np.all(diff == diff[0]):
        raise ValueError('Wavelength array not uniformly sampled')
    spectral_scale = diff[0]

    ### Set scale and reference positions
    wave1 = wavearr[0] * (1. + zgal)
    spectral_scale *= (1. + zgal)

    ### Get the bright pixel from reference cube
    brightpix, brightcoords = utils.bright_pix_coords(refnb, refwcs)

    ### Create the new WCS
    newwcs = WCS(naxis=3)
    spatscaledeg = spatial_scale * cosmo.arcsec_per_kpc_proper(zgal) / 3600.
    print(spatscaledeg)
    if center is not None:
        cencoords = SkyCoord(center[0], center[1], unit=(u.hourangle, u.deg))
    else:
        cencoords = brightcoords
    newwcs.wcs.crpix = np.array([150.5, 150.5, 1])
    newwcs.wcs.crval = np.array([cencoords.ra.deg, cencoords.dec.deg, wave1])
    newwcs.wcs.cdelt = np.array([8.0226e-6, 8.0226e-6, spectral_scale])
    newwcs.wcs.cunit = ['deg', 'deg', 'Angstrom']
    newwcs.wcs.ctype = ('RA---TAN', 'DEC--TAN', 'VAC')
    rotangle = np.radians(PA)  # Assuming PA given in degrees
    psmat = np.array([[
        newwcs.wcs.cdelt[0] * np.cos(rotangle),
        -newwcs.wcs.cdelt[1] * np.sin(rotangle), 0.
    ],
                      [
                          newwcs.wcs.cdelt[0] * np.sin(rotangle),
                          newwcs.wcs.cdelt[1] * np.cos(rotangle), 0.
                      ], [0., 0., 1.]])
    newwcs.wcs.cd = psmat

    newhdu = newwcs.to_fits()
    newhdu[0].header['PC3_3'] = spectral_scale
    newhdu[0].data = newdat

    newhdu.writeto(outfile, overwrite=True)
Пример #9
0
ax.axvline(3*np.nanmean(mean_noise), ls=':', lw=0.5, color='k')
#ax.text(0.14,1.75, 'Mean $3\\sigma$ Noise')
ax.axhline(0, ls='-', color='k', lw=0.5)
ax.legend(loc=1,fontsize='small', markerscale=0.7)
#ax.set_title('Convolved Dataset')
#plt.savefig('../sim_stack.png', dpi=400)
plt.savefig('../sim_stack.png', dpi=400)


## Angular Scale

z = np.linspace(0.3,4,100)
A = 1.45
alpha = 0.11
Reff = (A*(1+z)**(-alpha))*u.kpc
scale = cosmo.arcsec_per_kpc_proper(z)
D_ang = 2.430*Reff*scale
plt.figure(figsize=(8.0, 5.6))
plt.plot(z, D_ang, 'k-', linewidth=0.8)
plt.xlabel('$z$')
plt.ylabel('$\\theta_{M}$ [arcesc]')
plt.savefig('../../angscale.png', dpi=400)


## Scaling Relations
#MISM
boot_mism = np.load('mism_boot2.npy')
mism_m = boot_mism[:,1].reshape((8,5))
err_mism_m = (((boot_mism[:,1]-boot_mism[:,0]) + (boot_mism[:,2]-boot_mism[:,1]))/2).reshape((8,5))
z_avg = np.load('z_boot2.npy')[:,1].reshape((8,5))
mstar = np.load('mst_boot2.npy')[:,1].reshape((8,5))