Setting Up the Database
To install, just do:
pip install BDNYCdb
Then download the bdnyc198.db database file. This initial release contains the astrometry, photometry and spectra for the 198 objects in the Filippazzo et al. (2015) sample.
To use the published and unpublished spectra, photometry and astrometry for all 1300 sources, ask someone who has access to the private database to share the BDNYCdb Dropbox folder with you.
Accessing the Database
Create a database instance by launching the Python interpreter and pointing the get_db() function to the database file by doing:
import BDdb
db = BDdb.get_db(filepath)
Voila! You can see an inventory of all data for a specific source by passing a source_id to the inventory() method.
db.inventory(767)
This will also plot all available spectra for that source for visual inspection if you set plot=True.
Querying the Database
Now that you have the database at your fingertips, you’ll want to get some info out of it. To do this, you can pass SQL queries wrapped in double-quotes (“) to the query() method.
data = db.query( "SQL_query_goes_here" )
Here is a detailed post about how to write a SQL query.
The result of a SQL query is a sequence of tuples with the data requested from each record. For example, we can get a source’s photometry from the PHOTOMETRY table with:
db.query("select band,magnitude from photometry where source_id=202")
which gives the output
[('J', 13.526),('H', 12.807),('Ks', 12.503),('W1', 12.486),('W2', 12.386),('W3', 12.313),('W4', 8.525)]
Alternatively, we can have this data returned as a Python dictionary with:
db.query("select band,magnitude from photometry where source_id=202", DICT=True)
which looks like
[{'band': 'J', 'magnitude': 13.526},{'band': 'H', 'magnitude': 12.807},{'band': 'Ks', 'magnitude': 12.503},{'band': 'W1', 'magnitude': 12.486},{'band': 'W2', 'magnitude': 12.386},{'band': 'W3', 'magnitude': 12.313},{'band': 'W4', 'magnitude': 8.525}]
Example Queries
Some SQL query examples to pass to the query() method (wrapped in double-quotes (“) of course) are:
- SELECT shortname, ra, dec FROM sources WHERE (222<ra AND ra<232) AND (5<dec AND dec<15)
- SELECT band, magnitude, magnitude_unc FROM photometry WHERE source_id=58
- SELECT source_id, band, magnitude FROM photometry WHERE band=’z’ AND magnitude<15
- SELECT wavelength, flux, unc FROM spectra WHERE observation_id=75”
As you hopefully gathered:
- Returns the shortname, ra and dec of all objects in a 10 square degree patch of sky centered at RA = 227, DEC = 10
- Returns all the photometry and uncertainties available for object 58
- Returns all objects and z magnitudes with z less than 15
- Returns the wavelength, flux and uncertainty arrays for all spectra of object 75
The above examples are for querying individual tables only. We can query from multiple tables at the same time with the JOIN command like so:
- SELECT t.name, p.band, p.magnitude, p.magnitude_unc FROM telescopes as t JOIN photometry AS p ON p.telescope_id=t.id WHERE p.source_id=58
- SELECT p1.magnitude-p2.magnitude FROM photometry AS p1 JOIN photometry AS p2 ON p1.source_id=p2.source_id WHERE p1.band=’J’ AND p2.band=’H’
- SELECT src.designation, src.unum, spt.spectral_type FROM sources AS src JOIN spectral_types AS spt ON spt.source_id=src.id WHERE spt.spectral_type>=10 AND spt.spectral_type<20 AND spt.regime=’optical’
- SELECT s.unum, p.parallax, p.parallax_unc, p.publication_id FROM sources as s JOIN parallaxes AS p ON p.source_id=s.id
As you may have gathered:
- Returns the survey, band and magnitude for all photometry of source 58
- Returns the J-H color for every object
- Returns the designation, U-number and optical spectral type for all L dwarfs
- Returns the parallax measurements and publications for all sources
Database Schema and Browsing
In order to write the SQL queries above you of course need to know the names of the fields in each table are. One way to do this is:
db.query("PRAGMA table_info('PHOTOMETRY')")
Even easier is to use the DB Browser for SQLite pictured at left which lets you expand and collapse each table, sort and order columns, and other fun stuff.
It even allows you to manually create/edit/destroy records with a very nice GUI.
IMPORTANT: Keep in mind that if you change a database record, you immediately change it for everyone since we share the same database file on Dropbox. Be careful!
Always check and double-check that you are entering the correct data for the correct source before you save any changes with the SQLite Database Browser.