Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config/ascii file reader #67

Merged
merged 3 commits into from
Jan 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tardis/io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#readin model_data
from tardis.io.model_reader import read_simple_ascii_density, read_simple_ascii_abundances
61 changes: 61 additions & 0 deletions tardis/io/model_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#reading different model files
from numpy import recfromtxt, genfromtxt
import pandas as pd

from tardis import config_reader

def read_simple_ascii_density(fname):
"""
Reading a density file of the following structure (example; lines starting with a hash will be ignored):
5 s
#index velocity [km/s] density [g/cm^3]
0 53e5 1.6e-

Parameters
----------

fname: str
filename or path with filename


Returns
-------

time_of_model: ~astropy.units.Quantity
time at which the model is valid

data: ~pandas.DataFrame
data frame containing index, velocity (in km/s) and density
"""

with open(fname) as fh:
time_of_model_string = fh.readline().strip()
time_of_model = config_reader.parse_quantity(time_of_model_string)

data = recfromtxt(fname, skip_header=1, names=('index', 'velocity', 'density'), dtype=(int, float, float))
data = pd.DataFrame(data)

return time_of_model, data


def read_simple_ascii_abundances(fname):
"""
Reading an abundance file of the following structure (example; lines starting with hash will be ignored):

#index element1, element2, ..., element30
0 0.4 0.3, .. 0.2

Parameters
----------

fname: str
filename or path with filename

Returns
-------

data: ~pandas.DataFrame
data frame containing index, element1 - element30
"""
data = pd.DataFrame(recfromtxt(fname))
return data
4 changes: 4 additions & 0 deletions tardis/io/setup_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

def get_package_data():
return {'tardis.io.tests':['*.dat']}
Empty file added tardis/io/tests/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tardis/io/tests/tardis_simple_ascii_density_test.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1 day
# index velocity (km/s) density (g/cm^3)
0 10000 1e9
1 1.1e4 2e9
2 1.2e4 3e9
3 13000.0 4e9
4 14000 5e9
5 15000 4e9
6 1.6e4 3e9
7 1.7e4 3.3e9
8 1.8e4 2.9e9
9 1.9e4 1e9
20 changes: 20 additions & 0 deletions tardis/io/tests/test_ascii_readers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
from astropy import units as u
from tardis import io
from numpy.testing import assert_almost_equal

test_data_directory = os.path.dirname(__file__)

def test_simple_ascii_density_reader():
time_model, data = io.read_simple_ascii_density(os.path.join(test_data_directory,
'tardis_simple_ascii_density_test.dat'))

assert time_model.unit.physical_type == 'time'
assert_almost_equal(time_model.to(u.day).value, 1.0)

assert data['index'][0] == 0

assert_almost_equal(data['velocity'][3], 1.3e4)