Beispiel #1
0
def mag_in_filter(filter, spectrum, redshift):
    """
        Same as before just does not use em line.
    """
    f_lam = 0
    weight = 0
    
    if redshift == 0: redshift = 1e-8
    
    # calculate distances
    dp = 1e-3*2.99792458e8*cc.proper_distance(H_0, omega_m0, omega_r0, omega_lam0, 1/(1 + redshift))[0] * 1e6 * 3.09e18 # in cm
    dl = (1 + redshift) * dp
    
    # redshift spectrum
    # This part is for BC03 spectra
    
    lum_lam = spectrum['flux'] * solar_lum # bc03 spectra are in solar lum per angstrom
    flux_redshifted = lum_lam / (4 * np.pi * dl**2 * (1+redshift))
    lam_redshifted = spectrum['lambda'] * (1+redshift)

    # calculate magnitude
    # filter['lambda'] is in nm # convert all to angstroms

    for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i]*10)))
        # the spectrum's index for the wavelength of the throughput curve
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] *\
                 np.exp(-1 * igm_trans(lam_redshifted[lam_index], redshift)) / (3631 * c * 1e-23)
        # in the above expression: wavelength in angstroms, flux density in ergs/s/cm^2/angstrom, filter throughput in fractions
        weight += filter['throughput'][i]

    #print f_lam, weight
    mag = -2.5 * np.log10(f_lam/weight)
    
    return mag
Beispiel #2
0
def mag_in_filter_line(filter, spectrum, redshift, ew):
    f_lam = 0
    weight = 0
    
    if redshift == 0: redshift = 1e-8
    
    # calculate distances
    dp = 1e-3*2.99792458e8*cc.proper_distance(H_0, omega_m0, omega_r0, omega_lam0, 1/(1 + redshift))[0] * 1e6 * 3.09e18 # in cm
    dl = (1 + redshift) * dp
    
    # redshift spectrum
    # This part is for BC03 spectra
    
    lum_lam = spectrum['flux'] * solar_lum # bc03 spectra are in solar lum per angstrom
    flux_redshifted = lum_lam / (4 * np.pi * dl**2 * (1+redshift))
    lam_redshifted = spectrum['lambda'] * (1+redshift)
    
    # add lines and check if it indeed becomes bluer in the corresponding filter
    lya_redshifted = 1215.67 * (1 + redshift)
    redshifted_lines = np.array([lya_redshifted], dtype='float64') # the len of this arr is the num of lines you want to add
    ew_rest = np.array([ew]) # rest frame eq width # this is assumed
    ew_obs = np.array([ew_rest * (1 + redshift)]) # this should have the same len as the redshifted_lines array
    # observed eq width
    
    # convert lam and flux arr to numpy arrays with dtype-'float64'
    # they were 'float32' before
    lam_redshifted = np.array([lam_redshifted], dtype='float64')
    flux_redshifted = np.array([flux_redshifted], dtype='float64')
    
    ind_arr = np.array([np.argmin(abs(lam_redshifted - redshifted_lines))])
    
    lam_redshifted = np.insert(lam_redshifted, ind_arr, redshifted_lines)
    flux_to_add = np.array([flux_redshifted[0][ind_arr[i]] * 2 * ew_obs[i] / \
                            (lam_redshifted[ind_arr[i] + 1] - lam_redshifted[ind_arr[i] - 1]) for i in range(len(ew_obs))])
    flux_redshifted = np.insert(flux_redshifted, ind_arr, flux_to_add)

    print ew_obs
    print flux_to_add, flux_redshifted[ind_arr] * 2 * ew_obs / (lam_redshifted[ind_arr + 1] - lam_redshifted[ind_arr - 1])

    for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i]*10))) # the spectrum's index for the wavelength of the throughput curve
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] *\
            np.exp(-1 * igm_trans(lam_redshifted[lam_index], redshift)) / (3631 * c * 1e-23)
        # in the above expression: wavelength in angstroms, flux density in ergs/s/cm^2/angstrom, filter throughput in fractions
        weight += filter['throughput'][i]

    mag = -2.5 * np.log10(f_lam/weight)
                            
    return mag
