Beispiel #1
0
def lya_splices(stars='all'):
    if stars == 'all':
        stars = rc.stars[:11]
        stars.remove('gj551')
    dw = 0.05
    for star in stars:
        pan = io.readpan(star)
        pan = utils.keepranges(pan, 1100, 1300)
        pan = utils.evenbin(pan, dw)

        sf = db.findfiles('u', star, 'coadd', 'cos_g130m')
        spec, = io.read(sf)
        spec = utils.evenbin(spec, dw)

        lf = db.findfiles('u', star, 'mod', 'lya')
        lya, = io.read(lf)

        plt.figure()
        [ss(s, err=False) for s in [pan, spec, lya]]
        plt.xlim(1210, 1222)
        up, _ = utils.flux_integral(spec, 1217, 1220)
        plt.ylim(0, up * 4)
        plt.legend(['pan', 'cos', 'lya'], loc='best')
        plt.savefig(
            os.path.join(rc.scratchpath, 'lya splices',
                         '{} linear.pdf'.format(star)))

        mx = spec['flux'].max()
        plt.ylim(mx / 1e7, mx)
        plt.yscale('log')
        plt.savefig(
            os.path.join(rc.scratchpath, 'lya splices',
                         '{} log.pdf'.format(star)))
Beispiel #2
0
def phxFit(star='gj832', ax=None):
    if ax is None: ax = plt.gca()
    ax.autoscale(axis='x', tight=True)
    ax.set_yscale('log')

    pan = io.readpan(star)
    bolo = utils.bolo_integral(star)
    xf = db.findfiles('ir', 'phx', star, fullpaths=True)
    phx = io.read(xf)[0]
    phx['flux'] *= pan['normfac'][-1]
    pan = pan[pan['instrument'] != pan['instrument'][-1]]
    fmin = np.min(pan['error'][pan['error'] > 0]) / bolo
    rng = [phx['w0'][0], 6000.]
    phx, pan = [
        utils.keepranges(spec, rng, ends='loose') for spec in [phx, pan]
    ]
    phx, pan = [utils.fancyBin(s, mindw=10.0) for s in [phx, pan]]
    pan['flux'][pan['flux'] < pan['error'] / 2.0] = np.nan
    pan['normflux'] = pan['flux'] / bolo
    phx['normflux'] = phx['flux'] / bolo
    ymax = 10**np.ceil(np.log10(np.max(phx['normflux'])))
    ymin = 10**np.floor(np.log10(fmin) - 3)
    ax.set_ylim(ymin, ymax)

    pline = plot.specstep(pan, ax=ax, color='k', err=False, key='normflux')
    xline = plot.specstep(phx, ax=ax, color='gray', err=False, key='normflux')
    ax.legend((pline, xline), ('$HST$ Data', 'PHOENIX Model'),
              loc='lower right')
Beispiel #3
0
def phxCompare(star, wlim=None, maxpow=None, mindw=None, ax=None):
    if ax is None: ax = plt.gca()

    xf = db.findfiles('ir', 'phx', star, fullpaths=True)
    pf = db.panpath(star)

    phx = io.read(xf)[0]
    pan = io.read(pf)[0]
    normfac = pan['normfac'][-1]
    if wlim is not None:
        phx, pan = utils.keepranges(phx, wlim), utils.keepranges(pan, wlim)

    if maxpow or mindw:
        pan = utils.fancyBin(pan, maxpow=maxpow, mindw=mindw)
        phx = utils.fancyBin(phx, maxpow=maxpow, mindw=mindw)

    Fbol = utils.bolo_integral(star)
    pan['normflux'] = pan['flux'] / Fbol
    pan['normerr'] = pan['error'] / Fbol
    phx['normflux'] = phx['flux'] * normfac / Fbol

    line = specstep(pan, key='normflux', label='Panspec', ax=ax)
    specstep(pan,
             key='normerr',
             label='Panspec Error',
             ax=ax,
             color=line.get_color(),
             ls=':')
    specstep(phx, key='normflux', label='Phoenix', ax=ax, color='r', alpha=0.5)
    ax.set_xlim(wlim)
    ax.legend(loc='best')
