12 def find(bec=None, layer_disk=None, phi=None, eta=None, side=None, asdec=False, input_data="geometry.json", output_file=None):
15 with open(input_data)
as f:
16 if input_data.endswith(
".json"):
19 print(
"Unexpected input file or format.")
24 for ID, info
in data.items():
25 if bec
is not None and info[
"BEC"] !=
str(bec):
28 if layer_disk
is not None and info[
"LayerDisk"] !=
str(layer_disk):
31 if phi
is not None and info[
"PhiModule"]
not in str([i
for i
in phi]):
34 if eta
is not None and info[
"EtaModule"] >
str(eta):
37 if side
is not None and info[
"Side"] !=
str(side):
41 info[
"Decimal_ID"] =
str(
int(ID, 16))
45 with open(output_file,
"w")
as f:
46 selected_data = {ID: data[ID]
for ID
in IDs}
47 json.dump(selected_data, f, indent=4)
52 def merge(file_1="selected_modules_1.json", file_2="selected_modules_2.json", output_file=None):
57 with open(file_1)
as f1:
58 if file_1.endswith(
".json"):
61 print(
"Unexpected input file_1 or format.")
64 IDs.extend(data1.keys())
66 with open(file_2)
as f2:
67 if file_2.endswith(
".json"):
70 print(
"Unexpected input file_2 or format.")
73 IDs.extend(data2.keys())
75 with open(output_file,
"w")
as f3:
79 selected_data[ID] = data1[ID]
81 selected_data[ID] = data2[ID]
82 json.dump(selected_data, f3, indent=4)
89 with open(input_data)
as f:
90 if input_data.endswith(
".json"):
93 print(
"Unexpected input file or format.")
98 num =
int(
round(frac * len(data), 0))
99 IDs = random.sample(
list(data.keys()), k=num)
101 for ID, info
in data.items():
102 info[
"Decimal_ID"] =
str(
int(ID, 16))
104 with open(output_file,
"w")
as f:
105 selected_data = {ID: data[ID]
for ID
in IDs}
106 json.dump(selected_data, f, indent=4)
111 if __name__ ==
"__main__":
113 data =
find(bec = 0, layer_disk = 0, phi = [0,1,2,3,4], asdec =
True, output_file=
"test_selected_modules.json")