Hubble Source Catalog SWEEPS Proper Motion Notebook

August 2019, Steve Lubow and Rick White

A new MAST interface supports queries to the current and previous versions of the Hubble Source Catalog. It allows searches of the summary table (with multi-filter mean photometry) and the detailed table (with all the multi-epoch measurements). It also has an associated API, which is used in this notebook.

The web-based user interface to the HSC does not currently include access to the new proper motions available for the SWEEPS field in version 3.1 of the Hubble Source Catalog. However those tables are accessible via the API. This notebook shows how to use them.

This notebook is similar to the previously released version that uses CasJobs rather than the new API. The Casjobs interface is definitely more powerful and flexible, but the API is simpler to use for users who are not already experienced Casjobs users. Currently the API does not include easy access to the colors and magnitudes of the SWEEPS objects, but they will be added soon.

Additional information is available on the SWEEPS Proper Motions help page.

This notebook is available for download. A simpler notebook that provides a quick introduction to the HSC API is also available. Another simple notebook generates a color-magnitude diagram for the Small Magellanic Cloud in only a couple of minutes.

Instructions:

  • Complete the initialization steps described below.
  • Run the notebook to completion.
  • Modify and rerun any sections of the Table of Contents below.

Running the notebook from top to bottom takes about 4 minutes (depending on the speed of your computer).

Table of Contents

Initialization

Install Python modules

This notebook requires the use of Python 3.

This needs some special modules in addition to the common requirements of astropy, numpy and scipy. For anaconda versions of Python the installation commands are:

conda install requests
conda install pillow
pip install fastkde

Other optional configuration

If desired, you can set resPath, the output directory, in the next code block (the default location is the current working directory, which is probably the same directory as this script).

In [1]:
resPath="./" # directory where generated plots are saved

%matplotlib inline
import astropy, pylab, time, sys, os, requests, json
import numpy as np
from matplotlib.colors import LogNorm

## For handling ordinary astropy Tables
from astropy.table import Table
from astropy.io import fits, ascii

from PIL import Image
from io import BytesIO

from fastkde import fastKDE
from scipy.interpolate import RectBivariateSpline
from astropy.modeling import models, fitting

# There are a number of relatively unimportant warnings that 
# show up, so for now, suppress them:
import warnings
warnings.filterwarnings("ignore")

# Set page width to fill browser for longer output lines
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
# set width for pprint
astropy.conf.max_width = 150

Useful functions

Execute HSC searches and resolve names using MAST query.

In [2]:
hscapiurl = "https://catalogs.mast.stsci.edu/api/v0.1/hsc"

def hsccone(ra,dec,radius,table="summary",release="v3",format="csv",magtype="magaper2",
            columns=None, baseurl=hscapiurl, verbose=False,
            **kw):
    """Do a cone search of the HSC catalog
    
    Parameters
    ----------
    ra (float): (degrees) J2000 Right Ascension
    dec (float): (degrees) J2000 Declination
    radius (float): (degrees) Search radius (<= 0.5 degrees)
    table (string): summary, detailed, propermotions, or sourcepositions
    release (string): v3 or v2
    magtype (string): magaper2 or magauto (only applies to summary table)
    format: csv, votable, json
    columns: list of column names to include (None means use defaults)
    baseurl: base URL for the request
    verbose: print info about request
    **kw: other parameters (e.g., 'numimages.gte':2)
    """
    
    data = kw.copy()
    data['ra'] = ra
    data['dec'] = dec
    data['radius'] = radius
    return hscsearch(table=table,release=release,format=format,magtype=magtype,
                     columns=columns,baseurl=baseurl,verbose=verbose,**data)


def hscsearch(table="summary",release="v3",magtype="magaper2",format="csv",
              columns=None, baseurl=hscapiurl, verbose=False,
           **kw):
    """Do a general search of the HSC catalog (possibly without ra/dec/radius)
    
    Parameters
    ----------
    table (string): summary, detailed, propermotions, or sourcepositions
    release (string): v3 or v2
    magtype (string): magaper2 or magauto (only applies to summary table)
    format: csv, votable, json
    columns: list of column names to include (None means use defaults)
    baseurl: base URL for the request
    verbose: print info about request
    **kw: other parameters (e.g., 'numimages.gte':2).  Note this is required!
    """
    
    data = kw.copy()
    if not data:
        raise ValueError("You must specify some parameters for search")
    if format not in ("csv","votable","json"):
        raise ValueError("Bad value for format")
    url = "{}.{}".format(cat2url(table,release,magtype,baseurl=baseurl),format)
    if columns:
        # check that column values are legal
        # create a dictionary to speed this up
        dcols = {}
        for col in hscmetadata(table,release,magtype)['name']:
            dcols[col.lower()] = 1
        badcols = []
        for col in columns:
            if col.lower().strip() not in dcols:
                badcols.append(col)
        if badcols:
            raise ValueError('Some columns not found in table: {}'.format(', '.join(badcols)))
        # two different ways to specify a list of column values in the API
        # data['columns'] = columns
        data['columns'] = '[{}]'.format(','.join(columns))

    # either get or post works
    # r = requests.post(url, data=data)
    r = requests.get(url, params=data)

    if verbose:
        print(r.url)
    r.raise_for_status()
    if format == "json":
        return r.json()
    else:
        return r.text


def hscmetadata(table="summary",release="v3",magtype="magaper2",baseurl=hscapiurl):
    """Return metadata for the specified catalog and table
    
    Parameters
    ----------
    table (string): summary, detailed, propermotions, or sourcepositions
    release (string): v3 or v2
    magtype (string): magaper2 or magauto (only applies to summary table)
    baseurl: base URL for the request
    
    Returns an astropy table with columns name, type, description
    """
    url = "{}/metadata".format(cat2url(table,release,magtype,baseurl=baseurl))
    r = requests.get(url)
    r.raise_for_status()
    v = r.json()
    # convert to astropy table
    tab = Table(rows=[(x['name'],x['type'],x['description']) for x in v],
               names=('name','type','description'))
    return tab


def cat2url(table="summary",release="v3",magtype="magaper2",baseurl=hscapiurl):
    """Return URL for the specified catalog and table
    
    Parameters
    ----------
    table (string): summary, detailed, propermotions, or sourcepositions
    release (string): v3 or v2
    magtype (string): magaper2 or magauto (only applies to summary table)
    baseurl: base URL for the request
    
    Returns a string with the base URL for this request
    """
    checklegal(table,release,magtype)
    if table == "summary":
        url = "{baseurl}/{release}/{table}/{magtype}".format(**locals())
    else:
        url = "{baseurl}/{release}/{table}".format(**locals())
    return url


def checklegal(table,release,magtype):
    """Checks if this combination of table, release and magtype is acceptable
    
    Raises a ValueError exception if there is problem
    """
    
    releaselist = ("v2", "v3")
    if release not in releaselist:
        raise ValueError("Bad value for release (must be one of {})".format(
            ', '.join(releaselist)))
    if release=="v2":
        tablelist = ("summary", "detailed")
    else:
        tablelist = ("summary", "detailed", "propermotions", "sourcepositions")
    if table not in tablelist:
        raise ValueError("Bad value for table (for {} must be one of {})".format(
            release, ", ".join(tablelist)))
    if table == "summary":
        magtypelist = ("magaper2", "magauto")
        if magtype not in magtypelist:
            raise ValueError("Bad value for magtype (must be one of {})".format(
                ", ".join(magtypelist)))


def mastQuery(request, url='https://mast.stsci.edu/api/v0/invoke'):
    """Perform a MAST query.

    Parameters
    ----------
    request (dictionary): The MAST request json object
    url (string): The service URL

    Returns the returned data content
    """
    
    # Encoding the request as a json string
    requestString = json.dumps(request)
    r = requests.post(url, data={'request': requestString})
    r.raise_for_status()
    return r.text


def resolve(name):
    """Get the RA and Dec for an object using the MAST name resolver
    
    Parameters
    ----------
    name (str): Name of object

    Returns RA, Dec tuple with position
    """

    resolverRequest = {'service':'Mast.Name.Lookup',
                       'params':{'input':name,
                                 'format':'json'
                                },
                      }
    resolvedObjectString = mastQuery(resolverRequest)
    resolvedObject = json.loads(resolvedObjectString)
    # The resolver returns a variety of information about the resolved object, 
    # however for our purposes all we need are the RA and Dec
    try:
        objRa = resolvedObject['resolvedCoordinate'][0]['ra']
        objDec = resolvedObject['resolvedCoordinate'][0]['decl']
    except IndexError as e:
        raise ValueError("Unknown object '{}'".format(name))
    return (objRa, objDec)

