def read_meszarosgcdata(filename=os.path.join(
    os.path.dirname(os.path.realpath(__file__)), '..', 'data', 'clusterdata',
    'aj509073t2_mrt.txt')):
    """
    NAME:
       read_meszarosgcdata
    PURPOSE:
       Read the data on globular clusters from Meszaros et al. (2015)
    INPUT:
       filename= Name of the file that has the ApJ machine-readable table
    OUTPUT:
       data structure with the data
    HISTORY:
       2015-02-11 - Started - Bovy (IAS@KITP)
       2015-08-13 - Re-written for new data format - Bovy (UofT)
    """
    data = astropy.io.ascii.read(filename)
    data.rename_column('Clust', 'CLUSTER')
    data.rename_column('Teff', 'TEFF')
    data.rename_column('log(g)', 'LOGG')
    data.rename_column('[Fe/H]', 'FEH')
    data.rename_column('2MASS', 'ID')
    # Now match to allStar to get the location_ids and H magnitudes
    alldata = apread.allStar(raw=True)
    locids = numpy.zeros(len(data), dtype='int') - 1
    hmags = numpy.zeros(len(data), dtype='float') - 1
    # and match to allVisit for the fibers that each star was observed in
    allvdata = apread.allVisit(raw=True)
    fibers = numpy.zeros(
        (len(data), numpy.nanmax(alldata['NVISITS'])), dtype='int') - 1
    for ii in range(len(data)):
        if 'Pleiades' in data['CLUSTER'][ii]: continue
        indx = alldata['APOGEE_ID'] == data['ID'][ii]
        if numpy.sum(indx) == 0:
            raise ValueError('allStar match for %s not found ...' %
                             (data['ID'][ii]))
        if len(list(set(alldata['LOCATION_ID'][indx]))) > 1:
            raise ValueError('Multiple matches found for for %s ...' %
                             (data['ID'][ii]))
        locids[ii] = alldata['LOCATION_ID'][indx][0]
        hmags[ii] = alldata['H'][indx][0]
        for jj in range(alldata['NVISITS'][indx][0]):
            fibers[ii, jj] = allvdata[alldata['VISIT_PK'][indx][0,
                                                                jj]]['FIBERID']
    data['LOCATION_ID'] = locids
    data['H'] = hmags
    data['FIBERID'] = fibers
    data['APOGEE_ID'] = data['ID']
    data['FE_H'] = data['FEH']
    return data
def read_meszarosgcdata(filename='../clusterdata/aj509073t2_mrt.txt'):
    """
    NAME:
       read_meszarosgcdata
    PURPOSE:
       Read the data on globular clusters from Meszaros et al. (2015)
    INPUT:
       filename= Name of the file that has the ApJ machine-readable table
    OUTPUT:
       data structure with the data
    HISTORY:
       2015-02-11 - Started - Bovy (IAS@KITP)
       2015-08-13 - Re-written for new data format - Bovy (UofT)
    """
    data= astropy.io.ascii.read(filename)
    data.rename_column('Clust','CLUSTER')
    data.rename_column('Teff','TEFF')
    data.rename_column('log(g)','LOGG')
    data.rename_column('[Fe/H]','FEH')
    data.rename_column('2MASS','ID')
    # Now match to allStar to get the location_ids and H magnitudes
    alldata= apread.allStar(raw=True)
    locids= numpy.zeros(len(data),dtype='int')-1
    hmags= numpy.zeros(len(data),dtype='float')-1
    # and match to allVisit for the fibers that each star was observed in
    allvdata= apread.allVisit(raw=True)
    fibers= numpy.zeros((len(data),numpy.nanmax(alldata['NVISITS'])),
                        dtype='int')-1
    for ii in range(len(data)):
        if 'Pleiades' in data['CLUSTER'][ii]: continue
        indx= alldata['APOGEE_ID'] == data['ID'][ii]
        if numpy.sum(indx) == 0:
            raise ValueError('allStar match for %s not found ...' % (data['ID'][ii]))
        if len(list(set(alldata['LOCATION_ID'][indx]))) > 1:
            raise ValueError('Multiple matches found for for %s ...' % (data['ID'][ii]))
        locids[ii]= alldata['LOCATION_ID'][indx][0]
        hmags[ii]= alldata['H'][indx][0]
        for jj in range(alldata['NVISITS'][indx][0]):
            fibers[ii,jj]= allvdata[alldata['VISIT_PK'][indx][0,jj]]['FIBERID']
    data['LOCATION_ID']= locids
    data['H']= hmags
    data['FIBERID']= fibers
    data['APOGEE_ID'] = data['ID']
    data['FE_H'] = data['FEH']
    return data
