Coverage for src/km3dq_common/lw_db_fact_library.py: 0%
57 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-25 11:58 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-25 11:58 +0000
1#! /usr/bin/env python
2###############################################################################
3import re
4import sys
5import urllib.request
7from km3dq_common.detector_fact_classes import DetectorFact
10###############################################################################
11def read_basic_fact_file(det, fact_type):
12 """
13 Read defect files stored on SFTP. Simply returns lines
15 === Arguments ===
16 - det : detector name - [string] - Ex: "D0ARCA021", "D0ORCA018"...
17 - fact_type
19 === Output ===
20 - Array of lines
22 """
24 if "ORCA" in det:
25 site = "ORCA"
26 else:
27 site = "ARCA"
29 lines = []
30 fact_url = ("https://sftp.km3net.de/data/km3dq_lw_db/"
31 f"{site}/{det}/Facts/"
32 f"{fact_type}.txt")
33 try:
34 with urllib.request.urlopen(fact_url) as def_file:
35 tmp = ((def_file.read()).split(b"\n"))
36 for i_line in tmp:
37 if i_line != "":
38 lines.append(i_line.decode("utf-8"))
39 except urllib.error.URLError:
40 # No fact file found
41 lines = None
43 return lines
46###############################################################################
47def read_det_fact_file(site):
48 """
49 Read detector fact files stored on SFTP
51 === Arguments ===
52 - det : detector name - [string] - Ex: "D0ARCA021", "D0ORCA018"...
53 - def_tag : defect tag - [string]
54 - type_filter : if not empty, filter on the defect type - If empty, read
55 all defect files - [array of strings] -
56 Ex: ["daq", "operation"]
58 === Output ===
59 - raw detector facts
60 - detector start/end
61 """
63 # det_facts0 = configure_det_fact()
65 raw_df = {}
66 det_prop = {}
68 re_line = re.compile(r"(.*)\|"
69 r"(.*)\|"
70 r"(.*)\|"
71 r"(.*)\|"
72 r"(.*)\|"
73 r"(.*)\|"
74 r"(.*)\|"
75 r"(.*)\|"
76 r"(.*)\|"
77 r"(.*)\|"
78 r"(.*)")
80 raw_df = []
82 lines = []
83 det_fact_url = ("https://sftp.km3net.de/data/km3dq_lw_db/"
84 f"{site}//DetectorFacts/"
85 f"detector_facts.txt")
86 try:
87 with urllib.request.urlopen(det_fact_url) as def_file:
88 tmp = ((def_file.read()).split(b"\n"))
89 for i_line in tmp:
90 if i_line != "":
91 lines.append(i_line.decode("utf-8"))
92 except urllib.error.URLError:
93 # No defect file found
94 print("Missing detector-fact file")
95 sys.exit()
97 for (i_line_index, i_line) in enumerate(lines):
98 if i_line_index == 0:
99 continue
101 r_m = re_line.match(i_line)
102 if r_m:
103 raw_df.append(DetectorFact())
104 raw_df[-1].hardware = r_m.group(4).split("-")[0].replace(" ", "")
105 raw_df[-1].site = site.replace(" ", "")
106 raw_df[-1].time = r_m.group(1)
107 raw_df[-1].det = r_m.group(2).replace(" ", "")
108 raw_df[-1].following_run = int(r_m.group(3))
109 raw_df[-1].upi = (r_m.group(4).split(f"{raw_df[-1].hardware}-")[1])\
110 .replace(" ", "")
111 raw_df[-1].location = r_m.group(5).replace(" ", "")
112 if r_m.group(6).replace(" ", "") != "":
113 raw_df[-1].position = int(r_m.group(6))
114 else: # Detector object has no position
115 raw_df[-1].position = ""
116 raw_df[-1].status = r_m.group(7).replace(" ", "")
117 raw_df[-1].comment = r_m.group(8)
118 raw_df[-1].waveform = r_m.group(9).replace(" ", "")
119 raw_df[-1].documentation = r_m.group(10)
120 raw_df[-1].author = r_m.group(11)
122 return (raw_df, det_prop)