Beispiel #3
0
def mag_in_filter(filter, spectrum, redshift):
    """
        Same as before just does not use em line.
    """
    f_lam = 0
    weight = 0

    if redshift == 0: redshift = 1e-8

    # calculate distances
    dp = 1e-3 * 2.99792458e8 * cc.proper_distance(
        H_0, omega_m0, omega_r0, omega_lam0, 1 /
        (1 + redshift))[0] * 1e6 * 3.09e18  # in cm
    dl = (1 + redshift) * dp

    # redshift spectrum
    # This part is for BC03 spectra

    lum_lam = spectrum[
        'flux'] * solar_lum  # bc03 spectra are in solar lum per angstrom
    flux_redshifted = lum_lam / (4 * np.pi * dl**2 * (1 + redshift))
    lam_redshifted = spectrum['lambda'] * (1 + redshift)

    # calculate magnitude
    # filter['lambda'] is in nm # convert all to angstroms

    for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i] * 10)))
        # the spectrum's index for the wavelength of the throughput curve
        #print lam_redshifted[lam_index], filter['lambda'][i]*10, flux_redshifted[lam_index]
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] *\
                 np.exp(-1 * igm_trans(lam_redshifted[lam_index], redshift)) / (3631 * c * 1e-23)
        # in the above expression: wavelength in angstroms, flux density in ergs/s/cm^2/angstrom, filter throughput in fractions
        weight += filter['throughput'][i]

    #print sum(flux_redshifted), '\n'

    #print f_lam, weight, '\n'
    mag = -2.5 * np.log10(f_lam / weight)

    return mag
Beispiel #4
0
solar_lum = 3.86e33
#spec_l_nu = spec['Flux'] * solar_lum * (spec['Lambda'])**2 / 2.99792458e18
spec_l_lam = spec['Flux'] * solar_lum

# Convert to Flux
# Redshift template spectra
# Redshift range
test_z = np.array((0.5, 1.0, 2.0, 3.0, 5.0))

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, title = '$Redshifted\ Template\ Spectra$')
ax1.set_xlabel('$\lambda\ [\mu m]$', fontsize=14)
ax1.set_ylabel('$F_{\lambda}\ [erg\ s^{-1}\ cm^{-2}\ \AA^{-1}]$', fontsize=14)
ax1.set_yscale('log')
plot_labels = ['0.5', '1.0', '2.0', '3.0', '5.0']

for count in range(len(test_z)):
    f_nu = 0
    redshift = test_z[count]
    dp = 1e-3*3e8*cc.proper_distance(H_0, omega_m0, omega_r0, omega_lam0, 1/(1 + redshift))[0] * 1e6 * 3.09e18 # in cm
    dl = (1 + redshift) * dp
    f_lam = spec_l_lam / (4 * np.pi * dl**2 * (1 + redshift))
    ax1.plot(spec['Lambda'] * (1 + redshift)/1e4, f_lam, label=plot_labels[count])

ax1.set_xlim(0.07, 3)
ax1.minorticks_on()
ax1.tick_params('both', width=1, length=3, which='minor')
ax1.tick_params('both', width=1, length=4.7, which='major')
ax1.legend(loc=0, numpoints=1, prop={'size':14})