Use metadata query to get information on available columns

This query works for any of the tables in the API (summary, detailed, propermotions, sourcepositions).

In [3]:
meta = hscmetadata("propermotions")
print(' '.join(meta['name']))
meta[:5]
objID numSources raMean decMean lonMean latMean raMeanErr decMeanErr lonMeanErr latMeanErr pmRA pmDec pmLon pmLat pmRAErr pmDecErr pmLonErr pmLatErr pmRADev pmDecDev pmLonDev pmLatDev epochStart epochEnd epochMean DSigma NumFilters NumVisits NumImages CI CI_Sigma KronRadius KronRadius_Sigma HTMID X Y Z
Out[3]:
Table length=5
nametypedescription
str16str5str31
objIDlongobjID_descriptionTBD
numSourcesintnumSources_descriptionTBD
raMeanfloatraMean_descriptionTBD
decMeanfloatdecMean_descriptionTBD
lonMeanfloatlonMean_descriptionTBD

Retrieve data on selected SWEEPS objects

This makes a single large request to the HSC search interface to the get the contents of the proper motions table. Despite its large size (460K rows), the query is relatively efficient: it takes about 25 seconds to retrieve the results from the server, and then another 20 seconds to convert it to an astropy table. The speed of the table conversion will depend on your computer.

Note that the query restricts the sample to objects with at least 20 images total spread over at least 10 different visits. These constraints can be modified depending on your science goals.

In [4]:
columns = """ObjID,raMean,decMean,raMeanErr,decMeanErr,NumFilters,NumVisits,
    pmLat,pmLon,pmLatErr,pmLonErr,pmLatDev,pmLonDev,epochMean,epochStart,epochEnd""".split(",")
columns = [x.strip() for x in columns]
columns = [x for x in columns if x and not x.startswith('#')]

# missing -- impossible with current data I think
# MagMed, n, MagMAD

constraints = {'NumFilters.gt':1, 'NumVisits.gt':9, 'NumImages.gt':19}

# note the pagesize parameter, which allows retrieving very large results
# the default pagesize is 50000 rows

t0 = time.time()
results = hscsearch(table="propermotions",release='v3',columns=columns,verbose=True,
                    pagesize=500000, **constraints)
print("{:.1f} s: retrieved data".format(time.time()-t0))
tab = ascii.read(results)
print("{:.1f} s: converted to astropy table".format(time.time()-t0))

# change some column names for consistency with the Casjobs version of this notebook

tab.rename_column("raMean","RA")
tab.rename_column("decMean","Dec")
tab.rename_column("raMeanErr","RAerr")
tab.rename_column("decMeanErr","Decerr")
tab.rename_column("pmLat","bpm")
tab.rename_column("pmLon","lpm")
tab.rename_column("pmLatErr","bpmerr")
tab.rename_column("pmLonErr","lpmerr")

# compute some additional columns

tab['pmdev'] = np.sqrt(tab['pmLonDev']**2+tab['pmLatDev']**2)
tab['yr'] = (tab['epochMean'] - 47892)/365.25+1990
tab['dT'] = (tab['epochEnd']-tab['epochStart'])/365.25
tab['yrStart'] = (tab['epochStart'] - 47892)/365.25+1990
tab['yrEnd'] = (tab['epochEnd'] - 47892)/365.25+1990

# delete some columns that are not needed after the computations
del tab['pmLonDev'], tab['pmLatDev'], tab['epochEnd'], tab['epochStart'], tab['epochMean']

