BDdb module

BDdb.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
BDdb.adapt_header(header)

Adapts a FITS header into a HEADER string to put into the database.

Parameters:header (fits.header) – The FITS header to be adapted into a HEADER type that can be inserted into a SQL file.
Returns:The adapted header object
Return type:HEADER
BDdb.clean_header(fitsPath)

Clean illegal characters from keywords, insert END card, and rewrite header.

Parameters:fitsPath (str) – The path of the FITS file to be repaired.
Returns:The FITS header with an END card inserted and illegal characters removed from keywords.
Return type:fits.header
BDdb.compare_records(db, table, columns, old, new, options=['r', 'c', 'k', 'sql'], delete=False)

Compares similar records and prompts the user to make decisions about keeping, updating, or modifying records in question.

Parameters:
  • table (str) – The name of the table whose records are being compared.
  • columns (list) – The list of columns across which the comparison should be made.
  • old ((str, int, float, blob)) – The value of the record with the lower id.
  • new ((str, int, float, blob)) – The value of the record with the higher id.
  • options (list) – The allowed options: ‘r’ for replace, ‘c’ for complete, ‘k’ for keep, ‘sql’ for raw SQL input.
  • delete (bool) – Delete the record with the higher id.
BDdb.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
BDdb.convert_header(header)

Converts a HEADER string stored in the database back into a FITS header.

Parameters:header (HEADER) – The header object to be converted back into a FITS header.
Returns:The converted FITS header.
Return type:fits.header
BDdb.convert_spectrum(url)
class BDdb.get_db(dbpath)
add_ascii(asciiPath, source_id, header_chars=['#'], start=0, snrPath='', headerPath='', wavelength_units='', flux_units='', publication_id='', obs_date='', wavelength_order='', instrument_id='', telescope_id='', mode_id='', regime='', airmass=0, comment='')

Adds an ascii spectrum to the spectra table given an asciiPath. Any spectra table columns besides wavelength, flux, unc, snr arrays can be specified as arguments.

Parameters:
  • asciiPath (str) – The path to the ascii file with the wavelength, flux and (optional) uncertainty data
  • source_id (int) – The id from the SOURCES table of the object to which the spectrum will be associated.
  • header_chars (list) – If a line begins with any character in this list, that text will be used to generate a FITS header for the spectrum.
  • start (int) – The index of the line after the last header line that begins the data to be saved.
  • snrPath (str (optional)) – The path to the separate ascii file with the signal to noise values. Must be the same length as the wavelength and flux columns in the asciiPath file.
  • headerPath (str (optional)) – The path to the FITS file that contains the header information to be stored with the spectrum.
  • wavelength_units (str (optional)) – The wavelength units of the spectrum, e.g. ‘um’, ‘A’, ‘nm’
  • flux_units (str (optional)) – The flux units of the spectrum, e.g. ‘erg/s/cm2/A’, ‘W m-2 um-1’
  • publication_id (int (optional)) – The id from the PUBLICATIONS table that indicates the reference for the spectrum
  • obs_date (str (optional)) – The date of the observation
  • wavelength_order (int (optional)) – For high resolution spectra, the order of the observation, e.g. ‘65’ for NIRSPEC order 65. Leave blank for low and medium resolution spectra.
  • instrument_id (int (optional)) – The id from the INSTRUMENTS table used to take the spectrum
  • telescope_id (int (optional)) – The id from the TELESCOPES table used to take the spectrum
  • mode_id (int (optional)) – The id from the MODES table used to take the spectrum, if applicable
  • regime (str (optional)) – The regime of the spectrum, e.g. ‘OPT’,’NIR’ or ‘MIR’
  • airmass (float (optional)) – The airmass at the time of observation
  • comment (str (optional)) – Any comments about the spectrum that might be useful to future users.
add_data(ascii, table, delimiter='|', multiband=False)

Adds data in ascii file to the specified database table. Note column names (row 1 of ascii file) must match table fields to insert, however order and completeness don’t matter.

Parameters:
  • ascii (str) – The path to the ascii file to be read in.
  • 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
  • multiband (bool) – Digest columns of multiple photometric measurements (e.g. J, H, Ks) into individual rows of data for database insertion
Returns:

Return type:

None

add_fits(fitsPath, source_id, unc_fitsPath='', wavelength_units='', flux_units='', publication_id='', obs_date='', wavelength_order='', regime='', instrument_id='', telescope_id='', mode_id='', airmass=0, comment='', wlog=False, SDSS=False)

Checks the header of the fitsFile and inserts the data with source_id.

Parameters:
  • fitsPath (str) – The path to the FITS file with the wavelength, flux and (optional) uncertainty data
  • source_id (int) – The id from the SOURCES table of the object to which the spectrum will be associated.
  • unc_fitsPath (str) – The path to the separate FITS file with the uncertainty values. Must be the same length as the wavelength and flux arrays in the fitsPath file.
  • wavelength_units (str (optional)) – The wavelength units of the spectrum, e.g. ‘um’, ‘A’, ‘nm’
  • flux_units (str (optional)) – The flux units of the spectrum, e.g. ‘erg/s/cm2/A’, ‘W m-2 um-1’
  • publication_id (int (optional)) – The id from the PUBLICATIONS table that indicates the reference for the spectrum
  • obs_date (str (optional)) – The date of the observation
  • wavelength_order (int (optional)) – For high resolution spectra, the order of the observation, e.g. ‘65’ for NIRSPEC order 65. Leave blank for low and medium resolution spectra.
  • instrument_id (int (optional)) – The id from the INSTRUMENTS table used to take the spectrum
  • telescope_id (int (optional)) – The id from the TELESCOPES table used to take the spectrum
  • mode_id (int (optional)) – The id from the MODES table used to take the spectrum, if applicable
  • regime (str (optional)) – The regime of the spectrum, e.g. ‘OPT’,’NIR’ or ‘MIR’
  • airmass (float (optional)) – The airmass at the time of observation
  • comment (str (optional)) – Any comments about the spectrum that might be useful to future users.