Beispiel #4
0
def blackbody_fit(star):
    """Return a function that is a blackbody fit to the phoenix spectrum for the star. The fit is to the unnormalized
    phoenix spectrum, so the fit function values must be multiplied by the appropriate normalization factor to match
    the normalized spectrum."""

    phx = io.read(db.findfiles('ir', 'phx', star))[0]

    # recursively identify relative maxima until there are fewer than N points
    N = 10000
    keep = np.arange(len(phx))
    while len(keep) > N:
        temp, = argrelmax(phx['flux'][keep])
        keep = keep[temp]

    Teff = rc.starprops['Teff_muscles'][star]
    efac = const.h * const.c / const.k_B / (Teff * u.K)
    efac = efac.to(u.angstrom).value
    w = (phx['w0'] + phx['w1']) / 2.0
    w = w[keep]
    planck_shape = 1.0 / w**5 / (np.exp(efac / w) - 1)
    y = phx['flux'][keep]

    Sfy = np.sum(planck_shape * y)
    Sff = np.sum(planck_shape**2)

    norm = Sfy / Sff

    return lambda w: norm / w**5 / (np.exp(efac / w) - 1)
Beispiel #5
0
 def plotspec(version, ax):
     f, = db.findfiles(rc.hlsppath, star, 'broadband', version, res)
     spec, = io.read(f)
     spec = utils.keepranges(spec, 0, maxw)
     spec = utils.evenbin(spec, binto)
     plt.axes(ax)
     piecespec(spec)
     plt.text(0.05, 0.9, version, transform=ax.transAxes)
Beispiel #6
0
def phx_compare_single(star):
    pan = io.read(db.panpath(star))[0]
    xf = db.findfiles('ir', 'phx', star, fullpaths=True)
    phx = io.read(xf)[0]
    phx['flux'] *= pan['normfac'][-1]

    bands = [rc.fuv, rc.nuv, [rc.vis[0], 5700.]]
    (pff, pfe) , (pnf, pne), (pvf, pve) = [utils.flux_integral(pan, *b) for b in bands]
    (xff, _) , (xnf, _), (xvf, _) = [utils.flux_integral(phx, *b) for b in bands]

    return ((pff - xff)/pff, pfe/pff), ((pnf - xnf)/pnf, pne/pnf), ((pvf - xvf)/pvf, pve/pvf)
Beispiel #7
0
def compareEUV(star):
    euvfile = db.findfiles('u', 'euv', star, fullpaths=True)[0]
    w, f, _ = np.loadtxt(euvfile).T
    I0 = np.trapz(f, w)
    print 'Trapz from Allison\'s file: %g' % I0
    spec = io.read(euvfile)[0]
    I1 = np.sum((spec['w1'] - spec['w0']) * spec['flux'])
    print 'Direct integration from spectbl version of Allison\'s file: %g' % I1
    I2 = np.trapz(spec['flux'], (spec['w0'] + spec['w1']) / 2.0)
    print 'Trapz from spectbl version of Allison\'s file: %g' % I2
    p = io.read(db.panpath(star))[0]
    keep = p['instrument'] == rc.getinsti('mod_euv_young')
    p = p[keep]
    I3 = np.sum((spec['w1'] - spec['w0']) * spec['flux'])
    print 'Direct integration from panspec EUV portion: %g' % I3
Beispiel #8
0
def readFlareTbl(star, inst, label):
    tblfile = db.findfiles(rc.flaredir, star, inst, label, 'flares', fullpaths=True)
    assert len(tblfile) == 1
    tbl = table.Table.read(tblfile[0])

    w0, w1 = [], []
    i = 0
    while True:
        istr = str(i)
        if 'BANDBEG' + str(i) not in tbl.meta:
            break
        w0.append(tbl.meta['BANDBEG' + istr])
        w1.append(tbl.meta['BANDEND' + istr])
        i += 1
    bands = np.array(zip(w0, w1))

    return table.Table.read(tblfile[0]), bands