tab
https://catalogs.mast.stsci.edu/api/v0.1/hsc/v3/propermotions.csv?pagesize=500000&NumFilters.gt=1&NumVisits.gt=9&NumImages.gt=19&columns=%5BObjID%2CraMean%2CdecMean%2CraMeanErr%2CdecMeanErr%2CNumFilters%2CNumVisits%2CpmLat%2CpmLon%2CpmLatErr%2CpmLonErr%2CpmLatDev%2CpmLonDev%2CepochMean%2CepochStart%2CepochEnd%5D
8.5 s: retrieved data
31.9 s: converted to astropy table
Out[4]:
Table length=462899
ObjIDRADecRAerrDecerrNumFiltersNumVisitsbpmlpmbpmerrlpmerrpmdevyrdTyrStartyrEnd
int64float64float64float64float64int64int64float64float64float64float64float64float64float64float64float64
4000709005181269.8113107222242-29.204806019512870.74664632085862210.713755538697197247-5.10451382895036-5.897651161181470.347962883210794770.50989617148757855.3057338872972592013.300790214705811.3719145413717642003.43617966200852014.8080942033803
4000709005182269.8128664828206-29.2047034365090430.223672197706013760.28809884080897222473.890337432221249-9.3598876674632270.10234543233440.192456589980468721.44100899183166462013.300790214705811.3719145413717642003.43617966200852014.8080942033803
4000709005183269.81521901829785-29.2048052415336270.339391002606176330.53947023825358012471.1397477459634553-6.2991594217296230.224282814894976980.30786989717227893.00198471798003472013.300790214705811.3719145413717642003.43617966200852014.8080942033803
4000709005184269.8135840655936-29.204842248440161.0461553060777761.1099588261791542462.0066237101659845-6.874022123643060.67109846328281940.61427197443955187.7809049145194692013.333387568191511.3719145413717642003.43617966200852014.8080942033803
4000709005185269.8318125502752-29.2048348341951.65056265004839921.2957974426933525233-2.1191635793690704-11.2560395568691941.96037988086102581.8818220429009058.5499750591837762013.63360876534332.43497233745794352012.34333598006242014.7783083175204
4000709005186269.8387558085273-29.204831947871051.11090941019083121.7268787328882316244-0.9736207737749203-7.7285633872153421.231230889680542.15092910909312310.9873296254461082013.52193197667933.00678224901781552011.80131195436252014.8080942033803
4000709005187269.8153494657498-29.204659305828532.496822550820805313.873605184546227239-1.367980570643435-5.53782082954569253.90303141560503877.096755495151362575.70274369198912013.203824817868711.3719145413717642003.43617966200852014.8080942033803
4000709005188269.78670608428894-29.2046567902152850.80633109771264430.6816889134899603248-5.029307843964451-6.4559659723793380.52767557488788630.358008082724935064.6567013217125952013.30364083087211.3719145413717642003.43617966200852014.8080942033803
4000709005189269.8415079333277-29.204631353889530.278439516610413330.86739839270015422463.7504756809405118-2.67243408839153760.409036257611970161.0097374279552684.233003119641192013.51523827019923.00678224901781552011.80131195436252014.8080942033803
4000709005190269.78980540363534-29.204718019407630.25060381088670540.199759118135745762471.4369682993728530.083250994926651940.137366998664166050.133467994441809821.64062348142068952013.300790214705811.3719145413717642003.43617966200852014.8080942033803
................................................
4000805716072269.75349922919384-29.187237529076072.2254418869586370.78231751896833511170.7682489073323565-9.0949330733718361.05212127024130320.58956535179791294.372476489720292014.41097063681635.2193083778612732010.3563552740772015.5756636519384
4000805716573269.74337423557154-29.1874173481739062.20572216306230871.9380676063030071313-1.7246226034552836-3.7722862082888041.96667917456569620.90778578216902727.2753008155219082013.32700904360675.2167722216139892010.3563552740772015.573127495691
4000805716797269.75311840756876-29.18752544424582.92716674026236930.948254900319411612181.3713042559931965-6.35401881431313151.264428873827180.51319274948506746.9825817041300642013.89981919479175.2193083778612732010.3563552740772015.5756636519384
4000805718205269.7531594948824-29.188150175143150.54941083853518020.615524496808399211162.5304788303426915-12.5400479254640760.377725333826746670.293745758212878852.35473335465282572014.66438409698735.2180337886148162010.35762986332342015.5756636519384
4000805718227269.75302524494737-29.1880809875080730.25675677292085050.643063656703462711170.813067868041367-3.614029759122210.16879165307967680.31118324715860581.82942922585144442014.41097063681635.2193083778612732010.3563552740772015.5756636519384
4000805721823269.7695982219451-29.1758039219665731.24645160909261941.51634588072404462111.1412990011218498-5.4282143948170460.8744719015587020.42598681746083214.6211444699378432012.32489948071687.503425723534592006.18598796677132013.6894136903059
4000805722552269.7505635379468-29.1761243016462081.35085672718061441.8500679483476012111.9211167391693023-4.8437339694426460.256477947695195050.5599654489933696.0953791834192532006.10895259209610.2000379164898882004.14670268496322014.346740601453
4000805728345269.7539224541331-29.1787945213682570.427358956693027871.51044321579428649120.7590503605846199-4.1544996103492871.50071012469702511.9088781853844474.10387188489649552015.23421650524282.13804386125738552013.43761979068092015.5756636519384
4000805728821269.7537554562008-29.1789000304996760.65154936911499031.447739611457231812-2.453791229451978-4.1504567628372461.44546630423519321.98532392074928393.9127426590711662015.23421650524282.13804386125738552013.43761979068092015.5756636519384
4000805729371269.7539432727861-29.179239214021820.94287078492880952.22706593052826161221-1.3667461636569604-6.2115490039554270.73962466194043911.33163863362235347.6443710459311192015.64602457051967.1502336912701122010.38102520019222017.5312588914621

