def convert():

    global req_params

    #if position given in ra & dec, convert to l & b
    if (req_params.get('l') == -9.999):
        req_params['l'], req_params['b'] = b_c.radec_to_lb(req_params['ra'],
                                                           req_params['dec'],
                                                           degree=True,
                                                           epoch=2000.0)

    #if position given in l & b, convert to ra & dec
    if (req_params.get('ra') == -9.999):
        req_params['ra'], req_params['dec'] = b_c.lb_to_radec(req_params['l'],
                                                              req_params['b'],
                                                              degree=True,
                                                              epoch=2000.0)

    #if proper motion given in pmra & pmdec, convert to pml & pmb
    if (req_params.get('pml') == -9.999):
        req_params['pml'], req_params['pmb'] = b_c.pmrapmdec_to_pmllpmbb(
            req_params['pmra'],
            req_params['pmdec'],
            req_params['ra'],
            req_params['dec'],
            degree=True,
            epoch=2000.0)
def obs_to_galcen(ra, dec, dist, pmra, pmdec, rv, ro=_R0, vo=_v0, zo=_z0):
    vxvv = np.dstack([ra, dec, dist, pmra, pmdec, rv])[0]
    ra, dec = vxvv[:, 0], vxvv[:, 1]
    lb = bovy_coords.radec_to_lb(ra, dec, degree=True)
    pmra, pmdec = vxvv[:, 3], vxvv[:, 4]
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(pmra, pmdec, ra, dec, degree=True)
    d, vlos = vxvv[:, 2], vxvv[:, 5]
    rectgal = bovy_coords.sphergal_to_rectgal(lb[:, 0], lb[:, 1], d, vlos, pmllpmbb[:, 0], pmllpmbb[:, 1], degree=True)
    vsolar = np.array([-11.1, 245.7, 7.25])
    vsun = vsolar / vo
    X = rectgal[:, 0] / ro
    Y = rectgal[:, 1] / ro
    Z = rectgal[:, 2] / ro
    vx = rectgal[:, 3] / vo
    vy = rectgal[:, 4] / vo
    vz = rectgal[:, 5] / vo
    XYZ = np.dstack([X, Y, Z])[0]
    vxyz = np.dstack([vx, vy, vz])[0]
    Rpz = bovy_coords.XYZ_to_galcencyl(XYZ[:, 0], XYZ[:, 1], XYZ[:, 2], Zsun=zo / ro)
    vRvTvz = bovy_coords.vxvyvz_to_galcencyl(vxyz[:, 0], vxyz[:, 1], vxyz[:, 2], Rpz[:, 0], Rpz[:, 1], Rpz[:, 2],
                                             vsun=vsun,
                                             Xsun=1.,
                                             Zsun=zo / ro,
                                             galcen=True)

    return XYZ, vxyz, Rpz, vRvTvz
示例#3
0
def pmphi12_to_pmllpmbb(pmphi1, pmphi2, phi1, phi2, degree=False):
    """
    NAME:
       pmllpmbb_to_pmphi12
    PURPOSE:
       Transform proper motions in (phi1,phi2) to Galactic coordinates (l,b)
    INPUT:
       pmphi1 - proper motion Galactic longitude (rad or degree); contains xcosphi2
       pmphi2 - Galactic latitude (rad or degree)
       phi1 - phi longitude (rad or degree)
       phi2 - phi latitude (rad or degree)
       degree= (False) if True, input (phi1,phi2) are in degrees
    OUTPUT:
       (pmll,pmbb) for scalar input
       [:,2] array for vector input
    HISTORY:
        2014-11-04 - Written - Bovy (IAS)
    """
    #First go from phi12 to ra and dec
    lb = phi12_to_lb(phi1, phi2)
    radec = bovy_coords.lb_to_radec(lb[:, 0], lb[:, 1])
    ra = radec[:, 0]
    dec = radec[:, 1]
    #Build A and Aphi matrices
    AInv = numpy.zeros((3, 3, len(ra)))
    AInv[0, 0] = numpy.cos(ra) * numpy.cos(dec)
    AInv[0, 1] = numpy.sin(ra) * numpy.cos(dec)
    AInv[0, 2] = numpy.sin(dec)
    AInv[1, 0] = -numpy.sin(ra)
    AInv[1, 1] = numpy.cos(ra)
    AInv[1, 2] = 0.
    AInv[2, 0] = -numpy.cos(ra) * numpy.sin(dec)
    AInv[2, 1] = -numpy.sin(ra) * numpy.sin(dec)
    AInv[2, 2] = numpy.cos(dec)
    Aphi = numpy.zeros((3, 3, len(ra)))
    Aphi[0, 0] = numpy.cos(phi1) * numpy.cos(phi2)
    Aphi[0, 1] = -numpy.sin(phi1)
    Aphi[0, 2] = -numpy.cos(phi1) * numpy.sin(phi2)
    Aphi[1, 0] = numpy.sin(phi1) * numpy.cos(phi2)
    Aphi[1, 1] = numpy.cos(phi1)
    Aphi[1, 2] = -numpy.sin(phi1) * numpy.sin(phi2)
    Aphi[2, 0] = numpy.sin(phi2)
    Aphi[2, 1] = 0.
    Aphi[2, 2] = numpy.cos(phi2)
    TAphi = numpy.dot(_TKOP.T, numpy.swapaxes(Aphi, 0, 1))
    #Got lazy...
    trans = numpy.zeros((2, 2, len(ra)))
    for ii in range(len(ra)):
        trans[:, :, ii] = numpy.dot(AInv[:, :, ii], TAphi[:, :, ii])[1:, 1:]
    pmradec = (trans *
               numpy.array([[pmphi1, pmphi2], [pmphi1, pmphi2]])).sum(1).T
    pmra = pmradec[:, 0]
    pmdec = pmradec[:, 1]
    #Now convert to pmll
    return bovy_coords.pmrapmdec_to_pmllpmbb(pmra, pmdec, ra, dec)
示例#4
0
 def conv_pmrapmdec_to_pmllpmbb(self):
     # step 1: convert PM radec to PM l,b
     pmra = self.get_col('PMRA')
     pmdec = self.get_col('PMDEC')
     if self.biascorrect is not None:
         (dpmra, dpmdec) = self.get_pm_corr()
         pmra += dpmra
         pmdec += dpmdec
     self.pmll_pmbb = bcoords.pmrapmdec_to_pmllpmbb(pmra, pmdec,
                                                    self.get_col('RA'),
                                                    self.get_col('DEC'),
                                                    degree=self.degree,
                                                    epoch=2000.0)
示例#5
0
def test_pmrapmdec_to_pmllpmbb():
    #This is a random ra,dec
    ra, dec= 132., -20.4
    pmra, pmdec= 10., 20.
    pmll, pmbb= bovy_coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,
                                              ra,dec,degree=True,epoch=1950.)
    assert numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10., 'pmrapmdec_to_pmllpmbb conversion did not work as expected'
    # This is close to the NGP at 1950.
    ra, dec= 192.24, 27.39
    pmra, pmdec= 10., 20.
    os= numpy.ones(2)
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(os*pmra,os*pmdec,
                                                  os*ra,os*dec,
                                                  degree=True,epoch=1950.)
    
    pmll= pmllpmbb[:,0]
    pmbb= pmllpmbb[:,1]
    assert numpy.all(numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10.), 'pmrapmdec_to_pmllpmbb conversion did not work as expected close to the NGP'
    # This is the NGP at 1950.
    ra, dec= 192.25, 27.4
    pmra, pmdec= 10., 20.
    os= numpy.ones(2)
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(os*pmra,os*pmdec,
                                                  os*ra,os*dec,
                                                  degree=True,epoch=1950.)
    
    pmll= pmllpmbb[:,0]
    pmbb= pmllpmbb[:,1]
    assert numpy.all(numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10.), 'pmrapmdec_to_pmllpmbb conversion did not work as expected for the NGP'
    # This is the NCP
    ra, dec= numpy.pi, numpy.pi/2.
    pmra, pmdec= 10., 20.
    pmll, pmbb= bovy_coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,
                                                  ra,dec,degree=False,
                                                  epoch=1950.)
    assert numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10., 'pmrapmdec_to_pmllpmbb conversion did not work as expected for the NCP'
    return None
示例#6
0
def test_coords():
    from galpy.util import bovy_coords
    ra, dec, dist = 161., 50., 8.5
    pmra, pmdec, vlos = -6.8, -10., -115.
    # Convert to Galactic and then to rect. Galactic
    ll, bb = bovy_coords.radec_to_lb(ra, dec, degree=True)
    pmll, pmbb = bovy_coords.pmrapmdec_to_pmllpmbb(pmra,
                                                   pmdec,
                                                   ra,
                                                   dec,
                                                   degree=True)
    X, Y, Z = bovy_coords.lbd_to_XYZ(ll, bb, dist, degree=True)
    vX, vY, vZ = bovy_coords.vrpmllpmbb_to_vxvyvz(vlos,
                                                  pmll,
                                                  pmbb,
                                                  X,
                                                  Y,
                                                  Z,
                                                  XYZ=True)
    # Convert to cylindrical Galactocentric
    # Assuming Sun's distance to GC is (8,0.025) in (R,z)
    R, phi, z = bovy_coords.XYZ_to_galcencyl(X, Y, Z, Xsun=8., Zsun=0.025)
    vR, vT, vz = bovy_coords.vxvyvz_to_galcencyl(vX,
                                                 vY,
                                                 vZ,
                                                 R,
                                                 phi,
                                                 Z,
                                                 vsun=[-10.1, 244., 6.7],
                                                 galcen=True)
    # 5/12/2016: test weakened, because improved galcen<->heliocen
    #            transformation has changed these, but still close
    print(R, phi, z, vR, vT, vz)
    assert numpy.fabs(R - 12.51328515156942
                      ) < 10.**-1., 'Coordinate transformation has changed'
    assert numpy.fabs(phi - 0.12177409073433249
                      ) < 10.**-1., 'Coordinate transformation has changed'
    assert numpy.fabs(z - 7.1241282354856228
                      ) < 10.**-1., 'Coordinate transformation has changed'
    assert numpy.fabs(vR - 78.961682923035966
                      ) < 10.**-1., 'Coordinate transformation has changed'
    assert numpy.fabs(vT + 241.49247772351964
                      ) < 10.**-1., 'Coordinate transformation has changed'
    assert numpy.fabs(vz + 102.83965442188689
                      ) < 10.**-1., 'Coordinate transformation has changed'
    return None
示例#7
0
def add_gaia_galactic_pms(df, errors=True):
    """Add proper motions in galactic coordinates to a Gaia DR2 pandas DataFrame"""
    old_settings = np.seterr(invalid='ignore')
    try:
        ra, dec = np.array(df['ra_gaia']), np.array(df['dec_gaia'])
    except KeyError:
        ra, dec = np.array(df['ra']), np.array(df['dec'])
    pmra, pmdec = np.array(df['pmra']), np.array(df['pmdec'])

    mul, mub = bovy_coords.pmrapmdec_to_pmllpmbb(pmra,
                                                 pmdec,
                                                 ra,
                                                 dec,
                                                 degree=True).T
    df['pml'] = mul
    df['pmb'] = mub
    if errors:
        add_gaia_galactic_pm_errors(df)
    np.seterr(**old_settings)
def test_coords():
    from galpy.util import bovy_coords
    ra, dec, dist= 161., 50., 8.5
    pmra, pmdec, vlos= -6.8, -10., -115.
  # Convert to Galactic and then to rect. Galactic
    ll, bb= bovy_coords.radec_to_lb(ra,dec,degree=True)
    pmll, pmbb= bovy_coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,ra,dec,degree=True)
    X,Y,Z= bovy_coords.lbd_to_XYZ(ll,bb,dist,degree=True)
    vX,vY,vZ= bovy_coords.vrpmllpmbb_to_vxvyvz(vlos,pmll,pmbb,X,Y,Z,XYZ=True)
    # Convert to cylindrical Galactocentric
    # Assuming Sun's distance to GC is (8,0.025) in (R,z)
    R,phi,z= bovy_coords.XYZ_to_galcencyl(X,Y,Z,Xsun=8.,Zsun=0.025)
    vR,vT,vz= bovy_coords.vxvyvz_to_galcencyl(vX,vY,vZ,R,phi,Z,vsun=[-10.1,244.,6.7],galcen=True)
    assert numpy.fabs(R-12.51328515156942) < 10.**-4., 'Coordinate transformation has changed'
    assert numpy.fabs(phi-0.12177409073433249) < 10.**-4., 'Coordinate transformation has changed'
    assert numpy.fabs(z-7.1241282354856228) < 10.**-4., 'Coordinate transformation has changed'
    assert numpy.fabs(vR-78.961682923035966) < 10.**-4., 'Coordinate transformation has changed'
    assert numpy.fabs(vT+241.49247772351964) < 10.**-4., 'Coordinate transformation has changed'
    assert numpy.fabs(vz+102.83965442188689) < 10.**-4., 'Coordinate transformation has changed'
    return None
示例#9
0
rdata[:, 9] = rdata[:, 9] * 1000.0
rdata[:, 10] = rdata[:, 10] * 1000.0
rdata[:, 15] = rdata[:, 15] * 1000.0
rdata[:, 16] = rdata[:, 16] * 1000.0

# Galactic coordinates
# True l and b
RA_true = rdata[:, 0]
DEC_true = rdata[:, 1]
Tllbb = bovy_coords.radec_to_lb(RA_true, DEC_true, degree=True, epoch=2000.0)
GLON_true = Tllbb[:, 0]
GLAT_true = Tllbb[:, 1]
# True pmGLON, pmGLAT
pmRA_true = rdata[:, 3]
pmDEC_true = rdata[:, 4]
Tpmllbb=bovy_coords.pmrapmdec_to_pmllpmbb(pmRA_true,pmDEC_true \
   ,RA_true,DEC_true,degree=True,epoch=2000.0)
pmGLON_true = Tpmllbb[:, 0]
pmGLAT_true = Tpmllbb[:, 1]
# observed
RA_obs = rdata[:, 6]
DEC_obs = rdata[:, 7]
pmRA_obs = rdata[:, 9]
pmDEC_obs = rdata[:, 10]
Tpmllbb=bovy_coords.pmrapmdec_to_pmllpmbb(pmRA_obs,pmDEC_obs \
   ,RA_true,DEC_true,degree=True,epoch=2000.0)
pmGLON_obs = Tpmllbb[:, 0]
pmGLAT_obs = Tpmllbb[:, 1]
# error
e_pmRA = rdata[:, 12]
e_pmDEC = rdata[:, 13]
Tpmllbb=bovy_coords.pmrapmdec_to_pmllpmbb(e_pmRA,e_pmDEC \
示例#10
0
def _add_proper_motions_pregaia(data, savefilename):
    #Get proper motions, in a somewhat roundabout way
    pmfile = savefilename.split('.')[0] + '_pms.fits'
    if os.path.exists(pmfile):
        pmdata = fitsread(pmfile, 1)
    else:
        pmdata = numpy.recarray(
            len(data),
            formats=['f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'i4'],
            names=[
                'RA', 'DEC', 'PMRA', 'PMDEC', 'PMRA_ERR', 'PMDEC_ERR',
                'PMMATCH'
            ])
        # Write positions, again ...
        posfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        resultfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        with open(posfilename, 'w') as csvfile:
            wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA', 'DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'], data[ii]['DEC']])
        # Send to CDS for matching
        result = open(resultfilename, 'w')
        try:
            subprocess.check_call([
                'curl', '-X', 'POST', '-F', 'request=xmatch', '-F',
                'distMaxArcsec=4', '-F', 'RESPONSEFORMAT=csv', '-F',
                'cat1=@%s' % os.path.basename(posfilename), '-F', 'colRA1=RA',
                '-F', 'colDec1=DEC', '-F', 'cat2=vizier:UCAC4',
                'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'
            ],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Match back and only keep the closest one
        ma = numpy.loadtxt(resultfilename,
                           delimiter=',',
                           skiprows=1,
                           converters={
                               15: lambda s: float(s.strip() or -9999),
                               16: lambda s: float(s.strip() or -9999),
                               17: lambda s: float(s.strip() or -9999),
                               18: lambda s: float(s.strip() or -9999)
                           },
                           usecols=(4, 5, 15, 16, 17, 18))
        h = esutil.htm.HTM()
        m1, m2, d12 = h.match(data['RA'],
                              data['DEC'],
                              ma[:, 0],
                              ma[:, 1],
                              4. / 3600.,
                              maxmatch=1)
        pmdata['PMMATCH'] = 0
        pmdata['RA'] = data['RA']
        pmdata['DEC'] = data['DEC']
        pmdata['PMMATCH'][m1] = 1
        pmdata['PMRA'][m1] = ma[m2, 2]
        pmdata['PMDEC'][m1] = ma[m2, 3]
        pmdata['PMRA_ERR'][m1] = ma[m2, 4]
        pmdata['PMDEC_ERR'][m1] = ma[m2, 5]
        pmdata['PMMATCH'][(pmdata['PMRA'] == -9999) \
                          +(pmdata['PMDEC'] == -9999) \
                          +(pmdata['PMRA_ERR'] == -9999) \
                          +(pmdata['PMDEC_ERR'] == -9999)]= 0
        fitswrite(pmfile, pmdata, clobber=True)
        #To make sure we're using the same format below
        pmdata = fitsread(pmfile, 1)
        os.remove(posfilename)
        os.remove(resultfilename)
    #Match proper motions
    try:  #These already exist currently, but may not always exist
        data = esutil.numpy_util.remove_fields(data, ['PMRA', 'PMDEC'])
    except ValueError:
        pass
    data = esutil.numpy_util.add_fields(data, [('PMRA', numpy.float),
                                               ('PMDEC', numpy.float),
                                               ('PMRA_ERR', numpy.float),
                                               ('PMDEC_ERR', numpy.float),
                                               ('PMMATCH', numpy.int32)])
    data['PMMATCH'] = 0
    h = esutil.htm.HTM()
    m1, m2, d12 = h.match(pmdata['RA'],
                          pmdata['DEC'],
                          data['RA'],
                          data['DEC'],
                          2. / 3600.,
                          maxmatch=1)
    data['PMRA'][m2] = pmdata['PMRA'][m1]
    data['PMDEC'][m2] = pmdata['PMDEC'][m1]
    data['PMRA_ERR'][m2] = pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR'][m2] = pmdata['PMDEC_ERR'][m1]
    data['PMMATCH'][m2] = pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx = data['PMMATCH'] == 1
    data['PMRA'][True ^ pmindx] = -9999.99
    data['PMDEC'][True ^ pmindx] = -9999.99
    data['PMRA_ERR'][True ^ pmindx] = -9999.99
    data['PMDEC_ERR'][True ^ pmindx] = -9999.99
    #Calculate Galactocentric velocities
    data = esutil.numpy_util.add_fields(data, [('GALVR', numpy.float),
                                               ('GALVT', numpy.float),
                                               ('GALVZ', numpy.float)])
    lb = bovy_coords.radec_to_lb(data['RA'], data['DEC'], degree=True)
    XYZ = bovy_coords.lbd_to_XYZ(lb[:, 0],
                                 lb[:, 1],
                                 data['RC_DIST'],
                                 degree=True)
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA'],
                                                 data['PMDEC'],
                                                 data['RA'],
                                                 data['DEC'],
                                                 degree=True)
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              lb[:, 0],
                                              lb[:, 1],
                                              data['RC_DIST'],
                                              degree=True)
    vRvTvZ = bovy_coords.vxvyvz_to_galcencyl(
        vxvyvz[:, 0],
        vxvyvz[:, 1],
        vxvyvz[:, 2],
        8. - XYZ[:, 0],
        XYZ[:, 1],
        XYZ[:, 2] + 0.025,
        vsun=[-11.1, 30.24 * 8.,
              7.25])  #Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR'] = vRvTvZ[:, 0]
    data['GALVT'] = vRvTvZ[:, 1]
    data['GALVZ'] = vRvTvZ[:, 2]
    data['GALVR'][True ^ pmindx] = -9999.99
    data['GALVT'][True ^ pmindx] = -9999.99
    data['GALVZ'][True ^ pmindx] = -9999.99
    #Get HSOY proper motions, in a somewhat roundabout way
    pmfile = savefilename.split('.')[0] + '_pms_ppmxl.fits'
    if os.path.exists(pmfile):
        pmdata = fitsread(pmfile, 1)
    else:
        pmdata = numpy.recarray(
            len(data),
            formats=['f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'i4'],
            names=[
                'RA', 'DEC', 'PMRA', 'PMDEC', 'PMRA_ERR', 'PMDEC_ERR',
                'PMMATCH'
            ])
        # Write positions, again ...
        posfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        resultfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        with open(posfilename, 'w') as csvfile:
            wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA', 'DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'], data[ii]['DEC']])
        # Send to CDS for matching
        result = open(resultfilename, 'w')
        try:
            subprocess.check_call([
                'curl', '-X', 'POST', '-F', 'request=xmatch', '-F',
                'distMaxArcsec=4', '-F', 'RESPONSEFORMAT=csv', '-F',
                'cat1=@%s' % os.path.basename(posfilename), '-F', 'colRA1=RA',
                '-F', 'colDec1=DEC', '-F', 'cat2=vizier:I/339/hsoy',
                'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'
            ],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Match back and only keep the closest one
        ma = numpy.loadtxt(resultfilename,
                           delimiter=',',
                           skiprows=1,
                           converters={
                               12: lambda s: float(s.strip() or -9999),
                               13: lambda s: float(s.strip() or -9999),
                               14: lambda s: float(s.strip() or -9999),
                               15: lambda s: float(s.strip() or -9999)
                           },
                           usecols=(3, 4, 12, 13, 14, 15))
        h = esutil.htm.HTM()
        m1, m2, d12 = h.match(data['RA'],
                              data['DEC'],
                              ma[:, 0],
                              ma[:, 1],
                              4. / 3600.,
                              maxmatch=1)
        pmdata['PMMATCH'] = 0
        pmdata['RA'] = data['RA']
        pmdata['DEC'] = data['DEC']
        pmdata['PMMATCH'][m1] = 1
        pmdata['PMRA'][m1] = ma[m2, 2]
        pmdata['PMDEC'][m1] = ma[m2, 3]
        pmdata['PMRA_ERR'][m1] = ma[m2, 4]
        pmdata['PMDEC_ERR'][m1] = ma[m2, 5]
        pmdata['PMMATCH'][(pmdata['PMRA'] == -9999) \
                          +(pmdata['PMDEC'] == -9999) \
                          +(pmdata['PMRA_ERR'] == -9999) \
                          +(pmdata['PMDEC_ERR'] == -9999)]= 0
        fitswrite(pmfile, pmdata, clobber=True)
        #To make sure we're using the same format below
        pmdata = fitsread(pmfile, 1)
        os.remove(posfilename)
        os.remove(resultfilename)
    #Match proper motions to ppmxl/HSOY
    data = esutil.numpy_util.add_fields(data, [('PMRA_HSOY', numpy.float),
                                               ('PMDEC_HSOY', numpy.float),
                                               ('PMRA_ERR_HSOY', numpy.float),
                                               ('PMDEC_ERR_HSOY', numpy.float),
                                               ('PMMATCH_HSOY', numpy.int32)])
    data['PMMATCH_HSOY'] = 0
    h = esutil.htm.HTM()
    m1, m2, d12 = h.match(pmdata['RA'],
                          pmdata['DEC'],
                          data['RA'],
                          data['DEC'],
                          2. / 3600.,
                          maxmatch=1)
    data['PMRA_HSOY'][m2] = pmdata['PMRA'][m1]
    data['PMDEC_HSOY'][m2] = pmdata['PMDEC'][m1]
    data['PMRA_ERR_HSOY'][m2] = pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR_HSOY'][m2] = pmdata['PMDEC_ERR'][m1]
    data['PMMATCH_HSOY'][m2] = pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx = data['PMMATCH_HSOY'] == 1
    data['PMRA_HSOY'][True ^ pmindx] = -9999.99
    data['PMDEC_HSOY'][True ^ pmindx] = -9999.99
    data['PMRA_ERR_HSOY'][True ^ pmindx] = -9999.99
    data['PMDEC_ERR_HSOY'][True ^ pmindx] = -9999.99
    #Calculate Galactocentric velocities
    data = esutil.numpy_util.add_fields(data, [('GALVR_HSOY', numpy.float),
                                               ('GALVT_HSOY', numpy.float),
                                               ('GALVZ_HSOY', numpy.float)])
    lb = bovy_coords.radec_to_lb(data['RA'], data['DEC'], degree=True)
    XYZ = bovy_coords.lbd_to_XYZ(lb[:, 0],
                                 lb[:, 1],
                                 data['RC_DIST'],
                                 degree=True)
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA_HSOY'],
                                                 data['PMDEC_HSOY'],
                                                 data['RA'],
                                                 data['DEC'],
                                                 degree=True)
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              lb[:, 0],
                                              lb[:, 1],
                                              data['RC_DIST'],
                                              degree=True)
    vRvTvZ = bovy_coords.vxvyvz_to_galcencyl(
        vxvyvz[:, 0],
        vxvyvz[:, 1],
        vxvyvz[:, 2],
        8. - XYZ[:, 0],
        XYZ[:, 1],
        XYZ[:, 2] + 0.025,
        vsun=[-11.1, 30.24 * 8.,
              7.25])  #Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR_HSOY'] = vRvTvZ[:, 0]
    data['GALVT_HSOY'] = vRvTvZ[:, 1]
    data['GALVZ_HSOY'] = vRvTvZ[:, 2]
    data['GALVR_HSOY'][True ^ pmindx] = -9999.99
    data['GALVT_HSOY'][True ^ pmindx] = -9999.99
    data['GALVZ_HSOY'][True ^ pmindx] = -9999.99
    #Return
    return data
    return None
示例#11
0
def pmphi12_to_pmllpmbb(pmphi1,pmphi2,phi1,phi2,degree=False):
    """
        NAME:
        pmllpmbb_to_pmphi12
        PURPOSE:
        Transform proper motions in (phi1,phi2) to Galactic coordinates (l,b)
        INPUT:
        pmphi1 - proper motion Galactic longitude (rad or degree); contains xcosphi2
        pmphi2 - Galactic latitude (rad or degree)
        phi1 - phi longitude (rad or degree)
        phi2 - phi latitude (rad or degree)
        degree= (False) if True, input (phi1,phi2) are in degrees
        OUTPUT:
        (pmll,pmbb) for scalar input
        [:,2] array for vector input
        HISTORY:
        2014-11-04 - Written - Bovy (IAS)
        """
    import numpy
    
    _TKOP= numpy.zeros((3,3))
    _TKOP[0,:]= [-0.4776303088,-0.1738432154,0.8611897727]
    _TKOP[1,:]= [0.510844589,-0.8524449229,0.111245042]
    _TKOP[2,:]= [0.7147776536,0.4930681392,0.4959603976]
    
    #First go from phi12 to ra and dec
    lb= phi12_to_lb(phi1,phi2)
    radec= bovy_coords.lb_to_radec(lb[:,0],lb[:,1])
    ra= radec[:,0]
    dec= radec[:,1]
    #Build A and Aphi matrices
    AInv= numpy.zeros((3,3,len(ra)))
    AInv[0,0]= numpy.cos(ra)*numpy.cos(dec)
    AInv[0,1]= numpy.sin(ra)*numpy.cos(dec)
    AInv[0,2]= numpy.sin(dec)
    AInv[1,0]= -numpy.sin(ra)
    AInv[1,1]= numpy.cos(ra)
    AInv[1,2]= 0.
    AInv[2,0]= -numpy.cos(ra)*numpy.sin(dec)
    AInv[2,1]= -numpy.sin(ra)*numpy.sin(dec)
    AInv[2,2]= numpy.cos(dec)
    Aphi= numpy.zeros((3,3,len(ra)))
    Aphi[0,0]= numpy.cos(phi1)*numpy.cos(phi2)
    Aphi[0,1]= -numpy.sin(phi1)
    Aphi[0,2]= -numpy.cos(phi1)*numpy.sin(phi2)
    Aphi[1,0]= numpy.sin(phi1)*numpy.cos(phi2)
    Aphi[1,1]= numpy.cos(phi1)
    Aphi[1,2]= -numpy.sin(phi1)*numpy.sin(phi2)
    Aphi[2,0]= numpy.sin(phi2)
    Aphi[2,1]= 0.
    Aphi[2,2]= numpy.cos(phi2)
    TAphi= numpy.dot(_TKOP.T,numpy.swapaxes(Aphi,0,1))
    #Got lazy...
    trans= numpy.zeros((2,2,len(ra)))
    for ii in range(len(ra)):
        trans[:,:,ii]= numpy.dot(AInv[:,:,ii],TAphi[:,:,ii])[1:,1:]
    pmradec= (trans*numpy.array([[pmphi1,pmphi2],[pmphi1,pmphi2]])).sum(1).T
    pmra= pmradec[:,0]
    pmdec= pmradec[:,1]
    #Now convert to pmll
    return bovy_coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,ra,dec)
