Skip to content

Commit

Permalink
Merge pull request #4 from h2020charisma/bugfix
Browse files Browse the repository at this point in the history
Fix logbook encoding problems and invalid date in header
  • Loading branch information
georgievgeorgi authored Mar 15, 2023
2 parents 6a5cd75 + 01e2700 commit 1575f32
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
0.0.2
20 changes: 13 additions & 7 deletions code/spc_io/high_level/spc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions code/spc_io/low_level/headers/logstc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion code/spc_io/low_level/spc_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1575f32

Please sign in to comment.