示例#1
0
def host_correction(spec, undo=False):
    #TODO:finish, apply before scaling

    wave = spec.wavelength[spec.x1:spec.x2]
    flux = spec.flux[spec.x1:spec.x2]
    ivar = spec.ivar[spec.x1:spec.x2]
    if spec.av_25 != None:
        Av_host = spec.av_25
        rv = 2.5
    elif spec.av_mlcs31 != None:
        Av_host = spec.av_mlcs31
        rv = 3.1
    elif spec.av_mlcs17 != None:
        Av_host = spec.av_mlcs17
        rv = 1.7
    else:
        print 'No Host Correction'
        return spec

    wave_u = wave * u.Angstrom
    flux_u = flux * u.Unit('W m-2 angstrom-1 sr-1')
    spec1d = Spectrum1D.from_array(wave_u, flux_u)
    spec1d_ivar = Spectrum1D.from_array(wave_u, ivar)
    red = ex.reddening(spec1d.wavelength, a_v=Av_host, r_v=rv, model='f99')
    if not undo:
        flux_new = spec1d.flux * red
        ivar_new = spec1d_ivar.flux * (1. / (red**2.))
    else:
        flux_new = spec1d.flux / red
        ivar_new = spec1d_ivar.flux / (1. / (red**2.))

    spec.flux[spec.x1:spec.x2] = flux_new
    spec.ivar[spec.x1:spec.x2] = ivar_new

    return spec
示例#2
0
def test_normalize():
    w = np.arange(4000., 5000., 10.)
    coeff = [1., 0.1, 0.1]
    pol = Polynomial(coeff)
    obs_spectrum = Spectrum1D.from_array(w, pol(w), dispersion_unit=u.AA)
    norm = Normalize(obs_spectrum, npol=3)
    model = Spectrum1D.from_array(w, np.ones_like(w), dispersion_unit=u.AA)
    fit = norm(model)
    npt.assert_allclose(fit.flux, obs_spectrum.flux)
    npt.assert_allclose(norm.polynomial(obs_spectrum.wavelength.value),
                        obs_spectrum.flux)
    npt.assert_allclose(norm.polynomial.convert().coef,
                        np.array(coeff + [0.]), rtol=1e-5, atol=1.e-10)
示例#3
0
def compprep(waves, fluxes, variances, redshift, ebv, dereddened, deredshifted,
             u_fluxes):
    old_wave = waves
    old_flux = fluxes

    old_wave_r = waves * u.Angstrom
    old_flux_r = fluxes * u.Unit(u_fluxes)  # fluxes
    old_error = variances  # check if supernovae has error array
    spec1d = Spectrum1D.from_array(old_wave, old_flux)

    old_ivar = genivar(old_wave, old_flux,
                       old_error)  # generate inverse variance
    snr = getsnr(old_flux, old_ivar)
    newdata = []

    if not dereddened:
        new_flux = dered(
            ebv, old_wave_r, old_flux_r
        )  # Dereddending (see if sne in extinction files match the SN name)
    if not deredshifted:
        new_wave = old_wave / (1. + redshift)  # Deredshifting

    new_error = old_error  # Placeholder if it needs to be changed
    new_ivar = genivar(new_wave, new_flux,
                       new_error)  # generate new inverse variance
    #var = new_flux*0+1
    newdata = Interpo(new_wave, new_flux, new_ivar)  # Do the interpolation
    #    print 'new spectra',newdata
    return newdata, snr
示例#4
0
def test_spectrum1d_fromarray_quantity():
    test_spec = Spectrum1D.from_array(np.arange(3000, 9000) * u.angstrom,
                          np.random.random(6000))

    assert hasattr(test_spec, 'wavelength')

    assert test_spec.dispersion_unit == u.angstrom
示例#5
0
def test_spectrum1d_fromarray_quantity():
    test_spec = Spectrum1D.from_array(
        np.arange(3000, 9000) * u.angstrom, np.random.random(6000))

    assert hasattr(test_spec, 'wavelength')

    assert test_spec.dispersion_unit == u.angstrom
示例#6
0
def test_spectrum1d_flux1():
        test_spec = Spectrum1D.from_array(np.arange(3000, 9000) * u.angstrom,
                          np.random.random(6000) * u.erg/u.s,
                          )

        assert not hasattr(test_spec.data, 'unit')
        assert test_spec.flux.unit == u.erg / u.s
        assert test_spec.unit == u.erg / u.s
示例#7
0
def test_interpolate():
    obs_spectrum = Spectrum1D.from_array(np.arange(1,26), np.zeros(25),
                                         dispersion_unit='micron',
                                         unit='erg/(cm^2 s Angstrom)')
    test_spectrum = Spectrum1D.from_array(np.arange(0.5, 26.5, 1),
                                          np.arange(0.5, 26.5, 1),
                                          dispersion_unit='micron',
                                          unit='erg/(cm^2 s Angstrom)')

    interpolate_plugin = Interpolate(obs_spectrum)
    interpolated_test_spectrum = interpolate_plugin(test_spectrum)

    expected_interpolated_flux = np.arange(1,26)
    interpolated_test_flux = interpolated_test_spectrum.flux

    npt.assert_array_almost_equal(interpolated_test_flux,
                                  expected_interpolated_flux, decimal=6)
示例#8
0
def read_spec(ispec, second_file=None):
    '''Parse spectrum out of the input
    Parameters:
    -----------
    ispec: Spectrum1D, str, or tuple

    Returns:
    -----------
    spec: XSpectrum1D 
    spec_file: str
    '''
    from specutils import Spectrum1D
    from linetools.spectra.utils import XSpectrum1D
    #
    if isinstance(ispec,basestring):
        spec_fil = ispec
        spec = lsi.readspec(spec_fil)
        # Second file?
        if not second_file is None:
            spec2 = lsi.readspec(second_file)
            if spec2.sig is None:
                spec2.sig = np.zeros(spec.flux.size)
            # Scale for convenience of plotting
            xper1 = xstats.basic.perc(spec.flux, per=0.9)
            xper2 = xstats.basic.perc(spec2.flux, per=0.9)
            scl = xper1[1]/xper2[1]
            # Stitch together
            wave3 = np.append(spec.dispersion, spec2.dispersion)
            flux3 = np.append(spec.flux, spec2.flux*scl)
            sig3 = np.append(spec.sig, spec2.sig*scl)
            spec3 = Spectrum1D.from_array(wave3, flux3, uncertainty=StdDevUncertainty(sig3))
            # Overwrite
            spec = spec3
            spec.filename = spec_fil
    elif isinstance(ispec,Spectrum1D):
        spec = ispec # Assuming Spectrum1D
        spec_fil = spec.filename # Grab from Spectrum1D 
    elif isinstance(ispec,tuple):
        # Units
        try:
            wv_unit = ispec[0].unit
        except AttributeError:
            wv_unit = u.AA
        uwave = u.Quantity(ispec[0], unit=wv_unit)
        # Generate
        if len(ispec) == 2: # wave, flux
            spec = XSpectrum1D.from_array(uwave, u.Quantity(ispec[1])) 
        else:
            spec = XSpectrum1D.from_array(uwave, u.Quantity(ispec[1]), 
                uncertainty=StdDevUncertainty(ispec[2]))
        #
        spec_fil = 'none'
        spec.filename = spec_fil
    else:
        raise ValueError('Bad input to read_spec')

    # Return
    return spec, spec_fil
