Skip to content

Commit

Permalink
Removed BDNYCdb focus of index.rst.
Browse files Browse the repository at this point in the history
  • Loading branch information
hover2pi committed Feb 9, 2016
1 parent 3dada6a commit 676a065
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
40 changes: 31 additions & 9 deletions astrodbkit/astrodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def inventory(self, source_id, plot=False, fetch=False):
if data:
data_tables[table] = data
data = data[list(columns)]
pprint(data, title=table.upper())
if not fetch: pprint(data, title=table.upper())

else: pass

Expand All @@ -378,18 +378,31 @@ def merge(self, conflicted, tables=[], diff_only=True):
"""
if os.path.isfile(conflicted):
# Load and attach master and conflicted databases
con, master, reassign = get_db(conflicted), self.list("PRAGMA database_list").fetchall()[0][2], {}
con.list("ATTACH DATABASE '{}' AS m".format(master)), self.list("ATTACH DATABASE '{}' AS c".format(conflicted)), con.list("ATTACH DATABASE '{}' AS c".format(conflicted)), self.list("ATTACH DATABASE '{}' AS m".format(master))
con, master, reassign = get_db(conflicted) self.list("PRAGMA database_list").fetchall()[0][2], {}
con.list("ATTACH DATABASE '{}' AS m".format(master))
self.list("ATTACH DATABASE '{}' AS c".format(conflicted))
con.list("ATTACH DATABASE '{}' AS c".format(conflicted))
self.list("ATTACH DATABASE '{}' AS m".format(master))

# Drop any backup tables from failed merges
for table in tables: self.list("DROP TABLE IF EXISTS Backup_{0}".format(table))

# Gather user data to add to CHANGELOG table
import socket, datetime
user, machine_name, date, modified_tables = raw_input('Please enter your name : '), socket.gethostname(), datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), []
user = raw_input('Please enter your name : '),
machine_name = socket.gethostname()
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
modified_tables = []

# Print instructions for user
pprint(np.asarray([['-'*30,'-'*100],['[column name]','Display full record entry for that column without taking action'],['k','Keeps both records and assigns second one new id if necessary'],['r','Replaces all columns of first record with second record values'],['r [column name] [column name]...','Replaces specified columns of first record with second record values'],['c','Complete empty columns of first record with second record values where possible'],['[Enter]','Keep first record and delete second'],['abort','Abort merge of current table, undo all changes, and proceed to next table']]), names=['Command','Result'])
pprint(np.asarray([['[column name]','Display full record entry for that column without taking action'], \
['k','Keeps both records and assigns second one new id if necessary'], \
['r','Replaces all columns of first record with second record values'], \
['r [column name] [column name]...','Replaces specified columns of first record with second record values'], \
['c','Complete empty columns of first record with second record values where possible'], \
['[Enter]','Keep first record and delete second'],\
['abort','Abort merge of current table, undo all changes, and proceed to next table']]), \
names=['Command','Result'])

# Merge table by table, starting with SOURCES
tables = tables or ['sources']+[t for t in zip(*self.list("SELECT * FROM sqlite_master WHERE name NOT LIKE '%Backup%' AND type='table'{}".format(" AND name IN ({})".format("'"+"','".join(tables)+"'") if tables else '')).fetchall())[1] if t!='sources']
Expand All @@ -399,11 +412,16 @@ def merge(self, conflicted, tables=[], diff_only=True):

if any([i not in columns for i in conflicted_cols]):
# Abort table merge if conflicted has new columns not present in master. New columns must be added to the master database first via db.edit_columns().
print "\nMerge of {0} table aborted since conflicted copy has columns {1} not present in master.\nAdd new columns to master with BDdb.edit_columns() and try again.\n".format(table.upper(),[i for i in conflicted_cols if i not in columns])
print "\nMerge of {0} table aborted since conflicted copy has columns {1} not present in master.\nAdd new columns to master with astrodb.table() method and try again.\n".format(table.upper(),[i for i in conflicted_cols if i not in columns])

else:
# Add new columns from master table to conflicted table if necessary
if any([i not in conflicted_cols for i in columns]): con.modify("DROP TABLE IF EXISTS Conflicted_{0}".format(table)), con.modify("ALTER TABLE {0} RENAME TO Conflicted_{0}".format(table)), con.modify("CREATE TABLE {0} ({1})".format(table, ', '.join(['{} {}'.format(c,t) for c,t in zip(columns,types)]))), con.modify("INSERT INTO {0} ({1}) SELECT {1} FROM Conflicted_{0}".format(table, ','.join(conflicted_cols))), con.modify("DROP TABLE Conflicted_{0}".format(table))
if any([i not in conflicted_cols for i in columns]):
con.modify("DROP TABLE IF EXISTS Conflicted_{0}".format(table))
con.modify("ALTER TABLE {0} RENAME TO Conflicted_{0}".format(table))
con.modify("CREATE TABLE {0} ({1})".format(table, ', '.join(['{} {}'.format(c,t) for c,t in zip(columns,types)])))
con.modify("INSERT INTO {0} ({1}) SELECT {1} FROM Conflicted_{0}".format(table, ','.join(conflicted_cols)))
con.modify("DROP TABLE Conflicted_{0}".format(table))

# Pull unique records from conflicted table
data = map(list, con.list("SELECT * FROM (SELECT 1 AS db, {0} FROM m.{2} UNION ALL SELECT 2 AS db, {0} FROM c.{2}) GROUP BY {1} HAVING COUNT(*)=1 AND db=2".format(','.join(columns),','.join(columns[1:]),table)).fetchall())
Expand Down Expand Up @@ -437,8 +455,12 @@ def merge(self, conflicted, tables=[], diff_only=True):
abort = self.clean_up(table)

# Undo all changes to table if merge is aborted. Otherwise, push table changes to master.
if abort: self.modify("DROP TABLE {0}".format(table)), self.modify("ALTER TABLE Backup_{0} RENAME TO {0}".format(table))
else: self.modify("DROP TABLE Backup_{0}".format(table)), modified_tables.append(table.upper())
if abort:
self.modify("DROP TABLE {0}".format(table))
self.modify("ALTER TABLE Backup_{0} RENAME TO {0}".format(table))
else:
self.modify("DROP TABLE Backup_{0}".format(table))
modified_tables.append(table.upper())

else: print "{} tables identical.".format(table.upper())

Expand Down
49 changes: 35 additions & 14 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,63 @@
Welcome to astrodbkit's documentation!
======================================

The BDNYC Database is an advanced SQL relational database of published spectra, photometry and astrometry for over 1300 very low-mass stars, brown dwarfs and planetary mass objects.

This documentation describes a tool kit of classes, methods and functions useful for CRUD operations and analysis of data from The BDNYC Data Archive.
This documentation describes a tool kit of classes, methods and functions useful for CRUD operations and analysis of data from an SQL database.

Getting Started
===============

To install, just do::
To install, do::

pip install astrodbkit

This package includes the initial release of the BDNYC Database, which contains the astrometry, photometry and spectra for the 198 objects in the `Filippazzo et al. (2015)`_ sample.
Creating a Database
===================

To create a database from scratch, do::

dbpath = '/desired/path/to/my_new_database.db'
astrodb.create_database(dbpath)
Alternatively, you can `download and use the BDNYC Database`_, which contains the astrometry, photometry and spectra for the 198 objects in the `Filippazzo et al. (2015)`_ sample.

.. _download and use the BDNYC Database: https://github.com/BDNYC/BDNYCdb
.. _Filippazzo et al. (2015): http://adslabs.org/adsabs/abs/2015ApJ...810..158F/

.. note:: For access to the full dataset, an email request must be made to a BDNYC group admin.

Accessing the Database
======================
====================

To start using the database, launch iPython, import the module, then initialize the database with the :class:`astrodb.get_db()` class like so::

from astrodbkit import astrodb
db = astrodb.get_db()
db = astrodb.get_db(dbpath)

Voila!

Voila! You can see an inventory of all data for a specific source by passing a *source_id* to the :py:meth:`~astrodb.get_db.inventory` method::
Querying the Database
=====================

db.inventory(86)
Now that you have the database at your fingertips, you’ll want to get some info out of it.

This will also plot all available spectra for that source for visual inspection if you set **plot=True**.
You can see an inventory of all data for a specific source by passing an integer id to the :py:meth:`~astrodb.get_db.inventory` method::

Querying the Database
=====================
data = db.inventory(86)

This will retrieve the data across all tables with the specified source_id for visual inspection. Setting *fetch=True* will return the data as a dictionary of Astropy tables so that table and column keys can be used to access the results. For example::

data['photometry'][['band','magnitude','magnitude_unc']]

will return a table of the band, magnitude and uncertainty for all records in the sources table with that source_id.

You can search any table in the database with the :py:meth:`~astrodb.get_db.identify` method by supplying a string, integer, or (ra,dec) coordinates along with the table to search. For example, if I want to find all the records in the SOURCES table in the HR 8799 system::

db.identify('8799','sources')
Or all the papers published by Joe Filippazzo::

db.identify('Fili','publications')

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 :py:meth:`~astrodb.get_db.query` method::
You can also pass SQL queries wrapped in double-quotes (") to the :py:meth:`~astrodb.get_db.query` method::

data = db.query( "SQL_query_goes_here" )

Expand Down

0 comments on commit 676a065

Please sign in to comment.