-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGriListFileReader.py
More file actions
65 lines (57 loc) · 2.22 KB
/
GriListFileReader.py
File metadata and controls
65 lines (57 loc) · 2.22 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
# coding=utf-8
# Copyright (C) 2020-2022 CS GROUP – France, https://www.csgroup.eu
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# author : Esquis Benjamin for CSGroup
#
# coding=utf-8
import copy
import re
from lxml import etree as et
class GriListFileReader(object):
def __init__(self, filename):
self.tree_hdr = et.parse(filename)
self.root_hdr = self.tree_hdr.getroot()
self.filename = filename
def strip_detector(self, detector_list):
gri_files = self.root_hdr.xpath(
'//GRI_GR')
# Check if there is one detector element, if not keep all
matched = False
for gri in gri_files:
for det in detector_list:
if re.search(".*_D" + det + "_.*", gri.attrib["GRI_GR_Id"]) is not None:
matched = True
break
if matched:
break
# Do the job
if matched:
idx = 1
for gri in gri_files:
matched = False
for det in detector_list:
if re.search(".*_D"+det+"_.*",gri.attrib["GRI_GR_Id"]) is not None:
matched = True
if matched:
gri.attrib["GRI_GR_Number"] = str(idx)
idx += 1
else:
self.root_hdr.remove(gri)
self.root_hdr.attrib["Total_Number_of_GRI_GR"] = str(idx-1)
else:
for gri in gri_files[1:]:
self.root_hdr.remove(gri)
self.root_hdr.attrib["Total_Number_of_GRI_GR"] = str(1)
def write_to_file(self, filename):
self.tree_hdr.write(filename, encoding="UTF-8")