def compCframe(plate, frame, apred='test', ratio=True, rows=range(300), yr=None, hdu=1): load = apload.ApLoad(apred=apred) mjd = 55562 + int(frame // 10000) new = load.apCframe('M67', plate, mjd, frame) old = {} fig, ax = plots.multi(1, 3, hspace=0.001) x = np.arange(2048) for ichip, chip in enumerate(chips): old[chip] = fits.open(os.environ['APOGEE_REDUX'] + '/r8/apo25m/{:d}/{:d}/apCframe-{:s}-{:d}.fits'. format(plate, mjd, chip, frame)) for row in rows: if ratio: plots.plotl(ax[ichip], x, new[chip][hdu].data[row, :] / old[chip][hdu].data[row, :], yr=[0, 1.5]) else: plots.plotl(ax[ichip], x, new[chip][hdu].data[row, :], yr=yr) plots.plotl(ax[ichip], x, old[chip][hdu].data[row, :], yr=yr) plots.plotl(ax[ichip], x, new[chip][hdu].data[row, :] - old[chip][hdu].data[row, :], yr=yr)
def comp1d(frame, apred='test', rows=range(300)): load = apload.ApLoad(apred=apred) new = load.ap1D(frame) old = {} mjd = 55562 + int(frame // 10000) fig, ax = plots.multi(1, 3, hspace=0.001) x = np.arange(2048) for ichip, chip in enumerate(chips): old[chip] = fits.open( os.environ['APOGEE_REDUX'] + '/r8/red/{:d}/ap1D-{:s}-{:d}.fits'.format(mjd, chip, frame)) for row in rows: plots.plotl(ax[ichip], x, new[chip][1].data[row, :] / old[chip][1].data[row, :], yr=[0, 1.5])
def dr_compare(): # load the DRs, select stars with SN>150 dr12load = apload.ApLoad(dr='dr12') dr12 = dr12load.allStar()[1].data gd = np.where(dr12['SNR'] > 150)[0] dr12 = dr12[gd] dr13load = apload.ApLoad(dr='dr13') dr13 = dr13load.allStar()[1].data gd = np.where(dr13['SNR'] > 150)[0] dr13 = dr13[gd] dr14load = apload.ApLoad(dr='dr14') dr14 = dr14load.allStar()[1].data gd = np.where(dr14['SNR'] > 150)[0] dr14 = dr14[gd] c = apload.allStar()[3].data # match them m1a, m2a = match.match(dr12['APOGEE_ID'], dr13['APOGEE_ID']) m1b, m2b = match.match(dr12['APOGEE_ID'], dr14['APOGEE_ID']) m1c, m2c = match.match(dr13['APOGEE_ID'], dr14['APOGEE_ID']) # parameter figures figu, axu = plots.multi(3, 7, hspace=0.001, wspace=0.001) figc, axc = plots.multi(3, 7, hspace=0.001, wspace=0.001) tit = [ r'T$_{\rm eff}$', 'log g', r'V$_{\rm micro}$', '[M/H]', '[C/M]', '[N/M]', r'[$\alpha$/M]' ] for iparam in range(7): print(iparam) for iy, param in enumerate(['FPARAM', 'PARAM']): if iy == 0: ax = axu else: ax = axc yt = r'$\Delta$' + tit[iparam] if iparam == 6: xt = r'T$_{\rm eff}$' else: xt = None if iparam == 0: ax[iparam, 0].text(0.5, 1.0, 'DR13-DR12', transform=ax[iparam, 0].transAxes, ha='center', va='bottom') ax[iparam, 1].text(0.5, 1.0, 'DR14-DR12', transform=ax[iparam, 1].transAxes, ha='center', va='bottom') ax[iparam, 2].text(0.5, 1.0, 'DR14-DR13', transform=ax[iparam, 2].transAxes, ha='center', va='bottom') if iparam == 0: yr = [-300, 300] elif iparam == 1: yr = [-0.5, 0.5] else: yr = [-0.3, 0.3] xr = [3500, 6000] axim = plots.plotc(ax[iparam, 0], dr12['TEFF'][m1a], dr13[param][m2a, iparam] - dr12[param][m1a, iparam], dr12[param][m1a, 3], size=1, xr=xr, yr=yr, zr=[-1, 0.5], yt=yt, xt=xt, rasterized=True) plots.plotl(ax[iparam, 0], xr, [0., 0.], ls=':') plots.plotc(ax[iparam, 1], dr12['TEFF'][m1b], dr14[param][m2b, iparam] - dr12[param][m1b, iparam], dr12[param][m1b, 3], size=1, xr=xr, yr=yr, zr=[-1, 0.5], xt=xt, rasterized=True) plots.plotl(ax[iparam, 1], xr, [0., 0.], ls=':') plots.plotc(ax[iparam, 2], dr13['TEFF'][m1c], dr14[param][m2c, iparam] - dr13[param][m1c, iparam], dr13[param][m1c, 3], size=1, xr=xr, yr=yr, zr=[-1, 0.5], xt=xt, rasterized=True) plots.plotl(ax[iparam, 2], xr, [0., 0.], ls=':') for iax in range(3): ax[iparam, iax].tick_params(axis='both', labelsize=8) # add colorbar for fig in [figu, figc]: cbaxes = fig.add_axes([0.91, 0.1, 0.01, 0.8]) cb = plt.colorbar(axim, cax=cbaxes) cb.set_label('[M/H]') cbaxes.tick_params(axis='both', labelsize=8) figu.savefig('drcomp_uncal.pdf') figc.savefig('drcomp_cal.pdf') plots.close() # abundance figure fig, ax = plots.multi(3, 14, hspace=0.001, wspace=0.001, figsize=(8, 32)) for ielem, elem in enumerate([ 'C', 'N', 'O', 'Na', 'Mg', 'Al', 'Si', 'S', 'K', 'Ca', 'Ti', 'V', 'Mn', 'Ni' ]): print(elem) yt = r'$\Delta$' + elem if ielem == 13: xt = r'T$_{\rm eff}$' else: xt = None if ielem == 0: ax[ielem, 0].text(0.5, 1.0, 'DR13-DR12', transform=ax[ielem, 0].transAxes, ha='center', va='bottom') ax[ielem, 1].text(0.5, 1.0, 'DR14-DR12', transform=ax[ielem, 1].transAxes, ha='center', va='bottom') ax[ielem, 2].text(0.5, 1.0, 'DR14-DR13', transform=ax[ielem, 2].transAxes, ha='center', va='bottom') yr = [-0.5, 0.5] dr12elem = dr12[elem.upper() + '_H'][m1a] - dr12['FE_H'][m1a] dr13elem = dr13[elem.upper() + '_FE'][m2a] gd = np.where((dr12elem > -99) & (dr13elem > -99))[0] plots.plotc(ax[ielem, 0], dr12['TEFF'][m1a[gd]], dr13elem[gd] - dr12elem[gd], dr12['PARAM'][m1a[gd], 3], size=1, xr=[3500, 6000], yr=yr, zr=[-1, 0.5], yt=yt, xt=xt, nytick=5, rasterized=True) plots.plotl(ax[ielem, 0], xr, [0., 0.], ls=':') ax[ielem, 0].tick_params(axis='both', labelsize=8) dr12elem = dr12[elem.upper() + '_H'][m1b] - dr12['FE_H'][m1b] dr14elem = dr14[elem.upper() + '_FE'][m2b] gd = np.where((dr12elem > -99) & (dr14elem > -99))[0] plots.plotc(ax[ielem, 1], dr12['TEFF'][m1b[gd]], dr14elem[gd] - dr12elem[gd], dr12['PARAM'][m1b[gd], 3], size=1, xr=[3500, 6000], yr=yr, zr=[-1, 0.5], xt=xt, nytick=5, rasterized=True) plots.plotl(ax[ielem, 1], xr, [0., 0.], ls=':') ax[ielem, 1].tick_params(axis='both', labelsize=8) dr13elem = dr13[elem.upper() + '_FE'][m1c] dr14elem = dr14[elem.upper() + '_FE'][m2c] gd = np.where((dr13elem > -99) & (dr14elem > -99))[0] plots.plotc(ax[ielem, 2], dr13['TEFF'][m1c[gd]], dr14elem[gd] - dr13elem[gd], dr13['PARAM'][m1c[gd], 3], size=1, xr=[3500, 6000], yr=yr, zr=[-1, 0.5], xt=xt, nytick=5, rasterized=True) plots.plotl(ax[ielem, 2], xr, [0., 0.], ls=':') ax[ielem, 2].tick_params(axis='both', labelsize=8) cbaxes = fig.add_axes([0.91, 0.1, 0.01, 0.8]) cb = plt.colorbar(axim, cax=cbaxes) cb.set_label('[M/H]') cbaxes.tick_params(axis='both', labelsize=8) for item in (cbaxes.get_xticklabels() + cbaxes.get_yticklabels()): item.set_fontsize(8) fig.savefig('drcomp_elem.pdf')
print('y6: ') lsfsum(y6) y7 = load.apLSF(25560065)[chip] print('y7: ') lsfsum(y7) ii = 0 for ipar in range(26): iy = ipar % 3 ix = ipar // 3 pax[iy, ix].plot(y1[0].data[ii, :]) ii += 1 for icol in range(4): for fiber in [150]: col = 500 + icol * 500 n = y1[1].data.shape[0] plots.plotl(ax[ichip, icol], np.arange(21), y1[1].data[n / 2 - 10:n / 2 + 11, fiber, col]) n = y2[1].data.shape[0] plots.plotl(ax[ichip, icol], np.arange(21), y2[1].data[n / 2 - 10:n / 2 + 11, fiber, col]) n = y3[1].data.shape[0] plots.plotl(ax[ichip, icol], np.arange(21), y3[1].data[n / 2 - 10:n / 2 + 11, fiber, col]) n = y4[1].data.shape[0] plots.plotl(ax[ichip, icol], np.arange(21), y4[1].data[n / 2 - 10:n / 2 + 11, fiber, col]) n = y5[1].data.shape[0] plots.plotl(ax[ichip, icol], np.arange(21), y5[1].data[n / 2 - 10:n / 2 + 11, fiber, col]) n = y6[1].data.shape[0] plots.plotl(ax[ichip, icol], np.arange(21), y6[1].data[n / 2 - 10:n / 2 + 11, fiber, col])
def comp(plate=7267, mjd=56654, fiber=150, frame=10920059, field='M67'): r11 = apload.ApLoad(apred='r11') v = r11.apVisit(plate, mjd, fiber) a = r11.ap1D(frame) c = r11.apCframe(field, plate, mjd, frame) v14 = fits.open(os.environ['APOGEE_REDUX'] + '/r8/apo25m/{:d}/{:d}/apVisit-r8-{:d}-{:d}-{:03d}.fits'. format(plate, mjd, plate, mjd, fiber)) a14 = {} c14 = {} for chip in chips: a14[chip] = fits.open( os.environ['APOGEE_REDUX'] + '/r8/red/{:d}/ap1D-{:s}-{:d}.fits'.format(mjd, chip, frame)) c14[chip] = fits.open(os.environ['APOGEE_REDUX'] + '/r8/apo25m/{:d}/{:d}/apCframe-{:s}-{:08d}.fits'. format(plate, mjd, chip, frame)) fig, ax = plots.multi(1, 3, hspace=0.01) x = np.arange(4096) pixmask = bitmask.PixelBitMask() for ichip, chip in enumerate(chips): y = v[1].data[ichip, :] plots.plotl(ax[ichip], x, v[1].data[ichip, :] / v14[1].data[ichip, :]) bd = np.where(((v[3].data[ichip, :] & pixmask.badval()) > 0) | ( (v[3].data[ichip, :] & pixmask.getval('SIG_SKYLINE')) > 0))[0] y[bd] = np.nan plots.plotl(ax[ichip], x, y / v14[1].data[ichip, :]) fig, ax = plots.multi(3, 3, hspace=0.01) x = np.arange(2048) for ichip, chip in enumerate(chips): plots.plotl(ax[ichip, 0], x, c[chip][1].data[300 - fiber, :]) plots.plotl(ax[ichip, 0], x, c14[chip][1].data[300 - fiber, :]) plots.plotl( ax[ichip, 1], x, c[chip][1].data[300 - fiber, :] / c14[chip][1].data[300 - fiber]) plots.plotl( ax[ichip, 2], x, a[chip][1].data[300 - fiber, :] / a14[chip][1].data[300 - fiber])
def visitcomb(allvisit, starver, load=None, apred='r13', telescope='apo25m', nres=[5, 4.25, 3.5], bconly=False, plot=False, write=True, dorvfit=True, apstar_vers='stars', logger=None): """ Combine multiple visits with individual RVs to rest frame sum """ if logger is None: logger = dln.basiclogger() if load is None: load = apload.ApLoad(apred=apred, telescope=telescope) cspeed = 2.99792458e5 # speed of light in km/s logger.info('Doing visitcomb for {:s} '.format(allvisit['apogee_id'][0])) wnew = norm.apStarWave() nwave = len(wnew) nvisit = len(allvisit) # initialize array for stack of interpolated spectra zeros = np.zeros([nvisit, nwave]) izeros = np.zeros([nvisit, nwave], dtype=int) stack = apload.ApSpec(zeros, err=zeros.copy(), bitmask=izeros, cont=zeros.copy(), sky=zeros.copy(), skyerr=zeros.copy(), telluric=zeros.copy(), telerr=zeros.copy()) apogee_target1, apogee_target2, apogee_target3 = 0, 0, 0 apogee2_target1, apogee2_target2, apogee2_target3, apogee2_target4 = 0, 0, 0, 0 starflag, andflag = np.uint64(0), np.uint64(0) starmask = bitmask.StarBitMask() # Loop over each visit and interpolate to final wavelength grid if plot: fig, ax = plots.multi(1, 2, hspace=0.001) for i, visit in enumerate(allvisit): if bconly: vrel = -visit['bc'] else: vrel = visit['vrel'] # Skip if we don't have an RV if np.isfinite(vrel) is False: continue # Load the visit if load.telescope == 'apo1m': apvisit = load.apVisit1m(visit['plate'], visit['mjd'], visit['apogee_id'], load=True) else: apvisit = load.apVisit(int(visit['plate']), visit['mjd'], visit['fiberid'], load=True) pixelmask = bitmask.PixelBitMask() # Rest-frame wavelengths transformed to this visit spectra w = norm.apStarWave() * (1.0 + vrel / cspeed) # Loop over the chips for chip in range(3): # Get the pixel values to interpolate to pix = wave.wave2pix(w, apvisit.wave[chip, :]) gd, = np.where(np.isfinite(pix)) # Get a smoothed, filtered spectrum to use as replacement for bad values cont = gaussian_filter( median_filter(apvisit.flux[chip, :], [501], mode='reflect'), 100) errcont = gaussian_filter( median_filter(apvisit.flux[chip, :], [501], mode='reflect'), 100) bd, = np.where(apvisit.bitmask[chip, :] & pixelmask.badval()) if len(bd) > 0: apvisit.flux[chip, bd] = cont[bd] apvisit.err[chip, bd] = errcont[bd] # Load up quantity/error pairs for interpolation raw = [[apvisit.flux[chip, :], apvisit.err[chip, :]**2], [apvisit.sky[chip, :], apvisit.skyerr[chip, :]**2], [apvisit.telluric[chip, :], apvisit.telerr[chip, :]**2]] # Load up individual mask bits for ibit, name in enumerate(pixelmask.name): if name is not '' and len( np.where(apvisit.bitmask[chip, :] & 2**ibit)[0]) > 0: raw.append([ np.clip(apvisit.bitmask[chip, :] & 2**ibit, None, 1), None ]) # Do the sinc interpolation out = sincint.sincint(pix[gd], nres[chip], raw) # From output flux, get continuum to remove, so that all spectra are # on same scale. We'll later multiply in the median continuum flux = out[0][0] stack.cont[i, gd] = gaussian_filter( median_filter(flux, [501], mode='reflect'), 100) # Load interpolated spectra into output stack stack.flux[i, gd] = out[0][0] / stack.cont[i, gd] stack.err[i, gd] = out[0][1] / stack.cont[i, gd] stack.sky[i, gd] = out[1][0] stack.skyerr[i, gd] = out[1][1] stack.telluric[i, gd] = out[2][0] stack.telerr[i, gd] = out[2][1] # For mask, set bits where interpolated value is above some threshold # defined for each mask bit iout = 3 for ibit, name in enumerate(pixelmask.name): if name is not '' and len( np.where(apvisit.bitmask[chip, :] & 2**ibit)[0]) > 0: j = np.where( np.abs(out[iout][0]) > pixelmask.maskcontrib[ibit])[0] stack.bitmask[i, gd[j]] |= 2**ibit iout += 1 # Increase uncertainties for persistence pixels bd, = np.where( (stack.bitmask[i, :] & pixelmask.getval('PERSIST_HIGH')) > 0) if len(bd) > 0: stack.err[i, bd] *= np.sqrt(5) bd, = np.where(( (stack.bitmask[i, :] & pixelmask.getval('PERSIST_HIGH')) == 0) & ( (stack.bitmask[i, :] & pixelmask.getval('PERSIST_MED')) > 0)) if len(bd) > 0: stack.err[i, bd] *= np.sqrt(4) bd, = np.where( ((stack.bitmask[i, :] & pixelmask.getval('PERSIST_HIGH')) == 0) & ((stack.bitmask[i, :] & pixelmask.getval('PERSIST_MED')) == 0) & ((stack.bitmask[i, :] & pixelmask.getval('PERSIST_LOW')) > 0)) if len(bd) > 0: stack.err[i, bd] *= np.sqrt(3) bd, = np.where( (stack.bitmask[i, :] & pixelmask.getval('SIG_SKYLINE')) > 0) if len(bd) > 0: stack.err[i, bd] *= np.sqrt(100) if plot: ax[0].plot(norm.apStarWave(), stack.flux[i, :]) ax[1].plot(norm.apStarWave(), stack.flux[i, :] / stack.err[i, :]) plt.draw() pdb.set_trace() # Accumulate for header of combined frame. Turn off visit specific RV flags first visitflag = visit['starflag'] & ~starmask.getval( 'RV_REJECT') & ~starmask.getval('RV_SUSPECT') starflag |= visitflag andflag &= visitflag if visit['survey'] == 'apogee': apogee_target1 |= visit['apogee_target1'] apogee_target2 |= visit['apogee_target2'] apogee_target3 |= visit['apogee_target3'] elif visit['survey'].find('apogee2') >= 0: apogee2_target1 |= visit['apogee_target1'] apogee2_target2 |= visit['apogee_target2'] apogee2_target3 |= visit['apogee_target3'] try: apogee2_target4 |= visit['apogee_target4'] except: pass # MWM target flags? # Create final spectrum zeros = np.zeros([nvisit + 2, nwave]) izeros = np.zeros([nvisit + 2, nwave], dtype=int) apstar = apload.ApSpec(zeros, err=zeros.copy(), bitmask=izeros, wave=norm.apStarWave(), sky=zeros.copy(), skyerr=zeros.copy(), telluric=zeros.copy(), telerr=zeros.copy(), cont=zeros.copy(), template=zeros.copy()) apstar.header['CRVAL1'] = norm.logw0 apstar.header['CDELT1'] = norm.dlogw apstar.header['CRPIX1'] = 1 apstar.header['CTYPE1'] = ( 'LOG-LINEAR', 'Logarithmic wavelength scale in subsequent HDU') apstar.header['DC-FLAG'] = 1 # Pixel-by-pixel weighted average cont = np.median(stack.cont, axis=0) apstar.flux[0, :] = np.sum(stack.flux / stack.err**2, axis=0) / np.sum( 1. / stack.err**2, axis=0) * cont apstar.err[0, :] = np.sqrt(1. / np.sum(1. / stack.err**2, axis=0)) * cont apstar.bitmask[0, :] = np.bitwise_and.reduce(stack.bitmask, 0) apstar.cont[0, :] = cont # Individual visits apstar.flux[2:, :] = stack.flux * stack.cont apstar.err[2:, :] = stack.err * stack.cont apstar.bitmask[2:, :] = stack.bitmask apstar.sky[2:, :] = stack.sky apstar.skyerr[2:, :] = stack.skyerr apstar.telluric[2:, :] = stack.telluric apstar.telerr[2:, :] = stack.telerr # Populate header apstar.header['OBJID'] = (allvisit['apogee_id'][0], 'APOGEE object name') apstar.header['APRED'] = (apred, 'APOGEE reduction version') apstar.header['STARVER'] = (starver, 'apStar version') apstar.header['HEALPIX'] = (apload.obj2healpix(allvisit['apogee_id'][0]), 'HEALPix location') try: apstar.header['SNR'] = (np.nanmedian(apstar.flux / apstar.err), 'Median S/N per apStar pixel') except: apstar.header['SNR'] = (0., 'Median S/N per apStar pixel') apstar.header['RA'] = (allvisit['ra'].max(), 'right ascension, deg, J2000') apstar.header['DEC'] = (allvisit['dec'].max(), 'declination, deg, J2000') apstar.header['GLON'] = (allvisit['glon'].max(), 'Galactic longitude') apstar.header['GLAT'] = (allvisit['glat'].max(), 'Galactic latitude') apstar.header['J'] = (allvisit['j'].max(), '2MASS J magnitude') apstar.header['J_ERR'] = (allvisit['j_err'].max(), '2MASS J magnitude uncertainty') apstar.header['H'] = (allvisit['h'].max(), '2MASS H magnitude') apstar.header['H_ERR'] = (allvisit['h_err'].max(), '2MASS H magnitude uncertainty') apstar.header['K'] = (allvisit['k'].max(), '2MASS K magnitude') apstar.header['K_ERR'] = (allvisit['k_err'].max(), '2MASS K magnitude uncertainty') try: apstar.header['SRC_H'] = (allvisit['src_h'][0], 'source of H magnitude') except KeyError: pass keys = [ 'wash_m', 'wash_t2', 'ddo51', 'irac_3_6', 'irac_4_5', 'irac_5_8', 'wise_4_5', 'targ_4_5' ] for key in keys: try: apstar.header[key] = allvisit[key].max() except KeyError: pass apstar.header['AKTARG'] = (allvisit['ak_targ'].max(), 'Extinction used for targeting') apstar.header['AKMETHOD'] = (allvisit['ak_targ_method'][0], 'Extinction method using for targeting') apstar.header['AKWISE'] = (allvisit['ak_wise'].max(), 'WISE all-sky extinction') apstar.header['SFD_EBV'] = (allvisit['sfd_ebv'].max(), 'SFD E(B-V)') apstar.header['APTARG1'] = (apogee_target1, 'APOGEE_TARGET1 targeting flag') apstar.header['APTARG2'] = (apogee_target2, 'APOGEE_TARGET2 targeting flag') apstar.header['APTARG3'] = (apogee_target3, 'APOGEE_TARGET3 targeting flag') apstar.header['AP2TARG1'] = (apogee2_target1, 'APOGEE2_TARGET1 targeting flag') apstar.header['AP2TARG2'] = (apogee2_target2, 'APOGEE2_TARGET2 targeting flag') apstar.header['AP2TARG3'] = (apogee2_target3, 'APOGEE2_TARGET3 targeting flag') apstar.header['AP2TARG4'] = (apogee2_target4, 'APOGEE2_TARGET4 targeting flag') apstar.header['NVISITS'] = (len(allvisit), 'Number of visit spectra combined flag') apstar.header['STARFLAG'] = (starflag, 'bitwise OR of individual visit starflags') apstar.header['ANDFLAG'] = (andflag, 'bitwise AND of individual visit starflags') try: apstar.header['N_COMP'] = (allvisit['n_components'].max(), 'Maximum number of components in RV CCFs') except: pass apstar.header['VHBARY'] = ( (allvisit['vheliobary'] * allvisit['snr']).sum() / allvisit['snr'].sum(), 'S/N weighted mean barycentric RV') if len(allvisit) > 1: apstar.header['vscatter'] = (allvisit['vheliobary'].std(ddof=1), 'standard deviation of visit RVs') else: apstar.header['VSCATTER'] = (0., 'standard deviation of visit RVs') apstar.header['VERR'] = (0., 'unused') apstar.header['RV_TEFF'] = (allvisit['rv_teff'].max(), 'Effective temperature from RV fit') apstar.header['RV_LOGG'] = (allvisit['rv_logg'].max(), 'Surface gravity from RV fit') apstar.header['RV_FEH'] = (allvisit['rv_feh'].max(), 'Metallicity from RV fit') if len(allvisit) > 0: meanfib = (allvisit['fiberid'] * allvisit['snr']).sum() / allvisit['snr'].sum() else: meanfib = 999999. if len(allvisit) > 1: sigfib = allvisit['fiberid'].std(ddof=1) else: sigfib = 0. apstar.header['MEANFIB'] = (meanfib, 'S/N weighted mean fiber number') apstar.header['SIGFIB'] = ( sigfib, 'standard deviation (unweighted) of fiber number') apstar.header['NRES'] = ('{:5.2f}{:5.2f}{:5.2f}'.format(*nres), 'number of pixels/resolution used for sinc') # individual visit information in header for i0, visit in enumerate(allvisit): i = i0 + 1 apstar.header['SFILE{:d}'.format(i)] = ( visit['file'], ' Visit #{:d} spectrum file'.format(i)) apstar.header['DATE{:d}'.format(i)] = ( visit['dateobs'], 'DATE-OBS of visit {:d}'.format(i)) apstar.header['JD{:d}'.format(i)] = ( visit['jd'], 'Julian date of visit {:d}'.format(i)) # hjd = helio_jd(visitstr[i].jd-2400000.0,visitstr[i].ra,visitstr[i].dec) #apstar.header['HJD{:d}'.format(i)] = apstar.header['FIBER{:d}'.format(i)] = (visit['fiberid'], ' Fiber, visit {:d}'.format(i)) apstar.header['BC{:d}'.format(i)] = ( visit['bc'], ' Barycentric correction (km/s), visit {:d}'.format(i)) apstar.header['VRAD{:d}'.format(i)] = ( visit['vrel'], ' Doppler shift (km/s) of visit {:d}'.format(i)) #apstar.header['VERR%d'.format(i)] = apstar.header['VHBARY{:d}'.format(i)] = ( visit['vheliobary'], ' Barycentric velocity (km/s), visit {:d}'.format(i)) apstar.header['SNRVIS{:d}'.format(i)] = ( visit['snr'], ' Signal/Noise ratio, visit {:d}'.format(i)) apstar.header['FLAG{:d}'.format(i)] = ( visit['starflag'], ' STARFLAG for visit {:d}'.format(i)) apstar.header.insert('SFILE{:d}'.format(i), ('COMMENT', 'VISIT {:d} INFORMATION'.format(i))) # Do a RV fit just to get a template and normalized spectrum, for plotting if dorvfit: try: apstar.setmask(pixelmask.badval()) spec = doppler.Spec1D(apstar.flux[0, :], err=apstar.err[0, :], bitmask=apstar.bitmask[0, :], mask=apstar.mask[0, :], wave=apstar.wave, lsfpars=np.array([0]), lsfsigma=apstar.wave / 22500 / 2.354, instrument='APOGEE', filename=apstar.filename) out = doppler.rv.jointfit([spec], verbose=False, plot=False, tweak=False, maxvel=[-50, 50]) apstar.cont = out[3][0].flux apstar.template = out[2][0].flux except ValueError as err: logger.error('Exception raised in visitcomb RV for: ', apstar.header['FIELD'], apstar.header['OBJID']) logger.error("ValueError: {0}".format(err)) except RuntimeError as err: logger.error('Exception raised in visitcomb RV for: ', apstar.header['FIELD'], apstar.header['OBJID']) logger.error("Runtime error: {0}".format(err)) except: logger.error('Exception raised in visitcomb RV fit for: ', apstar.header['FIELD'], apstar.header['OBJID']) # Write the spectrum to file if write: outfilenover = load.filename('Star', obj=apstar.header['OBJID']) outdir = os.path.dirname(outfilenover) outbase = os.path.splitext(os.path.basename(outfilenover))[0] outbase += '-' + starver # add star version outfile = outdir + '/' + outbase + '.fits' if apstar_vers != 'stars': outfile = outfile.replace('/stars/', '/' + apstar_vers + '/') outdir = os.path.dirname(outfile) try: os.makedirs(os.path.dirname(outfile)) except: pass logger.info('Writing apStar file to ' + outfile) apstar.write(outfile) apstar.filename = outfile mwm_root = os.environ['MWM_ROOT'] apstar.uri = outfile[len(mwm_root) + 1:] # Create symlink no file with no version if os.path.exists(outfilenover) or os.path.islink(outfilenover): os.remove(outfilenover) os.symlink(os.path.basename(outfile), outfilenover) # relative path # Plot gd, = np.where( (apstar.bitmask[0, :] & (pixelmask.badval() | pixelmask.getval('SIG_SKYLINE'))) == 0) fig, ax = plots.multi(1, 3, hspace=0.001, figsize=(48, 6)) med = np.nanmedian(apstar.flux[0, :]) plots.plotl(ax[0], norm.apStarWave(), apstar.flux[0, :], color='k', yr=[0, 2 * med]) ax[0].plot(norm.apStarWave()[gd], apstar.flux[0, gd], color='g') ax[0].set_ylabel('Flux') try: ax[1].plot(norm.apStarWave()[gd], apstar.cont[gd], color='g') ax[1].set_ylabel('Normalized') ax[1].plot(norm.apStarWave(), apstar.template, color='r') except: pass plots.plotl(ax[2], norm.apStarWave(), apstar.flux[0, :] / apstar.err[0, :], yt='S/N') for i in range(3): ax[i].set_xlim(15100, 17000) ax[0].set_xlabel('Wavelength') fig.savefig(outdir + '/plots/' + outbase + '.png') # Plot if plot: ax[0].plot(norm.apStarWave(), apstar.flux, color='k') ax[1].plot(norm.apStarWave(), apstar.flux / apstar.err, color='k') plt.draw() pdb.set_trace() return apstar
def gauss_decomp(out, phase='one', alpha1=0.5, alpha2=1.5, thresh=[4, 4], plot=None, filt=False): """ Do Gaussian decomposition of CCF using gausspy Parameters: out : list of dictionaries for each frame, giving x_ccf, ccf, and ccferr phase : gausspy paramater alpha1 : gausspy parameter alpha2 : gausspy parameter for second set of gaussians if phase=='two' thresh : gausspy parameter plot (str) : if not None, do plot and use as root file name for plot filt (bool) : if true, apply filtering to remove components judged to be insignificant """ g = gp.GaussianDecomposer() g.set('phase', phase) g.set('SNR_thresh', thresh) g.set('alpha1', alpha1) g.set('alpha2', alpha2) gout = [] if plot is not None: fig, ax = plots.multi(1, len(out), hspace=0.001, figsize=(6, 2 + n)) for i, final in enumerate(out): gd, = np.where(np.isfinite(final['x_ccf'])) x = final['x_ccf'][gd] y = final['ccf'][gd] # high pass filter for better performance if filt: final['ccf'][gd] -= gaussian_filter(final['ccf'][gd], 50, mode='nearest') try: decomp = g.decompose(x, final['ccf'][gd], final['ccferr'][gd]) n = decomp['N_components'] except: print( 'Exception in Gaussian decomposition, setting to 0 components') n = 0 decomp = None if filt and n > 0: # remove components if they are within width of brighter component, or <0.25 peak , # or more than twice as wide, or if primary component is wide for j in range(1, n): pars_j = decomp['best_fit_parameters'][j::n] for k in range(j): pars_k = decomp['best_fit_parameters'][k::n] if (pars_j[0] > pars_k[0] and pars_k[0] > 0 and (abs(pars_j[2] - pars_k[2]) < abs(pars_j[1]) or pars_k[0] < 0.25 * pars_j[0] or abs(pars_j[1]) > 100 or np.abs(pars_k[1]) > 2 * np.abs(pars_j[1]))): decomp['best_fit_parameters'][k] = 0 decomp['N_components'] -= 1 elif (pars_k[0] > pars_j[0] and pars_j[0] > 0 and (abs(pars_j[2] - pars_k[2]) < abs(pars_k[1]) or pars_j[0] < 0.25 * pars_k[0] or abs(pars_k[1]) > 100 or np.abs(pars_j[1]) > 2 * np.abs(pars_k[1]))): decomp['best_fit_parameters'][j] = 0 pars_j = decomp['best_fit_parameters'][j::n] decomp['N_components'] -= 1 gout.append(decomp) if plot is not None: plots.plotl(ax[i], final['x_ccf'], final['ccf']) ax[i].plot(final['x_ccf'], final['ccferr'], color='r') for j in range(n): pars = gout[i]['best_fit_parameters'][j::n] ax[i].plot(x, gaussian(*pars)(x)) if pars[0] > 0: color = 'k' else: color = 'r' ax[i].text(0.1, 0.8 - j * 0.1, '{:8.1f}{:8.1f}{:8.1f}'.format(*pars), transform=ax[i].transAxes, color=color) fig.savefig(plot + '_ccf.png') del g return gout
def norm(w, coef, ichip): x = w - 16000. logflux = coef[0] * x**3 + coef[1] * x**2 + coef[2] * x if ichip == 0: logflux += coef[3] elif ichip == 2: logflux += coef[4] return 10.**logflux for row in rows[0:30:5]: for ichip, chip in enumerate(chips): x = b[chip][4].data[row, :] plots.plotl(ax[0], x, b[chip][1].data[row, :], color='k', semilogy=True) plots.plotl(ax[0], x, b[chip][1].data[row, :] / norm(x, w, ichip), color=colors[ichip], semilogy=True) r10 = apload.ApLoad(apred='r10') b = r10.ap1D(3190056) for row in rows[0:30:5]: plots.plotl(ax[1], b['c'][4].data[row, :], b['c'][1].data[row, :], color='b',