Files

128 lines
4.2 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
class StreamMapItem:
def __init__(self):
self.networkCode = ""
self.stationCode = ""
self.locationCode = ""
self.stationID = ""
self.baseCode = None
self.folder = None
self.startTime = None
self.endTime = None
class StreamMap:
# -------------------------------------------------------------------------
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"Stream map: Could not open file: {err}")
return False
try:
lineNo = -1
for line in f:
lineNo += 1
line = line.strip()
if line.startswith("#"):
continue
if len(line) == 0:
continue
folder = line.strip()
toks = folder.split("_")
tokCount = len(toks)
if tokCount != 3:
logging.error(
f"Stream map: Invalid stream ID in line {lineNo}"
)
continue
item = StreamMapItem()
item.networkCode = toks[0]
item.stationCode = toks[1]
item.locationCode = toks[2]
item.baseCode = str(int(item.networkCode[0:3]))
item.folder = folder
item.stationID = (
item.networkCode
+ "."
+ item.stationCode
+ "."
+ item.locationCode
)
self.items[item.stationID] = item
except IOError as err:
logging.error(
f"Stream map: Could not read stream map from file: {err}"
)
finally:
f.close()
if len(self.items) == 0:
logging.info("No streams configured: Nothing todo")
return False
logging.info("Configured stations")
for k, _v in self.items.items():
logging.info(f" + {k}")
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"Stream map: 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"Stream map: Could not open file: {err}")
return False
return True