105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
|
|
############################################################################
|
|
# Copyright (C) 2024 by gempa GmbH #
|
|
# #
|
|
# All Rights Reserved. #
|
|
# #
|
|
# NOTICE: All information contained herein is, and remains #
|
|
# the property of gempa GmbH and its suppliers, if any. The intellectual #
|
|
# and technical concepts contained herein are proprietary to gempa GmbH #
|
|
# and its suppliers. #
|
|
# Dissemination of this information or reproduction of this material #
|
|
# is strictly forbidden unless prior written permission is obtained #
|
|
# from gempa GmbH. #
|
|
############################################################################
|
|
|
|
import os
|
|
|
|
from seiscomp import logging
|
|
|
|
from gempa import CAPS
|
|
|
|
|
|
class JournalItem:
|
|
def __init__(self, startTime=None, endTime=None):
|
|
self.startTime = startTime
|
|
self.endTime = endTime
|
|
|
|
|
|
class Journal:
|
|
# -------------------------------------------------------------------------
|
|
def __init__(self):
|
|
self.items = {}
|
|
|
|
# -------------------------------------------------------------------------
|
|
def get(self, streamID):
|
|
return self.items.get(streamID)
|
|
|
|
# -------------------------------------------------------------------------
|
|
def read(self, filename):
|
|
try:
|
|
f = open(filename, "r", encoding="UTF-8")
|
|
except Exception as err:
|
|
logging.error(f"Journal: Could not open file: {err}")
|
|
return False
|
|
|
|
try:
|
|
lineNo = 0
|
|
for line in f:
|
|
line = line.strip()
|
|
if line.startswith("#"):
|
|
continue
|
|
|
|
try:
|
|
stationID, strStartTime, strEndTime = line.split(" ")
|
|
except ValueError:
|
|
logging.error(
|
|
f"Journal: Invalid line format in line {lineNo}"
|
|
)
|
|
return False
|
|
|
|
item = JournalItem()
|
|
|
|
item.startTime = CAPS.Time.FromString(strStartTime, "%FT%T.%Z")
|
|
item.endTime = CAPS.Time.FromString(strEndTime, "%FT%T.%Z")
|
|
|
|
self.items[stationID] = item
|
|
|
|
lineNo += 1
|
|
except IOError as err:
|
|
logging.error(f"Journal: Could not read journal from file: {err}")
|
|
finally:
|
|
f.close()
|
|
|
|
logging.info("Recovered journal")
|
|
for k, v in self.items.items():
|
|
logging.info(f" + {k} {v.startTime.iso()} ~ {v.endTime.iso()}")
|
|
|
|
logging.info("End")
|
|
|
|
return True
|
|
|
|
# -------------------------------------------------------------------------
|
|
def write(self, filename):
|
|
path = os.path.dirname(filename)
|
|
if not path:
|
|
return False
|
|
|
|
if not os.path.exists(path):
|
|
try:
|
|
os.makedirs(path)
|
|
except Exception as err:
|
|
logging.error(f"Journal: Could not create directory: {err}")
|
|
return False
|
|
|
|
try:
|
|
with open(filename, "w", encoding="UTF-8") as f:
|
|
for k, v in self.items.items():
|
|
f.write(f"{k} {v.startTime.iso()} {v.endTime.iso()}\n")
|
|
except Exception as err:
|
|
logging.error(f"Journal: Faild to write journal: {err}")
|
|
return False
|
|
|
|
return True
|