plt.show()
Beispiel #5
0
def mag_in_filter_line(filter, spectrum, redshift, ew):
    """
        Takes in f_lambda in the rest frame and the throughput
        curve of the filter and spits out the AB magnitude of the
        given spectrum in the filter.
        Also needs redshift.
        
        It can also use SDSS spectra which won't need to be redshifted
        and calculate magnitudes in chosen filters with or without lines
        added.
    """
    f_lam = 0
    weight = 0

    if redshift == 0: redshift = 1e-8

    # calculate distances
    dp = 1e-3 * 2.99792458e8 * cc.proper_distance(
        H_0, omega_m0, omega_r0, omega_lam0, 1 /
        (1 + redshift))[0] * 1e6 * 3.08567758e18  # in cm
    dl = (1 + redshift) * dp

    # redshift spectrum
    # This part is for BC03 spectra

    lum_lam = spectrum[
        'flux'] * solar_lum  # bc03 spectra are in solar lum per angstrom
    flux_redshifted = lum_lam / (4 * np.pi * dl**2 * (1 + redshift))
    lam_redshifted = spectrum['lambda'] * (1 + redshift)

    # add lines and check if it indeed becomes bluer in the corresponding filter
    lya_redshifted = 1215.67 * (1 + redshift)
    redshifted_lines = np.array(
        [lya_redshifted], dtype='float64'
    )  # the len of this arr is the num of lines you want to add
    ew_rest = np.array([ew])  # rest frame eq width # this is assumed
    ew_obs = np.array([
        ew_rest * (1 + redshift)
    ])  # this should have the same len as the redshifted_lines array
    # observed eq width

    # convert lam and flux arr to numpy arrays with dtype-'float64'
    # they were 'float32' before
    lam_redshifted = np.array([lam_redshifted], dtype='float64')
    flux_redshifted = np.array([flux_redshifted], dtype='float64')

    flux_redshifted_noline = flux_redshifted

    ind_arr = np.array([np.argmin(abs(lam_redshifted - redshifted_lines))])
    lam_redshifted = np.insert(lam_redshifted, ind_arr[0], redshifted_lines)
    print lam_redshifted[150:155]
    #print lam_redshifted[ind_arr[0] + 1] - lam_redshifted[ind_arr[0] - 1]
    flux_to_add = np.array([flux_redshifted[0][ind_arr[i]] * 2 * ew_obs[i] / \
                            (lam_redshifted[ind_arr[i] + 1] - lam_redshifted[ind_arr[i] - 1]) for i in range(len(ew_obs))])
    flux_redshifted = np.insert(flux_redshifted, ind_arr[0], flux_to_add)

    #print ind_arr
    print flux_to_add
    #print flux_redshifted[150:155]

    if flux_to_add < 0:
        print flux_to_add, "Error! Negative flux for emission line"

    # calculate magnitude
    # filter['lambda'] is in nm # convert all to angstroms
    count_for_noline = 0
    f_lam_noline = 0

    for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i] * 10)))
        # the spectrum's index for the wavelength of the throughput curve
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] *\
            np.exp(-1 * igm_trans(lam_redshifted[lam_index], redshift)) / (3631 * c * 1e-23)
        f_lam_noline += flux_redshifted_noline[0][lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] *\
                    np.exp(-1 * igm_trans(lam_redshifted[lam_index], redshift)) / (3631 * c * 1e-23)
        # in the above expression: wavelength in angstroms, flux density in ergs/s/cm^2/angstrom, filter throughput in fractions
        if f_lam != f_lam_noline:
            #print i
            print lam_redshifted[lam_index], filter['lambda'][i] * 10
            print filter['throughput'][i]
            print flux_redshifted[lam_index - 2:lam_index +
                                  3], flux_redshifted_noline[0][lam_index -
                                                                2:lam_index +
                                                                3]
            print f_lam, f_lam_noline
            print '\n'
            break

        weight += filter['throughput'][i]
        count_for_noline += 1


# for debugging try print flam, ON EACH STEP, using flux_redshifted with line and
# flux_redshifted without the line (along with the corresponding lambdas of course)
# and check the step where it starts deviating

# also add line to flat spectrum which has all values zero

# This part is for SDSS spectra
    """
        sdss_spectrum = spectrum
        flux_redshifted = sdss_spectrum['flux'] * 1e-17
        lam_redshifted = np.power(10, sdss_spectrum['loglam'])
        
        
        # add lines and check if it indeed becomes bluer in the corresponding filter
        index1 = np.argmin(abs(lam_redshifted - 7000))
        index2 = np.argmin(abs(lam_redshifted - 6200))
        
        lam_redshifted = np.array([lam_redshifted], dtype='float64')
        flux_redshifted = np.array([flux_redshifted], dtype='float64')
        
        lam_redshifted = np.insert(lam_redshifted, [index1, index2], [7000.0, 6200])
        flux_redshifted = np.insert(flux_redshifted, [index1, index2],\
        [flux_redshifted[0][index1] * 236, flux_redshifted[0][index2] * 472])
        
        
        for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i]*10)))
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] / (3631 * c * 1e-23)
        weight += filter['throughput'][i]
    """
    #print sum(flux_redshifted)
    #print flux_to_add
    #print f_lam, weight
    mag = -2.5 * np.log10(f_lam / weight)

    return mag
Beispiel #6
0
# Convert to Flux
# Redshift template spectra
# Redshift range
test_z = np.array((0.5, 1.0, 2.0, 3.0, 5.0))

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, title='$Redshifted\ Template\ Spectra$')
ax1.set_xlabel('$\lambda\ [\mu m]$', fontsize=14)
ax1.set_ylabel('$F_{\lambda}\ [erg\ s^{-1}\ cm^{-2}\ \AA^{-1}]$', fontsize=14)
ax1.set_yscale('log')
plot_labels = ['0.5', '1.0', '2.0', '3.0', '5.0']

for count in range(len(test_z)):
    f_nu = 0
    redshift = test_z[count]
    dp = 1e-3 * 3e8 * cc.proper_distance(
        H_0, omega_m0, omega_r0, omega_lam0, 1 /
        (1 + redshift))[0] * 1e6 * 3.09e18  # in cm
    dl = (1 + redshift) * dp
    f_lam = spec_l_lam / (4 * np.pi * dl**2 * (1 + redshift))
    ax1.plot(spec['Lambda'] * (1 + redshift) / 1e4,
             f_lam,
             label=plot_labels[count])