add_numpy(wav, flx, err, snr, filename, source_id, wavelength_units='', flux_units='', publication_id='', obs_date='', wavelength_order='', regime='', instrument_id='', telescope_id='', mode_id='', airmass=0, comment='', headerPath='', wlog=False, SDSS=False)

Generic function to insert spectra formatted as numpy arrays. Useful if the .fits file format is unusual (for instance, Magellan II Clay MIKE data) and needs to be extracted in some custom way.

Parameters:
  • wav (array) – The Numpy wavelength array
  • flx (array) – The Numpy flux array
  • err (array) – The Numpy uncertainty array
  • snr (array) – The Numpy signal to noise array
  • filename (str) – The filename from which the data was taken
  • source_id (int) – The id from the SOURCES table of the object to which the spectrum will be associated.
  • wavelength_units (str (optional)) – The wavelength units of the spectrum, e.g. ‘um’, ‘A’, ‘nm’
  • flux_units (str (optional)) – The flux units of the spectrum, e.g. ‘erg/s/cm2/A’, ‘W m-2 um-1’
  • publication_id (int (optional)) – The id from the PUBLICATIONS table that indicates the reference for the spectrum
  • obs_date (str (optional)) – The date of the observation
  • wavelength_order (int (optional)) – For high resolution spectra, the order of the observation, e.g. ‘65’ for NIRSPEC order 65. Leave blank for low and medium resolution spectra.
  • instrument_id (int (optional)) – The id from the INSTRUMENTS table used to take the spectrum
  • telescope_id (int (optional)) – The id from the TELESCOPES table used to take the spectrum
  • mode_id (int (optional)) – The id from the MODES table used to take the spectrum, if applicable
  • regime (str (optional)) – The regime of the spectrum, e.g. ‘OPT’,’NIR’ or ‘MIR’
  • airmass (float (optional)) – The airmass at the time of observation
  • comment (str (optional)) – Any comments about the spectrum that might be useful to future users.
clean_up(table)

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.
edit_columns(table, columns, types)

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

Parameters:
  • table (str) – The name of the table to modify
  • columns (list) – A list of the columns in the order in which they are to appear in the SQL table
  • types (list) – A list of the types corresponding to each column in the columns list above.
header(spectrum_id_or_path)

Prints the header information for the given spectrum_id_or_path.

Parameters:spectrum_id_or_path ((int, str)) – The id from the SPECTRA table or the path to the FITS file of the spectrum header to print.
identify(search)

For search input of (ra,dec) decimal degree tuple, i.e. ‘(12.3456,-65.4321)’, returns all sources within 1 arcminute. For search input of text string, i.e. ‘vb10’, returns all sources with case-insensitive partial text matches in names or designation columns.

Parameters:search ((str, tuple)) – The text or coordinate tuple to search the SOURCES table with.
inventory(ID, verbose=True, plot=False, data=False)

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

Parameters:
  • ID ((int, list)) – The id or list of ids from the SOURCES table whose data across all tables is to be printed.
  • verbose (bool) – Prints all data from all tables if True else prints a data summary.
  • plot (bool) – Plots all spectra for the object.
  • data (bool) – If ID is a list and data is True, returns the results of the SOURCES table search.
Returns:

If ID is a list and data is True, returns the results of the summary SQL query for all given source_id.

Return type:

dict

lookup(table, ids=None, concatenate='', delim='/')

Quickly look up records from the specified table and list ids to limit results. Specify column values to concatenate into a string.

Parameters:
  • table (str) – The name of the table to perform a lookup on.
  • ids ((int, list)) – An id or list of ids to lookup.
  • concatenate (str) – The name of the column whose values should be joined by delim and returned.
  • delim (str) – If concatenate, the delimiter to be used to join the results.
Returns:

A list of the complete record(s) or a concatenated string from the desired table with the given ids.

Return type:

(list, str)

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

Merges specific tables or all tables of conflicted databse 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='')

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) – Mimicks the native parameter substitution of sqlite3
output_spectrum(spectrum_id, filepath)

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

Parameters:
  • spectrum_id (int) – The id from the SPECTRA table of the spectrum to print to file.
  • filepath (str) – The path of the file to print the data to.
plot_spectrum(spectrum_id)

Plots spectrum ID from SPECTRA table.

Parameters:spectrum_id (int) – The id from the SPECTRA table of the spectrum to plot.
query(SQL, params='', DICT=False, fetch='all')

Wrapper for cursors so data can be retrieved as a list or dictionary from same method

Parameters:
  • SQL (str) – The SQL query to execute
  • params (sequence) – Mimicks the native parameter substitution of sqlite3
  • DICT (bool) – Returns the data as a dictionary if True, else a list