parent
b0a4e35d3d
commit
c5ec09a5e3
@ -1,6 +1,6 @@
|
||||
from pyzebra.anatric import *
|
||||
from pyzebra.ccl_findpeaks import ccl_findpeaks
|
||||
from pyzebra.ccl_io import export_comm, load_1D, parse_1D
|
||||
from pyzebra.ccl_io import export_1D, load_1D, parse_1D
|
||||
from pyzebra.fit2 import fitccl
|
||||
from pyzebra.h5 import *
|
||||
from pyzebra.merge_function import add_dict, unified_merge
|
||||
|
@ -68,7 +68,10 @@ def create():
|
||||
det_data = {}
|
||||
fit_params = {}
|
||||
peak_pos_textinput_lock = False
|
||||
js_data = ColumnDataSource(data=dict(cont=[], ext=[]))
|
||||
js_data = {
|
||||
".comm": ColumnDataSource(data=dict(cont=[], ext=[])),
|
||||
".incomm": ColumnDataSource(data=dict(cont=[], ext=[])),
|
||||
}
|
||||
|
||||
def proposal_textinput_callback(_attr, _old, new):
|
||||
ccl_path = os.path.join(PROPOSAL_PATH, new.strip())
|
||||
@ -84,12 +87,7 @@ def create():
|
||||
|
||||
def _init_datatable():
|
||||
scan_list = list(det_data["scan"].keys())
|
||||
hkl = [
|
||||
f'{int(m["h_index"])} {int(m["k_index"])} {int(m["l_index"])}'
|
||||
if det_data["meta"]["indices"] == "hkl"
|
||||
else f'{m["h_index"]} {m["k_index"]} {m["l_index"]}'
|
||||
for m in det_data["scan"].values()
|
||||
]
|
||||
hkl = [f'{m["h_index"]} {m["k_index"]} {m["l_index"]}' for m in det_data["scan"].values()]
|
||||
scan_table_source.data.update(
|
||||
scan=scan_list,
|
||||
hkl=hkl,
|
||||
@ -511,26 +509,28 @@ def create():
|
||||
preview_output_textinput = TextAreaInput(title="Export file preview:", width=500, height=400)
|
||||
|
||||
def preview_output_button_callback():
|
||||
if det_data["meta"]["indices"] == "hkl":
|
||||
ext = ".comm"
|
||||
elif det_data["meta"]["indices"] == "real":
|
||||
ext = ".incomm"
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
temp_file = temp_dir + "/temp"
|
||||
export_data = deepcopy(det_data)
|
||||
for s, export in zip(scan_table_source.data["scan"], scan_table_source.data["export"]):
|
||||
if not export:
|
||||
del export_data["scan"][s]
|
||||
pyzebra.export_comm(
|
||||
|
||||
pyzebra.export_1D(
|
||||
export_data,
|
||||
temp_file,
|
||||
lorentz=lorentz_toggle.active,
|
||||
hkl_precision=int(hkl_precision_select.value),
|
||||
)
|
||||
|
||||
with open(f"{temp_file}{ext}") as f:
|
||||
preview_output_textinput.value = f.read()
|
||||
exported_content = ""
|
||||
for ext in (".comm", ".incomm"):
|
||||
fname = temp_file + ext
|
||||
if os.path.isfile(fname):
|
||||
with open(fname) as f:
|
||||
exported_content += f"{ext} file:\n" + f.read()
|
||||
|
||||
preview_output_textinput.value = exported_content
|
||||
|
||||
preview_output_button = Button(label="Preview file", default_size=200)
|
||||
preview_output_button.on_click(preview_output_button_callback)
|
||||
@ -539,37 +539,29 @@ def create():
|
||||
title="hkl precision:", options=["2", "3", "4"], value="2", default_size=80
|
||||
)
|
||||
|
||||
def export_results(det_data):
|
||||
if det_data["meta"]["indices"] == "hkl":
|
||||
ext = ".comm"
|
||||
elif det_data["meta"]["indices"] == "real":
|
||||
ext = ".incomm"
|
||||
|
||||
def save_button_callback():
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
temp_file = temp_dir + "/temp"
|
||||
export_data = deepcopy(det_data)
|
||||
for s, export in zip(scan_table_source.data["scan"], scan_table_source.data["export"]):
|
||||
if not export:
|
||||
del export_data["scan"][s]
|
||||
pyzebra.export_comm(
|
||||
export_data,
|
||||
temp_file,
|
||||
lorentz=lorentz_toggle.active,
|
||||
hkl_precision=int(hkl_precision_select.value),
|
||||
)
|
||||
|
||||
with open(f"{temp_file}{ext}") as f:
|
||||
output_content = f.read()
|
||||
pyzebra.export_1D(export_data, temp_file, lorentz=lorentz_toggle.active)
|
||||
|
||||
return output_content, ext
|
||||
|
||||
def save_button_callback():
|
||||
cont, ext = export_results(det_data)
|
||||
js_data.data.update(cont=[cont], ext=[ext])
|
||||
for ext in (".comm", ".incomm"):
|
||||
fname = temp_file + ext
|
||||
if os.path.isfile(fname):
|
||||
with open(fname) as f:
|
||||
cont = f.read()
|
||||
else:
|
||||
cont = ""
|
||||
js_data[ext].data.update(cont=[cont], ext=[ext])
|
||||
|
||||
save_button = Button(label="Download file", button_type="success", default_size=200)
|
||||
save_button.on_click(save_button_callback)
|
||||
save_button.js_on_click(CustomJS(args={"js_data": js_data}, code=javaScript))
|
||||
save_button.js_on_click(CustomJS(args={"js_data": js_data[".comm"]}, code=javaScript))
|
||||
save_button.js_on_click(CustomJS(args={"js_data": js_data[".incomm"]}, code=javaScript))
|
||||
|
||||
findpeak_controls = column(
|
||||
row(peak_pos_textinput, column(Spacer(height=19), smooth_toggle)),
|
||||
|
@ -77,7 +77,10 @@ def create():
|
||||
det_data = {}
|
||||
fit_params = {}
|
||||
peak_pos_textinput_lock = False
|
||||
js_data = ColumnDataSource(data=dict(cont=[], ext=[]))
|
||||
js_data = {
|
||||
".comm": ColumnDataSource(data=dict(cont=[], ext=[])),
|
||||
".incomm": ColumnDataSource(data=dict(cont=[], ext=[])),
|
||||
}
|
||||
|
||||
def proposal_textinput_callback(_attr, _old, new):
|
||||
full_proposal_path = os.path.join(PROPOSAL_PATH, new.strip())
|
||||
@ -612,51 +615,50 @@ def create():
|
||||
preview_output_textinput = TextAreaInput(title="Export file preview:", width=450, height=400)
|
||||
|
||||
def preview_output_button_callback():
|
||||
if det_data["meta"]["indices"] == "hkl":
|
||||
ext = ".comm"
|
||||
elif det_data["meta"]["indices"] == "real":
|
||||
ext = ".incomm"
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
temp_file = temp_dir + "/temp"
|
||||
export_data = deepcopy(det_data)
|
||||
for s, export in zip(scan_table_source.data["scan"], scan_table_source.data["export"]):
|
||||
if not export:
|
||||
del export_data["scan"][s]
|
||||
pyzebra.export_comm(export_data, temp_file, lorentz=lorentz_toggle.active)
|
||||
|
||||
with open(f"{temp_file}{ext}") as f:
|
||||
preview_output_textinput.value = f.read()
|
||||
pyzebra.export_1D(export_data, temp_file, lorentz=lorentz_toggle.active)
|
||||
|
||||
exported_content = ""
|
||||
for ext in (".comm", ".incomm"):
|
||||
fname = temp_file + ext
|
||||
if os.path.isfile(fname):
|
||||
with open(fname) as f:
|
||||
exported_content += f"{ext} file:\n" + f.read()
|
||||
|
||||
preview_output_textinput.value = exported_content
|
||||
|
||||
preview_output_button = Button(label="Preview file", default_size=220)
|
||||
preview_output_button.on_click(preview_output_button_callback)
|
||||
|
||||
def export_results(det_data):
|
||||
if det_data["meta"]["indices"] == "hkl":
|
||||
ext = ".comm"
|
||||
elif det_data["meta"]["indices"] == "real":
|
||||
ext = ".incomm"
|
||||
|
||||
def save_button_callback():
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
temp_file = temp_dir + "/temp"
|
||||
export_data = deepcopy(det_data)
|
||||
for s, export in zip(scan_table_source.data["scan"], scan_table_source.data["export"]):
|
||||
if not export:
|
||||
del export_data["scan"][s]
|
||||
pyzebra.export_comm(export_data, temp_file, lorentz=lorentz_toggle.active)
|
||||
|
||||
with open(f"{temp_file}{ext}") as f:
|
||||
output_content = f.read()
|
||||
pyzebra.export_1D(export_data, temp_file, lorentz=lorentz_toggle.active)
|
||||
|
||||
return output_content, ext
|
||||
|
||||
def save_button_callback():
|
||||
cont, ext = export_results(det_data)
|
||||
js_data.data.update(cont=[cont], ext=[ext])
|
||||
for ext in (".comm", ".incomm"):
|
||||
fname = temp_file + ext
|
||||
if os.path.isfile(fname):
|
||||
with open(fname) as f:
|
||||
cont = f.read()
|
||||
else:
|
||||
cont = ""
|
||||
js_data[ext].data.update(cont=[cont], ext=[ext])
|
||||
|
||||
save_button = Button(label="Download file", button_type="success", default_size=220)
|
||||
save_button.on_click(save_button_callback)
|
||||
save_button.js_on_click(CustomJS(args={"js_data": js_data}, code=javaScript))
|
||||
save_button.js_on_click(CustomJS(args={"js_data": js_data[".comm"]}, code=javaScript))
|
||||
save_button.js_on_click(CustomJS(args={"js_data": js_data[".incomm"]}, code=javaScript))
|
||||
|
||||
findpeak_controls = column(
|
||||
row(peak_pos_textinput, column(Spacer(height=19), smooth_toggle)),
|
||||
|
@ -208,13 +208,15 @@ def parse_1D(fileobj, data_type):
|
||||
print("Unknown file extention")
|
||||
|
||||
# utility information
|
||||
if all(
|
||||
s["h_index"].is_integer() and s["k_index"].is_integer() and s["l_index"].is_integer()
|
||||
for s in scan.values()
|
||||
):
|
||||
metadata["indices"] = "hkl"
|
||||
else:
|
||||
metadata["indices"] = "real"
|
||||
metadata["indices"] = []
|
||||
for s in scan.values():
|
||||
if s["h_index"].is_integer() and s["k_index"].is_integer() and s["l_index"].is_integer():
|
||||
s["h_index"] = int(s["h_index"])
|
||||
s["k_index"] = int(s["k_index"])
|
||||
s["l_index"] = int(s["l_index"])
|
||||
metadata["indices"].append("hkl")
|
||||
else:
|
||||
metadata["indices"].append("real")
|
||||
|
||||
metadata["data_type"] = data_type
|
||||
metadata["area_method"] = AREA_METHODS[0]
|
||||
@ -222,54 +224,55 @@ def parse_1D(fileobj, data_type):
|
||||
return {"meta": metadata, "scan": scan}
|
||||
|
||||
|
||||
def export_comm(data, path, lorentz=False, hkl_precision=2):
|
||||
"""exports data in the *.comm format
|
||||
:param lorentz: perform Lorentz correction
|
||||
:param path: path to file + name
|
||||
:arg data - data to export, is dict after peak fitting
|
||||
def export_1D(data, path, lorentz=False, hkl_precision=2):
|
||||
"""Exports data in the .comm/.incomm format
|
||||
|
||||
Scans with integer/real hkl values are saved in .comm/.incomm files correspondingly. If no scans
|
||||
are present for a particular output format, that file won't be created.
|
||||
"""
|
||||
zebra_mode = data["meta"]["zebra_mode"]
|
||||
if data["meta"]["indices"] == "hkl":
|
||||
extension = ".comm"
|
||||
else: # data["meta"]["indices"] == "real":
|
||||
extension = ".incomm"
|
||||
file_content = {".comm": [], ".incomm": []}
|
||||
|
||||
with open(str(path + extension), "w") as out_file:
|
||||
for key, scan in data["scan"].items():
|
||||
if "fit" not in scan:
|
||||
print("Scan skipped - no fit value for:", key)
|
||||
continue
|
||||
for (key, scan), indices in zip(data["scan"].items(), data["meta"]["indices"]):
|
||||
if "fit" not in scan:
|
||||
print("Scan skipped - no fit value for:", key)
|
||||
continue
|
||||
|
||||
scan_str = f"{key:6}"
|
||||
scan_str = f"{key:6}"
|
||||
|
||||
h, k, l = scan["h_index"], scan["k_index"], scan["l_index"]
|
||||
if data["meta"]["indices"] == "hkl":
|
||||
hkl_str = f"{int(h):6}{int(k):6}{int(l):6}"
|
||||
else: # data["meta"]["indices"] == "real"
|
||||
hkl_str = f"{h:8.{hkl_precision}f}{k:8.{hkl_precision}f}{l:8.{hkl_precision}f}"
|
||||
h, k, l = scan["h_index"], scan["k_index"], scan["l_index"]
|
||||
if indices == "hkl":
|
||||
hkl_str = f"{h:6}{k:6}{l:6}"
|
||||
else: # indices == "real"
|
||||
hkl_str = f"{h:8.{hkl_precision}f}{k:8.{hkl_precision}f}{l:8.{hkl_precision}f}"
|
||||
|
||||
area_method = data["meta"]["area_method"]
|
||||
area_n = scan["fit"][area_method].n
|
||||
area_s = scan["fit"][area_method].s
|
||||
area_method = data["meta"]["area_method"]
|
||||
area_n = scan["fit"][area_method].n
|
||||
area_s = scan["fit"][area_method].s
|
||||
|
||||
# apply lorentz correction to area
|
||||
if lorentz:
|
||||
if zebra_mode == "bi":
|
||||
twotheta_angle = np.deg2rad(scan["twotheta_angle"])
|
||||
corr_factor = np.sin(twotheta_angle)
|
||||
else: # zebra_mode == "nb":
|
||||
gamma_angle = np.deg2rad(scan["gamma_angle"])
|
||||
nu_angle = np.deg2rad(scan["nu_angle"])
|
||||
corr_factor = np.sin(gamma_angle) * np.cos(nu_angle)
|
||||
# apply lorentz correction to area
|
||||
if lorentz:
|
||||
if zebra_mode == "bi":
|
||||
twotheta_angle = np.deg2rad(scan["twotheta_angle"])
|
||||
corr_factor = np.sin(twotheta_angle)
|
||||
else: # zebra_mode == "nb":
|
||||
gamma_angle = np.deg2rad(scan["gamma_angle"])
|
||||
nu_angle = np.deg2rad(scan["nu_angle"])
|
||||
corr_factor = np.sin(gamma_angle) * np.cos(nu_angle)
|
||||
|
||||
area_n = np.abs(area_n * corr_factor)
|
||||
area_s = np.abs(area_s * corr_factor)
|
||||
area_n = np.abs(area_n * corr_factor)
|
||||
area_s = np.abs(area_s * corr_factor)
|
||||
|
||||
area_str = f"{area_n:10.2f}{area_s:10.2f}"
|
||||
area_str = f"{area_n:10.2f}{area_s:10.2f}"
|
||||
|
||||
ang_str = ""
|
||||
for angle, _ in CCL_ANGLES[zebra_mode]:
|
||||
ang_str = ang_str + f"{scan[angle]:8}"
|
||||
ang_str = ""
|
||||
for angle, _ in CCL_ANGLES[zebra_mode]:
|
||||
ang_str = ang_str + f"{scan[angle]:8}"
|
||||
|
||||
out_file.write(scan_str + hkl_str + area_str + ang_str + "\n")
|
||||
file_content_ref = file_content[".comm"] if indices == "hkl" else file_content[".incomm"]
|
||||
file_content_ref.append(scan_str + hkl_str + area_str + ang_str + "\n")
|
||||
|
||||
for ext, content in file_content.items():
|
||||
if content:
|
||||
with open(path + ext, "w") as out_file:
|
||||
out_file.writelines(content)
|
||||
|
Loading…
x
Reference in New Issue
Block a user