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

Fix TOPAS reader #687

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions pymchelper/input_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ def guess_reader(filename):
:return: Instantiated reader object
"""
reader = None
logging.debug("Guessing reader for file %s", filename)
logging.debug("Checking FlukaReader")
fluka_reader = FlukaReaderFactory(filename).get_reader()
logging.debug("Got: %s", fluka_reader)
if fluka_reader:
logging.debug("Got FlukaReader")
reader = fluka_reader(filename)
else:
logging.debug("Checking SHReader")
sh_reader = SHReaderFactory(filename).get_reader()
logging.debug("Got: %s", sh_reader)
if sh_reader:
reader = sh_reader(filename)
try:
reader = sh_reader(filename)
reader.read_header(Estimator())
except ValueError as e:
topas_reader = TopasReaderFactory(filename).get_reader()
if topas_reader:
reader = topas_reader(filename)
else:
topas_reader = TopasReaderFactory(filename).get_reader()
if topas_reader:
Expand All @@ -55,7 +67,9 @@ def guess_corename(filename):
def fromfile(filename: str) -> Optional[Estimator]:
"""Read estimator data from a binary file ```filename```"""

logger.debug("Reading file %s, guessing reader", filename)
reader = guess_reader(filename)
logger.debug("Reader: %s", reader)
if reader is None:
raise Exception("File format not compatible", filename)
estimator = Estimator()
Expand Down Expand Up @@ -140,8 +154,9 @@ def fromfilelist(input_file_list, error: ErrorEstimate = ErrorEstimate.stderr, n

result.file_counter = len(input_file_list)
core_names_dict = group_input_files(input_file_list)
if len(core_names_dict) == 1:
if len(core_names_dict) == 1 and getattr(result, 'file_corename', None) is None:
result.file_corename = list(core_names_dict)[0]
print("here result.file_corename", result.file_corename)

return result

Expand Down
2 changes: 1 addition & 1 deletion pymchelper/readers/shieldhit/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_reader(self):
# format tag specifying binary standard was introduced in SH12A v0.7.4-dev on 07.06.2019 (commit 6eddf98)
file_format = read_token(self.filename, SHBDOTagID.format)
if file_format:
logger.debug("File format: {} {:s}".format(file_format, SHFileFormatId(file_format).name))
logger.debug("File format: %s %s", file_format, SHFileFormatId(file_format).name)
if file_format == SHFileFormatId.bdo2019:
reader = SHReaderBDO2019
elif file_format == SHFileFormatId.bdo2016:
Expand Down
26 changes: 14 additions & 12 deletions pymchelper/readers/shieldhit/reader_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from pymchelper.axis import MeshAxis
from pymchelper.estimator import Estimator
from pymchelper.readers.common import Reader
from pymchelper.shieldhit.detector.detector_type import SHDetType
from pymchelper.shieldhit.detector.estimator_type import SHGeoType
Expand All @@ -16,7 +17,11 @@ class SHReader(Reader):
Reads binary output files generated by SHIELD-HIT12A code.
"""

def read_data(self, estimator, nscale=1):
def read_header(self, _) -> str:
"""Reads header of the file and returns True if header is valid."""
return True

def read_data(self, estimator: Estimator, nscale: int = 1) -> bool:
"""
TODO
:param estimator:
Expand All @@ -27,12 +32,12 @@ def read_data(self, estimator, nscale=1):
return True

@property
def corename(self):
def corename(self) -> str:
"""
TODO
:return:
"""
core_name = None
core_name = ''

if self.filename.endswith(('.bdo', '.bdox')): # TODO add more sophisticated check for file being SH12A output
basename = os.path.basename(self.filename)
Expand Down Expand Up @@ -64,8 +69,9 @@ def mesh_unit_and_name(estimator, axis):

unit = _geotyp_units.get(estimator.geotyp, _default_units)[axis]

if estimator.geotyp in {SHGeoType.msh, SHGeoType.dmsh, SHGeoType.voxscore, SHGeoType.geomap,
SHGeoType.plane, SHGeoType.dplane}:
if estimator.geotyp in {
SHGeoType.msh, SHGeoType.dmsh, SHGeoType.voxscore, SHGeoType.geomap, SHGeoType.plane, SHGeoType.dplane
}:
name = ("Position (X)", "Position (Y)", "Position (Z)")[axis]
elif estimator.geotyp in {SHGeoType.cyl, SHGeoType.dcyl}:
name = ("Radius (R)", "Angle (PHI)", "Position (Z)")[axis]
Expand All @@ -86,7 +92,7 @@ def mesh_unit_and_name(estimator, axis):
return unit, name


def _bintyp(n):
def _bintyp(n: int):
"""
Calculates type of binning based on number of bins.

Expand Down Expand Up @@ -161,9 +167,7 @@ def read_next_token(f):
f is an open and readable file pointer.
returns None if no token was found / EOF
"""
tag = np.dtype([('pl_id', '<u8'),
('pl_type', 'S8'),
('pl_len', '<u8')])
tag = np.dtype([('pl_id', '<u8'), ('pl_type', 'S8'), ('pl_len', '<u8')])

x1 = np.fromfile(f, dtype=tag, count=1) # read the data into numpy

Expand All @@ -174,9 +178,7 @@ def read_next_token(f):
pl_type = x1['pl_type'][0]
pl_len = x1['pl_len'][0]
try:
pl = np.fromfile(f,
dtype=pl_type,
count=pl_len) # read the data into numpy
pl = np.fromfile(f, dtype=pl_type, count=pl_len) # read the data into numpy
return pl_id, pl_type, pl_len, pl
except TypeError:
return None
Expand Down
Loading