Пример #1
0
def test_linear_write1():
    spec = read_fits.read_fits_spectrum1d(data_path('UVES.fits'))
    write_fits.write(spec, 'test.fits')
    test_spec = read_fits.read_fits_spectrum1d('test.fits')
    np.testing.assert_allclose(spec.data, test_spec.data)
    np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion, "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit
Пример #2
0
def test_multispec_combined():
    spectra = read_fits.read_fits_spectrum1d(data_path('Combined.fits'))
    write_fits.write(spectra, 'test.fits')
    test_spectra = read_fits.read_fits_spectrum1d('test.fits')
    for spec, test_spec in zip(spectra, test_spectra):
        np.testing.assert_allclose(spec.data, test_spec.data)
        np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion, "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit
Пример #3
0
def test_linear_write2():
    spec = read_fits.read_fits_spectrum1d(data_path('gbt_1d.fits'))
    write_fits.write(spec, 'test.fits')
    test_spec = read_fits.read_fits_spectrum1d('test.fits')
    np.testing.assert_allclose(spec.data, test_spec.data)
    np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion,
                                                   "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit
Пример #4
0
def test_multispec_combined():
    spectra = read_fits.read_fits_spectrum1d(data_path('Combined.fits'))
    write_fits.write(spectra, 'test.fits')
    test_spectra = read_fits.read_fits_spectrum1d('test.fits')
    for spec, test_spec in zip(spectra, test_spectra):
        np.testing.assert_allclose(spec.data, test_spec.data)
        np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion,
                                                   "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit
Пример #5
0
def test_multispec_spline():
    pytest.importorskip("scipy")
    spectra = read_fits.read_fits_spectrum1d(data_path('spline.fits'))
    write_fits.write(spectra, 'test.fits')
    test_spectra = read_fits.read_fits_spectrum1d('test.fits')
    for spec, test_spec in zip(spectra, test_spectra):
        np.testing.assert_allclose(spec.data, test_spec.data)
        np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion, "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit
Пример #6
0
def test_multispec_spline():
    pytest.importorskip("scipy")
    spectra = read_fits.read_fits_spectrum1d(data_path('spline.fits'))
    write_fits.write(spectra, 'test.fits')
    test_spectra = read_fits.read_fits_spectrum1d('test.fits')
    for spec, test_spec in zip(spectra, test_spectra):
        np.testing.assert_allclose(spec.data, test_spec.data)
        np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion,
                                                   "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit
Пример #7
0
def test_1d_multispec_combined():
    legendre = read_fits.read_fits_spectrum1d(data_path('TRES.fits'))[0]
    combined, chebyshev = read_fits.read_fits_spectrum1d(
        data_path('Combined.fits'))[0:2]
    weight_legendre = 2.0
    offset_legendre = 1.0
    weight_chebyshev = 3.0
    offset_chebyshev = 0.0

    test_value = weight_legendre*(offset_legendre+legendre.dispersion.value)\
        + weight_chebyshev*(offset_chebyshev+chebyshev.dispersion.value)
    np.testing.assert_allclose(combined.dispersion.value, test_value)
Пример #8
0
def test_1d_multispec_combined():
    legendre = read_fits.read_fits_spectrum1d(data_path('TRES.fits'))[0]
    combined, chebyshev = read_fits.read_fits_spectrum1d(
        data_path('Combined.fits'))[0:2]
    weight_legendre = 2.0
    offset_legendre = 1.0
    weight_chebyshev = 3.0
    offset_chebyshev = 0.0

    test_value = weight_legendre*(offset_legendre+legendre.dispersion.value)\
        + weight_chebyshev*(offset_chebyshev+chebyshev.dispersion.value)
    np.testing.assert_allclose(combined.dispersion.value, test_value)
Пример #9
0
def test_multispec_linear():
    spectra = read_fits.read_fits_spectrum1d(data_path(
                                                'multispec-linear-log.fits'))
    write_fits.write(spectra, 'test.fits')
    test_spectra = read_fits.read_fits_spectrum1d('test.fits')
    for spec, test_spec in zip(spectra, test_spectra):
        np.testing.assert_allclose(spec.data, test_spec.data)
        np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion, "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit

# TODO: create tests that utilize all methods to write fits header
Пример #10
0
def test_multispec_linear():
    spectra = read_fits.read_fits_spectrum1d(
        data_path('multispec-linear-log.fits'))
    write_fits.write(spectra, 'test.fits')
    test_spectra = read_fits.read_fits_spectrum1d('test.fits')
    for spec, test_spec in zip(spectra, test_spectra):
        np.testing.assert_allclose(spec.data, test_spec.data)
        np.testing.assert_allclose(spec.dispersion, test_spec.dispersion)
    if hasattr(spec.dispersion, "unit") or hasattr(test_spec.dispersion,
                                                   "unit"):
        assert spec.dispersion.unit == test_spec.dispersion.unit


