Skip to content

Commit

Permalink
scrupt to generate processed_data for resolution calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing Li committed Oct 28, 2024
1 parent 31110d0 commit c79d31d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
66 changes: 66 additions & 0 deletions scripts/scans_rez.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from tavi.data.nxdict import NXdataset, NXentry
from tavi.data.nxentry import NexusEntry
from tavi.data.tavi import TAVI
from tavi.instrument.resolution.cooper_nathans import CN
from tavi.sample.xtal import Xtal

instrument_config_json_path = "./src/tavi/instrument/instrument_params/cg4c.json"
tas = CN(SPICE_CONVENTION=False)
tas.load_instrument_params_from_json(instrument_config_json_path)

sample_json_path = "./test_data/test_samples/nitio3.json"
sample = Xtal.from_json(sample_json_path)
tas.mount_sample(sample)

tavi = TAVI()
tavi.load_spice_data_from_disk("./test_data/exp424")

scan_list = list(range(42, 49, 1)) + list(range(70, 76, 1))

for scan_num in scan_list:
scan = tavi.get_scan(scan_num)

scan_data = scan.get_data(axes=("en", "detector"))

ei_list = scan.data["ei"]
ef_list = scan.data["ef"]
qh_list = scan.data["qh"]
qk_list = scan.data["qk"]
ql_list = scan.data["ql"]

projection = ((1, 1, 0), (0, 0, 1), (1, -1, 0))
R0 = True

rez_mat_list = []
rez_r0_list = []

for i in range(len(ei_list)):

rez = tas.cooper_nathans(
ei=ei_list[i],
ef=ef_list[i],
hkl=(qh_list[i], qk_list[i], ql_list[i]),
projection=projection,
R0=R0,
)
rez_mat_list.append(rez.mat)
rez_r0_list.append(rez.r0)

rez_entry = NXentry(
qh=NXdataset(ds=qh_list),
qk=NXdataset(ds=qk_list),
ql=NXdataset(ds=ql_list),
ei=NXdataset(ds=ei_list),
ef=NXdataset(ds=ef_list),
detector=NXdataset(ds=scan_data.y),
err=NXdataset(ds=scan_data.err),
rez_mat=NXdataset(ds=rez_mat_list),
rez_r0=NXdataset(ds=rez_r0_list),
projection=NXdataset(ds=projection),
)

tavi.processed_data.update(NexusEntry._dict_to_nexus_entry({scan.name: rez_entry}))


file_path = "./test_data/tavi_rez.h5"
tavi.save(file_path)
15 changes: 11 additions & 4 deletions src/tavi/data/nxentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ def to_nexus(self, path_to_nexus: str, name="scan") -> None:
scan_grp_data[def_x].attrs["target"] = path_x

# Create the ATTRIBUTES
scan_grp.attrs["file_name"] = os.path.abspath(path_to_nexus)
scan_grp.attrs["file_time"] = datetime.now().isoformat()
scan_grp.attrs["h5py_version"] = h5py.version.version
scan_grp.attrs["HDF5_Version"] = h5py.version.hdf5_version
nexus_file.attrs["file_name"] = os.path.abspath(path_to_nexus)
nexus_file.attrs["file_time"] = datetime.now().isoformat()
nexus_file.attrs["h5py_version"] = h5py.version.version
nexus_file.attrs["HDF5_Version"] = h5py.version.hdf5_version

def get(self, key, ATTRS=False, default=None):
"""
Expand All @@ -273,8 +273,15 @@ def get(self, key, ATTRS=False, default=None):
for log in ("SPICElogs", "DASlogs"):
if log in self.keys():
self.pop(log)

# special treatment for annoying cases!!!
if (key == "detector") and (not ATTRS):
key = "detector/data"
if (key == "monitor") and (not ATTRS):
key = "monitor/monitor"
if (key == "sample") and (not ATTRS):
key = "sample/sample"

value = NexusEntry._getitem_recursively(self, key, ATTRS)

return value if value is not None else default
Expand Down
4 changes: 4 additions & 0 deletions src/tavi/data/tavi.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def save(self, file_path: Optional[str] = None):
nxentry.to_nexus(file_path, name=name)
# TODO save pdata, fits and plots
grp_pdata = root.create_group("processed_data", track_order=True)
for entry_name, nxentry in self.processed_data.items():
name = f"processed_data/{entry_name}"
nxentry.to_nexus(file_path, name=name)

grp_fits = root.create_group("fits", track_order=True)
grp_plots = root.create_group("plots", track_order=True)
except OSError:
Expand Down
Binary file modified test_data/scan_to_nexus_test.h5
Binary file not shown.
Binary file added test_data/tavi_rez.h5
Binary file not shown.

0 comments on commit c79d31d

Please sign in to comment.