Beispiel #9
0
def lya_galex_ratio(star):
    import numpy as np
    nuv = np.loadtxt(
        '/Users/rolo7566/Datasets/shared/filter response curves/galexNUV.txt',
        skiprows=1).T
    fuv = np.loadtxt(
        '/Users/rolo7566/Datasets/shared/filter response curves/galexFUV.txt',
        skiprows=1).T

    pan = io.readpan(star)
    lya, = io.read(db.findfiles('u', star, 'mod_lya'))
    lya = utils.flux_integral(lya)[0]

    w = (pan['w0'] + pan['w1']) / 2.0
    f = pan['flux']
    twf = lambda bp: np.trapz(np.interp(bp[0], w, f) * bp[1], bp[0])
    NUV, FUV = map(twf, [nuv, fuv])
    print star
    print 'Lya/NUV ratio = {:.2g}'.format(lya / NUV)
    print 'Lya/FUV ratio = {:.2g}'.format(lya / FUV)
Beispiel #10
0
def lightcurveCompendium(stars='hosts',
                         figure=None,
                         flarecut=2.0,
                         flarelabel='SiIV',
                         dt=30.0,
                         colorful=False):
    """
    Create a compendium of lightcurves for the specified stars highlighting flares.
    """
    fig = plt.gcf() if figure is None else figure
    if colorful:
        colors = itertools.cycle(['b', 'g', 'r'])
    else:
        colors = itertools.cycle(['k'])
    inst = 'hst_cos_g130m'
    if stars == 'hosts':
        stars = filter(
            lambda s: len(db.findfiles('u', inst, 'corrtag_a', s)) >= 4,
            rc.observed)

    ## SET UP AXES
    ## -----------
    # setup axes with just bottom axis showing
    fig.set_facecolor('w')
    fig.add_axes((0.23, 0.14, 0.72, 0.84), frameon=False)
    ax = fig.axes[0]
    xax = ax.get_xaxis()
    xax.tick_bottom()
    yax = ax.get_yaxis()
    yax.set_visible(False)
    ax.set_clip_on(False)

    fntsz = mpl.rcParams['font.size']
    spacedata = (fntsz / 72.0 / fig.get_figwidth()) / 0.8 * 14000

    # common keywords to use in errorbar plot
    ekwds = dict(fmt='.', capsize=0.0)
    alphaNF = 0.1 if colorful else 0.4

    ## MAKE LIGHTCURVES
    ## ----------------
    bands = rc.flare_bands[inst]
    offset, offsets = 0.0, []
    for star, color in zip(stars, colors):
        # get flare info
        flares, bands = io.readFlareTbl(star, inst, flarelabel)

        curve = reduce.auto_curve(star,
                                  inst,
                                  dt=30.0,
                                  bands=bands,
                                  groups=[range(len(bands))])
        t0, t1, cps, err = zip(*curve)[0]

        t = (t0 + t1) / 2.0

        # get rid of gaps
        s, j, _ = mnp.shorten_jumps(t, maxjump=10 * dt, newjump=30 * dt)

        # identify flare pts
        flares = flares[flares['PEWratio'] > flarecut]
        flare_ranges = np.array([flares['start'], flares['stop']]).T
        flarepts = mnp.inranges(t, flare_ranges)

        # normalize the data to median
        med = np.median(cps[~flarepts])
        y = cps / med
        y -= 1.0
        e = err / med

        # normalize data to max - min
        ymax = np.max(y)
        y /= ymax
        e /= ymax

        # cull negative outliers
        good = y > -ymax
        s, y, e, flarepts = [a[good] for a in [s, y, e, flarepts]]

        # offset data in y
        offset = offset - np.min(y - e)
        offsets.append(offset)
        yo = y.copy() + offset

        # plot data
        ax.errorbar(s[~flarepts],
                    yo[~flarepts],
                    e[~flarepts],
                    alpha=alphaNF,
                    color=color,
                    **ekwds)
        ax.axhline(offset, linestyle='--', color='k', alpha=0.5)
        flarecolor = 'r' if color == 'k' else color
        ax.errorbar(s[flarepts],
                    yo[flarepts],
                    e[flarepts],
                    color=flarecolor,
                    **ekwds)

        # make arrow
        xy = (0.0, offset)
        xytext = (0.0, offset + 1.1)
        arrowprops = dict(arrowstyle='<-', color=color)
        ax.annotate('', xy=xy, xytext=xytext, arrowprops=arrowprops)

        # label arrow
        ymaxstr = '{:4.1f}'.format(ymax + 1.0)
        ax.text(-0.5 * spacedata,
                offset,
                '1.0',
                va='bottom',
                ha='right',
                color=color,
                fontsize=fntsz * 0.8)
        ax.text(-0.5 * spacedata,
                offset + 1.1,
                ymaxstr,
                va='top',
                ha='right',
                color=color,
                fontsize=fntsz * 0.8)

        # label star
        starlbl = starprops['name tex'][star]
        ax.text(-3.0 * spacedata,
                offset + 0.5,
                starlbl,
                va='center',
                ha='right',
                color=color)

        # increase offset
        offset += np.max(y + e)
        offset += 0.3

    ax.autoscale(axis='both', tight=True)

    # add x axis line and label
    ax.axhline(0, color='k')
    ax.set_xlabel('Time, Observation Gaps Shortened (s)')
