Handle default cfl file

This commit is contained in:
usov_i 2022-04-11 15:16:37 +02:00
parent 636badfba8
commit fbce5de7b9
2 changed files with 81 additions and 20 deletions

View File

@ -39,22 +39,17 @@ def create():
chinu_ti.value = " ".join(ang_lims["chi"][:2]) chinu_ti.value = " ".join(ang_lims["chi"][:2])
phi_ti.value = " ".join(ang_lims["phi"][:2]) phi_ti.value = " ".join(ang_lims["phi"][:2])
def geom_radiogroup_callback(_attr, _old, new): def _update_params(params):
if new == 0: wavelen_input.value = float(params["WAVE"])
geom_file = pyzebra.get_zebraBI_default_geom_file() cryst_space_group.value = params["SPGR"]
else: cryst_cell.value = params["CELL"]
geom_file = pyzebra.get_zebraNB_default_geom_file() ub_matrix.value = " ".join(params["UBMAT"])
ranges_hkl.value = params["HLIM"]
_update_ang_lims(pyzebra.read_ang_limits(geom_file)) ranges_expression.value = params["SRANG"]
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 open_geom_callback(_attr, _old, new): def open_geom_callback(_attr, _old, new):
with io.StringIO(base64.b64decode(new).decode()) as geom_file: 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_div = Div(text="or open GEOM:")
open_geom = FileInput(accept=".geom", width=200) 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_hkl = TextInput(title="HKL", value="-25 25 -25 25 -25 25")
ranges_expression = TextInput(title="sin(​θ​)/λ", value="0.0 0.7", width=200) 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_div = Div(text="Magnetic structure (optional):")
mag_struct_lattice = TextInput(title="lattice", width=150) mag_struct_lattice = TextInput(title="lattice", width=150)
mag_struct_kvec = TextAreaInput(title="k vector", width=150) mag_struct_kvec = TextAreaInput(title="k vector", width=150)

View File

@ -46,6 +46,35 @@ ANG_LIMITS Min Max Offset
DET_OFF 0 0 0 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(): def get_zebraBI_default_geom_file():
return io.StringIO(_zebraBI_default_geom) return io.StringIO(_zebraBI_default_geom)
@ -55,7 +84,11 @@ def get_zebraNB_default_geom_file():
return io.StringIO(_zebraNB_default_geom) 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() ang_lims = dict()
# locate angular limits in .geom text file # locate angular limits in .geom text file
for line in fileobj: for line in fileobj:
@ -73,7 +106,7 @@ def read_ang_limits(fileobj):
return ang_lims return ang_lims
def export_geom(path, ang_lims): def export_geom_file(path, ang_lims):
if "chi" in ang_lims: # BI geometry if "chi" in ang_lims: # BI geometry
default_file = get_zebraBI_default_geom_file() default_file = get_zebraBI_default_geom_file()
n_ang = 4 n_ang = 4
@ -97,9 +130,9 @@ def calc_ub_matrix(params):
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
cfl_file = os.path.join(temp_dir, "ub_matrix.cfl") 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(): for key, value in params.items():
f.write(f"{key} {value}\n") fileobj.write(f"{key} {value}\n")
comp_proc = subprocess.run( comp_proc = subprocess.run(
[SXTAL_REFGEN_PATH, cfl_file], [SXTAL_REFGEN_PATH, cfl_file],
@ -114,12 +147,30 @@ def calc_ub_matrix(params):
sfa_file = os.path.join(temp_dir, "ub_matrix.sfa") sfa_file = os.path.join(temp_dir, "ub_matrix.sfa")
ub_matrix = [] ub_matrix = []
with open(sfa_file, "r") as f: with open(sfa_file, "r") as fileobj:
for line in f: for line in fileobj:
if "BL_M" in line: # next 3 lines contain the matrix if "BL_M" in line: # next 3 lines contain the matrix
for _ in range(3): for _ in range(3):
next_line = next(f) next_line = next(fileobj)
*vals, _ = next_line.split(maxsplit=3) *vals, _ = next_line.split(maxsplit=3)
ub_matrix.extend(vals) ub_matrix.extend(vals)
return ub_matrix 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