Пример #1
0
def get_table(teff=None,
              logg=None,
              ebv=None,
              vrad=None,
              star=None,
              wave_units='AA',
              flux_units='erg/cm2/s/AA/sr',
              **kwargs):
    """
    Retrieve the specific intensity of a model atmosphere.
    
    ebv is reddening
    vrad is radial velocity: positive is redshift, negative is blueshift (km/s!)
    
    extra kwargs are for reddening
    
    You get limb angles, wavelength and a table. The shape of the table is
    (N_wave,N_mu).
    
    WARNING: wave and flux units cannot be specificed for the moment.
        
    >>> mu,wave,table = get_table(10000,4.0)
    
    >>> p = pl.figure()
    >>> ax1 = pl.subplot(221)
    >>> p = pl.title('E(B-V)=0, vrad=0')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10000,4.0,ebv=0.5)
    
    >>> p = pl.subplot(222,sharex=ax1,sharey=ax1)
    >>> p = pl.title('E(B-V)=0.5, vrad=0')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10000,4.0,vrad=-1000.)
    
    >>> p = pl.subplot(223,sharex=ax1,sharey=ax1)
    >>> p = pl.title('E(B-V)=0., vrad=-10000.')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10000,4.0,vrad=-1000.,ebv=0.5)
    
    >>> p = pl.subplot(224,sharex=ax1,sharey=ax1)
    >>> p = pl.title('E(B-V)=0.5, vrad=-10000.')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10050,4.12)
    
    >>> p = pl.figure()
    >>> pl.gca().set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    
    
    @param teff: effective temperature (K)
    @type teff: float
    @param logg: log surface gravity (cgs, dex)
    @type logg: float
    @param ebv: reddening (mag)
    @type ebv: float
    @param vrad: radial velocity (for doppler shifting) (km/s)
    @type vrad: float
    @return: mu angles, wavelengths, table (Nwave x Nmu)
    @rtype: array, array, array
    """
    #-- get the FITS-file containing the tables
    gridfile = get_file(**kwargs)

    #-- read the file:
    ff = pyfits.open(gridfile)

    teff = float(teff)
    logg = float(logg)

    #-- if we have a grid model, no need for interpolation
    try:
        #-- extenstion name as in fits files prepared by Steven
        mod_name = "T%05d_logg%01.02f" % (teff, logg)
        mod = ff[mod_name]
        mu = np.array(mod.columns.names[1:], float)
        table = np.array(mod.data.tolist())[:, 1:]
        wave = mod.data.field('wavelength')
        logger.debug('Model LD taken directly from file (%s)' %
                     (os.path.basename(gridfile)))
    except KeyError:
        mu, wave, teffs, loggs, flux, flux_grid = get_grid_mesh(**kwargs)
        logger.debug('Model LD interpolated from grid %s (%s)' %
                     (os.path.basename(gridfile), kwargs))
        wave = wave + 0.
        table = flux_grid(np.log10(teff), logg) + 0.

    ff.close()

    #-- velocity shift if necessary
    if vrad is not None and vrad != 0:
        cc = constants.cc / 1000.  #speed of light in km/s
        for i in range(len(mu)):
            flux_shift = tools.doppler_shift(wave, vrad, flux=table[:, i])
            table[:, i] = flux_shift - 5. * vrad / cc * table[:, i]

    #-- redden if necessary
    if ebv is not None and ebv > 0:
        for i in range(len(mu)):
            table[:, i] = reddening.redden(table[:, i],
                                           wave=wave,
                                           ebv=ebv,
                                           rtype='flux',
                                           **kwargs)

    #-- that's it!
    return mu, wave, table