Beispiel #11
0
def lyavsdata(star):
    data, = io.read(db.lyafile(star))
    mod, = io.read(db.findfiles('u', 'lya', star, fullpaths=True)[0])
    specstep(data)
    specstep(mod)
    plt.xlim(1208, 1222)
Beispiel #12
0
def writehlsp(star_or_spectbl, components=True, overwrite=False):
    """
    Writes spectbl to a standardized MUSCLES FITS file format that also
    includes all the keywords required for the archive.

    Parameters
    ----------
    spectbl : astropy table in MUSCLES format
        The spectrum to be written to a MSUCLES FITS file.
    name : str
        filename for the FITS output
    overwrite : {True|False}
        whether to overwrite if output file already exists

    Returns
    -------
    None
    """

    if type(star_or_spectbl) is str:
        star = star_or_spectbl
        pfs = db.allpans(star)
        pan = read(filter(lambda s: 'native_resolution' in s, pfs))[0]
        writehlsp(pan, components=components, overwrite=overwrite)
        dpan = read(filter(lambda s: 'dR=' in s, pfs))[0]
        writehlsp(dpan, components=False)
        return
    else:
        spectbl = star_or_spectbl

    star = spectbl.meta['STAR']
    srcspecs = spectbl.meta['SOURCESPECS']
    name = spectbl.meta['NAME']
    pan = 'panspec' in name
    mod = 'mod' in name

    hlspname = db.hlsppath(name)

    # add a wavelength midpoint column
    w = (spectbl['w0'] + spectbl['w1']) / 2.0
    spectbl['w'] = w
    spectbl['w'].description = 'midpoint of the wavelength bin'
    spectbl['w'].unit = 'Angstrom'

    # CREATE PRIMARY EXTENSION
    prihdr = fits.Header()
    if pan:
        prihdr['TELESCOP'] = 'MULTI'
        prihdr['INSTRUME'] = 'MULTI'
        prihdr['GRATING'] = 'MULTI'

        insts = []
        for specname in srcspecs:
            tel = db.parse_observatory(specname)
            spec = db.parse_spectrograph(specname)
            grating = db.parse_info(specname, 3, 4)
            insts.append([rc.HLSPtelescopes[tel], rc.HLSPinstruments[spec], rc.HLSPgratings[grating]])
        insts = set(tuple(inst) for inst in insts)

        for i,inst in enumerate(insts):
            tkey, ikey, gkey = 'TELESC{:02d}'.format(i), 'INSTRU{:02d}'.format(i), 'GRATIN{:02d}'.format(i)
            prihdr[tkey], prihdr[ikey], prihdr[gkey] = inst
    else:
        prihdr['TELESCOP'] = rc.HLSPtelescopes[db.parse_observatory(name)]
        prihdr['INSTRUME'] = rc.HLSPinstruments[db.parse_spectrograph(name)]
        prihdr['GRATING'] = rc.HLSPgratings[db.parse_grating(name)]


        if 'hst' in name:
            # clooge. meh.
            band = name[0]
            f = db.findfiles(band, name, fullpaths=True)[0]
            aper_key = 'APER_ID' if 'fos' in name else 'APERTURE'
            if 'custom' in name or 'coadd' in name:
                srcspecs = fits.getdata(f, 'sourcespecs')
                srcids = [db.parse_id(s) for s in srcspecs['sourcespecs']]
                srcpaths = [db.sourcespecfiles(star, id)[0] for id in srcids]
                apertures = [fits.getval(sf, aper_key) for sf in srcpaths]
                assert len(set(apertures)) == 1
                prihdr['APERTURE'] = apertures[0]
            else:
                prihdr['APERTURE'] = fits.getval(f, aper_key)
        if 'xmm' in name or 'cxo' in name:
            hdr = fits.getheader(db.name2path(name))
            prihdr['GRATING'] = 'NA'
            if 'multi' in name:
                prihdr['DETECTOR'] = 'MULTI'
                prihdr['DETECT00'] = 'PN'
                prihdr['DETECT01'] = 'MOS1'
                prihdr['DETECT02'] = 'MOS2'
                prihdr['FILTER'] = 'MULTI'
                prihdr['FILTER00'] = hdr['pn_filter']
                prihdr['FILTER01'] = hdr['mos1_filter']
                prihdr['FILTER02'] = hdr['mos2_filter']
            if 'pn' in name:
                prihdr['DETECTOR'] = 'PN'
                prihdr['FILTER'] = hdr['pn_filter']
            if 'acs' in name:
                prihdr['DETECTOR'] = hdr['DETNAM']
                prihdr['FILTER'] = 'OBF'

    prihdr['TARGNAME'] = star.upper()
    prihdr['RA_TARG'] = rc.starprops['RA'][star]
    prihdr['DEC_TARG'] = rc.starprops['dec'][star]
    prihdr['PROPOSID'] = 13650
    prihdr['HLSPNAME'] = 'Measurements of the Ultraviolet Spectral Characteristics of Low-mass Exoplanet Host Stars'
    prihdr['HLSPACRN'] = 'MUSCLES'
    prihdr['HLSPLEAD'] = 'R. O. Parke Loyd'
    prihdr['PR_INV_L'] = 'France'
    prihdr['PR_INV_F'] = 'Kevin'

    if not (pan or mod):
        mjd0 = np.min(spectbl['minobsdate'])
        mjd1 = np.max(spectbl['maxobsdate'])
        if np.isfinite(mjd0):
            date0 = Time(mjd0, format='mjd')
            prihdr['DATE-OBS'] = date0.isot
            prihdr['EXPSTART'] = date0.mjd
        if np.isfinite(mjd1):
            date1 = Time(mjd1, format='mjd')
            prihdr['EXPEND'] =  date1.mjd
        expt = spectbl['exptime']
        if 'xmm' in name:
            prihdr['EXPTIME'] = expt[0]
            prihdr['EXPDEFN'] = 'MEAN'
        if 'cxo' in name:
            prihdr['EXPTIME'] = expt[0]
        if not np.allclose(expt, expt[0]):
            expmed = np.median(expt)
            prihdr['EXPTIME'] = expmed
            prihdr['EXPDEFN'] = 'MEDIAN'
            prihdr['EXPMAX'] = np.max(expt)
            prihdr['EXPMIN'] = np.min(expt[expt > 0])
            prihdr['EXPMED'] = expmed
        else:
            prihdr['EXPTIME'] = expt[0]

    if not (pan or mod) or 'phx' in name:
        try:
            inst = db.parse_instrument(name)
            normfac = rc.normfacs[star][inst][0]
            panspec = readpan(star)
            insti = rc.getinsti(inst)
            assert insti == spectbl['instrument'][0]
            normfac_vec = panspec['normfac'][panspec['normfac'] == insti]
            if len(normfac_vec) > 0:
                assert np.isclose(normfac_vec, normfac)
        except KeyError:
            normfac = 1.0
        prihdr['normfac'] = (normfac, 'normalization factor used by MUSCLES')


    prihdr['WAVEMIN'] = w[0]
    prihdr['WAVEMAX'] = w[-1]
    prihdr['WAVEUNIT'] = 'ang'
    prihdr['AIRORVAC'] = 'vac'

    if not pan or 'constant' in name:
        mid = len(w) / 2
        prihdr['SPECRES'] = w[mid]
        prihdr['WAVERES'] = w[mid+1] - w[mid]

    prihdr['FLUXMIN'] = np.min(spectbl['flux'])
    prihdr['FLUXMAX'] = np.max(spectbl['flux'])
    prihdr['FLUXUNIT'] = 'erg/s/cm2/ang' if 'phx' not in name else 'arbitrary'

    # CREATE SPECTRUM EXTENSION
    spechdr = fits.Header()
    spechdr['EXTNAME'] = 'SPECTRUM'
    spechdr['EXTNO'] = 2

    cols = ['w', 'w0', 'w1', 'flux']
    descriptions = ['midpoint of the wavelength bin',
                    'left/blue edge of the wavelength bin',
                    'right/red edge of the wavelength bin',
                    'average flux over the bin']
    fitsnames = ['WAVELENGTH', 'WAVELENGTH0', 'WAVELENGTH1', 'FLUX']
    fmts = ['D']*4

    if 'mod' not in name:
        cols.extend(['error', 'exptime', 'flags', 'minobsdate', 'maxobsdate'])
        descriptions.extend(['error on the flux',
                             'cumulative exposure time for the bin',
                             'data quality flags (HST data only)',
                             'modified julian date of start of first exposure',
                             'modified julian date of end of last exposure'])
        fitsnames.extend(['ERROR', 'EXPTIME', 'DQ', 'EXPSTART', 'EXPEND'])
        fmts.extend(['D']*2 + ['I'] + ['D']*2)

    if pan:
        # add a normalized flux column
        spectbl = utils.add_normflux(spectbl)
        spectbl['normflux'].unit = 'Angstrom-1'
        spectbl['normerr'].unit = 'Angstrom-1'
        prihdr['BOLOFLUX'] = utils.bolo_integral(spectbl.meta['STAR'])

        # add header keywords for lorentzian fit
        prihdr['LNZ_NORM'] = spectbl.meta['LNZ_NORM']
        prihdr['LNZ_GAM'] = spectbl.meta['LNZ_GAM']

        cols.extend(['instrument', 'normfac', 'normflux', 'normerr'])
        descriptions.extend(['bitmask identifying the source instrument(s). See "instlgnd" extension for a legend.',
                             'normalization factor applied to the source spectrum',
                             'flux density normalized by the bolometric flux',
                             'error on bolometrically-normalized flux density'])
        fitsnames.extend(['INSTRUMENT', 'NORMFAC', 'BOLOFLUX', 'BOLOERR'])
        fmts.extend(['J', 'D', 'D', 'D'])

    for i, desc in enumerate(descriptions):
        spechdr['TDESC' + str(i+1)] = desc

    if 'COMMENT' in spectbl.meta and len(spectbl.meta['COMMENT']) > 1 and not pan:
        spechdr['COMMENT'] = spectbl.meta['COMMENT']

    datas = [spectbl[col].data for col in cols]
    units = [spectbl[col].unit.to_string() for col in cols]
    fitscols = [fits.Column(array=a, name=n, format=fmt, unit=u) for a, n, fmt, u in zip(datas, fitsnames, fmts, units)]
    spechdu = fits.BinTableHDU.from_columns(fitscols, header=spechdr)
    spechdu.name = 'SPECTRUM'
    for fname, data in zip(fitsnames, datas):
        spechdu.data[fname] = data

    prihdu = fits.PrimaryHDU(header=prihdr)
    hdus = [prihdu, spechdu]

    # INSTRUMENT LEGEND
    if pan:
        lgndhdr = fits.Header()
        lgndhdr['comment'] = legendcomment
        lgndhdr['extno'] = 3
        lgndhdr['comment'] = ('Not all of these instruments were used to acquire data for this particular spectrum. '
                              'Therefore, not all the listed HLSP files will exist in the database. Also note that '
                              'polynomial fits for filling spectral gaps were not saved as separate spectra.')

        vals = rc.instvals
        instnames = rc.instruments
        pieces = [s.split('_') for s in instnames]
        tels, insts, gratings = zip(*pieces)
        tels = [rc.HLSPtelescopes[t] for t in tels]
        insts = [rc.HLSPinstruments[inst] for inst in insts]
        gratings = [rc.HLSPgratings[g] for g in gratings]
        dummynames = ['-_' + s + '_' + star for s in instnames]
        hlspnames = [path.basename(db.hlsppath(n)) for n in dummynames]

        names = ['BITVALUE', 'TELESCOPE', 'INSTRUMENT', 'GRATING', 'HLSP_FILE']
        datas = [vals, tels, insts, gratings, hlspnames]
        lens = [max(map(len, d)) for d in datas[1:]]
        fmts = ['J'] + [str(n) + 'A' for n in lens]
        fitscols = [fits.Column(n, fmt, array=a) for n, fmt, a in zip(names, fmts, datas)]

        lgndhdu = fits.BinTableHDU.from_columns(fitscols, header=lgndhdr)
        lgndhdu.name = 'INSTLGND'

        hdus.append(lgndhdu)

        if components:
            specs, lyaspec = read_panspec_sources(star)
            if lyaspec is not None: specs.append(lyaspec)
            for inst in instnames:
                spec = filter(lambda s: inst in s.meta['NAME'], specs)
                if len(spec) == 0:
                    continue
                assert len(spec) == 1
                spec = spec[0]
                writehlsp(spec, overwrite=overwrite)

    # SOURCE SPECTRA LIST
    if 'hst' in name:
        srchdr = fits.Header()
        srchdr['COMMENT'] = ('This extension contains a list of HST rootnames (9 character string in HST files '
                             'downloaded from MAST) and dataset IDs of the exposures used to create this spectrum '
                             'file. The dataset IDs can be used to directly locate the observations through the MAST '
                             'HST data archive search interface. Multiple identifiers indicate the spectra were '
                             'coadded.')
        srchdr['EXTNO'] = 3
        specnames = spectbl.meta['SOURCESPECS']
        if len(specnames) == 0: specnames = [name]
        rootnames = [s.split('_')[5] for s in specnames]
        files = [db.choosesourcespecs(db.findfiles(band, star, rn))[0] for rn in rootnames]
        id_key = 'ROOTNAME' if 'fos' in name else 'ASN_ID'
        dataids = [fits.getval(f, id_key) for f in files]
        custom = [('custom' in s) or ('x2d' in s) for s in specnames]
        assert all(custom) or (not any(custom))
        srchdr['CUSTOM'] = custom[0], 'spectrum extracted from x2d (bad x1d)'

        if 'gj551' in name:
            if 'g230lb' in name:
                rootnames = dataids = ['OCR7QQANQ', 'OCR7QQANQ']
            if 'g430l' in name:
                rootnames = dataids = ['OCR7QQAOQ', 'OCR7QQAPQ']
            if 'g750l' in name:
                rootnames = dataids = ['OCR7QQARQ', 'OCR7QQASQ', 'OCR7QQAQQ']

        fitscols = [fits.Column(name='ROOTNAME', format='9A', array=rootnames),
                    fits.Column(name='DATASET_ID', format='9A', array=dataids)]
        srchdu = fits.BinTableHDU.from_columns(fitscols, header=srchdr)
        srchdu.name = 'SRCSPECS'

        hdus.append(srchdu)
    if 'xmm' in name or 'cxo' in name:
        srchdr = fits.Header()
        if 'xmm' in name:
            srchdr['COMMENT'] = ('This extension contains a list of observation IDs (DATASET_ID used for consistency '
                                 'with HST data) that can be used to locate the data in the XMM archives. XMM data '
                                 'all come from only a single observation (unlike the HST observations), '
                                 'but this extension is retained in place of a keyword for consistency with the HST '
                                 'files.')
        if 'cxo' in name:
            srchdr['COMMENT'] = ('This extension contains a list of observation IDs (DATASET_ID used for consistency '
                                 'with HST data) that can be used to locate the data in the CXO archives.')
        srchdr['EXTNO'] = 3
        obsids = _parse_keys_sequential(hdr, 'OBS_ID')
        col = fits.Column(name='DATASET_ID', format='10A', array=obsids)
        srchdu = fits.BinTableHDU.from_columns([col], header=srchdr)
        srchdu.name = 'SRCSPECS'
        hdus.append(srchdu)

    hdus = fits.HDUList(hdus)
    hdus.writeto(hlspname, clobber=overwrite)
