From fbce5de7b90b1ebfcc82b8f27a9afb78530da2a8 Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Mon, 11 Apr 2022 15:16:37 +0200 Subject: [PATCH] Handle default cfl file --- pyzebra/app/panel_ccl_prepare.py | 36 +++++++++++------- pyzebra/sxtal_refgen.py | 65 ++++++++++++++++++++++++++++---- 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/pyzebra/app/panel_ccl_prepare.py b/pyzebra/app/panel_ccl_prepare.py index 4b51b5c..688a0de 100644 --- a/pyzebra/app/panel_ccl_prepare.py +++ b/pyzebra/app/panel_ccl_prepare.py @@ -39,22 +39,17 @@ def create(): chinu_ti.value = " ".join(ang_lims["chi"][:2]) phi_ti.value = " ".join(ang_lims["phi"][:2]) - def geom_radiogroup_callback(_attr, _old, new): - if new == 0: - geom_file = pyzebra.get_zebraBI_default_geom_file() - else: - geom_file = pyzebra.get_zebraNB_default_geom_file() - - _update_ang_lims(pyzebra.read_ang_limits(geom_file)) - - geom_radiogroup_div = Div(text="Geometry:") - geom_radiogroup = RadioGroup(labels=["bisecting", "normal beam"], width=150) - geom_radiogroup.on_change("active", geom_radiogroup_callback) - geom_radiogroup.active = 0 + def _update_params(params): + wavelen_input.value = float(params["WAVE"]) + cryst_space_group.value = params["SPGR"] + cryst_cell.value = params["CELL"] + ub_matrix.value = " ".join(params["UBMAT"]) + ranges_hkl.value = params["HLIM"] + ranges_expression.value = params["SRANG"] def open_geom_callback(_attr, _old, new): with io.StringIO(base64.b64decode(new).decode()) as geom_file: - _update_ang_lims(pyzebra.read_ang_limits(geom_file)) + _update_ang_lims(pyzebra.read_geom_file(geom_file)) open_geom_div = Div(text="or open GEOM:") open_geom = FileInput(accept=".geom", width=200) @@ -99,6 +94,21 @@ def create(): ranges_hkl = TextInput(title="HKL", value="-25 25 -25 25 -25 25") ranges_expression = TextInput(title="sin(​θ​)/λ", value="0.0 0.7", width=200) + def geom_radiogroup_callback(_attr, _old, new): + if new == 0: + geom_file = pyzebra.get_zebraBI_default_geom_file() + else: + geom_file = pyzebra.get_zebraNB_default_geom_file() + cfl_file = pyzebra.get_zebra_default_cfl_file() + + _update_ang_lims(pyzebra.read_geom_file(geom_file)) + _update_params(pyzebra.read_cfl_file(cfl_file)) + + geom_radiogroup_div = Div(text="Geometry:") + geom_radiogroup = RadioGroup(labels=["bisecting", "normal beam"], width=150) + geom_radiogroup.on_change("active", geom_radiogroup_callback) + geom_radiogroup.active = 0 + mag_struct_div = Div(text="Magnetic structure (optional):") mag_struct_lattice = TextInput(title="lattice", width=150) mag_struct_kvec = TextAreaInput(title="k vector", width=150) diff --git a/pyzebra/sxtal_refgen.py b/pyzebra/sxtal_refgen.py index 776075c..9e2624f 100644 --- a/pyzebra/sxtal_refgen.py +++ b/pyzebra/sxtal_refgen.py @@ -46,6 +46,35 @@ ANG_LIMITS Min Max Offset DET_OFF 0 0 0 """ +_zebra_default_cfl = """TITLE mymaterial +SPGR P 63 2 2 +CELL 5.73 5.73 11.89 90 90 120 + +WAVE 1.383 + +UBMAT +0.000000 0.000000 0.084104 +0.000000 0.174520 -0.000000 +0.201518 0.100759 0.000000 + +INSTR zebra.geom + +ORDER 1 2 3 + +ANGOR gamma + +HLIM -25 25 -25 25 -25 25 +SRANG 0.0 0.7 + +Mag_Structure +lattiCE P 1 +kvect 0.0 0.0 0.0 +magcent +symm x,y,z +msym u,v,w, 0.0 +End_Mag_Structure +""" + def get_zebraBI_default_geom_file(): return io.StringIO(_zebraBI_default_geom) @@ -55,7 +84,11 @@ def get_zebraNB_default_geom_file(): return io.StringIO(_zebraNB_default_geom) -def read_ang_limits(fileobj): +def get_zebra_default_cfl_file(): + return io.StringIO(_zebra_default_cfl) + + +def read_geom_file(fileobj): ang_lims = dict() # locate angular limits in .geom text file for line in fileobj: @@ -73,7 +106,7 @@ def read_ang_limits(fileobj): return ang_lims -def export_geom(path, ang_lims): +def export_geom_file(path, ang_lims): if "chi" in ang_lims: # BI geometry default_file = get_zebraBI_default_geom_file() n_ang = 4 @@ -97,9 +130,9 @@ def calc_ub_matrix(params): with tempfile.TemporaryDirectory() as temp_dir: cfl_file = os.path.join(temp_dir, "ub_matrix.cfl") - with open(cfl_file, "w") as f: + with open(cfl_file, "w") as fileobj: for key, value in params.items(): - f.write(f"{key} {value}\n") + fileobj.write(f"{key} {value}\n") comp_proc = subprocess.run( [SXTAL_REFGEN_PATH, cfl_file], @@ -114,12 +147,30 @@ def calc_ub_matrix(params): sfa_file = os.path.join(temp_dir, "ub_matrix.sfa") ub_matrix = [] - with open(sfa_file, "r") as f: - for line in f: + with open(sfa_file, "r") as fileobj: + for line in fileobj: if "BL_M" in line: # next 3 lines contain the matrix for _ in range(3): - next_line = next(f) + next_line = next(fileobj) *vals, _ = next_line.split(maxsplit=3) ub_matrix.extend(vals) return ub_matrix + + +def read_cfl_file(fileobj): + params = {"SPGR": None, "CELL": None, "WAVE": None, "UBMAT": None, "HLIM": None, "SRANG": None} + param_names = tuple(params) + for line in fileobj: + if line.startswith(param_names): + if line.startswith("UBMAT"): # next 3 lines contain the matrix + param, val = "UBMAT", [] + for _ in range(3): + next_line = next(fileobj) + val.extend(next_line.split(maxsplit=2)) + else: + param, val = line.split(maxsplit=1) + + params[param] = val + + return params