示例#9
0
def test_spectrum1d_fromarray_quantity2():
    test_spec = Spectrum1D.from_array(np.arange(3000, 9000) * u.angstrom,
                          np.random.random(6000), dispersion_unit='nm')

    assert hasattr(test_spec, 'wavelength')

    assert test_spec.dispersion_unit == u.nm
    nptesting.assert_allclose(test_spec.wavelength.value,
                               np.arange(3000, 9000) / 10.)
示例#10
0
文件: plugins.py 项目: mhvk/specgrid
 def __call__(self, spectrum):
     wavelength, flux = spectrum.wavelength.value, spectrum.flux
     interpolated_flux = np.interp(self.observed.wavelength.value,
                                   wavelength, flux)
     return Spectrum1D.from_array(
         self.observed.wavelength,
         interpolated_flux,
         dispersion_unit=self.observed.wavelength.unit,
         unit=self.observed.unit)
示例#11
0
def test_spectrum1d_flux1():
    test_spec = Spectrum1D.from_array(
        np.arange(3000, 9000) * u.angstrom,
        np.random.random(6000) * u.erg / u.s,
    )

    assert not hasattr(test_spec.data, 'unit')
    assert test_spec.flux.unit == u.erg / u.s
    assert test_spec.unit == u.erg / u.s
示例#12
0
def get_vega(vega_file=None):
    if vega_file is None:
        vega_file = default_vega
    vega_table = fits.getdata(get_calibration_dir(vega_file), extension=1)
    vega = Spectrum1D.from_array(
        vega_table['wavelength'] * u.angstrom,
        vega_table['flux'] * u.erg / u.s / u.cm**2 / u.angstrom)

    return vega
示例#13
0
    def __call__(self, spectrum):
        wavelength, flux = spectrum.wavelength.value, spectrum.flux.value

        interpolated_flux = np.interp(self.observed.wavelength.value,
                                      wavelength, flux)
        return Spectrum1D.from_array(
            self.observed.wavelength,
            interpolated_flux,
            dispersion_unit=self.observed.wavelength.unit,
            unit=self.spectrum.unit)
示例#14
0
def test_spectrum1d_fromarray_quantity2():
    test_spec = Spectrum1D.from_array(np.arange(3000, 9000) * u.angstrom,
                                      np.random.random(6000),
                                      dispersion_unit='nm')

    assert hasattr(test_spec, 'wavelength')

    assert test_spec.dispersion_unit == u.nm
    nptesting.assert_allclose(test_spec.wavelength.value,
                              np.arange(3000, 9000) / 10.)
示例#15
0
    def __call__(self, spectrum):

        from specutils import extinction

        extinction_factor = 10**(-0.4 * extinction.extinction_ccm89(
            spectrum.wavelength, a_v=self.a_v, r_v=self.r_v))

        return Spectrum1D.from_array(spectrum.wavelength.value,
                                     extinction_factor * spectrum.flux,
                                     dispersion_unit=spectrum.wavelength.unit,
                                     unit=spectrum.unit)
示例#16
0
文件: plugins.py 项目: mhvk/specgrid
    def __call__(self, spectrum):

        from specutils import extinction

        extinction_factor = 10**(-0.4*extinction.extinction_ccm89(
            spectrum.wavelength, a_v=self.a_v, r_v=self.r_v))


        return Spectrum1D.from_array(
            spectrum.wavelength.value,
            extinction_factor * spectrum.flux,
            dispersion_unit=spectrum.wavelength.unit, unit=spectrum.unit)
示例#17
0
文件: base.py 项目: afeldmei/wsynphot
    def __mul__(self, other):
        if not hasattr(other, 'flux') or not hasattr(other, 'wavelength'):
            raise ValueError(
                'requiring a specutils.Spectrum1D-like object that'
                'has attributes "flux" and "wavelength"')

        #new_wavelength = np.union1d(other.wavelength.to(self.wavelength.unit).value,
        #                            self.wavelength.value) * self.wavelength.unit
        transmission = self.interpolate(other.wavelength)

        return Spectrum1D.from_array(other.wavelength,
                                     transmission * other.flux)
示例#18
0
    def get_query_data(self,
                       filter_tuple,
                       plugin,
                       warning_threshold=1 * u.gigabyte):
        """
        Write spectra to disk
        :param filter_tuple:
        :param models:
        :return:
        """

        query = self.get_spectrum_query(filter_tuple)

        sample_spectrum_row = query.first()
        sample_spectrum_flux = plugin(
            sample_spectrum_row.get_spectrum1d().flux)

        no_spectra = query.count()

        size_of_spectra = (query.count() *
                           len(sample_spectrum_flux)) * 8 * u.byte

        if size_of_spectra > warning_threshold:
            continue_query = raw_input('The size of the spectra are {0:.2f}. '
                                       'Continue [y/N]'.format(
                                           size_of_spectra.to(
                                               warning_threshold.unit)))
            if continue_query.strip().lower() != 'y':
                raise ValueError('Size of requested grid ({:.2f}) to '
                                 'large for user ... aborting'.format(
                                     size_of_spectra.to(
                                         warning_threshold.unit)))

        fluxes = np.empty((query.count(), len(sample_spectrum_flux)))
        parameters = []
        param_names = [
            item.name for item in sample_spectrum_row.parameter_set.parameters
        ]

        for i, spectrum_row in enumerate(query):
            logger.info("{0} {1}/{2}".format(spectrum_row, i, no_spectra))
            spectrum = spectrum_row.get_spectrum1d()
            fluxes[i] = plugin(spectrum.flux)
            parameters.append([
                getattr(spectrum_row.parameter_set, key) for key in param_names
            ])

        parameters = pd.DataFrame(parameters, columns=param_names)
        output_sample_spectrum = Spectrum1D.from_array(
            plugin.output_wavelength * u.angstrom, sample_spectrum_flux)

        return output_sample_spectrum, parameters, fluxes
示例#19
0
def MW_correction(spec, undo=False):
    #TODO:finish, apply before scaling

    wave = spec.wavelength[spec.x1:spec.x2]
    flux = spec.flux[spec.x1:spec.x2]
    ivar = spec.ivar[spec.x1:spec.x2]
    av_mw = spec.av_mw

    wave_u = wave * u.Angstrom
    flux_u = flux * u.Unit('W m-2 angstrom-1 sr-1')
    spec1d = Spectrum1D.from_array(wave_u, flux_u)
    spec1d_ivar = Spectrum1D.from_array(wave_u, ivar)
    red = ex.reddening(spec1d.wavelength, a_v=av_mw, r_v=3.1, model='f99')
    if not undo:
        flux_new = spec1d.flux * red
        ivar_new = spec1d_ivar.flux * (1. / (red**2.))
    else:
        flux_new = spec1d.flux / red
        ivar_new = spec1d_ivar.flux / (1. / (red**2.))

    spec.flux[spec.x1:spec.x2] = flux_new
    spec.ivar[spec.x1:spec.x2] = ivar_new

    return spec
示例#20
0
文件: plugins.py 项目: mhvk/specgrid
 def __call__(self, spectrum):
     wavelength, flux = spectrum.wavelength.value, spectrum.flux
     log_grid_log_wavelength = np.arange(np.log(wavelength.min()),
                                         np.log(wavelength.max()),
                                         self.resolution.to(1).value)
     log_grid_wavelength = np.exp(log_grid_log_wavelength)
     log_grid_flux = np.interp(log_grid_wavelength, wavelength, flux)
     profile = self.rotational_profile()
     log_grid_convolved = nd.convolve1d(log_grid_flux, profile)
     convolved_flux = np.interp(wavelength, log_grid_wavelength,
                                log_grid_convolved)
     return Spectrum1D.from_array(spectrum.wavelength,
                                  convolved_flux,
                                  dispersion_unit=spectrum.wavelength.unit,
                                  unit=spectrum.unit)
示例#21
0
 def __call__(self, spectrum):
     wavelength, flux = spectrum.wavelength.value, spectrum.flux
     log_grid_log_wavelength = np.arange(np.log(wavelength.min()),
                                         np.log(wavelength.max()),
                                         self.resolution.to(1).value)
     log_grid_wavelength = np.exp(log_grid_log_wavelength)
     log_grid_flux = np.interp(log_grid_wavelength, wavelength, flux)
     profile = self.rotational_profile()
     log_grid_convolved = nd.convolve1d(log_grid_flux, profile)
     convolved_flux = np.interp(wavelength, log_grid_wavelength,
                                log_grid_convolved)
     return Spectrum1D.from_array(spectrum.wavelength,
                                  convolved_flux,
                                  dispersion_unit=spectrum.wavelength.unit,
                                  unit=spectrum.unit)
示例#22
0
def test_spectrum1d_flux2():
        test_spec = Spectrum1D.from_array(np.arange(3000, 9000) * u.angstrom,
                          np.random.random(6000) * u.erg/u.s,
                          )

        assert not hasattr(test_spec.data, 'unit')
        assert test_spec.flux.unit == u.erg / u.s
        assert test_spec.unit == u.erg / u.s
        new_flux = np.random.random(6000) * u.W

        test_spec.flux = new_flux

        assert test_spec.flux.unit == u.erg / u.s

        nptesting.assert_allclose(new_flux.to(u.erg / u.s).value,
                                  test_spec.data)
示例#23
0
def test_spectrum1d_flux2():
    test_spec = Spectrum1D.from_array(
        np.arange(3000, 9000) * u.angstrom,
        np.random.random(6000) * u.erg / u.s,
    )

    assert not hasattr(test_spec.data, 'unit')
    assert test_spec.flux.unit == u.erg / u.s
    assert test_spec.unit == u.erg / u.s
    new_flux = np.random.random(6000) * u.W

    test_spec.flux = new_flux

    assert test_spec.flux.unit == u.erg / u.s

    nptesting.assert_allclose(new_flux.to(u.erg / u.s).value, test_spec.data)
示例#24
0
    def to_spectrum(self):
        try:
            from specutils import Spectrum1D
        except ImportError:
            raise ImportError('specutils needed for this functionality')
        from xtool.fix_spectrum1d import Spectrum1D

        if getattr(self, 'amplitude_uncertainty', None) is None:
            uncertainty = None
        else:
            uncertainty = self.amplitude_uncertainty

        spec = Spectrum1D.from_array(self.wavelength_pixels * u.nm,
                                     self.amplitude.value,
                                     uncertainty=uncertainty)

        return spec
示例#25
0
    def get_query_data(self, filter_tuple, plugin,
                       warning_threshold=1 * u.gigabyte):
        """
        Write spectra to disk
        :param filter_tuple:
        :param models:
        :return:
        """

        query = self.get_spectrum_query(filter_tuple)

        sample_spectrum_row = query.first()
        sample_spectrum_flux = plugin(sample_spectrum_row.get_spectrum1d().flux)

        no_spectra = query.count()

        size_of_spectra = (query.count() *
                           len(sample_spectrum_flux)) * 8 * u.byte

        if size_of_spectra > warning_threshold:
            continue_query = raw_input('The size of the spectra are {0:.2f}. '
                                       'Continue [y/N]'.format(
                size_of_spectra.to(warning_threshold.unit)))
            if continue_query.strip().lower() != 'y':
                raise ValueError('Size of requested grid ({:.2f}) to '
                                 'large for user ... aborting'.format(
                    size_of_spectra.to(warning_threshold.unit)))

        fluxes = np.empty((query.count(),
                          len(sample_spectrum_flux)))
        parameters = []
        param_names = [item.name
                       for item in sample_spectrum_row.parameter_set.parameters]

        for i, spectrum_row in enumerate(query):
            print "{0}/{1}".format(i, no_spectra)
            spectrum = spectrum_row.get_spectrum1d()
            fluxes[i] = plugin(spectrum.flux)
            parameters.append([getattr(spectrum_row.parameter_set, key)
                               for key in param_names])

        parameters = pd.DataFrame(parameters, columns= param_names)
        output_sample_spectrum = Spectrum1D.from_array(
            plugin.output_wavelength * u.angstrom, sample_spectrum_flux)

        return output_sample_spectrum, parameters, fluxes
示例#26
0
    def to_spectrum(self):
        try:
            from specutils import Spectrum1D
        except ImportError:
            raise ImportError('specutils needed for this functionality')
        from xtool.fix_spectrum1d import Spectrum1D

        if getattr(self, 'amplitude_uncertainty', None) is None:
            uncertainty = None
        else:
            uncertainty = self.amplitude_uncertainty

        spec = Spectrum1D.from_array(
            self.wavelength_pixels * u.nm, self.amplitude.value,
            uncertainty=uncertainty)

        return spec
示例#27
0
def read_apogee(fitsfile,doppler=0,wave_range=None,aspcap=False,replace=None,mask=True):
    """ Read an APOGEE spectrum. The data model is described here:
    http://data.sdss3.org/datamodel/files/APOGEE_REDUX/APRED_VERS/APSTAR_VERS/TELESCOPE/LOCATION_ID/apStar.html

    Optional
    ========
    doppler - doppler correction (z = (labmda_observed - lambda_emitted)/lambda_emitted)
    wave_range - wavelength range to extract (in micron, default: None)
    aspcap - the spectrum is a best fit ASPCAP spectrum (default: False)
    mask - replace the bad pixels with NaNs (default: True)
    replace - a value to replace ones that are zero (defulat: None)
    """

    hdu =fits.open(fitsfile)

    h = hdu[1].header

    if aspcap:
        flux = hdu[1].data
    else:
        flux = hdu[1].data[0,:] # just take the first form of the combine (pixel weighted)

    pix = np.arange(h['NAXIS1'])+1 # fits start at the first pixel
    
    wave = 10.0**((h['CRVAL1']+h['CDELT1']*(pix - h['CRPIX1']))/(1.0+doppler))

    wave = wave/1e4  # convert to microns

    # mask out all bad pixels first
    if not aspcap:
        if mask:
            bad = np.where(hdu[3].data[0,:] == 1)[0]
            flux[bad] = np.nan

    if wave_range is not None:
        good = np.where((wave >= wave_range[0]) & (wave <= wave_range[1]))[0]
        wave = wave[good]
        flux = flux[good]
    
    if replace is not None:
        bad = np.where(flux == 0)[0]
        flux[bad] = replace
    spectrum = Spectrum1D.from_array(wave, flux, dispersion_unit='u.micron')
    
    return spectrum
