From 01e27006ae515bea56f0f6f3371a0eab11a48b5e Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Wed, 15 Mar 2023 17:59:43 +0200 Subject: [PATCH] Fix logbook encoding problems and invalid date in header --- VERSION | 2 +- code/spc_io/high_level/spc.py | 20 +++++++++++++------- code/spc_io/low_level/headers/logstc.py | 8 ++++++-- code/spc_io/low_level/spc_raw.py | 3 ++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index 8acdd82..4e379d2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.1 +0.0.2 diff --git a/code/spc_io/high_level/spc.py b/code/spc_io/high_level/spc.py index b4e036e..0a91b2a 100644 --- a/code/spc_io/high_level/spc.py +++ b/code/spc_io/high_level/spc.py @@ -2,9 +2,11 @@ import io from typing import Union, List, Literal, ForwardRef +import logging from datetime import datetime import numpy.typing as npt import numpy as np +import pandas as pd from pydantic import validate_arguments from spc_io.low_level.headers.fxytype import Fxtype, Fytype from spc_io.low_level.spc_raw import SpcRaw @@ -17,7 +19,7 @@ from .log_book import LogBook from .even_axis import EvenAxis -import pandas as pd +logger = logging.getLogger(__name__) SPC = ForwardRef('SPC') @@ -132,12 +134,16 @@ def from_spc_raw(cls, spc_raw: SpcRaw): text=spc_raw.log_book.txt_as_dict()) main_header = spc_raw.main_header fdate = main_header.fdate - date = datetime(year=fdate.year, - month=fdate.month, - day=fdate.day, - hour=fdate.hour, - minute=fdate.min, - ) + try: + date = datetime(year=fdate.year, + month=fdate.month, + day=fdate.day, + hour=fdate.hour, + minute=fdate.min, + ) + except ValueError as e: + logger.warning(repr(e)) + date = datetime.fromtimestamp(0) if not main_header.ftflgs.TXYXYS: if main_header.ftflgs.TXVALS: diff --git a/code/spc_io/low_level/headers/logstc.py b/code/spc_io/low_level/headers/logstc.py index 473d57d..5ed2ea3 100644 --- a/code/spc_io/low_level/headers/logstc.py +++ b/code/spc_io/low_level/headers/logstc.py @@ -6,7 +6,11 @@ class LogBookBase: def txt_as_dict(self): - return dict([i.split(b'=') for ii in self.txt.split(b'\r\n') for i in ii.split(b'\n\r') if b'=' in i]) + return dict([i.decode(errors='surrogateescape').split('=') + for ii in self.txt.split(b'\r\n') + for i in ii.split(b'\n\r') + if b'=' in i + ]) def disk_as_bytes(self): return bytearray(string_at(addressof(self.disk), sizeof(self.disk))) @@ -33,7 +37,7 @@ def new_header_and_logbook_from_data(cls, *, binary: bytes = b'', txt: Union[bytes, Dict] = b''): if isinstance(txt, dict): - txt = b'\r\n'.join([f'{k}={v}'.encode() for k, v in txt.items()]) + txt = b'\r\n'.join([f'{k}={v}'.encode(errors='surrogateescape') for k, v in txt.items()]) txt += b'\x00' # NUL terminate logsizd = len(disk)+len(binary)+len(txt)+sizeof(cls) logsizm = ((logsizd // 4096) + 1) * 4096 diff --git a/code/spc_io/low_level/spc_raw.py b/code/spc_io/low_level/spc_raw.py index 1b78b40..2a4105e 100644 --- a/code/spc_io/low_level/spc_raw.py +++ b/code/spc_io/low_level/spc_raw.py @@ -147,7 +147,8 @@ def calcuate_offsets(self): size += sizeof(sub._xarray) size += sizeof(sub._yarray) ofs += size - self.dirs.append(Ssfstc(ssfposn=ofs, ssfsize=size, ssftime=sub.z)) + if sub.z is not None: + self.dirs.append(Ssfstc(ssfposn=ofs, ssfsize=size, ssftime=sub.z)) # Directory offset ofs += sizeof(self.log_book)