astrodb module

class astrodbkit.astrodb.Database(dbpath, directory=u'')
add_changelog(user=u'', mod_tables=u'', user_desc=u'')

Add an entry to the changelog table. This should be run when changes or edits are done to the database.

Parameters:
  • user (str) – Name of the person who made the edits
  • mod_tables (str) – Table or tables that were edited
  • user_desc (str) – A short message describing the changes
add_data(data, table, delimiter=u'|', bands=u'', clean_up=True, verbose=False)

Adds data to the specified database table. Column names must match table fields to insert, however order and completeness don’t matter.

Parameters:
  • data (str, array-like, astropy.table.Table) – The path to an ascii file, array-like object, or table. The first row or element must be the list of column names
  • table (str) – The name of the table into which the data should be inserted
  • delimiter (str) – The string to use as the delimiter when parsing the ascii file
  • bands (sequence) – Sequence of band to look for in the data header when digesting columns of multiple photometric measurements (e.g. [‘MKO_J’,’MKO_H’,’MKO_K’]) into individual rows of data for database insertion
  • clean_up (bool) – Run self.clean_up()
  • verbose (bool) – Print diagnostic messages
add_foreign_key(table, parent, key_child, key_parent, verbose=True)

Add foreign key (key_parent from parent) to table column key_child

Parameters:
  • table (string) – The name of the table to modify. This is the child table.
  • parent (string or list of strings) – The name of the reference table. This is the parent table.
  • key_child (string or list of strings) – Column in table to set as foreign key. This is the child key.
  • key_parent (string or list of strings) – Column in parent that the foreign key refers to. This is the parent key.
  • verbose (bool, optional) – Verbose output
clean_up(table, verbose=False)

Removes exact duplicates, blank records or data without a source_id from the specified table. Then finds possible duplicates and prompts for conflict resolution.

Parameters:
  • table (str) – The name of the table to remove duplicates, blanks, and data without source attributions.
  • verbose (bool) – Print out some diagnostic messages
close(silent=False)

Close the database and ask to save and delete the file

Parameters:silent (bool) – Close quietly without saving or deleting (Default: False).
get_bibtex(id, fetch=False, table=u'publications')

Grab bibtex entry from NASA ADS

Parameters:
  • id (int or str) – The id or shortname from the PUBLICATIONS table to search
  • fetch (bool) – Whether or not to return the bibtex string in addition to printing (default: False)
  • table (str) – Table name, defaults to publications
Returns:

bibtex – If fetch=True, return the bibtex string

Return type:

str

help()

See a quick summary of the most useful methods in astrodb.Database

info()

Prints out information for the loaded database, namely the available tables and the number of entries for each.

inventory(source_id, fetch=False, fmt=u'table')

Prints a summary of all objects in the database. Input string or list of strings in ID or unum for specific objects.

Parameters:
  • source_id (int) – The id from the SOURCES table whose data across all tables is to be printed.
  • fetch (bool) – Return the results.
  • fmt (str) – Returns the data as a dictionary, array, or astropy.table given ‘dict’, ‘array’, or ‘table’
Returns:

data_tables – Returns a dictionary of astropy tables with the table name as the keys.

Return type:

dict

lookup(criteria, table, columns=u'')

Returns a table of records from table the same length as criteria with the best match for each element.

Parameters:
  • criteria (sequence) – The search criteria
  • table (str) – The table to search
  • columns (sequence) – The column name in the sources table to search
Returns:

results – A sequence the same length as objlist with source_ids that correspond to successful matches and blanks where no matches could be made

Return type:

sequence

merge(conflicted, tables=[], diff_only=True)

Merges specific tables or all tables of conflicted database into the master database.

Parameters:
  • conflicted (str) – The path of the SQL database to be merged into the master.
  • tables (list (optional)) – The list of tables to merge. If None, all tables are merged.
  • diff_only (bool) – If True, only prints the differences of each table and doesn’t actually merge anything.
modify(SQL, params=u'', verbose=True)

Wrapper for CRUD operations to make them distinct from queries and automatically pass commit() method to cursor.

