Adding Data to a SQL Database using astrodbkit

NOTE: The information on this post may be outdated. We recommend looking into the up-to-date documentation at trac (internal) and ReadTheDocs.

To add data to any table, there are two easy steps. As our working example, we’ll add some new objects to the SOURCES table.

Step 1: Create an ascii file of the data

First, you must choose a delimiter, which is just the character that will break up the data into columns. I recommend a pipe ‘|’ character since they don’t normally appear in text. This is better than a comma since some data fields may have comma-separated values.

Put the data to be added in an ascii file with the following formatting:

  1. The first line must be the |-separated column names to insert/update, e.g. ra|dec|publication_id. Note that the column names in the ascii file need not be in the same order as the table. Also, only the column names that match will be added and non-matching or missing column names will be ignored, e.g. spectral_type|ra|publication_id|dec will ignore the spectral_type values as this is not a column in the SOURCES table and input the other columns in the correct places.
  2. If a record (i.e. a line in your ascii file) has no value for a particular column, type nothing. E.g. for the given column names ra|dec|publication_id|comments, a record with no publication_id should read 34.567|12.834||This object is my favorite!.

Step 2: Add the data to the specified table

To add the data to the table (in our example, the SOURCES table), import astrodbkit and initialize the .db file. Then run the add_data() method with the path to the ascii file as the first argument and the table to add the data to as the second argument. Be sure to specify your delimiter with delim='|'. Here’s what that looks like:

from astrodbkit import astrodb
db = astrodb.Database('/path/to/the/database/file.db')
db.add_data('/path/to/the/upload/file.csv', 'sources', delim='|')

That’s it!

BDNYC at AAS 225

BDNYC (and friends) are out in force for the 225th meeting of the American Astronomical Society!

Please come see our posters and talks (mostly on Monday). To whet your appetite, or if you missed them, here are some samplers:

Munazza Alam (Monday, 138.40)
High-Resolution Spectral Analysis of Red & Blue L Dwarfs
AAS225_Munazza_posterSara Camnasio (Monday, 138.39)
Multi-resolution Analysis of Red and Blue L Dwarfs
AAS225_Sara_posterKelle Cruz and Stephanie Douglas (Monday, 138.37)
When good fits go wrong: Untangling Physical Parameters of Warm Brown Dwarfs

AAS225_Stephanie_posterStephanie Douglas (Monday 138.19)
Rotation and Activity in Praesepe and the Hyades

AAS225_Steph_poster2Jackie Faherty (Talk, Monday 130.05)
Clouds in the Coldest Brown Dwarfs

Joe Filippazzo (Monday, 138.34)
Fundamental Parameters for an Age Calibrated Sequence of the Lowest Mass Stars to the Highest Mass Planets
Joe Filippazzo - AAS225

Paige Giorla (Monday, 138.44)
T Dwarf Model Fits for Spectral Standards at Low Spectral Resolution
AAS225_Paige_posterKay Hiranaka (Talk, Monday 130.04D)
Constraining the Properties of the Dust Haze in the Atmospheres of Young Brown Dwarfs

Erini Lambrides (Thursday, 432.02)
Can 3000 IR spectra unveil the connection between AGN and the interstellar medium of their host galaxies?

Emily Rice (Tuesday, 243.02)
STARtorialist: Astronomy Outreach via Fashion, Sci-Fi, & Pop Culture
AAS225_STARtorialistAdric Riedel (Monday, 138.38)
The Young and the Red: What we can learn from Young Brown Dwarfs

Using WriteLaTeX for Collaborative Papers

Update: WriteLaTeX is now Overleaf

We all love Google Docs. It’s a functional and convenient way to share and collaboratively edit documents across platforms, time zones, and even continents. We in the BDNYC group use it extensively.

But what if you want to write a scientific paper? Google Docs, as awesome as it is, is not much more than a word processor. We want the internal hyperlinks for sections, figures, tables, and citations, elegant mathematical formulae, well-formatted tables, more control over where and how our components are arranged – in a word, LaTeX. Yes, LaTeX has its own host of problems, but it’s very good at what it does.

There are a number of collaborative editing projects out there – Authorea springs to mind. But one of the simpler options out there is actually pretty good: WriteLaTeX.
Continue reading

