implementation of PilotDataStructure as subclass to GenericDataStructure

This commit is contained in:
Sebastian Wehling-Benatelli 2015-05-18 13:12:12 +02:00
parent f177901883
commit 56a0563709

View File

@ -154,11 +154,11 @@ class GenericDataStructure(object):
base working on. base working on.
''' '''
def __init__(self, rootpath='/data', dataformat=None, filesuffix=None, def __init__(self, **kwargs):
**kwargs):
self.__allowExtraFields = True self.allowedFields = []
self.__expandFields = ['root'] self.expandFields = ['root']
self.__dsFields = {'root': rootpath} self.dsFields = {}
self.modifyFields(**kwargs) self.modifyFields(**kwargs)
@ -166,8 +166,12 @@ class GenericDataStructure(object):
assert isinstance(kwargs, dict), 'dictionary type object expected' assert isinstance(kwargs, dict), 'dictionary type object expected'
if not self.extraAllowed():
kwargs = self.updateNotAllowed(kwargs)
for key, value in kwargs.iteritems(): for key, value in kwargs.iteritems():
key = str(key).lower() key = str(key).lower()
if value is not None:
if type(value) not in (str, int, float): if type(value) not in (str, int, float):
for n, val in enumerate(value): for n, val in enumerate(value):
value[n] = str(val) value[n] = str(val)
@ -192,78 +196,73 @@ class GenericDataStructure(object):
return return
def setFieldValue(self, key, value): def setFieldValue(self, key, value):
if self.isField(key): if not self.extraAllowed() and key not in self.getAllowed():
self.getFields()[key] = value raise KeyError
else: else:
print('Warning: trying to set value of non-existent field ' if not self.isField(key):
'{field}'.format(field=key)) print 'creating new field "%s"' % key
self.getFields()[key] = value
def getFields(self): def getFields(self):
return self.__dsFields return self.dsFields
def getExpandFields(self): def getExpandFields(self):
return self.__expandFields return self.expandFields
def setExpandFields(self, keys):
expandFields = []
for key in keys:
if self.isField(key):
expandFields.append(key)
self.expandFields = expandFields
def getAllowed(self):
return self.allowedFields
def extraAllowed(self):
return not self.allowedFields
def updateNotAllowed(self, kwargs):
for key in kwargs:
if key not in self.getAllowed():
kwargs.__delitem__(key)
return kwargs
def hasSuffix(self):
try:
self.getFieldValue('suffix')
except KeyError:
return False
else:
if self.getFieldValue('suffix'):
return True
return False
def expandDataPath(self): def expandDataPath(self):
expandList = [] expandList = []
for item in self.getExpandFields(): for item in self.getExpandFields():
expandList.append(self.getFieldValue(item)) expandList.append(self.getFieldValue(item))
return os.path.join(*self.getExpandFields()) if self.hasSuffix():
expandList.append('*%s' % self.getFieldValue('suffix'))
return os.path.join(*expandList)
class PilotDataStructure(object): class PilotDataStructure(GenericDataStructure):
''' '''
Object containing the data access information for the old PILOT data Object containing the data access information for the old PILOT data
structure. structure.
''' '''
def __init__(self, dataformat='GSE2', fsuffix='gse', def __init__(self, **fields):
root='/data/Egelados/EVENT_DATA/LOCAL/', database='2006.01',
**kwargs):
self.__name = 'PDS'
self.dataFormat = dataformat
self.dataType = 'waveform'
self.__pdsFields = {'root': root,
'database': database,
'suffix': fsuffix
}
self.modifyFields(**kwargs) if not fields:
fields = {'database':'2006.01',
'root':'/data/Egelados/EVENT_DATA/LOCAL', 'suffix':'gse',
'format':'GSE2'}
def modifyFields(self, **kwargs): GenericDataStructure.__init__(self, **fields)
if kwargs and isinstance(kwargs, dict):
for key, value in kwargs.iteritems():
key = str(key)
if type(value) not in (str, int, float):
for n, val in enumerate(value):
value[n] = str(val)
else:
value = str(value)
try:
if key in self.getFields().keys():
self.getFields()[key] = value
else:
raise KeyError('unknown PDS wildcard: %s.' % key)
except KeyError, e:
errmsg = ''
errmsg += 'WARNING:\n'
errmsg += 'unable to set values for PDS fields\n'
errmsg += '%s; desired value was: %s\n' % (e, value)
print errmsg
def getType(self): self.setExpandFields(['root','database'])
return self.dataType
def getFields(self):
return self.__pdsFields
def getName(self):
return self.__name
def expandDataPath(self):
datapath = os.path.join(self.getFields()['ROOT'],
self.getFields()['DATABASE'])
return datapath
class SeiscompDataStructure(GenericDataStructure): class SeiscompDataStructure(GenericDataStructure):
@ -278,8 +277,7 @@ class SeiscompDataStructure(GenericDataStructure):
def __init__(self, rootpath='/data/SDS', dataformat='MSEED', def __init__(self, rootpath='/data/SDS', dataformat='MSEED',
filesuffix=None, **kwargs): filesuffix=None, **kwargs):
super(GenericDataStructure, self).__init__(rootpath, dataformat, super(GenericDataStructure, self).__init__()
filesuffix, **kwargs)
edate = UTCDateTime() edate = UTCDateTime()
@ -300,14 +298,9 @@ class SeiscompDataStructure(GenericDataStructure):
# definitions from # definitions from
# http://www.seiscomp3.org/wiki/doc/applications/slarchive/SDS # http://www.seiscomp3.org/wiki/doc/applications/slarchive/SDS
self.__sdsFields = {'SDSdir': '/data/SDS', # base directory self.dsFields = {'root': '/data/SDS', 'YEAR': year, 'NET': '??',
'YEAR': year, # 4 digits 'STA': '????', 'CHAN': 'HH?', 'TYPE': 'D', 'LOC': '',
'NET': '??', # up to 8 characters 'DAY': '{0:03d}'.format(sdate.julday)
'STA': '????', # up to 8 characters
'CHAN': 'HH?', # up to 8 characters
'TYPE': self.getType(), # 1 character
'LOC': '', # up to 8 characters
'DAY': '{0:03d}'.format(sdate.julday) # 3 digits
} }
self.modifiyFields(**kwargs) self.modifiyFields(**kwargs)
@ -329,18 +322,6 @@ class SeiscompDataStructure(GenericDataStructure):
errmsg += '%s; desired value was: %s\n' % (e, value) errmsg += '%s; desired value was: %s\n' % (e, value)
print errmsg print errmsg
def getType(self):
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): def setFieldValue(self, key, value):
if self.isField(key): if self.isField(key):
self.getFields()[key] = value self.getFields()[key] = value