示例#28
0
文件: plugins.py 项目: mhvk/specgrid
    def __call__(self,spectrum):
        R = self.resolution
        Lambda = self.central_wavelength.value
        wavelength = spectrum.dispersion.value

        conversionfactor = 2 * np.sqrt(2 * np.log(2))
        deltax = np.mean(wavelength[1:] - wavelength[0:-1])
        FWHM = Lambda/R
        sigma = (FWHM/deltax)/conversionfactor

        flux = spectrum.flux

        convolved_flux = gaussian_filter1d(flux, sigma, axis=0, order=0)

        return Spectrum1D.from_array(
            spectrum.dispersion,
            convolved_flux,
            dispersion_unit=spectrum.dispersion.unit, unit=spectrum.unit)
示例#29
0
def get_phoenix_model_spectrum(T_eff, log_g=4.5, cache=True):
    """
    Download a PHOENIX model atmosphere spectrum for a star with given
    properties.

    Parameters
    ----------
    T_eff : float
        Effective temperature. The nearest grid-temperature will be selected.
    log_g : float
        This must be a log g included in the grid for the effective temperature
        nearest ``T_eff``.
    cache : bool
        Cache the result to the local astropy cache. Default is `True`.

    Returns
    -------
    spectrum : `~specutils.Spectrum1D`
        Model spectrum
    """
    url = get_url(T_eff=T_eff, log_g=log_g)
    fluxes_path = download_file(url, cache=cache, timeout=30)
    fluxes = fits.getdata(fluxes_path)

    wavelength_url = (
        'ftp://phoenix.astro.physik.uni-goettingen.de/v2.0/HiResFITS/'
        'WAVE_PHOENIX-ACES-AGSS-COND-2011.fits')
    wavelength_path = download_file(wavelength_url, cache=cache, timeout=30)
    wavelengths_vacuum = fits.getdata(wavelength_path)

    # Wavelengths are provided at vacuum wavelengths. For ground-based
    # observations convert this to wavelengths in air, as described in
    # Husser 2013, Eqns. 8-10:
    sigma_2 = (10**4 / wavelengths_vacuum)**2
    f = (1.0 + 0.05792105 / (238.0185 - sigma_2) + 0.00167917 /
         (57.362 - sigma_2))
    wavelengths_air = wavelengths_vacuum / f

    spectrum = Spectrum1D.from_array(wavelengths_air,
                                     fluxes,
                                     dispersion_unit=u.Angstrom)

    return spectrum
示例#30
0
def compprep(spectrum, sn_name, z, source):
    old_wave = spectrum[:, 0]  # wavelengths
    old_flux = spectrum[:, 1]  # fluxes
    try:
        old_error = spectrum[:, 2]  # check if supernovae has error array
    except IndexError:
        old_error = np.array([0])  # if not, set default
    if sn_name == '2011fe':
        old_error = np.sqrt(old_error)
    old_ivar = df.genivar(old_wave, old_flux,
                          old_error)  # generate inverse variance
    snr = prep.getsnr(old_flux, old_ivar)

    if source == 'cfa':  # choosing source dataset
        sne = prep.ReadExtin('extinction.dat')
    if source == 'bsnip':
        sne = prep.ReadExtin('extinctionbsnip.dat')
    if source == 'csp':
        sne = prep.ReadExtin('extinctioncsp.dat')
        old_wave *= 1 + float(z)  # Redshift back
    if source == 'uv':
        sne = prep.ReadExtin('extinctionuv.dat')
    if source == 'other':
        sne = prep.ReadExtin('extinctionother.dat')

    newdata = []
    old_wave = old_wave * u.Angstrom
    old_flux = old_flux * u.Unit('W m-2 angstrom-1 sr-1')
    spec1d = Spectrum1D.from_array(old_wave, old_flux)
    test_flux = test_dered.dered(sne, sn_name, spec1d.wavelength, spec1d.flux)
    new_flux = test_flux.value
    old_wave = old_wave.value
    old_wave = old_wave / (1. + z)

    old_flux = np.asarray(old_flux)
    new_flux = np.asarray(new_flux)
    # s = scale_composites_in_range(old_flux, new_flux)
    # old_flux = s*old_flux
    new_wave = old_wave / (1. + z)
    new_error = old_error
    new_ivar = df.genivar(new_wave, new_flux, new_error)
    newdata = prep.Interpo(new_wave, new_flux, new_ivar)
    return newdata, snr
示例#31
0
def test_convolution():
    my_spec = Spectrum1D.from_array(np.linspace(6000, 8000, 2000),
                                    np.ones(2000), dispersion_unit='Angstrom',
                                    unit='erg/(cm^2 s Angstrom)')
    my_spec.flux[1000] = 0.0

    R = 5000.
    central_wavelength = 7000. * u.Angstrom

    convolve_plugin = Convolve(R, central_wavelength)
    conv_spectrum = convolve_plugin(my_spec)
    assert conv_spectrum.flux[999] < 1.
    assert conv_spectrum.flux[1000] > 0

    integral_pre_convolved = simps(my_spec.flux, my_spec.wavelength.value)
    integral_convolved = simps(conv_spectrum.flux,
                               conv_spectrum.wavelength.value)

    npt.assert_allclose(integral_pre_convolved, integral_convolved,
                        rtol=1.0, atol=0.0002)
示例#32
0
    def eval(self, teff, logg, feh):
        """
        Interpolating on the grid to the necessary parameters

        Parameters
        ----------

        teff: float
            effective temperature
        logg: float
            base ten logarithm of surface gravity in cgs
        feh: float
            [Fe/H]

        """
        flux = self.interpolate_grid(teff, logg, feh)
        return Spectrum1D.from_array(self.wavelength,
                                     flux,
                                     dispersion_unit=u.angstrom,
                                     unit=u.Unit('erg/ (cm2 s Angstrom)'))
示例#33
0
    def eval(self, *args):
        """
        Interpolating on the grid to the necessary parameters

        Parameters
        ----------

        teff: float
            effective temperature
        logg: float
            base ten logarithm of surface gravity in cgs
        feh: float
            [Fe/H]

        """
        flux = self.interpolate_grid(*args)
        return Spectrum1D.from_array(self.wavelength.value,
                                     flux,
                                     dispersion_unit=self.wavelength.unit,
                                     unit=self.flux_unit)
示例#34
0
    def extract_spectrum(self):
        """Extract 1D spectrum from the information provided so far and
           createa  `~specutils.Spectrum1D` object

        """
        try:
            from specutils import Spectrum1D
        except:
            from .spectrum1d import Spectrum1D

        if self.wavelength is None:
            raise ValueError('wavelength is None')
        if self.wavelength_unit is None:
            raise ValueError('wavelength_unit is None')
        if self.flux is None:
            raise ValueError('flux is None')
        if self.flux_unit is None:
            raise ValueError('flux_unit is None')

        wave = self.wavelength * self.wavelength_unit
        flux = self.flux * self.flux_unit
        return Spectrum1D.from_array(wave, flux)