ax1.set_xlim(0.07, 3)
ax1.minorticks_on()
ax1.tick_params('both', width=1, length=3, which='minor')
ax1.tick_params('both', width=1, length=4.7, which='major')
ax1.legend(loc=0, numpoints=1, prop={'size': 14})

plt.show()
Beispiel #7
0
def mag_in_filter_line(filter, spectrum, redshift, ew):
    """
        Takes in f_lambda in the rest frame and the throughput
        curve of the filter and spits out the AB magnitude of the
        given spectrum in the filter.
        Also needs redshift.
        
        It can also use SDSS spectra which won't need to be redshifted
        and calculate magnitudes in chosen filters with or without lines
        added.
    """
    f_lam = 0
    weight = 0
    
    if redshift == 0: redshift = 1e-8
    
    # calculate distances
    dp = 1e-3*2.99792458e8*cc.proper_distance(H_0, omega_m0, omega_r0, omega_lam0, 1/(1 + redshift))[0] * 1e6 * 3.09e18 # in cm
    dl = (1 + redshift) * dp
    
    # redshift spectrum
    # This part is for BC03 spectra
    
    lum_lam = spectrum['flux'] * solar_lum # bc03 spectra are in solar lum per angstrom
    flux_redshifted = lum_lam / (4 * np.pi * dl**2 * (1+redshift))
    lam_redshifted = spectrum['lambda'] * (1+redshift)

    # add lines and check if it indeed becomes bluer in the corresponding filter
    lya_redshifted = 1215.67 * (1 + redshift)
    redshifted_lines = np.array([lya_redshifted], dtype='float64') # the len of this arr is the num of lines you want to add
    ew_rest = np.array([ew]) # rest frame eq width # this is assumed
    ew_obs = np.array([ew_rest * (1 + redshift)]) # this should have the same len as the redshifted_lines array
    # observed eq width
    
    # convert lam and flux arr to numpy arrays with dtype-'float64'
    # they were 'float32' before
    lam_redshifted = np.array([lam_redshifted], dtype='float64')
    flux_redshifted = np.array([flux_redshifted], dtype='float64')
    
    ind_arr = np.array([np.argmin(abs(lam_redshifted - redshifted_lines))])
    lam_redshifted = np.insert(lam_redshifted, ind_arr[0] + 1, redshifted_lines)
    flux_to_add = np.array([flux_redshifted[0][ind_arr[i]] * ew_obs[i] / \
                            (lam_redshifted[ind_arr[i] + 1] - lam_redshifted[ind_arr[i]]) for i in range(len(ew_obs))])
    flux_redshifted = np.insert(flux_redshifted, ind_arr[0] + 1, flux_to_add)
    
    if flux_to_add < 0 :
        print flux_to_add, "Error"
    
    # calculate magnitude
    # filter['lambda'] is in nm # convert all to angstroms
    
    for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i]*10))) # the spectrum's index for the wavelength of the throughput curve
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] *\
                 np.exp(-1 * igm_trans(lam_redshifted[lam_index], redshift)) / (3631 * c * 1e-23)
        # in the above expression: wavelength in angstroms, flux density in ergs/s/cm^2/angstrom, filter throughput in fractions
        weight += filter['throughput'][i]
    
    
    # This part is for SDSS spectra
    """
    sdss_spectrum = spectrum
    flux_redshifted = sdss_spectrum['flux'] * 1e-17
    lam_redshifted = np.power(10, sdss_spectrum['loglam'])
    

    # add lines and check if it indeed becomes bluer in the corresponding filter
    index1 = np.argmin(abs(lam_redshifted - 7000))
    index2 = np.argmin(abs(lam_redshifted - 6200))
    
    lam_redshifted = np.array([lam_redshifted], dtype='float64')
    flux_redshifted = np.array([flux_redshifted], dtype='float64')
    
    lam_redshifted = np.insert(lam_redshifted, [index1, index2], [7000.0, 6200])
    flux_redshifted = np.insert(flux_redshifted, [index1, index2],\
                                [flux_redshifted[0][index1] * 236, flux_redshifted[0][index2] * 472])
    
    
    for i in range(len(filter)):
        lam_index = np.argmin(abs(lam_redshifted - (filter['lambda'][i]*10)))
        f_lam += flux_redshifted[lam_index] * lam_redshifted[lam_index]**2 * filter['throughput'][i] / (3631 * c * 1e-23)
        weight += filter['throughput'][i]
    """
    #print f_lam, weight
    mag = -2.5 * np.log10(f_lam/weight)

    return mag