Documentation using Sphinx

Sphinx is a free, online tool that helps generate documentation for primarily Python projects.  We use it for HTML format outputs, but it also supports LaTeX, ePub, Texinfo, manual pages, and plain text formats.  The markup language of Sphinx is reStructuredText (reST) (for documentation see  Of Sphinx’s many features, the one most useful thus far is its auto-documentation of Python files, the steps of which this post details below.

Sphinx auto-documentation steps:

  1. Install Sphinx at the command line using: easy_install -U Sphinx
  2. Read through the Sphinx documentation at
  3. Run Sphinx’s Getting Started function Sphinx-quickstart to quickly set up directories and configurations. NOTE: Since I have already completed this setup, it DOES NOT need to be run again for the BDNYC documentation.
    1. Access the documentation directories in Dropbox through the shared folder BDNYCdb/Documentation
    2. The Documentation folder contains the .rst and .py files that Sphinx uses to build documentation.  Place any files for which documentation is to be made within that directory since Sphinx automatically looks there.
      Screen Shot 2014-08-07 at 2.46.31 PMBDdb.html within the Documentation directories
  4. The index.rst file is the master file that contains the table of contents (known as a TOCtree) for the entire documentation.  Within  the TOCtree, Sphinx knows that all the files are .rst, so there is no need to specify the file extension or path.  The index also serves as a welcome page for users, so it may include an introduction or other information that would be useful for outside reference.  Within the TOCtree (and all of reST) indentation and notation is especially important.  Below is an example of a functioning TOCtree.  .. toctree::
    :maxdepth: 2
    # blank line

    The “maxdepth” function specifies the depth of nesting allowed.  In order to keep the index clean and aesthetically simple, a maximum of one nested page is reasonable. In other files, however, highly nested pages are not only common, but well-supported by reST.

    Screen Shot 2014-08-07 at 2.52.07 PM
    Screenshot of index.rst
  5. For each item in the TOCtree of the index file, there must be a corresponding .rst file in the main directory for Sphinx to nest within the index HTML file.  In the above example, a Database_instructions.rst file might contain in plain English how to interact with the database, while the BDdb.rst file contains the auto-documentation of
  6. The .rst file within the TOCtree of the index must be formatted like below to correctly call for the Python files. [name of module]
    ================    # make page header (see
    .. automodule:: [name of Python file w/o extension]
    :members:    # tells Sphinx to pull from the file
    # blank line

    Screen Shot 2014-08-07 at 2.57.41 PM

    Screenshot of BDdb.rst

  7. The referenced Python files for auto-documentation can either be in located in the main directory, or their location can be specified.  Since we will be constantly pushing and pulling from Github, it is best for those doing documentation to change the file path to their local copy of,, etc, before making documentation.  This can be achieved as follows:
  8. Open, which is the configuration file for Sphinx.  It was produced during the “quickstart” function to allow easy modifications of aesthetic features (i.e., theme, font), and facilitate the HTML building process.
    1. Change the file path in line 21 to the applicable local copy.
    2. Comment out the file path of previous users in order to make it easier for others to update documentation.
  9. In order to make the HTML files, move Terminal to the directory of the documentation, i.e., ‘…/Dropbox/BDNYCdb/Documentation’ and run make html.
    1. The make htmlfunction comes build-in through running “quickstart” and uses the makefile in the main directory.  It can take other functions as well, including make clean, which deletes the entire contents of the “build” directory.
    2. Sphinx should put the generated HTML files in the /_build/html directory.
  10. To make the HTML files live on, use the application Cyberduck, which can be downloaded at
    1. Click “Open Connection” and put in the server, username, and password.
    2. Drag and drop any .rst and HTML files that were modified into the appropriate directory and the documentation should go live immediately!


References: — Sphinx documentation homepage — Outline of Sphinx documentation — Using reStructuredText in Sphinx — Auto-documentation documentation — Download Cyberduck

Rotational and Vibrational Energy of Diatomic Molecules

Molecules can store energy in 4 different ways: translation (whole molecule moves in each of 3 Cartesian coordinates), rotation (whole molecule rotates—linear molecules can rotate around 2 axes, nonlinear around all 3 axes), vibration (bonds stretch and contract—3N-5 kinds of vibrations for linear molecules, 3N-6 for nonlinear), and electronic (ground state vs. excited). The energy absorbed in electronic transitions is usually in the UV-visible range, that for vibrations is in the IR range, for rotations in the microwave range, and the transition energy for translations is basically infinitesimal. This is because electronic transitions require the most energy, then vibrations, rotations and finally translations require the least. With high enough resolution, rotational transitions can be seen in vibrational spectra, with the rotational transitions giving structure to the vibrational absorption peaks.

Figure 1: An energy diagram showing the relative energies for rotational, vibrational, and electronic transitions and how they overlap.3

The energy of rotational and vibrational levels can be calculated separately and summed. Vibrations can be modeled using the simple harmonic oscillator—the potential energy of which comes from Hooke’s law. Rotational energy of a molecule can be calculated by assuming the molecule acts as a rigid rotor and solving the corresponding Schrodinger Equation. Both of these types of energy are quantized, which accounts for the vibrational (v) and rotational (J) quantum levels in the equation.

This equation will give energy levels in Joules. Vibrational spectroscopists generally use units of wavenumbers, or cm-1, though, which can be found by dividing the entire equation by hc.



  • v: vibration quantum number (0,1,2,…)
  • J: rotation quantum number (0,1,2,…)
  • k: spring constant/strength of bond
  • μ: reduced mass
  • I: moment of inertia, where is bond length
  • B: rotational constant

I began looking into this to explain why multiple carbon monoxide absorption peaks appeared where they did in my near-IR spectra at 2.29 and 2.32 microns or 4367 and 4310 cm-1. I was able to calculate the correct 2.29 μm peak by subtracting the energy where v=0 and J=8 from the energy where v=2 and J=10. This means that these peaks are due to the first vibrational overtone of CO, i.e. Δv=2 instead of 1. The peak at 2.32 and any other peaks around the same area can be found by going from different rotational energy states, 6 to 8, 4 to 6, etc. ΔJ for CO is usually ±2 because it is a linear molecule.

  1. McQuarrie, Donald A. Quantum Chemistry. 2nd ed. Sausalito: University Science Books, 2008.
  2. Atkins, P.; de Paula, J. Physical Chemistry. 8th ed. New York: W. H. Freeman and Company, 2006.
  3. “Electronic Spectra of Organic Molecules”. Organic Chemistry. Accessed July 24, 2014.

On solving the Rigid Rotor Schrodinger Equation:ödinger_Equation

Special thanks to Professor Andrew Crowther.


Use of the DISTINCT function in an SQLite query

When using an sqlite query to pull from our database, you may run into the issue where you only want to see the desired field entry once, and the resulting list or dictionary is giving it to you multiple times.

An Example

Say you want to get the source_id from the spectra table where the spectral_type is an L dwarf and the spectra regime is NIR. Meaning, you want to know which L dwarfs we have NIR spectra for. Well, the output will give you a list of all of the entries we have spectra for, even if some entries overlap on source_id. Each source_id will show up in the list per the number of separate NIR spectra we have for that object. If  you only want to see the source_id once, we can use a function tag called DISTINCT. It follows like:

SELECT DISTINCT sp.source_id FROM spectra AS sp JOIN spectral_types AS spt ON spt.source_id=sp.source_id WHERE spt.spectral_type BETWEEN 10 AND 19 AND sp.regime='NIR'

(See the joining tables post if confused on how to do so.)

*NOTE: Here I used another function BETWEEN instead of writing out spt.spectral_type>=10 AND spt.spectral_type<20

Using astroquery

If you’re like me, you’ve spent a lot of time downloading a table from VizieR, and then trying to insert it into whatever database or table you are currently using. It’s annoying, particularly because VizieR either won’t output information in a machine-readable format or won’t put an empty line into a massive list of results when there’s no match in the catalog.
Astroquery is better. It’s a python package affiliated with astropy that can grab data from all your favorite catalog servers (Vizier, SIMBAD, IRSA, NED, etc) directly, and use it; combine that with Astropy’s ability (or Python’s capacity) to print out tables to files, and it’s a lot easier to write a script to do it all for you.

Astroquery is in the Python Package Index, so you can install and update it with
pip install astroquery

You also need the ‘requests’ module to do the web-interfacing (and Astropy v3); it’s available on the Python Package Index and comes preloaded in Anaconda. Using it is simple (this part is almost entirely taken from their pages on using VizieR and IRSA):
Continue reading

Uncertainty Propagation

All observations have associated uncertainties which must be propagated through your analysis to a an uncertainty on a final result. The principle of uncertainty propagation is fairly simple:

$$!\sigma_x^2 = \sigma_u^2\left(\frac{\partial x}{\partial u}\right)^2 + \sigma_v^2\left(\frac{\partial x}{\partial v}\right)^2+\dots +2\sigma_{uv}^2\left(\frac{\partial x}{\partial u}\right)\left(\frac{\partial x}{\partial v}\right)+\dots$$

The first two terms on the right are the averages of the squares of the deviations in x produced by the uncertainties in observables u and v respectively. The third term on the right is the average of the cross terms, which cancel out if u and v are uncorrelated. Thus in most situations, a reasonable approximation is:

$$!\sigma_x^2 = \sum\limits_u\sigma_u^2\left(\frac{\partial x}{\partial u}\right)^2$$


A typical situation is the sum of two observables each with a multiplicative factor:


$$!\left(\frac{\partial x}{\partial u}\right)=a,\hspace{10pt}\left(\frac{\partial x}{\partial v}\right)=b$$

$$!\sigma_x = \sqrt{\sigma_u^2a^2 + \sigma_v^2b^2}$$

which is the oft used “summing in quadrature.”

Perhaps you have the product of two observables:


$$!\left(\frac{\partial x}{\partial u}\right)=av,\hspace{10pt}\left(\frac{\partial x}{\partial v}\right)=au$$

$$!\sigma_x = \sqrt{\sigma_u^2(av)^2 + \sigma_v^2(au)^2}=a\sqrt{\sigma_u^2v^2 + \sigma_v^2u^2}$$

Finally, perhaps you have the inverse of one observable times the exponential of another:


$$!\left(\frac{\partial x}{\partial u}\right)=\left(-\frac{a}{u^2}\right)e^{bv},\hspace{10pt}\left(\frac{\partial x}{\partial v}\right)=\frac{a}{u}be^{bv}$$

$$!\sigma_x = \sqrt{\sigma_u^2\left(\frac{a}{u^2}e^{bv}\right)^2 + \sigma_v^2\left(\frac{a}{u}be^{bv}\right)^2}=\frac{a}{u}e^{bv}\sqrt{\frac{\sigma_u^2}{u^2}+\sigma_v^2b^2}$$


Setting Up Your BDNYC Astro Python Environment

Here’s a step-by-step tutorial on how to set up your Python development environment.

Distribution and Compiler

The first step is to install Anaconda. Click here, select the appropriate version for your machine and operating system, download the .dmg file, open it and double-click the installer.

Make sure that when the installer asks where to put the distribution, you choose your actual hard drive and not some sub-directory on your machine. This will make things much easier later on.

What’s nice about this distribution is that it includes common plotting, computing, and astronomy packages such as numpy and scipy, astropy, matplotlib, and many others.

Next you’ll need a C compiler. If you’re using MacOSX, I recommend using Xcode which you can download free here. (This step may take a while so go get a beverage.) Once the download is complete, run the installer.

Required Packages

For future installation of packages, I recommend using Pip. To install this, at your Terminal command line type sudo easy_install pip. Then whenever you want to install a package you might need, you just open Terminal and do pip install package_name.

Not every package is this easy (though most are). If you can’t get something through Pip just download, unzip and put the folder of your new module with the rest of your packages in the directory /anaconda/pkgs/.

Then in Terminal, navigate to that directory with cd /anaconda/pkgs/package_dir_name and do python install.

Development Tools

MacOSX comes with the text editing application TextEdit but it is not good for editing code. I strongly recommend using TextMate though it is not free so you should ask your advisor to buy a license for you! Otherwise, some folks find the free TextWrangler to be pretty good.

Next, you’ll want to get access to the BDNYC database. Detailed instructions are here on how to setup Dropbox and Github on your machine in order to interact with the database.

Launching Python

Now to use Python, in Terminal just type ipython --pylab. I recommend always launching Python this way to have the plotting library preloaded.