Пример #1
0
    def test_known_regression(self):
        # prevent regression of known bug
        self.assertEqual(mag_to_absmag(1., MAGIC_NUMBER), MAGIC_NUMBER)
        self.assertEqual(mag_to_absmag(MAGIC_NUMBER, MAGIC_NUMBER), MAGIC_NUMBER)
        self.assertEqual(
            np.all(mag_to_absmag(MAGIC_NUMBER, MAGIC_NUMBER, 1.) == (MAGIC_NUMBER, MAGIC_NUMBER)), True)
        self.assertEqual(
            np.all(mag_to_fakemag(MAGIC_NUMBER, MAGIC_NUMBER, 1.) == (MAGIC_NUMBER, MAGIC_NUMBER)), True)

        self.assertEqual(mag_to_fakemag(1., MAGIC_NUMBER), MAGIC_NUMBER)
        self.assertEqual(mag_to_fakemag(MAGIC_NUMBER, MAGIC_NUMBER), MAGIC_NUMBER)

        self.assertEqual(fakemag_to_pc(1., MAGIC_NUMBER).value, MAGIC_NUMBER)
        self.assertEqual(fakemag_to_pc(-1., 2.).value, MAGIC_NUMBER)
        self.assertEqual(absmag_to_pc(1., MAGIC_NUMBER).value, MAGIC_NUMBER)
Пример #2
0
def load_apogee_rc(dr=None, unit='distance', extinction=True):
    """
    Load apogee red clumps (absolute magnitude measurement)

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

                   - "absmag" for k-band absolute magnitude
                   - "fakemag" for k-band fake magnitude
                   - "distance" for distance in parsec
    :type unit: string
    :param extinction: Whether to take extinction into account, only affect when unit is NOT 'distance'
    :type extinction: bool
    :return: numpy array of ra, dec, array
    :rtype: ndarrays
    :History:
        | 2018-Jan-21 - Written - Henry Leung (University of Toronto)
        | 2018-May-12 - Updated - Henry Leung (University of Toronto)
    """
    fullfilename = apogee_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 unit == 'distance':
        output = rc_dist * 1000

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

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

    else:
        raise ValueError('Unknown unit')

    return ra, dec, output
Пример #3
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
Пример #4
0
    def test_astrometry_conversion(self):
        # Example data of [Vega, Sirius]
        absmag = np.array([0.582, 1.42])
        mag = np.array([0.026, -1.46])
        parallax = np.array([130.23, 379.21])
        parallax_err = np.array([0.36, 1.58])

        # Absmag related test
        pc = absmag_to_pc(absmag, mag)
        npt.assert_almost_equal(pc.value, 1000 / parallax, decimal=1)
        npt.assert_almost_equal(mag_to_absmag(mag, parallax * u.mas),
                                absmag,
                                decimal=1)
        self.assertEqual(
            np.any(absmag_to_fakemag(MAGIC_NUMBER) == MAGIC_NUMBER), True)
        absmag_test, absmag_err_test = mag_to_absmag(mag, parallax * u.mas,
                                                     parallax_err)
        absmag_test_uniterr, absmag_err_test_uniterr = mag_to_absmag(
            mag, parallax * u.mas, parallax_err / 1000 * u.arcsec)
        absmag_test_arc, absmag_err_test_arc = mag_to_absmag(
            mag, parallax / 1000 * u.arcsec, parallax_err / 1000)
        absmag_test_unitless, absmag_err_test_unitless = mag_to_absmag(
            mag, parallax, parallax_err)

        # make sure unitless same as using astropy unit
        npt.assert_almost_equal(absmag_test, absmag_test_unitless)
        npt.assert_almost_equal(absmag_test, absmag_test_unitless)
        npt.assert_almost_equal(absmag_err_test, absmag_err_test_uniterr)

        # make sure astropy unit conversion works fine
        npt.assert_almost_equal(absmag_test, absmag_test_arc)
        npt.assert_almost_equal(absmag_err_test, absmag_err_test_arc)

        # =================== Fakemag related test ===================#
        # make sure these function did identity transform
        npt.assert_almost_equal(fakemag_to_absmag(absmag_to_fakemag(absmag)),
                                absmag,
                                decimal=1)

        # we can tests this after identity transformation confirmed
        npt.assert_almost_equal(fakemag_to_pc(absmag_to_fakemag(absmag),
                                              mag).value,
                                1000 / parallax,
                                decimal=1)
        npt.assert_almost_equal(fakemag_to_pc(
            mag_to_fakemag(mag, parallax * u.mas), mag).value,
                                1000 / parallax,
                                decimal=1)

        fakemag_test, fakemag_err_test = mag_to_fakemag(
            mag, parallax * u.mas, parallax_err)
        fakemag_test_uniterr, fakemag_err_test_uniterr = mag_to_fakemag(
            mag, parallax * u.mas, parallax_err / 1000 * u.arcsec)
        fakemag_test_unitless, fakemag_err_test_unitless = mag_to_fakemag(
            mag, parallax, parallax_err)
        fakemag_test_arc, fakemag_err_test_arc = mag_to_fakemag(
            mag, parallax / 1000 * u.arcsec, parallax_err / 1000)

        pc_result, pc_result_err = fakemag_to_pc(fakemag_test, mag,
                                                 fakemag_err_test)
        pc_result_arc, pc_result_err_arc = fakemag_to_pc(
            fakemag_test_arc, mag, fakemag_err_test_arc)

        # Analytically solution checkung
        npt.assert_almost_equal(pc_result_err.value,
                                (parallax_err / parallax) * pc.value,
                                decimal=1)

        # make sure unitless same as using astropy unit
        npt.assert_almost_equal(fakemag_test, fakemag_test_unitless)
        npt.assert_almost_equal(fakemag_err_test, fakemag_err_test_uniterr)
        npt.assert_almost_equal(fakemag_err_test, fakemag_err_test_unitless)

        # make sure astropy unit conversion works fine
        npt.assert_almost_equal(fakemag_test, fakemag_test_arc)
        npt.assert_almost_equal(fakemag_err_test, fakemag_err_test_arc)
        npt.assert_almost_equal(pc_result.value, pc_result_arc.value)
        npt.assert_almost_equal(pc_result_err.value, pc_result_err_arc.value)

        # check gaia default dr
        dr = gaia_default_dr()
        self.assertEqual(dr, 1)
        dr = gaia_default_dr(dr=3)
        self.assertEqual(dr, 3)