Beispiel #1
0
def rebin_spec(spec, wavnew, waveunits='um'):
    from pysynphot import spectrum, observation
    # Gives same error answer: Err = np.array([np.sqrt(sum(spec[2].value[idx_include(wavnew,[((wavnew[0] if n==0 else
    #  wavnew[n-1]+wavnew[n])/2,wavnew[-1] if n==len(wavnew) else (wavnew[n]+wavnew[n+1])/2)])]**2)) for n in range(
    # len(wavnew)-1)])*spec[2].unit if spec[2] is not '' else ''
    if len(spec) == 2:
        spec += ['']
    try:
        Flx, Err, filt = spectrum.ArraySourceSpectrum(
            wave=spec[0].value,
            flux=spec[1].value), spectrum.ArraySourceSpectrum(
                wave=spec[0].value, flux=spec[2].value
            ) if spec[2] else '', spectrum.ArraySpectralElement(
                spec[0].value, np.ones(len(spec[0])), waveunits=waveunits)
    except:
        spec, wavnew = [i * q.Unit('') for i in spec], wavnew * q.Unit('')
        Flx, Err, filt = spectrum.ArraySourceSpectrum(
            wave=spec[0].value,
            flux=spec[1].value), spectrum.ArraySourceSpectrum(
                wave=spec[0].value, flux=spec[2].value
            ) if spec[2] else '', spectrum.ArraySpectralElement(
                spec[0].value, np.ones(len(spec[0])), waveunits=waveunits)
    return [
        wavnew,
        observation.Observation(Flx, filt, binset=wavnew.value,
                                force='taper').binflux * spec[1].unit,
        observation.Observation(Err, filt, binset=wavnew.value,
                                force='taper').binflux *
        spec[2].unit if spec[2] else np.ones(len(wavnew)) * spec[1].unit
    ]
Beispiel #2
0
def rebin_spec(wave, specin, wavnew):
    spec = spectrum.ArraySourceSpectrum(wave=wave, flux=specin)
    f = np.ones(len(wave))
    filt = spectrum.ArraySpectralElement(wave, f, waveunits='angstrom')
    obs = observation.Observation(spec, filt, binset=wavnew, force='taper')

    return obs.binflux
Beispiel #3
0
def spec_res_downgrade(l_in, spec_in, l_out):
    templ_spec = spectrum.ArraySourceSpectrum(wave=l_in, flux=spec_in)
    white_filter = spectrum.ArraySpectralElement(l_out,\
    np.ones(len(l_out)), waveunits='angstrom')
    convolved_spec = observation.Observation(templ_spec,\
    white_filter, binset=l_out, force='taper').binflux

    return convolved_spec
Beispiel #4
0
def spectra_rebin(wave,flux,rebin_wave):

    spect=spectrum.ArraySourceSpectrum(wave=wave.values,flux=flux.values)
    f = np.ones(len(wave))
    filt=spectrum.ArraySpectralElement(wave.values,f,waveunits='microns')
    obs=observation.Observation(spect,filt,binset=rebin_wave,force='taper')

    return obs.binflux
def rebin(new_wav, old_wav, flux):

    f_ = np.ones(len(old_wav))
    spec_ = pysynspec.ArraySourceSpectrum(wave=old_wav, flux=flux)
    filt = pysynspec.ArraySpectralElement(old_wav, f_, waveunits='angstrom')
    obs = observation.Observation(spec_, filt, binset=new_wav, force='taper')
    newflux = obs.binflux
    return newflux
Beispiel #6
0
def rebin_spec(wave, specin, wavnew):
    '''(inwave (ndarray),influx (ndarray),outwave(ndarray)-> outflux
    Correctly rebins spectra
    '''
    spec = spectrum.ArraySourceSpectrum(wave=wave, flux=specin)
    f = nu.ones(len(wave))
    filt = spectrum.ArraySpectralElement(wave, f, waveunits='angstrom')
    obs = observation.Observation(spec, filt, binset=wavnew, force='taper')

    return obs.binflux