示例#12
0
    def __init__(self,
                 vxvv=None,
                 uvw=False,
                 lb=False,
                 radec=False,
                 vo=235.,
                 ro=8.5,
                 zo=0.025,
                 solarmotion='hogg'):
        """
        NAME:

           __init__

        PURPOSE:

           Initialize an Orbit instance

        INPUT:

           vxvv - initial conditions 
                  3D can be either

              1) in Galactocentric cylindrical coordinates [R,vR,vT(,z,vz,phi)]

              2) [ra,dec,d,mu_ra, mu_dec,vlos] in [deg,deg,kpc,mas/yr,mas/yr,km/s] (all J2000.0; mu_ra = mu_ra * cos dec)

              3) [ra,dec,d,U,V,W] in [deg,deg,kpc,km/s,km/s,kms]

              4) (l,b,d,mu_l, mu_b, vlos) in [deg,deg,kpc,mas/yr,mas/yr,km/s) (all J2000.0; mu_l = mu_l * cos b)

              5) [l,b,d,U,V,W] in [deg,deg,kpc,km/s,km/s,kms]

           4) and 5) also work when leaving out b and mu_b/W

        OPTIONAL INPUTS:

           radec - if True, input is 2) (or 3) above

           uvw - if True, velocities are UVW

           lb - if True, input is 4) or 5) above

           vo - circular velocity at ro

           ro - distance from vantage point to GC (kpc)

           zo - offset toward the NGP of the Sun wrt the plane (kpc)

           solarmotion - 'hogg' or 'dehnen', or 'schoenrich', or value in 
           [-U,V,W]

        OUTPUT:

           instance

        HISTORY:

           2010-07-20 - Written - Bovy (NYU)

        """
        if isinstance(solarmotion, str) and solarmotion.lower() == 'hogg':
            vsolar = nu.array([-10.1, 4.0, 6.7]) / vo
        elif isinstance(solarmotion, str) and solarmotion.lower() == 'dehnen':
            vsolar = nu.array([-10., 5.25, 7.17]) / vo
        elif isinstance(solarmotion,str) \
                and solarmotion.lower() == 'schoenrich':
            vsolar = nu.array([-11.1, 12.24, 7.25]) / vo
        else:
            vsolar = nu.array(solarmotion) / vo
        if radec or lb:
            if radec:
                l, b = coords.radec_to_lb(vxvv[0], vxvv[1], degree=True)
            elif len(vxvv) == 4:
                l, b = vxvv[0], 0.
            else:
                l, b = vxvv[0], vxvv[1]
            if uvw:
                X, Y, Z = coords.lbd_to_XYZ(l, b, vxvv[2], degree=True)
                vx = vxvv[3]
                vy = vxvv[4]
                vz = vxvv[5]
            else:
                if radec:
                    pmll, pmbb = coords.pmrapmdec_to_pmllpmbb(vxvv[3],
                                                              vxvv[4],
                                                              vxvv[0],
                                                              vxvv[1],
                                                              degree=True)
                    d, vlos = vxvv[2], vxvv[5]
                elif len(vxvv) == 4:
                    pmll, pmbb = vxvv[2], 0.
                    d, vlos = vxvv[1], vxvv[3]
                else:
                    pmll, pmbb = vxvv[3], vxvv[4]
                    d, vlos = vxvv[2], vxvv[5]
                X, Y, Z, vx, vy, vz = coords.sphergal_to_rectgal(l,
                                                                 b,
                                                                 d,
                                                                 vlos,
                                                                 pmll,
                                                                 pmbb,
                                                                 degree=True)
            X /= ro
            Y /= ro
            Z /= ro
            vx /= vo
            vy /= vo
            vz /= vo
            vsun = nu.array([
                0.,
                1.,
                0.,
            ]) + vsolar
            R, phi, z = coords.XYZ_to_galcencyl(X, Y, Z, Zsun=zo / ro)
            vR, vT, vz = coords.vxvyvz_to_galcencyl(vx,
                                                    vy,
                                                    vz,
                                                    R,
                                                    phi,
                                                    z,
                                                    vsun=vsun,
                                                    galcen=True)
            if lb and len(vxvv) == 4: vxvv = [R, vR, vT, phi]
            else: vxvv = [R, vR, vT, z, vz, phi]
        self.vxvv = vxvv
        if len(vxvv) == 2:
            self._orb = linearOrbit(vxvv=vxvv)
        elif len(vxvv) == 3:
            self._orb = planarROrbit(vxvv=vxvv)
        elif len(vxvv) == 4:
            self._orb = planarOrbit(vxvv=vxvv)
        elif len(vxvv) == 5:
            self._orb = RZOrbit(vxvv=vxvv)
        elif len(vxvv) == 6:
            self._orb = FullOrbit(vxvv=vxvv)
示例#13
0
def calc_eccentricity(args, options):
    table = os.path.join(args[0],'table2.dat')
    readme = os.path.join(args[0],'ReadMe')
    dierickx = ascii.read(table, readme=readme)
    vxvv = np.dstack([dierickx['RAdeg'], dierickx['DEdeg'], dierickx['Dist']/1e3, dierickx['pmRA'], dierickx['pmDE'], dierickx['HRV']])[0]
    ro, vo, zo = 8., 220., 0.025
    ra, dec= vxvv[:,0], vxvv[:,1]
    lb= bovy_coords.radec_to_lb(ra,dec,degree=True)
    pmra, pmdec= vxvv[:,3], vxvv[:,4]
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,ra,dec,degree=True)
    d, vlos= vxvv[:,2], vxvv[:,5]
    rectgal= bovy_coords.sphergal_to_rectgal(lb[:,0],lb[:,1],d,vlos,pmllpmbb[:,0], pmllpmbb[:,1],degree=True)
    vsolar= np.array([-10.1,4.0,6.7])
    vsun= np.array([0.,1.,0.,])+vsolar/vo
    X = rectgal[:,0]/ro
    Y = rectgal[:,1]/ro
    Z = rectgal[:,2]/ro
    vx = rectgal[:,3]/vo
    vy = rectgal[:,4]/vo
    vz = rectgal[:,5]/vo
    vsun= np.array([0.,1.,0.,])+vsolar/vo
    Rphiz= bovy_coords.XYZ_to_galcencyl(X,Y,Z,Zsun=zo/ro)
    vRvTvz= bovy_coords.vxvyvz_to_galcencyl(vx,vy,vz,Rphiz[:,0],Rphiz[:,1],Rphiz[:,2],vsun=vsun,Xsun=1.,Zsun=zo/ro,galcen=True)
    #do the integration and individual analytic estimate for each object
    ts= np.linspace(0.,20.,10000)
    lp= LogarithmicHaloPotential(normalize=1.)
    e_ana = numpy.zeros(len(vxvv))
    e_int = numpy.zeros(len(vxvv))
    print('Performing orbit integration and analytic parameter estimates for Dierickx et al. sample...')
    for i in tqdm(range(len(vxvv))):
        try:
            orbit = Orbit(vxvv[i], radec=True, vo=220., ro=8.)
            e_ana[i] = orbit.e(analytic=True, pot=lp, c=True)
        except UnboundError:
            e_ana[i] = np.nan
        orbit.integrate(ts, lp)
        e_int[i] = orbit.e(analytic=False)
    fig = plt.figure()
    fig.set_size_inches(1.5*columnwidth, 1.5*columnwidth)
    plt.scatter(e_int, e_ana,  s=1, color='Black', lw=0.)
    plt.xlabel(r'$\mathrm{galpy\ integrated}\ e$')
    plt.ylabel(r'$\mathrm{galpy\ analytic}\ e$')
    plt.xlim(0.,1.)
    plt.ylim(0.,1.)
    fig.tight_layout()
    plt.savefig(os.path.join(args[0],'dierickx-integratedeanalytice.png'), format='png', dpi=200)
    fig = plt.figure()
    fig.set_size_inches(1.5*columnwidth, 1.5*columnwidth)
    plt.hist(e_int, bins=30)
    plt.xlim(0.,1.)
    plt.xlabel(r'$\mathrm{galpy}\ e$')
    fig.tight_layout()
    plt.savefig(os.path.join(args[0], 'dierickx-integratedehist.png'), format='png', dpi=200)
    fig = plt.figure()
    fig.set_size_inches(1.5*columnwidth, 1.5*columnwidth)
    plt.scatter(dierickx['e'], e_int,  s=1, color='Black', lw=0.)
    plt.xlabel(r'$\mathrm{Dierickx\ et\ al.}\ e$')
    plt.ylabel(r'$\mathrm{galpy\ integrated}\ e$')
    plt.xlim(0.,1.)
    plt.ylim(0.,1.)
    fig.tight_layout()
    plt.savefig(os.path.join(args[0],'dierickx-integratedee.png'), format='png', dpi=200)
    fig = plt.figure()
    fig.set_size_inches(1.5*columnwidth, 1.5*columnwidth)
    plt.scatter(dierickx['e'], e_ana,  s=1, color='Black', lw=0.)
    plt.xlabel(r'$\mathrm{Dierickx\ et\ al.}\ e$')
    plt.ylabel(r'$\mathrm{galpy\ estimated}\ e$')
    plt.xlim(0.,1.)
    plt.ylim(0.,1.)
    fig.tight_layout()
    plt.savefig(os.path.join(args[0],'dierickx-analyticee.png'), format='png', dpi=200)
    arr = numpy.recarray(len(e_ana), dtype=[('analytic_e', float), ('integrated_e', float)])
    arr['analytic_e'] = e_ana
    arr['integrated_e'] = e_int
    with open(os.path.join(args[0],'eccentricities.dat'), 'w') as file:
        pickle.dump(arr, file)
        file.close()
def orbit_N(method, req_dict, opt_dict, calc_dict):

    count_orbits = 0.

    orbit_params = {
        'Energy': [],
        'L_z': [],
        'L_p': [],
        'I_3': [],
        'Z_max': [],
        'ecc': [],
        'r_apo': [],
        'r_peri': [],
        'R_apo_P': [],
        'R_peri_P': []
    }

    orbit_unc = {
        'Energy_unc': -9.999,
        'L_z_unc': -9.999,
        'L_p_unc': -9.999,
        'I_3_unc': -9.999,
        'Z_max_unc': -9.999,
        'ecc_unc': -9.999,
        'r_apo_unc': -9.999,
        'r_peri_unc': -9.999,
        'R_apo_P_unc': -9.999,
        'R_peri_P_unc': -9.999,
        'N_orbits': -9.999
    }

    #loop over N orbits
    for i in range(0, N_orbits):

        #generate input parameters by randomly selecting from a normal distribution with spread = input uncertainty
        rand_dict = req_params.copy()

        rand_dict['dist'] = np.random.normal(loc=req_dict["dist"],
                                             scale=opt_dict["edist"])
        rand_dict['rv'] = np.random.normal(loc=req_dict["rv"],
                                           scale=opt_dict["erv"])

        rand_dict['pmra'] = np.random.normal(loc=req_dict["pmra"],
                                             scale=opt_dict["epmra"])
        rand_dict['pmdec'] = np.random.normal(loc=req_dict["pmdec"],
                                              scale=opt_dict["epmdec"])

        rand_dict['pml'], rand_dict['pmb'] = b_c.pmrapmdec_to_pmllpmbb(
            rand_dict['pmra'],
            rand_dict['pmdec'],
            req_dict['ra'],
            req_dict['dec'],
            degree=True,
            epoch=2000.0)

        #use randomly generated input parameters to calculate positions, velocities
        rand_x, rand_y, rand_z = xyz(rand_dict)
        rand_u, rand_v, rand_w, rand_vx_gc, rand_vy_gc, rand_vz_gc = uvw(
            rand_dict)

        rand_calc_dict = collections.OrderedDict([('x_gc', rand_x),
                                                  ('y_gc', rand_y),
                                                  ('z_gc', rand_z),
                                                  ('u', rand_u), ('v', rand_v),
                                                  ('w', rand_w),
                                                  ('vx_gc', rand_vx_gc),
                                                  ('vy_gc', rand_vy_gc),
                                                  ('vz_gc', rand_vz_gc),
                                                  ('R', -9.999),
                                                  ('phi', -9.999),
                                                  ('vRg', -9.999),
                                                  ('vTg', -9.999)])

        rand_calc_dict['R'], rand_calc_dict['phi'] = cyl_coords(rand_calc_dict)
        rand_calc_dict['vRg'], rand_calc_dict['vTg'] = cylindrical_vs(
            rand_calc_dict)

        returned_orb = orbit_1_staeckel(rand_dict, rand_calc_dict)

        for key in returned_orb:
            if (returned_orb['bound'] == True and key in orbit_params):
                orbit_params[key].append(returned_orb[key])

        if (returned_orb['bound'] == True):
            count_orbits += 1.

    orbit_unc['N_orbits'] = count_orbits

    fits_dir_name = "{}{}_uncertainty_fits".format(final_dir_name, method)

    if not os.path.exists(fits_dir_name):
        os.makedirs(fits_dir_name)

    fits_dir_name = fits_dir_name + "/"

    f, axarr = plt.subplots(5, 2, figsize=(12, 10))

    row_num = 0
    col_num = 0

    for key in orbit_params:

        data = orbit_params[key]

        #fit a gaussian to the randomly generated orbital parameters
        mu, std = norm.fit(data)

        #uncertainty on parameter = standard dev of fit
        orbit_unc['{}_unc'.format(key)] = std

        #plot the histogram
        axarr[row_num, col_num].hist(data,
                                     bins=25,
                                     normed=True,
                                     facecolor='none',
                                     edgecolor="black",
                                     histtype="step")

        #plot the fit
        xmin, xmax = axarr[row_num, col_num].get_xlim()
        x = np.linspace(xmin, xmax, 100)
        p = norm.pdf(x, mu, std)
        axarr[row_num, col_num].plot(x, p, 'k', linewidth=2)

        #plot the calculated parameter value
        axarr[row_num, col_num].axvline(x=calc_dict[key],
                                        color='b',
                                        ls='--',
                                        lw=2)

        #plot the mean of the fit
        axarr[row_num, col_num].axvline(x=mu, color='k', lw=2)

        axarr[row_num, col_num].set_title("{} (mu = {}, std = {})".format(
            key, round(mu, 2), round(std, 2)))

        if (col_num == 0):
            col_num += 1
        else:
            col_num = 0
            row_num += 1

    plt.tight_layout()
    plt.savefig("{}{}_{}_{}_fits.png".format(fits_dir_name, main_title,
                                             req_dict['name'], method))
    plt.close()

    return orbit_unc
def obs_to_galcen(ra,
                  dec,
                  dist,
                  pmra,
                  pmdec,
                  rv,
                  pmra_err,
                  pmdec_err,
                  pmra_pmdec_corr,
                  dist_err,
                  rv_err,
                  return_cov=True,
                  verbose=True,
                  return_rphiz=True,
                  ro=8.,
                  vo=220.,
                  zo=0.025,
                  parallax=False):
    vxvv = np.dstack([ra, dec, dist, pmra, pmdec, rv])[0]
    ra, dec = vxvv[:, 0], vxvv[:, 1]
    lb = bovy_coords.radec_to_lb(ra, dec, degree=True)
    pmra, pmdec = vxvv[:, 3], vxvv[:, 4]
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(pmra,
                                                 pmdec,
                                                 ra,
                                                 dec,
                                                 degree=True)
    d, vlos = vxvv[:, 2], vxvv[:, 5]
    if parallax:
        d = 1. / d
    rectgal = bovy_coords.sphergal_to_rectgal(lb[:, 0],
                                              lb[:, 1],
                                              d,
                                              vlos,
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              degree=True)
    vsolar = np.array([-10.1, 4.0, 6.7])
    vsun = np.array([
        0.,
        1.,
        0.,
    ]) + vsolar / vo
    X = rectgal[:, 0] / ro
    Y = rectgal[:, 1] / ro
    Z = rectgal[:, 2] / ro
    vx = rectgal[:, 3] / vo
    vy = rectgal[:, 4] / vo
    vz = rectgal[:, 5] / vo
    XYZ = np.dstack([X, Y, Z])[0]
    vxyz = np.dstack([vx, vy, vz])[0]
    if return_rphiz:
        Rpz = bovy_coords.XYZ_to_galcencyl(XYZ[:, 0],
                                           XYZ[:, 1],
                                           XYZ[:, 2],
                                           Zsun=zo / ro)
        vRvTvz = bovy_coords.vxvyvz_to_galcencyl(vxyz[:, 0],
                                                 vxyz[:, 1],
                                                 vxyz[:, 2],
                                                 Rpz[:, 0],
                                                 Rpz[:, 1],
                                                 Rpz[:, 2],
                                                 vsun=vsun,
                                                 Xsun=1.,
                                                 Zsun=zo / ro,
                                                 galcen=True)
    if return_cov == True:
        cov_pmradec = np.empty([len(pmra_err), 2, 2])
        cov_pmradec[:, 0, 0] = pmra_err**2
        cov_pmradec[:, 1, 1] = pmdec_err**2
        cov_pmradec[:, 0, 1] = pmra_pmdec_corr * pmra_err * pmdec_err
        cov_pmradec[:, 1, 0] = pmra_pmdec_corr * pmra_err * pmdec_err
        if verbose:
            print('propagating covariance in pmra pmdec -> pmll pmbb')
        cov_pmllbb = bovy_coords.cov_pmrapmdec_to_pmllpmbb(cov_pmradec,
                                                           vxvv[:, 0],
                                                           vxvv[:, 1],
                                                           degree=True,
                                                           epoch='J2015')
        if verbose:
            print('propagating covariance in pmll pmbb -> vx vy vz')
        cov_vxyz = bovy_coords.cov_dvrpmllbb_to_vxyz(vxvv[:, 2], dist_err,
                                                     rv_err, pmllpmbb[:, 0],
                                                     pmllpmbb[:,
                                                              1], cov_pmllbb,
                                                     lb[:, 0], lb[:, 1])
        if not return_rphiz:
            return XYZ, vxyz, cov_vxyz

        if verbose:
            print('propagating covariance in vx vy vz -> vR vT vz')
        cov_galcencyl = bovy_coords.cov_vxyz_to_galcencyl(cov_vxyz,
                                                          Rpz[:, 1],
                                                          Xsun=1.,
                                                          Zsun=zo / ro)
        return XYZ, vxyz, cov_vxyz, Rpz, vRvTvz, cov_galcencyl
    if not return_rphiz:
        return XYZ, vxyz
    return XYZ, vxyz, Rpz, vRvTvz
def dat_to_galcen(
        dat,
        return_cov=True,
        return_rphiz=True,
        verbose=False,
        ro=8.,
        vo=220.,
        zo=0.025,
        keys=['ra', 'dec', 'BPG_meandist', 'pmra', 'pmdec', 'VHELIO_AVG'],
        cov_keys=[
            'pmra_error', 'pmdec_error', 'pmra_pmdec_corr', 'BPG_diststd',
            'VERR'
        ],
        parallax=False):
    vxvv = np.dstack([dat[keys[i]] for i in range(len(keys))])[0]
    ra, dec = vxvv[:, 0], vxvv[:, 1]
    lb = bovy_coords.radec_to_lb(ra, dec, degree=True)
    pmra, pmdec = vxvv[:, 3], vxvv[:, 4]
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(pmra,
                                                 pmdec,
                                                 ra,
                                                 dec,
                                                 degree=True)
    d, vlos = vxvv[:, 2], vxvv[:, 5]
    if parallax:
        d = 1. / d
    rectgal = bovy_coords.sphergal_to_rectgal(lb[:, 0],
                                              lb[:, 1],
                                              d,
                                              vlos,
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              degree=True)
    vsolar = np.array([-11.1, 245.6,
                       7.25])  #use SBD10 vR and vZ and SGR proper motion vT
    vsun = np.array([
        0.,
        0.,
        0.,
    ]) + vsolar / vo
    X = rectgal[:, 0] / ro
    Y = rectgal[:, 1] / ro
    Z = rectgal[:, 2] / ro
    vx = rectgal[:, 3] / vo
    vy = rectgal[:, 4] / vo
    vz = rectgal[:, 5] / vo
    XYZ = np.dstack([X, Y, Z])[0]
    vxyz = np.dstack([vx, vy, vz])[0]
    if return_rphiz:
        Rpz = bovy_coords.XYZ_to_galcencyl(XYZ[:, 0],
                                           XYZ[:, 1],
                                           XYZ[:, 2],
                                           Zsun=zo / ro)
        vRvTvz = bovy_coords.vxvyvz_to_galcencyl(vxyz[:, 0],
                                                 vxyz[:, 1],
                                                 vxyz[:, 2],
                                                 Rpz[:, 0],
                                                 Rpz[:, 1],
                                                 Rpz[:, 2],
                                                 vsun=vsun,
                                                 Xsun=1.,
                                                 Zsun=zo / ro,
                                                 galcen=True)
    if return_cov == True:
        cov_pmradec = np.array([[[
            dat[cov_keys[0]][i]**2,
            dat[cov_keys[2]][i] * dat[cov_keys[0]][i] * dat[cov_keys[1]][i]
        ],
                                 [
                                     dat[cov_keys[2]][i] *
                                     dat[cov_keys[0]][i] * dat[cov_keys[1]][i],
                                     dat[cov_keys[1]][i]**2
                                 ]] for i in range(len(dat))])
        if verbose:
            print('propagating covariance in pmra pmdec -> pmll pmbb')
        cov_pmllbb = bovy_coords.cov_pmrapmdec_to_pmllpmbb(cov_pmradec,
                                                           vxvv[:, 0],
                                                           vxvv[:, 1],
                                                           degree=True,
                                                           epoch='J2015')
        if verbose:
            print('propagating covariance in pmll pmbb -> vx vy vz')
        cov_vxyz = bovy_coords.cov_dvrpmllbb_to_vxyz(vxvv[:,
                                                          2], dat[cov_keys[3]],
                                                     dat[cov_keys[4]],
                                                     pmllpmbb[:,
                                                              0], pmllpmbb[:,
                                                                           1],
                                                     cov_pmllbb, lb[:,
                                                                    0], lb[:,
                                                                           1])
        if not return_rphiz:
            return XYZ, vxyz, cov_vxyz

        if verbose:
            print('propagating covariance in vx vy vz -> vR vT vz')
        cov_galcencyl = bovy_coords.cov_vxyz_to_galcencyl(cov_vxyz,
                                                          Rpz[:, 1],
                                                          Xsun=1.,
                                                          Zsun=zo / ro)
        return XYZ, vxyz, cov_vxyz, Rpz, vRvTvz, cov_galcencyl
    if not return_rphiz:
        return XYZ, vxyz
    return XYZ, vxyz, Rpz, vRvTvz
示例#17
0
            pmradec_corrs = star['pmra_pmdec_corr'][sindx]
            # age [Fe/H] only for Galaxia
            fehs_true = np.zeros_like(e_plxs)
            ages_true = np.zeros_like(e_plxs)

        # convert deg -> rad
        glonrads = glons * np.pi / 180.0
        glatrads = glats * np.pi / 180.0

        # get observed position and velocity
        dists_obs = 1.0 / plxs_obs

        # velocity
        if flagGalaxia == True:
            Tpmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb( \
                pmras_obs, pmdecs_obs, ras, \
                decs, degree=True, epoch=epoch)
        else:
            Tpmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb( \
                pmras_obs, pmdecs_obs, ras, \
                decs, degree=True)
        pmlons_obs = Tpmllpmbb[:, 0]
        pmlats_obs = Tpmllpmbb[:, 1]
        # mas/yr -> km/s
        vlons_obs = pmvconst * pmlons_obs * dists_obs
        vlats_obs = pmvconst * pmlats_obs * dists_obs
        # galactic position
        distxys_obs = dists_obs * np.cos(glatrads)
        xpos_obs = distxys_obs * np.cos(glonrads)
        ypos_obs = distxys_obs * np.sin(glonrads)
        zpos_obs = dists_obs * np.sin(glatrads)
def calc_eccentricity(args, options):
    table = os.path.join(args[0], 'table2.dat')
    readme = os.path.join(args[0], 'ReadMe')
    dierickx = ascii.read(table, readme=readme)
    vxvv = np.dstack([
        dierickx['RAdeg'], dierickx['DEdeg'], dierickx['Dist'] / 1e3,
        dierickx['pmRA'], dierickx['pmDE'], dierickx['HRV']
    ])[0]
    ro, vo, zo = 8., 220., 0.025
    ra, dec = vxvv[:, 0], vxvv[:, 1]
    lb = bovy_coords.radec_to_lb(ra, dec, degree=True)
    pmra, pmdec = vxvv[:, 3], vxvv[:, 4]
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(pmra,
                                                 pmdec,
                                                 ra,
                                                 dec,
                                                 degree=True)
    d, vlos = vxvv[:, 2], vxvv[:, 5]
    rectgal = bovy_coords.sphergal_to_rectgal(lb[:, 0],
                                              lb[:, 1],
                                              d,
                                              vlos,
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              degree=True)
    vsolar = np.array([-10.1, 4.0, 6.7])
    vsun = np.array([
        0.,
        1.,
        0.,
    ]) + vsolar / vo
    X = rectgal[:, 0] / ro
    Y = rectgal[:, 1] / ro
    Z = rectgal[:, 2] / ro
    vx = rectgal[:, 3] / vo
    vy = rectgal[:, 4] / vo
    vz = rectgal[:, 5] / vo
    vsun = np.array([
        0.,
        1.,
        0.,
    ]) + vsolar / vo
    Rphiz = bovy_coords.XYZ_to_galcencyl(X, Y, Z, Zsun=zo / ro)
    vRvTvz = bovy_coords.vxvyvz_to_galcencyl(vx,
                                             vy,
                                             vz,
                                             Rphiz[:, 0],
                                             Rphiz[:, 1],
                                             Rphiz[:, 2],
                                             vsun=vsun,
                                             Xsun=1.,
                                             Zsun=zo / ro,
                                             galcen=True)
    #do the integration and individual analytic estimate for each object
    ts = np.linspace(0., 20., 10000)
    lp = LogarithmicHaloPotential(normalize=1.)
    e_ana = numpy.zeros(len(vxvv))
    e_int = numpy.zeros(len(vxvv))
    print(
        'Performing orbit integration and analytic parameter estimates for Dierickx et al. sample...'
    )
    for i in tqdm(range(len(vxvv))):
        try:
            orbit = Orbit(vxvv[i], radec=True, vo=220., ro=8.)
            e_ana[i] = orbit.e(analytic=True, pot=lp, c=True)
        except UnboundError:
            e_ana[i] = np.nan
        orbit.integrate(ts, lp)
        e_int[i] = orbit.e(analytic=False)
    fig = plt.figure()
    fig.set_size_inches(1.5 * columnwidth, 1.5 * columnwidth)
    plt.scatter(e_int, e_ana, s=1, color='Black', lw=0.)
    plt.xlabel(r'$\mathrm{galpy\ integrated}\ e$')
    plt.ylabel(r'$\mathrm{galpy\ analytic}\ e$')
    plt.xlim(0., 1.)
    plt.ylim(0., 1.)
    fig.tight_layout()
    plt.savefig(os.path.join(args[0], 'dierickx-integratedeanalytice.png'),
                format='png',
                dpi=200)
    fig = plt.figure()
    fig.set_size_inches(1.5 * columnwidth, 1.5 * columnwidth)
    plt.hist(e_int, bins=30)
    plt.xlim(0., 1.)
    plt.xlabel(r'$\mathrm{galpy}\ e$')
    fig.tight_layout()
    plt.savefig(os.path.join(args[0], 'dierickx-integratedehist.png'),
                format='png',
                dpi=200)
    fig = plt.figure()
    fig.set_size_inches(1.5 * columnwidth, 1.5 * columnwidth)
    plt.scatter(dierickx['e'], e_int, s=1, color='Black', lw=0.)
    plt.xlabel(r'$\mathrm{Dierickx\ et\ al.}\ e$')
    plt.ylabel(r'$\mathrm{galpy\ integrated}\ e$')
    plt.xlim(0., 1.)
    plt.ylim(0., 1.)
    fig.tight_layout()
    plt.savefig(os.path.join(args[0], 'dierickx-integratedee.png'),
                format='png',
                dpi=200)
    fig = plt.figure()
    fig.set_size_inches(1.5 * columnwidth, 1.5 * columnwidth)
    plt.scatter(dierickx['e'], e_ana, s=1, color='Black', lw=0.)
    plt.xlabel(r'$\mathrm{Dierickx\ et\ al.}\ e$')
    plt.ylabel(r'$\mathrm{galpy\ estimated}\ e$')
    plt.xlim(0., 1.)
    plt.ylim(0., 1.)
    fig.tight_layout()
    plt.savefig(os.path.join(args[0], 'dierickx-analyticee.png'),
                format='png',
                dpi=200)
    arr = numpy.recarray(len(e_ana),
                         dtype=[('analytic_e', float),
                                ('integrated_e', float)])
    arr['analytic_e'] = e_ana
    arr['integrated_e'] = e_int
    with open(os.path.join(args[0], 'eccentricities.dat'), 'w') as file:
        pickle.dump(arr, file)
        file.close()
