-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffractionDataHandling_class.py
More file actions
121 lines (99 loc) · 5.12 KB
/
Copy pathdiffractionDataHandling_class.py
File metadata and controls
121 lines (99 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from IPython.display import display
from ipyfilechooser import FileChooser
import h5py
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import IntSlider, Text, HBox, VBox, Output, Button, Layout, Label
class DiffractionDataHandling:
def __init__(self, dest_root_path=None, nxs_data_path=None, h5_data_path=None,in_path=None, ou_path=None, root_id=None):
self.in_path = in_path
self.ou_path = ou_path
self.root_id = root_id
self.nxs_data_path = nxs_data_path
self.h5_data_path = h5_data_path
self.dest_root_path = dest_root_path
self.KB_a = 1879.6183
self.KB_b = 51.5338
self.KB_c = 9950.2586
self.KB_d = -52.1046
self.KB_e = -2105.5378
self.KB_f = 2875.4338
if (self.root_id != None) and (self.in_path != None) and (self.ou_path != None):
self.nxs_data_path = self.in_path + '/' + self.root_id + '.nxs'
self.h5_data_path = self.in_path + '/' + self.root_id + '/' + self.root_id +'-gtsPanda.h5'
self.dest_root_path = self.ou_path + '/' + self.root_id + '/' + self.root_id + '-gtsPanda'
def load_nxs_data_path(self, chooser):
if chooser.selected:
self.nxs_data_path = chooser.selected
def load_h5_data_path(self, chooser):
if chooser.selected:
self.h5_data_path = chooser.selected
def load_dest_root_path(self, chooser):
if chooser.selected:
self.dest_root_path = chooser.selected
def InputOutput(self):
if self.nxs_data_path is None:
dfile_chooser = FileChooser()
dfile_chooser.title = 'Nexus Data:'
dfile_chooser.register_callback(self.load_nxs_data_path)
display(dfile_chooser)
if self.h5_data_path is None:
dfile_chooser = FileChooser()
dfile_chooser.title = 'h5 Data:'
dfile_chooser.register_callback(self.load_h5_data_path)
display(dfile_chooser)
if self.dest_root_path is None:
destfile_chooser = FileChooser()
destfile_chooser.title = 'Destination Root File Path:'
destfile_chooser.register_callback(self.load_dest_root_path)
display(destfile_chooser)
def ReadData(self):
with h5py.File(self.nxs_data_path, 'r') as f:
if not ('/entry/diffraction_sum/kb_cs_x' in f): return 1
if not ('/entry/diffraction_sum/kb_cs_y' in f): return 2
self.row_data_x = f['/entry/diffraction_sum/kb_cs_x'][()]
self.row_data_y = f['/entry/diffraction_sum/kb_cs_y'][()]
with h5py.File(self.h5_data_path, 'r') as f:
if not ('/entry/kb_x.value/kb_x.value' in f): return 3
if not ('/entry/kb_y.value/kb_y.value' in f): return 4
self.out_data_x = f['/entry/kb_x.value/kb_x.value'][()]
self.out_data_y = f['/entry/kb_y.value/kb_y.value'][()]
if isinstance(self.row_data_x, np.ndarray):
if len(self.out_data_x) != len(self.row_data_y): return 5
if len(self.out_data_x) > 1:
if len(self.out_data_x[0]) != len(self.row_data_x): return 6
for i in range(len(self.row_data_x)):
for j in range(len(self.row_data_y)):
kb_x, kb_y = self.CS_ConvertTo_KB(self.row_data_x[i],self.row_data_y[j])
if len(self.row_data_y) > 1 :
if len(self.row_data_x) > 1 :
self.out_data_x[j][i] = kb_x
self.out_data_y[j][i] = kb_y
else:
self.out_data_x[i] = kb_x
self.out_data_y[i] = kb_y
else:
self.out_data_x[j] = kb_x
self.out_data_y[j] = kb_y
else:
kb_x, kb_y = self.CS_ConvertTo_KB(self.row_data_x,self.row_data_y)
self.out_data_x[0] = kb_x
self.out_data_y[0] = kb_y
return 0
def WriteData(self):
with h5py.File(f'{self.dest_root_path}.h5', 'w') as f:
f.create_dataset('/entry/kb_x.value/kb_x.value', data=self.out_data_x)
f.create_dataset('/entry/kb_y.value/kb_y.value', data=self.out_data_y)
def CS_ConvertTo_KB(self, cs_x, cs_y):
kb_x = (self.KB_e*cs_x + self.KB_b*cs_y + self.KB_b*self.KB_f - self.KB_c*self.KB_e) / (self.KB_a*self.KB_e - self.KB_b*self.KB_d)
kb_y = (self.KB_a*cs_y + self.KB_d*cs_y + self.KB_c*self.KB_d - self.KB_a*self.KB_f) / (self.KB_a*self.KB_e - self.KB_b*self.KB_d)
return kb_x, kb_y
def DiffKBUpdate(self, root_id = None):
if root_id != None:
self.root_id = root_id
self.nxs_data_path = self.in_path + '/' + self.root_id + '.nxs'
self.h5_data_path = self.in_path + '/' + self.root_id + '/' + self.root_id +'-gtsPanda.h5'
self.dest_root_path = self.ou_path + '/' + self.root_id + '/' + self.root_id + '-gtsPanda'
self.InputOutput()
print(self.ReadData())
self.WriteData()