Beispiel #7
0
def rebin_spec(wavelength, flux, waveout, keepneg=False):
    spec = spectrum.ArraySourceSpectrum(wave=wavelength,
                                        flux=flux,
                                        keepneg=keepneg)
    f = np.ones(len(flux))
    filt = spectrum.ArraySpectralElement(wavelength.to(wavelength.unit).value,
                                         f,
                                         waveunits=str(wavelength.unit))
    obs = observation.Observation(spec, filt, binset=waveout, force='taper')

    return obs.binflux
Beispiel #8
0
def rebin_spec(wave, specin, wavnew):
    '''	
	Given wavelength, a spectrum, and new wavelength array, this
    function resamples the spectrum to match new array.
	'''
    import numpy as np
    from pysynphot import observation
    from pysynphot import spectrum

    spec = spectrum.ArraySourceSpectrum(wave=wave, flux=specin, keepneg=True)
    f = np.ones(len(wave))
    filt = spectrum.ArraySpectralElement(wave, f, waveunits='angstrom')
    obs = observation.Observation(spec, filt, binset=wavnew, force='taper')
    return obs.binflux
Beispiel #9
0
def rebin_spec(wave, specin, wavnew):
    """
    Rebin spectra to bins used in wavnew.
    Ref: http://www.astrobetter.com/blog/2013/08/12/python-tip-re-sampling-spectra-with-pysynphot/
    """
    from pysynphot import observation
    from pysynphot import spectrum as pysynphot_spec
    import numpy as np

    spec = pysynphot_spec.ArraySourceSpectrum(wave=wave, flux=specin)
    f = np.ones(len(wave))
    filt = pysynphot_spec.ArraySpectralElement(wave, f, waveunits='angstrom')
    obs = observation.Observation(spec, filt, binset=wavnew, force='taper')

    return obs.binflux
Beispiel #10
0
def read_mk_sky_emission_ir(pwv=1.6, airmass=1.5):
    path_to_file = inspect.getsourcefile(Vega)
    directory = os.path.split(path_to_file)[0] + '/maunakea_files/'
    
    pwv_suffix = {1.0: '10', 1.6: '16', 3.0: '30', 5.0: '50'}
    am_suffix = {1.0: '10', 1.5: '15', 2.0: '20'}
    
    if pwv not in pwv_suffix.keys():
        print 'Invalid precipatable water vapor (PWV): ', pwv
        print 'Choose from:'
        print pwv_suffix.keys()
        return
    
    if airmass not in am_suffix.keys():
        print 'Invalid airmass (AM): ', airmass
        print 'Choose from:'
        print am_suffix.keys()
        return
    
    mk_sky_file = 'mk_skybg_zm_'
    mk_sky_file += pwv_suffix[pwv] + '_'
    mk_sky_file += am_suffix[airmass] + '_ph.dat'
    
    try:
        t = Table.read(directory + mk_sky_file + '.fits')
    except IOError:
        t = Table.read(directory + mk_sky_file, format='ascii')
        t.rename_column('col1', 'wave') # Units in nm
        t.rename_column('col2', 'flux') # Units in ph/sec/arcsec^2/nm/m^2

        t['wave'].unit = 'nm'
        t['flux'].unit = 'photons s^-1 arcsec^-2 nm^-1 m^-2'
        t.write(directory + mk_sky_file + '.fits')

    # Convert to photlam flux units (m->cm and nm->Ang).
    # Drop the arcsec density... but sill there
    t['flux'] *= (1.0 / 100.0)**2 * (1.0 / 10.0)
    t['flux'].unit = 'photons cm^-2 s^-1 Ang^-1 arcsec^-2'
    spec = spectrum.ArraySourceSpectrum(wave=t['wave'], flux=t['flux'], 
                                        waveunits='nm', fluxunits=units.Photlam, 
                                        name='Maunakea Sky')
    spec.convert(units.Angstrom)

    return spec
Beispiel #11
0
 def testrows(self):
     try:
         sp = spectrum.ArraySourceSpectrum(self.wv, self.fx)
     except exceptions.DuplicateWavelength as e:
         self.assertEqual(e.rows, 1)
Beispiel #12
0
 def testpass(self):
     self.wv = np.array([10, 20, 30, 40, 50, 100])
     sp = spectrum.ArraySourceSpectrum(self.wv, self.fx)