Exemple #3
0
def read_apogee_catalog():
    '''
    read in the catalog of info for all stars in a data release. 
    '''
    all_star_catalog = apread.allStar(rmcommissioning=False,
                                      rmdups=False,
                                      main=False,
                                      raw=True)

    # and match to allVisit for the fibers that each star was observed in
    allvdata = apread.allVisit(raw=True)
    fibers = np.zeros(
        (len(all_star_catalog), np.nanmax(all_star_catalog['NVISITS'])),
        dtype='int') - 1

    for ii in range(len(all_star_catalog)):
        for jj in range(all_star_catalog['NVISITS'][ii]):
            fibers[ii, jj] = allvdata[all_star_catalog['VISIT_PK'][ii]
                                      [jj]]['FIBERID']

    return all_star_catalog, fibers
def read_caldata(filename=os.path.join(
    os.path.dirname(os.path.realpath(__file__)), 'aj485195t4_mrt.txt'),
                 dr='12'):
    """
    NAME:
       read_caldata
    PURPOSE:
       Read the data on calibration clusters from Meszaros et al. (2013)
    INPUT:
       filename= Name of the file that has the ApJ machine-readable table
    OUTPUT:
       data structure with the data
    HISTORY:
       2015-02-11 - Written - Bovy (IAS@KITP)
    """
    data = astropy.io.ascii.read(filename)
    data.rename_column('Cluster', 'CLUSTER')
    data.remove_column('Teff')
    data.rename_column('TeffC', 'TEFF')
    data.remove_column('logg')
    data.rename_column('loggC', 'LOGG')
    data.remove_column('[M/H]')
    data.rename_column('[M/H]C', 'FEH')
    data.rename_column('2MASS', 'ID')
    # Now match to allStar to get the location_ids
    alldata = apread.allStar(raw=True)
    locids = numpy.zeros(len(data), dtype='int') - 1
    hmags = numpy.zeros(len(data), dtype='float') - 1
    snrs = numpy.zeros(len(data), dtype='float') - 1
    ras = numpy.zeros(len(data), dtype='float') - 1
    decs = numpy.zeros(len(data), dtype='float') - 1
    # and match to allVisit for the fibers that each star was observed in
    allvdata = apread.allVisit(raw=True)
    fibers = numpy.zeros(
        (len(data), numpy.nanmax(alldata['NVISITS'])), dtype='int') - 1
    inds = []
    for ii in range(len(data)):
        if 'Pleiades' in data['CLUSTER'][ii]:
            inds.append(0)
            continue
        indx = alldata['APOGEE_ID'] == data['ID'][ii]
        success = numpy.where(indx == True)[0]
        if success.size == 0 or success.size > 1:
            inds.append(0)
        elif success.size == 1:
            inds.append(success[0])
        print(indx)
        #        if numpy.sum(indx) == 0:
        #            raise ValueError('allStar match for %s not found ...' % (data['ID'][ii]))
        #        if len(list(set(alldata['LOCATION_ID'][indx]))) > 1:
        #            raise ValueError('Multiple matches found for for %s ...' % (data['ID'][ii]))
        locids[ii] = alldata['LOCATION_ID'][indx][0]
        hmags[ii] = alldata['H'][indx][0]
        snrs[ii] = alldata['SNR'][indx][0]
        ras[ii] = alldata['RA'][indx][0]
        decs[ii] = alldata['DEC'][indx][0]
        for jj in range(alldata['NVISITS'][indx][0]):
            fibers[ii, jj] = allvdata[alldata['VISIT_PK'][indx][0,
                                                                jj]]['FIBERID']
    inds = (numpy.array(inds), )
    data['LOCATION_ID'] = locids
    data['H'] = hmags
    data['FIBERID'] = fibers
    data['SNR'] = snrs
    data['APOGEE_ID'] = data['ID']
    data['RA'] = ras
    data['DEC'] = decs
    data['index'] = inds[0]
    data['M_H'] = data['FEH']
    data['FE_H'] = alldata['FE_H'][inds]
    if int(dr) > 12:
        rel = 'FE'
    if int(dr) <= 12:
        rel = 'H'
    data['C_{0}'.format(rel)] = alldata['C_{0}'.format(rel)][inds]
    data['N_{0}'.format(rel)] = alldata['N_{0}'.format(rel)][inds]
    data['O_{0}'.format(rel)] = alldata['O_{0}'.format(rel)][inds]
    data['NA_{0}'.format(rel)] = alldata['NA_{0}'.format(rel)][inds]
    data['MG_{0}'.format(rel)] = alldata['MG_{0}'.format(rel)][inds]
    data['AL_{0}'.format(rel)] = alldata['AL_{0}'.format(rel)][inds]
    data['SI_{0}'.format(rel)] = alldata['SI_{0}'.format(rel)][inds]
    data['S_{0}'.format(rel)] = alldata['S_{0}'.format(rel)][inds]
    data['K_{0}'.format(rel)] = alldata['K_{0}'.format(rel)][inds]
    data['CA_{0}'.format(rel)] = alldata['CA_{0}'.format(rel)][inds]
    data['TI_{0}'.format(rel)] = alldata['TI_{0}'.format(rel)][inds]
    data['V_{0}'.format(rel)] = alldata['V_{0}'.format(rel)][inds]
    data['MN_{0}'.format(rel)] = alldata['MN_{0}'.format(rel)][inds]
    data['NI_{0}'.format(rel)] = alldata['NI_{0}'.format(rel)][inds]
    return numpy.array(data)
