Skip to content
Snippets Groups Projects
Commit c54ff40b authored by Kien Le's avatar Kien Le
Browse files

Modify the way EH/ET packets are stored

Modify EH/ET packets are stored to include necessary information that
was truncated before
parent 21b715a7
No related branches found
No related tags found
1 merge request!139Change the way RT130 data is read for better performance
......@@ -10,7 +10,6 @@ Suggested updates to obspy.io.reftek.core:
Maeva Pourpoint IRIS/PASSCAL
"""
import copy
import obspy.io.reftek.core as obspy_rt130_core
import warnings
......@@ -19,8 +18,27 @@ import numpy as np
from obspy import Trace, Stream, UTCDateTime
from obspy.core.util.obspy_types import ObsPyException
from obspy.io.reftek.packet import _unpack_C0_C2_data
from sohstationviewer.model.reftek.from_rt2ms.packet import EHPacket
from obspy.io.reftek.util import _decode_ascii
from sohstationviewer.model.reftek.from_rt2ms import packet
EH_PAYLOAD = {
"station_name_extension": (35, 1, _decode_ascii),
"station_name": (36, 4, _decode_ascii),
"sampling_rate": (64, 4, float),
}
class EHPacket(packet.EHPacket):
def __init__(self, data):
self._data = data
payload = self._data["payload"].tobytes()
for name, (start, length, converter) in EH_PAYLOAD.items():
data = payload[start:start + length]
if converter is not None:
data = converter(data)
setattr(self, name, data)
class Reftek130Exception(ObsPyException):
......
......@@ -14,8 +14,8 @@ def read_eh_et_packet(packet: bytes, unpacker: Unpacker):
extended_header = EHETExtendedHeader(event_number, data_stream_number,
flags, data_format)
first_data_point = 9999
return extended_header, first_data_point
payload = packet[24:92]
return extended_header, payload
@dataclasses.dataclass
......@@ -34,4 +34,4 @@ class EHETExtendedHeader:
class EHETPacket:
header: PacketHeader
extended_header: EHETExtendedHeader
data: int
data: bytes
......@@ -5,6 +5,7 @@ from typing import Callable, Dict, Union
import numpy
import numpy as np
from obspy import UTCDateTime
from obspy.io.reftek.packet import PACKET_FINAL_DTYPE
from obspy.io.reftek.util import (
bcd, bcd_hex,
bcd_julian_day_string_to_nanoseconds_of_year, bcd_16bit_int, bcd_8bit_hex,
......@@ -27,6 +28,12 @@ from sohstationviewer.model.reftek.rt130_experiment.reftek_helper import (
from sohstationviewer.model.reftek.from_rt2ms import core
# This is the minimum size of the payload we can use while keeping the
# necessary information from the EH/ET packets.
payload_size = 68
dt_payload_padding = [0] * (payload_size - 4)
PACKET = [
("packet_type", "|S2", None, "S2"),
("experiment_number", np.uint8, bcd, np.uint8),
......@@ -43,7 +50,7 @@ PACKET = [
("flags", np.uint8, None, np.uint8),
("data_format", np.uint8, bcd_8bit_hex, "S2"),
# Temporarily store the payload here.
("payload", (np.uint8, 4), None, (np.uint8, 4))]
("payload", (np.uint8, payload_size), None, (np.uint8, payload_size))]
PACKET_FINAL_DTYPE = np.dtype([
(name, dtype_final)
......@@ -170,21 +177,25 @@ def convert_packet_to_obspy_format(packet: Union[EHETPacket, DTPacket], unpacker
converted_packet['flags'] = packet.extended_header.flags
converted_packet['data_format'] = packet.extended_header.data_format
# Obspy stores the data as list of 1-byte integers. We store the data as an
# arbitrary length integer, so we need to do some conversion.
# To make converting the resulting tuple to an element of a structured
# array of type PACKET_FINAL_DTYPE easier, we set the size of the payload
# to be 4. This only affect data with format 16, and as long as we are
# careful in self.to_stream, we don't even have to make a special case when
# decoding (note: this is possible because the affected data will be
# prepended with 0s when encoded. Then, during decoding, we will get the
# original data prepended with 0s, which is the original data.)
data_size = 4
format_char = 'B'
converted_packet['payload'] = list(unpacker.unpack(
f'{data_size}{format_char}',
packet.data.to_bytes(data_size, 'big', signed=True)
))
if converted_packet['packet_type'] == 'DT':
# Obspy stores the data as list of 1-byte integers. We store the
# data as an arbitrary length integer, so we need to do some
# conversion. To make converting the resulting tuple to an element
# of a structured array of type PACKET_FINAL_DTYPE easier, we set
# the size of the payload to be 4. This only affect data with format
# 16, and as long as we are careful in self.to_stream, we don't even
# have to make a special case when decoding (note: this is possible
# because of a peculiarity of the 2's complement encoding).
data_size = 4
format_char = 'B'
converted_packet['payload'] = list(unpacker.unpack(
f'{data_size}{format_char}',
packet.data.to_bytes(data_size, 'big', signed=True)
)) + dt_payload_padding
elif converted_packet['packet_type'] in ['EH', 'ET']:
converted_packet['payload'] = list(unpacker.unpack(
f'{payload_size}B', packet.data
))
return tuple(converted_packet.values())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment