Skip to content
Snippets Groups Projects
Commit 3ebf39b2 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 ff6f5290
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,6 @@ Suggested updates to obspy.io.reftek.core: ...@@ -10,7 +10,6 @@ Suggested updates to obspy.io.reftek.core:
Maeva Pourpoint IRIS/PASSCAL Maeva Pourpoint IRIS/PASSCAL
""" """
import copy import copy
import obspy.io.reftek.core as obspy_rt130_core import obspy.io.reftek.core as obspy_rt130_core
import warnings import warnings
...@@ -19,8 +18,27 @@ import numpy as np ...@@ -19,8 +18,27 @@ import numpy as np
from obspy import Trace, Stream, UTCDateTime from obspy import Trace, Stream, UTCDateTime
from obspy.core.util.obspy_types import ObsPyException from obspy.core.util.obspy_types import ObsPyException
from obspy.io.reftek.packet import _unpack_C0_C2_data from obspy.io.reftek.util import _decode_ascii
from sohstationviewer.model.reftek.from_rt2ms.packet import EHPacket
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): class Reftek130Exception(ObsPyException):
......
...@@ -14,8 +14,8 @@ def read_eh_et_packet(packet: bytes, unpacker: Unpacker): ...@@ -14,8 +14,8 @@ def read_eh_et_packet(packet: bytes, unpacker: Unpacker):
extended_header = EHETExtendedHeader(event_number, data_stream_number, extended_header = EHETExtendedHeader(event_number, data_stream_number,
flags, data_format) flags, data_format)
first_data_point = 9999 payload = packet[24:92]
return extended_header, first_data_point return extended_header, payload
@dataclasses.dataclass @dataclasses.dataclass
...@@ -34,4 +34,4 @@ class EHETExtendedHeader: ...@@ -34,4 +34,4 @@ class EHETExtendedHeader:
class EHETPacket: class EHETPacket:
header: PacketHeader header: PacketHeader
extended_header: EHETExtendedHeader extended_header: EHETExtendedHeader
data: int data: bytes
...@@ -5,6 +5,7 @@ from typing import Callable, Dict, Union ...@@ -5,6 +5,7 @@ from typing import Callable, Dict, Union
import numpy import numpy
import numpy as np import numpy as np
from obspy import UTCDateTime from obspy import UTCDateTime
from obspy.io.reftek.packet import PACKET_FINAL_DTYPE
from obspy.io.reftek.util import ( from obspy.io.reftek.util import (
bcd, bcd_hex, bcd, bcd_hex,
bcd_julian_day_string_to_nanoseconds_of_year, bcd_16bit_int, bcd_8bit_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 ( ...@@ -27,6 +28,12 @@ from sohstationviewer.model.reftek.rt130_experiment.reftek_helper import (
from sohstationviewer.model.reftek.from_rt2ms import core 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 = [
("packet_type", "|S2", None, "S2"), ("packet_type", "|S2", None, "S2"),
("experiment_number", np.uint8, bcd, np.uint8), ("experiment_number", np.uint8, bcd, np.uint8),
...@@ -43,7 +50,7 @@ PACKET = [ ...@@ -43,7 +50,7 @@ PACKET = [
("flags", np.uint8, None, np.uint8), ("flags", np.uint8, None, np.uint8),
("data_format", np.uint8, bcd_8bit_hex, "S2"), ("data_format", np.uint8, bcd_8bit_hex, "S2"),
# Temporarily store the payload here. # 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([ PACKET_FINAL_DTYPE = np.dtype([
(name, dtype_final) (name, dtype_final)
...@@ -170,21 +177,25 @@ def convert_packet_to_obspy_format(packet: Union[EHETPacket, DTPacket], unpacker ...@@ -170,21 +177,25 @@ def convert_packet_to_obspy_format(packet: Union[EHETPacket, DTPacket], unpacker
converted_packet['flags'] = packet.extended_header.flags converted_packet['flags'] = packet.extended_header.flags
converted_packet['data_format'] = packet.extended_header.data_format 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 if converted_packet['packet_type'] == 'DT':
# arbitrary length integer, so we need to do some conversion. # Obspy stores the data as list of 1-byte integers. We store the
# To make converting the resulting tuple to an element of a structured # data as an arbitrary length integer, so we need to do some
# array of type PACKET_FINAL_DTYPE easier, we set the size of the payload # conversion. To make converting the resulting tuple to an element
# to be 4. This only affect data with format 16, and as long as we are # of a structured array of type PACKET_FINAL_DTYPE easier, we set
# careful in self.to_stream, we don't even have to make a special case when # the size of the payload to be 4. This only affect data with format
# decoding (note: this is possible because the affected data will be # 16, and as long as we are careful in self.to_stream, we don't even
# prepended with 0s when encoded. Then, during decoding, we will get the # have to make a special case when decoding (note: this is possible
# original data prepended with 0s, which is the original data.) # because of a peculiarity of the 2's complement encoding).
data_size = 4 data_size = 4
format_char = 'B' format_char = 'B'
converted_packet['payload'] = list(unpacker.unpack( converted_packet['payload'] = list(unpacker.unpack(
f'{data_size}{format_char}', f'{data_size}{format_char}',
packet.data.to_bytes(data_size, 'big', signed=True) 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()) 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