示例#19
0
    def shuffle(self,
                GRVScut=16.,
                vtotcut=450 * u.km / u.s,
                dustmap=None,
                N=100):
        '''
            Randomize the galactocentric right ascension and
            computes how many stars satisfy the photometric/dynamical conditions GRVS < GRVScut, vtot<vtotcut. Do this N times.

            Parameters
            ----------
                GRVScut : float
                    Magnitude cut to impose
                vtotcut : Quantity
                    Galactocentric velocity cut to impose
                dustmap : DustMap
                    Dustmap to be used to compute the photometry
                N : int
                    Number of random realizations

            Returns
            -------
                NGaia : ndarray
                    Array of size N containing the number of selected stars per each realization.
        '''
        import astropy.coordinates as coord
        from galpy.util.bovy_coords import radec_to_lb, pmrapmdec_to_pmllpmbb

        vSun = [
            -self.solarmotion[0], self.solarmotion[1], self.solarmotion[2]
        ] * u.km / u.s  # (U, V, W)
        vrot = [0., 220., 0.] * u.km / u.s
        RSun = 8. * u.kpc
        zSun = 0 * u.pc
        v_sun = coord.CartesianDifferential(vSun + vrot)
        GCCS = coord.Galactocentric(galcen_distance=RSun,
                                    z_sun=zSun,
                                    galcen_v_sun=v_sun)
        GCS = coord.Galactic()

        data = radec_to_lb(self.ra.to('deg').value,
                           self.dec.to('deg').value,
                           degree=True)
        ll, bb = data[:, 0], data[:, 1]
        data = pmrapmdec_to_pmllpmbb(self.pmra,
                                     self.pmdec,
                                     self.ra.to('deg').value,
                                     self.dec.to('deg').value,
                                     degree=True)
        pmll, pmbb = data[:, 0], data[:, 1]

        galactic_coords = coord.Galactic(l=ll * u.deg,
                                         b=bb * u.deg,
                                         distance=self.dist,
                                         pm_l_cosb=pmll * u.mas / u.yr,
                                         pm_b=pmbb * u.mas / u.yr,
                                         radial_velocity=self.vlos)
        galactocentric_coords = galactic_coords.transform_to(GCCS)

        phi = np.arctan2(galactocentric_coords.y, galactocentric_coords.x)

        r = (galactocentric_coords.y**2. + galactocentric_coords.x**2.)**0.5
        self.vtot = np.sqrt(galactocentric_coords.v_x**2. +
                            galactocentric_coords.v_y**2. +
                            galactocentric_coords.v_z**2.).to(u.km / u.s)
        NGaia = np.zeros(N)

        for i in xrange(N):
            phi2 = (2 * np.random.random(phi.size) - 1) * np.pi

            galactocentric_coords2 = coord.Galactocentric(
                x=r * np.cos(phi2),
                y=r * np.sin(phi2),
                z=galactocentric_coords.z,
                galcen_distance=RSun,
                z_sun=zSun,
                galcen_v_sun=v_sun)

            galactic2 = galactocentric_coords2.transform_to(GCS)
            self.ll, self.bb = galactic2.l.to(u.deg).value, galactic2.b.to(
                u.deg).value
            self.dist = galactic2.distance

            self.photometry(dustmap=dustmap, v=False)

            NGaia[i] = ((self.GRVS < GRVScut) & (self.vtot > vtotcut)).sum()

        self.cattype = 2

        return NGaia
示例#20
0
    def photometry(self, dustmap=None, v=True):
        '''
        Computes the Grvs-magnitudes and total velocity in Galactocentric restframe.

        Parameters
        ----------
            dustmap : DustMap
                Dustmap object to be used
        '''

        from hvs.utils import DustMap
        from hvs.utils.gaia import get_GRVS
        from galpy.util.bovy_coords import radec_to_lb

        if (self.cattype < 1):
            raise RuntimeError('The catalog needs to be propagated!')

        if (not isinstance(dustmap, DustMap)):
            raise ValueError(
                'You must provide an instance of the class DustMap.')

        if (hasattr(self, 'll')):
            self.GRVS, self.V, self.G, self.e_par, self.e_pmra, self.pmdec = get_GRVS(
                self.dist.to('kpc').value, self.ll, self.bb,
                self.m.to('Msun').value,
                self.tage.to('Myr').value, dustmap)
        else:
            data = radec_to_lb(self.ra.to('deg').value,
                               self.dec.to('deg').value,
                               degree=True)
            l, b = data[:, 0], data[:, 1]

            self.GRVS, self.V, self.G, self.e_par, self.e_pmra, self.e_pmdec = get_GRVS(
                self.dist.to('kpc').value, l, b,
                self.m.to('Msun').value,
                self.tage.to('Myr').value, dustmap)

        self.e_par = self.e_par * u.uas
        self.e_pmra = self.e_pmra * u.uas / u.yr
        self.e_pmdec = self.e_pmdec * u.uas / u.yr
        if (v):
            import astropy.coordinates as coord
            from galpy.util.bovy_coords import radec_to_lb, pmrapmdec_to_pmllpmbb

            vSun = [
                -self.solarmotion[0], self.solarmotion[1], self.solarmotion[2]
            ] * u.km / u.s  # (U, V, W)
            vrot = [0., 220., 0.] * u.km / u.s
            RSun = 8. * u.kpc
            zSun = 0 * u.pc
            v_sun = coord.CartesianDifferential(vSun + vrot)
            GCCS = coord.Galactocentric(galcen_distance=RSun,
                                        z_sun=zSun,
                                        galcen_v_sun=v_sun)

            data = radec_to_lb(self.ra.to('deg').value,
                               self.dec.to('deg').value,
                               degree=True)
            ll, bb = data[:, 0], data[:, 1]
            data = pmrapmdec_to_pmllpmbb(self.pmra,
                                         self.pmdec,
                                         self.ra.to('deg').value,
                                         self.dec.to('deg').value,
                                         degree=True)
            pmll, pmbb = data[:, 0], data[:, 1]

            galactic_coords = coord.Galactic(l=ll * u.deg,
                                             b=bb * u.deg,
                                             distance=self.dist,
                                             pm_l_cosb=pmll * u.mas / u.yr,
                                             pm_b=pmbb * u.mas / u.yr,
                                             radial_velocity=self.vlos)
            galactocentric_coords = galactic_coords.transform_to(GCCS)

            self.GCv = np.sqrt(galactocentric_coords.v_x**2. +
                               galactocentric_coords.v_y**2. +
                               galactocentric_coords.v_z**2.).to(u.km / u.s)
            self.GCdist = np.sqrt(galactocentric_coords.x**2. +
                                  galactocentric_coords.y**2. +
                                  galactocentric_coords.z**2.).to(u.kpc)

        self.cattype = 2
示例#21
0
def make_rcsample(parser):
    options,args= parser.parse_args()
    savefilename= options.savefilename
    if savefilename is None:
        #Create savefilename if not given
        savefilename= os.path.join(appath._APOGEE_DATA,
                                   'rcsample_'+appath._APOGEE_REDUX+'.fits')
        print("Saving to %s ..." % savefilename)
    #Read the base-sample
    data= apread.allStar(adddist=_ADDHAYDENDIST,rmdups=options.rmdups)
    #Remove a bunch of fields that we do not want to keep
    data= esutil.numpy_util.remove_fields(data,
                                          ['TARGET_ID',
                                           'FILE',
                                           'AK_WISE',
                                           'SFD_EBV',
                                           'SYNTHVHELIO_AVG',
                                           'SYNTHVSCATTER',
                                           'SYNTHVERR',
                                           'SYNTHVERR_MED',
                                           'RV_TEFF',
                                           'RV_LOGG',
                                           'RV_FEH',
                                           'RV_ALPHA',
                                           'RV_CARB',
                                           'RV_CCFWHM',
                                           'RV_AUTOFWHM',
                                           'SYNTHSCATTER',
                                           'STABLERV_CHI2',
                                           'STABLERV_RCHI2',
                                           'STABLERV_CHI2_PROB',
                                           'CHI2_THRESHOLD',
                                           'APSTAR_VERSION',
                                           'ASPCAP_VERSION',
                                           'RESULTS_VERSION',
                                           'WASH_M',
                                           'WASH_M_ERR',
                                           'WASH_T2',
                                           'WASH_T2_ERR',
                                           'DDO51',
                                           'DDO51_ERR',
                                           'IRAC_3_6',
                                           'IRAC_3_6_ERR',
                                           'IRAC_4_5',
                                           'IRAC_4_5_ERR',
                                           'IRAC_5_8',
                                           'IRAC_5_8_ERR',
                                           'IRAC_8_0',
                                           'IRAC_8_0_ERR',
                                           'WISE_4_5',
                                           'WISE_4_5_ERR',
                                           'TARG_4_5',
                                           'TARG_4_5_ERR',
                                           'WASH_DDO51_GIANT_FLAG',
                                           'WASH_DDO51_STAR_FLAG',
                                           'REDUCTION_ID',
                                           'SRC_H',
                                           'PM_SRC'])
    if not appath._APOGEE_REDUX.lower() == 'current' \
            and not 'l30' in appath._APOGEE_REDUX \
            and int(appath._APOGEE_REDUX[1:]) < 500:
        data= esutil.numpy_util.remove_fields(data,
                                              ['ELEM'])
    #Select red-clump stars
    jk= data['J0']-data['K0']
    z= isodist.FEH2Z(data['METALS'],zsolar=0.017)
    if 'l30' in appath._APOGEE_REDUX:
        logg= data['LOGG']
    elif appath._APOGEE_REDUX.lower() == 'current' \
            or int(appath._APOGEE_REDUX[1:]) > 600:
        from apogee.tools import paramIndx
        if False:
            #Use my custom logg calibration that's correct for the RC
            logg= (1.-0.042)*data['FPARAM'][:,paramIndx('logg')]-0.213
            lowloggindx= data['FPARAM'][:,paramIndx('logg')] < 1.
            logg[lowloggindx]= data['FPARAM'][lowloggindx,paramIndx('logg')]-0.255
            hiloggindx= data['FPARAM'][:,paramIndx('logg')] > 3.8
            logg[hiloggindx]= data['FPARAM'][hiloggindx,paramIndx('logg')]-0.3726
        else:
            #Use my custom logg calibration that's correct on average
            logg= (1.+0.03)*data['FPARAM'][:,paramIndx('logg')]-0.37
            lowloggindx= data['FPARAM'][:,paramIndx('logg')] < 1.
            logg[lowloggindx]= data['FPARAM'][lowloggindx,paramIndx('logg')]-0.34
            hiloggindx= data['FPARAM'][:,paramIndx('logg')] > 3.8
            logg[hiloggindx]= data['FPARAM'][hiloggindx,paramIndx('logg')]-0.256
    else:
        logg= data['LOGG']
    indx= (jk < 0.8)*(jk >= 0.5)\
        *(z <= 0.06)\
        *(z <= rcmodel.jkzcut(jk,upper=True))\
        *(z >= rcmodel.jkzcut(jk))\
        *(logg >= rcmodel.loggteffcut(data['TEFF'],z,upper=False))\
        *(logg <= rcmodel.loggteffcut(data['TEFF'],z,upper=True))
    data= data[indx]
    #Add more aggressive flag cut
    data= esutil.numpy_util.add_fields(data,[('ADDL_LOGG_CUT',numpy.int32)])
    data['ADDL_LOGG_CUT']= ((data['TEFF']-4800.)/1000.+2.75) > data['LOGG']
    if options.loggcut:
        data= data[data['ADDL_LOGG_CUT'] == 1]
    print("Making catalog of %i objects ..." % len(data))
    #Add distances
    data= esutil.numpy_util.add_fields(data,[('RC_DIST', float),
                                             ('RC_DM', float),
                                             ('RC_GALR', float),
                                             ('RC_GALPHI', float),
                                             ('RC_GALZ', float)])
    rcd= rcmodel.rcdist()
    jk= data['J0']-data['K0']
    z= isodist.FEH2Z(data['METALS'],zsolar=0.017)
    data['RC_DIST']= rcd(jk,z,appmag=data['K0'])*options.distfac
    data['RC_DM']= 5.*numpy.log10(data['RC_DIST'])+10.
    XYZ= bovy_coords.lbd_to_XYZ(data['GLON'],
                                data['GLAT'],
                                data['RC_DIST'],
                                degree=True)
    R,phi,Z= bovy_coords.XYZ_to_galcencyl(XYZ[:,0],
                                          XYZ[:,1],
                                          XYZ[:,2],
                                          Xsun=8.,Zsun=0.025)
    data['RC_GALR']= R
    data['RC_GALPHI']= phi
    data['RC_GALZ']= Z
    #Save
    fitsio.write(savefilename,data,clobber=True)
    # Add Tycho-2 matches
    if options.tyc2:
        data= esutil.numpy_util.add_fields(data,[('TYC2MATCH',numpy.int32),
                                                 ('TYC1',numpy.int32),
                                                 ('TYC2',numpy.int32),
                                                 ('TYC3',numpy.int32)])
        data['TYC2MATCH']= 0
        data['TYC1']= -1
        data['TYC2']= -1
        data['TYC3']= -1
        # Write positions
        posfilename= tempfile.mktemp('.csv',dir=os.getcwd())
        resultfilename= tempfile.mktemp('.csv',dir=os.getcwd())
        with open(posfilename,'w') as csvfile:
            wr= csv.writer(csvfile,delimiter=',',quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA','DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'],data[ii]['DEC']])
        # Send to CDS for matching
        result= open(resultfilename,'w')
        try:
            subprocess.check_call(['curl',
                                   '-X','POST',
                                   '-F','request=xmatch',
                                   '-F','distMaxArcsec=2',
                                   '-F','RESPONSEFORMAT=csv',
                                   '-F','cat1=@%s' % os.path.basename(posfilename),
                                   '-F','colRA1=RA',
                                   '-F','colDec1=DEC',
                                   '-F','cat2=vizier:Tycho2',
                                   'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Directly match on input RA
        ma= numpy.loadtxt(resultfilename,delimiter=',',skiprows=1,
                          usecols=(1,2,7,8,9))
        iis= numpy.arange(len(data))
        mai= [iis[data['RA'] == ma[ii,0]][0] for ii in range(len(ma))]
        data['TYC2MATCH'][mai]= 1
        data['TYC1'][mai]= ma[:,2]
        data['TYC2'][mai]= ma[:,3]
        data['TYC3'][mai]= ma[:,4]
        os.remove(posfilename)
        os.remove(resultfilename)
    if not options.nostat:
        #Determine statistical sample and add flag
        apo= apogee.select.apogeeSelect()
        statIndx= apo.determine_statistical(data)
        mainIndx= apread.mainIndx(data)
        data= esutil.numpy_util.add_fields(data,[('STAT',numpy.int32),
                                                 ('INVSF',float)])
        data['STAT']= 0
        data['STAT'][statIndx*mainIndx]= 1
        for ii in range(len(data)):
            if (statIndx*mainIndx)[ii]:
                data['INVSF'][ii]= 1./apo(data['LOCATION_ID'][ii],
                                          data['H'][ii])
            else:
                data['INVSF'][ii]= -1.
    if options.nopm:
        fitsio.write(savefilename,data,clobber=True)       
        return None
    #Get proper motions, in a somewhat roundabout way
    pmfile= savefilename.split('.')[0]+'_pms.fits'
    if os.path.exists(pmfile):
        pmdata= fitsio.read(pmfile,1)
    else:
        pmdata= numpy.recarray(len(data),
                               formats=['f8','f8','f8','f8','f8','f8','i4'],
                               names=['RA','DEC','PMRA','PMDEC',
                                      'PMRA_ERR','PMDEC_ERR','PMMATCH'])
        # Write positions, again ...
        posfilename= tempfile.mktemp('.csv',dir=os.getcwd())
        resultfilename= tempfile.mktemp('.csv',dir=os.getcwd())
        with open(posfilename,'w') as csvfile:
            wr= csv.writer(csvfile,delimiter=',',quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA','DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'],data[ii]['DEC']])
        # Send to CDS for matching
        result= open(resultfilename,'w')
        try:
            subprocess.check_call(['curl',
                                   '-X','POST',
                                   '-F','request=xmatch',
                                   '-F','distMaxArcsec=4',
                                   '-F','RESPONSEFORMAT=csv',
                                   '-F','cat1=@%s' % os.path.basename(posfilename),
                                   '-F','colRA1=RA',
                                   '-F','colDec1=DEC',
                                   '-F','cat2=vizier:UCAC4',
                                   'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Match back and only keep the closest one
        ma= numpy.loadtxt(resultfilename,delimiter=',',skiprows=1,
                          converters={15: lambda s: float(s.strip() or -9999),
                                      16: lambda s: float(s.strip() or -9999),
                                      17: lambda s: float(s.strip() or -9999),
                                      18: lambda s: float(s.strip() or -9999)},
                          usecols=(4,5,15,16,17,18))
        h=esutil.htm.HTM()
        m1,m2,d12 = h.match(data['RA'],data['DEC'],
                            ma[:,0],ma[:,1],4./3600.,maxmatch=1)
        pmdata['PMMATCH']= 0
        pmdata['RA']= data['RA']
        pmdata['DEC']= data['DEC']
        pmdata['PMMATCH'][m1]= 1
        pmdata['PMRA'][m1]= ma[m2,2]
        pmdata['PMDEC'][m1]= ma[m2,3]
        pmdata['PMRA_ERR'][m1]= ma[m2,4]
        pmdata['PMDEC_ERR'][m1]= ma[m2,5]
        pmdata['PMMATCH'][(pmdata['PMRA'] == -9999) \
                          +(pmdata['PMDEC'] == -9999) \
                          +(pmdata['PMRA_ERR'] == -9999) \
                          +(pmdata['PMDEC_ERR'] == -9999)]= 0
        fitsio.write(pmfile,pmdata,clobber=True)
        #To make sure we're using the same format below
        pmdata= fitsio.read(pmfile,1)
        os.remove(posfilename)
        os.remove(resultfilename)
    #Match proper motions
    try: #These already exist currently, but may not always exist
        data= esutil.numpy_util.remove_fields(data,['PMRA','PMDEC'])
    except ValueError:
        pass
    data= esutil.numpy_util.add_fields(data,[('PMRA', numpy.float),
                                             ('PMDEC', numpy.float),
                                             ('PMRA_ERR', numpy.float),
                                             ('PMDEC_ERR', numpy.float),
                                             ('PMMATCH',numpy.int32)])
    data['PMMATCH']= 0
    h=esutil.htm.HTM()
    m1,m2,d12 = h.match(pmdata['RA'],pmdata['DEC'],
                        data['RA'],data['DEC'],
                        2./3600.,maxmatch=1)
    data['PMRA'][m2]= pmdata['PMRA'][m1]
    data['PMDEC'][m2]= pmdata['PMDEC'][m1]
    data['PMRA_ERR'][m2]= pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR'][m2]= pmdata['PMDEC_ERR'][m1]
    data['PMMATCH'][m2]= pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx= data['PMMATCH'] == 1
    data['PMRA'][True-pmindx]= -9999.99
    data['PMDEC'][True-pmindx]= -9999.99
    data['PMRA_ERR'][True-pmindx]= -9999.99
    data['PMDEC_ERR'][True-pmindx]= -9999.99
    #Calculate Galactocentric velocities
    data= esutil.numpy_util.add_fields(data,[('GALVR', numpy.float),
                                             ('GALVT', numpy.float),
                                             ('GALVZ', numpy.float)])
    lb= bovy_coords.radec_to_lb(data['RA'],data['DEC'],degree=True)
    XYZ= bovy_coords.lbd_to_XYZ(lb[:,0],lb[:,1],data['RC_DIST'],degree=True)
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA'],data['PMDEC'],
                                                data['RA'],data['DEC'],
                                                degree=True)
    vxvyvz= bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                             pmllpmbb[:,0],
                                             pmllpmbb[:,1],
                                             lb[:,0],lb[:,1],data['RC_DIST'],
                                             degree=True)
    vR, vT, vZ= bovy_coords.vxvyvz_to_galcencyl(vxvyvz[:,0],
                                                vxvyvz[:,1],
                                                vxvyvz[:,2],
                                                8.-XYZ[:,0],
                                                XYZ[:,1],
                                                XYZ[:,2]+0.025,
                                                vsun=[-11.1,30.24*8.,7.25])#Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR']= vR
    data['GALVT']= vT
    data['GALVZ']= vZ
    data['GALVR'][True-pmindx]= -9999.99
    data['GALVT'][True-pmindx]= -9999.99
    data['GALVZ'][True-pmindx]= -9999.99
    #Get PPMXL proper motions, in a somewhat roundabout way
    pmfile= savefilename.split('.')[0]+'_pms_ppmxl.fits'
    if os.path.exists(pmfile):
        pmdata= fitsio.read(pmfile,1)
    else:
        pmdata= numpy.recarray(len(data),
                               formats=['f8','f8','f8','f8','f8','f8','i4'],
                               names=['RA','DEC','PMRA','PMDEC',
                                      'PMRA_ERR','PMDEC_ERR','PMMATCH'])
        # Write positions, again ...
        posfilename= tempfile.mktemp('.csv',dir=os.getcwd())
        resultfilename= tempfile.mktemp('.csv',dir=os.getcwd())
        with open(posfilename,'w') as csvfile:
            wr= csv.writer(csvfile,delimiter=',',quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA','DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'],data[ii]['DEC']])
        # Send to CDS for matching
        result= open(resultfilename,'w')
        try:
            subprocess.check_call(['curl',
                                   '-X','POST',
                                   '-F','request=xmatch',
                                   '-F','distMaxArcsec=4',
                                   '-F','RESPONSEFORMAT=csv',
                                   '-F','cat1=@%s' % os.path.basename(posfilename),
                                   '-F','colRA1=RA',
                                   '-F','colDec1=DEC',
                                   '-F','cat2=vizier:PPMXL',
                                   'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Match back and only keep the closest one
        ma= numpy.loadtxt(resultfilename,delimiter=',',skiprows=1,
                          converters={15: lambda s: float(s.strip() or -9999),
                                      16: lambda s: float(s.strip() or -9999),
                                      17: lambda s: float(s.strip() or -9999),
                                      18: lambda s: float(s.strip() or -9999)},
                          usecols=(4,5,15,16,19,20))
        h=esutil.htm.HTM()
        m1,m2,d12 = h.match(data['RA'],data['DEC'],
                            ma[:,0],ma[:,1],4./3600.,maxmatch=1)
        pmdata['PMMATCH']= 0
        pmdata['RA']= data['RA']
        pmdata['DEC']= data['DEC']
        pmdata['PMMATCH'][m1]= 1
        pmdata['PMRA'][m1]= ma[m2,2]
        pmdata['PMDEC'][m1]= ma[m2,3]
        pmdata['PMRA_ERR'][m1]= ma[m2,4]
        pmdata['PMDEC_ERR'][m1]= ma[m2,5]
        pmdata['PMMATCH'][(pmdata['PMRA'] == -9999) \
                          +(pmdata['PMDEC'] == -9999) \
                          +(pmdata['PMRA_ERR'] == -9999) \
                          +(pmdata['PMDEC_ERR'] == -9999)]= 0
        fitsio.write(pmfile,pmdata,clobber=True)
        #To make sure we're using the same format below
        pmdata= fitsio.read(pmfile,1)
        os.remove(posfilename)
        os.remove(resultfilename)
    #Match proper motions to ppmxl
    data= esutil.numpy_util.add_fields(data,[('PMRA_PPMXL', numpy.float),
                                             ('PMDEC_PPMXL', numpy.float),
                                             ('PMRA_ERR_PPMXL', numpy.float),
                                             ('PMDEC_ERR_PPMXL', numpy.float),
                                             ('PMMATCH_PPMXL',numpy.int32)])
    data['PMMATCH_PPMXL']= 0
    h=esutil.htm.HTM()
    m1,m2,d12 = h.match(pmdata['RA'],pmdata['DEC'],
                        data['RA'],data['DEC'],
                        2./3600.,maxmatch=1)
    data['PMRA_PPMXL'][m2]= pmdata['PMRA'][m1]
    data['PMDEC_PPMXL'][m2]= pmdata['PMDEC'][m1]
    data['PMRA_ERR_PPMXL'][m2]= pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR_PPMXL'][m2]= pmdata['PMDEC_ERR'][m1]
    data['PMMATCH_PPMXL'][m2]= pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx= data['PMMATCH_PPMXL'] == 1
    data['PMRA_PPMXL'][True-pmindx]= -9999.99
    data['PMDEC_PPMXL'][True-pmindx]= -9999.99
    data['PMRA_ERR_PPMXL'][True-pmindx]= -9999.99
    data['PMDEC_ERR_PPMXL'][True-pmindx]= -9999.99
    #Calculate Galactocentric velocities
    data= esutil.numpy_util.add_fields(data,[('GALVR_PPMXL', numpy.float),
                                             ('GALVT_PPMXL', numpy.float),
                                             ('GALVZ_PPMXL', numpy.float)])
    lb= bovy_coords.radec_to_lb(data['RA'],data['DEC'],degree=True)
    XYZ= bovy_coords.lbd_to_XYZ(lb[:,0],lb[:,1],data['RC_DIST'],degree=True)
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA_PPMXL'],
                                                data['PMDEC_PPMXL'],
                                                data['RA'],data['DEC'],
                                                degree=True)
    vxvyvz= bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                             pmllpmbb[:,0],
                                             pmllpmbb[:,1],
                                             lb[:,0],lb[:,1],data['RC_DIST'],
                                             degree=True)
    vR, vT, vZ= bovy_coords.vxvyvz_to_galcencyl(vxvyvz[:,0],
                                                vxvyvz[:,1],
                                                vxvyvz[:,2],
                                                8.-XYZ[:,0],
                                                XYZ[:,1],
                                                XYZ[:,2]+0.025,
                                                vsun=[-11.1,30.24*8.,7.25])#Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR_PPMXL']= vR
    data['GALVT_PPMXL']= vT
    data['GALVZ_PPMXL']= vZ
    data['GALVR_PPMXL'][True-pmindx]= -9999.99
    data['GALVT_PPMXL'][True-pmindx]= -9999.99
    data['GALVZ_PPMXL'][True-pmindx]= -9999.99
    #Save
    fitsio.write(savefilename,data,clobber=True)
    return None
示例#22
0
# Teff
teff_true = star['Teff_true'][sindx]
teff_obs = star['Teff_obs'][sindx]
e_teff = star['e_Teff'][sindx]
# age [Fe/H]
fehs_true = star['[Fe/H]_true'][sindx]
ages_true = star['Age'][sindx]

# convert deg -> rad
glonrads = glons*np.pi/180.0
glatrads = glats*np.pi/180.0

# get true position and velocity
dists_true = 1.0/plxs_true
# velocity
Tpmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(pmras_true, pmdecs_true, ras, \
            decs, degree=True, epoch=2000.0)
pmlons_true = Tpmllpmbb[:,0]
pmlats_true = Tpmllpmbb[:,1]
# mas/yr -> km/s
vlons_true = pmvconst*pmlons_true*dists_true
vlats_true = pmvconst*pmlats_true*dists_true
Tvxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(hrvs_true, Tpmllpmbb[:,0], \
          Tpmllpmbb[:,1], glons, glats, dists_true, XYZ=False, degree=True)
vxs_true = Tvxvyvz[:,0]
vys_true = Tvxvyvz[:,1]
vzs_true = Tvxvyvz[:,2]
# Galactocentric position and velcoity
distxys_true = dists_true*np.cos(glatrads)
xpos_true = distxys_true*np.cos(glonrads)
ypos_true = distxys_true*np.sin(glonrads)
zpos_true = dists_true*np.sin(glatrads)
示例#23
0
hrvv = star['radial_velocity'][sindx]
errhrvv = star['radial_velocity_error'][sindx]
# radian glon and glat
glonradv = glonv * np.pi / 180.0
glatradv = glatv * np.pi / 180.0

# x, y position
xposv = -rsun + np.cos(glonradv) * distv * np.cos(glatradv)
yposv = np.sin(glonradv) * distv * np.cos(glatradv)
zposv = distv * np.sin(glatradv) + zsun
# rgal with Reid et al. value
rgalv = np.sqrt(xposv**2 + yposv**2)

# plx, pmra, pmdec -> vx,vy,vz
# velocity
Tpmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb( \
    pmrav ,pmdecv ,rav ,decv, degree=True, epoch=None)
Tvxvyvz=bovy_coords.vrpmllpmbb_to_vxvyvz( \
    hrvv, Tpmllpmbb[:,0], Tpmllpmbb[:,1], glonv, glatv, distv, \
    XYZ=False, degree=True)
vxv = Tvxvyvz[:, 0] + usun
vyv = Tvxvyvz[:, 1] + vsun
vzv = Tvxvyvz[:, 2] + wsun
# Vcirc at the radius of the stars, including dVc/dR
vcircrv = vcirc + dvcdr * (rgalv - rsun)
vyv = vyv + vcircrv
# original velocity
vxv0 = vxv
vyv0 = vyv
vzv0 = vzv
# Galactic radius and velocities
vradv0 = (vxv0 * xposv + vyv0 * yposv) / rgalv
示例#24
0
def findfriends(targname,radial_velocity,velocity_limit=5.0,search_radius=25.0,rvcut=5.0,radec=[None,None],output_directory = None,showplots=False,verbose=False,DoGALEX=True,DoWISE=True,DoROSAT=True):
    
    radvel= radial_velocity * u.kilometer / u.second
    
    if output_directory == None:
        outdir = './' + targname.replace(" ", "") + '_friends/'
    else: 
        outdir = output_directory
    if os.path.isdir(outdir) == True:
        print('Output directory ' + outdir +' Already Exists!!')
        print('Either Move it, Delete it, or input a different [output_directory] Please!')
        return
    os.mkdir(outdir)
    
    if velocity_limit < 0.00001 : 
        print('input velocity_limit is too small, try something else')
        print('velocity_limit: ' + str(velocity_limit))
    if search_radius < 0.0000001:
        print('input search_radius is too small, try something else')
        print('search_radius: ' + str(search_radius))
     
    # Search parameters
    vlim=velocity_limit * u.kilometer / u.second
    searchradpc=search_radius * u.parsec

    if (radec[0] != None) & (radec[1] != None):
        usera,usedec = radec[0],radec[1]
    else:  ##use the target name to get simbad ra and dec.
        print('Asking Simbad for RA and DEC')
        result_table = Simbad.query_object(targname)
        usera,usedec = result_table['RA'][0],result_table['DEC'][0]
    
    if verbose == True:
        print('Target name: ',targname)
        print('Coordinates: ' + str(usera) +' '+str(usedec))
        print()

    c = SkyCoord( ra=usera , dec=usedec , unit=(u.hourangle, u.deg) , frame='icrs')
    if verbose == True: print(c)

    # Find precise coordinates and distance from Gaia, define search radius and parallax cutoff
    print('Asking Gaia for precise coordinates')
    sqltext = "SELECT * FROM gaiaedr3.gaia_source WHERE CONTAINS( \
               POINT('ICRS',gaiaedr3.gaia_source.ra,gaiaedr3.gaia_source.dec), \
               CIRCLE('ICRS'," + str(c.ra.value) +","+ str(c.dec.value) +","+ str(6.0/3600.0) +"))=1;"
    job = Gaia.launch_job_async(sqltext , dump_to_file=False)
    Pgaia = job.get_results()
    if verbose == True:
        print(sqltext)
        print()
        print(Pgaia['source_id','ra','dec','phot_g_mean_mag','parallax','ruwe'].pprint_all())
        print()

    minpos = Pgaia['phot_g_mean_mag'].tolist().index(min(Pgaia['phot_g_mean_mag']))
    Pcoord = SkyCoord( ra=Pgaia['ra'][minpos]*u.deg , dec=Pgaia['dec'][minpos]*u.deg , \
                      distance=(1000.0/Pgaia['parallax'][minpos])*u.parsec , frame='icrs' , \
                      radial_velocity=radvel , \
                      pm_ra_cosdec=Pgaia['pmra'][minpos]*u.mas/u.year , pm_dec=Pgaia['pmdec'][minpos]*u.mas/u.year )

    searchraddeg = np.arcsin(searchradpc/Pcoord.distance).to(u.deg)
    minpar = (1000.0 * u.parsec) / (Pcoord.distance + searchradpc) * u.mas
    if verbose == True:
        print(Pcoord)
        print()
        print('Search radius in deg: ',searchraddeg)
        print('Minimum parallax: ',minpar)


    # Query Gaia with search radius and parallax cut
    # Note, a cut on parallax_error was added because searches at low galactic latitude 
    # return an overwhelming number of noisy sources that scatter into the search volume - ALK 20210325
    print('Querying Gaia for neighbors')

    Pllbb     = bc.radec_to_lb(Pcoord.ra.value , Pcoord.dec.value , degree=True)
    if ( np.abs(Pllbb[1]) > 10.0): plxcut = max( 0.5 , (1000.0/Pcoord.distance.value/10.0) )
    else: plxcut = 0.5
    print('Parallax cut: ',plxcut)

    if (searchradpc < Pcoord.distance):
        sqltext = "SELECT * FROM gaiaedr3.gaia_source WHERE CONTAINS( \
            POINT('ICRS',gaiaedr3.gaia_source.ra,gaiaedr3.gaia_source.dec), \
            CIRCLE('ICRS'," + str(Pcoord.ra.value) +","+ str(Pcoord.dec.value) +","+ str(searchraddeg.value) +"))\
            =1 AND parallax>" + str(minpar.value) + " AND parallax_error<" + str(plxcut) + ";"
    if (searchradpc >= Pcoord.distance):
        sqltext = "SELECT * FROM gaiaedr3.gaia_source WHERE parallax>" + str(minpar.value) + " AND parallax_error<" + str(plxcut) + ";"
        print('Note, using all-sky search')
    if verbose == True:
        print(sqltext)
        print()

    job = Gaia.launch_job_async(sqltext , dump_to_file=False)
    r = job.get_results()
   
    if verbose == True: print('Number of records: ',len(r['ra']))


    # Construct coordinates array for all stars returned in cone search

    gaiacoord = SkyCoord( ra=r['ra'] , dec=r['dec'] , distance=(1000.0/r['parallax'])*u.parsec , \
                         frame='icrs' , \
                         pm_ra_cosdec=r['pmra'] , pm_dec=r['pmdec'] )

    sep = gaiacoord.separation(Pcoord)
    sep3d = gaiacoord.separation_3d(Pcoord)

    if verbose == True:
        print('Printing angular separations in degrees as sanity check')
        print(sep.degree)



    Pllbb     = bc.radec_to_lb(Pcoord.ra.value , Pcoord.dec.value , degree=True)
    Ppmllpmbb = bc.pmrapmdec_to_pmllpmbb( Pcoord.pm_ra_cosdec.value , Pcoord.pm_dec.value , \
                                         Pcoord.ra.value , Pcoord.dec.value , degree=True )
    Pvxvyvz   = bc.vrpmllpmbb_to_vxvyvz(Pcoord.radial_velocity.value , Ppmllpmbb[0] , Ppmllpmbb[1] , \
                                   Pllbb[0] , Pllbb[1] , Pcoord.distance.value/1000.0 , XYZ=False , degree=True)

    if verbose == True:
        print('Science Target Name: ',targname)
        print('Science Target RA/DEC: ',Pcoord.ra.value,Pcoord.dec.value)
        print('Science Target Galactic Coordinates: ',Pllbb)
        print('Science Target UVW: ',Pvxvyvz)
        print()

    Gllbb = bc.radec_to_lb(gaiacoord.ra.value , gaiacoord.dec.value , degree=True)
    Gxyz = bc.lbd_to_XYZ( Gllbb[:,0] , Gllbb[:,1] , gaiacoord.distance/1000.0 , degree=True)
    Gvrpmllpmbb = bc.vxvyvz_to_vrpmllpmbb( \
                    Pvxvyvz[0]*np.ones(len(Gxyz[:,0])) , Pvxvyvz[1]*np.ones(len(Gxyz[:,1])) , Pvxvyvz[2]*np.ones(len(Gxyz[:,2])) , \
                    Gxyz[:,0] , Gxyz[:,1] , Gxyz[:,2] , XYZ=True)
    Gpmrapmdec = bc.pmllpmbb_to_pmrapmdec( Gvrpmllpmbb[:,1] , Gvrpmllpmbb[:,2] , Gllbb[:,0] , Gllbb[:,1] , degree=True)

    # Code in case I want to do chi^2 cuts someday
    Gvtanerr = 1.0 * np.ones(len(Gxyz[:,0]))
    Gpmerr = Gvtanerr * 206265000.0 * 3.154e7 / (gaiacoord.distance.value * 3.086e13)


    Gchi2 = ( (Gpmrapmdec[:,0]-gaiacoord.pm_ra_cosdec.value)**2 + (Gpmrapmdec[:,1]-gaiacoord.pm_dec.value)**2 )**0.5
    Gchi2 = Gchi2 / Gpmerr
    if verbose == True:
        print('Predicted PMs if comoving:')
        print(Gpmrapmdec , "\n")
        print('Actual PMRAs from Gaia:')
        print(gaiacoord.pm_ra_cosdec.value , "\n")
        print('Actual PMDECs from Gaia:')
        print(gaiacoord.pm_dec.value , "\n")
        print('Predicted PM errors:')
        print(Gpmerr , "\n")
        print('Chi^2 values:')
        print(Gchi2)


    # Query external list(s) of RVs

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) )
    yy = zz[0][np.argsort(sep3d[zz])]
    
    RV    = np.empty(np.array(r['ra']).size)
    RVerr = np.empty(np.array(r['ra']).size)
    RVsrc = np.array([ '                             None' for x in range(np.array(r['ra']).size) ])
    RV[:]    = np.nan
    RVerr[:] = np.nan

    print('Populating RV table')
    for x in range(0 , np.array(yy).size):
        if np.isnan(r['dr2_radial_velocity'][yy[x]]) == False:        # First copy over DR2 RVs
            RV[yy[x]]    = r['dr2_radial_velocity'][yy[x]]
            RVerr[yy[x]] = r['dr2_radial_velocity_error'][yy[x]]
            RVsrc[yy[x]] = 'Gaia DR2'
    if os.path.isfile('LocalRV.csv'):
        with open('LocalRV.csv') as csvfile:                          # Now check for a local RV that would supercede
            readCSV = csv.reader(csvfile, delimiter=',')
            for row in readCSV:
                ww = np.where(r['designation'] == row[0])[0]
                if (np.array(ww).size == 1):
                    RV[ww]    = row[2]
                    RVerr[ww] = row[3]
                    RVsrc[ww] = row[4]
                    if verbose == True: 
                        print('Using stored RV: ',row)
                        print(r['ra','dec','phot_g_mean_mag'][ww])
                        print(RV[ww])
                        print(RVerr[ww])
                        print(RVsrc[ww])



    # Create Gaia CMD plot

    mamajek  = np.loadtxt(datapath+'/sptGBpRp.txt')
    pleiades = np.loadtxt(datapath+'/PleGBpRp.txt')
    tuchor   = np.loadtxt(datapath+'/TucGBpRp.txt')
    usco     = np.loadtxt(datapath+'/UScGBpRp.txt')
    chai     = np.loadtxt(datapath+'/ChaGBpRp.txt')

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (np.isnan(r['bp_rp']) == False) ) # Note, this causes an error because NaNs
    yy = zz[0][np.argsort(sep3d[zz])]
    zz2= np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) & \
                 (r['phot_bp_rp_excess_factor'] < (1.3 + 0.06*r['bp_rp']**2)) & \
                 (np.isnan(r['bp_rp']) == False) )                                                              # Note, this causes an error because NaNs
    yy2= zz2[0][np.argsort((-Gchi2)[zz2])]


    figname=outdir + targname.replace(" ", "") + "cmd.png"
    if verbose == True: print(figname)

    fig,ax1 = plt.subplots(figsize=(12,8))

    ax1.axis([ math.floor(min(r['bp_rp'][zz])) , \
               math.ceil(max(r['bp_rp'][zz])), \
               math.ceil(max((r['phot_g_mean_mag'][zz] - (5.0*np.log10(gaiacoord.distance[zz].value)-5.0))))+1, \
               math.floor(min((r['phot_g_mean_mag'][zz] - (5.0*np.log10(gaiacoord.distance[zz].value)-5.0))))-1 ] )
    ax1.set_xlabel(r'$B_p-R_p$ (mag)' , fontsize=16)
    ax1.set_ylabel(r'$M_G$ (mag)' , fontsize=16)
    ax1.tick_params(axis='both',which='major',labelsize=12)

    ax2 = ax1.twiny()
    ax2.set_xlim(ax1.get_xlim())
    spttickvals = np.array([ -0.037 , 0.377 , 0.782 , 0.980 , 1.84 , 2.50 , 3.36 , 4.75 ])
    sptticklabs = np.array([ 'A0' , 'F0' , 'G0' , 'K0' , 'M0' , 'M3' , 'M5' , 'M7' ])
    xx = np.where( (spttickvals >= math.floor(min(r['bp_rp'][zz]))) & (spttickvals <= math.ceil(max(r['bp_rp'][zz]))) )[0]
    ax2.set_xticks(spttickvals[xx])
    ax2.set_xticklabels( sptticklabs[xx] )
    ax2.set_xlabel('SpT' , fontsize=16, labelpad=15)
    ax2.tick_params(axis='both',which='major',labelsize=12)

    ax1.plot(    chai[:,1] ,     chai[:,0]  , zorder=1 , label='Cha-I (0-5 Myr)')
    ax1.plot(    usco[:,1] ,     usco[:,0]  , zorder=2 , label='USco (11 Myr)')
    ax1.plot(  tuchor[:,1] ,   tuchor[:,0]  , zorder=3 , label='Tuc-Hor (40 Myr)')
    ax1.plot(pleiades[:,1] , pleiades[:,0]  , zorder=4 , label='Pleiades (125 Myr)')
    ax1.plot( mamajek[:,2] ,  mamajek[:,1]  , zorder=5 , label='Mamajek MS')

    for x in range(0 , np.array(yy2).size):
        msize  = (17-12.0*(sep3d[yy2[x]].value/searchradpc.value))**2
        mcolor = Gchi2[yy2[x]]
        medge  = 'black'
        mzorder= 7
        if (r['ruwe'][yy2[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy2[x]] >= 1.2):
            mshape='s'
        if (np.isnan(rvcut) == False): 
            if (np.isnan(RV[yy2[x]])==False) & (np.abs(RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) > rvcut):
                mshape='+'
                mcolor='black'
                mzorder=6
            if (np.isnan(RV[yy2[x]])==False) & (np.abs(RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) <= rvcut):
                medge='blue'

        ccc = ax1.scatter(r['bp_rp'][yy2[x]] , (r['phot_g_mean_mag'][yy2[x]] - (5.0*np.log10(gaiacoord.distance[yy2[x]].value)-5.0)) , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )

    temp1 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='o' , s=12**2 , label = 'RUWE < 1.2')
    temp2 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='s' , s=12**2 , label = 'RUWE >= 1.2')
    temp3 = ax1.scatter([] , [] , c='white' , edgecolors='blue' , marker='o' , s=12**2 , label = 'RV Comoving')
    temp4 = ax1.scatter([] , [] , c='black' , marker='+' , s=12**2 , label = 'RV Outlier')

    ax1.plot(r['bp_rp'][yy[0]] , (r['phot_g_mean_mag'][yy[0]] - (5.0*np.log10(gaiacoord.distance[yy[0]].value)-5.0)) , \
             'rx' , markersize=18 , mew=3 , markeredgecolor='red' , zorder=10 , label=targname)

    ax1.arrow( 1.3 , 2.5 , 0.374, 0.743 , length_includes_head=True , head_width=0.07 , head_length = 0.10 )
    ax1.text(  1.4 , 2.3, r'$A_V=1$' , fontsize=12)



    ax1.legend(fontsize=11)
    cb = plt.colorbar(ccc , ax=ax1)
    cb.set_label(label='Velocity Difference (km/s)',fontsize=14)
    plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)
    if showplots == True: plt.show()
    plt.close('all')


    # Create PM plot


    zz2= np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) )
    yy2= zz2[0][np.argsort((-Gchi2)[zz2])]
    zz3= np.where( (sep3d.value < searchradpc.value) & (sep.degree > 0.00001) )

    figname=outdir + targname.replace(" ", "") + "pmd.png"

    fig,ax1 = plt.subplots(figsize=(12,8))

    ax1.axis([ (max(r['pmra'][zz2]) + 0.05*np.ptp(r['pmra'][zz2]) ) , \
           (min(r['pmra'][zz2]) - 0.05*np.ptp(r['pmra'][zz2]) ) , \
           (min(r['pmdec'][zz2])- 0.05*np.ptp(r['pmra'][zz2]) ) , \
           (max(r['pmdec'][zz2])+ 0.05*np.ptp(r['pmra'][zz2]) ) ] )
    ax1.tick_params(axis='both',which='major',labelsize=16)

    if  ((max(r['pmra'][zz2]) + 0.05*np.ptp(r['pmra'][zz2])) > 0.0) & \
            ((min(r['pmra'][zz2]) - 0.05*np.ptp(r['pmra'][zz2])) < 0.0) & \
            ((min(r['pmdec'][zz2])- 0.05*np.ptp(r['pmra'][zz2])) < 0.0) & \
            ((max(r['pmdec'][zz2])+ 0.05*np.ptp(r['pmra'][zz2])) > 0.0):
        ax1.plot( [0.0,0.0] , [-1000.0,1000.0] , 'k--' , linewidth=1 )
        ax1.plot( [-1000.0,1000.0] , [0.0,0.0] , 'k--' , linewidth=1 )

    ax1.errorbar( (r['pmra'][yy2]) , (r['pmdec'][yy2]) , \
            yerr=(r['pmdec_error'][yy2]) , xerr=(r['pmra_error'][yy2]) , fmt='none' , ecolor='k' )

    ax1.scatter( (r['pmra'][zz3]) , (r['pmdec'][zz3]) , \
              s=(0.5)**2 , marker='o' , c='black' , zorder=2 , label='Field' )

    for x in range(0 , np.array(yy2).size):
        msize  = (17-12.0*(sep3d[yy2[x]].value/searchradpc.value))**2
        mcolor = Gchi2[yy2[x]]
        medge  = 'black'
        mzorder= 7
        if (r['ruwe'][yy2[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy2[x]] >= 1.2):
            mshape='s'
        if (np.isnan(rvcut) == False): 
            if (np.isnan(RV[yy2[x]])==False) & (np.abs(RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) > rvcut):
                mshape='+'
                mcolor='black'
                mzorder=6
            if (np.isnan(RV[yy2[x]])==False) & (np.abs(RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) <= rvcut):
                medge='blue'
        ccc = ax1.scatter(r['pmra'][yy2[x]] , r['pmdec'][yy2[x]] , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )

    temp1 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='o' , s=12**2 , label = 'RUWE < 1.2')
    temp2 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='s' , s=12**2 , label = 'RUWE >= 1.2')
    temp3 = ax1.scatter([] , [] , c='white' , edgecolors='blue' , marker='o' , s=12**2 , label = 'RV Comoving')
    temp4 = ax1.scatter([] , [] , c='black' , marker='+' , s=12**2 , label = 'RV Outlier')

    ax1.plot( Pgaia['pmra'][minpos] , Pgaia['pmdec'][minpos] , \
         'rx' , markersize=18 , mew=3 , markeredgecolor='red' , zorder=3 , label=targname)

    ax1.set_xlabel(r'$\mu_{RA}$ (mas/yr)' , fontsize=22 , labelpad=10)
    ax1.set_ylabel(r'$\mu_{DEC}$ (mas/yr)' , fontsize=22 , labelpad=10)
    ax1.legend(fontsize=12)

    cb = plt.colorbar(ccc , ax=ax1)
    cb.set_label(label='Tangential Velocity Difference (km/s)',fontsize=18 , labelpad=10)
    plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)
    if showplots == True: plt.show()
    plt.close('all')


    # Create RV plot

    zz2= np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) & \
             (np.isnan(RV) == False) )
    yy2= zz2[0][np.argsort((-Gchi2)[zz2])]

    zz3= np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) & \
             (np.isnan(RV) == False) & (np.isnan(r['phot_g_mean_mag']) == False) & \
             (np.abs(RV-Gvrpmllpmbb[:,0]) < 20.0) ) # Just to set Y axis

    fig,ax1 = plt.subplots(figsize=(12,8))
    ax1.axis([ -20.0 , +20.0, \
           max( np.append( np.array(r['phot_g_mean_mag'][zz3] - (5.0*np.log10(gaiacoord.distance[zz3].value)-5.0)) ,  0.0 )) + 0.3 , \
           min( np.append( np.array(r['phot_g_mean_mag'][zz3] - (5.0*np.log10(gaiacoord.distance[zz3].value)-5.0)) , 15.0 )) - 0.3   ])
    ax1.tick_params(axis='both',which='major',labelsize=16)

    ax1.plot( [0.0,0.0] , [-20.0,25.0] , 'k--' , linewidth=1 )

    ax1.errorbar( (RV[yy2]-Gvrpmllpmbb[yy2,0]) , \
           (r['phot_g_mean_mag'][yy2] - (5.0*np.log10(gaiacoord.distance[yy2].value)-5.0)) , \
            yerr=None,xerr=(RVerr[yy2]) , fmt='none' , ecolor='k' )

    for x in range(0 , np.array(yy2).size):
        msize  = (17-12.0*(sep3d[yy2[x]].value/searchradpc.value))**2
        mcolor = Gchi2[yy2[x]]
        medge  = 'black'
        mzorder= 2
        if (r['ruwe'][yy2[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy2[x]] >= 1.2):
            mshape='s'
        ccc = ax1.scatter( (RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) , \
                (r['phot_g_mean_mag'][yy2[x]] - (5.0*np.log10(gaiacoord.distance[yy2[x]].value)-5.0)) , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )

    temp1 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='o' , s=12**2 , label = 'RUWE < 1.2')
    temp2 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='s' , s=12**2 , label = 'RUWE >= 1.2')
    temp3 = ax1.scatter([] , [] , c='white' , edgecolors='blue' , marker='o' , s=12**2 , label = 'RV Comoving')

    if ( (Pgaia['phot_g_mean_mag'][minpos] - (5.0*np.log10(Pcoord.distance.value)-5.0)) < \
                                     (max( np.append( np.array(r['phot_g_mean_mag'][zz3] - (5.0*np.log10(gaiacoord.distance[zz3].value)-5.0)) , 0.0 )) + 0.3) ):
        ax1.plot( [0.0] , (Pgaia['phot_g_mean_mag'][minpos] - (5.0*np.log10(Pcoord.distance.value)-5.0)) , \
                  'rx' , markersize=18 , mew=3 , markeredgecolor='red' , zorder=3 , label=targname)


    ax1.set_ylabel(r'$M_G$ (mag)' , fontsize=22 , labelpad=10)
    ax1.set_xlabel(r'$v_{r,obs}-v_{r,pred}$ (km/s)' , fontsize=22 , labelpad=10)
    ax1.legend(fontsize=12)

    cb = plt.colorbar(ccc , ax=ax1)
    cb.set_label(label='Tangential Velocity Difference (km/s)',fontsize=18 , labelpad=10)

    figname=outdir + targname.replace(" ", "") + "drv.png"
    plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)
    if showplots == True: plt.show()
    plt.close('all')



    
    # Create XYZ plot

    Pxyz = bc.lbd_to_XYZ( Pllbb[0] , Pllbb[1] , Pcoord.distance.value/1000.0 , degree=True)

    fig,axs = plt.subplots(2,2)
    fig.set_figheight(16)
    fig.set_figwidth(16)
    fig.subplots_adjust(hspace=0.03,wspace=0.03)

    zz2= np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) )
    yy2= zz2[0][np.argsort((-Gchi2)[zz2])]

    for x in range(0 , np.array(yy2).size):
        msize  = (17-12.0*(sep3d[yy2[x]].value/searchradpc.value))**2
        mcolor = Gchi2[yy2[x]]
        medge  = 'black'
        mzorder= 3
        if (r['ruwe'][yy2[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy2[x]] >= 1.2):
            mshape='s'
        if (np.isnan(rvcut) == False): 
            if (np.isnan(RV[yy2[x]])==False) & (np.abs(RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) > rvcut):
                mshape='+'
                mcolor='black'
                mzorder=2
            if (np.isnan(RV[yy2[x]])==False) & (np.abs(RV[yy2[x]]-Gvrpmllpmbb[yy2[x],0]) <= rvcut):
                medge='blue'
        ccc = axs[0,0].scatter( 1000.0*Gxyz[yy2[x],0] , 1000.0*Gxyz[yy2[x],1] , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )
        ccc = axs[0,1].scatter( 1000.0*Gxyz[yy2[x],2] , 1000.0*Gxyz[yy2[x],1] , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )
        ccc = axs[1,0].scatter( 1000.0*Gxyz[yy2[x],0] , 1000.0*Gxyz[yy2[x],2] , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )

    temp1 = axs[0,0].scatter([] , [] , c='white' , edgecolors='black', marker='o' , s=12**2 , label = 'RUWE < 1.2')
    temp2 = axs[0,0].scatter([] , [] , c='white' , edgecolors='black', marker='s' , s=12**2 , label = 'RUWE >= 1.2')
    temp3 = axs[0,0].scatter([] , [] , c='white' , edgecolors='blue' , marker='o' , s=12**2 , label = 'RV Comoving')
    temp4 = axs[0,0].scatter([] , [] , c='black' , marker='+' , s=12**2 , label = 'RV Outlier')

    axs[0,0].plot( 1000.0*Pxyz[0] , 1000.0*Pxyz[1] , 'rx' , markersize=18 , mew=3 , markeredgecolor='red')
    axs[0,1].plot( 1000.0*Pxyz[2] , 1000.0*Pxyz[1] , 'rx' , markersize=18 , mew=3 , markeredgecolor='red')
    axs[1,0].plot( 1000.0*Pxyz[0] , 1000.0*Pxyz[2] , 'rx' , markersize=18 , mew=3 , markeredgecolor='red' , zorder=1 , label = targname)

    axs[0,0].set_xlim( [1000.0*Pxyz[0]-(search_radius+1.0) , 1000.0*Pxyz[0]+(search_radius+1.0)] )
    axs[0,0].set_ylim( [1000.0*Pxyz[1]-(search_radius+1.0) , 1000.0*Pxyz[1]+(search_radius+1.0)] )
    axs[0,1].set_xlim( [1000.0*Pxyz[2]-(search_radius+1.0) , 1000.0*Pxyz[2]+(search_radius+1.0)] )
    axs[0,1].set_ylim( [1000.0*Pxyz[1]-(search_radius+1.0) , 1000.0*Pxyz[1]+(search_radius+1.0)] )
    axs[1,0].set_xlim( [1000.0*Pxyz[0]-(search_radius+1.0) , 1000.0*Pxyz[0]+(search_radius+1.0)] )
    axs[1,0].set_ylim( [1000.0*Pxyz[2]-(search_radius+1.0) , 1000.0*Pxyz[2]+(search_radius+1.0)] )
    
    axs[0,0].set_xlabel(r'$X$ (pc)',fontsize=20,labelpad=10)
    axs[0,0].set_ylabel(r'$Y$ (pc)',fontsize=20,labelpad=10)

    axs[1,0].set_xlabel(r'$X$ (pc)',fontsize=20,labelpad=10)
    axs[1,0].set_ylabel(r'$Z$ (pc)',fontsize=20,labelpad=10)

    axs[0,1].set_xlabel(r'$Z$ (pc)',fontsize=20,labelpad=10)
    axs[0,1].set_ylabel(r'$Y$ (pc)',fontsize=20,labelpad=10)

    axs[0,0].xaxis.set_ticks_position('top')
    axs[0,1].xaxis.set_ticks_position('top')
    axs[0,1].yaxis.set_ticks_position('right')

    axs[0,0].xaxis.set_label_position('top')
    axs[0,1].xaxis.set_label_position('top')
    axs[0,1].yaxis.set_label_position('right')

    for aa in [0,1]:
        for bb in [0,1]:
            axs[aa,bb].tick_params(top=True,bottom=True,left=True,right=True,direction='in',labelsize=18)

    fig.delaxes(axs[1][1])
    strsize = 26
    if (len(targname) > 12.0): strsize = np.floor(24 / (len(targname)/14.5))
    fig.legend( bbox_to_anchor=(0.92,0.37) , prop={'size':strsize})

    cbaxes = fig.add_axes([0.55,0.14,0.02,0.34])
    cb = plt.colorbar( ccc , cax=cbaxes )
    cb.set_label( label='Velocity Difference (km/s)' , fontsize=24 , labelpad=20 )
    cb.ax.tick_params(labelsize=18)

    figname=outdir + targname.replace(" ", "") + "xyz.png"
    plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)

    if showplots == True: plt.show()
    plt.close('all')



    # Create sky map
    # Hacked from cartopy.mpl.gridliner
    _DEGREE_SYMBOL = u'\u00B0'
    def _east_west_formatted(longitude, num_format='g'):
        fmt_string = u'{longitude:{num_format}}{degree}'
        return fmt_string.format(longitude=(longitude if (longitude >= 0) else (longitude + 360)) , \
                                            num_format=num_format,degree=_DEGREE_SYMBOL)
    def _north_south_formatted(latitude, num_format='g'):
        fmt_string = u'{latitude:{num_format}}{degree}'
        return fmt_string.format(latitude=latitude, num_format=num_format,degree=_DEGREE_SYMBOL)
    LONGITUDE_FORMATTER = mticker.FuncFormatter(lambda v, pos:
                                                _east_west_formatted(v))
    LATITUDE_FORMATTER = mticker.FuncFormatter(lambda v, pos:
                                               _north_south_formatted(v))

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) )
    yy = zz[0][np.argsort((-Gchi2)[zz])]

    searchcircle = Pcoord.directional_offset_by( (np.arange(0,360)*u.degree) , searchraddeg*np.ones(360))
    circleRA = searchcircle.ra.value
    circleDE = searchcircle.dec.value
    ww = np.where(circleRA > 180.0)
    circleRA[ww] = circleRA[ww] - 360.0

    RAlist = gaiacoord.ra[yy].value
    DElist = gaiacoord.dec[yy].value
    ww = np.where( RAlist > 180.0 )
    RAlist[ww] = RAlist[ww] - 360.0

    polelat = ((Pcoord.dec.value+90) if (Pcoord.dec.value<0) else (90-Pcoord.dec.value))
    polelong= (Pcoord.ra.value if (Pcoord.dec.value<0.0) else (Pcoord.ra.value+180.0))
    polelong= (polelong if polelong < 180 else polelong - 360.0)

    if verbose == True:
        print('Alignment variables: ',polelat,polelong,Pcoord.ra.value)
        print(Pcoord.dec.value+searchraddeg.value)
    rotated_pole = ccrs.RotatedPole( \
        pole_latitude=polelat , \
        pole_longitude=polelong , \
        central_rotated_longitude=90.0 )#\
    #    (Pcoord.ra.value if (Pcoord.dec.value > 0.0) else (Pcoord.ra.value+180.0)) )

    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(1, 1, 1, projection=rotated_pole)

    ax.gridlines(draw_labels=True,x_inline=True,y_inline=True, \
                 xformatter=LONGITUDE_FORMATTER,yformatter=LATITUDE_FORMATTER)
    ax.plot( circleRA , circleDE , c="gray" , ls="--" , transform=ccrs.Geodetic())
    
    figname=outdir + targname.replace(" ", "") + "sky.png"

    base=plt.cm.get_cmap('cubehelix')

    for x in range(0 , np.array(yy).size):
        msize  = (17-12.0*(sep3d[yy[x]].value/searchradpc.value))
        mcolor = base(Gchi2[yy[x]]/vlim.value)
        medge  = 'black'
        mzorder= 3
        if (r['ruwe'][yy[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy[x]] >= 1.2):
            mshape='s'
        if (np.isnan(rvcut) == False): 
            if (np.isnan(RV[yy[x]])==False) & (np.abs(RV[yy[x]]-Gvrpmllpmbb[yy[x],0]) > rvcut):
                mshape='+'
                mcolor='black'
                mzorder=2
            if (np.isnan(RV[yy[x]])==False) & (np.abs(RV[yy[x]]-Gvrpmllpmbb[yy[x],0]) <= rvcut):
                medge='blue'
        ccc = ax.plot( RAlist[x] , DElist[x] , marker=mshape ,  \
                markeredgecolor=medge , ms = msize , mfc = mcolor , transform=ccrs.Geodetic() )
        
    ax.plot( (Pcoord.ra.value-360.0) , Pcoord.dec.value , \
            'rx' , markersize=18 , mew=3 , transform=ccrs.Geodetic())

    plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)
    
    if showplots == True: plt.show()
    plt.close('all')

    ## Query GALEX and 2MASS data

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) )
    yy = zz[0][np.argsort((-Gchi2)[zz])]
    
    NUVmag = np.empty(np.array(r['ra']).size)
    NUVerr = np.empty(np.array(r['ra']).size)
    NUVmag[:] = np.nan
    NUVerr[:] = np.nan

    print('Searching on neighbors in GALEX')
    ##suppress the stupid noresultswarning from the catalogs package
    warnings.filterwarnings("ignore",category=NoResultsWarning)

    for x in range(0 , np.array(yy).size):
        querystring=((str(gaiacoord.ra[yy[x]].value) if (gaiacoord.ra[yy[x]].value > 0) \
                      else str(gaiacoord.ra[yy[x]].value+360.0)) + " " + str(gaiacoord.dec[yy[x]].value))
        print('GALEX query ',x,' of ',np.array(yy).size, end='\r')
        if verbose == True: print('GALEX query ',x,' of ',np.array(yy).size)
        if verbose == True: print(querystring)
        if (DoGALEX == True): 
            galex = Catalogs.query_object(querystring , catalog="Galex" , radius=0.0028 , TIMEOUT=600)
            if ((np.where(galex['nuv_magerr'] > 0.0)[0]).size > 0):
                ww = np.where( (galex['nuv_magerr'] == min(galex['nuv_magerr'][np.where(galex['nuv_magerr'] > 0.0)])))
                NUVmag[yy[x]] = galex['nuv_mag'][ww][0]
                NUVerr[yy[x]] = galex['nuv_magerr'][ww][0]
                if verbose == True: print(galex['distance_arcmin','ra','nuv_mag','nuv_magerr'][ww])

        
    Jmag = np.empty(np.array(r['ra']).size)
    Jerr = np.empty(np.array(r['ra']).size)
    Jmag[:] = np.nan
    Jerr[:] = np.nan

    print('Searching on neighbors in 2MASS')

    for x in range(0 , np.array(yy).size):
        if ( np.isnan(NUVmag[yy[x]]) == False ):
            querycoord = SkyCoord((str(gaiacoord.ra[yy[x]].value) if (gaiacoord.ra[yy[x]].value > 0) else \
                     str(gaiacoord.ra[yy[x]].value+360.0)) , str(gaiacoord.dec[yy[x]].value) , \
                     unit=(u.deg,u.deg) , frame='icrs')
            print('2MASS query ',x,' of ',np.array(yy).size, end='\r')
            if verbose == True: print('2MASS query ',x,' of ',np.array(yy).size)
            if verbose == True: print(querycoord)
            tmass = []
            if (DoGALEX == True): 
                tmass = Irsa.query_region(querycoord , catalog='fp_psc' , radius='0d0m10s' )
                if ((np.where(tmass['j_m'] > -10.0)[0]).size > 0):
                    ww = np.where( (tmass['j_m'] == min(tmass['j_m'][np.where(tmass['j_m'] > 0.0)])))
                    Jmag[yy[x]] = tmass['j_m'][ww][0]
                    Jerr[yy[x]] = tmass['j_cmsig'][ww][0]
                    if verbose == True: print(tmass['j_m','j_cmsig'][ww])
        


    # Create GALEX plots
    mamajek = np.loadtxt(datapath+'/sptGBpRp.txt')
    f = interp1d( mamajek[:,2] , mamajek[:,0] , kind='cubic')

    zz2 = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) )
    yy2 = zz[0][np.argsort(sep3d[zz])]
    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) )
    yy = zz[0][np.argsort((-Gchi2)[zz])]

    fnuvj = (3631.0 * 10**6 * 10**(-0.4 * NUVmag)) / (1594.0 * 10**6 * 10**(-0.4 * Jmag))
    spt = f(r['bp_rp'].filled(np.nan))
    sptstring = ["nan" for x in range(np.array(r['bp_rp']).size)]
    for x in range(0 , np.array(zz2).size):
        if (round(spt[yy2[x]],1) >= 17.0) and (round(spt[yy2[x]],1) < 27.0):
            sptstring[yy2[x]] = 'M' + ('% 3.1f' % (round(spt[yy2[x]],1)-17.0)).strip()
        if (round(spt[yy2[x]],1) >= 16.0) and (round(spt[yy2[x]],1) < 17.0):
            sptstring[yy2[x]] = 'K' + ('% 3.1f' % (round(spt[yy2[x]],1)-9.0)).strip()
        if (round(spt[yy2[x]],1) >= 10.0) and (round(spt[yy2[x]],1) < 16.0):
            sptstring[yy2[x]] = 'K' + ('% 3.1f' % (round(spt[yy2[x]],1)-10.0)).strip()
        if (round(spt[yy2[x]],1) >= 0.0) and (round(spt[yy2[x]],1) < 10.0):
            sptstring[yy2[x]] = 'G' + ('% 3.1f' % (round(spt[yy2[x]],1)-0.0)).strip()
        if (round(spt[yy2[x]],1) >= -10.0) and (round(spt[yy2[x]],1) < 0.0):
            sptstring[yy2[x]] = 'F' + ('% 3.1f' % (round(spt[yy2[x]],1)+10.0)).strip()
        if (round(spt[yy2[x]],1) >= -20.0) and (round(spt[yy2[x]],1) < -10.0):
            sptstring[yy2[x]] = 'A' + ('% 3.1f' % (round(spt[yy2[x]],1)+20.0)).strip()       
        if (round(spt[yy2[x]],1) >= -30.0) and (round(spt[yy2[x]],1) < -20.0):
            sptstring[yy2[x]] = 'B' + ('% 3.1f' % (round(spt[yy2[x]],1)+30.0)).strip()  
    


    figname=outdir + targname.replace(" ", "") + "galex.png"
    if verbose == True: print(figname)
    ##Muck with the axis to get two x axes

    fig,ax1 = plt.subplots(figsize=(12,8))
    ax1.set_yscale('log')
    ax1.axis([5.0 , 24.0 , 0.000004 , 0.02])
    ax2 = ax1.twiny()
    ax2.set_xlim(ax1.get_xlim())
    ax1.set_xticks(np.array([5.0 , 10.0 , 15.0 , 17.0 , 22.0 , 24.0]))
    ax1.set_xticklabels(['G5','K0','K5','M0','M5','M7'])
    ax1.set_xlabel('SpT' , fontsize=20, labelpad=15)
    ax1.tick_params(axis='both',which='major',labelsize=16)
    ax2.set_xticks(np.array([5.0 , 10.0 , 15.0 , 17.0 , 22.0 , 24.0]))
    ax2.set_xticklabels(['0.85','0.98','1.45','1.84','3.36','4.75'])
    ax2.set_xlabel(r'$B_p-R_p$ (mag)' , fontsize=20, labelpad=15)
    ax2.tick_params(axis='both',which='major',labelsize=16)
    ax1.set_ylabel(r'$F_{NUV}/F_{J}$' , fontsize=22, labelpad=0)

    ##Hyades
    hyades = readsav(datapath +'/HYsaved.sav')
    hyadesfnuvj = (3631.0 * 10**6 * 10**(-0.4 * hyades['clnuv'])) / (1594.0 * 10**6 * 10**(-0.4 * hyades['clJ']))
    ax1.plot(hyades['clspt'] , hyadesfnuvj , 'x' , markersize=4 , mew=1 , markeredgecolor='black' , zorder=1 , label='Hyades' )

    for x in range(0 , np.array(yy).size):
        msize  = (17-12.0*(sep3d[yy[x]].value/searchradpc.value))**2
        mcolor = Gchi2[yy[x]]
        medge  = 'black'
        mzorder= 3
        if (r['ruwe'][yy[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy[x]] >= 1.2):
            mshape='s'
        if (np.isnan(rvcut) == False): 
            if (np.isnan(RV[yy[x]])==False) & (np.abs(RV[yy[x]]-Gvrpmllpmbb[yy[x],0]) > rvcut):
                mshape='+'
                mcolor='black'
                mzorder=2
            if (np.isnan(RV[yy[x]])==False) & (np.abs(RV[yy[x]]-Gvrpmllpmbb[yy[x],0]) <= rvcut):
                medge='blue'
        ccc = ax1.scatter( spt[yy[x]] , fnuvj[yy[x]] , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )

    temp1 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='o' , s=12**2 , label = 'RUWE < 1.2')
    temp2 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='s' , s=12**2 , label = 'RUWE >= 1.2')
    temp3 = ax1.scatter([] , [] , c='white' , edgecolors='blue' , marker='o' , s=12**2 , label = 'RV Comoving')
    temp4 = ax1.scatter([] , [] , c='black' , marker='+' , s=12**2 , label = 'RV Outlier')



    # Plot science target
    if (spt[yy[0]] > 5): ax1.plot(spt[yy[0]] , fnuvj[yy[0]] , 'rx' , markersize=18 , mew=3 , markeredgecolor='red' , zorder=3 , label=targname )

    ax1.legend(fontsize=16 , loc='lower left')
    cb = fig.colorbar(ccc , ax=ax1)
    cb.set_label(label='Velocity Offset (km/s)',fontsize=13)
    if (DoGALEX == True): plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)
    if showplots == True: plt.show()
    plt.close('all')
    
    
    # Query CatWISE for W1+W2 and AllWISE for W3+W4

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) )
    yy = zz[0][np.argsort((-Gchi2)[zz])]

    WISEmag = np.empty([np.array(r['ra']).size,4])
    WISEerr = np.empty([np.array(r['ra']).size,4])
    WISEmag[:] = np.nan
    WISEerr[:] = np.nan

    print('Searching on neighbors in WISE')
    ##there's an annoying nan warning here, hide it for now as it's not a problem
    warnings.filterwarnings("ignore",category=UserWarning)

    for x in range(0 , np.array(yy).size):
        querycoord = SkyCoord((str(gaiacoord.ra[yy[x]].value) if (gaiacoord.ra[yy[x]].value > 0) else \
                     str(gaiacoord.ra[yy[x]].value+360.0)) , str(gaiacoord.dec[yy[x]].value) , \
                     unit=(u.deg,u.deg) , frame='icrs')
        print('WISE query ',x,' of ',np.array(yy).size, end='\r')
        if verbose == True: print('WISE query ',x,' of ',np.array(yy).size)
        if verbose == True: print(querycoord)
    
        wisecat = []
        if (DoWISE == True): 
            wisecat = Irsa.query_region(querycoord,catalog='catwise_2020' , radius='0d0m10s')
            if ((np.where(wisecat['w1mpro'] > -10.0)[0]).size > 0):
                ww = np.where( (wisecat['w1mpro'] == min( wisecat['w1mpro'][np.where(wisecat['w1mpro'] > -10.0)]) ))
                WISEmag[yy[x],0] = wisecat['w1mpro'][ww][0]
                WISEerr[yy[x],0] = wisecat['w1sigmpro'][ww][0]
            if ((np.where(wisecat['w2mpro'] > -10.0)[0]).size > 0):
                ww = np.where( (wisecat['w2mpro'] == min( wisecat['w2mpro'][np.where(wisecat['w2mpro'] > -10.0)]) ))
                WISEmag[yy[x],1] = wisecat['w2mpro'][ww][0]
                WISEerr[yy[x],1] = wisecat['w2sigmpro'][ww][0]
 
        if (DoWISE == True): 
            wisecat = Irsa.query_region(querycoord,catalog='allwise_p3as_psd' , radius='0d0m10s')
            if ((np.where(wisecat['w1mpro'] > -10.0)[0]).size > 0):
                ww = np.where( (wisecat['w1mpro'] == min( wisecat['w1mpro'][np.where(wisecat['w1mpro'] > -10.0)]) ))
                if (np.isnan(WISEmag[yy[x],0]) == True) | (wisecat['w1mpro'][ww][0] < 11.0):				# Note, only if CatWISE absent/saturated
                    WISEmag[yy[x],0] = wisecat['w1mpro'][ww][0]
                    WISEerr[yy[x],0] = wisecat['w1sigmpro'][ww][0]
            if ((np.where(wisecat['w2mpro'] > -10.0)[0]).size > 0):
                ww = np.where( (wisecat['w2mpro'] == min( wisecat['w2mpro'][np.where(wisecat['w2mpro'] > -10.0)]) ))
                if (np.isnan(WISEmag[yy[x],1]) == True) | (wisecat['w2mpro'][ww][0] < 11.0):				# Note, only if CatWISE absent/saturated
                    WISEmag[yy[x],1] = wisecat['w2mpro'][ww][0]
                    WISEerr[yy[x],1] = wisecat['w2sigmpro'][ww][0]
            if ((np.where(wisecat['w3mpro'] > -10.0)[0]).size > 0):
                ww = np.where( (wisecat['w3mpro'] == min( wisecat['w3mpro'][np.where(wisecat['w3mpro'] > -10.0)]) ))
                WISEmag[yy[x],2] = wisecat['w3mpro'][ww][0]
                WISEerr[yy[x],2] = wisecat['w3sigmpro'][ww][0]
            if ((np.where(wisecat['w4mpro'] > -10.0)[0]).size > 0):
                ww = np.where( (wisecat['w4mpro'] == min( wisecat['w4mpro'][np.where(wisecat['w4mpro'] > -10.0)]) ))
                WISEmag[yy[x],3] = wisecat['w4mpro'][ww][0]
                WISEerr[yy[x],3] = wisecat['w4sigmpro'][ww][0]
        
        if verbose == True: print(yy[x],WISEmag[yy[x],:],WISEerr[yy[x],:])

    # Create WISE plots

    W13 = WISEmag[:,0]-WISEmag[:,2]
    W13err = ( WISEerr[:,0]**2 + WISEerr[:,2]**2 )**0.5

    zz = np.argwhere( np.isnan(W13err) )
    W13[zz] = np.nan
    W13err[zz] = np.nan

    zz = np.where( (W13err > 0.15) )
    W13[zz] = np.nan
    W13err[zz] = np.nan
    warnings.filterwarnings("default",category=UserWarning)




    zz2 = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value))
    yy2 = zz[0][np.argsort(sep3d[zz])]
    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) & (sep.degree > 0.00001) )
    yy = zz[0][np.argsort((-Gchi2)[zz])]

    figname=outdir + targname.replace(" ", "") + "wise.png"
    if verbose == True: print(figname)
    plt.figure(figsize=(12,8))

    if (verbose == True) & ((np.where(np.isfinite(W13+W13err))[0]).size > 0): print('Max y value: ' , (max((W13+W13err)[np.isfinite(W13+W13err)])+0.1) )
    plt.axis([ 5.0 , 24.0 , \
              max( [(min(np.append((W13-W13err)[ np.isfinite(W13-W13err) ],-0.1))-0.1) , -0.3]) , \
              max( [(max(np.append((W13+W13err)[ np.isfinite(W13+W13err) ],+0.0))+0.2) , +0.6]) ])

    ax1 = plt.gca()
    ax2 = ax1.twiny()
    ax2.set_xlim(5.0,24.0)

    ax1.set_xticks(np.array([5.0 , 10.0 , 15.0 , 17.0 , 22.0 , 24.0]))
    ax1.set_xticklabels(['G5','K0','K5','M0','M5','M7'])
    ax1.set_xlabel('SpT' , fontsize=20, labelpad=15)
    ax1.tick_params(axis='both',which='major',labelsize=16)

    ax2.set_xticks(np.array([5.0 , 10.0 , 15.0 , 17.0 , 22.0 , 24.0]))
    ax2.set_xticklabels(['0.85','0.98','1.45','1.84','3.36','4.75'])
    ax2.set_xlabel(r'$B_p-R_p$ (mag)' , fontsize=20, labelpad=15)
    ax2.tick_params(axis='both',which='major',labelsize=16)

    ax1.set_ylabel(r'$W1-W3$ (mag)' , fontsize=22, labelpad=0)

    # Plot field sequence from Tuc-Hor (Kraus et al. 2014)
    fldspt = [ 5 , 7 , 10 , 12 , 15 , 17 , 20 , 22 , 24 ]
    fldW13 = [ 0 , 0 ,  0 , .02, .06, .12, .27, .40, .60]
    plt.plot(fldspt , fldW13  , zorder=0 , label='Photosphere')

    # Plot neighbors
    ax1.errorbar( spt[yy] , W13[yy] , yerr=W13err[yy] , fmt='none' , ecolor='k')


    for x in range(0 , np.array(yy).size):
        msize  = (17-12.0*(sep3d[yy[x]].value/searchradpc.value))**2
        mcolor = Gchi2[yy[x]]
        medge  = 'black'
        mzorder= 3
        if (r['ruwe'][yy[x]] < 1.2):
            mshape='o'
        if (r['ruwe'][yy[x]] >= 1.2):
            mshape='s'
        if (np.isnan(rvcut) == False): 
            if (np.isnan(RV[yy[x]])==False) & (np.abs(RV[yy[x]]-Gvrpmllpmbb[yy[x],0]) > rvcut):
                mshape='+'
                mcolor='black'
                mzorder=2
            if (np.isnan(RV[yy[x]])==False) & (np.abs(RV[yy[x]]-Gvrpmllpmbb[yy[x],0]) <= rvcut):
                medge='blue'
        ccc = ax1.scatter( spt[yy[x]] , W13[yy[x]] , \
                s=msize , c=mcolor , marker=mshape , edgecolors=medge , zorder=mzorder , \
                vmin=0.0 , vmax=vlim.value , cmap='cubehelix' , label='_nolabel' )

    temp1 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='o' , s=12**2 , label = 'RUWE < 1.2')
    temp2 = ax1.scatter([] , [] , c='white' , edgecolors='black', marker='s' , s=12**2 , label = 'RUWE >= 1.2')
    temp3 = ax1.scatter([] , [] , c='white' , edgecolors='blue' , marker='o' , s=12**2 , label = 'RV Comoving')
    temp4 = ax1.scatter([] , [] , c='black' , marker='+' , s=12**2 , label = 'RV Outlier')


    # Plot science target
    if (spt[yy2[0]] > 5):
        plt.plot(spt[yy2[0]] , W13[yy2[0]] , 'rx' , markersize=18 , mew=3 , markeredgecolor='red' , zorder=3 , label=targname )

    plt.legend(fontsize=16 , loc='upper left')
    cb = plt.colorbar(ccc , ax=ax1)
    cb.set_label(label='Velocity Offset (km/s)',fontsize=14)
    if (DoWISE == True): plt.savefig(figname , bbox_inches='tight', pad_inches=0.2 , dpi=200)
    if showplots == True: plt.show()
    plt.close('all')

    # Cross-reference with ROSAT

    v = Vizier(columns=["**", "+_R"] , catalog='J/A+A/588/A103/cat2rxs' )

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) )
    yy = zz[0][np.argsort(sep3d[zz])]

    ROSATflux = np.empty([np.array(r['ra']).size])
    ROSATflux[:] = np.nan

    print('Searching on neighbors in ROSAT')
    for x in range(0 , np.array(yy).size):
        querycoord = SkyCoord((str(gaiacoord.ra[yy[x]].value) if (gaiacoord.ra[yy[x]].value > 0) else \
                     str(gaiacoord.ra[yy[x]].value+360.0)) , str(gaiacoord.dec[yy[x]].value) , \
                     unit=(u.deg,u.deg) , frame='icrs')
        print('ROSAT query ',x,' of ',np.array(yy).size, end='\r')
        if verbose == True: print('ROSAT query ',x,' of ',np.array(yy).size)
        if verbose == True: print(querycoord)
        if (DoROSAT == True): 
            rosatcat = v.query_region(querycoord , radius='0d1m0s' )
            if (len(rosatcat) > 0):
                rosatcat = rosatcat['J/A+A/588/A103/cat2rxs']
                if verbose == True: print(rosatcat)
                if ((np.where(rosatcat['CRate'] > -999)[0]).size > 0):
                    ww = np.where( (rosatcat['CRate'] == max(rosatcat['CRate'][np.where(rosatcat['CRate'] > -999)])))
                    ROSATflux[yy[x]] = rosatcat['CRate'][ww][0]
                if verbose == True: print(x,yy[x],ROSATflux[yy[x]])


    # Create output table with results
    print('Creating Output Tables with Results')
    if verbose == True: 
        print('Reminder, there were this many input entries: ',len(Gxyz[:,0]))
        print('The search radius in velocity space is: ',vlim)
        print()

    zz = np.where( (sep3d.value < searchradpc.value) & (Gchi2 < vlim.value) )
    sortlist = np.argsort(sep3d[zz])
    yy = zz[0][sortlist]

    fmt1 = "%11.7f %11.7f %6.3f %6.3f %11.3f %8.4f %8.4f %8.2f %8.2f %8.2f %8.3f %4s %8.6f %6.2f %7.3f %7.3f %35s"
    fmt2 = "%11.7f %11.7f %6.3f %6.3f %11.3f %8.4f %8.4f %8.2f %8.2f %8.2f %8.3f %4s %8.6f %6.2f %7.3f %7.3f %35s"
    filename=outdir + targname.replace(" ", "") + ".txt"
    
    warnings.filterwarnings("ignore",category=UserWarning)
    if verbose == True: 
        print('Also creating SIMBAD query table')
        print(filename)
        print('RA            DEC        Gmag   Bp-Rp  Voff(km/s) Sep(deg)   3D(pc) Vr(pred)  Vr(obs)    Vrerr Plx(mas)  SpT    FnuvJ  W1-W3    RUWE  XCrate RVsrc')
    with open(filename,'w') as file1:
        file1.write('RA            DEC        Gmag   Bp-Rp  Voff(km/s) Sep(deg)   3D(pc) Vr(pred)  Vr(obs)    Vrerr Plx(mas)  SpT    FnuvJ  W1-W3    RUWE  XCrate RVsrc \n')
    for x in range(0 , np.array(zz).size):
            if verbose == True:
                print(fmt1 % (gaiacoord.ra[yy[x]].value,gaiacoord.dec[yy[x]].value, \
                  r['phot_g_mean_mag'][yy[x]], r['bp_rp'][yy[x]] , \
                  Gchi2[yy[x]] , sep[yy[x]].value , sep3d[yy[x]].value , \
                  Gvrpmllpmbb[yy[x],0] , RV[yy[x]] , RVerr[yy[x]] , \
                  r['parallax'][yy[x]], \
                  sptstring[yy[x]] , fnuvj[yy[x]] , W13[yy[x]] , r['ruwe'][yy[x]] , ROSATflux[yy[x]] , RVsrc[yy[x]]) )
            with open(filename,'a') as file1:
                  file1.write(fmt2 % (gaiacoord.ra[yy[x]].value,gaiacoord.dec[yy[x]].value, \
                      r['phot_g_mean_mag'][yy[x]], r['bp_rp'][yy[x]] , \
                      Gchi2[yy[x]],sep[yy[x]].value,sep3d[yy[x]].value , \
                      Gvrpmllpmbb[yy[x],0] , RV[yy[x]] , RVerr[yy[x]] , \
                      r['parallax'][yy[x]], \
                      sptstring[yy[x]] , fnuvj[yy[x]] , W13[yy[x]] , r['ruwe'][yy[x]] , ROSATflux[yy[x]] , RVsrc[yy[x]]) )
                  file1.write("\n")

    filename=outdir + targname.replace(" ", "") + ".csv"
    with open(filename,mode='w') as result_file:
        wr = csv.writer(result_file)
        wr.writerow(['RA','DEC','Gmag','Bp-Rp','Voff(km/s)','Sep(deg)','3D(pc)','Vr(pred)','Vr(obs)','Vrerr','Plx(mas)','SpT','FnuvJ','W1-W3','RUWE','XCrate','RVsrc'])
        for x in range(0 , np.array(zz).size):
            wr.writerow(( "{0:.7f}".format(gaiacoord.ra[yy[x]].value) , "{0:.7f}".format(gaiacoord.dec[yy[x]].value) , \
                      "{0:.3f}".format(r['phot_g_mean_mag'][yy[x]]), "{0:.3f}".format(r['bp_rp'][yy[x]]) , \
                      "{0:.3f}".format(Gchi2[yy[x]]) , "{0:.4f}".format(sep[yy[x]].value) , "{0:.4f}".format(sep3d[yy[x]].value) , \
                      "{0:.2f}".format(Gvrpmllpmbb[yy[x],0]) , "{0:.2f}".format(RV[yy[x]]) , "{0:.2f}".format(RVerr[yy[x]]) , \
                      "{0:.3f}".format(r['parallax'][yy[x]]), \
                      sptstring[yy[x]] , "{0:.6f}".format(fnuvj[yy[x]]) , "{0:.2f}".format(W13[yy[x]]) , \
                      "{0:.3f}".format(r['ruwe'][yy[x]]) , "{0:.3f}".format(ROSATflux[yy[x]]) , RVsrc[yy[x]].strip()) )

    if verbose == True: print('All output can be found in ' + outdir)



    return outdir
示例#25
0
def make_rcsample(parser):
    options, args = parser.parse_args()
    savefilename = options.savefilename
    if savefilename is None:
        #Create savefilename if not given
        savefilename = os.path.join(
            appath._APOGEE_DATA, 'rcsample_' + appath._APOGEE_REDUX + '.fits')
        print("Saving to %s ..." % savefilename)
    #Read the base-sample
    data = apread.allStar(adddist=_ADDHAYDENDIST, rmdups=options.rmdups)
    #Remove a bunch of fields that we do not want to keep
    data = esutil.numpy_util.remove_fields(data, [
        'TARGET_ID', 'FILE', 'AK_WISE', 'SFD_EBV', 'SYNTHVHELIO_AVG',
        'SYNTHVSCATTER', 'SYNTHVERR', 'SYNTHVERR_MED', 'RV_TEFF', 'RV_LOGG',
        'RV_FEH', 'RV_ALPHA', 'RV_CARB', 'RV_CCFWHM', 'RV_AUTOFWHM',
        'SYNTHSCATTER', 'STABLERV_CHI2', 'STABLERV_RCHI2',
        'STABLERV_CHI2_PROB', 'CHI2_THRESHOLD', 'APSTAR_VERSION',
        'ASPCAP_VERSION', 'RESULTS_VERSION', 'WASH_M', 'WASH_M_ERR', 'WASH_T2',
        'WASH_T2_ERR', 'DDO51', 'DDO51_ERR', 'IRAC_3_6', 'IRAC_3_6_ERR',
        'IRAC_4_5', 'IRAC_4_5_ERR', 'IRAC_5_8', 'IRAC_5_8_ERR', 'IRAC_8_0',
        'IRAC_8_0_ERR', 'WISE_4_5', 'WISE_4_5_ERR', 'TARG_4_5', 'TARG_4_5_ERR',
        'WASH_DDO51_GIANT_FLAG', 'WASH_DDO51_STAR_FLAG', 'REDUCTION_ID',
        'SRC_H', 'PM_SRC'
    ])
    if not appath._APOGEE_REDUX.lower() == 'current' \
            and not 'l30' in appath._APOGEE_REDUX \
            and int(appath._APOGEE_REDUX[1:]) < 500:
        data = esutil.numpy_util.remove_fields(data, ['ELEM'])
    #Select red-clump stars
    jk = data['J0'] - data['K0']
    z = isodist.FEH2Z(data['METALS'], zsolar=0.017)
    if 'l30' in appath._APOGEE_REDUX:
        logg = data['LOGG']
    elif appath._APOGEE_REDUX.lower() == 'current' \
            or int(appath._APOGEE_REDUX[1:]) > 600:
        from apogee.tools import paramIndx
        if False:
            #Use my custom logg calibration that's correct for the RC
            logg = (1. - 0.042) * data['FPARAM'][:, paramIndx('logg')] - 0.213
            lowloggindx = data['FPARAM'][:, paramIndx('logg')] < 1.
            logg[lowloggindx] = data['FPARAM'][lowloggindx,
                                               paramIndx('logg')] - 0.255
            hiloggindx = data['FPARAM'][:, paramIndx('logg')] > 3.8
            logg[hiloggindx] = data['FPARAM'][hiloggindx,
                                              paramIndx('logg')] - 0.3726
        else:
            #Use my custom logg calibration that's correct on average
            logg = (1. + 0.03) * data['FPARAM'][:, paramIndx('logg')] - 0.37
            lowloggindx = data['FPARAM'][:, paramIndx('logg')] < 1.
            logg[lowloggindx] = data['FPARAM'][lowloggindx,
                                               paramIndx('logg')] - 0.34
            hiloggindx = data['FPARAM'][:, paramIndx('logg')] > 3.8
            logg[hiloggindx] = data['FPARAM'][hiloggindx,
                                              paramIndx('logg')] - 0.256
    else:
        logg = data['LOGG']
    indx= (jk < 0.8)*(jk >= 0.5)\
        *(z <= 0.06)\
        *(z <= rcmodel.jkzcut(jk,upper=True))\
        *(z >= rcmodel.jkzcut(jk))\
        *(logg >= rcmodel.loggteffcut(data['TEFF'],z,upper=False))\
        *(logg <= rcmodel.loggteffcut(data['TEFF'],z,upper=True))
    data = data[indx]
    #Add more aggressive flag cut
    data = esutil.numpy_util.add_fields(data, [('ADDL_LOGG_CUT', numpy.int32)])
    data['ADDL_LOGG_CUT'] = (
        (data['TEFF'] - 4800.) / 1000. + 2.75) > data['LOGG']
    if options.loggcut:
        data = data[data['ADDL_LOGG_CUT'] == 1]
    print("Making catalog of %i objects ..." % len(data))
    #Add distances
    data = esutil.numpy_util.add_fields(data, [('RC_DIST', float),
                                               ('RC_DM', float),
                                               ('RC_GALR', float),
                                               ('RC_GALPHI', float),
                                               ('RC_GALZ', float)])
    rcd = rcmodel.rcdist()
    jk = data['J0'] - data['K0']
    z = isodist.FEH2Z(data['METALS'], zsolar=0.017)
    data['RC_DIST'] = rcd(jk, z, appmag=data['K0']) * options.distfac
    data['RC_DM'] = 5. * numpy.log10(data['RC_DIST']) + 10.
    XYZ = bovy_coords.lbd_to_XYZ(data['GLON'],
                                 data['GLAT'],
                                 data['RC_DIST'],
                                 degree=True)
    R, phi, Z = bovy_coords.XYZ_to_galcencyl(XYZ[:, 0],
                                             XYZ[:, 1],
                                             XYZ[:, 2],
                                             Xsun=8.,
                                             Zsun=0.025)
    data['RC_GALR'] = R
    data['RC_GALPHI'] = phi
    data['RC_GALZ'] = Z
    #Save
    fitsio.write(savefilename, data, clobber=True)
    # Add Tycho-2 matches
    if options.tyc2:
        data = esutil.numpy_util.add_fields(data, [('TYC2MATCH', numpy.int32),
                                                   ('TYC1', numpy.int32),
                                                   ('TYC2', numpy.int32),
                                                   ('TYC3', numpy.int32)])
        data['TYC2MATCH'] = 0
        data['TYC1'] = -1
        data['TYC2'] = -1
        data['TYC3'] = -1
        # Write positions
        posfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        resultfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        with open(posfilename, 'w') as csvfile:
            wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA', 'DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'], data[ii]['DEC']])
        # Send to CDS for matching
        result = open(resultfilename, 'w')
        try:
            subprocess.check_call([
                'curl', '-X', 'POST', '-F', 'request=xmatch', '-F',
                'distMaxArcsec=2', '-F', 'RESPONSEFORMAT=csv', '-F',
                'cat1=@%s' % os.path.basename(posfilename), '-F', 'colRA1=RA',
                '-F', 'colDec1=DEC', '-F', 'cat2=vizier:Tycho2',
                'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'
            ],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Directly match on input RA
        ma = numpy.loadtxt(resultfilename,
                           delimiter=',',
                           skiprows=1,
                           usecols=(1, 2, 7, 8, 9))
        iis = numpy.arange(len(data))
        mai = [iis[data['RA'] == ma[ii, 0]][0] for ii in range(len(ma))]
        data['TYC2MATCH'][mai] = 1
        data['TYC1'][mai] = ma[:, 2]
        data['TYC2'][mai] = ma[:, 3]
        data['TYC3'][mai] = ma[:, 4]
        os.remove(posfilename)
        os.remove(resultfilename)
    if not options.nostat:
        #Determine statistical sample and add flag
        apo = apogee.select.apogeeSelect()
        statIndx = apo.determine_statistical(data)
        mainIndx = apread.mainIndx(data)
        data = esutil.numpy_util.add_fields(data, [('STAT', numpy.int32),
                                                   ('INVSF', float)])
        data['STAT'] = 0
        data['STAT'][statIndx * mainIndx] = 1
        for ii in range(len(data)):
            if (statIndx * mainIndx)[ii]:
                data['INVSF'][ii] = 1. / apo(data['LOCATION_ID'][ii],
                                             data['H'][ii])
            else:
                data['INVSF'][ii] = -1.
    if options.nopm:
        fitsio.write(savefilename, data, clobber=True)
        return None
    #Get proper motions, in a somewhat roundabout way
    pmfile = savefilename.split('.')[0] + '_pms.fits'
    if os.path.exists(pmfile):
        pmdata = fitsio.read(pmfile, 1)
    else:
        pmdata = numpy.recarray(
            len(data),
            formats=['f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'i4'],
            names=[
                'RA', 'DEC', 'PMRA', 'PMDEC', 'PMRA_ERR', 'PMDEC_ERR',
                'PMMATCH'
            ])
        # Write positions, again ...
        posfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        resultfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        with open(posfilename, 'w') as csvfile:
            wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA', 'DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'], data[ii]['DEC']])
        # Send to CDS for matching
        result = open(resultfilename, 'w')
        try:
            subprocess.check_call([
                'curl', '-X', 'POST', '-F', 'request=xmatch', '-F',
                'distMaxArcsec=4', '-F', 'RESPONSEFORMAT=csv', '-F',
                'cat1=@%s' % os.path.basename(posfilename), '-F', 'colRA1=RA',
                '-F', 'colDec1=DEC', '-F', 'cat2=vizier:UCAC4',
                'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'
            ],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Match back and only keep the closest one
        ma = numpy.loadtxt(resultfilename,
                           delimiter=',',
                           skiprows=1,
                           converters={
                               15: lambda s: float(s.strip() or -9999),
                               16: lambda s: float(s.strip() or -9999),
                               17: lambda s: float(s.strip() or -9999),
                               18: lambda s: float(s.strip() or -9999)
                           },
                           usecols=(4, 5, 15, 16, 17, 18))
        h = esutil.htm.HTM()
        m1, m2, d12 = h.match(data['RA'],
                              data['DEC'],
                              ma[:, 0],
                              ma[:, 1],
                              4. / 3600.,
                              maxmatch=1)
        pmdata['PMMATCH'] = 0
        pmdata['RA'] = data['RA']
        pmdata['DEC'] = data['DEC']
        pmdata['PMMATCH'][m1] = 1
        pmdata['PMRA'][m1] = ma[m2, 2]
        pmdata['PMDEC'][m1] = ma[m2, 3]
        pmdata['PMRA_ERR'][m1] = ma[m2, 4]
        pmdata['PMDEC_ERR'][m1] = ma[m2, 5]
        pmdata['PMMATCH'][(pmdata['PMRA'] == -9999) \
                          +(pmdata['PMDEC'] == -9999) \
                          +(pmdata['PMRA_ERR'] == -9999) \
                          +(pmdata['PMDEC_ERR'] == -9999)]= 0
        fitsio.write(pmfile, pmdata, clobber=True)
        #To make sure we're using the same format below
        pmdata = fitsio.read(pmfile, 1)
        os.remove(posfilename)
        os.remove(resultfilename)
    #Match proper motions
    try:  #These already exist currently, but may not always exist
        data = esutil.numpy_util.remove_fields(data, ['PMRA', 'PMDEC'])
    except ValueError:
        pass
    data = esutil.numpy_util.add_fields(data, [('PMRA', numpy.float),
                                               ('PMDEC', numpy.float),
                                               ('PMRA_ERR', numpy.float),
                                               ('PMDEC_ERR', numpy.float),
                                               ('PMMATCH', numpy.int32)])
    data['PMMATCH'] = 0
    h = esutil.htm.HTM()
    m1, m2, d12 = h.match(pmdata['RA'],
                          pmdata['DEC'],
                          data['RA'],
                          data['DEC'],
                          2. / 3600.,
                          maxmatch=1)
    data['PMRA'][m2] = pmdata['PMRA'][m1]
    data['PMDEC'][m2] = pmdata['PMDEC'][m1]
    data['PMRA_ERR'][m2] = pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR'][m2] = pmdata['PMDEC_ERR'][m1]
    data['PMMATCH'][m2] = pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx = data['PMMATCH'] == 1
    data['PMRA'][True - pmindx] = -9999.99
    data['PMDEC'][True - pmindx] = -9999.99
    data['PMRA_ERR'][True - pmindx] = -9999.99
    data['PMDEC_ERR'][True - pmindx] = -9999.99
    #Calculate Galactocentric velocities
    data = esutil.numpy_util.add_fields(data, [('GALVR', numpy.float),
                                               ('GALVT', numpy.float),
                                               ('GALVZ', numpy.float)])
    lb = bovy_coords.radec_to_lb(data['RA'], data['DEC'], degree=True)
    XYZ = bovy_coords.lbd_to_XYZ(lb[:, 0],
                                 lb[:, 1],
                                 data['RC_DIST'],
                                 degree=True)
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA'],
                                                 data['PMDEC'],
                                                 data['RA'],
                                                 data['DEC'],
                                                 degree=True)
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              lb[:, 0],
                                              lb[:, 1],
                                              data['RC_DIST'],
                                              degree=True)
    vR, vT, vZ = bovy_coords.vxvyvz_to_galcencyl(
        vxvyvz[:, 0],
        vxvyvz[:, 1],
        vxvyvz[:, 2],
        8. - XYZ[:, 0],
        XYZ[:, 1],
        XYZ[:, 2] + 0.025,
        vsun=[-11.1, 30.24 * 8.,
              7.25])  #Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR'] = vR
    data['GALVT'] = vT
    data['GALVZ'] = vZ
    data['GALVR'][True - pmindx] = -9999.99
    data['GALVT'][True - pmindx] = -9999.99
    data['GALVZ'][True - pmindx] = -9999.99
    #Get PPMXL proper motions, in a somewhat roundabout way
    pmfile = savefilename.split('.')[0] + '_pms_ppmxl.fits'
    if os.path.exists(pmfile):
        pmdata = fitsio.read(pmfile, 1)
    else:
        pmdata = numpy.recarray(
            len(data),
            formats=['f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'i4'],
            names=[
                'RA', 'DEC', 'PMRA', 'PMDEC', 'PMRA_ERR', 'PMDEC_ERR',
                'PMMATCH'
            ])
        # Write positions, again ...
        posfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        resultfilename = tempfile.mktemp('.csv', dir=os.getcwd())
        with open(posfilename, 'w') as csvfile:
            wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            wr.writerow(['RA', 'DEC'])
            for ii in range(len(data)):
                wr.writerow([data[ii]['RA'], data[ii]['DEC']])
        # Send to CDS for matching
        result = open(resultfilename, 'w')
        try:
            subprocess.check_call([
                'curl', '-X', 'POST', '-F', 'request=xmatch', '-F',
                'distMaxArcsec=4', '-F', 'RESPONSEFORMAT=csv', '-F',
                'cat1=@%s' % os.path.basename(posfilename), '-F', 'colRA1=RA',
                '-F', 'colDec1=DEC', '-F', 'cat2=vizier:PPMXL',
                'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync'
            ],
                                  stdout=result)
        except subprocess.CalledProcessError:
            os.remove(posfilename)
            if os.path.exists(resultfilename):
                result.close()
                os.remove(resultfilename)
        result.close()
        # Match back and only keep the closest one
        ma = numpy.loadtxt(resultfilename,
                           delimiter=',',
                           skiprows=1,
                           converters={
                               15: lambda s: float(s.strip() or -9999),
                               16: lambda s: float(s.strip() or -9999),
                               17: lambda s: float(s.strip() or -9999),
                               18: lambda s: float(s.strip() or -9999)
                           },
                           usecols=(4, 5, 15, 16, 19, 20))
        h = esutil.htm.HTM()
        m1, m2, d12 = h.match(data['RA'],
                              data['DEC'],
                              ma[:, 0],
                              ma[:, 1],
                              4. / 3600.,
                              maxmatch=1)
        pmdata['PMMATCH'] = 0
        pmdata['RA'] = data['RA']
        pmdata['DEC'] = data['DEC']
        pmdata['PMMATCH'][m1] = 1
        pmdata['PMRA'][m1] = ma[m2, 2]
        pmdata['PMDEC'][m1] = ma[m2, 3]
        pmdata['PMRA_ERR'][m1] = ma[m2, 4]
        pmdata['PMDEC_ERR'][m1] = ma[m2, 5]
        pmdata['PMMATCH'][(pmdata['PMRA'] == -9999) \
                          +(pmdata['PMDEC'] == -9999) \
                          +(pmdata['PMRA_ERR'] == -9999) \
                          +(pmdata['PMDEC_ERR'] == -9999)]= 0
        fitsio.write(pmfile, pmdata, clobber=True)
        #To make sure we're using the same format below
        pmdata = fitsio.read(pmfile, 1)
        os.remove(posfilename)
        os.remove(resultfilename)
    #Match proper motions to ppmxl
    data = esutil.numpy_util.add_fields(data,
                                        [('PMRA_PPMXL', numpy.float),
                                         ('PMDEC_PPMXL', numpy.float),
                                         ('PMRA_ERR_PPMXL', numpy.float),
                                         ('PMDEC_ERR_PPMXL', numpy.float),
                                         ('PMMATCH_PPMXL', numpy.int32)])
    data['PMMATCH_PPMXL'] = 0
    h = esutil.htm.HTM()
    m1, m2, d12 = h.match(pmdata['RA'],
                          pmdata['DEC'],
                          data['RA'],
                          data['DEC'],
                          2. / 3600.,
                          maxmatch=1)
    data['PMRA_PPMXL'][m2] = pmdata['PMRA'][m1]
    data['PMDEC_PPMXL'][m2] = pmdata['PMDEC'][m1]
    data['PMRA_ERR_PPMXL'][m2] = pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR_PPMXL'][m2] = pmdata['PMDEC_ERR'][m1]
    data['PMMATCH_PPMXL'][m2] = pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx = data['PMMATCH_PPMXL'] == 1
    data['PMRA_PPMXL'][True - pmindx] = -9999.99
    data['PMDEC_PPMXL'][True - pmindx] = -9999.99
    data['PMRA_ERR_PPMXL'][True - pmindx] = -9999.99
    data['PMDEC_ERR_PPMXL'][True - pmindx] = -9999.99
    #Calculate Galactocentric velocities
    data = esutil.numpy_util.add_fields(data, [('GALVR_PPMXL', numpy.float),
                                               ('GALVT_PPMXL', numpy.float),
                                               ('GALVZ_PPMXL', numpy.float)])
    lb = bovy_coords.radec_to_lb(data['RA'], data['DEC'], degree=True)
    XYZ = bovy_coords.lbd_to_XYZ(lb[:, 0],
                                 lb[:, 1],
                                 data['RC_DIST'],
                                 degree=True)
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA_PPMXL'],
                                                 data['PMDEC_PPMXL'],
                                                 data['RA'],
                                                 data['DEC'],
                                                 degree=True)
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              lb[:, 0],
                                              lb[:, 1],
                                              data['RC_DIST'],
                                              degree=True)
    vR, vT, vZ = bovy_coords.vxvyvz_to_galcencyl(
        vxvyvz[:, 0],
        vxvyvz[:, 1],
        vxvyvz[:, 2],
        8. - XYZ[:, 0],
        XYZ[:, 1],
        XYZ[:, 2] + 0.025,
        vsun=[-11.1, 30.24 * 8.,
              7.25])  #Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR_PPMXL'] = vR
    data['GALVT_PPMXL'] = vT
    data['GALVZ_PPMXL'] = vZ
    data['GALVR_PPMXL'][True - pmindx] = -9999.99
    data['GALVT_PPMXL'][True - pmindx] = -9999.99
    data['GALVZ_PPMXL'][True - pmindx] = -9999.99
    #Save
    fitsio.write(savefilename, data, clobber=True)
    return None