Parameters:
  • SQL (str) – The SQL query to execute
  • params (sequence) – Mimics the native parameter substitution of sqlite3
  • verbose (bool) – Prints the number of modified records
output_spectrum(spectrum, filepath, header={})

Prints a file of the given spectrum to an ascii file with specified filepath.

Parameters:
  • spectrum (int, sequence) – The id from the SPECTRA table or a [w,f,e] sequence
  • filepath (str) – The path of the file to print the data to.
  • header (dict) – A dictionary of metadata to add of update in the header
plot_spectrum(spectrum_id, table=u'spectra', column=u'spectrum', overplot=False, color=u'b', norm=False)

Plots a spectrum from the given column and table

Parameters:
  • spectrum_id (int) – The id from the table of the spectrum to plot.
  • overplot (bool) – Overplot the spectrum
  • table (str) – The table from which the plot is being made
  • column (str) – The column with SPECTRUM data type to plot
  • color (str) – The color used for the data
  • norm (bool, sequence) – True or (min,max) wavelength range in which to normalize the spectrum
query(SQL, params=u'', fmt=u'array', fetch=u'all', unpack=False, export=u'', verbose=False, use_converters=True)

Returns data satisfying the provided SQL script. Only SELECT or PRAGMA statements are allowed. Results can be returned in a variety of formats. For example, to extract the ra and dec of all entries in SOURCES in astropy.Table format one can write:

data = db.query(‘SELECT ra, dec FROM sources’, fmt=’table’)

For more general SQL statements, see the modify() method.

Parameters:
  • SQL (str) – The SQL query to execute
  • params (sequence) – Mimics the native parameter substitution of sqlite3
  • fmt (str) – Returns the data as a dictionary, array, astropy.table, or pandas.Dataframe given ‘dict’, ‘array’, ‘table’, or ‘pandas’
  • fetch (str) – String indicating whether to return all results or just one
  • unpack (bool) – Returns the transpose of the data
  • export (str) – The file path of the ascii file to which the data should be exported
  • verbose (bool) – print the data as well
  • use_converters (bool) – Apply converters to columns with custom data types
Returns:

result – The result of the database query

Return type:

(array,dict,table)

references(criteria, publications=u'publications', column_name=u'publication_shortname', fetch=False)

Do a reverse lookup on the publications table. Will return every entry that matches that reference.

Parameters:
  • criteria (int or str) – The id from the PUBLICATIONS table whose data across all tables is to be printed.
  • publications (str) – Name of the publications table
  • column_name (str) – Name of the reference column in other tables
  • fetch (bool) – Return the results.
Returns:

data_tables – Returns a dictionary of astropy tables with the table name as the keys.

Return type:

dict

save(directory=None)

Dump the entire contents of the database into the tabledata directory as ascii files

schema(table)

Print the table schema

Parameters:table (str) – The table name
search(criterion, table, columns=u'', fetch=False, radius=0.016666666666666666, use_converters=False, sql_search=False)

General search method for tables. For (ra,dec) input in decimal degrees, i.e. (12.3456,-65.4321), returns all sources within 1 arcminute, or the specified radius. For string input, i.e. ‘vb10’, returns all sources with case-insensitive partial text matches in columns with ‘TEXT’ data type. For integer input, i.e. 123, returns all exact matches of columns with INTEGER data type.

Parameters:
  • criterion ((str, int, sequence, tuple)) – The text, integer, coordinate tuple, or sequence thereof to search the table with.
  • table (str) – The name of the table to search
  • columns (sequence) – Specific column names to search, otherwise searches all columns
  • fetch (bool) – Return the results of the query as an Astropy table
  • radius (float) – Radius in degrees in which to search for objects if using (ra,dec). Default: 1/60 degree
  • use_converters (bool) – Apply converters to columns with custom data types
  • sql_search (bool) – Perform the search by coordinates in a box defined within the SQL commands, rather than with true angular separations. Faster, but not a true radial search.
show_image(image_id, table=u'images', column=u'image', overplot=False, cmap=u'hot', log=False)

Plots a spectrum from the given column and table

