Minor parse_1D code refactoring

This commit is contained in:
usov_i 2023-01-31 13:49:50 +01:00
parent 32aba1e75d
commit 3458a6c755

View File

@ -104,38 +104,41 @@ def parse_1D(fileobj, data_type):
# read metadata # read metadata
for line in fileobj: for line in fileobj:
if "=" in line: if "#data" in line:
variable, value = line.split("=", 1) # this is the end of metadata and the start of data section
variable = variable.strip() break
if "=" not in line:
# skip comments / empty lines
continue
var_name, value = line.split("=", 1)
var_name = var_name.strip()
value = value.strip() value = value.strip()
try: try:
if variable in META_VARS_STR: if var_name in META_VARS_STR:
metadata[variable] = value metadata[var_name] = value
elif variable in META_VARS_FLOAT: elif var_name in META_VARS_FLOAT:
if variable == "2-theta": # fix that angle name not to be an expression if var_name == "2-theta": # fix that angle name not to be an expression
variable = "twotheta" var_name = "twotheta"
if variable in ("a", "b", "c", "alpha", "beta", "gamma"): if var_name in ("a", "b", "c", "alpha", "beta", "gamma"):
variable += "_cell" var_name += "_cell"
metadata[variable] = float(value) metadata[var_name] = float(value)
elif variable in META_UB_MATRIX: elif var_name in META_UB_MATRIX:
if variable == "UB": if var_name == "UB":
metadata["ub"] = np.array(literal_eval(value)).reshape(3, 3) metadata["ub"] = np.array(literal_eval(value)).reshape(3, 3)
else: else:
if "ub" not in metadata: if "ub" not in metadata:
metadata["ub"] = np.zeros((3, 3)) metadata["ub"] = np.zeros((3, 3))
row = int(variable[-2]) - 1 row = int(var_name[-2]) - 1
metadata["ub"][row, :] = list(map(float, value.split())) metadata["ub"][row, :] = list(map(float, value.split()))
except Exception: except Exception:
print(f"Error reading {variable} with value '{value}'") print(f"Error reading {var_name} with value '{value}'")
metadata[variable] = 0 metadata[var_name] = 0
if "#data" in line:
# this is the end of metadata and the start of data section
break
# handle older files that don't contain "zebra_mode" metadata # handle older files that don't contain "zebra_mode" metadata
if "zebra_mode" not in metadata: if "zebra_mode" not in metadata: