How to add data to the database

Here are some guidelines for inputting data into the BDNYC database!

Part 1: Opening database

This should be done in the directory in which you have the database stored.

>>> import pickle
>>> import BDNYC
>>> f=open(‘BDNYCData.txt’,’rb’)
>>> bdnyc=pickle.load(f)
>>> f.close()

IMPORTANT: BDNYC and bdnyc are not the same thing.  Throughout this tutorial I use both BDNYC and bdnyc.  Make sure to keep track of which is which when using these commands.

Part 2: Create a dictionary that holds your data

The first thing you need to do is check whether the target is already in the database.  You do this by checking whether the u number exists in the database. (Inside the parentheses goes your u-number in single quotes. Ex. ‘U12140’):

>>> bdnyc.matchUNum(‘unum’)

If the target exists in the database, it will output the target index.  You will need this index when inputting the data.Then we must create a dictionary to hold your data.  This is done by creating an empty dictionary, and adding levels to it.  It is a good idea to automate this if you’re going to be adding more than a few objects to the database. You can name your dictionary anything you want. We will do different things with the dictionary depending on whether the object is already in the database.

>>> dictionary={}
>>> dictionary[‘resolution’]={}
>>> dictionary[‘resolution’][‘instrument’]={}
>>> dictionary[‘resolution’][‘instrument’][‘observation date’]={}
>>> dictionary[‘resolution’][‘instrument’][‘observation date’][order]={}
Now we start adding in actual data.
>>> dictionary[‘resolution’][‘instrument’][‘observation date’][order][‘wl’]=array_w_wavelength_data
>>> dictionary[‘resolution’][‘instrument’][‘observation date’][order][‘flux’]=array_w_flux_data
>>> dictionary[‘resolution’][‘instrument’][‘observation date’][order][‘snr’]=array_w_snr_data
>>> dictionary[‘resolution’][‘instrument’][‘observation date’][order][‘uncertainty’]=array_w_uncertainty_data

Part 3: Putting the data in the database

Part 3.1: If the target does not exist in the database

>>> target=BDNYC.Target(‘name’,’unum’,’ra’,’dec’,’sptype’,opt,nir,mir)

If you are adding optical data to the database, you would replace the word opt with your dictionary.  If you are adding NIR data, replace nir, etc.  If you are not adding a certain kind of data, put an empty dictionary.  Let’s say I was adding only NIR data.  I would type the following:
>>>  target1=BDNYC.Target(‘name’,’unum’,’ra’,’dec’,’sptype’,{},nir_dictionary,{})

Continue with the following command:
>>> bdnyc.addTarget(target1)

Now your new target and data are in the database!

Part 3.2: If the target exists in the database

Here it is important to figure out what kind of data exists for your object.  Basically if the object exists, you just need to fill in whatever extra information you have. Let’s consider the following examples:

Example 1: You are adding near infrared, high-res NIRSPEC data to a target that already has near infrared, high-res Phoenix data in the database.  For this object, the dictionary level for high resolution already exists, meaning the dictionary holding your data doesn’t need to have this, or the level below (the level containing the instrument).  You can alter the dictionary from Part 2, or create one that doesn’t have these levels.  To alter the dictionary from Part 2, you would use the following command:
>>> new_dictionary=dictionary[‘high’][‘NIRSPEC’]

Now, simply add the new dictionary to the pre-existing target. Remember that the object index is output by the matchUNum function we used in Part 2:
>>> bdnyc.targets[object_index].nir[‘high’][‘NIRSPEC’]=new_dictionary

Example 2: You are adding near infrared, high-res NIRSPEC data from the night of Sep 16, 2008 to a target which already has near infrared, high-res NIRSPEC data from the night of Sep 15, 2008.  You would use the following commands, assuming you created the dictionary as in Part 2:

>>> new_dictionary=dictionary[‘high’][‘NIRSPEC’][‘2008sep16’]
>>> bdnyc.targets[object_index].nir[‘high’][‘NIRSPEC’][‘2008sep16’]=new_dictionary

Now your new data has been added to the pre-existing target!

Part 4: Saving your work

Again, do this in the directory in which you store the database.

>>> f=open(“BDNYCData.txt”,”wb”)
>>> pickle.dump(bdnyc,f)
>>> f.close()

Remember to use GitHub to commit/push your changes so everyone else can access the updated database!

One thought on “How to add data to the database

  1. Pingback: New Database Method: res_initializer() | BDNYC

Comments are closed.