Exemple #5
0
def get_visit_spectra_individual_object(apogee_id,
                                        allvisit_cat=None,
                                        save_local=False):
    '''
    Download the visit spectra for an individual object. 
    Get the v_helios from the allStar catalog, which are more accurate than 
        the values reported in the visit spectra fits files. 
    Use the barycentric correction to shift spectra to the heliocentric frame.
    Do a preliminary normalization using Bovy's visit spectrum normalization tool. 
        It's critical that the spectra be renormalized prior to fitting using the 
        spectral_model.get_apogee_continuum() function for self-consistency. 
    apogee_id = byte-like object, i.e. b'2M06133561+2433362'
    
    ##### NOTE #####
    I had to make some minor changes to Bovy's APOGEE package to get it to 
    properly read in the visit spectra. In particular, after the 3rd line of 
    the function apVisit() in that package's /apogee/tools/read.py file, I added
    the following lines:
    
    if ext == 0:
        from astropy.io import fits
        hdulist = fits.open(filePath)
        header = hdulist[0].header 
        hdulist.close()
        return header
    
    I think this is the only change that was necessary, but it's possible I'm 
    forgetting another change. 
    '''
    import apogee.tools.read as apread
    from apogee.tools import bitmask
    from apogee.spec import continuum
    import astropy

    if allvisit_cat is None:
        allvisit_cat = apread.allVisit(rmcommissioning=False, ak=False)
    where_visits = np.where(allvisit_cat['APOGEE_ID'] == apogee_id)[0]

    plate_ids = np.array([int(i) for i in allvisit_cat[where_visits]['PLATE']])
    fiberids = allvisit_cat[where_visits]['FIBERID']
    mjds = allvisit_cat[where_visits]['MJD']
    JDs = allvisit_cat[where_visits]['JD']
    vhelios_accurate = allvisit_cat[where_visits]['VHELIO']
    vhelios_synth = allvisit_cat[where_visits]['SYNTHVHELIO']
    snrs = allvisit_cat[where_visits]['SNR']
    BCs = allvisit_cat[where_visits]['BC']

    badcombpixmask = bitmask.badpixmask() + 2**bitmask.apogee_pixmask_int(
        "SIG_SKYLINE")
    all_spec, all_err, all_snr, all_hjd, all_vhelio = [], [], [], [], []

    for i, pid in enumerate(plate_ids):
        try:
            spec = apread.apVisit(pid,
                                  mjds[i],
                                  fiberids[i],
                                  ext=1,
                                  header=False)
            specerr = apread.apVisit(pid,
                                     mjds[i],
                                     fiberids[i],
                                     ext=2,
                                     header=False)
            wave = apread.apVisit(pid,
                                  mjds[i],
                                  fiberids[i],
                                  ext=4,
                                  header=False)
            mask = apread.apVisit(pid,
                                  mjds[i],
                                  fiberids[i],
                                  ext=3,
                                  header=False)
            masterheader = apread.apVisit(pid,
                                          mjds[i],
                                          fiberids[i],
                                          ext=0,
                                          header=True)

            badpix = (mask & (badcombpixmask)) != 0
            if np.sum(badpix) / len(badpix) > 0.5:
                print('too many bad pixels!')
                continue  # if 50% or more of the pixels are bad, don't bother.
            specerr[badpix] = 100 * np.median(spec)

            # a small fraction of the visit spectra are on a different wavelength
            # grid than normal (maybe commissioning?). In any case, interpolate them
            # to the wavelength grid expected by Bovy's visit normalization routine.
            if len(wave) != 12288:
                print('fixing wavelength...')
                standard_grid = utils.load_visit_wavelength()
                spec = np.interp(standard_grid, wave, spec)
                specerr = np.interp(standard_grid, wave, specerr)
                wave = np.copy(standard_grid)

            # preliminary normalization using Bovy's visit normalization routine.
            cont = continuum.fitApvisit(spec, specerr, wave)
            specnorm, errnorm = spec / cont, specerr / cont

            # correct for Earth's orbital motion.
            spec_shift = utils.doppler_shift(wavelength=wave,
                                             flux=specnorm,
                                             dv=BCs[i])
            spec_err_shift = utils.doppler_shift(wavelength=wave,
                                                 flux=errnorm,
                                                 dv=BCs[i])

            # interpolate to the standard wavelength grid we use for combined spectra.
            interp_spec = np.interp(wavelength, wave, spec_shift)
            interp_err = np.interp(wavelength, wave, spec_err_shift)

            # truncate SNR at 200
            interp_err[interp_err < 0.005] = 0.005

            all_spec.append(interp_spec)
            all_err.append(interp_err)

            # Ideally, get the Julian date of the observations in the heliocentric frame.
            # Sometimes this isn't available; in that case, get the Julian date in Earth's
            # frame. These differ by at most 8 minutes, so not a big deal.
            try:
                all_hjd.append(masterheader['HJD'])
            except KeyError:
                all_hjd.append(JDs[i])

            # There are a few cases where the v_helios from the allvisit catalog are clearly wrong.
            if np.abs(vhelios_accurate[i] > 1000) and np.abs(
                    vhelios_synth[i] < 1000):
                vhel = vhelios_synth[i]
            else:
                vhel = vhelios_accurate[i]

            all_snr.append(snrs[i])
            all_vhelio.append(vhel)
        except astropy.io.fits.verify.VerifyError:
            print('there was a verification error')
            continue
    all_spec, all_err, all_snr, all_hjd, all_vhelio = np.array(all_spec), \
        np.array(all_err), np.array(all_snr), np.array(all_hjd), np.array(all_vhelio)
    msk = np.argsort(all_hjd)
    all_spec, all_err, all_snr, all_hjd, all_vhelio = all_spec[msk], all_err[msk], \
        all_snr[msk], all_hjd[msk], all_vhelio[msk]

    if save_local:
        np.savez('spectra/visit/visit_spectra_ap_id_' +
                 str(apogee_id.decode()) + '_.npz',
                 spectra=all_spec,
                 spec_errs=all_err,
                 snrs=all_snr,
                 hjds=all_hjd,
                 vhelios=all_vhelio)

    return all_spec, all_err, all_snr, all_hjd, all_vhelio
