def multiBandCurves(star='gj832', inst='cos_g130m', trange=[24400, 24900], n=100, tref=24625.0, bandnames=['NV', 'SiIV', 'SiIII', 'CII', 'cont1380'], ax=None, norms=None, fluxed=False, maxpts=500): if ax is None: ax = plt.gca() ph, p = io.readphotons(star, inst) p['time'] -= tref trange = np.array(trange) - tref ax.set_xlabel('Time [s]') ax.set_xlim(trange) if fluxed: wtkey = 'epera' ax.set_ylabel(linefluxlabel) else: wtkey = 'epsilon' ax.set_ylabel('Count Rate [s$^{-1}$]') if norms is not None: ax.set_ylabel('Normalized Flux') lines, labels = [], [] for i, name in enumerate(bandnames): bands = rc.stdbands.loc[name, 'bands'] label = rc.stdbands.loc[name, 'name txt'] Tform = rc.stdbands.loc[name, 'Tform'] # wavelbl = rc.stdbands.loc[name, 'wave lbl'] # label += ' ' + wavelbl if not np.isnan(Tform): label += ' ({:.1f})'.format(Tform) t0, t1, rate, err = sp.smooth_curve(p['time'], p['wavelength'], p[wtkey], n=n, bands=bands) t = (t0 + t1) / 2.0 keep = (t1 > trange[0]) & (t0 < trange[1]) t, rate, err = t[keep], rate[keep], err[keep] if len(t) > maxpts: keep = un.downsample_even(t, maxpts) t, rate, err = t[keep], rate[keep], err[keep] if norms is not None: rate /= norms[i] line, poly = pu.errorpoly(t, rate, err, ealpha=0.2, ax=ax) lines.append(line) labels.append(label) ax.legend(lines, labels, loc='best', fontsize='small')
def flareCompare(inst='cos_g130m', band='SiIV', nflares=3, ax=None): if ax is None: ax = plt.gca() ax.set_xlabel('Time [s]') ax.set_ylabel('Normalized Flux') flares = reduce.combine_flarecats(band, inst) flares = flares[:nflares] bands, dt = flares.meta['BANDS'], flares.meta['DT'] groups = [range(len(bands))] mxpeak = max(flares['pkratio']) # plot all curves lines, labels = [], [] for flare in flares: star = flare['star'] label = starprops['name tex'][star] # curve = reduce.auto_curve(star, inst, bands=bands, dt=dt, appx=False, groups=groups) # t0, t1, cps, cpserr = zip(*curve)[0] ph, p = io.readphotons(star, inst) t0, t1, cps, cpserr = sp.smooth_curve(p['time'], p['wavelength'], p['epsilon'], bands=bands, n=100) tmid = (t0 + t1) / 2.0 t = tmid - flare['peak rel'] qcps = fits.getval(rc.flarepath(star, inst, 'SiIV'), 'QSCTCPS', ext=1) rate, err = cps / qcps, cpserr / qcps fac = 10.0**np.floor(np.log10(mxpeak / flare['pkratio'])) if fac > 1.0: label += ' $\\times$%i' % fac rate = (rate - 1.0) * fac + 1.0 err = err * fac # line = ax.plot(t, rate, '-')[0] # ax.errorbar(t, rate, err, fmt='.', color=line.get_color(), capsize=0) line, poly = pu.errorpoly(t, rate, err, ax=ax) lines.append(line) labels.append(label) ax.legend(lines, labels, loc='best', fontsize='small') starts, stops = flares['start rel'] - flares['peak rel'], flares[ 'stop rel'] - flares['peak rel'] ax.set_xlim(min(starts) * 1.5, max(stops))
def specSnapshot(star, inst, trange, wrange, n=100, ax=None, vCen=None, maxpts=500, **kwargs): if ax is None: ax = plt.gca() ph, p = io.readphotons(star, inst) keep = mnp.inranges(p['time'], trange) p = p[keep] gtis = np.array([ph['gti'].data['start'], ph['gti'].data['stop']]).T gt = mnp.range_intersect([trange], gtis) dt = np.sum(gt[:, 1] - gt[:, 0]) w0, w1, spec, err = sp.smooth_spec(p['wavelength'], p['epera'], n) spec, err = spec / dt, err / dt w = (w0 + w1) / 2.0 keep, = np.nonzero(mnp.inranges(w, wrange)) keep = np.insert(keep, [0, len(keep)], [keep[0] - 1, keep[-1] + 1]) w, spec, err = w[keep], spec[keep], err[keep] if len(w) > maxpts: keep = un.downsample_even(w, maxpts) w, spec, err = w[keep], spec[keep], err[keep] velocify = lambda w: (w - vCen) / vCen * 3e5 if vCen is not None: w = velocify(w) ax.set_xlabel('Doppler Velocity [km s$^{-1}$]') ax.set_xlim(map(velocify, wrange)) else: ax.set_xlabel('Wavelength [$\AA$]') ax.set_xlim(wrange) ax.set_ylabel(fluxlabel) return pu.errorpoly(w, spec, err, **kwargs)
def spectrumMovieFrames(star, inst, band, trange, dt, smoothfac, axspec, axcurve, folder, dpi=80, velocityplot=False, reftrange=None, dryRun=False, ylim=None): ph, photons = io.readphotons(star, inst) band, trange, reftrange = map(np.asarray, [band, trange, reftrange]) fig = axcurve.get_figure() figwidth = fig.get_figwidth() axwidth = axcurve.get_position().width * figwidth axPix = axwidth * dpi def goodN(Nphotons): n = 100 # SN of 10 if Nphotons / n > axPix: # don't need to sample more finely than the pixel scale n = Nphotons / axPix elif Nphotons / n < 20: # want at least some resolution n = max(Nphotons / 20, 9) # but no less than SN of 3ish return n # re-reference times to start of time range tref = trange[0] photons['time'] -= tref if reftrange is not None: reftrange -= tref trange -= tref if velocityplot: velocify = lambda w: (w - velocityplot) / velocityplot * 3e5 vband = velocify(band) p = photons tkeep = [ max(p['time'][0], -trange[1] * 0.5), min(p['time'][-1], trange[1] * 1.5) ] dw = band[1] - band[0] wkeep = [band[0] - 5 * dw, band[1] + 5 * dw] # get rid of superfluous counts keep = mnp.inranges(p['time'], tkeep) & mnp.inranges( p['wavelength'], wkeep) p = p[keep] ## make lightcurve and set up initial plot nlc = goodN( np.sum( mnp.inranges(p['wavelength'], band) & mnp.inranges(p['time'], trange))) t0, t1, lc, lcerr = sp.smooth_curve(p['time'], p['wavelength'], p['epera'], nlc, bands=[band], trange=tkeep) tlc = (t0 + t1) / 2.0 axcurve.set_xlim(trange) axcurve.set_xlabel('Time [s]') axcurve.set_ylabel('Integrated Flux \n[erg cm$^{-2}$ s$^{-1}$]') inrange = mnp.inranges(tlc, trange) pu.errorpoly(tlc[inrange], lc[inrange], lcerr[inrange], 'k-', ax=axcurve, alpha=0.3, ealpha=0.15) # make spectrum frames T = dt * smoothfac nframes = int(round((trange[1] - trange[0] - T) / dt)) t1s = np.linspace(trange[0] + T, trange[1], nframes) t0s = t1s - T wList, specList, errList = [], [], [] for t0, t1 in zip(t0s, t1s): inInterval = mnp.inranges(p['time'], [t0, t1]) pt = p[inInterval] n = goodN(np.sum(mnp.inranges(pt['wavelength'], band))) w0, w1, spec, err = sp.smooth_spec(pt['wavelength'], pt['epera'], n, wkeep) wList.append((w0 + w1) / 2.0) specList.append(spec / T) errList.append(err / T) ## set up spectrum plot if velocityplot: axspec.set_xlabel('Doppler Velocity [km s$^{-1}$]') axspec.set_xlim(vband) else: axspec.set_xlabel('Wavelength [$\AA$]') axspec.set_xlim(band) axspec.set_ylabel(fluxlabel) if ylim is None: ymin = min([np.min(s - e) for s, e in zip(specList, errList)]) ymax = max([np.max(s + e) for s, e in zip(specList, errList)]) axspec.set_ylim(ymin, ymax) else: axspec.set_ylim(ylim) # compute and plot reference spectrum, if desired if reftrange is not None: gtis = np.array([ph['gti'].data['start'], ph['gti'].data['stop'] ]).T - tref gt = mnp.range_intersect([reftrange], gtis) Tref = np.sum(gt[:, 1] - gt[:, 0]) keep = mnp.inranges(photons['time'], reftrange) & mnp.inranges( photons['wavelength'], wkeep) pt = photons[keep] nref = goodN(np.sum(mnp.inranges(pt['wavelength'], band))) w0, w1, spec, _ = sp.smooth_spec(pt['wavelength'], pt['epera'], nref, wkeep) w = (w0 + w1) / 2.0 if velocityplot: w = velocify(w) spec = spec / Tref axspec.plot(w, spec, 'k-', alpha=0.3) # make folder to save frames in if it doesn't exist if not os.path.exists(folder): os.mkdir(folder) ## loop through frames if dryRun: nframes = dryRun for i in range(nframes): # plot time range on lightcurve span = axcurve.axvspan(t0s[i], t1s[i], color='k', alpha=0.2) inrange = mnp.inranges(tlc, [t0s[i], t1s[i]]) linelc, polylc = pu.errorpoly(tlc[inrange], lc[inrange], lcerr[inrange], 'k-', ax=axcurve, ealpha=0.3) # plot spectrum w, spec, err = wList[i], specList[i], errList[i] inrange = mnp.inranges(w, band) inrange = np.nonzero(inrange)[0] inrange = np.insert(inrange, [0, len(inrange)], [inrange[0] - 1, inrange[-1] + 1]) ww, ss, ee = w[inrange], spec[inrange], err[inrange] ss[[0, -1]] = np.interp(band, ww, spec[inrange]) ee[[0, -1]] = np.interp(band, ww, err[inrange]) ww[[0, -1]] = band if velocityplot: ww = velocify(ww) linespec, polyspec = pu.errorpoly(ww, ss, ee, 'k-', ax=axspec, ealpha=0.2) # save frame path = os.path.join(folder, '{:04d}.png'.format(i)) fig.savefig(path, dpi=dpi) # remove plots [obj.remove() for obj in [span, linelc, polylc, linespec, polyspec]]