示例#26
0
    gmag_obs = np.append(gmag_obs, star['phot_g_mean_mag'][sindx])
    gbpmag_obs = np.append(gbpmag_obs, star['phot_bp_mean_mag'][sindx])
    grpmag_obs = np.append(grpmag_obs, star['phot_rp_mean_mag'][sindx])

print ' Total number of selected stars = ', len(gmag_obs)

# convert deg -> rad
glonrads = glons * np.pi / 180.0
glatrads = glats * np.pi / 180.0

# get observed position and velocity
dists_obs = 1.0 / plxs_obs

# velocity
Tpmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb( \
    pmras_obs, pmdecs_obs, ras, \
    decs, degree=True, epoch=None)
pmlons_obs = Tpmllpmbb[:, 0]
pmlats_obs = Tpmllpmbb[:, 1]
# mas/yr -> km/s
vlons_obs = pmvconst * pmlons_obs * dists_obs
vlats_obs = pmvconst * pmlats_obs * dists_obs
# galactic position
distxys_obs = dists_obs * np.cos(glatrads)
xpos_obs = distxys_obs * np.cos(glonrads)
ypos_obs = distxys_obs * np.sin(glonrads)
zpos_obs = dists_obs * np.sin(glatrads)
xposgals_obs = xpos_obs - rsun
yposgals_obs = ypos_obs
zposgals_obs = zpos_obs + zsun
rgals_obs = np.sqrt(xposgals_obs**2 + yposgals_obs**2)
示例#27
0
def action(ra_deg, dec_deg, d_kpc, pm_ra_masyr, pm_dec_masyr, v_los_kms,
           verbose=False):
    """
    parameters:
    ----------
    ra_deg: (float)
        RA in degrees.
    dec_deg: (float)
        Dec in degress.
    d_kpc: (float)
        Distance in kpc.
    pm_ra_masyr: (float)
        RA proper motion in mas/yr.
    pm_decmasyr: (float)
        Dec proper motion in mas/yr.
    v_los_kms: (float)
        RV in kms.
    returns:
    ------
    R_kpc, phi_rad, z_kpc, vR_kms, vT_kms, vz_kms
    jR: (float)
        Radial action.
    lz: (float)
        Vertical ang mom.
    jz: (float)
        Vertical action.
    """
    ra_rad = ra_deg * (np.pi / 180.)  # RA [rad]
    dec_rad = dec_deg * (np.pi / 180.)  # dec [rad]

    # Galactocentric position of the Sun:
    X_gc_sun_kpc = 8.  # [kpc]
    Z_gc_sun_kpc = 0.025  # [kpc]

    # Galactocentric velocity of the Sun:
    vX_gc_sun_kms = -9.58  # = -U              [kms]
    vY_gc_sun_kms = 10.52 + 220.  # = V+v_circ(R_Sun) [kms]
    vZ_gc_sun_kms = 7.01  # = W               [kms]

    # a. convert spatial coordinates (ra,dec,d) to (R,z,phi)

    # (ra,dec) --> Galactic coordinates (l,b):
    lb = bovy_coords.radec_to_lb(ra_rad, dec_rad, degree=False, epoch=2000.0)
    # l_rad = lb[:, 0]
    # b_rad = lb[:, 1]
    l_rad = lb[0]
    b_rad = lb[1]

    # (l,b,d) --> Galactocentric cartesian coordinates (x,y,z):
    xyz = bovy_coords.lbd_to_XYZ(l_rad, b_rad, d_kpc, degree=False)
    # x_kpc = xyz[:, 0]
    # y_kpc = xyz[:, 1]
    # z_kpc = xyz[:, 2]
    x_kpc = xyz[0]
    y_kpc = xyz[1]
    z_kpc = xyz[2]

    # (x,y,z) --> Galactocentric cylindrical coordinates (R,z,phi):
    Rzphi = bovy_coords.XYZ_to_galcencyl(x_kpc, y_kpc, z_kpc,
                                         Xsun=X_gc_sun_kpc, Zsun=Z_gc_sun_kpc)
    # R_kpc = Rzphi[:, 0]
    # phi_rad = Rzphi[:, 1]
    # z_kpc = Rzphi[:, 2]
    R_kpc = Rzphi[0]
    phi_rad = Rzphi[1]
    z_kpc = Rzphi[2]

    # b. convert velocities (pm_ra,pm_dec,vlos) to (vR,vz,vT)

    # (pm_ra,pm_dec) --> (pm_l,pm_b):
    pmlpmb = bovy_coords.pmrapmdec_to_pmllpmbb(pm_ra_masyr, pm_dec_masyr,
                                               ra_rad, dec_rad, degree=False,
                                               epoch=2000.0)
    # pml_masyr = pmlpmb[:, 0]
    # pmb_masyr = pmlpmb[:, 1]
    pml_masyr = pmlpmb[0]
    pmb_masyr = pmlpmb[1]

    # (v_los,pm_l,pm_b) & (l,b,d) --> (vx,vy,vz):
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(v_los_kms, pml_masyr, pmb_masyr,
                                              l_rad, b_rad, d_kpc, XYZ=False,
                                              degree=False)
    # vx_kms = vxvyvz[:, 0]
    # vy_kms = vxvyvz[:, 1]
    # vz_kms = vxvyvz[:, 2]
    vx_kms = vxvyvz[0]
    vy_kms = vxvyvz[1]
    vz_kms = vxvyvz[2]

    # (vx,vy,vz) & (x,y,z) --> (vR,vT,vz):
    vRvTvZ = bovy_coords.vxvyvz_to_galcencyl(vx_kms, vy_kms, vz_kms, R_kpc,
                                             phi_rad, z_kpc,
                                             vsun=[vX_gc_sun_kms,
                                                   vY_gc_sun_kms,
                                                   vZ_gc_sun_kms],
                                             galcen=True)
    # vR_kms = vRvTvZ[:, 0]
    # vT_kms = vRvTvZ[:, 1]
    # vz_kms = vRvTvZ[:, 2]
    vR_kms = vRvTvZ[0]
    vT_kms = vRvTvZ[1]
    vz_kms = vRvTvZ[2]

    if verbose:
        print("R = ", R_kpc, "\t kpc")
        print("phi = ", phi_rad, "\t rad")
        print("z = ", z_kpc, "\t kpc")
        print("v_R = ", vR_kms, "\t km/s")
        print("v_T = ", vT_kms, "\t km/s")
        print("v_z = ", vz_kms, "\t km/s")

    jR, lz, jz = calc_actions(R_kpc, phi_rad, z_kpc, vR_kms, vT_kms, vz_kms)

    return R_kpc, phi_rad, z_kpc, vR_kms, vT_kms, vz_kms, jR, lz, jz
示例#28
0
文件: Orbit.py 项目: ritabanc/galpy
    def __init__(self,vxvv=None,uvw=False,lb=False,
                 radec=False,vo=235.,ro=8.5,zo=0.025,
                 solarmotion='hogg'):
        """
        NAME:

           __init__

        PURPOSE:

           Initialize an Orbit instance

        INPUT:

           vxvv - initial conditions 
                  3D can be either

              1) in Galactocentric cylindrical coordinates [R,vR,vT(,z,vz,phi)]

              2) [ra,dec,d,mu_ra, mu_dec,vlos] in [deg,deg,kpc,mas/yr,mas/yr,km/s] (all J2000.0; mu_ra = mu_ra * cos dec)

              3) [ra,dec,d,U,V,W] in [deg,deg,kpc,km/s,km/s,kms]

              4) (l,b,d,mu_l, mu_b, vlos) in [deg,deg,kpc,mas/yr,mas/yr,km/s) (all J2000.0; mu_l = mu_l * cos b)

              5) [l,b,d,U,V,W] in [deg,deg,kpc,km/s,km/s,kms]

           4) and 5) also work when leaving out b and mu_b/W

        OPTIONAL INPUTS:

           radec - if True, input is 2) (or 3) above

           uvw - if True, velocities are UVW

           lb - if True, input is 4) or 5) above

           vo - circular velocity at ro

           ro - distance from vantage point to GC (kpc)

           zo - offset toward the NGP of the Sun wrt the plane (kpc)

           solarmotion - 'hogg' or 'dehnen', or 'schoenrich', or value in 
           [-U,V,W]

        OUTPUT:

           instance

        HISTORY:

           2010-07-20 - Written - Bovy (NYU)

        """
        if isinstance(solarmotion,str) and solarmotion.lower() == 'hogg':
            vsolar= nu.array([-10.1,4.0,6.7])/vo
        elif isinstance(solarmotion,str) and solarmotion.lower() == 'dehnen':
            vsolar= nu.array([-10.,5.25,7.17])/vo
        elif isinstance(solarmotion,str) \
                and solarmotion.lower() == 'schoenrich':
            vsolar= nu.array([-11.1,12.24,7.25])/vo
        else:
            vsolar= nu.array(solarmotion)/vo           
        if radec or lb:
            if radec:
                l,b= coords.radec_to_lb(vxvv[0],vxvv[1],degree=True)
            elif len(vxvv) == 4:
                l, b= vxvv[0], 0.
            else:
                l,b= vxvv[0],vxvv[1]
            if uvw:
                X,Y,Z= coords.lbd_to_XYZ(l,b,vxvv[2],degree=True)
                vx= vxvv[3]
                vy= vxvv[4]
                vz= vxvv[5]
            else:
                if radec:
                    pmll, pmbb= coords.pmrapmdec_to_pmllpmbb(vxvv[3],vxvv[4],
                                                             vxvv[0],vxvv[1],
                                                             degree=True)
                    d, vlos= vxvv[2], vxvv[5]
                elif len(vxvv) == 4:
                    pmll, pmbb= vxvv[2], 0.
                    d, vlos= vxvv[1], vxvv[3]
                else:
                    pmll, pmbb= vxvv[3], vxvv[4]
                    d, vlos= vxvv[2], vxvv[5]
                X,Y,Z,vx,vy,vz= coords.sphergal_to_rectgal(l,b,d,
                                                           vlos,pmll, pmbb,
                                                           degree=True)
            X/= ro
            Y/= ro
            Z/= ro
            vx/= vo
            vy/= vo
            vz/= vo
            vsun= nu.array([0.,1.,0.,])+vsolar
            R, phi, z= coords.XYZ_to_galcencyl(X,Y,Z,Zsun=zo/ro)
            vR, vT,vz= coords.vxvyvz_to_galcencyl(vx,vy,vz,
                                                  R,phi,z,
                                                  vsun=vsun,galcen=True)
            if lb and len(vxvv) == 4: vxvv= [R,vR,vT,phi]
            else: vxvv= [R,vR,vT,z,vz,phi]
        self.vxvv= vxvv
        if len(vxvv) == 2:
            self._orb= linearOrbit(vxvv=vxvv)
        elif len(vxvv) == 3:
            self._orb= planarROrbit(vxvv=vxvv)
        elif len(vxvv) == 4:
            self._orb= planarOrbit(vxvv=vxvv)
        elif len(vxvv) == 5:
            self._orb= RZOrbit(vxvv=vxvv)
        elif len(vxvv) == 6:
            self._orb= FullOrbit(vxvv=vxvv)
