示例#1
0
    def test_extinction(self):
        from astroNN.gaia import extinction_correction

        self.assertEqual(np.any([extinction_correction(10., -90.) == -9999.]),
                         False)
        self.assertEqual(extinction_correction(10., -90.), 10.)
        self.assertEqual(
            np.any([extinction_correction(-99.99, -90.) == -9999.]), True)
示例#2
0
def load_apogee_rc(dr=None, metric='distance', extinction=True):
    """
    Load apogee red clumps (absolute magnitude measurement)

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

                   - "absmag" for k-band absolute magnitude
                   - "fakemag" for k-band fake magnitude
                   - "distance" for distance in parsec
    :type metric: string
    :param extinction: Whether to take extinction into account, only affect when metric is NOT 'distance'
    :type extinction: bool
    :return: numpy array of ra, dec, metrics_array
    :rtype: ndarrays
    :History:
        | 2018-Jan-21 - Written - Henry Leung (University of Toronto)
        | 2018-May-12 - Updated - Henry Leung (University of Toronto)
    """
    fullfilename = apogee_vac_rc(dr=dr)

    with fits.open(fullfilename) as F:
        hdulist = F[1].data
        ra = hdulist['RA']
        dec = hdulist['DEC']
        rc_dist = hdulist['RC_DIST']
        rc_parallax = (1 / rc_dist) * u.mas  # Convert kpc to parallax in mas
        k_mag = hdulist['K']
        if extinction:
            k_mag = extinction_correction(k_mag, hdulist['AK_TARG'])

    if metric == 'distance':
        output = rc_dist * 1000

    elif metric == 'absmag':
        absmag = mag_to_absmag(k_mag, rc_parallax)
        output = absmag

    elif metric == 'fakemag':
        # fakemag requires parallax (mas)
        fakemag = mag_to_fakemag(k_mag, rc_parallax)
        output = fakemag

    else:
        raise ValueError('Unknown metric')

    return ra, dec, output
示例#3
0
                format="18A"),
    fits.Column(name="LOCATION_ID",
                array=allstar_data["LOCATION_ID"],
                format="J"),
    fits.Column(name="RA", array=allstar_data["RA"], format="D"),
    fits.Column(name="DEC", array=allstar_data["DEC"], format="D"),
    fits.Column(name="astroNN", array=pred, format="22E"),
    fits.Column(name="astroNN_error", array=pred_error["total"], format="22E"),
]

t = fits.BinTableHDU.from_columns(columns_list)
t.writeto(astronn_chem_f)

# ====================================== Distance ====================================== #

corrected_K = extinction_correction(allstar_data["K"], allstar_data["AK_TARG"])

# cross matched APOGEE-Gaia
apogeegaia_file = fits.getdata(gaia_rowmatch_f)
# add the offset we found for inv var weighting parallax
try:
    parallax = apogeegaia_file["parallax_w_zp"]
except KeyError:
    parallax = apogeegaia_file["parallax"]

parallax_error = apogeegaia_file["parallax_error"]

# set negative parallax after constant offset correction to np.nan
parallax_error[(parallax < 0) & (parallax > 1e10)] = np.nan
parallax[(parallax < 0) & (parallax > 1e10)] = np.nan
示例#4
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
示例#5
0
                format="18A"),
    fits.Column(name='LOCATION_ID',
                array=allstar_data['LOCATION_ID'],
                format="J"),
    fits.Column(name='RA', array=allstar_data['RA'], format='D'),
    fits.Column(name='DEC', array=allstar_data['DEC'], format='D'),
    fits.Column(name='astroNN', array=pred, format='22E'),
    fits.Column(name='astroNN_error', array=pred_error['total'], format='22E')
]

t = fits.BinTableHDU.from_columns(columns_list)
t.writeto(astronn_chem_f)

# ====================================== Distance ====================================== #

corrected_K = extinction_correction(allstar_data['K'], allstar_data['AK_TARG'])

# cross matched APOGEE-Gaia DR2
apogeegaia_file = fits.getdata(gaia_rowmatch_f)
# add the offset we found for inv var weighting parallax
parallax = apogeegaia_file["parallax"] + 0.052
parallax_error = apogeegaia_file["parallax_error"]

# set negative parallax after constant offset correction to np.nan
parallax[(parallax < 0)] = np.nan
parallax_error[(parallax < 0)] = np.nan

# inference
net = load_folder(astronn_dist_model)
pred, pred_err = net.test(all_spec)
pred[:, 0][pred[:, 0] == 0.] = np.nan
示例#6
0
    def compile(self):
        h5name_check(self.filename)

        hdulist = self.load_allstar()
        indices = self.filter_apogeeid_list(hdulist)

        info = chips_pix_info(dr=self.apogee_dr)
        total_pix = (info[1] - info[0]) + (info[3] - info[2]) + (info[5] -
                                                                 info[4])
        default_length = 900000

        spec = np.zeros((default_length, total_pix), dtype=np.float32)
        spec_err = np.zeros((default_length, total_pix), dtype=np.float32)
        RA = np.zeros(default_length, dtype=np.float32)
        DEC = np.zeros(default_length, dtype=np.float32)
        SNR = np.zeros(default_length, dtype=np.float32)
        individual_flag = np.zeros(default_length, dtype=np.float32)
        Kmag = np.zeros(default_length, dtype=np.float32)
        AK_TARG = np.zeros(default_length, dtype=np.float32)

        # Data array
        teff = np.zeros(default_length, dtype=np.float32)
        logg = np.zeros(default_length, dtype=np.float32)
        MH = np.zeros(default_length, dtype=np.float32)
        alpha_M = np.zeros(default_length, dtype=np.float32)
        C = np.zeros(default_length, dtype=np.float32)
        C1 = np.zeros(default_length, dtype=np.float32)
        N = np.zeros(default_length, dtype=np.float32)
        O = np.zeros(default_length, dtype=np.float32)
        Na = np.zeros(default_length, dtype=np.float32)
        Mg = np.zeros(default_length, dtype=np.float32)
        Al = np.zeros(default_length, dtype=np.float32)
        Si = np.zeros(default_length, dtype=np.float32)
        P = np.zeros(default_length, dtype=np.float32)
        S = np.zeros(default_length, dtype=np.float32)
        K = np.zeros(default_length, dtype=np.float32)
        Ca = np.zeros(default_length, dtype=np.float32)
        Ti = np.zeros(default_length, dtype=np.float32)
        Ti2 = np.zeros(default_length, dtype=np.float32)
        V = np.zeros(default_length, dtype=np.float32)
        Cr = np.zeros(default_length, dtype=np.float32)
        Mn = np.zeros(default_length, dtype=np.float32)
        Fe = np.zeros(default_length, dtype=np.float32)
        Co = np.zeros(default_length, dtype=np.float32)
        Ni = np.zeros(default_length, dtype=np.float32)
        Cu = np.zeros(default_length, dtype=np.float32)
        Ge = np.zeros(default_length, dtype=np.float32)
        Ce = np.zeros(default_length, dtype=np.float32)
        Rb = np.zeros(default_length, dtype=np.float32)
        Y = np.zeros(default_length, dtype=np.float32)
        Nd = np.zeros(default_length, dtype=np.float32)
        parallax = np.zeros(default_length, dtype=np.float32)
        fakemag = np.zeros(default_length, dtype=np.float32)

        # Error array
        teff_err = np.zeros(default_length, dtype=np.float32)
        logg_err = np.zeros(default_length, dtype=np.float32)
        MH_err = np.zeros(default_length, dtype=np.float32)
        alpha_M_err = np.zeros(default_length, dtype=np.float32)
        C_err = np.zeros(default_length, dtype=np.float32)
        C1_err = np.zeros(default_length, dtype=np.float32)
        N_err = np.zeros(default_length, dtype=np.float32)
        O_err = np.zeros(default_length, dtype=np.float32)
        Na_err = np.zeros(default_length, dtype=np.float32)
        Mg_err = np.zeros(default_length, dtype=np.float32)
        Al_err = np.zeros(default_length, dtype=np.float32)
        Si_err = np.zeros(default_length, dtype=np.float32)
        P_err = np.zeros(default_length, dtype=np.float32)
        S_err = np.zeros(default_length, dtype=np.float32)
        K_err = np.zeros(default_length, dtype=np.float32)
        Ca_err = np.zeros(default_length, dtype=np.float32)
        Ti_err = np.zeros(default_length, dtype=np.float32)
        Ti2_err = np.zeros(default_length, dtype=np.float32)
        V_err = np.zeros(default_length, dtype=np.float32)
        Cr_err = np.zeros(default_length, dtype=np.float32)
        Mn_err = np.zeros(default_length, dtype=np.float32)
        Fe_err = np.zeros(default_length, dtype=np.float32)
        Co_err = np.zeros(default_length, dtype=np.float32)
        Ni_err = np.zeros(default_length, dtype=np.float32)
        Cu_err = np.zeros(default_length, dtype=np.float32)
        Ge_err = np.zeros(default_length, dtype=np.float32)
        Ce_err = np.zeros(default_length, dtype=np.float32)
        Rb_err = np.zeros(default_length, dtype=np.float32)
        Y_err = np.zeros(default_length, dtype=np.float32)
        Nd_err = np.zeros(default_length, dtype=np.float32)
        parallax_err = np.zeros(default_length, dtype=np.float32)
        fakemag_err = np.zeros(default_length, dtype=np.float32)

        array_counter = 0

        start_time = time.time()

        # provide a cont mask so no need to read every loop
        if self.cont_mask is None:
            maskpath = os.path.join(astroNN.data.datapath(),
                                    f'dr{self.apogee_dr}_contmask.npy')
            self.cont_mask = np.load(maskpath)

        for counter, index in enumerate(indices):
            nvisits = 1
            apogee_id = hdulist[1].data['APOGEE_ID'][index]
            location_id = hdulist[1].data['LOCATION_ID'][index]
            if counter % 100 == 0:
                print(
                    f'Completed {counter + 1} of {indices.shape[0]}, {(time.time() - start_time):.{2}f}s elapsed'
                )
            if not self.continuum:
                path = combined_spectra(dr=self.apogee_dr,
                                        location=location_id,
                                        apogee=apogee_id,
                                        verbose=0)
                if path is False:
                    # if path is not found then we should skip
                    continue
                combined_file = fits.open(path)
                _spec = combined_file[
                    1].data  # Pseudo-continuum normalized flux
                _spec_err = combined_file[2].data  # Spectrum error array
                _spec = gap_delete(
                    _spec, dr=self.apogee_dr)  # Delete the gap between sensors
                _spec_err = gap_delete(_spec_err, dr=self.apogee_dr)
                inSNR = combined_file[0].header['SNR']
                combined_file.close()
            else:
                path = visit_spectra(dr=self.apogee_dr,
                                     location=location_id,
                                     apogee=apogee_id,
                                     verbose=0)
                if path is False:
                    # if path is not found then we should skip
                    continue
                apstar_file = fits.open(path)
                nvisits = apstar_file[0].header['NVISITS']
                if nvisits == 1:
                    _spec = apstar_file[1].data
                    _spec_err = apstar_file[2].data
                    _spec_mask = apstar_file[3].data
                    inSNR = np.ones(nvisits)
                    inSNR[0] = apstar_file[0].header['SNR']
                else:
                    _spec = apstar_file[1].data[1:]
                    _spec_err = apstar_file[2].data[1:]
                    _spec_mask = apstar_file[3].data[1:]
                    inSNR = np.ones(nvisits + 1)
                    inSNR[0] = apstar_file[0].header['SNR']
                    for i in range(nvisits):
                        inSNR[i + 1] = apstar_file[0].header[f'SNRVIS{i + 1}']

                    # Deal with spectra thats all zeros flux
                    ii = 0
                    while ii < _spec.shape[0]:
                        if np.count_nonzero(_spec[ii]) == 0:
                            nvisits -= 1
                            _spec = np.delete(_spec, ii, 0)
                            _spec_err = np.delete(_spec_err, ii, 0)
                            _spec_mask = np.delete(_spec_mask, ii, 0)
                            inSNR = np.delete(inSNR, ii, 0)
                            ii -= 1
                        ii += 1

                    # Just for the sake of program to work, the real nvisits still nvisits
                    nvisits += 1

                # Normalize spectra and Set some bitmask to 0
                _spec, _spec_err = self.apstar_normalization(
                    _spec, _spec_err, _spec_mask)
                apstar_file.close()

            if nvisits == 1:
                individual_flag[array_counter:array_counter + nvisits] = 0
            else:
                individual_flag[array_counter:array_counter + 1] = 0
                individual_flag[array_counter + 1:array_counter + nvisits] = 1
            spec[array_counter:array_counter + nvisits, :] = _spec
            spec_err[array_counter:array_counter + nvisits, :] = _spec_err
            SNR[array_counter:array_counter + nvisits] = inSNR
            RA[array_counter:array_counter + nvisits] = np.tile(
                hdulist[1].data['RA'][index], nvisits)
            DEC[array_counter:array_counter + nvisits] = np.tile(
                hdulist[1].data['DEC'][index], nvisits)
            parallax[array_counter:array_counter + nvisits] = np.tile(
                -9999, nvisits)
            parallax_err[array_counter:array_counter + nvisits] = np.tile(
                -9999, nvisits)
            fakemag[array_counter:array_counter + nvisits] = np.tile(
                -9999, nvisits)
            fakemag_err[array_counter:array_counter + nvisits] = np.tile(
                -9999, nvisits)
            Kmag[array_counter:array_counter + nvisits] = np.tile(
                hdulist[1].data['K'][index], nvisits)
            AK_TARG[array_counter:array_counter + nvisits] = np.tile(
                hdulist[1].data['AK_TARG'][index], nvisits)

            if self.spectra_only is not True:
                teff[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['PARAM'][index, 0], nvisits)
                logg[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['PARAM'][index, 1], nvisits)
                MH[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['PARAM'][index, 3], nvisits)
                alpha_M[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['PARAM'][index, 6], nvisits)
                C[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 0], nvisits)
                C1[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 1], nvisits)
                N[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 2], nvisits)
                O[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 3], nvisits)
                Na[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 4], nvisits)
                Mg[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 5], nvisits)
                Al[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 6], nvisits)
                Si[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 7], nvisits)
                P[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 8], nvisits)
                S[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 9], nvisits)
                K[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 10], nvisits)
                Ca[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 11], nvisits)
                Ti[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 12], nvisits)
                Ti2[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 13], nvisits)
                V[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 14], nvisits)
                Cr[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 15], nvisits)
                Mn[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 16], nvisits)
                Fe[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 17], nvisits)
                Co[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 18], nvisits)
                Ni[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 19], nvisits)
                Cu[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 20], nvisits)
                Ge[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 21], nvisits)
                Ce[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 22], nvisits)
                Rb[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 23], nvisits)
                Y[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 24], nvisits)
                Nd[array_counter:array_counter + nvisits] = np.tile(
                    hdulist[1].data['X_H'][index, 25], nvisits)

                if self.use_err is True:
                    teff_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['TEFF_ERR'][index], nvisits)
                    logg_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['LOGG_ERR'][index], nvisits)
                    MH_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['M_H_ERR'][index], nvisits)
                    alpha_M_err[array_counter:array_counter +
                                nvisits] = np.tile(
                                    hdulist[1].data['ALPHA_M_ERR'][index],
                                    nvisits)
                    C_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 0], nvisits)
                    C1_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 1], nvisits)
                    N_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 2], nvisits)
                    O_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 3], nvisits)
                    Na_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 4], nvisits)
                    Mg_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 5], nvisits)
                    Al_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 6], nvisits)
                    Si_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 7], nvisits)
                    P_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 8], nvisits)
                    S_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 9], nvisits)
                    K_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 10], nvisits)
                    Ca_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 11], nvisits)
                    Ti_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 12], nvisits)
                    Ti2_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 13], nvisits)
                    V_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 14], nvisits)
                    Cr_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 15], nvisits)
                    Mn_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 16], nvisits)
                    Fe_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 17], nvisits)
                    Co_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 18], nvisits)
                    Ni_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 19], nvisits)
                    Cu_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 20], nvisits)
                    Ge_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 21], nvisits)
                    Ce_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 22], nvisits)
                    Rb_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 23], nvisits)
                    Y_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 24], nvisits)
                    Nd_err[array_counter:array_counter + nvisits] = np.tile(
                        hdulist[1].data['X_H_ERR'][index, 25], nvisits)
            array_counter += nvisits

        spec = spec[0:array_counter]
        spec_err = spec_err[0:array_counter]
        individual_flag = individual_flag[0:array_counter]
        RA = RA[0:array_counter]
        DEC = DEC[0:array_counter]
        SNR = SNR[0:array_counter]

        if self.spectra_only is not True:
            teff = teff[0:array_counter]
            logg = logg[0:array_counter]
            Kmag = Kmag[0:array_counter]
            AK_TARG = AK_TARG[0:array_counter]
            MH = MH[0:array_counter]
            alpha_M = alpha_M[0:array_counter]
            C = C[0:array_counter]
            C1 = C1[0:array_counter]
            N = N[0:array_counter]
            O = O[0:array_counter]
            Na = Na[0:array_counter]
            Mg = Mg[0:array_counter]
            Al = Al[0:array_counter]
            Si = Si[0:array_counter]
            P = P[0:array_counter]
            S = S[0:array_counter]
            K = K[0:array_counter]
            Ca = Ca[0:array_counter]
            Ti = Ti[0:array_counter]
            Ti2 = Ti2[0:array_counter]
            V = V[0:array_counter]
            Cr = Cr[0:array_counter]
            Mn = Mn[0:array_counter]
            Fe = Fe[0:array_counter]
            Co = Co[0:array_counter]
            Ni = Ni[0:array_counter]
            Cu = Cu[0:array_counter]
            Ge = Ge[0:array_counter]
            Ce = Ce[0:array_counter]
            Rb = Rb[0:array_counter]
            Y = Y[0:array_counter]
            Nd = Nd[0:array_counter]
            parallax = parallax[0:array_counter]
            fakemag = fakemag[0:array_counter]

            teff_err = teff_err[0:array_counter]
            logg_err = logg_err[0:array_counter]
            MH_err = MH_err[0:array_counter]
            alpha_M_err = alpha_M_err[0:array_counter]
            C_err = C_err[0:array_counter]
            C1_err = C1_err[0:array_counter]
            N_err = N_err[0:array_counter]
            O_err = O_err[0:array_counter]
            Na_err = Na_err[0:array_counter]
            Mg_err = Mg_err[0:array_counter]
            Al_err = Al_err[0:array_counter]
            Si_err = Si_err[0:array_counter]
            P_err = P_err[0:array_counter]
            S_err = S_err[0:array_counter]
            K_err = K_err[0:array_counter]
            Ca_err = Ca_err[0:array_counter]
            Ti_err = Ti_err[0:array_counter]
            Ti2_err = Ti2_err[0:array_counter]
            V_err = V_err[0:array_counter]
            Cr_err = Cr_err[0:array_counter]
            Mn_err = Mn_err[0:array_counter]
            Fe_err = Fe_err[0:array_counter]
            Co_err = Co_err[0:array_counter]
            Ni_err = Ni_err[0:array_counter]
            Cu_err = Cu_err[0:array_counter]
            Ge_err = Ge_err[0:array_counter]
            Ce_err = Ce_err[0:array_counter]
            Rb_err = Rb_err[0:array_counter]
            Y_err = Y_err[0:array_counter]
            Nd_err = Nd_err[0:array_counter]
            parallax_err = parallax_err[0:array_counter]
            fakemag_err = fakemag_err[0:array_counter]

            if self.use_esa_gaia is True:
                gaia_ra, gaia_dec, gaia_parallax, gaia_err = gaiadr2_parallax(
                    cuts=True, keepdims=False)
                m1, m2, sep = xmatch(RA,
                                     gaia_ra,
                                     maxdist=2,
                                     colRA1=RA,
                                     colDec1=DEC,
                                     colRA2=gaia_ra,
                                     colDec2=gaia_dec,
                                     swap=False)
                parallax[m1] = gaia_parallax[m2]
                parallax_err[m1] = gaia_err[m2]
                fakemag[m1], fakemag_err[m1] = mag_to_fakemag(
                    extinction_correction(Kmag[m1], AK_TARG[m1]), parallax[m1],
                    parallax_err[m1])
            elif self.use_anderson_2017 is True:
                gaia_ra, gaia_dec, gaia_parallax, gaia_err = anderson_2017_parallax(
                )
                m1, m2, sep = xmatch(RA,
                                     gaia_ra,
                                     maxdist=2,
                                     colRA1=RA,
                                     colDec1=DEC,
                                     epoch1=2000.,
                                     colRA2=gaia_ra,
                                     colDec2=gaia_dec,
                                     epoch2=2000.,
                                     swap=False)
                parallax[m1] = gaia_parallax[m2]
                parallax_err[m1] = gaia_err[m2]
                fakemag[m1], fakemag_err[m1] = mag_to_fakemag(
                    extinction_correction(Kmag[m1], AK_TARG[m1]), parallax[m1],
                    parallax_err[m1])

        print(f'Creating {self.filename}.h5')
        h5f = h5py.File(f'{self.filename}.h5', 'w')
        h5f.create_dataset('spectra', data=spec)
        h5f.create_dataset('spectra_err', data=spec_err)
        h5f.create_dataset('in_flag', data=individual_flag)
        h5f.create_dataset('index', data=indices)

        if self.spectra_only is not True:
            h5f.create_dataset('SNR', data=SNR)
            h5f.create_dataset('RA', data=RA)
            h5f.create_dataset('DEC', data=DEC)
            h5f.create_dataset('Kmag', data=Kmag)
            h5f.create_dataset('AK_TARG', data=AK_TARG)
            h5f.create_dataset('teff', data=teff)
            h5f.create_dataset('logg', data=logg)
            h5f.create_dataset('M', data=MH)
            h5f.create_dataset('alpha', data=alpha_M)
            h5f.create_dataset('C', data=C)
            h5f.create_dataset('C1', data=C1)
            h5f.create_dataset('N', data=N)
            h5f.create_dataset('O', data=O)
            h5f.create_dataset('Na', data=Na)
            h5f.create_dataset('Mg', data=Mg)
            h5f.create_dataset('Al', data=Al)
            h5f.create_dataset('Si', data=Si)
            h5f.create_dataset('P', data=P)
            h5f.create_dataset('S', data=S)
            h5f.create_dataset('K', data=K)
            h5f.create_dataset('Ca', data=Ca)
            h5f.create_dataset('Ti', data=Ti)
            h5f.create_dataset('Ti2', data=Ti2)
            h5f.create_dataset('V', data=V)
            h5f.create_dataset('Cr', data=Cr)
            h5f.create_dataset('Mn', data=Mn)
            h5f.create_dataset('Fe', data=Fe)
            h5f.create_dataset('Co', data=Co)
            h5f.create_dataset('Ni', data=Ni)
            h5f.create_dataset('Cu', data=Cu)
            h5f.create_dataset('Ge', data=Ge)
            h5f.create_dataset('Ce', data=Ce)
            h5f.create_dataset('Rb', data=Rb)
            h5f.create_dataset('Y', data=Y)
            h5f.create_dataset('Nd', data=Nd)
            h5f.create_dataset('parallax', data=parallax)
            h5f.create_dataset('fakemag', data=fakemag)

            if self.use_err is True:
                h5f.create_dataset('AK_TARG_err', data=np.zeros_like(AK_TARG))
                h5f.create_dataset('teff_err', data=teff_err)
                h5f.create_dataset('logg_err', data=logg_err)
                h5f.create_dataset('M_err', data=MH_err)
                h5f.create_dataset('alpha_err', data=alpha_M_err)
                h5f.create_dataset('C_err', data=C_err)
                h5f.create_dataset('C1_err', data=C1_err)
                h5f.create_dataset('N_err', data=N_err)
                h5f.create_dataset('O_err', data=O_err)
                h5f.create_dataset('Na_err', data=Na_err)
                h5f.create_dataset('Mg_err', data=Mg_err)
                h5f.create_dataset('Al_err', data=Al_err)
                h5f.create_dataset('Si_err', data=Si_err)
                h5f.create_dataset('P_err', data=P_err)
                h5f.create_dataset('S_err', data=S_err)
                h5f.create_dataset('K_err', data=K_err)
                h5f.create_dataset('Ca_err', data=Ca_err)
                h5f.create_dataset('Ti_err', data=Ti_err)
                h5f.create_dataset('Ti2_err', data=Ti2_err)
                h5f.create_dataset('V_err', data=V_err)
                h5f.create_dataset('Cr_err', data=Cr_err)
                h5f.create_dataset('Mn_err', data=Mn_err)
                h5f.create_dataset('Fe_err', data=Fe_err)
                h5f.create_dataset('Co_err', data=Co_err)
                h5f.create_dataset('Ni_err', data=Ni_err)
                h5f.create_dataset('Cu_err', data=Cu_err)
                h5f.create_dataset('Ge_err', data=Ge_err)
                h5f.create_dataset('Ce_err', data=Ce_err)
                h5f.create_dataset('Rb_err', data=Rb_err)
                h5f.create_dataset('Y_err', data=Y_err)
                h5f.create_dataset('Nd_err', data=Nd_err)
                h5f.create_dataset('parallax_err', data=parallax_err)
                h5f.create_dataset('fakemag_err', data=fakemag_err)

        h5f.close()
        print(f'Successfully created {self.filename}.h5 in {currentdir}')
