Skip to content

Commit

Permalink
added a more convenient download mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
wkerzendorf committed Jun 22, 2015
1 parent 52a4491 commit f0de577
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
11 changes: 10 additions & 1 deletion tardisnuclear/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@
# ----------------------------------------------------------------------------

from tardisnuclear.ejecta import Ejecta
from tardisnuclear.nuclear_data import NuclearData
from tardisnuclear.nuclear_data import NuclearData

import logging

logger = logging.getLogger('tardisnuclear')
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
16 changes: 12 additions & 4 deletions tardisnuclear/ejecta.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,20 @@ def _pad_material(self):
except KeyError:
self.material[isotope] = 0.0

def decay(self, epochs):
"""
Decay the ejecta material
Parameters
----------
epochs: numpy or quantity array
def decay(self, time):
new_material= self.material.decay(time.to(u.s).value)
return self.__class__(self.mass_g / msun_to_cgs, new_material)
Returns
-------
: ~pd.DataFrame
def decay_epochs(self, epochs):
"""
epochs = u.Quantity(epochs, u.day)
isotope_children = self.get_all_children()
columns = self.get_all_children_nuc_name()
Expand Down
31 changes: 24 additions & 7 deletions tardisnuclear/io/nndc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import bs4
import pandas as pd
import numpy as np

from astropy import units as u

#getting the data_path
Expand Down Expand Up @@ -37,9 +37,9 @@ def download_decay_radiation(nuclear_string):
nuclear_string = _sanitize_nuclear_string(nuclear_string)
base_url = ('http://www.nndc.bnl.gov/chart/decaysearchdirect.jsp?'
'nuc={nucname}&unc=nds')

nuclear_bs = bs4.BeautifulSoup(urllib2.urlopen(
base_url.format(nucname=nuclear_string.upper())))
data_url = base_url.format(nucname=nuclear_string.upper())
logger.info('Downloading data from {0}'.format(data_url))
nuclear_bs = bs4.BeautifulSoup(urllib2.urlopen(data_url))

data_list = nuclear_bs.find_all('u')
if data_list == []:
Expand Down Expand Up @@ -94,7 +94,7 @@ def store_decay_radiation_from_ejecta(ejecta, force_update=False):

def store_decay_radiation(nuclear_string, force_update=False):
nuclear_string = _sanitize_nuclear_string(nuclear_string)
fname = os.path.join(data_path, 'decay_radiation.h5')
fname = _get_nuclear_database_path()
with pd.HDFStore(fname, mode='a') as ds:
if nuclear_string in ds and not force_update:
raise IOError('{0} is already in the database '
Expand All @@ -118,7 +118,17 @@ def store_decay_radiation(nuclear_string, force_update=False):

def get_decay_radiation(nuclear_string, data_set_idx=0):
nuclear_string = _sanitize_nuclear_string(nuclear_string)
fname = os.path.join(data_path, 'decay_radiation.h5')
fname = _get_nuclear_database_path()

if not os.path.exists(fname):
if (not raw_input('{0} not in database - download [Y/n]'.format(
nuclear_string)).lower() == 'n'):
store_decay_radiation(nuclear_string)
else:
raise ValueError('{0} not in database'.format(
nuclear_string))


with pd.HDFStore(fname, mode='r') as ds:
current_keys = [key for key in ds.keys()
if key.startswith('/{0}/data_set{1:d}'.format(
Expand All @@ -129,7 +139,14 @@ def get_decay_radiation(nuclear_string, data_set_idx=0):
nuclear_string))
return {}
else:
raise ValueError('{0} not in database'.format(nuclear_string))
if (not raw_input(
'{0} not in database - download [Y/n]'.format(
nuclear_string)).lower() == 'n'):
ds.close()
store_decay_radiation(nuclear_string)
else:
raise ValueError('{0} not in database'.format(
nuclear_string))
data_set = {}
for key in current_keys:
data_set[key.split('/')[-1]] = ds[key]
Expand Down

0 comments on commit f0de577

Please sign in to comment.