示例#29
0
def comove_coords(t, lit_gaia):
    ###could add other outputs like Vr, pred, in addition to sep,sep3d,and Vtan off
    ra = t.target_df.squeeze()['ra'] * u.deg
    dec = t.target_df.squeeze()['dec'] * u.deg
    distance = (1000.0 / t.target_df.squeeze()['parallax']) * u.pc
    radvel = t.target_df.squeeze(
    )['dr2_radial_velocity'] * u.kilometer / u.second
    pmra = t.target_df.squeeze()['pmra'] * u.mas / u.year
    pmdec = t.target_df.squeeze()['pmdec'] * u.mas / u.year



    Pcoord = SkyCoord( ra=ra, dec=dec, \
                      distance=distance, frame='icrs' , \
                      radial_velocity=radvel , \
                      pm_ra_cosdec= pmra , pm_dec= pmdec )

    # # Query Gaia with search radius and parallax cut
    # # Note, a cut on parallax_error was added because searches at low galactic latitude
    # # return an overwhelming number of noisy sources that scatter into the search volume - ALK 20210325
    # print('Querying Gaia for neighbors')
    # if (searchradpc < Pcoord.distance):
    #     sqltext = "SELECT * FROM gaiaedr3.gaia_source WHERE CONTAINS( \
    #         POINT('ICRS',gaiaedr3.gaia_source.ra,gaiaedr3.gaia_source.dec), \
    #         CIRCLE('ICRS'," + str(Pcoord.ra.value) +","+ str(Pcoord.dec.value) +","+ str(searchraddeg.value) +"))\
    #         =1 AND parallax>" + str(minpar.value) + " AND parallax_error<0.5;"
    # if (searchradpc >= Pcoord.distance):
    #     sqltext = "SELECT * FROM gaiaedr3.gaia_source WHERE parallax>" + str(minpar.value) + " AND parallax_error<0.5;"
    #     print('Note, using all-sky search')
    # if verbose == True:
    #     print(sqltext)
    #     print()

    # job = Gaia.launch_job_async(sqltext , dump_to_file=False)
    # r = job.get_results()

    # if verbose == True: print('Number of records: ',len(r['ra']))

    # # Construct coordinates array for all stars returned in cone search

    # gaiacoord = SkyCoord( ra=r['ra'] , dec=r['dec'] , distance=(1000.0/r['parallax'])*u.parsec , \
    #                      frame='icrs' , \
    #                      pm_ra_cosdec=r['pmra'] , pm_dec=r['pmdec'] )

    lit_sc = SkyCoord(
        ra=lit_gaia.ra.to_numpy(dtype='float') * u.deg,
        dec=lit_gaia.dec.to_numpy(dtype='float') * u.deg,
        pm_ra_cosdec=lit_gaia.pmra.to_numpy(dtype='float') * u.mas / u.yr,
        pm_dec=lit_gaia.pmdec.to_numpy(dtype='float') * u.mas / u.yr,
        distance=u.pc * (1000. / lit_gaia.parallax.to_numpy(dtype='float')))

    sep = lit_sc.separation(Pcoord)  #in degrees
    sep3d = lit_sc.separation_3d(Pcoord)  #in parsec

    Pllbb = bc.radec_to_lb(Pcoord.ra.value, Pcoord.dec.value, degree=True)
    Ppmllpmbb = bc.pmrapmdec_to_pmllpmbb( Pcoord.pm_ra_cosdec.value , Pcoord.pm_dec.value , \
                                         Pcoord.ra.value , Pcoord.dec.value , degree=True )
    Pvxvyvz   = bc.vrpmllpmbb_to_vxvyvz(Pcoord.radial_velocity.value , Ppmllpmbb[0] , Ppmllpmbb[1] , \
                                   Pllbb[0] , Pllbb[1] , Pcoord.distance.value/1000.0 , XYZ=False , degree=True)

    Gllbb = bc.radec_to_lb(lit_sc.ra.value, lit_sc.dec.value, degree=True)
    Gxyz = bc.lbd_to_XYZ(Gllbb[:, 0],
                         Gllbb[:, 1],
                         lit_sc.distance / 1000.0,
                         degree=True)
    Gvrpmllpmbb = bc.vxvyvz_to_vrpmllpmbb( \
                    Pvxvyvz[0]*np.ones(len(Gxyz[:,0])) , Pvxvyvz[1]*np.ones(len(Gxyz[:,1])) , Pvxvyvz[2]*np.ones(len(Gxyz[:,2])) , \
                    Gxyz[:,0] , Gxyz[:,1] , Gxyz[:,2] , XYZ=True)
    Gpmrapmdec = bc.pmllpmbb_to_pmrapmdec(Gvrpmllpmbb[:, 1],
                                          Gvrpmllpmbb[:, 2],
                                          Gllbb[:, 0],
                                          Gllbb[:, 1],
                                          degree=True)

    # Code in case I want to do chi^2 cuts someday
    Gvtanerr = 1.0 * np.ones(len(Gxyz[:, 0]))
    Gpmerr = Gvtanerr * 206265000.0 * 3.154e7 / (lit_sc.distance.value *
                                                 3.086e13)

    Gchi2 = ((Gpmrapmdec[:, 0] - lit_sc.pm_ra_cosdec.value)**2 +
             (Gpmrapmdec[:, 1] - lit_sc.pm_dec.value)**2)**0.5
    vtanoff = Gchi2 / Gpmerr  #this is reported Vtan,off(km/s)

    ##vr pred
    vr_pred = Gvrpmllpmbb[:, 0]

    #create results dataframe
    res = pd.DataFrame(
        data={
            'tic': lit_gaia.tic.to_numpy(dtype='str'),
            'designation': lit_gaia.designation.to_numpy(dtype='str'),
            'ra': lit_sc.ra.value,
            'dec': lit_sc.dec.value,
            'sep2D(deg)': sep.value,
            'sep3D(pc)': sep3d.value,
            'Vtan,off(km/s)': vtanoff,
            'Vr,pred(km/s)': vr_pred
        })

    return (res)
示例#30
0
def from_radec(cluster, do_order=False, do_key_params=False):
    """Calculate galactocentric coordinates from on-sky position, proper motion, and radial velocity of cluster

    Parameters
    ----------
    cluster : class
        StarCluster
    do_order : bool
        sort star by radius after coordinate change (default: False)
    do_key_params : bool
        call key_params to calculate key parameters after unit change (default: False)

    Returns
    -------
    None

    History:
    -------
    2018 - Written - Webb (UofT)

    """
    if cluster.units == "radec" and cluster.origin == "sky":

        origin0 = cluster.origin

        l, b = bovy_coords.radec_to_lb(cluster.ra, cluster.dec, degree=True).T
        x0, y0, z0 = bovy_coords.lbd_to_XYZ(l, b, cluster.dist, degree=True).T
        cluster.x, cluster.y, cluster.z = bovy_coords.XYZ_to_galcenrect(
            x0, y0, z0, Xsun=8.0, Zsun=0.025).T

        pml, pmb = bovy_coords.pmrapmdec_to_pmllpmbb(cluster.pmra,
                                                     cluster.pmdec,
                                                     cluster.ra,
                                                     cluster.dec,
                                                     degree=True).T
        vx0, vy0, vz0 = bovy_coords.vrpmllpmbb_to_vxvyvz(cluster.vlos,
                                                         pml,
                                                         pmb,
                                                         l,
                                                         b,
                                                         cluster.dist,
                                                         degree=True).T
        cluster.vx, cluster.vy, cluster.vz = bovy_coords.vxvyvz_to_galcenrect(
            vx0,
            vy0,
            vz0,
            vsun=[0.0, 220.0, 0.0],
            Xsun=8.0,
            Zsun=0.025,
            _extra_rot=True,
        ).T

        l_gc, b_gc = bovy_coords.radec_to_lb(cluster.ra_gc,
                                             cluster.dec_gc,
                                             degree=True)
        x0_gc, y0_gc, z0_gc = bovy_coords.lbd_to_XYZ(l_gc,
                                                     b_gc,
                                                     cluster.dist_gc,
                                                     degree=True)
        cluster.xgc, cluster.ygc, cluster.zgc = bovy_coords.XYZ_to_galcenrect(
            x0_gc, y0_gc, z0_gc, Xsun=8.0, Zsun=0.025)

        pml_gc, pmb_gc = bovy_coords.pmrapmdec_to_pmllpmbb(cluster.pmra_gc,
                                                           cluster.pmdec_gc,
                                                           cluster.ra_gc,
                                                           cluster.dec_gc,
                                                           degree=True)
        vx0_gc, vy0_gc, vz0_gc = bovy_coords.vrpmllpmbb_to_vxvyvz(
            cluster.vlos_gc,
            pml_gc,
            pmb_gc,
            l_gc,
            b_gc,
            cluster.dist_gc,
            degree=True)
        cluster.vx_gc, cluster.vy_gc, cluster.vz_gc = bovy_coords.vxvyvz_to_galcenrect(
            vx0_gc,
            vy0_gc,
            vz0_gc,
            vsun=[0.0, 220.0, 0.0],
            Xsun=8.0,
            Zsun=0.025,
            _extra_rot=True,
        )

        cluster.origin = "galaxy"
        cluster.units = "kpckms"

    cluster.rv3d()

    if do_key_params:
        cluster.key_params(do_order=do_order)
示例#31
0
def make_rcsample(parser):
    options,args= parser.parse_args()
    savefilename= options.savefilename
    if savefilename is None:
        #Create savefilename if not given
        savefilename= os.path.join(appath._APOGEE_DATA,
                                   'rcsample_'+appath._APOGEE_REDUX+'.fits')
        print "Saving to %s ..." % savefilename
    #Read the base-sample
    data= apread.allStar(adddist=_ADDHAYDENDIST,rmdups=options.rmdups)
    #Remove a bunch of fields that we do not want to keep
    data= esutil.numpy_util.remove_fields(data,
                                          ['TARGET_ID',
                                           'FILE',
                                           'AK_WISE',
                                           'SFD_EBV',
                                           'SYNTHVHELIO_AVG',
                                           'SYNTHVSCATTER',
                                           'SYNTHVERR',
                                           'SYNTHVERR_MED',
                                           'RV_TEFF',
                                           'RV_LOGG',
                                           'RV_FEH',
                                           'RV_CCFWHM',
                                           'RV_AUTOFWHM',
                                           'SYNTHSCATTER',
                                           'CHI2_THRESHOLD',
                                           'APSTAR_VERSION',
                                           'ASPCAP_VERSION',
                                           'RESULTS_VERSION',
                                           'REDUCTION_ID',
                                           'SRC_H',
                                           'PM_SRC'])
    if not appath._APOGEE_REDUX.lower() == 'current' \
            and int(appath._APOGEE_REDUX[1:]) < 500:
        data= esutil.numpy_util.remove_fields(data,
                                              ['ELEM'])
    #Select red-clump stars
    jk= data['J0']-data['K0']
    z= isodist.FEH2Z(data['METALS'],zsolar=0.017)
    if appath._APOGEE_REDUX.lower() == 'current' \
            or int(appath._APOGEE_REDUX[1:]) > 600:
        from apogee.tools import paramIndx
        if False:
            #Use my custom logg calibration that's correct for the RC
            logg= (1.-0.042)*data['FPARAM'][:,paramIndx('logg')]-0.213
            lowloggindx= data['FPARAM'][:,paramIndx('logg')] < 1.
            logg[lowloggindx]= data['FPARAM'][lowloggindx,paramIndx('logg')]-0.255
            hiloggindx= data['FPARAM'][:,paramIndx('logg')] > 3.8
            logg[hiloggindx]= data['FPARAM'][hiloggindx,paramIndx('logg')]-0.3726
        else:
            #Use my custom logg calibration that's correct on average
            logg= (1.+0.03)*data['FPARAM'][:,paramIndx('logg')]-0.37
            lowloggindx= data['FPARAM'][:,paramIndx('logg')] < 1.
            logg[lowloggindx]= data['FPARAM'][lowloggindx,paramIndx('logg')]-0.34
            hiloggindx= data['FPARAM'][:,paramIndx('logg')] > 3.8
            logg[hiloggindx]= data['FPARAM'][hiloggindx,paramIndx('logg')]-0.256
    else:
        logg= data['LOGG']
    indx= (jk < 0.8)*(jk >= 0.5)\
        *(z <= 0.06)\
        *(z <= rcmodel.jkzcut(jk,upper=True))\
        *(z >= rcmodel.jkzcut(jk))\
        *(logg >= rcmodel.loggteffcut(data['TEFF'],z,upper=False))\
        *(logg <= rcmodel.loggteffcut(data['TEFF'],z,upper=True))
    data= data[indx]
    #Add more aggressive flag cut
    data= esutil.numpy_util.add_fields(data,[('ADDL_LOGG_CUT',numpy.int32)])
    data['ADDL_LOGG_CUT']= ((data['TEFF']-4800.)/1000.+2.75) > data['LOGG']
    if options.loggcut:
        data= data[data['ADDL_LOGG_CUT'] == 1]
    print "Making catalog of %i objects ..." % len(data)
    #Add distances
    data= esutil.numpy_util.add_fields(data,[('RC_DIST', float),
                                             ('RC_DM', float),
                                             ('RC_GALR', float),
                                             ('RC_GALPHI', float),
                                             ('RC_GALZ', float)])
    rcd= rcmodel.rcdist()
    jk= data['J0']-data['K0']
    z= isodist.FEH2Z(data['METALS'],zsolar=0.017)
    data['RC_DIST']= rcd(jk,z,appmag=data['K0'])*options.distfac
    data['RC_DM']= 5.*numpy.log10(data['RC_DIST'])+10.
    XYZ= bovy_coords.lbd_to_XYZ(data['GLON'],
                                data['GLAT'],
                                data['RC_DIST'],
                                degree=True)
    R,phi,Z= bovy_coords.XYZ_to_galcencyl(XYZ[:,0],
                                          XYZ[:,1],
                                          XYZ[:,2],
                                          Xsun=8.,Zsun=0.025)
    data['RC_GALR']= R
    data['RC_GALPHI']= phi
    data['RC_GALZ']= Z
    #Save
    fitsio.write(savefilename,data,clobber=True)
    if not options.nostat:
        #Determine statistical sample and add flag
        apo= apogee.select.apogeeSelect()
        statIndx= apo.determine_statistical(data)
        mainIndx= apread.mainIndx(data)
        data= esutil.numpy_util.add_fields(data,[('STAT',numpy.int32),
                                                 ('INVSF',float)])
        data['STAT']= 0
        data['STAT'][statIndx*mainIndx]= 1
        for ii in range(len(data)):
            if (statIndx*mainIndx)[ii]:
                data['INVSF'][ii]= 1./apo(data['LOCATION_ID'][ii],
                                          data['H'][ii])
            else:
                data['INVSF'][ii]= -1.
    if options.nopm:
        fitsio.write(savefilename,data,clobber=True)       
        return None
    #Get proper motions
    from astroquery.vizier import Vizier
    import astroquery
    from astropy import units as u
    import astropy.coordinates as coord
    pmfile= savefilename.split('.')[0]+'_pms.fits'
    if os.path.exists(pmfile):
        pmdata= fitsio.read(pmfile,1)
    else:
        pmdata= numpy.recarray(len(data),
                               formats=['f8','f8','f8','f8','f8','f8','i4'],
                               names=['RA','DEC','PMRA','PMDEC',
                                      'PMRA_ERR','PMDEC_ERR','PMMATCH'])
        rad= u.Quantity(4./3600.,u.degree)
        v= Vizier(columns=['RAJ2000','DEJ2000','pmRA','pmDE','e_pmRA','e_pmDE'])
        for ii in range(len(data)):
            #if ii > 100: break
            sys.stdout.write('\r'+"Getting pm data for point %i / %i" % (ii+1,len(data)))
            sys.stdout.flush()
            pmdata.RA[ii]= data['RA'][ii]
            pmdata.DEC[ii]= data['DEC'][ii]
            co= coord.ICRS(ra=data['RA'][ii],
                           dec=data['DEC'][ii],
                           unit=(u.degree, u.degree))
            trying= True
            while trying:
                try:
                    tab= v.query_region(co,rad,catalog='I/322') #UCAC-4 catalog
                except astroquery.exceptions.TimeoutError:
                    pass
                else:
                    trying= False
            if len(tab) == 0:
                pmdata.PMMATCH[ii]= 0
                print "Didn't find a match for %i ..." % ii
                continue
            else:
                pmdata.PMMATCH[ii]= len(tab)
                if len(tab[0]['pmRA']) > 1:
                    print "Found more than 1 match for %i ..." % ii
            try:
                pmdata.PMRA[ii]= float(tab[0]['pmRA'])
            except TypeError:
                jj= 1
                while len(tab[0]['pmRA']) > 1 and jj < 4: 
                    trad= u.Quantity((4.-jj)/3600.,u.degree)
                    trying= True
                    while trying:
                        try:
                            tab= v.query_region(co,trad,catalog='I/322') #UCAC-4 catalog
                        except astroquery.exceptions.TimeoutError:
                            pass
                        else:
                            trying= False
                    jj+= 1
                if len(tab) == 0:
                    pmdata.PMMATCH[ii]= 0
                    print "Didn't find a unambiguous match for %i ..." % ii
                    continue               
                pmdata.PMRA[ii]= float(tab[0]['pmRA'])
            pmdata.PMDEC[ii]= float(tab[0]['pmDE'])
            pmdata.PMRA_ERR[ii]= float(tab[0]['e_pmRA'])
            pmdata.PMDEC_ERR[ii]= float(tab[0]['e_pmDE'])
            if numpy.isnan(float(tab[0]['pmRA'])): pmdata.PMMATCH[ii]= 0
        sys.stdout.write('\r'+_ERASESTR+'\r')
        sys.stdout.flush()
        fitsio.write(pmfile,pmdata,clobber=True)
        #To make sure we're using the same format below
        pmdata= fitsio.read(pmfile,1)
    #Match proper motions
    try: #These already exist currently, but may not always exist
        data= esutil.numpy_util.remove_fields(data,['PMRA','PMDEC'])
    except ValueError:
        pass
    data= esutil.numpy_util.add_fields(data,[('PMRA', numpy.float),
                                             ('PMDEC', numpy.float),
                                             ('PMRA_ERR', numpy.float),
                                             ('PMDEC_ERR', numpy.float),
                                             ('PMMATCH',numpy.int32)])
    data['PMMATCH']= 0
    h=esutil.htm.HTM()
    m1,m2,d12 = h.match(pmdata['RA'],pmdata['DEC'],
                        data['RA'],data['DEC'],
                        2./3600.,maxmatch=1)
    data['PMRA'][m2]= pmdata['PMRA'][m1]
    data['PMDEC'][m2]= pmdata['PMDEC'][m1]
    data['PMRA_ERR'][m2]= pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR'][m2]= pmdata['PMDEC_ERR'][m1]
    data['PMMATCH'][m2]= pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx= data['PMMATCH'] == 1
    data['PMRA'][True-pmindx]= -9999.99
    data['PMDEC'][True-pmindx]= -9999.99
    data['PMRA_ERR'][True-pmindx]= -9999.99
    data['PMDEC_ERR'][True-pmindx]= -9999.99
    #Calculate Galactocentric velocities
    data= esutil.numpy_util.add_fields(data,[('GALVR', numpy.float),
                                             ('GALVT', numpy.float),
                                             ('GALVZ', numpy.float)])
    lb= bovy_coords.radec_to_lb(data['RA'],data['DEC'],degree=True)
    XYZ= bovy_coords.lbd_to_XYZ(lb[:,0],lb[:,1],data['RC_DIST'],degree=True)
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA'],data['PMDEC'],
                                                data['RA'],data['DEC'],
                                                degree=True)
    vxvyvz= bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                             pmllpmbb[:,0],
                                             pmllpmbb[:,1],
                                             lb[:,0],lb[:,1],data['RC_DIST'],
                                             degree=True)
    vR, vT, vZ= bovy_coords.vxvyvz_to_galcencyl(vxvyvz[:,0],
                                                vxvyvz[:,1],
                                                vxvyvz[:,2],
                                                8.-XYZ[:,0],
                                                XYZ[:,1],
                                                XYZ[:,2]+0.025,
                                                vsun=[-11.1,30.24*8.,7.25])#Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR']= vR
    data['GALVT']= vT
    data['GALVZ']= vZ
    data['GALVR'][True-pmindx]= -9999.99
    data['GALVT'][True-pmindx]= -9999.99
    data['GALVZ'][True-pmindx]= -9999.99
    #Get proper motions
    pmfile= savefilename.split('.')[0]+'_pms_ppmxl.fits'
    if os.path.exists(pmfile):
        pmdata= fitsio.read(pmfile,1)
    else:
        pmdata= numpy.recarray(len(data),
                               formats=['f8','f8','f8','f8','f8','f8','i4'],
                               names=['RA','DEC','PMRA','PMDEC',
                                      'PMRA_ERR','PMDEC_ERR','PMMATCH'])
        rad= u.Quantity(4./3600.,u.degree)
        v= Vizier(columns=['RAJ2000','DEJ2000','pmRA','pmDE','e_pmRA','e_pmDE'])
        for ii in range(len(data)):
            #if ii > 100: break
            sys.stdout.write('\r'+"Getting pm data for point %i / %i" % (ii+1,len(data)))
            sys.stdout.flush()
            pmdata.RA[ii]= data['RA'][ii]
            pmdata.DEC[ii]= data['DEC'][ii]
            co= coord.ICRS(ra=data['RA'][ii],
                           dec=data['DEC'][ii],
                           unit=(u.degree, u.degree))
            trying= True
            while trying:
                try:
                    tab= v.query_region(co,rad,catalog='I/317') #PPMXL catalog
                except astroquery.exceptions.TimeoutError:
                    pass
                else:
                    trying= False
            if len(tab) == 0:
                pmdata.PMMATCH[ii]= 0
                print "Didn't find a match for %i ..." % ii
                continue
            else:
                pmdata.PMMATCH[ii]= len(tab)
                if len(tab[0]['pmRA']) > 1:
                    pass
                    #print "Found more than 1 match for %i ..." % ii
            try:
                pmdata.PMRA[ii]= float(tab[0]['pmRA'])
            except TypeError:
                #Find nearest
                cosdists= numpy.zeros(len(tab[0]['pmRA']))
                for jj in range(len(tab[0]['pmRA'])):
                    cosdists[jj]= cos_sphere_dist(tab[0]['RAJ2000'][jj],
                                                  tab[0]['DEJ2000'][jj],
                                                  data['RA'][ii],
                                                  data['DEC'][ii])
                closest= numpy.argmax(cosdists)
                pmdata.PMRA[ii]= float(tab[0]['pmRA'][closest])
                pmdata.PMDEC[ii]= float(tab[0]['pmDE'][closest])
                pmdata.PMRA_ERR[ii]= float(tab[0]['e_pmRA'][closest])
                pmdata.PMDEC_ERR[ii]= float(tab[0]['e_pmDE'][closest])
                if numpy.isnan(float(tab[0]['pmRA'][closest])): pmdata.PMMATCH[ii]= 0
            else:
                pmdata.PMDEC[ii]= float(tab[0]['pmDE'])
                pmdata.PMRA_ERR[ii]= float(tab[0]['e_pmRA'])
                pmdata.PMDEC_ERR[ii]= float(tab[0]['e_pmDE'])
                if numpy.isnan(float(tab[0]['pmRA'])): pmdata.PMMATCH[ii]= 0
        sys.stdout.write('\r'+_ERASESTR+'\r')
        sys.stdout.flush()
        fitsio.write(pmfile,pmdata,clobber=True)
        #To make sure we're using the same format below
        pmdata= fitsio.read(pmfile,1)
    #Match proper motions to ppmxl
    data= esutil.numpy_util.add_fields(data,[('PMRA_PPMXL', numpy.float),
                                             ('PMDEC_PPMXL', numpy.float),
                                             ('PMRA_ERR_PPMXL', numpy.float),
                                             ('PMDEC_ERR_PPMXL', numpy.float),
                                             ('PMMATCH_PPMXL',numpy.int32)])
    data['PMMATCH_PPMXL']= 0
    h=esutil.htm.HTM()
    m1,m2,d12 = h.match(pmdata['RA'],pmdata['DEC'],
                        data['RA'],data['DEC'],
                        2./3600.,maxmatch=1)
    data['PMRA_PPMXL'][m2]= pmdata['PMRA'][m1]
    data['PMDEC_PPMXL'][m2]= pmdata['PMDEC'][m1]
    data['PMRA_ERR_PPMXL'][m2]= pmdata['PMRA_ERR'][m1]
    data['PMDEC_ERR_PPMXL'][m2]= pmdata['PMDEC_ERR'][m1]
    data['PMMATCH_PPMXL'][m2]= pmdata['PMMATCH'][m1].astype(numpy.int32)
    pmindx= data['PMMATCH_PPMXL'] == 1
    data['PMRA_PPMXL'][True-pmindx]= -9999.99
    data['PMDEC_PPMXL'][True-pmindx]= -9999.99
    data['PMRA_ERR_PPMXL'][True-pmindx]= -9999.99
    data['PMDEC_ERR_PPMXL'][True-pmindx]= -9999.99
    #Calculate Galactocentric velocities
    data= esutil.numpy_util.add_fields(data,[('GALVR_PPMXL', numpy.float),
                                             ('GALVT_PPMXL', numpy.float),
                                             ('GALVZ_PPMXL', numpy.float)])
    lb= bovy_coords.radec_to_lb(data['RA'],data['DEC'],degree=True)
    XYZ= bovy_coords.lbd_to_XYZ(lb[:,0],lb[:,1],data['RC_DIST'],degree=True)
    pmllpmbb= bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA_PPMXL'],
                                                data['PMDEC_PPMXL'],
                                                data['RA'],data['DEC'],
                                                degree=True)
    vxvyvz= bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                             pmllpmbb[:,0],
                                             pmllpmbb[:,1],
                                             lb[:,0],lb[:,1],data['RC_DIST'],
                                             degree=True)
    vR, vT, vZ= bovy_coords.vxvyvz_to_galcencyl(vxvyvz[:,0],
                                                vxvyvz[:,1],
                                                vxvyvz[:,2],
                                                8.-XYZ[:,0],
                                                XYZ[:,1],
                                                XYZ[:,2]+0.025,
                                                vsun=[-11.1,30.24*8.,7.25])#Assumes proper motion of Sgr A* and R0=8 kpc, zo= 25 pc
    data['GALVR_PPMXL']= vR
    data['GALVT_PPMXL']= vT
    data['GALVZ_PPMXL']= vZ
    data['GALVR_PPMXL'][True-pmindx]= -9999.99
    data['GALVT_PPMXL'][True-pmindx]= -9999.99
    data['GALVZ_PPMXL'][True-pmindx]= -9999.99
    #Save
    fitsio.write(savefilename,data,clobber=True)
    return None
