From cc3f1d28a5670dad6fdaec83a19edbee0b1a09ce Mon Sep 17 00:00:00 2001 From: JakHolzer <53743814+JakHolzer@users.noreply.github.com> Date: Tue, 15 Sep 2020 15:17:00 +0200 Subject: [PATCH] Update load_1D.py Corrected the dat beaking bug --- pyzebra/load_1D.py | 140 ++++++++++++++++++++++++--------------------- 1 file changed, 76 insertions(+), 64 deletions(-) diff --git a/pyzebra/load_1D.py b/pyzebra/load_1D.py index 23c984b..215270c 100644 --- a/pyzebra/load_1D.py +++ b/pyzebra/load_1D.py @@ -2,57 +2,6 @@ import re import numpy as np from decimal import Decimal -META_VARS_STR = ( - "instrument", - "title", - "sample", - "user", - "ProposalID", - "original_filename", - "date", - "zebra_mode", - "proposal", - "proposal_user", - "proposal_title", - "proposal_email", -) -META_VARS_FLOAT = ( - "mf", - "2-theta", - "chi", - "phi", - "nu", - "temp", - "wavelenght", - "a", - "b", - "c", - "alpha", - "beta", - "gamma", - "cex1", - "cex2", - "mexz", - "moml", - "mcvl", - "momu", - "mcvu", - "detectorDistance", - "snv", - "snh", - "snvm", - "snhm", - "s1vt", - "s1vb", - "s1hr", - "s1hl", - "s2vt", - "s2vb", - "s2hr", - "s2hl", -) -META_UB_MATRIX = ("ub1j", "ub2j", "ub3j") - def load_1D(filepath): """ @@ -63,23 +12,82 @@ def load_1D(filepath): :arg filepath :returns det_variables - dictionary of all detector/scan variables and dictinionary for every measurement. - Names of these dictionaries are M + measurement number. They include HKL indeces, angles, - monitors, stepsize and array of counts + Names of these dictionaries are M + measurement number. They include HKL indeces, angles, monitors, + stepsize and array of counts """ + det_variables = {"file_type": str(filepath)[-3:], "meta": {}} + meta_vars_str = ( + "instrument", + "title", + "sample", + "user", + "ProposalID", + "original_filename", + "date", + "zebra_mode", + "proposal", + "proposal_user", + "proposal_title", + "proposal_email", + ) + meta_vars_float = ( + "mf", + "2-theta", + "chi", + "phi", + "nu", + "temp", + "wavelenght", + "a", + "b", + "c", + "alpha", + "beta", + "gamma", + "cex1", + "cex2", + "mexz", + "moml", + "mcvl", + "momu", + "mcvu", + "detectorDistance", + "snv", + "snh", + "snvm", + "snhm", + "s1vt", + "s1vb", + "s1hr", + "s1hl", + "s2vt", + "s2vb", + "s2hr", + "s2hl", + ) + meta_ub_matrix = ("ub1j", "ub2j", "ub3j") + with open(filepath, "r") as infile: for line in infile: det_variables["Measurements"] = {} if "=" in line: variable, value = line.split("=") variable = variable.strip() - if variable in META_VARS_FLOAT: - det_variables["meta"][variable] = float(value) - elif variable in META_VARS_STR: - det_variables["meta"][variable] = str(value)[:-1].strip() - elif variable in META_UB_MATRIX: - det_variables["meta"][variable] = re.findall(r"[-+]?\d*\.\d+|\d+", str(value)) - + try: + if variable in meta_vars_float: + det_variables["meta"][variable] = float(value) + elif variable in meta_vars_str: + det_variables["meta"][variable] = str(value)[:-1].strip() + elif variable in meta_ub_matrix: + det_variables["meta"][variable] = re.findall( + r"[-+]?\d*\.\d+|\d+", str(value) + ) + except ValueError as error: + print( + "Some values are not in expected format (str or float), error:", + str(error), + ) elif "#data" in line: if det_variables["file_type"] == "ccl": decimal = list() @@ -111,6 +119,7 @@ def load_1D(filepath): d["gamma_angle"] = float(lines.split()[4]) # gamma d["omega_angle"] = float(lines.split()[5]) # omega d["nu_angle"] = float(lines.split()[6]) # nu + d["unkwn_angle"] = float(lines.split()[7]) next_line = data[position + 1] d["number_of_measurements"] = int(next_line.split()[0]) @@ -137,6 +146,12 @@ def load_1D(filepath): ) d["counts"] = counts det_variables["Measurements"][str("M" + str(measurement_number))] = d + if all(decimal): + det_variables["meta"]["indices"] = "hkl" + else: + det_variables["meta"]["indices"] = "real" + print("indeces:", det_variables["meta"]["indices"]) + elif det_variables["file_type"] == "dat": data = infile.readlines() @@ -162,9 +177,6 @@ def load_1D(filepath): det_variables["Measurements"]["time"] = time else: print("Unknown file extention") - if all(decimal): - det_variables["meta"]["indices"] = "hkl" - else: - det_variables["meta"]["indices"] = "real" return det_variables +