Beispiel #13
0
def readphotons(star, inst):
    pf = db.findfiles('photons', star, inst, fullpaths=True)
    assert len(pf) == 1
    ph = fits.open(pf[0])
    return ph, ph['events'].data
Beispiel #14
0
def getcurves():
    """
    Use this just to save time by not recreating curves each time you
    reload the function.
    """
    curves = []
    for star in stars:
        tagfiles = db.findfiles('u',
                                'corrtag_a',
                                'cos_g130m',
                                star,
                                fullpaths=True)
        x1dfiles = db.findfiles('u', 'x1d', 'cos_g130m', star, fullpaths=True)

        curve = cv.autocurve(tagfiles,
                             x1dfiles,
                             dt=dt,
                             bands=bands,
                             groups=groups)
        curves.append(curve[0])

    s, e, y, maxnorm, offsets, flarepts = [], [], [], [], [], []
    offset = 0.0
    for curve, flarerng in zip(curves, flarerngs):
        t = (curve['t0'] + curve['t1']) / 2.0
        t = t.data[1:]

        # get rid of gaps
        ss, j, _ = mnp.shorten_jumps(t, maxjump=10 * dt)
        s.append(ss)

        # normalize the data to median
        f = curve['cps'].data[1:]
        fmed = np.median(f)
        yy = f / fmed
        yy -= 1.0
        ee = curve['cps err'].data[1:] / fmed

        # normalize data to max - min
        ymax = np.max(yy)
        yy /= ymax
        ee /= ymax

        # identify flare pts
        i = np.arange(len(yy))
        ff = mnp.inranges(i, flarerng)

        # cull negative outliers
        good = yy / ee > -2.0
        ss, yy, ee, ff = [a[good] for a in [ss, yy, ee, ff]]

        y.append(yy)
        e.append(ee)
        flarepts.append(ff)
        maxnorm.append(ymax)

        offset = offset - np.min(yy - ee)
        offsets.append(offset)
        offset += np.max(yy + ee)
        offset += 0.2

    stuff = zip(s, y, e, flarepts, maxnorm, offsets)
    return stuff