Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _parseSEED(self, data):
"""
Parses through a whole SEED volume.
It will always parse the whole file and skip any time span data.
:type data: file or io.BytesIO
"""
# Jump to the beginning of the file.
data.seek(0)
# Retrieve some basic data like version and record length.
temp = data.read(8)
# Check whether it starts with record sequence number 1 and a volume
# index control header.
if temp != b'000001V ':
raise SEEDParserException("Expecting 000001V ")
# The first blockette has to be Blockette 10.
temp = data.read(3)
if temp not in [b'010', b'008', b'005']:
raise SEEDParserException("Expecting blockette 010, 008 or 005")
# Skip the next four bytes containing the length of the blockette.
# data.seek(4, 1)
data.read(4)
# Set the version.
self.version = float(data.read(4))
# Get the record length.
length = pow(2, int(data.read(2)))
# Test record length.
data.seek(length)
temp = data.read(6)
if temp != b'000002':
msg = "Got an invalid logical record length %d" % length
class_name = 'Blockette%03d' % blockette_id
if not hasattr(blockette, class_name):
raise SEEDParserException('Blockette %d not implemented!' %
blockette_id)
blockette_class = getattr(blockette, class_name)
blockette_obj = blockette_class(debug=self.debug,
strict=self.strict,
compact=self.compact,
version=self.version,
record_type=record_type,
xseed_version=xseed_version)
blockette_obj.parseXML(XML_blockette)
return blockette_obj
elif blockette_id != 0:
msg = "Unknown blockette type %d found" % blockette_id
raise SEEDParserException(msg)
if blk.end_date and blk.end_date < datetime:
continue
channel_flag = True
blockettes.append(tmpb50)
blockettes.append(blk)
elif channel_flag and station_flag:
blockettes.append(blk)
# check number of selected channels (equals number of blockette 52)
b50s = [b for b in blockettes if b.id == 50]
b52s = [b for b in blockettes if b.id == 52]
if len(b50s) == 0 or len(b52s) == 0:
msg = 'No channel found with the given SEED id: %s'
raise SEEDParserException(msg % (seed_id))
elif len(b50s) > 1 or len(b52s) > 1:
msg = 'More than one channel found with the given SEED id: %s'
raise SEEDParserException(msg % (seed_id))
return blockettes
def getSEED(self, compact=False):
"""
Takes everything stored in the object returns a valid SEED string.
"""
self.compact = compact
# Nothing to write if not all necessary data is available.
if not self.volume or not self.abbreviations or not self.stations:
msg = 'No data to be written available.'
raise SEEDParserException(msg)
# Check blockettes:
if not self._checkBlockettes():
msg = 'Not all necessary blockettes are available.'
raise SEEDParserException(msg)
# String to be written to:
seed_string = ''
cur_count = 1
volume, abbreviations, stations = self._createBlockettes11and12()
# Delete Blockette 11 again.
self._deleteBlockettes11and12()
# Finally write the actual SEED String.
for _i in volume:
seed_string += '%06i' % cur_count + _i
cur_count += 1
for _i in abbreviations:
seed_string += '%06i' % cur_count + _i
def getSEED(self, compact=False):
"""
Returns a SEED representation of the current Parser object.
"""
self.compact = compact
# Nothing to write if not all necessary data is available.
if not self.volume or not self.abbreviations or not self.stations:
msg = 'No data to be written available.'
raise SEEDParserException(msg)
# Check blockettes:
if not self._checkBlockettes():
msg = 'Not all necessary blockettes are available.'
raise SEEDParserException(msg)
# String to be written to:
seed_string = ''
cur_count = 1
volume, abbreviations, stations = self._createBlockettes11and12()
# Delete Blockette 11 again.
self._deleteBlockettes11and12()
# Finally write the actual SEED String.
for _i in volume:
seed_string += '%06i' % cur_count + _i
cur_count += 1
for _i in abbreviations:
seed_string += '%06i' % cur_count + _i
cur_count += 1
# Remove name of the stations.
stations = [_i[1:] for _i in stations]
for _i in stations:
def _parseSEED(self, data):
"""
Parses through a whole SEED volume.
It will always parse the whole file and skip any time span data.
:type data: File pointer or StringIO object.
"""
# Jump to the beginning of the file.
data.seek(0)
# Retrieve some basic data like version and record length.
temp = data.read(8)
# Check whether it starts with record sequence number 1 and a volume
# index control header.
if temp != '000001V ':
raise SEEDParserException("Expecting 000001V ")
# The first blockette has to be Blockette 10.
temp = data.read(3)
if temp != '010':
raise SEEDParserException("Expecting blockette 010")
# Skip the next four bytes containing the length of the blockette.
data.seek(4, 1)
# Set the version.
self.version = float(data.read(4))
# Get the record length.
length = pow(2, int(data.read(2)))
# Test record length.
data.seek(length)
temp = data.read(6)
if temp != '000002':
msg = "Got an invalid logical record length %d" % length
raise SEEDParserException(msg)
def getXSEED(self, version=DEFAULT_XSEED_VERSION, split_stations=False):
"""
Returns a XSEED representation of the current Parser object.
:type version: float, optional
:param version: XSEED version string (default is ``1.1``).
:type split_stations: boolean, optional
:param split_stations: Splits stations containing multiple channels
into multiple documents.
:rtype: str or dict
:return: Returns either a string or a dict of strings depending
on the flag ``split_stations``.
"""
if version not in XSEED_VERSIONS:
raise SEEDParserException("Unknown XML-SEED version!")
doc = Element("xseed", version=version)
# Nothing to write if not all necessary data is available.
if not self.volume or not self.abbreviations or \
len(self.stations) == 0:
msg = 'No data to be written available.'
raise SEEDParserException(msg)
# Check blockettes:
if not self._checkBlockettes():
msg = 'Not all necessary blockettes are available.'
raise SEEDParserException(msg)
# Add blockettes 11 and 12 only for XSEED version 1.0.
if version == '1.0':
self._createBlockettes11and12(blockette12=True)
# Now start actually filling the XML tree.
# Volume header:
sub = SubElement(doc, utils.toTag('Volume Index Control Header'))
# is passed.
if record_type not in HEADERS:
return
# Set standard values.
blockette_length = 0
blockette_id = -1
# Find out what kind of record is being parsed.
if record_type == 'S':
# Create new station blockettes list.
self.temp['stations'].append([])
root_attribute = self.temp['stations'][-1]
elif record_type == 'V':
# Just one Volume header per file allowed.
if len(self.temp['volume']):
msg = 'More than one Volume index control header found!'
raise SEEDParserException(msg)
root_attribute = self.temp['volume']
else:
# Just one abbreviations header allowed!
if len(self.temp['abbreviations']):
msg = 'More than one Abbreviation Dictionary Control ' + \
'Headers found!'
warnings.warn(msg, UserWarning)
root_attribute = self.temp['abbreviations']
# Loop over all blockettes in data.
while blockette_id != 0:
# remove spaces between blockettes
while data.read(1) == ' ':
continue
data.seek(-1, 1)
try:
blockette_id = int(data.read(3))
def read(self, data, strict=False):
data = self._read(data)
# check for datetime
if 'T' in self.flags:
# default value
if data:
# create a full SEED date string
temp = b"0000,000,00:00:00.0000"
data += temp[len(data):]
return UTCDateTime(data.decode())
if self.default_value:
return self.default_value
if self.min_length:
if strict:
raise SEEDParserException
warnings.warn('Date is required.', UserWarning)
return ""
else:
if self.flags:
return self._formatString(data)
else:
return data