# TODO: create tests that utilize all methods to write fits header
Пример #11
0
def test_multispec_linear():
    iraf = ascii.read(data_path('multispec-linear-log.fits.0001.dat'),
                      Reader=ascii.NoHeader, names=['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('multispec-linear-log.fits'))
    spec = spectra[0]
    np.testing.assert_allclose(iraf['wave'], spec.dispersion.value)
    assert spec.dispersion.unit == u.Angstrom
Пример #12
0
def test_multispec_cubic_spline():
    pytest.importorskip("scipy")
    filename = data_path("spline.fits")
    spectra = read_fits.read_fits_spectrum1d(filename)
    npieces = 10
    pmin = 1
    pmax = 2304
    pixels = np.arange(pmin, pmax + 1) * 1.0
    s = (pixels - pmin) / (pmax - pmin) * npieces
    j = np.array(map(int, s))
    a = (j + 1) - s
    b = s - j

    c2 = np.array([1.91932706, 4.50545551, 8.47358561, 8.37680956,
                   3.0511021, 2.54651656, 7.5758634, 7.68131867, 7.58694718,
                   3.14098023, 7.70766882, 7.90089733, 9.80179082])

    x0 = a ** 3
    x1 = (1 + 3 * a * (1 + a * b))
    x2 = (1 + 3 * b * (1 + a * b))
    x3 = b ** 3
    w2 = np.take(c2, j, mode='clip') * x0 + np.take(c2, 1 + j, mode='clip') * x1 \
        + np.take(c2, 2 + j, mode='clip') * x2 + np.take(c2, 3 + j,
                                                         mode='clip') * x3
    np.testing.assert_allclose(w2, spectra[1].dispersion.value)
Пример #13
0
    def read_fits_file(self):
        # filename = unicode(self.filename.toUtf8(), encoding="UTF-8")
        try:
            if 'specutils' not in sys.modules:
                from specutils.io import read_fits

            spectrum = read_fits.read_fits_spectrum1d(self.filename)
            if len(spectrum) > 1:
                spectrum = spectrum[0]
            try:
                wave = np.array(spectrum.wavelength)
                flux = np.array(spectrum.flux)
            except AttributeError:
                wave = np.array(spectrum.dispersion)
                flux = np.array(spectrum.flux)
                print("No wavelength attribute in FITS File. Using 'dispersion' attribute instead")
        except Exception as e:
            hdulist = afits.open(self.filename)
            flux = hdulist[0].data
            header = hdulist[0].header
            if 'CDELT1' in header:
                wave_step = header['CDELT1']
            else:
                wave_step = header['CD1_1']
            wave_start = header['CRVAL1'] - (header['CRPIX1'] - 1) * wave_step
            wave_num = flux.shape[0]
            wave = np.linspace(wave_start, wave_start + wave_step * wave_num, num=wave_num)

        flux[np.isnan(flux)] = 0  # convert nan's to zeros

        return wave, flux
Пример #14
0
def test_1dspec_UVES():
    spec = read_fits.read_fits_spectrum1d(data_path('UVES.fits'))
    iraf = ascii.read(data_path('uves_iraf_read_truncated.dat'),
                      names=['index', 'wave', 'flux'])
    assert_almost_equal(spec.dispersion[iraf['index']], iraf['wave'])

    assert not hasattr(spec.dispersion, 'unit')
Пример #15
0
def test_1dspec_UVES():
    spec = read_fits.read_fits_spectrum1d(data_path('UVES.fits'))
    iraf = ascii.read(data_path('uves_iraf_read_'
                                'truncated.dat'), names=['index', 'wave', 'flux'])
    np.testing.assert_allclose(spec.dispersion[iraf['index']], iraf['wave'])

    assert not hasattr(spec.dispersion, 'unit')
def fitspec():

    s=read_fits.read_fits_spectrum1d('E5_1_004.fits',dispersion_unit='Angstrom')
    wave_range = [21000,24000]

    good = np.where((s.wavelength.value > wave_range[0]) & (s.wavelength.value < wave_range[1]))[0]
    s = Spectrum1D.from_array(s.wavelength[good],s.flux[good]/np.median(s.flux[good]), dispersion_unit = s.wavelength.unit)
    snr = 40
    s.uncertainty = (np.zeros(len(s.flux.value))+1.0/snr)*s.flux.unit

    #g=load_grid('phoenix_20000_k.h5')
    g = load_grid('phoenix_r20000_1.9-2.5_k.h5')
    my_model = assemble_model(g, vrad=0, vrot=0,R=20000,spectrum=s,normalize_npol=4)

    wave,flux = my_model()
    plt.clf()
    plt.plot(s.wavelength,s.flux)
    plt.plot(wave,flux)

    teff_prior = priors.UniformPrior(3000,8000)
    logg_prior = priors.UniformPrior(0.0,4.4)
    mh_prior = priors.UniformPrior(-1.5,1.0)
    alpha_prior = priors.UniformPrior(-0.2,1.2)


    vrot_prior = priors.UniformPrior(0,20)
    vrad_prior = priors.UniformPrior(-300,300)
    R_prior = priors.GaussianPrior(5400,600)

    ll = Chi2Likelihood(s)
    my_model = my_model | ll

    fitobj = MultiNest(my_model, [teff_prior, logg_prior, mh_prior, alpha_prior, vrot_prior, vrad_prior, R_prior])
    result = fitobj.run()
    return result
Пример #17
0
def test_multispec_cubic_spline():
    pytest.importorskip("scipy")
    filename = data_path("spline.fits")
    spectra = read_fits.read_fits_spectrum1d(filename)
    npieces = 10
    pmin = 1
    pmax = 2304
    pixels = np.arange(pmin, pmax + 1) * 1.0
    s = (pixels - pmin) / (pmax - pmin) * npieces
    j = np.array(map(int, s))
    a = (j + 1) - s
    b = s - j

    c2 = np.array([
        1.91932706, 4.50545551, 8.47358561, 8.37680956, 3.0511021, 2.54651656,
        7.5758634, 7.68131867, 7.58694718, 3.14098023, 7.70766882, 7.90089733,
        9.80179082
    ])

    x0 = a**3
    x1 = (1 + 3 * a * (1 + a * b))
    x2 = (1 + 3 * b * (1 + a * b))
    x3 = b**3
    w2 = np.take(c2, j, mode='clip') * x0 + np.take(c2, 1 + j, mode='clip') * x1 \
        + np.take(c2, 2 + j, mode='clip') * x2 + np.take(c2, 3 + j,
                                                         mode='clip') * x3
    np.testing.assert_allclose(w2, spectra[1].dispersion.value)
Пример #18
0
def test_multispec_linear():
    iraf = ascii.read(data_path('multispec-linear-log.fits.0001.dat'),
                      Reader=ascii.NoHeader, names=['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('multispec-linear-log.fits'))
    spec = spectra[0]
    np.testing.assert_allclose(iraf['wave'], spec.dispersion.value)
    assert spec.dispersion.unit == u.Angstrom
Пример #19
0
def test_multispec_chebyshev():
    iraf = ascii.read(data_path('AAO_11.txt'),
                      data_start=175,
                      Reader=ascii.NoHeader,
                      names=['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('AAO.fits'))
    spec = spectra[10]
    np.testing.assert_allclose(iraf['wave'], spec.wavelength.value)
Пример #20
0
    def read_fits_file(self):
        #filename = unicode(self.filename.toUtf8(), encoding="UTF-8")
        spectrum = read_fits.read_fits_spectrum1d(self.filename)
        wave = np.array(spectrum.wavelength)
        flux = np.array(spectrum.flux)
        flux[np.isnan(flux)] = 0 #convert nan's to zeros

        return wave, flux
Пример #21
0
def test_multispec_legendre():
    iraf = ascii.read(data_path('TRES.dat'),
                      data_start=127,
                      Reader=ascii.NoHeader,
                      names=['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('TRES.fits'))
    spec = spectra[10]
    np.testing.assert_allclose(iraf['wave'], spec.dispersion.value)
    assert spec.dispersion.unit == u.Angstrom
Пример #22
0
def read_1dspec(file_name):
    """
    Reads 1-D Spectra from a FITS file and returns wavelength and flux arrays.
    Args:
        file_name    : FITS file from which data has to be extracted
    Returns:
        wave_array   : Array containing wavelength values extracted from the 1-D Spectra
        flux_array   : Array containing flux values extracted from the 1-D Spectra
    """
    with fits.open(file_name) as hdulist:
        axis = int(hdulist[0].header['NAXIS'])
        if axis == 1:
            flux_array = hdulist[0].data
            wave_array = spec.read_fits_spectrum1d(file_name).dispersion
        else:
            flux_array = hdulist[0].data[0][0]
            wave_array = spec.read_fits_spectrum1d(file_name)[0].dispersion

    return wave_array, flux_array
Пример #23
0
def test_spectrum_slicing():
    iraf = ascii.read(data_path('gbt_1d_iraf_read.dat'), names=['wave', 'flux'])
    spec = read_fits.read_fits_spectrum1d(data_path('gbt_1d.fits'))
    np.testing.assert_allclose(spec.dispersion.value, iraf['wave'])
    np.testing.assert_allclose(spec.flux, iraf['flux'])
    sliced = spec.slice_index(stop=100)
    np.testing.assert_allclose(sliced.dispersion.value, iraf["wave"][:100])
    np.testing.assert_allclose(sliced.flux, iraf["flux"][:100])
    np.testing.assert_allclose(spec.dispersion.value, iraf['wave'])
    np.testing.assert_allclose(spec.flux, iraf['flux'])
    sliced = sliced.slice_index(start=-1, step=-1)
    np.testing.assert_allclose(sliced.dispersion.value, iraf["wave"][99::-1])
    np.testing.assert_allclose(sliced.flux, iraf["flux"][99::-1])
Пример #24
0
    def from_fits(cls, path):
        """
        Load an echelle spectrum from a FITS file.

        Parameters
        ----------
        path : str
            Path to the FITS file
        """
        spectrum_list = read_fits.read_fits_spectrum1d(path)
        header = fits.getheader(path)

        name = header.get('OBJNAME', None)
        return cls(spectrum_list, header=header, name=name, fits_path=path)
Пример #25
0
def test_spectrum_slicing():
    iraf = ascii.read(data_path('gbt_1d_iraf_read.dat'),
                      names=['wave', 'flux'])
    spec = read_fits.read_fits_spectrum1d(data_path('gbt_1d.fits'))
    np.testing.assert_allclose(spec.dispersion.value, iraf['wave'])
    np.testing.assert_allclose(spec.flux, iraf['flux'])
    sliced = spec.slice_index(stop=100)
    np.testing.assert_allclose(sliced.dispersion.value, iraf["wave"][:100])
    np.testing.assert_allclose(sliced.flux, iraf["flux"][:100])
    np.testing.assert_allclose(spec.dispersion.value, iraf['wave'])
    np.testing.assert_allclose(spec.flux, iraf['flux'])
    sliced = sliced.slice_index(start=-1, step=-1)
    np.testing.assert_allclose(sliced.dispersion.value, iraf["wave"][99::-1])
    np.testing.assert_allclose(sliced.flux, iraf["flux"][99::-1])
Пример #26
0
def test_multispec_equispec_linear():
    iraf11 = ascii.read(data_path('multispec_equispec.11.dat'),
                      Reader=ascii.NoHeader, names=['wave', 'flux'])
    iraf12 = ascii.read(data_path('multispec_equispec.12.dat'),
                      Reader=ascii.NoHeader, names=['wave', 'flux'])
    iraf21 = ascii.read(data_path('multispec_equispec.21.dat'),
                      Reader=ascii.NoHeader, names=['wave', 'flux'])
    iraf22 = ascii.read(data_path('multispec_equispec.22.dat'),
                      Reader=ascii.NoHeader, names=['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('multispec_equispec.fits'))

    np.testing.assert_allclose(iraf11['wave'], spectra[0][0].dispersion.value)
    np.testing.assert_allclose(iraf12['wave'], spectra[0][1].dispersion.value)
    np.testing.assert_allclose(iraf21['wave'], spectra[1][0].dispersion.value)
    np.testing.assert_allclose(iraf22['wave'], spectra[1][1].dispersion.value)
    spec = spectra[0][0]
    assert spec.dispersion.unit == u.Angstrom
Пример #27
0
def test_multispec_linear_spline():
    pytest.importorskip("scipy")
    filename = data_path("spline.fits")
    spectra = read_fits.read_fits_spectrum1d(filename)
    c1 = np.array([8.61795084, 5.18731657, 0.78303947, 8.60234236,
                   9.65883078, 9.86579257, 8.2952905, 1.47467708,
                   7.86243508, 4.54125793, 7.96101273])
    npieces = 10
    pmin = 1
    pmax = 2304
    pixels = np.arange(pmin, pmax + 1) * 1.0
    s = (pixels - pmin) / (pmax - pmin) * npieces
    j = s.astype(int)
    a = (j + 1) - s
    b = s - j
    w1 = np.take(c1, j, mode='clip') * a + np.take(c1, 1 + j, mode='clip') * b
    np.testing.assert_allclose(w1, spectra[0].dispersion.value)
Пример #28
0
    def from_fits(cls, path):
        """
        Load an echelle spectrum from a FITS file.

        Parameters
        ----------
        path : str
            Path to the FITS file
        """
        spectrum_list = [
            Spectrum1D.from_specutils(s)
            for s in read_fits.read_fits_spectrum1d(path)
        ]
        header = fits.getheader(path)

        name = header.get('OBJNAME', None)
        return cls(spectrum_list, header=header, name=name, fits_path=path)
 def __init__(self, velscale, sigma, _noread=False):
     """ Load Miles templates for simulations. """
     self.velscale = velscale
     self.sigma = sigma
     miles_path = os.path.join(context.basedir, "ppxf/miles_models")
     # Search for spectra and their properties
     fitsfiles = [_ for _ in os.listdir(miles_path) if _.endswith(".fits")]
     # Define the different values of metallicities and ages of templates
     self.metals = np.unique([
         float(
             _.split("Z")[1].split("T")[0].replace("m",
                                                   "-").replace("p", "+"))
         for _ in fitsfiles
     ])
     self.ages = np.unique([
         float(
             _.split("T")[1].split("_iP")[0].replace("m",
                                                     "-").replace("p", "+"))
         for _ in fitsfiles
     ])
     # Defining arrays
     self.ages2D, self.metals2D = np.meshgrid(self.ages, self.metals)
     self.metals1D = self.metals2D.reshape(-1)
     self.ages1D = self.ages2D.reshape(-1)
     if _noread:
         return
     templates, norms = [], []
     for metal, age in zip(self.metals1D, self.ages1D):
         template_file = os.path.join(miles_path,
                                      self.miles_filename(metal, age))
         spec = read_fits_spectrum1d(template_file)
         wave = spec.dispersion
         flux = spec.flux
         speclog, logwave, _ = util.log_rebin([wave[0], wave[-1]],
                                              flux,
                                              velscale=self.velscale)
         speclog = gaussian_filter1d(speclog, sigma / velscale)
         wave = wave[1:-2]
         flux = spectres(wave, np.exp(logwave), speclog)
         norm = np.sum(flux)
         templates.append(flux / norm)
     self.templates = np.array(templates)
     self.norms = np.array(norms)
     self.wave = wave
     return
Пример #30
0
def test_multispec_linear_spline():
    pytest.importorskip("scipy")
    filename = data_path("spline.fits")
    spectra = read_fits.read_fits_spectrum1d(filename)
    c1 = np.array([
        8.61795084, 5.18731657, 0.78303947, 8.60234236, 9.65883078, 9.86579257,
        8.2952905, 1.47467708, 7.86243508, 4.54125793, 7.96101273
    ])
    npieces = 10
    pmin = 1
    pmax = 2304
    pixels = np.arange(pmin, pmax + 1) * 1.0
    s = (pixels - pmin) / (pmax - pmin) * npieces
    j = s.astype(int)
    a = (j + 1) - s
    b = s - j
    w1 = np.take(c1, j, mode='clip') * a + np.take(c1, 1 + j, mode='clip') * b
    np.testing.assert_allclose(w1, spectra[0].dispersion.value)
Пример #31
0
def test_multispec_equispec_linear():
    iraf11 = ascii.read(data_path('multispec_equispec.11.dat'),
                        Reader=ascii.NoHeader,
                        names=['wave', 'flux'])
    iraf12 = ascii.read(data_path('multispec_equispec.12.dat'),
                        Reader=ascii.NoHeader,
                        names=['wave', 'flux'])
    iraf21 = ascii.read(data_path('multispec_equispec.21.dat'),
                        Reader=ascii.NoHeader,
                        names=['wave', 'flux'])
    iraf22 = ascii.read(data_path('multispec_equispec.22.dat'),
                        Reader=ascii.NoHeader,
                        names=['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(
        data_path('multispec_equispec.fits'))

    np.testing.assert_allclose(iraf11['wave'], spectra[0][0].dispersion.value)
    np.testing.assert_allclose(iraf12['wave'], spectra[0][1].dispersion.value)
    np.testing.assert_allclose(iraf21['wave'], spectra[1][0].dispersion.value)
    np.testing.assert_allclose(iraf22['wave'], spectra[1][1].dispersion.value)
    spec = spectra[0][0]
    assert spec.dispersion.unit == u.Angstrom
Пример #32
0
def drawspectrumUDSz(DR1_ID, DR11_ID, z):

    spec = read_fits.read_fits_spectrum1d(
        'variable-spectra/UDSz_DR1_' + DR1_ID + '_DR11_' + DR11_ID + '.fits',
        dispersion_unit='angstrom')
    plt.figure(figsize=[17, 4])
    plt.plot(spec.wavelength, spec.flux)
    axes = plt.gca()
    ylims = axes.get_ylim()

    for key in linedict:
        val = linedict[key]
        lineval = val * (z + 1) * u.AA
        if lineval < max(spec.wavelength) and lineval > min(spec.wavelength):
            plt.vlines(lineval.value,
                       ymin=ylims[0],
                       ymax=ylims[1],
                       linestyles='dashed')
            if key == 'SiII' or key == 'LyA' or key == 'LyB':
                plt.text(lineval.value - 140, ylims[0] + 0.2e-18, key)
            elif key == 'SiIV_1393+OIV1402':
                plt.text(lineval.value - 400, ylims[0] + 0.2e-18, key)
            elif key == 'Balmer_break':
                plt.text(lineval.value - 300, ylims[1] + 0.2e-18, key)
            elif key == 'OIII_doublet-1/3':
                plt.text(lineval.value - 200, ylims[1] + 0.2e-18,
                         'OIII_doublet')
            elif key == 'OIII_doublet-1':
                continue
            else:
                plt.text(lineval.value + 10, ylims[0] + 0.2e-18, key)

    plt.ylim(ylims)
    plt.ylabel('Flux')
    unit = u.AA
    plt.xlabel('Wavelength, ' + unit.to_string('latex'))
    plt.tight_layout()
    plt.savefig(DR11_ID + 'spectra.pdf')
synbands=[]

for name, lams in zip(synname,synlam):
    synbands.append(sncosmo.Bandpass(lams, [1.,1.], name='tophat'+name))


names168 = [line.split()[0] for line in open('table.txt')]
dic_meta=cPickle.load(open("CABALLOv2/META.pkl"))
ans=[]
for sn in names168:
    meta = dic_meta[sn]
    for nm in meta['spectra']:
        spec=meta['spectra'][nm]
        if abs(spec['salt2.phase']) <2.5:
            name = "CABALLOv2/"+spec['idr.spec_merged']
            spectra = read_fits.read_fits_spectrum1d(name, dispersion_unit='angstrom')


            model0 = sncosmo.TimeSeriesSource(numpy.array([0.,1,2]), spectra.dispersion.value/(1+meta['salt2.Redshift']), \
                numpy.tile(spectra.flux.value,(3,1)))
            dust = sncosmo.F99Dust(r_v=2.5)
            dust.set(ebv=0.01)
            model = sncosmo.Model(source=model0, effects=[dust], effect_names=['host'], effect_frames=['rest'])
            try:

                A= -2.5*numpy.log10(model.bandflux(synbands,0.)/model0.bandflux(synbands,0.))
                ans.append(A[1]/(A[0]-A[1]))
            except:
                pass

ans = numpy.array(ans)
Пример #34
0
def test_1dspec_UVES():
    spec = read_fits.read_fits_spectrum1d(data_path("UVES.fits"))
    iraf = ascii.read(data_path("uves_iraf_read_" "truncated.dat"), names=["index", "wave", "flux"])
    np.testing.assert_allclose(spec.dispersion[iraf["index"]], iraf["wave"])

    assert not hasattr(spec.dispersion, "unit")
Пример #35
0
def test_1dspec_vrad():
    iraf = ascii.read(data_path("gbt_1d_iraf_read.dat"), names=["wave", "flux"])
    spec = read_fits.read_fits_spectrum1d(data_path("gbt_1d.fits"))
    np.testing.assert_allclose(iraf["wave"], spec.dispersion.value)
    assert spec.dispersion.unit == u.Unit("km/s")
Пример #36
0
def readspec(
    specfil,
    inflg=None,
    efil=None,
    outfil=None,
    show_plot=0,
    use_barak=False,
    verbose=False,
    flux_tags=None,
    sig_tags=None,
    multi_ivar=False,
):
    """ 
    specfil: string or Table
    multi_ivar: Bool (False)
      BOSS format of  flux, ivar, log10(wave) in multi-extension FITS
    """
    from xastropy.files import general as xfg

    # from xastropy.plotting import x_guis as xpxg
    from astropy.table import Table
    from astropy.table import Column

    raise ValueError("USE LINETOOLS.spectra.io INSTEAD!!")

    # Initialize
    dat = None
    if inflg == None:
        inflg = 0

    # Check specfil type
    if type(specfil) is Table:
        datfil = "None"
        # Dummy hdulist
        hdulist = [fits.PrimaryHDU(), specfil]
    else:
        # Read header
        datfil, chk = xfg.chk_for_gz(specfil)
        if chk == 0:
            print("xastropy.spec.readwrite: File does not exist ", specfil)
            return -1
        hdulist = fits.open(os.path.expanduser(datfil))

    head0 = hdulist[0].header

    ## #################
    # Binary FITS table?
    if head0["NAXIS"] == 0:
        # Flux
        if flux_tags is None:
            flux_tags = ["SPEC", "FLUX", "FLAM", "FX", "FLUXSTIS", "FLUX_OPT", "fl"]
        fx, fx_tag = get_table_column(flux_tags, hdulist)
        # xdb.set_trace()
        if fx is None:
            print("spec.readwrite: Binary FITS Table but no Flux tag")
            return
        # Error
        if sig_tags is None:
            sig_tags = ["ERROR", "ERR", "SIGMA_FLUX", "FLAM_SIG", "SIGMA_UP", "ERRSTIS", "FLUXERR", "er"]
        sig, sig_tag = get_table_column(sig_tags, hdulist)
        if sig is None:
            ivar_tags = ["IVAR", "IVAR_OPT"]
            ivar, ivar_tag = get_table_column(ivar_tags, hdulist)
            if ivar is None:
                print("spec.readwrite: Binary FITS Table but no error tags")
                return
            else:
                sig = np.zeros(ivar.size)
                gdi = np.where(ivar > 0.0)[0]
                sig[gdi] = np.sqrt(1.0 / ivar[gdi])
        # Wavelength
        wave_tags = ["WAVE", "WAVELENGTH", "LAMBDA", "LOGLAM", "WAVESTIS", "WAVE_OPT", "wa"]
        wave, wave_tag = get_table_column(wave_tags, hdulist)
        if wave_tag == "LOGLAM":
            wave = 10.0 ** wave
        if wave is None:
            print("spec.readwrite: Binary FITS Table but no wavelength tag")
            return
    elif head0["NAXIS"] == 1:  # Data in the zero extension
        # How many entries?
        if len(hdulist) == 1:  # Old school (one file per flux, error)
            # Error
            if efil == None:
                ipos = max(specfil.find("F.fits"), specfil.find("f.fits"))
                if ipos < 0:  # No error array
                    efil = None
                    # sig = np.zeros(fx.size)
                else:
                    if specfil.find("F.fits") > 0:
                        efil, chk = xfg.chk_for_gz(specfil[0:ipos] + "E.fits")
                    else:
                        efil, chk = xfg.chk_for_gz(specfil[0:ipos] + "e.fits")
                if efil != None:
                    efil = os.path.expanduser(efil)
            # Generate Spectrum1D
            spec1d = spec_read_fits.read_fits_spectrum1d(os.path.expanduser(datfil), dispersion_unit="AA", efil=efil)
            xspec1d = XSpectrum1D.from_spec1d(spec1d)

            # spec1d = spec_read_fits.read_fits_spectrum1d(os.path.expanduser(datfil))

        elif len(hdulist) == 2:  # NEW SCHOOL (one file per flux, error)
            spec1d = spec_read_fits.read_fits_spectrum1d(os.path.expanduser(datfil), dispersion_unit="AA")
            # Error array
            sig = hdulist[1].data
            spec1d.uncertainty = StdDevUncertainty(sig)
            #
            xspec1d = XSpectrum1D.from_spec1d(spec1d)

        else:  # ASSUMING MULTI-EXTENSION
            if len(hdulist) <= 2:
                print("spec.readwrite: No wavelength info but only 2 extensions!")
                return
            fx = hdulist[0].data.flatten()
            sig = hdulist[1].data.flatten()
            wave = hdulist[2].data.flatten()
            # BOSS/SDSS?
            try:
                multi_ivar = head0["TELESCOP"][0:4] in ["SDSS"]
            except KeyError:
                pass
            #
            if multi_ivar is True:
                tmpsig = np.zeros(len(sig))
                gdp = np.where(sig > 0.0)[0]
                tmpsig[gdp] = np.sqrt(1.0 / sig[gdp])
                sig = tmpsig
                wave = 10.0 ** wave
    else:  # Should not be here
        print("spec.readwrite: Looks like an image")
        return dat

    # Generate, as needed
    if "xspec1d" not in locals():
        # Give Ang as default
        if not hasattr(wave, "unit"):
            uwave = u.Quantity(wave, unit=u.AA)
        else:
            if wave.unit is None:
                uwave = u.Quantity(wave, unit=u.AA)
            else:
                uwave = u.Quantity(wave)
        xspec1d = XSpectrum1D.from_array(uwave, u.Quantity(fx), uncertainty=StdDevUncertainty(sig))

    xspec1d.filename = specfil

    # Continuum?
    try:
        co = fits.getdata(name + "_c.fits")
    except:
        try:
            npix = len(fx)
        except UnboundLocalError:
            npix = len(xspec1d.flux)
        co = np.nan * np.ones(npix)

    """
    # Generate a Barak Spectrum Class?
    hd = hdulist[0].header
    if use_barak is True:
        # Barak
        raise ValueError('Avoid!')
        from barak import spec as bs
        spec1d = bs.Spectrum(wa=wave, fl=fx, er=sig, co=co, filename=specfil)
        spec1d.header = hd
    """

    # Plot?
    if show_plot:
        xpxg.plot_1d_arrays(wave, fx, sig, co)

    # Write to disk? Unlikely
    if outfil != None:
        if use_barak is True:
            spec1d.fits_write(outfil, overwrite=True)
        else:
            xdb.set_trace()  # Not ready

    # Add in the header
    xspec1d.head = head0

    # Return
    return xspec1d
Пример #37
0
def readspec(specfil,
             inflg=None,
             efil=None,
             outfil=None,
             show_plot=0,
             use_barak=False,
             verbose=False,
             flux_tags=None,
             sig_tags=None,
             multi_ivar=False):
    ''' 
    specfil: string or Table
    multi_ivar: Bool (False)
      BOSS format of  flux, ivar, log10(wave) in multi-extension FITS
    '''
    from xastropy.files import general as xfg
    #from xastropy.plotting import x_guis as xpxg
    from astropy.table import Table
    from astropy.table import Column

    raise ValueError('USE LINETOOLS.spectra.io INSTEAD!!')

    # Initialize
    dat = None
    if inflg == None:
        inflg = 0

    # Check specfil type
    if type(specfil) is Table:
        datfil = 'None'
        # Dummy hdulist
        hdulist = [fits.PrimaryHDU(), specfil]
    else:
        # Read header
        datfil, chk = xfg.chk_for_gz(specfil)
        if chk == 0:
            print('xastropy.spec.readwrite: File does not exist ', specfil)
            return -1
        hdulist = fits.open(os.path.expanduser(datfil))

    head0 = hdulist[0].header

    ## #################
    # Binary FITS table?
    if head0['NAXIS'] == 0:
        # Flux
        if flux_tags is None:
            flux_tags = [
                'SPEC', 'FLUX', 'FLAM', 'FX', 'FLUXSTIS', 'FLUX_OPT', 'fl'
            ]
        fx, fx_tag = get_table_column(flux_tags, hdulist)
        #xdb.set_trace()
        if fx is None:
            print('spec.readwrite: Binary FITS Table but no Flux tag')
            return
        # Error
        if sig_tags is None:
            sig_tags = [
                'ERROR', 'ERR', 'SIGMA_FLUX', 'FLAM_SIG', 'SIGMA_UP',
                'ERRSTIS', 'FLUXERR', 'er'
            ]
        sig, sig_tag = get_table_column(sig_tags, hdulist)
        if sig is None:
            ivar_tags = ['IVAR', 'IVAR_OPT']
            ivar, ivar_tag = get_table_column(ivar_tags, hdulist)
            if ivar is None:
                print('spec.readwrite: Binary FITS Table but no error tags')
                return
            else:
                sig = np.zeros(ivar.size)
                gdi = np.where(ivar > 0.)[0]
                sig[gdi] = np.sqrt(1. / ivar[gdi])
        # Wavelength
        wave_tags = [
            'WAVE', 'WAVELENGTH', 'LAMBDA', 'LOGLAM', 'WAVESTIS', 'WAVE_OPT',
            'wa'
        ]
        wave, wave_tag = get_table_column(wave_tags, hdulist)
        if wave_tag == 'LOGLAM':
            wave = 10.**wave
        if wave is None:
            print('spec.readwrite: Binary FITS Table but no wavelength tag')
            return
    elif head0['NAXIS'] == 1:  # Data in the zero extension
        # How many entries?
        if len(hdulist) == 1:  # Old school (one file per flux, error)
            # Error
            if efil == None:
                ipos = max(specfil.find('F.fits'), specfil.find('f.fits'))
                if ipos < 0:  # No error array
                    efil = None
                    #sig = np.zeros(fx.size)
                else:
                    if specfil.find('F.fits') > 0:
                        efil, chk = xfg.chk_for_gz(specfil[0:ipos] + 'E.fits')
                    else:
                        efil, chk = xfg.chk_for_gz(specfil[0:ipos] + 'e.fits')
                if efil != None:
                    efil = os.path.expanduser(efil)
            # Generate Spectrum1D
            spec1d = spec_read_fits.read_fits_spectrum1d(
                os.path.expanduser(datfil), dispersion_unit='AA', efil=efil)
            xspec1d = XSpectrum1D.from_spec1d(spec1d)

            #spec1d = spec_read_fits.read_fits_spectrum1d(os.path.expanduser(datfil))

        elif len(hdulist) == 2:  # NEW SCHOOL (one file per flux, error)
            spec1d = spec_read_fits.read_fits_spectrum1d(
                os.path.expanduser(datfil), dispersion_unit='AA')
            # Error array
            sig = hdulist[1].data
            spec1d.uncertainty = StdDevUncertainty(sig)
            #
            xspec1d = XSpectrum1D.from_spec1d(spec1d)

        else:  # ASSUMING MULTI-EXTENSION
            if len(hdulist) <= 2:
                print(
                    'spec.readwrite: No wavelength info but only 2 extensions!'
                )
                return
            fx = hdulist[0].data.flatten()
            sig = hdulist[1].data.flatten()
            wave = hdulist[2].data.flatten()
            # BOSS/SDSS?
            try:
                multi_ivar = head0['TELESCOP'][0:4] in ['SDSS']
            except KeyError:
                pass
            #
            if multi_ivar is True:
                tmpsig = np.zeros(len(sig))
                gdp = np.where(sig > 0.)[0]
                tmpsig[gdp] = np.sqrt(1. / sig[gdp])
                sig = tmpsig
                wave = 10.**wave
    else:  # Should not be here
        print('spec.readwrite: Looks like an image')
        return dat

    # Generate, as needed
    if 'xspec1d' not in locals():
        # Give Ang as default
        if not hasattr(wave, 'unit'):
            uwave = u.Quantity(wave, unit=u.AA)
        else:
            if wave.unit is None:
                uwave = u.Quantity(wave, unit=u.AA)
            else:
                uwave = u.Quantity(wave)
        xspec1d = XSpectrum1D.from_array(uwave,
                                         u.Quantity(fx),
                                         uncertainty=StdDevUncertainty(sig))

    xspec1d.filename = specfil

    # Continuum?
    try:
        co = fits.getdata(name + '_c.fits')
    except:
        try:
            npix = len(fx)
        except UnboundLocalError:
            npix = len(xspec1d.flux)
        co = np.nan * np.ones(npix)
    '''
    # Generate a Barak Spectrum Class?
    hd = hdulist[0].header
    if use_barak is True:
        # Barak
        raise ValueError('Avoid!')
        from barak import spec as bs
        spec1d = bs.Spectrum(wa=wave, fl=fx, er=sig, co=co, filename=specfil)
        spec1d.header = hd
    '''

    # Plot?
    if show_plot:
        xpxg.plot_1d_arrays(wave, fx, sig, co)

    # Write to disk? Unlikely
    if outfil != None:
        if use_barak is True:
            spec1d.fits_write(outfil, overwrite=True)
        else:
            xdb.set_trace()  # Not ready

    # Add in the header
    xspec1d.head = head0

    # Return
    return xspec1d
Пример #38
0
def readspec(specfil, inflg=None, efil=None, verbose=False, flux_tags=None,
    sig_tags=None, multi_ivar=False, format='ascii', exten=None):
    """ Read a FITS file (or astropy Table or ASCII file) into a
    Spectrum1D class

    Parameters
    ----------
    specfil : str or Table
      Input file. If str:
        * FITS file must include in '.fit'
        * ASCII must either have a proper Table format
          or be 3 columns with WAVE,FLUX,ERROR
    efil : string, optional
      Explicit filename for Error array.  Code will attempt to find this
      file on its own.
    flux_tags : list of strings, optional
      Tags for flux in Binary FITS table. Default: flux_tags = ['SPEC',
      'FLUX','FLAM','FX', 'FLUXSTIS', 'FLUX_OPT', 'fl']
    sig_tags : list of strings, optional
      Tags for error in Binary FITS table. Default : sig_tags =
      ['ERROR','ERR','SIGMA_FLUX','FLAM_SIG', 'SIGMA_UP', 'ERRSTIS',
      'FLUXERR', 'er']
    multi_ivar : bool, optional
      If True, assume BOSS format of flux, ivar, log10(wave) in
      multi-extension FITS.
    format : str, optional
      Format for ASCII table input ['ascii']
    exten : int, optional
      FITS extension (mainly for multiple binary FITS tables)

    Returns
    -------
    An XSpectrum1D class
    """
    from specutils.io import read_fits as spec_read_fits
    from linetools.spectra.xspectrum1d import XSpectrum1D

    # Initialize

    if inflg == None:
        inflg = 0

    # Check specfil type
    if isinstance(specfil, Table):  # MAYBE SHOULD USE SPECUTILS FROM_TABLE
        datfil = 'None'
        # Dummy hdulist
        hdulist = [fits.PrimaryHDU(), specfil]
    elif isinstance(specfil, basestring):
        datfil = specfil.strip()
        flg_fits = False
        for ext in ['.fit']:
            if ext in specfil:
                flg_fits = True
        if flg_fits: # FITS
            # Read header
            datfil, chk = chk_for_gz(specfil.strip())
            if chk == 0:
                raise IOError('File does not exist {}'.format(specfil))
            hdulist = fits.open(os.path.expanduser(datfil))
        else: #ASCII
            tbl = Table.read(specfil,format=format)
            # No header?
            if tbl.colnames[0] == 'col1':
                names = 'WAVE', 'FLUX', 'ERROR', 'CONTINUUM'
                for i,name in enumerate(tbl.colnames):
                    tbl[name].name = names[i]
            hdulist = [fits.PrimaryHDU(), tbl]
    else:
        raise IOError('readspec: Bad spectra input')

    head0 = hdulist[0].header

    co = None

    ## #################
    # Binary FITS table?

    if is_UVES_popler(head0):
        xspec1d = parse_UVES_popler(hdulist)
    elif head0['NAXIS'] == 0:
        # Flux
        if flux_tags is None:
            flux_tags = ['SPEC', 'FLUX', 'FLAM', 'FX',
                         'FLUXSTIS', 'FLUX_OPT', 'fl', 'flux', 'counts',
                         'COUNTS']
        fx, fx_tag = get_table_column(flux_tags, hdulist, idx=exten)
        if fx is None:
            print('Binary FITS Table but no Flux tag')
            return
        # Error
        if sig_tags is None:
            sig_tags = ['ERROR','ERR','SIGMA_FLUX','FLAM_SIG', 'SIGMA_UP',
                        'ERRSTIS', 'FLUXERR', 'SIGMA', 'sigma', 'sigma_flux',
                        'er', 'err', 'error']
        sig, sig_tag = get_table_column(sig_tags, hdulist)
        if sig is None:
            ivar_tags = ['IVAR', 'IVAR_OPT', 'ivar']
            ivar, ivar_tag = get_table_column(ivar_tags, hdulist, idx=exten)
            if ivar is None:
                var_tags = ['VAR', 'var']
                var, var_tag = get_table_column(var_tags, hdulist, idx=exten)
                sig = np.sqrt(var)
            else:
                sig = np.zeros(ivar.size)
                gdi = np.where( ivar > 0.)[0]
                sig[gdi] = np.sqrt(1./ivar[gdi])
        # Wavelength
        wave_tags = ['WAVE','WAVELENGTH','LAMBDA','LOGLAM',
                     'WAVESTIS', 'WAVE_OPT', 'wa', 'wave', 'loglam']
        wave, wave_tag = get_table_column(wave_tags, hdulist, idx=exten)
        if wave_tag in ['LOGLAM','loglam']:
            wave = 10.**wave
        if wave is None:
            print('Binary FITS Table but no wavelength tag')
            return
        co_tags = ['CONT', 'CO', 'CONTINUUM', 'co', 'cont']
        co, co_tag = get_table_column(co_tags, hdulist, idx=exten)

    elif head0['NAXIS'] == 1: # Data in the zero extension

        # How many entries?
        if len(hdulist) == 1: # Old school (one file per flux, error)
            # Error
            if efil == None:
                ipos = max(specfil.find('F.fits'),
                    specfil.find('f.fits'), specfil.find('flx.fits'))
                if ipos < 0:
                    # Becker XShooter style
                    ipos = specfil.find('.fits')
                    efil,chk = chk_for_gz(specfil[0:ipos]+'e.fits')
                else:
                    if specfil.find('F.fits') > 0:
                        efil,chk = chk_for_gz(specfil[0:ipos]+'E.fits')
                    else:
                        efil,chk = chk_for_gz(specfil[0:ipos]+'e.fits')
                    if efil is None:
                        efil,chk = chk_for_gz(specfil[0:ipos]+'err.fits')
                if efil is not None:
                    efil = os.path.expanduser(efil)

            # Error file
            if efil is not None:
                sig = fits.getdata(efil)
                uncertainty = StdDevUncertainty(sig)
            else:
                uncertainty = None

            #Log-Linear?
            try:
                dc_flag = head0['DC-FLAG']
            except KeyError:
                # The following is necessary for Becker's XShooter output
                cdelt1, dc_flag = get_cdelt_dcflag(head0)

            # Read
            if dc_flag == 0:
                # Read FITS file
                spec1d = spec_read_fits.read_fits_spectrum1d(os.path.expanduser(datfil), dispersion_unit='AA')
                spec1d.uncertainty = uncertainty
                xspec1d = XSpectrum1D.from_spec1d(spec1d)
            elif dc_flag == 1: # Generate wavelengths and use array approach
                fx = hdulist[0].data
                # Generate wave
                wave = setwave(head0)
            else:
                raise ValueError('DC-FLAG has unusual value {:d}'.format(dc_flag))

        elif hdulist[0].name == 'FLUX':
            # NEW SCHOOL (one file for flux and error)
            if 'WAVELENGTH' not in hdulist:
                spec1d = spec_read_fits.read_fits_spectrum1d(
                    os.path.expanduser(datfil), dispersion_unit='AA')
                xspec1d = XSpectrum1D.from_spec1d(spec1d)
            else:
                wave = hdulist['WAVELENGTH'].data * u.AA
                fx = hdulist['FLUX'].data
                xspec1d = XSpectrum1D.from_tuple((wave, fx))

            # Error array
            if 'ERROR' in hdulist:
                sig = hdulist['ERROR'].data
                xspec1d.uncertainty = StdDevUncertainty(sig)
            else:
                sig = None

            if 'CONTINUUM' in hdulist:
                xspec1d.co = hdulist['CONTINUUM'].data

            if 'METADATA' in head0:
                xspec1d.meta.update(json.loads(head0['METADATA']))

        else:  # ASSUMING MULTI-EXTENSION
            if len(hdulist) <= 2:
                raise RuntimeError('No wavelength info but only 2 extensions!')
            fx = hdulist[0].data.flatten()
            try:
                sig = hdulist[1].data.flatten()
            except AttributeError:  # Error array is "None"
                sig = None
            wave = hdulist[2].data.flatten()
            # BOSS/SDSS?
            try:
                multi_ivar = head0['TELESCOP'][0:4] in ['SDSS']
            except KeyError:
                pass
            #
            if multi_ivar is True:
                tmpsig = np.zeros(len(sig))
                gdp = np.where(sig > 0.)[0]
                tmpsig[gdp] = np.sqrt(1./sig[gdp])
                sig = tmpsig
                wave = 10.**wave
    elif (head0['NAXIS'] == 2) and (head0['NAXIS2'] == 5): # SDSS .fit format
        fx = hdulist[0].data[0,:].flatten()
        sig = hdulist[0].data[2,:].flatten()
        wave = setwave(head0)
    else:  # Should not be here
        print('Looks like an image')
        return


    # Generate, as needed
    if 'xspec1d' not in locals():
        # Give Ang as default
        if not hasattr(wave, 'unit'):
            uwave = u.Quantity(wave, unit=u.AA)
        elif wave.unit is None:
            uwave = u.Quantity(wave, unit=u.AA)
        else:
            uwave = u.Quantity(wave)
        if sig is not None:
            xspec1d = XSpectrum1D.from_tuple((uwave, fx, sig))
        else:
            xspec1d = XSpectrum1D.from_tuple((uwave, fx))

    if np.any(np.isnan(xspec1d.dispersion)):
        warnings.warn('WARNING: Some wavelengths are NaN')

    # Filename
    xspec1d.filename = datfil

    if not hasattr(xspec1d, 'co'):
        xspec1d.co = co
        # Final check for continuum in a separate file
        if isinstance(specfil,basestring):
            if co is None and specfil.endswith('.fits'):
                try:
                    xspec1d.co = fits.getdata(specfil.replace('.fits', '_c.fits'))
                except IOError:
                    pass

    # Add in the header
    xspec1d.head = head0

    # Return
    return xspec1d
Пример #39
0
def convert_spectrum(fraw, fout, BCV=False):
    '''
    param fraw: the raw counts
    param fout: the .json file to save to

    '''

    raw_list = read_fits.read_fits_spectrum1d(fraw)
    head = fits.getheader(fraw)
    try:
        BCV = head["BCV"]
    except KeyError:
        print("No BCV correction for", fraw)
        BCV = 0.0

    BCV_cor = np.sqrt((c_kms_air + BCV) / (c_kms_air - BCV))

    echelle_dict = {}
    npix = len(raw_list[0].wavelength)

    # Do this for each order in the spectrum
    for i,raw in enumerate(raw_list):

        # Correct for the barycentric shift
        wl = raw.wavelength.value

        # Scale the sigma values by the same blaze function that the raw fluxes were scaled by
        # raw_flux = raw.flux.value
        # raw_flux[raw_flux==0] = 1.0
        # Where the ratio values are 0, just set it to 1, since the noise will be 0 here too.
        # ratio = blaze.flux.value/raw_flux
        # sigma = ratio * np.sqrt(raw.flux.value)

        if BCV:
            wl = wl * BCV_cor

        order_dict = {  "wl":wl,
                        "fl":raw.flux.value,
                        "mask": np.ones((npix,), dtype="bool")}

        echelle_dict["order_{}".format(i)] = order_dict

    UT = head["DATE-OBS"]
    echelle_dict["UT"] = UT
    echelle_dict["AIR"] = head["AIR"]
    echelle_dict["EXPTIME"] = head["EXPTIME"]

    try:
        echelle_dict["HJD"] = head["HJDN"]
    except KeyError:
        print("Spectrum does not have HJDN keyword", UT)
        print("Setting to 0.0")
        echelle_dict["HJD"] = 0.0

    try:
        echelle_dict["BCV"] = BCV
    except KeyError:
        print("Spectrum does not have BCV keyword", UT)
        print("Setting to 0.0")
        echelle_dict["BCV"] = 0.0

    ej.write(fout, echelle_dict)
Пример #40
0
def test_1dspec_vrad():
    iraf = ascii.read(data_path('gbt_1d_iraf_read.dat'), names=['wave', 'flux'])
    spec = read_fits.read_fits_spectrum1d(data_path('gbt_1d.fits'))
    np.testing.assert_allclose(iraf['wave'], spec.dispersion.value)
    assert spec.dispersion.unit == u.Unit('km/s')
Пример #41
0
def main():

    import argparse

    parser = argparse.ArgumentParser(description="Merge TRES echelle orders.")
    parser.add_argument(
        "rfits",
        help="Name of the FITS file containing the RAW spectrum to merge.")
    parser.add_argument(
        "bfits",
        help=
        "Name of the FITS file containing the BLAZE-corrected spectrum to merge."
    )
    parser.add_argument(
        "template",
        help="Name of the FITS file containing the Kurucz template spectrum.")
    parser.add_argument(
        "--outfile",
        default="merged-spec.txt",
        help="Name of the output file to write the merged echelle spectrum to."
    )
    parser.add_argument(
        "--clobber",
        action="store_true",
        help="Overwrite any existing output file with new data.")
    parser.add_argument("--plot",
                        action="store_true",
                        help="Make a set of plots of the merged spectra.")
    parser.add_argument(
        "-t",
        "--trim",
        type=int,
        default=6,
        help="How many pixels to trim from the front of the file. Default is 6"
    )
    parser.add_argument(
        "--shift",
        default=0.0,
        type=float,
        help=
        "Doppler shift the synthetic spectrum by this amount (in km/s) before doing the merge process. This may help if the target star has an exceptionally high radial velocity. Positive velocities correspond to redshifting the template."
    )
    parser.add_argument(
        "--poly-order",
        default=3,
        type=int,
        help=
        "The order Chebyshev polynomial used to flatten each echelle order. 0 = constant, 1 = line, 2 = parabola, 3 = cubic, ... etc."
    )
    args = parser.parse_args()

    ######################
    # TEMPLATE PROCESSING
    # read the template
    hdul_t = fits.open(args.template)
    fl_t = hdul_t[0].data
    hdr_t = hdul_t[0].header

    # calculate the wavelength vector from the quantities in the FITS header
    num = len(fl_t)
    p = np.arange(num)
    w1 = hdr_t['CRVAL1']
    dw = hdr_t['CDELT1']
    wl_t = 10**(w1 + dw * p)
    hdul_t.close()

    dopp_shift = np.sqrt((c_kms_air + args.shift) / (c_kms_air - args.shift))
    wl_t = wl_t * dopp_shift

    #Also, we should convert from f_nu to f_lam
    fl_t *= c_ang / wl_t**2  #Convert from f_nu to f_lambda
    fl_t /= np.average(fl_t)  #divide by the mean flux, so avg(f) = 1

    # create an interpolation object to the synthetic spectrum
    interp = InterpolatedUnivariateSpline(wl_t, fl_t, k=2, ext=2)
    ######################

    ######################
    # READING IN THE DATA
    ######################
    # returns all of the echelle orders as a list
    raw_list = read_fits.read_fits_spectrum1d(args.rfits)
    blaze_list = read_fits.read_fits_spectrum1d(args.bfits)
    header = fits.getheader(args.rfits)
    target = header["OBJECT"]
    date = header["DATE"]

    n_orders = 51
    n_pix = 2304 - args.trim

    # Create empty dataset to store pseudo-fluxcaled spectra
    wls = np.empty((n_orders, n_pix))
    fls = np.empty((n_orders, n_pix))
    fls_cor = np.empty((n_orders, n_pix))
    fls_t = np.empty(
        (n_orders, n_pix))  # to store the interpolated model fluxes
    sigmas = np.empty((n_orders, n_pix))

    # loop through the array, put things into an array of wl, fls
    # trimming and flux-caling along the way
    for order in range(51):
        # select the astropy spectrum corresponding to this order
        # and truncate according to trim
        raw = raw_list[order]
        blaze = blaze_list[order]

        wl = raw.wavelength.value[args.trim:]
        fl_r = raw.flux.value[args.trim:]
        # Where the ratio values are 0, just set it to 1, since the noise will be 0 here too.
        fl_r[fl_r == 0] = 1.0
        fl_b = blaze.flux.value[args.trim:]
        fl_b[fl_b == 0] = 1.0

        ratio = fl_b / fl_r

        # calculate the uncertainty on this pixel as simply the sqrt of the number of counts,
        # but in units of the blaze function
        sigma = ratio * np.sqrt(
            fl_r + 0.01 * np.median(fl_r))  # to avoid very low vals

        wls[order] = wl
        fls[order] = fl_b
        sigmas[order] = sigma

        try:
            fls_t[order] = interp(wl)
        except ValueError:
            print(
                "Cannot interpolate synthetic spectrum for echelle order {:}. Assuming flat continuum for now, but you may want to discard the spectrum in this order."
                .format(d))
            fls_t[order] = np.ones_like(wl)

    ############################################
    # FLATTENING THE DATA TO MATCH THE TEMPLATE
    ###########################################

    # Norm the average level of all orders to match the level of the synthetic spectrum
    fl_median = np.median(fls, axis=1)
    flt_median = np.median(fls_t, axis=1)
    factors = flt_median / fl_median
    # update these to be normalized to the synthetic spectra
    fls = fls * factors[:, np.newaxis]
    sigmas = sigmas * factors[:, np.newaxis]

    for order in range(n_orders):
        # get the coefficients for the polynomial which makes it match the synthetic spectrum.
        fl_cor, X = solve_flux(wls[order],
                               fls[order],
                               sigmas[order],
                               fls_t[order],
                               order=args.poly_order)
        fls_cor[order, :] = fl_cor

    ######################
    # Smoothing the overlap
    ######################

    # start with an existing stub, and add onto it with each iteration
    wl_all = wls[0]
    fl_all = fls_cor[0]
    sigma_all = sigmas[0]

    for order in range(1, n_orders):

        # Find all wavelengths in the stub that have a value larger than the shortest wavelength in the red order
        l_ind = (wl_all > np.min(wls[order]))

        # Find all wavelengths in the red order that have a value smaller than the largest wavelength in the stub
        r_ind = (wls[order] < np.max(wl_all))

        # if either of these is less than 2, then there isn't enough overlap to actually merge and we should
        # just take add on the new order to the stub without doing any fancy smoothing
        if (np.sum(l_ind) < 2) or (np.sum(r_ind) < 2):

            wl_all = np.concatenate((wl_all, wls[order]))
            fl_all = np.concatenate((fl_all, fls_cor[order]))
            sigma_all = np.concatenate((sigma_all, sigmas[order]))

        else:

            wl_smooth = wl_all[l_ind]

            # Grab both wavelengths, fluxes, and sigmas in the overlap region
            wl_comb = np.concatenate([wl_smooth, wls[order][r_ind]])
            fl_comb = np.concatenate([fl_all[l_ind], fls_cor[order][r_ind]])
            sigma_comb = np.concatenate(
                [sigma_all[l_ind], sigmas[order][r_ind]])

            # sort according to wl
            indsort = np.argsort(wl_comb)
            wl_sorted = wl_comb[indsort]
            fl_sorted = fl_comb[indsort]
            sigma_sorted = sigma_comb[indsort]

            # construct a spline
            # spline = UnivariateSpline(wl_sorted, fl_sorted, w=1/sigma_sorted)

            # choose the left_wl as the wl to interpolate onto
            # fl_smooth = spline(wl_smooth)

            # transfer the sigmas, too, as the sqrt of sum of squares of the adjacent pixels
            # go through each wl in wl_all[lind], find it's arguement in wl_sorted, and average the two
            # nearest pixels in sigma_sorted
            n_comb = len(sigma_sorted)
            n_smooth = len(wl_smooth)

            fl_smooth = np.empty(n_smooth)
            sigma_smooth = np.empty(n_smooth)

            for i in range(n_smooth):
                w = wl_smooth[i]

                # find where this appears in the combined array
                arg = np.searchsorted(wl_sorted, w)

                # choose this is the first value as long as it isn't the rightmost value
                if arg < (n_smooth - 1):
                    arg_left = arg
                    arg_right = arg + 1
                else:
                    arg_left = arg - 1
                    arg_right = arg

                sigma_left = sigma_sorted[arg_left]
                sigma_right = sigma_sorted[arg_right]
                fl_smooth[i] = np.average(
                    (fl_sorted[arg_left], fl_sorted[arg_right]),
                    weights=np.array((1 / sigma_left**2, 1 / sigma_right**2)))
                sigma_smooth[i] = np.sqrt(sigma_left**2 + sigma_right**2)

            # append all of the pieces together to grow the merged spectrum until we're done adding orders
            wl_all = np.concatenate((wl_all, wls[order][~r_ind]))
            fl_all = np.concatenate(
                (fl_all[~l_ind], fl_smooth, fls_cor[order][~r_ind]))
            sigma_all = np.concatenate(
                (sigma_all[~l_ind], sigma_smooth, sigmas[order][~r_ind]))

    if args.plot:
        ######################
        # PLOTTING THE DATA
        ######################
        # make some plots to examine what the spectrum looks like at the overlap regions
        with PdfPages('output.pdf') as pdf:

            for order in range(n_orders):

                order_min = order - 1 if order > 0 else 0
                order_max = order + 1 if order < (n_orders - 2) else (
                    n_orders - 1)

                wl_left = wls[order_min]
                fl_left = fls[order_min]
                fl_t_left = fls_t[order_min]
                fl_cor_left = fls_cor[order_min]

                wl_center = wls[order]
                fl_center = fls[order]
                fl_t_center = fls_t[order]
                fl_cor_center = fls_cor[order]

                wl_right = wls[order_max]
                fl_right = fls[order_max]
                fl_t_right = fls_t[order_max]
                fl_cor_right = fls_cor[order_max]

                # As many times as you like, create a figure fig and save it:
                fig, ax = plt.subplots(nrows=3, figsize=(8.5, 12), sharex=True)

                ax[0].plot(wl_left, fl_left, color="0.6")
                ax[0].plot(wl_right, fl_right, color="0.6")
                ax[0].plot(wl_center, fl_center, color="k", label="data")
                ax[0].set_title("Order {:d}".format(order + 1))

                ax[0].plot(wl_center, fl_cor_center, "r", label="cor. data")
                ax[0].plot(wl_center, fl_t_center, "b", label="template")
                ax[0].legend(loc="best", fontsize="x-small")

                # The polynomial-corrected spectra, with the synthetic model on top

                ax[1].plot(wl_left, fl_cor_left, "r", label="cor. data edge")
                ax[1].plot(wl_right, fl_cor_right, "r")
                ax[1].plot(wl_center,
                           fl_cor_center,
                           "g",
                           alpha=0.7,
                           label="cor. data center")
                ax[1].legend(loc="best", fontsize="x-small")

                ax[1].plot(wl_left, fl_t_left, "b", label="template")
                ax[1].plot(wl_center, fl_t_center, "b")
                ax[1].plot(wl_right, fl_t_right, "b")

                # ax[1].plot(wl_t, fl_t, "k", lw=0.5)
                ax[1].set_title("Flattened orders")

                # The final merged spectrum
                ax[2].plot(wl_all, fl_all, "b", label="merged data")
                ax[2].legend(loc="best", fontsize="x-small")
                ax[2].set_xlim(np.min(wl_left), np.max(wl_right))
                ax[2].set_ylim(ax[1].get_ylim())
                ax[2].set_title("Merged spectrum")

                ax[0].set_ylabel(r"$f_\lambda\,[\propto$\,counts]")
                ax[1].set_ylabel(r"$f_\lambda\,[\propto$\,counts]")
                ax[2].set_ylabel(r"$f_\lambda\,[\propto$\,counts]")

                ax[2].set_xlabel(r"$\lambda$\,[\AA]")
                # save figure to large PDF file
                pdf.savefig(fig)

                plt.close('all')

            # We can also set the file's metadata via the PdfPages object:
            d = pdf.infodict()
            d['Title'] = "{:} - {:}".format(target, date)
            d['Author'] = "Ian Czekala"
            d['CreationDate'] = datetime.datetime(2009, 11, 13)
            d['ModDate'] = datetime.datetime.today()

    # sort all of the wl, fl, and sigmas just for good measure
    indsort = np.argsort(wl_all)
    wl_all = wl_all[indsort]
    fl_all = fl_all[indsort]
    sigma_all = sigma_all[indsort]

    # construct an astropy table with the merged files
    t = Table((wl_all, fl_all, sigma_all), names=["wl", "fl", "sigma"])

    for (key, value) in header.items():
        t.meta[key] = value

    # specify how much accuracy to use for each column
    formats = {"wl": "%.3f", "fl": "%.3e", "sigma": "%.3e"}

    # write the result to file
    ascii.write(t,
                args.outfile,
                overwrite=args.clobber,
                format="ecsv",
                formats=formats)
Пример #42
0
def test_multispec_chebyshev():
    iraf = ascii.read(data_path('AAO_11.txt'), data_start = 175, Reader = ascii.NoHeader, names = ['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('AAO.fits'))
    spec = spectra[10]
    np.testing.assert_allclose(iraf['wave'], spec.wavelength.value)
Пример #43
0
def test_multispec_chebyshev():
    iraf = ascii.read(data_path("AAO_11.txt"), data_start=175, Reader=ascii.NoHeader, names=["wave", "flux"])
    spectra = read_fits.read_fits_spectrum1d(data_path("AAO.fits"))
    spec = spectra[10]
    np.testing.assert_allclose(iraf["wave"], spec.wavelength.value)
Пример #44
0
from pylab import *
from specutils.io import read_fits
myspec = read_fits.read_fits_spectrum1d('../../specutils/io/tests/files/UVES.fits', dispersion_unit='angstrom')
plot(myspec.wavelength, myspec.flux)
show()
Пример #45
0
def convert_spectrum(fraw, fblaze, fout, BCV=False):
    '''
    param fraw: the raw counts
    param fblaze: the blaze-corrected spectrum
    param fout: the .json file to save to

    '''

    raw_list = read_fits.read_fits_spectrum1d(fraw)
    blaze_list = read_fits.read_fits_spectrum1d(fblaze)
    head = fits.getheader(fraw)
    try:
        BCV = head["BCV"]
    except KeyError:
        print("No BCV correction for", fraw)
        BCV = 0.0

    BCV_cor = np.sqrt((c_kms_air + BCV) / (c_kms_air - BCV))

    echelle_dict = {}
    npix = len(blaze_list[0].wavelength)

    # Do this for each order in the spectrum
    for i, (raw, blaze) in enumerate(zip(raw_list, blaze_list)):

        # Correct for the barycentric shift
        wl = blaze.wavelength.value

        # Scale the sigma values by the same blaze function that the raw fluxes were scaled by
        raw_flux = raw.flux.value
        raw_flux[raw_flux == 0] = 1.0
        # Where the ratio values are 0, just set it to 1, since the noise will be 0 here too.
        ratio = blaze.flux.value / raw_flux
        sigma = ratio * np.sqrt(raw.flux.value)

        if BCV:
            wl = wl * BCV_cor

        order_dict = {
            "wl": wl,
            "fl": blaze.flux.value,
            "sigma": sigma,
            "mask": np.ones((npix, ), dtype="bool")
        }

        echelle_dict["order_{}".format(i)] = order_dict

    UT = head["DATE-OBS"]
    echelle_dict["UT"] = UT
    try:
        echelle_dict["HJD"] = head["HJDN"]
    except KeyError:
        print("Spectrum does not have HJDN keyword", UT)
        print("Setting to 0.0")
        echelle_dict["HJD"] = 0.0

    try:
        echelle_dict["BCV"] = BCV
    except KeyError:
        print("Spectrum does not have BCV keyword", UT)
        print("Setting to 0.0")
        echelle_dict["BCV"] = 0.0

    ej.write(fout, echelle_dict)
Пример #46
0
def two_gaussians(x, h1, c1, w1, h2, c2, w2, offset):
    return gaussian(x, h1, c1, w1, offset=0) + gaussian(x, h2, c2, w2, offset=0) + offset


def three_gaussians(x, h1, c1, w1, h2, c2, w2, h3, c3, w3, offset):
    return (gaussian(x, h1, c1, w1, offset=0) + gaussian(x, h2, c2, w2, offset=0) +
            gaussian(x, h3, c3, w3, offset=0) + offset)

# ------------------------------------------------------------------------------------------------------------------- #


# ------------------------------------------------------------------------------------------------------------------- #
# Read Data From The Normalised Spectra
# ------------------------------------------------------------------------------------------------------------------- #
read_data = spec.read_fits_spectrum1d(DIR_SPEC + file_name)

with fits.open(str(file_name)) as hdulist:
    image_data = hdulist[0].data

wav_data = read_data.dispersion
flux_data = image_data
# ------------------------------------------------------------------------------------------------------------------- #


# ------------------------------------------------------------------------------------------------------------------- #
# Setup The Plot For Fitting The Spectral Feature
# ------------------------------------------------------------------------------------------------------------------- #
fig = plt.figure(figsize=(12, 9))
ax = fig.add_subplot(111)
Пример #47
0
    ax.xaxis.set_major_locator(major_locator)
    ax.set_xlim(min(x_array) - 50, max(x_array) + 50)
    ax.grid()

    return fig, ax


# -------------------------------------------------------------------------------------------------------------------- #

# -------------------------------------------------------------------------------------------------------------------- #
# Read 1-D Spectra
# -------------------------------------------------------------------------------------------------------------------- #

# file_name = 'cfwcbs_ASASSN14dq-gr7.ms.fits'
file_name = 'final_28aug.fits'
read_data = spec.read_fits_spectrum1d(str(file_name))
with fits.open(str(file_name)) as hdulist:
    image_data = hdulist[0].data
    image_hdr = hdulist[0].header

wav_data = read_data.dispersion
flux_data = image_data

# print type(image_data), image_data.shape
# print read_data[0].dispersion
# print len(read_data[0].dispersion), len(list(read_data[0]))
# image_hdr2 = fits.getheader('fwcbs_ASASSN14dq-gr7.ms.fits')
# print image_data.shape, len(list(read_data.dispersion))
# init_value = image_hdr['CRVAL1']
# step_size = image_hdr['CD1_1']
# wav_array, flux_array = psl.read1dFitsSpec(fn=file_name, hdu=0)
Пример #48
0
def test_1dspec_vrad():
    iraf = ascii.read(data_path('gbt_1d_iraf_read.dat'),
                      names=['wave', 'flux'])
    spec = read_fits.read_fits_spectrum1d(data_path('gbt_1d.fits'))
    np.testing.assert_allclose(iraf['wave'], spec.dispersion.value)
    assert spec.dispersion.unit == u.Unit('km/s')
Пример #49
0
from pylab import *
from specutils.io import read_fits
myspec = read_fits.read_fits_spectrum1d(
    '../../specutils/io/tests/files/UVES.fits', dispersion_unit='angstrom')
plot(myspec.wavelength, myspec.flux)
show()
Пример #50
0
allvaris = []

for sn in dic_meta.itervalues():
    if sn['idr.subset'] != 'auxiliary':
        # container for all spectra
        phases = []
        fluxes = []
        varis = []
        for spec in sn['spectra'].itervalues():
            # salt2.phase is in the supernova restframe
            if (spec['salt2.phase'] >= prange[0]
                    and spec['salt2.phase'] < prange[1]):
                filename = 'SNF-0203-CABALLO2/' + spec['idr.spec_restframe']

                #get wavelength solution
                myspec = read_fits.read_fits_spectrum1d(filename)

                if myspec.dispersion[0] < lmin or myspec.dispersion[1] > lmax:
                    sedge = (myspec.dispersion +
                             numpy.roll(myspec.dispersion, 1)) / 2
                    sedge = sedge[1:]

                    hdulist = fits.open(filename)

                    #containers for all bands in a spectrum
                    f_ = []
                    v_ = []
                    for fin in xrange(len(ledge) - 1):

                        insertloc = [
                            numpy.searchsorted(sedge, ledge[fin],
Пример #51
0
def spec_fits(filename, get_err=False, get_meta=False):
    from specutils.io import read_fits
    import astropy.units as u
    from astropy.io import fits
    from synphot import SourceSpectrum
    from synphot.models import Empirical1D

    #load fits file
    spec_fits = read_fits.read_fits_spectrum1d(filename)
    #read header info
    spec_meta = fits.getheader(filename)
    #check for multispec
    if isinstance(spec_fits, list):
        spec = spec_fits[1]
        if get_err:
            spec_err = spec_fits[3]
        print spec_meta['SITEID']
    else:
        spec = spec_fits
    #spectrum flux data
    flux = spec.data
    if spec_meta['BUNIT']=='erg/cm2/s/A':
        flux = flux * u.erg/u.cm**2/u.s/u.AA
        if get_err:
            flux_err = spec_err.data
            flux_err = flux_err * u.erg/u.cm**2/u.s/u.AA
    wave = spec.dispersion
    if isinstance(wave, u.Quantity):
        wave = wave.value * u.AA
    #create synphot spectrum object
    spec = SourceSpectrum(Empirical1D, points=wave,
                          lookup_table=flux, keep_neg=True)
    if get_err:
        spec_err = SourceSpectrum(Empirical1D, points=wave,
                          lookup_table=flux_err, keep_neg=True)
    #check if metadata is needed
    if get_meta:
        from astropy.coordinates import SkyCoord
        
        #calculate time observed
        datestr = 'DATE-OBS'
        if 'T' in spec_meta[datestr]:
            isot_obs = spec_meta[datestr]
        elif 'UT' in spec_meta:
            isot_obs = spec_meta[datestr]+'T'+spec_meta['UT']
        else:
            isot_obs = spec_meta[datestr]+'T'+spec_meta['UT-TIME']
        #exposure time
        exp_obs = spec_meta['EXPTIME']
        #RA, DEC observed
        RA, DEC = spec_meta['RA'], spec_meta['DEC']
        coord = SkyCoord(RA, DEC, unit=(u.hourangle, u.deg))
        RA, DEC = coord.ra.degree, coord.dec.degree
        #return spectrum and metadata
        #return spectrum
        if get_err:
            return spec, spec_err, isot_obs, exp_obs, (RA,DEC)
        else:
            return spec, isot_obs, exp_obs, (RA,DEC)
    else:
        #return spectrum
        if get_err:
            return spec, spec_err
        else:
            return spec
Пример #52
0
def test_multispec_legendre():
    iraf = ascii.read(data_path('TRES.dat'), data_start = 127, Reader = ascii.NoHeader, names = ['wave', 'flux'])
    spectra = read_fits.read_fits_spectrum1d(data_path('TRES.fits'))
    spec = spectra[10]
    np.testing.assert_allclose(iraf['wave'], spec.dispersion.value)
    assert spec.dispersion.unit == u.Angstrom
Пример #53
0
synbands = []

for name, lams in zip(synname, synlam):
    synbands.append(sncosmo.Bandpass(lams, [1., 1.], name='tophat' + name))

names168 = [line.split()[0] for line in open('table.txt')]
dic_meta = cPickle.load(open("CABALLOv2/META.pkl"))
ans = []
for sn in names168:
    meta = dic_meta[sn]
    for nm in meta['spectra']:
        spec = meta['spectra'][nm]
        if abs(spec['salt2.phase']) < 2.5:
            name = "CABALLOv2/" + spec['idr.spec_merged']
            spectra = read_fits.read_fits_spectrum1d(
                name, dispersion_unit='angstrom')


            model0 = sncosmo.TimeSeriesSource(numpy.array([0.,1,2]), spectra.dispersion.value/(1+meta['salt2.Redshift']), \
                numpy.tile(spectra.flux.value,(3,1)))
            dust = sncosmo.F99Dust(r_v=2.5)
            dust.set(ebv=0.01)
            model = sncosmo.Model(source=model0,
                                  effects=[dust],
                                  effect_names=['host'],
                                  effect_frames=['rest'])
            try:

                A = -2.5 * numpy.log10(
                    model.bandflux(synbands, 0.) /
                    model0.bandflux(synbands, 0.))
Пример #54
0
def test_multispec_log_linear():
    iraf = ascii.read(data_path("log-linear.dat"), Reader=ascii.NoHeader, names=["wave", "flux"])
    spectra = read_fits.read_fits_spectrum1d(data_path("multispec-linear-log.fits"))
    spec = spectra[1]
    np.testing.assert_allclose(iraf["wave"], spec.dispersion.value)
    assert spec.dispersion.unit == u.Angstrom
Пример #55
0
 def from_fits(cls, path):
     spectrum_list = read_fits.read_fits_spectrum1d(path)
     header = fits.getheader(path)
     return cls(spectrum_list, header=header)