def get_table(teff=None, logg=None, vrad=0, vrot=0, **kwargs):
    """
    Retrieve synthetic spectrum.

    Wavelengths in angstrom, fluxes in eddington flux: erg/s/cm2/A.

    It is possible to rotationally broaden the spectrum by supplying at least
    'vrot' and optionally also other arguments for the rotation.rotin function.
    Supply vrot in km/s

    It is possibility to include a radial velocity shift in the spectrum. Supply
    'vrad' in km/s. (+vrad means redshift). Uses spectra.tools.doppler_shift
    """
    gridfile = get_file(**kwargs)
    template_wave = kwargs.pop('wave', None)

    ff = pf.open(gridfile)

    try:
        #-- extenstion name as in fits files prepared by Steven
        mod_name = "T%05d_logg%01.02f" % (teff, logg)
        mod = ff[mod_name]
        wave = mod.data.field('wavelength')
        flux = mod.data.field('flux')
        cont = mod.data.field('cont')
        logger.debug('Model spectrum taken directly from file (%s)' %
                     (os.path.basename(gridfile)))
        if template_wave is not None:
            flux = np.interp(template_wave, wave, flux)
            cont = np.interp(template_wave, wave, cont)
            wave = template_wave
        #-- if the teff/logg is not present, use the interpolation thing
    except KeyError:
        #-- it is possible we first have to set the interpolation function.
        #   This function is memoized, so if it will not be calculated
        #   twice.
        meshkwargs = copy.copy(kwargs)
        meshkwargs['wave'] = kwargs.get('wave', None)
        meshkwargs['teffrange'] = kwargs.get('teffrange', None)
        meshkwargs['loggrange'] = kwargs.get('loggrange', None)
        wave, teffs, loggs, flux, flux_grid, cont_grid = get_grid_mesh(
            wave=template_wave, **kwargs)
        logger.debug('Model spectrum interpolated from grid %s (%s)' %
                     (os.path.basename(gridfile), meshkwargs))
        wave = wave + 0.
        try:
            flux = flux_grid(np.log10(teff), logg) + 0.
            cont = cont_grid(np.log10(teff), logg) + 0.
        except ValueError:
            teffs, loggs = get_grid_dimensions(**kwargs)
            index = np.argmin(np.abs((teffs - teff)**2 + (loggs - logg)**2))
            #logger.error('teff=%f-->%f, logg=%f-->%f'%(teff,teffs[index],logg,loggs[index]))
            flux = flux_grid(np.log10(teffs[index]), loggs[index]) + 0.
            cont = cont_grid(np.log10(teffs[index]), loggs[index]) + 0.

    #-- convert to arrays
    wave = np.array(wave, float)
    flux = np.array(flux, float)

    if vrot > 0:
        #-- calculate rotational broadening but reinterpolate on original
        #   wavelength grid. First we need to check which arguments we can pass
        #   through
        argspec = inspect.getargspec(tools.rotational_broadening)
        mykwargs = dict(
            list(zip(argspec.args[-len(argspec.defaults):], argspec.defaults)))
        for key in kwargs:
            if key in mykwargs:
                mykwargs[key] = kwargs[key]
        wave_, flux_ = tools.rotational_broadening(wave, flux, vrot,
                                                   **mykwargs)
        flux = np.interp(wave, wave_, flux_)
    if vrad != 0:
        wave_ = tools.doppler_shift(wave, vrad)
        flux = np.interp(wave, wave_, flux)

    ff.close()
    return wave, flux, cont
Пример #3
0
def get_table(teff=None,logg=None,vrad=0,vrot=0,**kwargs):
    """
    Retrieve synthetic spectrum.
        
    Wavelengths in angstrom, fluxes in eddington flux: erg/s/cm2/A.
    
    It is possible to rotationally broaden the spectrum by supplying at least
    'vrot' and optionally also other arguments for the rotation.rotin function.
    Supply vrot in km/s
    
    It is possibility to include a radial velocity shift in the spectrum. Supply
    'vrad' in km/s. (+vrad means redshift). Uses spectra.tools.doppler_shift
    """
    gridfile = get_file(**kwargs)
    template_wave = kwargs.pop('wave',None)
    
    ff = pyfits.open(gridfile)
    
    try:
        #-- extenstion name as in fits files prepared by Steven
        mod_name = "T%05d_logg%01.02f" %(teff,logg)
        mod = ff[mod_name]
        wave = mod.data.field('wavelength')
        flux = mod.data.field('flux')
        cont = mod.data.field('cont')
        logger.debug('Model spectrum taken directly from file (%s)'%(os.path.basename(gridfile)))
        if template_wave is not None:
            flux = np.interp(template_wave,wave,flux)
            cont = np.interp(template_wave,wave,cont)
            wave = template_wave
        #-- if the teff/logg is not present, use the interpolation thing
    except KeyError:
        #-- it is possible we first have to set the interpolation function.
        #   This function is memoized, so if it will not be calculated
        #   twice.
        meshkwargs = copy.copy(kwargs)
        meshkwargs['wave'] = kwargs.get('wave',None)
        meshkwargs['teffrange'] = kwargs.get('teffrange',None)
        meshkwargs['loggrange'] = kwargs.get('loggrange',None)
        wave,teffs,loggs,flux,flux_grid,cont_grid = get_grid_mesh(wave=template_wave,**kwargs)
        logger.debug('Model spectrum interpolated from grid %s (%s)'%(os.path.basename(gridfile),meshkwargs))
        wave = wave + 0.
        try:
            flux = flux_grid(np.log10(teff),logg) + 0.
            cont = cont_grid(np.log10(teff),logg) + 0.
        except ValueError:
            teffs,loggs = get_grid_dimensions(**kwargs)
            index = np.argmin(np.abs(  (teffs-teff)**2 + (loggs-logg)**2 ))
            #logger.error('teff=%f-->%f, logg=%f-->%f'%(teff,teffs[index],logg,loggs[index]))
            flux = flux_grid(np.log10(teffs[index]),loggs[index]) + 0.
            cont = cont_grid(np.log10(teffs[index]),loggs[index]) + 0.
    
    #-- convert to arrays
    wave = np.array(wave,float)
    flux = np.array(flux,float)
    
    if vrot>0:
        #-- calculate rotational broadening but reinterpolate on original
        #   wavelength grid. First we need to check which arguments we can pass
        #   through
        argspec = inspect.getargspec(tools.rotational_broadening)
        mykwargs = dict(zip(argspec.args[-len(argspec.defaults):],argspec.defaults))
        for key in kwargs:
            if key in mykwargs:
                mykwargs[key] = kwargs[key]
        wave_,flux_ = tools.rotational_broadening(wave,flux,vrot,**mykwargs)
        flux = np.interp(wave,wave_,flux_)
    if vrad!=0:
        wave_ = tools.doppler_shift(wave,vrad)
        flux = np.interp(wave,wave_,flux)
    
    ff.close()
    return wave,flux,cont
Пример #4
0
def get_table(
    teff=None, logg=None, ebv=None, vrad=None, star=None, wave_units="AA", flux_units="erg/cm2/s/AA/sr", **kwargs
):
    """
    Retrieve the specific intensity of a model atmosphere.
    
    ebv is reddening
    vrad is radial velocity: positive is redshift, negative is blueshift (km/s!)
    
    extra kwargs are for reddening
    
    You get limb angles, wavelength and a table. The shape of the table is
    (N_wave,N_mu).
    
    WARNING: wave and flux units cannot be specificed for the moment.
        
    >>> mu,wave,table = get_table(10000,4.0)
    
    >>> p = pl.figure()
    >>> ax1 = pl.subplot(221)
    >>> p = pl.title('E(B-V)=0, vrad=0')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10000,4.0,ebv=0.5)
    
    >>> p = pl.subplot(222,sharex=ax1,sharey=ax1)
    >>> p = pl.title('E(B-V)=0.5, vrad=0')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10000,4.0,vrad=-1000.)
    
    >>> p = pl.subplot(223,sharex=ax1,sharey=ax1)
    >>> p = pl.title('E(B-V)=0., vrad=-10000.')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10000,4.0,vrad=-1000.,ebv=0.5)
    
    >>> p = pl.subplot(224,sharex=ax1,sharey=ax1)
    >>> p = pl.title('E(B-V)=0.5, vrad=-10000.')
    >>> ax = pl.gca()
    >>> ax.set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    >>> p = pl.xlim(700,15000)
    >>> p = pl.ylim(1e3,1e8)
    
    >>> mu,wave,table = get_table(10050,4.12)
    
    >>> p = pl.figure()
    >>> pl.gca().set_color_cycle([pl.cm.spectral(i) for i in np.linspace(0,1,len(mu))])
    >>> p = pl.loglog(wave,table)
    
    
    @param teff: effective temperature (K)
    @type teff: float
    @param logg: log surface gravity (cgs, dex)
    @type logg: float
    @param ebv: reddening (mag)
    @type ebv: float
    @param vrad: radial velocity (for doppler shifting) (km/s)
    @type vrad: float
    @return: mu angles, wavelengths, table (Nwave x Nmu)
    @rtype: array, array, array
    """
    # -- get the FITS-file containing the tables
    gridfile = get_file(**kwargs)

    # -- read the file:
    ff = pyfits.open(gridfile)

    teff = float(teff)
    logg = float(logg)

    # -- if we have a grid model, no need for interpolation
    try:
        # -- extenstion name as in fits files prepared by Steven
        mod_name = "T%05d_logg%01.02f" % (teff, logg)
        mod = ff[mod_name]
        mu = np.array(mod.columns.names[1:], float)
        table = np.array(mod.data.tolist())[:, 1:]
        wave = mod.data.field("wavelength")
        logger.debug("Model LD taken directly from file (%s)" % (os.path.basename(gridfile)))
    except KeyError:
        mu, wave, teffs, loggs, flux, flux_grid = get_grid_mesh(**kwargs)
        logger.debug("Model LD interpolated from grid %s (%s)" % (os.path.basename(gridfile), kwargs))
        wave = wave + 0.0
        table = flux_grid(np.log10(teff), logg) + 0.0

    ff.close()

    # -- velocity shift if necessary
    if vrad is not None and vrad != 0:
        cc = constants.cc / 1000.0  # speed of light in km/s
        for i in range(len(mu)):
            flux_shift = tools.doppler_shift(wave, vrad, flux=table[:, i])
            table[:, i] = flux_shift - 5.0 * vrad / cc * table[:, i]

    # -- redden if necessary
    if ebv is not None and ebv > 0:
        for i in range(len(mu)):
            table[:, i] = reddening.redden(table[:, i], wave=wave, ebv=ebv, rtype="flux", **kwargs)

    # -- that's it!
    return mu, wave, table