示例#35
0
    def extract_spectrum(self):
        """Extract 1D spectrum from the information provided so far and
           createa  `~specutils.Spectrum1D` object

        """
        try:
            from specutils import Spectrum1D
        except:
            from .spectrum1d import Spectrum1D

        if self.wavelength is None:
            raise ValueError('wavelength is None')
        if self.wavelength_unit is None:
            raise ValueError('wavelength_unit is None')
        if self.flux is None:
            raise ValueError('flux is None')
        if self.flux_unit is None:
            raise ValueError('flux_unit is None')

        wave = self.wavelength * self.wavelength_unit
        flux = self.flux * self.flux_unit
        return Spectrum1D.from_array(wave, flux)
示例#36
0
文件: plugins.py 项目: mhvk/specgrid
    def __call__(self, model):
        rcond = (len(self.observed.flux) *
                 np.finfo(self.observed.flux.dtype).eps)
        # V[:,0]=mfi/e, Vp[:,1]=mfi/e*w, .., Vp[:,npol]=mfi/e*w**npol
        V = self._Vp * (model.flux / self.uncertainty)[:,np.newaxis]
        # normalizes different powers
        scl = np.sqrt((V*V).sum(0))
        sol, resids, rank, s = np.linalg.lstsq(V/scl, self.signal_to_noise,
                                               rcond)
        sol = (sol.T/scl).T
        if rank != self._Vp.shape[-1] - 1:
            msg = "The fit may be poorly conditioned"
            warnings.warn(msg)

        fit = np.dot(V, sol) * self.uncertainty

        # keep coefficients in case the outside wants to look at it
        self.polynomial = Polynomial(sol, domain=self.domain.value,
                                     window=self.window.value)

        return Spectrum1D.from_array(
            self.observed.wavelength.value,
            fit, unit=self.observed.unit,
            dispersion_unit=self.observed.wavelength.unit)
