PAASS
Software suite to Acquire and Analyze Data from Pixie16
map_to_config.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 """
3 K. Miernik
4 2013
5 Generate basic <map> section for SHE DSSD for the new pixie_ldf_c Config.xml file
6 based on old style map.txt
7 
8 """
9 import xml.dom.minidom
10 
11 def test_load(data):
12  """Test how the data are loaded from map.txt file"""
13  for module in data:
14  print('Module: ', module['number'])
15  for channel in module['channels']:
16  print(' Ch: {} Loc: {} T: {} S: {}'.format(channel['number'],
17  channel['location'],
18  channel['type'],
19  channel['subtype']))
20  print(' cal: {}'.format(channel['calibration']))
21 
22 
23 if __name__ == '__main__':
24  data = []
25 
26  with open("map.txt") as map_file:
27  lineN = 0
28  module = None
29  for line in map_file:
30  lineN += 1
31  line = line.strip()
32  if line.startswith('%') or len(line) < 1:
33  continue
34  fields = line.split()
35  mod = fields[0]
36  ch = fields[1]
37  # Ignore raw channel number at [2]
38  det_type = fields[3]
39  det_subtype = fields[4]
40  loc = fields[5]
41  channel = {'number': ch,
42  'location' : loc,
43  'type' : det_type,
44  'subtype' : det_subtype}
45  if module is None:
46  module = {'number' : mod, 'channels' : []}
47  elif module['number'] != mod:
48  data.append(module)
49  module = {'number' : mod, 'channels' : []}
50  module['channels'].append(channel)
51 
52 
53  with open("cal.txt") as cal_file:
54  lineN = 0
55  for line in cal_file:
56  lineN += 1
57  line = line.strip()
58  if line.startswith('%') or len(line) < 1:
59  continue
60  fields = line.split()
61  loc = fields[0]
62  det_type = fields[1]
63  det_subtype = fields[2]
64  # Ignore number of calibrations at [3], order at [4],
65  # and threshold at [5]
66  # Notice that it will get only the linear factors for the
67  # first calibration
68  a0 = float(fields[6])
69  a1 = float(fields[7])
70  found = False
71  for module in data:
72  for channel in module['channels']:
73  if (channel['type'] == det_type and
74  channel['subtype'] == det_subtype and
75  channel['location'] == loc):
76  channel.update({'calibration' : [a0, a1]})
77  found = True
78  if found:
79  break
80  else:
81  print('Could not find channel: type {} subtype {} loc {}'.
82  format(det_type, det_subtype, loc))
83 
84  #test_load(data)
85 
86  out = open('out.xml', 'w')
87  dom = xml.dom.minidom.getDOMImplementation()
88  table = dom.createDocument(None, "Map", None)
89  root = table.documentElement
90 
91  for m in data:
92  module = table.createElement("Module")
93  module.setAttribute("number", m['number'])
94  for c in m['channels']:
95  channel = table.createElement("Channel")
96  # Force attriute number to be first (they are sorted alphabetically)
97  channel.setAttribute("aa_number", c['number'])
98  channel.setAttribute("type", c['type'])
99  channel.setAttribute("subtype", c['subtype'])
100  channel.setAttribute("location", c['location'])
101  calibration = table.createElement("Calibration")
102  calibration.setAttribute("model", "linear")
103  calibration.setAttribute("max", str(32000))
104  text_element = table.createTextNode('{} {}'.
105  format(c['calibration'][0], c['calibration'][1]))
106  calibration.appendChild(text_element)
107  channel.appendChild(calibration)
108  module.appendChild(channel)
109  root.appendChild(module)
110 
111  out.write(
112  table.toprettyxml(indent=" ", encoding="utf-8").decode("utf-8"))
def test_load(data)