def read_caldata(filename='../clusterdata/aj485195t4_mrt.txt',dr='13'):
    """
    NAME:
       read_caldata
    PURPOSE:
       Read the data on calibration clusters from Meszaros et al. (2013)
    INPUT:
       filename= Name of the file that has the ApJ machine-readable table
    OUTPUT:
       data structure with the data
    HISTORY:
       2015-02-11 - Written - Bovy (IAS@KITP)
    """
    data= astropy.io.ascii.read(filename)
    data.rename_column('Cluster','CLUSTER')
    data.remove_column('Teff')
    data.rename_column('TeffC','TEFF')
    data.remove_column('logg')
    data.rename_column('loggC','LOGG')
    data.remove_column('[M/H]')
    data.rename_column('[M/H]C','FEH')
    data.rename_column('2MASS','ID')
    # Now match to allStar to get the location_ids
    alldata= apread.allStar(raw=True,dr=dr)
    locids= numpy.zeros(len(data),dtype='int')-1
    hmags= numpy.zeros(len(data),dtype='float')-1
    snrs = numpy.zeros(len(data),dtype='float')-1
    ras= numpy.zeros(len(data),dtype='float')-1
    decs= numpy.zeros(len(data),dtype='float')-1
    # and match to allVisit for the fibers that each star was observed in
    allvdata= apread.allVisit(raw=True,dr=dr)
    fibers= numpy.zeros((len(data),numpy.nanmax(alldata['NVISITS'])),
                        dtype='int')-1
    inds = []
    for ii in range(len(data)):
        if 'Pleiades' in data['CLUSTER'][ii]: 
            inds.append(0)
            continue
        indx= alldata['APOGEE_ID'] == data['ID'][ii]
        success = numpy.where(indx==True)[0]
        if success.size==0 or success.size>1:
            inds.append(0)
        elif success.size==1:
            inds.append(success[0])
        if numpy.sum(indx) == 0:
            raise ValueError('allStar match for %s not found ...' % (data['ID'][ii]))
        if len(list(set(alldata['LOCATION_ID'][indx]))) > 1:
            raise ValueError('Multiple matches found for for %s ...' % (data['ID'][ii]))
        locids[ii]= alldata['LOCATION_ID'][indx][0]
        hmags[ii]= alldata['H'][indx][0]
        snrs[ii] = alldata['SNR'][indx][0]
        ras[ii] = alldata['RA'][indx][0]
        decs[ii] = alldata['DEC'][indx][0]
        for jj in range(alldata['NVISITS'][indx][0]):
            fibers[ii,jj]= allvdata[alldata['VISIT_PK'][indx][0,jj]]['FIBERID']
    inds = (numpy.array(inds),)
    data['LOCATION_ID']= locids
    data['H']= hmags
    data['FIBERID']= fibers
    data['SNR'] = snrs
    data['APOGEE_ID'] = data['ID']
    data['RA'] = ras
    data['DEC'] = decs
    data['index'] = inds[0]
    data['M_H'] = data['FEH']
    data['FE_H'] = alldata['FE_H'][inds]
    if dr == '13':
        rel = 'FE'
    if dr != '13':
        rel = 'H'
    data['C_{0}'.format(rel)] = alldata['C_{0}'.format(rel)][inds]
    data['N_{0}'.format(rel)] = alldata['N_{0}'.format(rel)][inds]
    data['O_{0}'.format(rel)] = alldata['O_{0}'.format(rel)][inds]
    data['NA_{0}'.format(rel)] = alldata['NA_{0}'.format(rel)][inds]
    data['MG_{0}'.format(rel)] = alldata['MG_{0}'.format(rel)][inds]
    data['AL_{0}'.format(rel)] = alldata['AL_{0}'.format(rel)][inds]
    data['SI_{0}'.format(rel)] = alldata['SI_{0}'.format(rel)][inds]
    data['S_{0}'.format(rel)] = alldata['S_{0}'.format(rel)][inds]
    data['K_{0}'.format(rel)] = alldata['K_{0}'.format(rel)][inds]
    data['CA_{0}'.format(rel)] = alldata['CA_{0}'.format(rel)][inds]
    data['TI_{0}'.format(rel)] = alldata['TI_{0}'.format(rel)][inds]
    data['V_{0}'.format(rel)] = alldata['V_{0}'.format(rel)][inds]
    data['MN_{0}'.format(rel)] = alldata['MN_{0}'.format(rel)][inds]
    data['NI_{0}'.format(rel)] = alldata['NI_{0}'.format(rel)][inds]
    return data