From 4b649e92170ab8c8455dbff865ecdd0db77478c1 Mon Sep 17 00:00:00 2001 From: ldam <ldam@passcal.nmt.edu> Date: Mon, 28 Aug 2023 11:49:39 -0600 Subject: [PATCH] move read_text() from mseed_helper.py to general_data_helper.py; Unpacker from record_reader_helper.py to general_record_helper.py because they will be used by reftek --- .../model/general_data/general_data_helper.py | 36 +++++++++++++++++++ .../general_data/general_record_helper.py | 27 ++++++++++++++ sohstationviewer/model/mseed_data/mseed.py | 9 ++--- .../model/mseed_data/mseed_helper.py | 33 ----------------- .../model/mseed_data/record_reader_helper.py | 27 ++------------ .../model/reftek_data/reftek_reader/core.py | 2 +- .../reftek_reader/packet_readers.py | 2 +- .../reftek_reader/reftek_helper.py | 2 +- 8 files changed, 73 insertions(+), 65 deletions(-) create mode 100644 sohstationviewer/model/general_data/general_record_helper.py diff --git a/sohstationviewer/model/general_data/general_data_helper.py b/sohstationviewer/model/general_data/general_data_helper.py index 408407d1d..b689a6e97 100644 --- a/sohstationviewer/model/general_data/general_data_helper.py +++ b/sohstationviewer/model/general_data/general_data_helper.py @@ -1,5 +1,8 @@ from typing import List, Dict, Optional, Union, Tuple import numpy as np +import os +from pathlib import Path + from sohstationviewer.database.extract_data import get_convert_factor @@ -207,3 +210,36 @@ def reset_data(selected_key: Union[str, Tuple[str, str]], data_dict: Dict): del selected_data_dict[chan_id][k] except KeyError: pass + + +def read_text(path2file: Path) -> Union[bool, str]: + """ + CHANGED FROM handling_data.read_text: + + Don't need to check binary because UnicodeDecodeError caught means + the file is binary + + Read text file and add to log_data under channel TEXT. + + Raise exception if the file isn't a text file + + Remove empty lines in content + :param path2file: str - absolute path to text file + :param file_name: str - name of text file + :param text_logs: holder to keep log string, refer to + DataTypeModel.__init__.log_data['TEXT'] + """ + try: + with open(path2file, 'r') as file: + content = file.read().strip() + except UnicodeDecodeError: + return + + if content != '': + # skip empty lines + no_empty_line_list = [ + line for line in content.splitlines() if line] + no_empty_line_content = os.linesep.join(no_empty_line_list) + + log_text = "\n\n** STATE OF HEALTH: %s\n" % path2file.name + log_text += no_empty_line_content + else: + log_text = '' + return log_text diff --git a/sohstationviewer/model/general_data/general_record_helper.py b/sohstationviewer/model/general_data/general_record_helper.py new file mode 100644 index 000000000..5d1e0603f --- /dev/null +++ b/sohstationviewer/model/general_data/general_record_helper.py @@ -0,0 +1,27 @@ +import struct + + +class Unpacker: + """ + A wrapper around struct.unpack() to unpack binary data without having to + explicitly define the byte order in the format string. Also restrict the + type of format to str and buffer to bytes. + """ + def __init__(self, byte_order_char: str = '') -> None: + self.byte_order_char = byte_order_char + + def unpack(self, format: str, buffer: bytes): + """ + Unpack a string of bytes into a tuple of values based on the given + format + :param format: the format used to unpack the byte string + :param buffer: the byte string + :return: a tuple containing the unpacked values. + """ + default_byte_order_chars = ('@', '=', '>', '<', '!') + if format.startswith(default_byte_order_chars): + format = self.byte_order_char + format[:1] + else: + format = self.byte_order_char + format + return struct.unpack(format, buffer) + diff --git a/sohstationviewer/model/mseed_data/mseed.py b/sohstationviewer/model/mseed_data/mseed.py index 19e515dbf..c04810830 100644 --- a/sohstationviewer/model/mseed_data/mseed.py +++ b/sohstationviewer/model/mseed_data/mseed.py @@ -8,16 +8,17 @@ from pathlib import Path from typing import Dict, Tuple, List from sohstationviewer.controller.util import validate_file, validate_dir -from sohstationviewer.model.mseed_data.mseed_reader import MSeedReader +from sohstationviewer.view.util.enums import LogType + from sohstationviewer.model.general_data.general_data import \ GeneralData, ThreadStopped, ProcessingDataError -from sohstationviewer.view.util.enums import LogType +from sohstationviewer.model.general_data.general_data_helper import read_text from sohstationviewer.model.mseed_data.mseed_helper import \ - retrieve_nets_from_data_dict, read_text + retrieve_nets_from_data_dict from sohstationviewer.model.mseed_data.record_reader_helper import \ MSeedReadError - +from sohstationviewer.model.mseed_data.mseed_reader import MSeedReader class MSeed(GeneralData): """ diff --git a/sohstationviewer/model/mseed_data/mseed_helper.py b/sohstationviewer/model/mseed_data/mseed_helper.py index 32d237e2e..0b604b55d 100644 --- a/sohstationviewer/model/mseed_data/mseed_helper.py +++ b/sohstationviewer/model/mseed_data/mseed_helper.py @@ -18,36 +18,3 @@ def retrieve_nets_from_data_dict(data_dict: Dict, for c in data_dict[sta_id]: nets_by_sta[sta_id].update( data_dict[sta_id][c]['nets']) - - -def read_text(path2file: Path) -> Union[bool, str]: - """ - CHANGED FROM handling_data.read_text: - + Don't need to check binary because UnicodeDecodeError caught means - the file is binary - - Read text file and add to log_data under channel TEXT. - + Raise exception if the file isn't a text file - + Remove empty lines in content - :param path2file: str - absolute path to text file - :param file_name: str - name of text file - :param text_logs: holder to keep log string, refer to - DataTypeModel.__init__.log_data['TEXT'] - """ - try: - with open(path2file, 'r') as file: - content = file.read().strip() - except UnicodeDecodeError: - return - - if content != '': - # skip empty lines - no_empty_line_list = [ - line for line in content.splitlines() if line] - no_empty_line_content = os.linesep.join(no_empty_line_list) - - log_text = "\n\n** STATE OF HEALTH: %s\n" % path2file.name - log_text += no_empty_line_content - else: - log_text = '' - return log_text diff --git a/sohstationviewer/model/mseed_data/record_reader_helper.py b/sohstationviewer/model/mseed_data/record_reader_helper.py index c9fa6ace5..4028d497c 100644 --- a/sohstationviewer/model/mseed_data/record_reader_helper.py +++ b/sohstationviewer/model/mseed_data/record_reader_helper.py @@ -4,37 +4,14 @@ from enum import Enum from obspy import UTCDateTime +from sohstationviewer.model.general_data.general_record_helper import Unpacker + class MSeedReadError(Exception): def __init__(self, msg): self.message = msg -class Unpacker: - """ - A wrapper around struct.unpack() to unpack binary data without having to - explicitly define the byte order in the format string. Also restrict the - type of format to str and buffer to bytes. - """ - def __init__(self, byte_order_char: str = '') -> None: - self.byte_order_char = byte_order_char - - def unpack(self, format: str, buffer: bytes): - """ - Unpack a string of bytes into a tuple of values based on the given - format - :param format: the format used to unpack the byte string - :param buffer: the byte string - :return: a tuple containing the unpacked values. - """ - default_byte_order_chars = ('@', '=', '>', '<', '!') - if format.startswith(default_byte_order_chars): - format = self.byte_order_char + format[:1] - else: - format = self.byte_order_char + format - return struct.unpack(format, buffer) - - @dataclass class FixedHeader: """ diff --git a/sohstationviewer/model/reftek_data/reftek_reader/core.py b/sohstationviewer/model/reftek_data/reftek_reader/core.py index ff6478934..74d02952b 100644 --- a/sohstationviewer/model/reftek_data/reftek_reader/core.py +++ b/sohstationviewer/model/reftek_data/reftek_reader/core.py @@ -22,7 +22,7 @@ from obspy import Trace, Stream, UTCDateTime from obspy.core.util.obspy_types import ObsPyException from obspy.io.reftek.packet import PACKET_FINAL_DTYPE -from sohstationviewer.model.mseed_data.record_reader_helper import Unpacker +from sohstationviewer.model.general_data.general_record_helper import Unpacker from sohstationviewer.model.reftek_data.reftek_reader.packet import EHPacket from sohstationviewer.model.reftek_data.reftek_reader.reftek_helper import ( diff --git a/sohstationviewer/model/reftek_data/reftek_reader/packet_readers.py b/sohstationviewer/model/reftek_data/reftek_reader/packet_readers.py index ff9cc90a9..9e1b4f75b 100644 --- a/sohstationviewer/model/reftek_data/reftek_reader/packet_readers.py +++ b/sohstationviewer/model/reftek_data/reftek_reader/packet_readers.py @@ -3,7 +3,7 @@ from typing import Tuple, Any import numpy from obspy.io.reftek.util import bcd -from sohstationviewer.model.mseed_data.record_reader_helper import Unpacker +from sohstationviewer.model.general_data.general_record_helper import Unpacker from sohstationviewer.model.reftek_data.reftek_reader.packet import \ eh_et_payload_end_in_packet from sohstationviewer.model.reftek_data.reftek_reader.packets import ( diff --git a/sohstationviewer/model/reftek_data/reftek_reader/reftek_helper.py b/sohstationviewer/model/reftek_data/reftek_reader/reftek_helper.py index 5f0e6cdc6..32b53080b 100644 --- a/sohstationviewer/model/reftek_data/reftek_reader/reftek_helper.py +++ b/sohstationviewer/model/reftek_data/reftek_reader/reftek_helper.py @@ -4,7 +4,7 @@ from typing import Any, Dict, Callable, Union, List, Tuple import numpy import numpy as np -from sohstationviewer.model.mseed_data.record_reader_helper import Unpacker +from sohstationviewer.model.general_data.general_record_helper import Unpacker from sohstationviewer.model.reftek_data.reftek_reader.packet import \ eh_et_payload_end_in_packet from sohstationviewer.model.reftek_data.reftek_reader.packet_readers import ( -- GitLab