Skip to content
Snippets Groups Projects

Save log reading options

Merged Kien Le requested to merge feature-save_log_reading_options into master
1 file
+ 7
0
Compare changes
  • Side-by-side
  • Inline
@@ -12,6 +12,14 @@ RT130_PACKETS = ['SH', 'SC', 'OM', 'DS', 'AD', 'CD', 'FD', 'EH', 'ET']
SeparatedPacketLines = Tuple[List[str], List[str], List[str]]
class LogReadError(ValueError):
"""
Error to raise when there is a problem when reading a log file.
"""
def __init__(self, message: str):
self.message = message
def detect_log_packet_format(packet: List[str]) -> Optional[LogFileFormat]:
"""
Detect the format of a log file packet. The format can be either rt2ms',
@@ -197,6 +205,33 @@ PACKET_PARSERS: Dict[Optional[LogFileFormat], Parser] = {
}
def validate_soh_lines(soh_lines: List[str]):
"""
Validate a list of SOH lines by examining its header.
:param soh_lines: a list of SOH lines
:return: whether soh_lines is valid
"""
valid_packet_types = {
'Auxiliary Data Parameter',
'Calibration Definition',
'Data Stream Definition',
'Filter Description',
'Operating Mode Definition',
'Station Channel Definition',
'State of Health',
}
if not any(soh_lines[0].startswith(packet_type)
for packet_type in valid_packet_types):
return False
splitted_header = soh_lines[0].split()
if not (len(splitted_header[-1]) == 4 and splitted_header[-1].isalnum()):
return False
if splitted_header[-2] != 'ST:':
return False
return True
def get_experiment_number(soh_lines: List[str]):
"""
Get the experiment number from the list of SOH lines in a packet.
@@ -253,12 +288,11 @@ class LogFileReader:
parser = PACKET_PARSERS[self.log_file_type]
eh_et_lines, soh_lines, masspos_lines = parser(packet)
if self.station_code is None and soh_lines:
split_packet_header = soh_lines[0].strip().split(' ')
possible_programs = ['logpeek:', 'SOHStationViewer:', 'rt2ms:']
# We want to skip the file header, which contains the program
# used to generate the file.
if split_packet_header[0] not in possible_programs:
self.station_code = split_packet_header[-1]
if validate_soh_lines(soh_lines):
self.station_code = soh_lines[0].split()[-1]
if self.station_code is None and eh_et_lines:
# Each event info line stores the station code as second word
self.station_code = eh_et_lines[0].split()[1]
if self.experiment_number is None and soh_lines:
found_experiment_number = get_experiment_number(soh_lines)
self.experiment_number = found_experiment_number
@@ -269,6 +303,16 @@ class LogFileReader:
# separate the SOH lines blocks during processing.
self.soh_lines.append('\n')
self.masspos_lines.extend(masspos_lines)
# In cases where there is no SOH data, we are going to assume that the
# experiment number is 0.
if self.experiment_number is None:
self.experiment_number = '0'
if self.station_code is None:
msg = (f'The log file {self.file_path.name} does not contain a '
f'station code. Please make sure it is a valid RT130 log '
f'file. RT130 log files can be generated with rt2ms, '
f'logpeek, or this program.')
raise LogReadError(msg)
def process_mass_poss_line(masspos_lines: List[str]
Loading