示例#32
0
def _add_proper_motions_gaia(data):
    from gaia_tools import xmatch
    gaia2_matches, matches_indx = xmatch.cds(data,
                                             colRA='RA',
                                             colDec='DEC',
                                             xcat='vizier:I/345/gaia2')
    # Add matches
    try:  #These already exist currently, but may not always exist
        data = esutil.numpy_util.remove_fields(data, ['PMRA', 'PMDEC'])
    except ValueError:
        pass
    data = esutil.numpy_util.add_fields(data, [('PLX', numpy.float),
                                               ('PMRA', numpy.float),
                                               ('PMDEC', numpy.float),
                                               ('PLX_ERR', numpy.float),
                                               ('PMRA_ERR', numpy.float),
                                               ('PMDEC_ERR', numpy.float),
                                               ('PMMATCH', numpy.int32)])
    data['PMMATCH'] = 0
    data['PMMATCH'][matches_indx] = 1
    data['PLX'][matches_indx] = gaia2_matches['parallax']
    data['PMRA'][matches_indx] = gaia2_matches['pmra']
    data['PMDEC'][matches_indx] = gaia2_matches['pmdec']
    data['PLX_ERR'][matches_indx] = gaia2_matches['parallax_error']
    data['PMRA_ERR'][matches_indx] = gaia2_matches['pmra_error']
    data['PMDEC_ERR'][matches_indx] = gaia2_matches['pmdec_error']
    # Set values for those without match to -999
    pmindx = data['PMMATCH'] == 1
    data['PLX'][True ^ pmindx] = -9999.99
    data['PMRA'][True ^ pmindx] = -9999.99
    data['PMDEC'][True ^ pmindx] = -9999.99
    data['PLX_ERR'][True ^ pmindx] = -9999.99
    data['PMRA_ERR'][True ^ pmindx] = -9999.99
    data['PMDEC_ERR'][True ^ pmindx] = -9999.99
    #Calculate Galactocentric velocities
    data = esutil.numpy_util.add_fields(data, [('GALVR', numpy.float),
                                               ('GALVT', numpy.float),
                                               ('GALVZ', numpy.float)])
    lb = bovy_coords.radec_to_lb(data['RA'], data['DEC'], degree=True)
    XYZ = bovy_coords.lbd_to_XYZ(lb[:, 0],
                                 lb[:, 1],
                                 data['RC_DIST'],
                                 degree=True)
    pmllpmbb = bovy_coords.pmrapmdec_to_pmllpmbb(data['PMRA'],
                                                 data['PMDEC'],
                                                 data['RA'],
                                                 data['DEC'],
                                                 degree=True)
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(data['VHELIO_AVG'],
                                              pmllpmbb[:, 0],
                                              pmllpmbb[:, 1],
                                              lb[:, 0],
                                              lb[:, 1],
                                              data['RC_DIST'],
                                              degree=True)
    vRvTvZ = bovy_coords.vxvyvz_to_galcencyl(
        vxvyvz[:, 0],
        vxvyvz[:, 1],
        vxvyvz[:, 2],
        8. - XYZ[:, 0],
        XYZ[:, 1],
        XYZ[:, 2] + 0.0208,
        vsun=[-11.1, 30.24 * 8.15, 7.25]
    )  #Assumes proper motion of Sgr A* and R0=8.15 kpc, zo= 20.8 pc (Bennett & Bovy 2019)
    data['GALVR'] = vRvTvZ[:, 0]
    data['GALVT'] = vRvTvZ[:, 1]
    data['GALVZ'] = vRvTvZ[:, 2]
    data['GALVR'][True ^ pmindx] = -9999.99
    data['GALVT'][True ^ pmindx] = -9999.99
    data['GALVZ'][True ^ pmindx] = -9999.99
    return data
示例#33
0
rdata[:,9]=rdata[:,9]*1000.0
rdata[:,10]=rdata[:,10]*1000.0
rdata[:,15]=rdata[:,15]*1000.0
rdata[:,16]=rdata[:,16]*1000.0

# Galactic coordinates
# True l and b
RA_true=rdata[:,0]
DEC_true=rdata[:,1]
Tllbb=bovy_coords.radec_to_lb(RA_true,DEC_true,degree=True,epoch=2000.0)
GLON_true=Tllbb[:,0]
GLAT_true=Tllbb[:,1]
# True pmGLON, pmGLAT
pmRA_true=rdata[:,3]
pmDEC_true=rdata[:,4]
Tpmllbb=bovy_coords.pmrapmdec_to_pmllpmbb(pmRA_true,pmDEC_true \
   ,RA_true,DEC_true,degree=True,epoch=2000.0)
pmGLON_true=Tpmllbb[:,0]
pmGLAT_true=Tpmllbb[:,1]
# observed 
RA_obs=rdata[:,6]
DEC_obs=rdata[:,7]
pmRA_obs=rdata[:,9]
pmDEC_obs=rdata[:,10]
Tpmllbb=bovy_coords.pmrapmdec_to_pmllpmbb(pmRA_obs,pmDEC_obs \
   ,RA_true,DEC_true,degree=True,epoch=2000.0)
pmGLON_obs=Tpmllbb[:,0]
pmGLAT_obs=Tpmllbb[:,1]
# error
e_pmRA=rdata[:,12]
e_pmDEC=rdata[:,13]
Tpmllbb=bovy_coords.pmrapmdec_to_pmllpmbb(e_pmRA,e_pmDEC \
示例#34
0
def calc_actions(ra_deg, dec_deg, d_kpc, pm_ra_masyr, pm_dec_masyr, v_los_kms):
    ra_rad = ra_deg * (np.pi / 180.)  # RA [rad]
    dec_rad = dec_deg * (np.pi / 180.)  # dec [rad]

    # Galactocentric position of the Sun:
    X_gc_sun_kpc = 8.  # [kpc]
    Z_gc_sun_kpc = 0.025  # [kpc]

    # Galactocentric velocity of the Sun:
    vX_gc_sun_kms = -9.58  # = -U              [kms]
    vY_gc_sun_kms = 10.52 + 220.  # = V+v_circ(R_Sun) [kms]
    vZ_gc_sun_kms = 7.01  # = W               [kms]

    # a. convert spatial coordinates (ra,dec,d) to (R,z,phi)

    # (ra,dec) --> Galactic coordinates (l,b):
    lb = bovy_coords.radec_to_lb(ra_rad, dec_rad, degree=False, epoch=2000.0)
    l_rad = lb[:, 0]
    b_rad = lb[:, 1]

    # (l,b,d) --> Galactocentric cartesian coordinates (x,y,z):
    xyz = bovy_coords.lbd_to_XYZ(l_rad, b_rad, d_kpc, degree=False)
    x_kpc = xyz[:, 0]
    y_kpc = xyz[:, 1]
    z_kpc = xyz[:, 2]

    # (x,y,z) --> Galactocentric cylindrical coordinates (R,z,phi):
    Rzphi = bovy_coords.XYZ_to_galcencyl(x_kpc,
                                         y_kpc,
                                         z_kpc,
                                         Xsun=X_gc_sun_kpc,
                                         Zsun=Z_gc_sun_kpc)
    R_kpc = Rzphi[:, 0]
    phi_rad = Rzphi[:, 1]
    z_kpc = Rzphi[:, 2]

    # b. convert velocities (pm_ra,pm_dec,vlos) to (vR,vz,vT)

    # (pm_ra,pm_dec) --> (pm_l,pm_b):
    pmlpmb = bovy_coords.pmrapmdec_to_pmllpmbb(pm_ra_masyr,
                                               pm_dec_masyr,
                                               ra_rad,
                                               dec_rad,
                                               degree=False,
                                               epoch=2000.0)
    pml_masyr = pmlpmb[:, 0]
    pmb_masyr = pmlpmb[:, 1]

    # (v_los,pm_l,pm_b) & (l,b,d) --> (vx,vy,vz):
    vxvyvz = bovy_coords.vrpmllpmbb_to_vxvyvz(v_los_kms,
                                              pml_masyr,
                                              pmb_masyr,
                                              l_rad,
                                              b_rad,
                                              d_kpc,
                                              XYZ=False,
                                              degree=False)
    vx_kms = vxvyvz[:, 0]
    vy_kms = vxvyvz[:, 1]
    vz_kms = vxvyvz[:, 2]

    # (vx,vy,vz) & (x,y,z) --> (vR,vT,vz):
    vRvTvZ = bovy_coords.vxvyvz_to_galcencyl(
        vx_kms,
        vy_kms,
        vz_kms,
        R_kpc,
        phi_rad,
        z_kpc,
        vsun=[vX_gc_sun_kms, vY_gc_sun_kms, vZ_gc_sun_kms],
        galcen=True)
    vR_kms = vRvTvZ[:, 0]
    vT_kms = vRvTvZ[:, 1]
    vz_kms = vRvTvZ[:, 2]

    print("R = ", R_kpc, "\t kpc")
    print("phi = ", phi_rad, "\t rad")
    print("z = ", z_kpc, "\t kpc")
    print("v_R = ", vR_kms, "\t km/s")
    print("v_T = ", vT_kms, "\t km/s")
    print("v_z = ", vz_kms, "\t km/s")
    return vz_kms
示例#35
0
def sample_obs_error_5D(property_list:_str_plist, Mg:"mag"=0.64, Mg_err:"mag"=0.24, Rsun:_str_kpc=8.2, Rsun_err:_str_kpc=None, U:_str_kms=11.1, V:_str_kms=12.24, W:_str_kms=7.25, U_err:_str_kms=None, V_err:_str_kms=None, W_err:_str_kms=None, Vlsr:_str_kms=235, Vlsr_err:_str_kms=None, N:"int"=1000)->"array and dic with properties":
	"""
	NB: THE INPUT ARE ASSUME A GALACTIC RH system (Sun is a x=Rsun),
		BUT THE OUTPUT ARE IN GALACTIC LH system (I know is crazy).
	:param property_list: A tuple with the following properties (in this order):
		"(id: source_id of the star (can be None),
		 ra: degrees,
		 dec: degrees,
		 l: degrees,
		 b: degrees,
		 pmra: pmra proper motion (non corrected for solar motion) mas/yr,
		 pmdec: pmdec proper motion (non corrected for solar motion) mas/yr,
		 pmra_err: pmra error,
		 pmdec_err: pmdec error,
		 cov_pmra_pmdec: pmra-pmdec corr. coff (sigma_ra_dec/(sigma_ra*sigma_dec)),
		 gc: G magnitude corrected for extinction (can be None if distance is provided),
		 distance: Heliocentric distance in kpc (can be None if gc is provided)
		 distance_error:
		 internal_id: a user defined internal_id (can be None)".
	:param Mg: Absolute magnitude to estimate distance from gc.
	:param Mg_err: error on Absolute magnitude.
	:param Rsun: Distance of the Sun from the Galactic centre.
	:param Rsun_err: error on Rsun.
	:param U: Solar motion (wrt LSR) toward the Galactic center
	(NB: here it is defined positive if it is toward the Galctice centre, but sample_obs_erro we used a left-hand system,
	in this system a motion toward the GC is negatie. However this converstion is automatically made).
	:param V: Solar proper motion (wrt LSR) along the direction of Galactic rotation.
	:param W: Solar proper motion (wrt LSR) along the normal to the Galactic plane (positive value is an upaward motion).
	:param U_err: error on U.
	:param V_err: error on V.
	:param W_err: error on W.
	:param Vlsr:  Circular motion of the LSR.
	:param Vlsr_err:  Error on Vlsr.
	:param N: Number of MC samples to generate.
	:return: An array and a dictionary containing spatial and kinematic information obtained from the observables.
	"""

	_key_list_obs = ('x', 'y', 'z', 'x_err', 'y_err', 'z_err', 'p_x_y', 'p_x_z', 'p_y_z',
					 'Rcyl', 'phi', 'Rcyl_err', 'phi_err', 'p_Rcyl_phi', 'p_Rcyl_z', 'p_phi_z',
					 'r', 'theta', 'r_err', 'theta_err', 'p_r_theta', 'p_r_phi', 'p_theta_phi',
					 'pmra', 'pmdec', 'pmra_err', 'pmdec_err', 'p_pmra_pmdec',
					 'pmra_c', 'pmdec_c', 'pmra_c_err', 'pmdec_c_err', 'p_pmra_c_pmdec',
					 'pml', 'pmb', 'pml_err', 'pmb_err', 'p_pml_pmb',
					 'pml_c', 'pmb_c', 'pml_c_err', 'pmb_c_err', 'p_pml_c_pmb_c',
					 'Vl', 'Vb', 'Vl_err', 'Vb_err', 'p_Vl_Vb',
					 'Vl_c', 'Vb_c', 'Vl_c_err', 'Vb_c_err', 'p_Vl_c_Vb_c',
					 'dsun', 'Vtan_c', 'dsun_err', 'Vtan_c_err', 'p_dsun_Vtan_c',
					 'l', 'b', 'ra', 'dec', 'gc', 'source_id', 'id')

	_K = COST._K
	cts = cartesian_to_spherical
	stc = spherical_to_cartesian

	id, ra, dec, l, b, pmra, pmdec, pmra_err, pmdec_err, cov_pmra_pmdec, gc, distance, distance_error, internal_id = property_list
	onesl   = np.ones(N) #list of ones
	ral     = onesl*ra
	decl    = onesl*dec
	ll      = onesl*l
	bl      = onesl*b
	ll = np.radians(ll)
	bl = np.radians(bl)

	cov_pm               =  pmra_err*pmdec_err*cov_pmra_pmdec
	cov_matrix           =  [ [pmra_err**2, cov_pm],  [cov_pm, pmdec_err**2] ]
	pmral, pmdecl        =  np.random.multivariate_normal( [pmra, pmdec], cov_matrix, N).T
	pmll, pmbl		     =  co.pmrapmdec_to_pmllpmbb(pmral, pmdecl, ral, decl, degree=True).T

	#Check  if we have to use distance (priority) or gc and Mg
	if distance is not None and distance_error is not None:
		Dsunl  = np.random.normal(distance, distance_error,N)
	elif distance is not None:
		Dsunl  = np.repeat(distance, N)
	elif gc is not None:
		Mgl = np.random.normal(Mg, Mg_err, N)
		Dsunl = m_to_dist(gc, Mgl)
	else:
		raise ValueError("distance and gc cannot be both None")
	#

	#Rsun
	if Rsun_err is None: Rsunl = onesl*Rsun
	else: Rsunl = np.random.normal(Rsun, Rsun_err, N)


	#Vsun
	Vsunl = _make_Vsunl(U, V, W, U_err, V_err, W_err, Vlsr, Vlsr_err, N)

	# LHS COORD
	xsl    =  Dsunl * np.cos(ll) * np.cos(bl)
	yl     =  Dsunl * np.sin(ll) * np.cos(bl)
	zl     =  Dsunl * np.sin(bl)
	xl     =  Rsunl - xsl
	Rl     =  np.sqrt(xl * xl + yl * yl)
	phil   =  np.arctan2(yl, xl)
	rl     =  np.sqrt( Rl*Rl + zl*zl )
	thetal =  np.arcsin(zl/rl)


	#pml pmb corr
	Vl_nocorr =  _K*Dsunl*pmll
	Vb_nocorr =  _K*Dsunl*pmbl

	vxsl_corr, vysl_corr, vzsl_corr  = stc(np.zeros_like(Vl_nocorr), Vl_nocorr, Vb_nocorr, ll, bl, true_theta=False, degree=False)
	vxl_corr, vyl_corr, vzl_corr     = -(vxsl_corr + Vsunl[:,0]), vysl_corr + Vsunl[:, 1], vzsl_corr + Vsunl[:, 2]
	_, Vbl, Vll                      = cts(-vxl_corr, vyl_corr, vzl_corr, ll, bl, true_theta=False, degree=False)
	pmbl_corr, pmll_corr             = Vbl/(_K*Dsunl), Vll/(_K*Dsunl)
	#pmral_corr, pmdecl_corr			 = co.pmllpmbb_to_pmrapmdec(pmll=pmll_corr, pmbb=pmbl_corr, l=ll, b=bl, degree=True).T
	#A		 = co.pmllpmbb_to_pmrapmdec(pmll=pmll_corr, pmbb=pmbl_corr, l=ll, b=bl, degree=True).T
	Vtanl                            = np.sqrt(Vll*Vll + Vbl*Vbl)

	#Estiamte Std, Cov
	Mean_cart, Std_cart, rho_cart = calc_covariance( xl, yl, zl)
	Mean_cyl, Std_cyl, rho_cyl    = calc_covariance( Rl, np.degrees(phil), zl)
	Mean_sph, Std_sph, rho_sph    = calc_covariance( rl, np.degrees(thetal), np.degrees(phil))
	Mean_sky, Std_sky, rho_sky    = calc_covariance(pmll, pmbl)
	Mean_sky_corr, Std_sky_corr, rho_sky_corr = calc_covariance(pmll_corr, pmbl_corr)
	Mean_skyp, Std_skyp, rho_skyp = calc_covariance(Vl_nocorr, Vb_nocorr)
	Mean_skyp_corr, Std_skyp_corr, rho_skyp_corr = calc_covariance(Vll, Vbl)
	Mean_sky_tan, Std_sky_tan, rho_Sky_tan = calc_covariance(Dsunl, Vtanl)
	_,_, rho_Dsun_Vlc = calc_covariance(Dsunl, Vll)
	_,_, rho_Dsun_Vbc = calc_covariance(Dsunl, Vbl)

	#pmra pmdec corr
	pmral_corr, pmdecl_corr = co.pmllpmbb_to_pmrapmdec(pmll_corr,pmbl_corr, ll, bl, degree=False, epoch=2000.0).T
	Mean_skyeq_corr, Std_skyeq_corr, rho_skyeq_corr = calc_covariance(pmral_corr, pmdecl_corr)

	out_array = np.zeros(65)
	#Cartesian
	out_array[0:3] = Mean_cart[:3] #x,y,z
	out_array[3:6] = Std_cart[:3] #err on x,y,z
	out_array[6:9] = rho_cart[:3] #cov on x,y,z
	#Cylindrical
	out_array[9:11] = Mean_cyl[:2] #R, phi
	out_array[11:13] = Std_cyl[:2] #err on Rphi
	out_array[13:16] = rho_cyl[:3]
	#Spherical
	out_array[16:18] = Mean_sph[:2] #r, theta
	out_array[18:20] = Std_sph[:2] #err on r, theta
	out_array[20:23] = rho_sph[:3]

	#PMRA
	out_array[23] = pmra
	out_array[24] = pmdec
	out_array[25] = pmra_err
	out_array[26] = pmdec_err
	out_array[27] = cov_pmra_pmdec
	out_array[28:30] = Mean_skyeq_corr #r, theta
	out_array[30:32] = Std_skyeq_corr #err on r, theta
	out_array[32] = rho_skyeq_corr[0]

	#PML
	out_array[33:35] = Mean_sky #r, theta
	out_array[35:37] = Std_sky #err on r, theta
	out_array[37] = rho_sky[0]
	out_array[38:40] = Mean_sky_corr #r, theta
	out_array[40:42] = Std_sky_corr #err on r, theta
	out_array[42] = rho_sky_corr[0]

	#V
	out_array[43:45] = Mean_skyp #r, theta
	out_array[45:47] = Std_skyp #err on r, theta
	out_array[47] = rho_skyp[0]
	out_array[48:50] = Mean_skyp_corr #r, theta
	out_array[50:52] = Std_skyp_corr #err on r, theta
	out_array[52] = rho_skyp_corr[0]
	out_array[53:55] = Mean_sky_tan #r, theta
	out_array[55:57] = Std_sky_tan#err on r, theta
	out_array[57] = rho_Sky_tan[0]

	#AUX
	out_array[58] 	 = l
	out_array[59] 	 = b
	out_array[60] 	 = ra
	out_array[61] 	 = dec
	out_array[62] 	 = gc


	#CHECK ID
	if id is None and internal_id is None: id = internal_id = ut.create_long_index()
	elif internal_id is None: internal_id = id
	elif id is None: id = internal_id


	out_array[63] 	 = int(id)
	out_array[64] 	 = internal_id



	return out_array, dict(zip(_key_list_obs, out_array))
def orbit_N(method, req_dict, opt_dict, calc_dict):

    count_orbits = 0.

    orbit_params = {
        'Energy': [],
        'L_z': [],
        'L_p': [],
        'I_3': [],
        'Z_max': [],
        'ecc': [],
        'r_apo': [],
        'r_peri': [],
        'R_apo_P': [],
        'R_peri_P': []
    }

    orbit_unc = {
        'Energy_unc': -9.999,
        'L_z_unc': -9.999,
        'L_p_unc': -9.999,
        'I_3_unc': -9.999,
        'Z_max_unc': -9.999,
        'ecc_unc': -9.999,
        'r_apo_unc': -9.999,
        'r_peri_unc': -9.999,
        'R_apo_P_unc': -9.999,
        'R_peri_P_unc': -9.999,
        'N_orbits': -9.999
    }

    #loop over N orbits
    for i in range(0, N_orbits):

        #generate input parameters by randomly selecting from a normal distribution with spread = input uncertainty
        rand_dict = collections.OrderedDict([('name', req_dict['name']),
                                             ('ra', -9.999), ('dec', -9.999),
                                             ('pmra', -9.999),
                                             ('pmdec', -9.999), ('l', -9.999),
                                             ('b', -9.999), ('pml', -9.999),
                                             ('pmb', -9.999), ('rv', -9.999),
                                             ('dist', -9.999)])

        plx = 1. / req_dict['dist']
        eplx = (plx**2.) * opt_dict['edist']
        mean = [
            req_dict['ra'], req_dict['dec'], plx, req_dict['pmra'],
            req_dict['pmdec'], req_dict['rv']
        ]

        cov_11 = opt_dict['era']**2.
        cov_12 = opt_dict['c_radec'] * opt_dict['era'] * opt_dict['edec']
        cov_13 = opt_dict['c_raplx'] * opt_dict['era'] * eplx
        cov_14 = opt_dict['c_rapmra'] * opt_dict['era'] * opt_dict['epmra']
        cov_15 = opt_dict['c_rapmdec'] * opt_dict['era'] * opt_dict['epmdec']
        cov_16 = opt_dict['c_rarv'] * opt_dict['era'] * opt_dict['erv']

        row1 = [cov_11, cov_12, cov_13, cov_14, cov_15, cov_16]

        cov_21 = opt_dict['c_radec'] * opt_dict['era'] * opt_dict['edec']
        cov_22 = opt_dict['edec']**2.
        cov_23 = opt_dict['c_decplx'] * opt_dict['edec'] * eplx
        cov_24 = opt_dict['c_decpmra'] * opt_dict['edec'] * opt_dict['epmra']
        cov_25 = opt_dict['c_decpmdec'] * opt_dict['edec'] * opt_dict['epmdec']
        cov_26 = opt_dict['c_decrv'] * opt_dict['edec'] * opt_dict['erv']

        row2 = [cov_21, cov_22, cov_23, cov_24, cov_25, cov_26]

        cov_31 = opt_dict['c_raplx'] * opt_dict['era'] * eplx
        cov_32 = opt_dict['c_decplx'] * opt_dict['edec'] * eplx
        cov_33 = eplx**2.
        cov_34 = opt_dict['c_plxpmra'] * eplx * opt_dict['epmra']
        cov_35 = opt_dict['c_plxpmdec'] * eplx * opt_dict['epmdec']
        cov_36 = opt_dict['c_plxrv'] * eplx * opt_dict['erv']

        row3 = [cov_31, cov_32, cov_33, cov_34, cov_35, cov_36]

        cov_41 = opt_dict['c_rapmra'] * opt_dict['era'] * opt_dict['epmra']
        cov_42 = opt_dict['c_decpmra'] * opt_dict['edec'] * opt_dict['epmdec']
        cov_43 = opt_dict['c_plxpmra'] * eplx * opt_dict['epmra']
        cov_44 = opt_dict['epmra']**2.
        cov_45 = opt_dict['c_pmrapmdec'] * opt_dict['epmra'] * opt_dict[
            'epmdec']
        cov_46 = opt_dict['c_pmrarv'] * opt_dict['epmra'] * opt_dict['erv']

        row4 = [cov_41, cov_42, cov_43, cov_44, cov_45, cov_46]

        cov_51 = opt_dict['c_rapmdec'] * opt_dict['era'] * opt_dict['epmdec']
        cov_52 = opt_dict['c_decpmdec'] * opt_dict['edec'] * opt_dict['epmdec']
        cov_53 = opt_dict['c_plxpmdec'] * eplx * opt_dict['epmdec']
        cov_54 = opt_dict['c_pmrapmdec'] * opt_dict['epmra'] * opt_dict[
            'epmdec']
        cov_55 = opt_dict['epmdec']**2.
        cov_56 = opt_dict['c_pmdecrv'] * opt_dict['epmdec'] * opt_dict['erv']

        row5 = [cov_51, cov_52, cov_53, cov_54, cov_55, cov_56]

        cov_61 = opt_dict['c_rarv'] * opt_dict['era'] * opt_dict['erv']
        cov_62 = opt_dict['c_decrv'] * opt_dict['edec'] * opt_dict['erv']
        cov_63 = opt_dict['c_plxrv'] * eplx * opt_dict['erv']
        cov_64 = opt_dict['c_pmrarv'] * opt_dict['epmra'] * opt_dict['erv']
        cov_65 = opt_dict['c_pmdecrv'] * opt_dict['epmdec'] * opt_dict['erv']
        cov_66 = opt_dict['erv']**2.

        row6 = [cov_61, cov_62, cov_63, cov_64, cov_65, cov_66]

        covar = np.array([row1, row2, row3, row4, row5, row6])

        rand_result = np.random.multivariate_normal(mean, covar)

        rand_dict['ra'] = rand_result[0]
        rand_dict['dec'] = rand_result[1]
        rand_dict['dist'] = 1. / rand_result[2]
        rand_dict['pmra'] = rand_result[3]
        rand_dict['pmdec'] = rand_result[4]
        rand_dict['rv'] = rand_result[5]

        rand_dict['pml'], rand_dict['pmb'] = b_c.pmrapmdec_to_pmllpmbb(
            rand_dict['pmra'],
            rand_dict['pmdec'],
            rand_dict['ra'],
            rand_dict['dec'],
            degree=True,
            epoch=2000.0)

        #use randomly generated input parameters to calculate positions, velocities
        rand_x, rand_y, rand_z = xyz(rand_dict)
        rand_u, rand_v, rand_w, rand_vx_gc, rand_vy_gc, rand_vz_gc = uvw(
            rand_dict)

        rand_calc_dict = collections.OrderedDict([('x_gc', rand_x),
                                                  ('y_gc', rand_y),
                                                  ('z_gc', rand_z),
                                                  ('u', rand_u), ('v', rand_v),
                                                  ('w', rand_w),
                                                  ('vx_gc', rand_vx_gc),
                                                  ('vy_gc', rand_vy_gc),
                                                  ('vz_gc', rand_vz_gc),
                                                  ('R', -9.999),
                                                  ('phi', -9.999),
                                                  ('vRg', -9.999),
                                                  ('vTg', -9.999)])

        rand_calc_dict['R'], rand_calc_dict['phi'] = cyl_coords(rand_calc_dict)
        rand_calc_dict['vRg'], rand_calc_dict['vTg'] = cylindrical_vs(
            rand_calc_dict)

        returned_orb = orbit_1_staeckel(rand_dict, rand_calc_dict)

        for key in returned_orb:
            if (returned_orb['bound'] == True and key in orbit_params):
                orbit_params[key].append(returned_orb[key])

        if (returned_orb['bound'] == True):
            count_orbits += 1.

    orbit_unc['N_orbits'] = count_orbits

    fits_dir_name = "{}{}_uncertainty_fits".format(final_dir_name, method)

    if not os.path.exists(fits_dir_name):
        os.makedirs(fits_dir_name)

    fits_dir_name = fits_dir_name + "/"

    f, axarr = plt.subplots(5, 2, figsize=(12, 10))

    row_num = 0
    col_num = 0

    for key in orbit_params:

        data = orbit_params[key]

        #fit a gaussian to the randomly generated orbital parameters
        mu, std = norm.fit(data)

        #uncertainty on parameter = standard dev of fit
        orbit_unc['{}_unc'.format(key)] = std

        #plot the histogram
        axarr[row_num, col_num].hist(data,
                                     bins=25,
                                     normed=True,
                                     facecolor='none',
                                     edgecolor="black",
                                     histtype="step")

        #plot the fit
        xmin, xmax = axarr[row_num, col_num].get_xlim()
        x = np.linspace(xmin, xmax, 100)
        p = norm.pdf(x, mu, std)
        axarr[row_num, col_num].plot(x, p, 'k', linewidth=2)

        #plot the calculated parameter value
        axarr[row_num, col_num].axvline(x=calc_dict[key],
                                        color='b',
                                        ls='--',
                                        lw=2)

        #plot the mean of the fit
        axarr[row_num, col_num].axvline(x=mu, color='k', lw=2)

        axarr[row_num, col_num].set_title("{} (mu = {}, std = {})".format(
            key, round(mu, 2), round(std, 2)))

        if (col_num == 0):
            col_num += 1
        else:
            col_num = 0
            row_num += 1

    plt.tight_layout()
    plt.savefig("{}{}_{}_{}_fits.png".format(fits_dir_name, main_title,
                                             req_dict['name'], method))
    plt.close()

    return orbit_unc