for i in range(len(points)):

    removed_points = points[i]
    removed_flux = values[i]

    print removed_points
    print removed_flux

    grid.fluxes = np.delete(values, i, axis=0)
    grid.index = np.delete(points, i, axis=0)

    print len(grid.index)

    starspectrum = Spectrum1D.from_array(dispersion=wavelengths,
                                         flux=removed_flux,
                                         dispersion_unit=u.angstrom,
                                         uncertainty=removed_flux * (1 / 100.))

    interp1 = Interpolate(starspectrum)
    norm1 = Normalize(starspectrum, 2)

    model = grid | interp1 | norm1

    setattr(model, 'teff_0', removed_points[0])
    setattr(model, 'logg_0', removed_points[1])
    setattr(model, 'mh_0', removed_points[2])
    setattr(model, 'alpha_0', removed_points[3])
    '''
    result = mtf.fit_array(starspectrum, model, R_fixed=25000.)

    print result.median
示例#38
0
 def get_spectrum1d(self):
     flux = self._read_flux()
     return Spectrum1D.from_array(self.wavelength, flux,
                                  unit=u.Unit(self.flux_unit))
示例#39
0
from specutils import Spectrum1D
from astropy import units
import numpy as np
dispersion = np.arange(4000, 5000, 0.12)
flux = np.random.randn(len(dispersion))
mySpectrum = Spectrum1D.from_array(dispersion, flux, dispersion_unit=units.m)

hBeta = mySpectrum.slice_dispersion(4851.0, 4871.0)
hBeta
示例#40
0
 def sum_order(self, order):
     wavelengths = self.es_list[0].get_order(order).wavelength
     total_flux = np.sum([spectrum.get_order(order).flux 
                          for spectrum in self.es_list], axis=0)
     return Spectrum1D.from_array(wavelengths, total_flux)
示例#41
0
def compprep(spectrum, sn_name, z, source):
    old_wave = spectrum[:, 0]  # wavelengths
    old_flux = spectrum[:, 1]  # fluxes
    try:
        old_error = spectrum[:, 2]  # check if supernovae has error array
    except IndexError:
        old_error = np.array([0])  # if not, set default
    old_ivar = df.genivar(old_wave, old_flux,
                          old_error)  # generate inverse variance
    snr = prep.getsnr(old_flux, old_ivar)

    if source == 'cfa':  # choosing source dataset
        #        z = ReadParam()
        sne = prep.ReadExtin('extinction.dat')
    if source == 'bsnip':
        sne = prep.ReadExtin('extinctionbsnip.dat')
    if source == 'csp':
        sne = prep.ReadExtin('extinctioncsp.dat')
        old_wave *= 1 + float(z)  # Redshift back
    if source == 'uv':
        sne = prep.ReadExtin('extinctionuv.dat')
    if source == 'other':
        sne = prep.ReadExtin('extinctionother.dat')

#     host_reddened = ReadExtin('../data/info_files/ryan_av.txt')
    newdata = []
    old_wave = old_wave * u.Angstrom  # wavelengths
    old_flux = old_flux * u.Unit('W m-2 angstrom-1 sr-1')
    spec1d = Spectrum1D.from_array(old_wave, old_flux)
    test_flux = test_dered.dered(
        sne, sn_name, spec1d.wavelength, spec1d.flux
    )  # Deredenning (see if sne in extinction files match the SN name)
    #     new_flux = host_correction(sne, sn_name, old_wave, new_flux)

    # new_flux = old_flux
    new_flux = test_flux.value
    old_wave = old_wave.value
    old_wave = old_wave / (1. + z)

    old_flux = np.asarray(old_flux)
    new_flux = np.asarray(new_flux)
    s = scale_composites_in_range(old_flux, new_flux)
    old_flux = s * old_flux

    # plt.rc('font', family='serif')
    # fig, ax = plt.subplots(1,1)
    # fig.set_size_inches(10, 8, forward = True)
    # ax.get_yaxis().set_ticks([])
    # plt.plot(old_wave, old_flux, linewidth = 2, color = 'r')
    # plt.plot(old_wave, new_flux, linewidth = 2, color = '#3F5D7D')
    # plt.ylabel('Relative Flux')
    # plt.xlabel('Wavelength ' + "($\mathrm{\AA}$)")
    # # plt.savefig('../../Paper_Drafts/MW_corr.png', dpi = 300, bbox_inches = 'tight')
    # plt.show()

    av = .1294  #2006sr
    # av = 2.9711 #2005a
    name = '2006sr'
    # name = '2005a'
    host_wave = old_wave * u.Angstrom  # wavelengths
    host_flux = new_flux * u.Unit('W m-2 angstrom-1 sr-1')
    spec1d = Spectrum1D.from_array(host_wave, host_flux)
    new_flux_host, new_ivar_host = test_dered.host_correction(
        av, 2.5, name, spec1d.wavelength, spec1d.flux, [0])

    new_flux = np.asarray(new_flux)
    new_flux_host = np.asarray(new_flux_host.value)
    s = scale_composites_in_range(new_flux_host, new_flux)
    new_flux_host = s * new_flux_host

    norm = 1. / np.amax(new_flux_host)
    new_flux_host = new_flux_host * norm
    new_flux = new_flux * norm
    old_flux = old_flux * norm

    plt.rc('font', family='serif')
    fig, ax = plt.subplots(1, 1)
    fig.set_size_inches(10, 8, forward=True)
    plt.minorticks_on()
    plt.xticks(fontsize=20)
    ax.xaxis.set_ticks(
        np.arange(np.round(old_wave[0], -3), np.round(old_wave[-1], -3), 1000))
    plt.yticks(fontsize=20)
    plt.tick_params(which='major',
                    bottom='on',
                    top='on',
                    left='on',
                    right='on',
                    length=10)
    plt.tick_params(which='minor',
                    bottom='on',
                    top='on',
                    left='on',
                    right='on',
                    length=5)
    plt.plot(old_wave, old_flux, linewidth=2, color='#d95f02')
    plt.plot(old_wave, new_flux, linewidth=2, color='#1b9e77')
    plt.plot(host_wave.value, new_flux_host, linewidth=2, color='#7570b3')
    plt.ylabel('Relative Flux', fontsize=30)
    plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30)
    plt.savefig('../../Paper_Drafts/red_corr.pdf',
                dpi=300,
                bbox_inches='tight')
    # plt.ylim([-.2,1.01])
    # plt.savefig('../../Paper_Drafts/red_corr_large.pdf', dpi = 300, bbox_inches = 'tight')
    plt.show()

    # new_wave = old_wave/(1.+z)  # Deredshifting
    new_wave = old_wave
    new_error = old_error  # Placeholder if it needs to be changed
    norm = 1. / np.amax(new_flux)
    new_flux = new_flux * norm
    new_ivar = df.genivar(new_wave, new_flux,
                          new_error)  # generate new inverse variance
    #var = new_flux*0+1
    newdata = prep.Interpo(new_wave, new_flux,
                           new_ivar)  # Do the interpolation
    plt.rc('font', family='serif')
    fig, ax = plt.subplots(1, 1)
    fig.set_size_inches(10, 8, forward=True)
    plt.minorticks_on()
    plt.xticks(fontsize=20)
    ax.xaxis.set_ticks(
        np.arange(np.round(old_wave[0], -3), np.round(old_wave[-1], -3), 1000))
    plt.yticks(fontsize=20)
    plt.tick_params(which='major',
                    bottom='on',
                    top='on',
                    left='on',
                    right='on',
                    length=10)
    plt.tick_params(which='minor',
                    bottom='on',
                    top='on',
                    left='on',
                    right='on',
                    length=5)
    plt.plot(old_wave, new_flux, linewidth=2, color='r')
    plt.plot(newdata[0], newdata[1], linewidth=2, color='#3F5D7D')
    plt.ylabel('Relative Flux', fontsize=30)
    plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30)
    plt.savefig('../../Paper_Drafts/interp.pdf', dpi=300, bbox_inches='tight')
    # plt.ylim([-.3,1.])
    # plt.savefig('../../Paper_Drafts/interp_large.pdf', dpi = 300, bbox_inches = 'tight')
    plt.show()

    #    print 'new spectra',newdata
    return newdata, snr
示例#42
0
 def __call__(self, spectrum):
     doppler_factor = 1. + self.vrad / const.c
     return Spectrum1D.from_array(spectrum.wavelength * doppler_factor,
                                  spectrum.flux,
                                  dispersion_unit=spectrum.wavelength.unit)
print slopes, np.median(slopes), np.std(slopes)

residual_flux = mt.calc_residuals(f, starspectrum35.flux.value)

plt.figure(figsize=(12, 10))

#plt.plot(masked_data_sl_w,masked_data_sl_f)

slope_masked_flux, slope_masked_wavelength, slope_masked_uncert = mt.slope_masked_data(
    starspectrum35.wavelength.value, starspectrum35.flux.value,
    starspectrum35.uncertainty.value, slopes, cutoff)
print type(slope_masked_flux)

masked_data_slope = Spectrum1D.from_array(dispersion=slope_masked_wavelength,
                                          flux=slope_masked_flux,
                                          dispersion_unit=u.angstrom,
                                          uncertainty=slope_masked_uncert)

interp_slope = Interpolate(masked_data_slope)
convolve_slope = InstrumentConvolveGrating.from_grid(g, R=24000)
rot_slope = RotationalBroadening.from_grid(g, vrot=np.array([10.0]))
norm_slope = Normalize(masked_data_slope, 2)
like_slope = Chi2Likelihood(masked_data_slope)

model = g | rot_slope | DopplerShift(
    vrad=0.0) | convolve_slope | interp_slope | norm_slope
masked_model_slope = model | like_slope
tw, tf = model()

masked_model_slope
        starspectrum_uncert = np.concatenate(
            (starspectrum_uncert, single_order_spec.uncertainty.value[::-1]))

        starspectrum_flux = np.concatenate(
            (starspectrum_flux, single_order_spec.flux.value[::-1]))

        starspectrum_wavelength = np.concatenate(
            (starspectrum_wavelength,
             single_order_spec.wavelength.value[::-1]))

#print allorders_path
#starspectrum = read_fits_file.read_nirspec_dat(allorders_path,desired_wavelength_units='Angstrom',
#                                                 wave_range=waveranges)
starspectrum = Spectrum1D.from_array(dispersion=starspectrum_wavelength,
                                     flux=starspectrum_flux,
                                     dispersion_unit=u.angstrom,
                                     uncertainty=starspectrum_uncert)

g = load_grid(
    '/u/rbentley/metallicity/grids/phoenix_t2500_6000_w20000_24000_R40000.h5')

w, f = g()
print len(starspectrum.flux.value)
interp1 = Interpolate(starspectrum)
convolve1 = InstrumentConvolveGrating.from_grid(g, R=24000)
rot1 = RotationalBroadening.from_grid(g, vrot=np.array([10.0]))
norm1 = Normalize(starspectrum, 2)

# concatenate the spectral grid (which will have the stellar parameters) with other
# model components that you want to fit
model = g | rot1 | DopplerShift(vrad=0.0) | convolve1 | interp1 | norm1
示例#45
0
文件: prep.py 项目: stevet40/kaepora
def compprep(spectrum,
             sn_name,
             z,
             source,
             use_old_error=True,
             testing=False,
             filename=None,
             mjd=None,
             mjd_max=None):
    """ Performs clipping, deredshifting, variance spectrum generation, MW extinction correction,
        and interpolation. If testing is True, several plots will be made to assess the quality 
        of this processing.
    """
    old_wave = spectrum[:, 0]  # wavelengths
    old_flux = spectrum[:, 1]  # fluxes
    try:
        old_error = spectrum[:, 2]  # check if supernovae has error array
    except IndexError:
        old_error = None  # if not, set default
    if sn_name == '2011fe' and source == 'other':
        old_error = np.sqrt(old_error)
    if old_error is not None:
        old_var = old_error**2.
    else:
        old_var = None

    if old_var is not None:
        num_non_zeros = np.count_nonzero(old_var)
        if len(old_var) - num_non_zeros > 100:
            old_var = None
        elif old_var[-1] == 0.:
            old_var[-1] = old_var[-2]
        elif True in np.isnan(old_var):
            nan_inds = np.transpose(np.argwhere(np.isnan(old_var)))[0]
            for ind in nan_inds:
                if ind != 0:
                    old_var[ind] = old_var[ind - 1]
                else:
                    old_var[ind] = old_var[ind + 1]

    # if testing:
    #     plt.plot(old_wave, old_flux)
    #     plt.plot(old_wave/(1.+z), old_flux)
    #     plt.plot(old_wave*(1.+z), old_flux)
    #     plt.xlim(5800,6000)
    #     # plt.show()
    #     if old_var is not None:
    #         plt.plot(old_wave, old_var)
    #         plt.show()
    # old_var = None
    vexp, SNR = df.find_vexp(old_wave, old_flux, var_y=old_var)
    if testing:
        print vexp, SNR

    if source != 'csp':  #already deredshifted
        old_wave = old_wave / (1. + z)  #deredshift for clipping

    old_wave, old_flux, old_var = df.clip(
        old_wave, old_flux, old_var, vexp, testing=testing,
        filename=filename)  #clip emission/absorption lines
    old_wave = old_wave * (1. + z)  #reredshift for MW extinction correction
    temp_ivar, SNR = df.genivar(old_wave,
                                old_flux,
                                old_var,
                                vexp=vexp,
                                testing=testing,
                                source=source)  # generate inverse variance

    #code to save foundation spec for david
    # print filename
    # plt.plot(old_wave, old_flux)
    # plt.show()
    # plt.plot(old_wave, temp_ivar)
    # plt.show()
    # file_path = '../../Foundation/mod_TNS_spec/' + filename.split('.')[0] + '_modified.flm'
    # print file_path
    # with open(file_path, 'w') as file:
    #     file.write('# Orginal file name = ' + filename + '\n')
    #     file.write('# z = ' + str(z) + '\n')
    #     # file.write('# MJD = ' + str(mjd) + '\n')
    #     # file.write('# MJD_max = ' + str(mjd_max) + '\n')
    #     file.write('\n')
    #     err = np.sqrt(1./np.asarray(temp_ivar))
    #     data = np.c_[old_wave,old_flux,err]
    #     table = tabulate(data, headers=['Wavelength', 'Flux', 'Error'],
    #                                         tablefmt = 'ascii')
    #     file.write(table)

    if testing:
        print SNR

    if old_var is not None:
        old_ivar = 1. / old_var
    else:
        old_ivar = temp_ivar
    # snr = getsnr(old_flux, old_ivar)

    if source == 'cfa':  # choosing source dataset
        #        z = ReadParam()
        sne = ReadExtin('extinction.dat')
    if source == 'bsnip':
        sne = ReadExtin('extinctionbsnip.dat')
    if source == 'csp':
        sne = ReadExtin('extinctioncsp.dat')
    if source == 'uv':
        sne = ReadExtin('extinctionuv.dat')
    if source == 'other':
        sne = ReadExtin('extinctionother.dat')
    if source == 'swift_uv':
        sne = ReadExtin('extinctionswiftuv.dat')
    if source == 'foley_hst':
        sne = ReadExtin('extinctionhst.dat')
    if source == 'foundation':
        sne = ReadExtin('extinctionfoundation.dat')

#     host_reddened = ReadExtin('../data/info_files/ryan_av.txt')
    newdata = []
    old_wave = old_wave * u.Angstrom  # wavelengths
    old_flux = old_flux * u.Unit('W m-2 angstrom-1 sr-1')
    spec1d = Spectrum1D.from_array(old_wave, old_flux)
    spec1d_ivar = Spectrum1D.from_array(old_wave, old_ivar)
    dered_flux, dered_ivar = test_dered.dered(
        sne,
        sn_name,
        spec1d.wavelength,
        spec1d.flux,
        spec1d_ivar.flux,
        source=source
    )  # Dereddening (see if sne in extinction files match the SN name)
    #     new_flux = host_correction(sne, sn_name, old_wave, new_flux)

    # new_flux = old_flux

    if testing:
        new_flux_plot = copy.deepcopy(dered_flux)
        new_ivar_plot = copy.deepcopy(dered_ivar)
        old_wave_plot = copy.deepcopy(old_wave)

    new_flux = dered_flux.value
    new_ivar = dered_ivar.value
    old_wave = old_wave.value

    if testing:
        av_specific = 0.2384  #2005lz
        av_specific = 0.4089  #2007af
        r_v = 2.5
        new_flux_host, new_ivar_host = test_dered.host_correction(
            av_specific, r_v, sn_name, old_wave_plot, new_flux_plot,
            new_ivar_plot)
        new_flux_host = new_flux_host.value
        old_flux = old_flux.value

        s = scale_composites_in_range(new_flux, old_flux)
        new_flux_scaled = s * new_flux
        s = scale_composites_in_range(new_flux_host, old_flux)
        new_flux_host_scaled = s * new_flux_host

        valid_data = np.where(old_wave > 4000)
        norm = 10. / np.nanmax(new_flux_host_scaled[valid_data])
        old_flux_norm = old_flux * norm
        new_flux_norm = new_flux_scaled * norm
        new_flux_host_norm = new_flux_host_scaled * norm

        plt.rc('font', family='serif')
        fig, ax = plt.subplots(1, 1)
        fig.set_size_inches(10, 8, forward=True)
        plt.minorticks_on()
        plt.xticks(fontsize=20)
        # ax.xaxis.set_ticks(np.arange(np.round(wave[0],-3),np.round(wave[-1],-3),1000))
        plt.yticks(fontsize=20)
        plt.tick_params(which='major',
                        bottom='on',
                        top='on',
                        left='on',
                        right='on',
                        length=10)
        plt.tick_params(which='minor',
                        bottom='on',
                        top='on',
                        left='on',
                        right='on',
                        length=5)
        plt.plot(old_wave,
                 old_flux_norm,
                 linewidth=2,
                 color='#000080',
                 label='Before Dereddening')
        plt.plot(old_wave,
                 new_flux_norm,
                 linewidth=2,
                 color='gold',
                 label='Milky Way Corrected')
        # plt.plot(old_wave, new_flux_host_norm, linewidth = 2, color = '#d95f02', label='Host Corrected')
        plt.ylabel('Relative Flux', fontsize=30)
        plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30)
        plt.xlim([old_wave[0] - 200, old_wave[-1] + 200])
        plt.legend(loc=1, fontsize=20)
        # plt.savefig('../../../Paper_Drafts/reprocessing_updated/red_corr.pdf', dpi = 300, bbox_inches = 'tight')
        plt.show()
        # plt.plot(old_wave, old_ivar)
        # plt.plot(old_wave, new_ivar)
        # plt.show()

    new_wave = old_wave / (1. + z)  # Deredshifting

    if not use_old_error:
        new_var = None
    else:
        new_var = old_var  # Placeholder if it needs to be changed
    #var = new_flux*0+1
    # newdata = Interpo(new_wave, new_flux, new_ivar)  # Do the interpolation
    newdata, scale, var_final = Interpo_flux_conserving(new_wave,
                                                        new_flux,
                                                        new_ivar,
                                                        testing=testing)

    if testing:
        # newdata_test = Interpo(new_wave, new_flux_host_norm, new_ivar)
        newdata_test, scale, var_final = Interpo_flux_conserving(
            new_wave, new_flux_host_norm, new_ivar)
        interp_wave = newdata_test[0, :]
        interp_flux = newdata_test[1, :]
        plt.rc('font', family='serif')
        fig, ax = plt.subplots(1, 1)
        fig.set_size_inches(10, 8, forward=True)
        plt.minorticks_on()
        plt.xticks(fontsize=20)
        # ax.xaxis.set_ticks(np.arange(np.round(wave[0],-3),np.round(wave[-1],-3),1000))
        plt.yticks(fontsize=20)
        plt.tick_params(which='major',
                        bottom='on',
                        top='on',
                        left='on',
                        right='on',
                        length=10)
        plt.tick_params(which='minor',
                        bottom='on',
                        top='on',
                        left='on',
                        right='on',
                        length=5)
        plt.plot(new_wave,
                 2. * new_flux_host_norm,
                 linewidth=2,
                 color='#d95f02',
                 label='Before Interpolation')
        plt.plot(interp_wave,
                 interp_flux,
                 linewidth=2,
                 color='darkgreen',
                 label='After Interpolation')
        plt.ylabel('Relative Flux', fontsize=30)
        plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30)
        plt.xlim([new_wave[0] - 200, new_wave[-1] + 200])
        plt.legend(loc=1, fontsize=20)
        # plt.savefig('../../../Paper_Drafts/reprocessing_updated/interp_deredshift.pdf', dpi = 300, bbox_inches = 'tight')
        plt.show()

    return newdata, SNR
示例#46
0
    if abs(apogee_res[i])>0.05:
        apogee_mask += [apogee_res[i]]
        apogee_mask_w += [resampled_apogeew[i]]
        apogee_ind += [i]




apogee_ind_s = sorted(apogee_ind, reverse=True)
print 'trimming'
starspectrum_fmasked = np.delete(starspectrum35.flux,apogee_ind_s)
starspectrum_wmasked = np.delete(starspectrum35.wavelength,apogee_ind_s)
starspectrum_umasked = np.delete(starspectrum35.uncertainty,apogee_ind_s)
print len(starspectrum_fmasked), len(apogee_ind)

spectrum_masked = Spectrum1D.from_array(starspectrum_wmasked, starspectrum_fmasked, dispersion_unit=starspectrum35.wavelength.unit, uncertainty=starspectrum_umasked) 
                                        #flux_unit=starspectrum35.flux.unit, wavelength_unit=starspectrum35.wavelength.unit)

print starspectrum_fmasked
interp1 = Interpolate(spectrum_masked)
print 'interpolated 2'
convolve1 = InstrumentConvolveGrating.from_grid(g,R=24000)
print 'convolved 2'
rot1 = RotationalBroadening.from_grid(g,vrot=np.array([10.0]))
print 'rot broadend 2'
norm1 = Normalize(spectrum_masked,2)
print 'normalized 2'
# concatenate the spectral grid (which will have the stellar parameters) with other
# model components that you want to fit
model = g | rot1 |DopplerShift(vrad=radv)| convolve1 | interp1 | norm1
print 'model concaten 2'
示例#47
0
def combine(spectra,
            rv,
            snr,
            wave_range=None,
            desired_wavelength_units='angstrom',
            flux_unit='W / (m^2 micron)',
            fill_value=1.0):
    '''Read in some spectra and shift them to rest wavelengths, then
    combine them in a weighted average.

    Inputs:
    ------
    spectra : list of spectra to input
    rv: array of RV values to shift the spectra
    snr: signal-to-noise ratio of the spectra

    Keywords:
    --------
    wave_range : the range of wavelengths to have in the final spectrum

    Output:
    -------
    Combined Spectrum1D object
    
    '''

    flux_arr = None
    #print(len(spectra))
    for i in np.arange(len(spectra)):

        if os.path.exists(spectra[i]):
            #print(spectra[i])
            spectrum = read_fits_file.read_fits_file(
                spectra[i], desired_wavelength_units=desired_wavelength_units)
            wave = spectrum.wavelength.value
            flux = spectrum.flux.value
            #print(flux)
            if flux_arr is None:
                if wave_range is not None:
                    good = np.where((wave >= wave_range[0])
                                    & (wave <= wave_range[1]))[0]
                    wave_arr = wave[good]
                else:
                    wave_arr = wave

                flux_arr = np.ones((len(wave_arr), len(spectra)))

            # shift and interpolate wavelengths
            shift_wave = wave / (rv[i] / 3e5 + 1.0)
            f = interpolate.interp1d(shift_wave,
                                     flux,
                                     fill_value=fill_value,
                                     bounds_error=False)

            flux_arr[:, i] = f(wave_arr)
            #print(np.shape(flux_arr))
        else:
            print('file not found:' + spectra[i])

        average_flux = np.average(flux_arr, axis=1, weights=snr**2)
        ret_spec = Spectrum1D.from_array(
            wave_arr,
            average_flux,
            dispersion_unit=desired_wavelength_units,
            unit=flux_unit)
    return ret_spec
示例#48
0
文件: plugins.py 项目: mhvk/specgrid
 def __call__(self, spectrum):
     doppler_factor = 1. + self.vrad / const.c
     return Spectrum1D.from_array(spectrum.wavelength * doppler_factor,
                                  spectrum.flux,
                                  dispersion_unit=spectrum.wavelength.unit)
ax.plot(starspectrum35.wavelength.value, sl_mh, 'b.')
ax.plot(mask_sl_w, mask_sl_f, 'r.')

masked_data_sl_f = np.delete(starspectrum35.flux.value, sl_mask_indices)
masked_data_sl_w = np.delete(starspectrum35.wavelength.value, sl_mask_indices)
masked_data_sl_u = np.delete(starspectrum35.uncertainty.value, sl_mask_indices)

plt.figure(figsize=(12, 10))

#plt.plot(masked_data_sl_w,masked_data_sl_f)

#masked_data_sl = SKSpectrum1D.from_array(wavelength=masked_data_sl_w*u.angstrom, flux=masked_data_sl_f*u.Unit('erg/s/cm^2/angstrom'), uncertainty=masked_data_sl_f*u.Unit('erg/s/cm^2/angstrom'))

masked_data_sl = Spectrum1D.from_array(dispersion=masked_data_sl_w,
                                       flux=masked_data_sl_f,
                                       dispersion_unit=u.angstrom,
                                       uncertainty=masked_data_sl_u)  #

interp_sl = Interpolate(masked_data_sl)
convolve_sl = InstrumentConvolveGrating.from_grid(g, R=24000)
rot_sl = RotationalBroadening.from_grid(g, vrot=np.array([10.0]))
norm_sl = Normalize(masked_data_sl, 2)
like_sl = Chi2Likelihood(masked_data_sl)

model = g | rot_sl | DopplerShift(vrad=0.0) | convolve_sl | interp_sl | norm_sl
masked_model_sl = model | like_sl
tw, tf = model()

masked_model_sl

#Fits S_lambda masked model
示例#50
0
from specutils import Spectrum1D
from astropy import units
import numpy as np
dispersion = np.arange(4000, 5000, 0.12)
flux = np.random.randn(len(dispersion))
mySpectrum = Spectrum1D.from_array(dispersion,
                                   flux,
                                   dispersion_unit=units.m)

hBeta = mySpectrum.slice_dispersion(4851.0, 4871.0)
hBeta