Properties of Full Catalog

Sky Coverage

In [5]:
x = tab['RA']
y = tab['Dec']

pylab.rcParams.update({'font.size': 16})
pylab.figure(1,(12,10))
pylab.scatter(x, y, s=1)
pylab.autoscale(tight=True)
pylab.xlabel('RA')
pylab.ylabel('Dec')
dc=0.01
pylab.xlim(min(x)-dc, max(x)+dc)
pylab.ylim(min(y)-dc, max(y)+dc)
pylab.gca().invert_xaxis()
pylab.text(0.5,0.93,'{:,} stars in SWEEPS'.format(len(x)),
       horizontalalignment='left',
       transform=pylab.gca().transAxes)
Out[5]:
Text(0.5, 0.93, '462,899 stars in SWEEPS')

Proper Motion Histograms

Proper motion histograms for lon and lat

In [6]:
bin = 0.2
hrange = (-20,20)
bincount = int((hrange[1]-hrange[0])/bin + 0.5) + 1
pylab.rcParams.update({'font.size': 16})
pylab.figure(1,(12,10))
pylab.hist(tab['lpm'], range=hrange, bins=bincount, label='Longitude', 
           histtype='step', linewidth=2)
pylab.hist(tab['bpm'], range=hrange, bins=bincount, label='Latitude', 
           histtype='step', linewidth=2)
pylab.xlabel('Proper motion [mas/yr]')
pylab.ylabel('Number [in {:.2} mas bins]'.format(bin))
pylab.legend(loc='upper right')
pylab.autoscale(enable=True, axis='x', tight=True)
pylab.ylim(0,13500)
pylab.title('{:,} stars in SWEEPS'.format(len(tab)))
pylab.tight_layout()
pylab.savefig('{}sweeps_api_pmerr_hist.png'.format(resPath))

Proper motion error cumulative histogram for lon and lat

In [7]:
bin = 0.01
hrange = (0,2)
bincount = int((hrange[1]-hrange[0])/bin + 0.5) + 1
pylab.rcParams.update({'font.size': 16})
pylab.figure(1,(12,10))
pylab.hist(tab['lpmerr'], range=hrange, bins=bincount, label='Longitude Error', 
           histtype='step', cumulative=1, linewidth=2)
pylab.hist(tab['bpmerr'], range=hrange, bins=bincount, label='Latitude Error', 
           histtype='step', cumulative=1, linewidth=2)
pylab.xlabel('Proper motion error [mas/yr]')
pylab.ylabel('Cumulative number [in {:0.2} mas bins]'.format(bin))
pylab.legend(loc='upper right')
pylab.autoscale(enable=True, axis='x', tight=True)
pylab.ylim(0,500000)
pylab.title('{:,} stars in SWEEPS'.format(len(tab)))
pylab.tight_layout()
pylab.savefig('{}sweeps_api_pmerr_cumhist.png'.format(resPath))

Proper motion error log histogram for lon and lat

In [8]:
bin = 0.01
hrange = (0,6)
bincount = int((hrange[1]-hrange[0])/bin + 0.5) + 1
pylab.rcParams.update({'font.size': 16})
pylab.figure(1,(12,10))
pylab.hist(tab['lpmerr'], range=hrange, bins=bincount, label='Longitude Error', 
           histtype='step', linewidth=2)
pylab.hist(tab['bpmerr'], range=hrange, bins=bincount, label='Latitude Error', 
           histtype='step', linewidth=2)
pylab.xlabel('Proper motion error [mas/yr]')
pylab.ylabel('Number [in {:0.2} mas bins]'.format(bin))
pylab.legend(loc='upper right')
pylab.yscale('log')
pylab.autoscale(enable=True, axis='x', tight=True)
pylab.ylim(0,15000)
pylab.title('{:,} stars in SWEEPS'.format(len(tab)))
pylab.tight_layout()
pylab.savefig('{}sweeps_api_pmerr_loghist.png'.format(resPath))