示例#7
0
    def test_arXiv_1902_08634 (self):
        """
        astroNN spectrophotometric distance
        """
        from astroNN.apogee import visit_spectra, apogee_continuum
        from astroNN.gaia import extinction_correction, fakemag_to_pc
        from astropy.io import fits

        # first model
        models_url = ["https://github.com/henrysky/astroNN_gaia_dr2_paper/trunk/astroNN_no_offset_model",
                      "https://github.com/henrysky/astroNN_gaia_dr2_paper/trunk/astroNN_constant_model",
                      "https://github.com/henrysky/astroNN_gaia_dr2_paper/trunk/astroNN_multivariate_model"]

        for model_url in models_url:
            download_args = ["svn", "export", model_url]
            res = subprocess.Popen(download_args, stdout=subprocess.PIPE)
            output, _error = res.communicate()
            if not _error:
                pass
            else:
                raise ConnectionError(f"Error downloading the models {model_url}")

        opened_fits = fits.open(visit_spectra(dr=14, location=4405, apogee='2M19060637+4717296'))
        spectrum = opened_fits[1].data
        spectrum_err = opened_fits[2].data
        spectrum_bitmask = opened_fits[3].data

        # using default continuum and bitmask values to continuum normalize
        norm_spec, norm_spec_err = apogee_continuum(spectrum, spectrum_err,
                                                    bitmask=spectrum_bitmask, dr=14)
        # correct for extinction
        K = extinction_correction(opened_fits[0].header['K'], opened_fits[0].header['AKTARG'])

        # ===========================================================================================#
        # load neural net
        neuralnet = load_folder('astroNN_no_offset_model')
        # inference, if there are multiple visits, then you should use the globally
        # weighted combined spectra (i.e. the second row)
        pred, pred_err = neuralnet.test(norm_spec)
        # convert prediction in fakemag to distance
        pc, pc_error = fakemag_to_pc(pred[:, 0], K, pred_err['total'][:, 0])
        # assert distance is close enough
        # http://simbad.u-strasbg.fr/simbad/sim-id?mescat.distance=on&Ident=%406876647&Name=KIC+10196240&submit=display+selected+measurements#lab_meas
        # no offset correction so further away
        self.assertEqual(pc.value < 1250, True)
        self.assertEqual(pc.value > 1100, True)

        # ===========================================================================================#
        # load neural net
        neuralnet = load_folder('astroNN_constant_model')
        # inference, if there are multiple visits, then you should use the globally
        # weighted combined spectra (i.e. the second row)
        pred, pred_err = neuralnet.test(np.hstack([norm_spec, np.zeros((norm_spec.shape[0], 4))]))
        # convert prediction in fakemag to distance
        pc, pc_error = fakemag_to_pc(pred[:, 0], K, pred_err['total'][:, 0])
        # assert distance is close enough
        # http://simbad.u-strasbg.fr/simbad/sim-id?mescat.distance=on&Ident=%406876647&Name=KIC+10196240&submit=display+selected+measurements#lab_meas
        self.assertEqual(pc.value < 1150, True)
        self.assertEqual(pc.value > 1000, True)

        # ===========================================================================================#
        # load neural net
        neuralnet = load_folder('astroNN_multivariate_model')
        # inference, if there are multiple visits, then you should use the globally
        # weighted combined spectra (i.e. the second row)
        pred, pred_err = neuralnet.test(np.hstack([norm_spec, np.zeros((norm_spec.shape[0], 4))]))
        # convert prediction in fakemag to distance
        pc, pc_error = fakemag_to_pc(pred[:, 0], K, pred_err['total'][:, 0])
        # assert distance is close enough
        # http://simbad.u-strasbg.fr/simbad/sim-id?mescat.distance=on&Ident=%406876647&Name=KIC+10196240&submit=display+selected+measurements#lab_meas
        self.assertEqual(pc.value < 1150, True)
        self.assertEqual(pc.value > 1000, True)