def rebin_spec(wave, specin, wavnew):                                            # wavnew is the new wave grid
	spec = spectrum.ArraySourceSpectrum(wave=wave, flux=specin)                  # configures wave/spectrum for pysynphot
	f = np.ones(len(wave))                                                       # preserve all flux at all wavelengths (no throughput curve)
	filt = spectrum.ArraySpectralElement(wave, f, waveunits='angstrom')          # apply throughput curve 'filter' (not necessary for us?)
	obs = observation.Observation(spec, filt, binset=wavnew, force='taper')
	return obs.binflux
def ssp_rebin(logL_ssp, spec_ssp, dlogL_new, Lll=3250.):
    '''
    rebin a GRID of model spectrum to have an identical velocity
    resolution to an input spectrum

    intended to be used on a grid of models with wavelength varying
    along final axis (in 3d array)

    DEPENDS ON pysynphot, which may not be an awesome thing, but
        it definitely preserves spectral integrity, and does not suffer
        from drawbacks of interpolation (failing to average line profiles)
    '''
    dlogL_ssp = np.median(logL_ssp[1:] - logL_ssp[:-1])
    f = dlogL_ssp / dlogL_new

    # print 'zoom factor: {}'.format(f)
    # print 'new array should have length {}'.format(logL_ssp.shape[0]*f)

    # print spec_ssp.shape

    # we want to only sample where we're sure we have data
    CRVAL1_new = logL_ssp[0] - 0.5 * dlogL_ssp + 0.5 * dlogL_new
    CRSTOP_new = logL_ssp[-1] + 0.5 * dlogL_ssp - 0.5 * dlogL_new
    NAXIS1_new = int((CRSTOP_new - CRVAL1_new) / dlogL_new)
    # start at exp(CRVAL1_new) AA, and take samples every exp(dlogL_new) AA
    logL_ssp_new = CRVAL1_new + \
        np.linspace(0., dlogL_new*(NAXIS1_new - 1), NAXIS1_new)
    L_new = np.exp(logL_ssp_new)
    L_ssp = np.exp(logL_ssp)

    # now find the desired new wavelengths

    spec = spectrum.ArraySourceSpectrum(wave=L_ssp, flux=spec_ssp)
    f = np.ones_like(L_ssp)
    filt = spectrum.ArraySpectralElement(wave=L_ssp,
                                         throughput=f,
                                         waveunits='angstrom')
    obs = observation.Observation(spec, filt, binset=L_new, force='taper')
    spec_ssp_new = obs.binflux

    # the following are previous attempts to do this rebinning
    '''
    # first, interpolate to a constant multiple of the desired resolution
    r_interm = int(1./f)
    print r_interm
    dlogL_interm = f * dlogL_new
    print dlogL_interm

    CDELT1_interm = dlogL_interm
    CRVAL1_interm = logL_ssp[0] + 0.5*CDELT1_interm
    CRSTOP_interm = logL_ssp[-1] - 0.5*CDELT1_interm
    NAXIS1_interm = int((CRSTOP_interm - CRVAL1_interm) / CDELT1_interm)
    logL_ssp_interm = CRVAL1_interm + np.linspace(
        0., CDELT1_interm * (NAXIS1_interm - 1), NAXIS1_interm)
    edges_interm = np.column_stack((logL_ssp_interm - 0.5*CDELT1_interm,
                                    logL_ssp_interm + 0.5*CDELT1_interm))

    spec_interp = interp1d(logL_ssp, spec_ssp)
    spec_interm = spec_interp(logL_ssp_interm)
    print spec_interm.shape

    spec_ssp_new = zoom(spec_interm, zoom=[1., 1., 1./r_interm])[1:-1]
    logL_ssp_new = zoom(logL_ssp_interm, zoom=1./r_interm)[1:-1]
    print logL_ssp_new.shape'''
    '''s = np.cumsum(spec_ssp, axis=-1)
    # interpolate cumulative array
    s_interpolator = interp1d(x=logL_ssp, y=s, kind='linear')
    s_interpolated_l = s_interpolator(edges[:, 0])
    s_interpolated_u = s_interpolator(edges[:, 1])
    total_in_bin = np.diff(
        np.row_stack((s_interpolated_l, s_interpolated_u)), n=1, axis=0)
    spec_ssp_new = total_in_bin * (dlogL_new/dlogL_ssp)'''

    return spec_ssp_new, logL_ssp_new