Parameters:
  • image_id (int) – The id from the table of the images to plot.
  • overplot (bool) – Overplot the image
  • table (str) – The table from which the plot is being made
  • column (str) – The column with IMAGE data type to plot
  • cmap (str) – The colormap used for the data
snapshot(name_db=u'export.db', version=1.0)

Function to generate a snapshot of the database by version number.

Parameters:
  • name_db (string) – Name of the new database (Default: export.db)
  • version (float) – Version number to export (Default: 1.0)
table(table, columns, types, constraints=u'', pk=u'', new_table=False)

Rearrange, add or delete columns from database table with desired ordered list of columns and corresponding data types.

Parameters:
  • table (sequence) – The name of the table to modify
  • columns (list) – A sequence of the columns in the order in which they are to appear in the SQL table
  • types (sequence) – A sequence of the types corresponding to each column in the columns list above.
  • constraints (sequence (optional)) – A sequence of the constraints for each column, e.g. ‘’, ‘UNIQUE’, ‘NOT NULL’, etc.
  • pk (string or list) – Name(s) of the primary key(s) if other than ID
  • new_table (bool) – Create a new table
class astrodbkit.astrodb.Image(data, header=u'', path=u'')
exception astrodbkit.astrodb.InputError(message)

Bases: exceptions.Exception

Exception raised for errors in the input.

message -- explanation of the error
class astrodbkit.astrodb.Spectrum(data, header=u'', path=u'')
astrodbkit.astrodb.adapt_array(arr)

Adapts a Numpy array into an ARRAY string to put into the database.

Parameters:arr (array) – The Numpy array to be adapted into an ARRAY type that can be inserted into a SQL file.
Returns:The adapted array object
Return type:ARRAY
astrodbkit.astrodb.ang_sep(row, ra1, dec1)

Calculate angular separation between two coordinates Uses Vicenty Formula (http://en.wikipedia.org/wiki/Great-circle_distance) and adapts from astropy’s SkyCoord Written to be used within the Database.search() method

Parameters:
  • row (dict, pandas Row) – Coordinate structure containing ra and dec keys in decimal degrees
  • ra1 (float) – RA to compare with, in decimal degrees
  • dec1 (float) – Dec to compare with, in decimal degrees
Returns:

Return type:

Angular distance, in degrees, between the coordinates

astrodbkit.astrodb.clean_header(header)
astrodbkit.astrodb.convert_array(array)

Converts an ARRAY string stored in the database back into a Numpy array.

Parameters:array (ARRAY) – The array object to be converted back into a Numpy array.
Returns:The converted Numpy array.
Return type:array
astrodbkit.astrodb.convert_image(File, verbose=False)

Converts a IMAGE data type stored in the database into a data cube

Parameters:
  • File (str) – The URL or filepath of the file to be converted into arrays.
  • verbose (bool) – Whether or not to display some diagnostic information (Default: False)
Returns:

The converted image

Return type:

sequence

astrodbkit.astrodb.convert_spectrum(File, verbose=False)

Converts a SPECTRUM data type stored in the database into a (W,F,E) sequence of arrays.

Parameters:
  • File (str) – The URL or filepath of the file to be converted into arrays.
  • verbose (bool) – Whether or not to display some diagnostic information (Default: False)
Returns:

The converted spectrum.

Return type:

sequence

astrodbkit.astrodb.create_database(dbpath, schema=u'', overwrite=True)

Create a new database at the given dbpath

Parameters:
  • dbpath (str) – The full path for the new database, including the filename and .db file extension.
  • schema (str) – The path to the .sql schema for the database
  • overwrite (bool) – Overwrite dbpath if it already exists
astrodbkit.astrodb.pprint(data, names=u'', title=u'', formats={})

Prints tables with a bit of formatting

Parameters:
  • data ((sequence, dict, table)) – The data to print in the table
  • names (sequence) – The column names
  • title (str (optional)) – The title of the table
  • formats (dict) – A dictionary of column:format values
astrodbkit.astrodb.scrub(data, units=False)

For input data [w,f,e] or [w,f] returns the list with NaN, negative, and zero flux (and corresponding wavelengths and errors) removed.