rearranging methods to provide a superclass to all other structure classes

This commit is contained in:
Sebastian Wehling-Benatelli 2015-05-11 13:41:08 +02:00
parent c4b6b4c648
commit f177901883

View File

@ -154,42 +154,61 @@ class GenericDataStructure(object):
base working on. base working on.
''' '''
def __init__(self, structexp='$R/$D/$E', folderdepth=2, **kwargs): def __init__(self, rootpath='/data', dataformat=None, filesuffix=None,
structureOptions = ('$R', '$D', '$E') **kwargs):
self.__name = 'GDS' self.__allowExtraFields = True
structExpression = [] self.__expandFields = ['root']
depth = 0 self.__dsFields = {'root': rootpath}
while structexp is not os.path.sep:
try:
[structexp, tlexp] = os.path.split(structexp)
except AttributeError:
rootExpression = None
structExpression = None
break
structExpression.append(tlexp)
depth += 1
if depth is folderdepth:
rootExpression = structexp
break
structExpression.reverse()
self.folderDepth = folderdepth
self.__gdsFields = {'ROOT': rootExpression}
self.modifyFields(**kwargs) self.modifyFields(**kwargs)
def modifyFields(self, **kwargs): def modifyFields(self, **kwargs):
for key, value in kwargs.iteritems():
key = str(key).upper()
self.__gdsFields[key] = value
def getName(self): assert isinstance(kwargs, dict), 'dictionary type object expected'
return self.__name
for key, value in kwargs.iteritems():
key = str(key).lower()
if type(value) not in (str, int, float):
for n, val in enumerate(value):
value[n] = str(val)
else:
value = str(value)
try:
self.setFieldValue(key, value)
except KeyError, e:
errmsg = ''
errmsg += 'WARNING:\n'
errmsg += 'unable to set values for datastructure fields\n'
errmsg += '%s; desired value was: %s\n' % (e, value)
print errmsg
def isField(self, key):
return key in self.getFields().keys()
def getFieldValue(self, key):
if self.isField(key):
return self.getFields()[key]
else:
return
def setFieldValue(self, key, value):
if self.isField(key):
self.getFields()[key] = value
else:
print('Warning: trying to set value of non-existent field '
'{field}'.format(field=key))
def getFields(self): def getFields(self):
return self.__gdsFields return self.__dsFields
def getExpandFields(self):
return self.__expandFields
def expandDataPath(self): def expandDataPath(self):
return os.path.join(*self.getFields().values()) expandList = []
for item in self.getExpandFields():
expandList.append(self.getFieldValue(item))
return os.path.join(*self.getExpandFields())
class PilotDataStructure(object): class PilotDataStructure(object):
@ -204,14 +223,14 @@ class PilotDataStructure(object):
self.__name = 'PDS' self.__name = 'PDS'
self.dataFormat = dataformat self.dataFormat = dataformat
self.dataType = 'waveform' self.dataType = 'waveform'
self.__pdsFields = {'ROOT': root, self.__pdsFields = {'root': root,
'DATABASE': database, 'database': database,
'SUFFIX': fsuffix 'suffix': fsuffix
} }
self.modifiyFields(**kwargs) self.modifyFields(**kwargs)
def modifiyFields(self, **kwargs): def modifyFields(self, **kwargs):
if kwargs and isinstance(kwargs, dict): if kwargs and isinstance(kwargs, dict):
for key, value in kwargs.iteritems(): for key, value in kwargs.iteritems():
key = str(key) key = str(key)
@ -247,7 +266,7 @@ class PilotDataStructure(object):
return datapath return datapath
class SeiscompDataStructure(object): class SeiscompDataStructure(GenericDataStructure):
''' '''
Dictionary containing the data access information for an SDS data archive: Dictionary containing the data access information for an SDS data archive:
@ -257,35 +276,16 @@ class SeiscompDataStructure(object):
:type sdate, edate: str or UTCDateTime or None :type sdate, edate: str or UTCDateTime or None
''' '''
# Data type options def __init__(self, rootpath='/data/SDS', dataformat='MSEED',
__typeOptions = {'waveform': 'D', # Waveform data filesuffix=None, **kwargs):
'detect': 'E', # Detection data super(GenericDataStructure, self).__init__(rootpath, dataformat,
'log': 'L', # Log data filesuffix, **kwargs)
'timing': 'T', # Timing data
'calib': 'C', # Calibration data
'resp': 'R', # Response data
'opaque': 'O' # Opaque data
}
def __init__(self, dataType='waveform', sdate=None, edate=None, **kwargs):
self.__name = 'SDS' edate = UTCDateTime()
halfyear = UTCDateTime('1970-07-01')
def checkDate(date): sdate = UTCDateTime(edate - halfyear)
if not isinstance(date, UTCDateTime): del halfyear
return True
return False
try:
if checkDate(sdate):
sdate = UTCDateTime(sdate)
if checkDate(edate):
edate = UTCDateTime(edate)
except TypeError:
edate = UTCDateTime()
halfyear = UTCDateTime('1970-07-01')
sdate = UTCDateTime(edate - halfyear)
del halfyear
year = '' year = ''
if not edate.year == sdate.year: if not edate.year == sdate.year:
@ -296,13 +296,6 @@ class SeiscompDataStructure(object):
else: else:
year = '{0:04d}'.format(sdate.year) year = '{0:04d}'.format(sdate.year)
if dataType in self.__typeOptions.keys():
self.dataType = dataType
else:
self.dataType = 'waveform' # default value for dataType
print '''Warning: Selected datatype ('%s') not available.\n
Using 'waveform' instead!'''.format(dataType)
# SDS fields' default values # SDS fields' default values
# definitions from # definitions from
# http://www.seiscomp3.org/wiki/doc/applications/slarchive/SDS # http://www.seiscomp3.org/wiki/doc/applications/slarchive/SDS
@ -328,10 +321,7 @@ class SeiscompDataStructure(object):
else: else:
value = str(value) value = str(value)
try: try:
if key in self.getFields().keys(): self.setField(key, value)
self.getFields()[key] = value
else:
raise KeyError('unknown SDS wildcard: %s.' % key)
except KeyError, e: except KeyError, e:
errmsg = '' errmsg = ''
errmsg += 'WARNING:\n' errmsg += 'WARNING:\n'
@ -342,6 +332,22 @@ class SeiscompDataStructure(object):
def getType(self): def getType(self):
return self.__typeOptions[self.dataType] return self.__typeOptions[self.dataType]
def isField(self, key):
return key in self.getFields().keys()
def getFieldValue(self, key):
if self.isField(key):
return self.getFields()[key]
else:
return
def setFieldValue(self, key, value):
if self.isField(key):
self.getFields()[key] = value
else:
print('Warning: trying to set value of non-existent field '
'{field}'.format(field=key))
def getFields(self): def getFields(self):
return self.__sdsFields return self.__sdsFields