Proper motion error as a function of dT

Exclude objects with dT near zero, and to improve the plotting add a bit of random noise to spread out the quanitized time values.

In [9]:
# restrict to sources with dT > 1 year
dtmin = 1.0
w = np.where(tab['dT']>dtmin)[0]
if ('rw' not in locals()) or len(rw) != len(w):
    rw = np.random.random(len(w))
x = np.array(tab['dT'][w]) + 0.5*(rw-0.5)
y = np.log(np.array(tab['lpmerr'][w]))

# Calculate the point density
t0 = time.time()
myPDF,axes = fastKDE.pdf(x,y,numPoints=2**9+1)
print("kde took {:.1f} sec".format(time.time()-t0))

# interpolate to get z values at points
finterp = RectBivariateSpline(axes[1],axes[0],myPDF)
z = finterp(y,x,grid=False)

# Sort the points by density, so that the densest points are plotted last
idx = z.argsort()
xs, ys, zs = x[idx], y[idx], z[idx]

# select a random subset of points in the most crowded regions to speed up plotting
wran = np.where(np.random.random(len(zs))*zs<0.05)[0]
print("Plotting {} of {} points".format(len(wran),len(zs)))
xs = xs[wran]
ys = ys[wran]
zs = zs[wran]

pylab.rcParams.update({'font.size': 16})
pylab.figure(1,(12,10))
pylab.yscale('log')
pylab.scatter(xs, np.exp(ys), c=zs, s=2, edgecolor='', cmap='plasma', 
              label='Longitude PM error')
pylab.autoscale(tight=True, axis='y')
pylab.xlim(0.0, max(x)*1.05)
pylab.xlabel('Date range [yr]')
pylab.ylabel('Proper motion error [mas/yr]')
pylab.legend(loc='best')
pylab.title('{:,} stars in SWEEPS'.format(len(tab)))
pylab.colorbar()
pylab.tight_layout()
pylab.savefig('{}sweeps_api_pmerr_vs_dt.png'.format(resPath))
kde took 4.7 sec
Plotting 190265 of 461199 points

Proper motion error log histogram for lon and lat

Divide sample into points with $<6$ years of data and points with more than 6 years of data.

In [10]:
bin = 0.01
hrange = (0,6)
bincount = int((hrange[1]-hrange[0])/bin + 0.5) + 1

tsplit = 6

pylab.rcParams.update({'font.size': 16})
pylab.figure(1,(12,12))
pylab.subplot(211)
w = np.where(tab['dT']<=tsplit)[0]
pylab.hist(tab['lpmerr'][w], range=hrange, bins=bincount, label='Longitude Error', 
           histtype='step', linewidth=2)
pylab.hist(tab['bpmerr'][w], range=hrange, bins=bincount, label='Latitude Error', 
           histtype='step', linewidth=2)
pylab.xlabel('Proper motion error [mas/yr]')
pylab.ylabel('Number [in {:0.2} mas bins]'.format(bin))
pylab.legend(loc='upper right')
pylab.yscale('log')
pylab.autoscale(enable=True, axis='x', tight=True)
pylab.ylim(0,15000)
pylab.title('{:,} stars in SWEEPS with dT < {} yrs'.format(len(w),tsplit))
pylab.tight_layout()

pylab.subplot(212)
w = np.where(tab['dT']>tsplit)
pylab.hist(tab['lpmerr'][w], range=hrange, bins=bincount, label='Longitude Error', 
           histtype='step', linewidth=2)
pylab.hist(tab['bpmerr'][w], range=hrange, bins=bincount, label='Latitude Error', 
           histtype='step', linewidth=2)
pylab.xlabel('Proper motion error [mas/yr]')
pylab.ylabel('Number [in {:0.2} mas bins]'.format(bin))
pylab.legend(loc='upper right')
pylab.yscale('log')
pylab.autoscale(enable=True, axis='x', tight=True)
pylab.ylim(0,15000)
pylab.title('{:,} stars in SWEEPS with dT > {} yrs'.format(len(w),tsplit))
pylab.tight_layout()

pylab.savefig('{}sweeps_api_pmerr_loghist2.png'.format(resPath))