コード例 #1
0
def load_apogee_distances(dr=None,
                          metric='distance',
                          cuts=True,
                          extinction=False):
    """
    Load apogee distances (absolute magnitude from stellar model)

    :param dr: Apogee DR
    :type dr: int
    :param metric: which metric you want ot get back

                   - "absmag" for absolute magnitude
                   - "fakemag" for fake magnitude
                   - "distance" for distance
    :type metric: string
    :param cuts: Whether to cut bad data (negative parallax and percentage error more than 20%), or a float to set the threshold
    :type cuts: Union[boolean, float]
    :param extinction: Whether to take extinction into account
    :type extinction: bool
    :return: numpy array of ra, dec, metrics_array, metrics_err_array
    :rtype: ndarrays
    :History: 2018-Jan-25 - Written - Henry Leung (University of Toronto)
    """
    fullfilename = apogee_distances(dr=dr)

    with fits.open(fullfilename) as F:
        hdulist = F[1].data
        # Convert kpc to pc
        distance = hdulist['BPG_dist50'] * 1000
        dist_err = (hdulist['BPG_dist84'] - hdulist['BPG_dist16']) * 1000

    allstarfullpath = allstar(dr=dr)

    with fits.open(allstarfullpath) as F:
        k_mag = F[1].data['K']
        if extinction:
            k_mag = extinction_correction(k_mag, F[1].data['AK_TARG'])
        ra = F[1].data['RA']
        dec = F[1].data['DEC']

    # Bad index refers to nan index
    bad_index = np.argwhere(np.isnan(distance))

    if metric == 'distance':
        # removed astropy units because of -9999. is dimensionless, will have issues
        output = distance
        output_err = dist_err

    elif metric == 'absmag':
        absmag, absmag_err = mag_to_absmag(k_mag, 1 / distance * u.arcsec,
                                           (1 / distance) *
                                           (dist_err / distance))
        output = absmag
        output_err = absmag_err

    elif metric == 'fakemag':
        # fakemag requires parallax (mas)
        fakemag, fakemag_err = mag_to_fakemag(k_mag, 1000 / distance * u.mas,
                                              (1000 / distance) *
                                              (dist_err / distance))
        output = fakemag
        output_err = fakemag_err

    else:
        raise ValueError('Unknown metric')

    # Set the nan index to -9999. as they are bad and unknown. Not magic_number as this is an APOGEE dataset
    output[bad_index], output_err[bad_index] = -9999., -9999.
    if cuts is False:
        pass
    else:
        distance[bad_index], dist_err[bad_index] = -9999., -9999.
        good_idx = [(dist_err / distance <
                     (0.2 if cuts is True else cuts)) & (distance != -9999.)]

        ra = ra[good_idx]
        dec = dec[good_idx]
        output = output[good_idx]
        output_err = output_err[good_idx]

    return ra, dec, output, output_err
コード例 #2
0
# This script defines global configuration/constant

from astroNN.apogee import allstar

# milkyway potential parameters
_R0 = 8.125  # distance to galactic center in kpc
_v0 = 220.  # sun tangential velocity in km/x
_z0 = 0.0208  # sun above galactic plane in kpc

# gaia table to use to cross matching
gaia_table_name = "gaiaedr3.gaia_source"

# path to the reference allstar (which is dr14 the models are trained on)
allstar14_path = allstar(dr=14)
# file name for dr14 continuum normalized spectra, can be a path if file is not stored in current dir
contspac14_file_name = "contspec_dr14.fits"
# base path to dr14apStar files
base14_path = "/yngve_data/sdss/apogee/dr14/apogee/spectro/redux/r8/stars"

# diff correction (calculate median diff to dr14, and subtract it from current dataset to make it looks more like dr14)
# will do north/south separately
corr_flag = True

# path to your current allstar
# allstar_path = "/yngve_data/sdss/apogee/apogeework/apogee/spectro/aspcap/r13/l33/allStar-r13-l33-58932beta.fits"
allstar_path = allstar(dr=17)  # path on my windows

# base path to apStar/asStar files
base_path = "/epsen_data/data_overflow/sdss/apogee/dr17/apogee/spectro/redux/dr17/stars/"
aspcap_base_path = "/epsen_data/data_overflow/sdss/apogee/dr17/apogee/spectro/aspcap/dr17/synspec/"
# base_path = "C:/sdss_local/dr16/apogee/spectro/redux/r12/stars"  # path on my windows
コード例 #3
0
ファイル: APOGEE.py プロジェクト: qthen/candle
	def create_data(self):
		local_path_to_file = allstar(dr=14)
		self.apogee_data = fits.open(local_path_to_file)
		self._PICKLE_DIR = "pickles/APOGEE/"
コード例 #4
0
    def create_data(self,
                    version=1,
                    max_number_of_stars=float("inf"),
                    use_steps=False):

        if os.path.exists('{}stars{}.pickle'.format(self._PICKLE_DIR,
                                                    version)):
            pickle_out = open(
                "{}stars{}.pickle".format(self._PICKLE_DIR, version), 'rb')
            self._star_dict = pickle.load(pickle_out)
            return True

        self._star_dict = {
            'KIC': [],
            'RA': [],
            'DEC': [],
            'Dnu': [],
            'PS': [],
            'e_PS': [],
            'q': [],
            'M': [],
            'spectra': [],
            'error_spectra': [],
            'T_eff': [],
            'logg': [],
            'RC': [],
            'status': [],
            'M_H': [],
            'C': [],
            'N': [],
            'O': [],
            'Na': [],
            'Mg': [],
            'Al': [],
            'Si': [],
            'P': [],
            'S': [],
            'K': [],
            'Ca': [],
            'Ti': [],
            'V': [],
            'Mn': [],
            'Fe': [],
            'Ni': [],
            'Cr': [],
            'Co': [],
            'Cl': []
        }

        # First create table of Kepler stars with PS and Δv with their RA, DEC (creates table1.dat)
        self._populate_kepler_with_rc_dec()

        # Loading in APOGEE star data (RA, DEC)
        local_path_to_file = allstar(dr=14)
        apogee_data = fits.open(local_path_to_file)
        apogee_ra = apogee_data[1].data['RA']
        apogee_de = apogee_data[1].data['DEC']

        # RA, DEC from KIC table
        kic_table = Table.read("tables/KIC_A87/table1.dat", format="ascii")
        kic_ra = np.array(kic_table['RA'])
        kic_de = np.array(kic_table['DE'])

        # Indices overlap between KIC and APOGEE
        idx_kic, idx_apogee, sep = xmatch(kic_ra,
                                          apogee_ra,
                                          colRA1=kic_ra,
                                          colDec1=kic_de,
                                          colRA2=apogee_ra,
                                          colDec2=apogee_de)

        # Building spectra data for KIC from APOGEE overlaps
        for i in range(0, len(idx_apogee)):
            index_in_apogee = idx_apogee[i]
            index_in_kepler = idx_kic[i]

            if version == 1 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0 and
                    apogee_data[1].data['ASPCAPFLAG'][index_in_apogee] == 0):
                continue

            # Version 2 - Subject to the following constraints
            # Flag checking, condition is the same as Hawkins et al. 2017, STARFLAG and ASPCAP bitwise OR is 0
            # KIC checking, uncertainty in PS should be less than 10s
            if version == 2 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0
                    and apogee_data[1].data['ASPCAPFLAG'][index_in_apogee] == 0
                    and kic_table['e_DPi1'][index_in_kepler] < 10):
                continue

            # Version 3 - Subject to the following constraints
            # Flag checking, condition is the same as Hawkins et al. 2017, STARFLAG and ASPCAP bitwise OR is 0
            # KIC checking, uncertainty in PS should be less than 10s
            if version == 3 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0
                    and apogee_data[1].data['ASPCAPFLAG'][index_in_apogee] == 0
                    and kic_table['e_DPi1'][index_in_kepler] < 10
                    and apogee_data[1].data['SNR'][index_in_apogee] >= 200):
                continue

            # Version 5 - DR13 data subject to same connstraints as Hawkings
            if version == 5 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0
                    and apogee_data[1].data['ASPCAPFLAG'][index_in_apogee] == 0
                    and kic_table['e_DPi1'][index_in_kepler] < 10):
                continue

            # Version 6 - DR13 data subject to same connstraints as Hawkings - normalization with bitmask as DR13
            if version == 6 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0
                    and apogee_data[1].data['ASPCAPFLAG'][index_in_apogee] == 0
                    and kic_table['e_DPi1'][index_in_kepler] < 10
                    and apogee_data[1].data['NVISITS'][index_in_apogee] >= 1):
                continue

            # Version 7 - DR13 data subject to same constraints as Hawkings - no bit mask as DR13
            if version == 7 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0
                    and kic_table['e_DPi1'][index_in_kepler] < 10
                    and apogee_data[1].data['NVISITS'][index_in_apogee] >= 1):
                continue

            # Version 8 - DR13 data subject to same constraints as Hawkings
            if version == 8 and not (
                    apogee_data[1].data['STARFLAG'][index_in_apogee] == 0
                    and kic_table['e_DPi1'][index_in_kepler] < 10
                    and apogee_data[1].data['NVISITS'][index_in_apogee] >= 1):
                continue

            a_apogee_id = apogee_data[1].data['APOGEE_ID'][index_in_apogee]
            a_location_id = apogee_data[1].data['LOCATION_ID'][index_in_apogee]

            local_path_to_file_for_star = None
            if version == 6 or version == 7 or version == 8:
                local_path_to_file_for_star = visit_spectra(
                    dr=14, location=a_location_id, apogee=a_apogee_id)
            elif version == 4 or version == 5:
                local_path_to_file_for_star = combined_spectra(
                    dr=13, location=a_location_id, apogee=a_apogee_id)
            else:
                local_path_to_file_for_star = combined_spectra(
                    dr=14, location=a_location_id, apogee=a_apogee_id)

            if (local_path_to_file_for_star):
                # Filter out the dr=14 gaps, value to be appended to the array
                spectra_no_gap = None
                error_spectra = None
                if version in (6, 7, 8):
                    # Read from visit spectra of the star
                    spectra_data = fits.open(local_path_to_file_for_star)
                    spectra, mask_spectra = None, None

                    # Best fit spectra data - use for spectra data
                    if apogee_data[1].data['NVISITS'][index_in_apogee] == 1:
                        spectra = spectra_data[1].data
                        error_spectra = spectra_data[2].data
                        mask_spectra = spectra_data[3].data
                    elif apogee_data[1].data['NVISITS'][index_in_apogee] > 1:
                        spectra = spectra_data[1].data[1]
                        error_spectra = spectra_data[2].data[1]
                        mask_spectra = spectra_data[3].data[1]

                    if version == 6:
                        norm_spec, norm_spec_err = apogee_continuum(
                            spectra,
                            error_spectra,
                            cont_mask=None,
                            deg=2,
                            dr=13,
                            bitmask=mask_spectra,
                            target_bit=None)
                        spectra_no_gap = norm_spec
                        error_spectra = norm_spec_err
                    elif version == 7:
                        norm_spec, norm_spec_err = apogee_continuum(
                            spectra,
                            error_spectra,
                            cont_mask=None,
                            deg=2,
                            dr=13,
                            bitmask=None,
                            target_bit=None)
                        spectra_no_gap = norm_spec
                        error_spectra = norm_spec_err
                    elif version == 8:
                        # Bit mask value is now 1
                        norm_spec, norm_spec_err = apogee_continuum(
                            spectra,
                            error_spectra,
                            cont_mask=None,
                            deg=2,
                            dr=13,
                            bitmask=mask_spectra,
                            target_bit=None,
                            mask_value=1)
                        spectra_no_gap = norm_spec
                        error_spectra = norm_spec_err
                    del mask_spectra

                elif version == 4 or version == 5:
                    spectra_data = fits.open(local_path_to_file_for_star)

                    # Best fit spectra data - use for spectra data
                    spectra = spectra_data[3].data
                    spectra_no_gap = gap_delete(spectra, dr=13)
                else:
                    spectra_data = fits.open(local_path_to_file_for_star)

                    # Best fit spectra data - use for spectra data
                    spectra = spectra_data[3].data

                    spectra_no_gap = gap_delete(spectra, dr=14)

                spectra_no_gap = spectra_no_gap.flatten()

                # APOGEE data
                self._star_dict['T_eff'].append(
                    apogee_data[1].data['Teff'][index_in_apogee].copy())
                self._star_dict['logg'].append(
                    apogee_data[1].data['logg'][index_in_apogee].copy())

                # Metals/H
                self._star_dict['M_H'].append(
                    apogee_data[1].data['M_H'][index_in_apogee])
                self._star_dict['C'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][0])
                self._star_dict['N'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][2])
                self._star_dict['O'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][3])
                self._star_dict['Na'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][4])
                self._star_dict['Mg'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][5])
                self._star_dict['Al'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][6])
                self._star_dict['Si'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][7])
                self._star_dict['P'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][8])
                self._star_dict['S'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][9])
                self._star_dict['K'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][10])
                self._star_dict['Ca'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][11])
                self._star_dict['Ti'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][12])
                self._star_dict['V'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][14])
                self._star_dict['Mn'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][16])
                self._star_dict['Fe'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][17])
                self._star_dict['Ni'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][19])
                self._star_dict['Cr'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][15])
                self._star_dict['Co'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][18])
                self._star_dict['Cl'].append(
                    apogee_data[1].data['X_H'][index_in_apogee][1])

                # KIC data
                self._star_dict['KIC'].append(
                    kic_table['KIC'][index_in_kepler])
                self._star_dict['RA'].append(kic_table['RA'][index_in_kepler])
                self._star_dict['DEC'].append(kic_table['DE'][index_in_kepler])
                self._star_dict['Dnu'].append(
                    kic_table['Dnu'][index_in_kepler])
                self._star_dict['PS'].append(
                    kic_table['DPi1'][index_in_kepler])
                self._star_dict['e_PS'].append(
                    kic_table['e_DPi1'][index_in_kepler])
                self._star_dict['q'].append(kic_table['q'][index_in_kepler])
                self._star_dict['M'].append(kic_table['M'][index_in_kepler])
                self._star_dict['status'].append(
                    kic_table['Status'][index_in_kepler])
                self._star_dict['RC'].append(
                    self.is_red_clump(kic_table['DPi1'][index_in_kepler],
                                      kic_table['Dnu'][index_in_kepler]))

                # Gap delete doesn't return row vector, need to manually reshape
                self._star_dict['spectra'].append(spectra_no_gap)

                if version in (6, 7, 8):
                    self._star_dict['error_spectra'].append(
                        error_spectra.flatten())
                    del error_spectra

                # Close file handler
                del spectra_data
                del spectra

            # Check max condition
            if i > max_number_of_stars - 1:
                break

        # Convert to numpy arrays
        self._star_dict['KIC'] = np.array(self._star_dict['KIC'])
        self._star_dict['RA'] = np.array(self._star_dict['RA'])
        self._star_dict['DEC'] = np.array(self._star_dict['DEC'])
        self._star_dict['Dnu'] = np.array(self._star_dict['Dnu'])
        self._star_dict['PS'] = np.array(self._star_dict['PS'])
        self._star_dict['e_PS'] = np.array(self._star_dict['e_PS'])
        self._star_dict['spectra'] = np.array(self._star_dict['spectra'])
        self._star_dict['logg'] = np.array(self._star_dict['logg'])
        self._star_dict['T_eff'] = np.array(self._star_dict['T_eff'])
        self._star_dict['RC'] = np.array(self._star_dict['RC'])
        self._star_dict['status'] = np.array(self._star_dict['status'])
        self._star_dict['M'] = np.array(self._star_dict['M'])
        self._star_dict['M_H'] = np.array(self._star_dict['M_H'])
        self._star_dict['C'] = np.array(self._star_dict['C'])
        self._star_dict['N'] = np.array(self._star_dict['N'])
        self._star_dict['O'] = np.array(self._star_dict['O'])
        self._star_dict['Na'] = np.array(self._star_dict['Na'])
        self._star_dict['Mg'] = np.array(self._star_dict['Mg'])
        self._star_dict['Al'] = np.array(self._star_dict['Al'])
        self._star_dict['Si'] = np.array(self._star_dict['Si'])
        self._star_dict['P'] = np.array(self._star_dict['P'])
        self._star_dict['S'] = np.array(self._star_dict['S'])
        self._star_dict['K'] = np.array(self._star_dict['K'])
        self._star_dict['Ca'] = np.array(self._star_dict['Ca'])
        self._star_dict['Ti'] = np.array(self._star_dict['Ti'])
        self._star_dict['V'] = np.array(self._star_dict['V'])
        self._star_dict['Mn'] = np.array(self._star_dict['Mn'])
        self._star_dict['Fe'] = np.array(self._star_dict['Fe'])
        self._star_dict['Ni'] = np.array(self._star_dict['Ni'])
        self._star_dict['Cl'] = np.array(self._star_dict['Cl'])
        self._star_dict['Cr'] = np.array(self._star_dict['Cr'])
        self._star_dict['Co'] = np.array(self._star_dict['Co'])
        self._star_dict['q'] = np.array(self._star_dict['q'])

        if version == 6:
            self._star_dict['error_spectra'] = np.array(
                self._star_dict['error_spectra'])

        # Pickle for caching
        pickle_out = open("{}stars{}.pickle".format(self._PICKLE_DIR, version),
                          'wb')
        pickle.dump(self._star_dict, pickle_out)
        pickle_out.close()

        return True
コード例 #5
0
 def load_allstar(self):
     self.apogee_dr = apogee_default_dr(dr=self.apogee_dr)
     allstarpath = allstar(dr=self.apogee_dr)
     hdulist = fits.open(allstarpath)
     print(f'Loading allStar DR{self.apogee_dr} catalog')
     return hdulist