Fix seek() for Python 3 and only read in requested table in ace.get_table()

This commit is contained in:
amandalund 2019-02-22 20:11:03 -06:00
parent ef2767a127
commit f6097dbe9e

View file

@ -15,7 +15,6 @@ generates ACE-format cross sections.
"""
from os import SEEK_CUR
from pathlib import PurePath
import struct
import sys
@ -185,16 +184,14 @@ def get_table(filename, name=None):
"""
lib = Library(filename)
if name is None:
return lib.tables[0]
return Library(filename).tables[0]
else:
for table in lib.tables:
if table.name == name:
return table
lib = Library(filename, name)
if lib.tables:
return lib.tables[0]
else:
raise ValueError('Could not find ACE table with name: {}'
.format(name))
raise ValueError(f'Could not find ACE table with name: {name}')
class Library(EqualityMixin):
@ -240,7 +237,6 @@ class Library(EqualityMixin):
# No exception so proceed with ASCII - reopen in non-binary
fh.close()
with open(filename, 'r') as fh:
fh.seek(0)
self._read_ascii(fh, table_names, verbose)
except UnicodeDecodeError:
fh.close()
@ -378,26 +374,21 @@ class Library(EqualityMixin):
nxs = np.fromstring(datastr, sep=' ', dtype=int)
n_lines = (nxs[1] + 3)//4
n_bytes = len(lines[-1]) * (n_lines - 2) + 1
# Ensure that we have more tables to read in
if (table_names is not None) and (table_names < tables_seen):
if (table_names is not None) and (table_names <= tables_seen):
break
tables_seen.add(name)
# verify that we are suppossed to read this table in
# verify that we are supposed to read this table in
if (table_names is not None) and (name not in table_names):
ace_file.seek(n_bytes, SEEK_CUR)
ace_file.readline()
for i in range(n_lines - 1):
ace_file.readline()
lines = [ace_file.readline() for i in range(13)]
continue
# read and fix over-shoot
lines += ace_file.readlines(n_bytes)
if 12 + n_lines < len(lines):
goback = sum([len(line) for line in lines[12+n_lines:]])
lines = lines[:12+n_lines]
ace_file.seek(-goback, SEEK_CUR)
# Read lines corresponding to this table
lines += [ace_file.readline() for i in range(n_lines - 1)]
if verbose:
kelvin = round(temperature * 1e6 / 8.617342e-5)