Skip to content

Commit

Permalink
Merge pull request #2 from Aquaveo/bug_fixes
Browse files Browse the repository at this point in the history
TIME_SERIES_INDEX map table
  • Loading branch information
swainn authored Nov 14, 2019
2 parents ffa3e51 + 8629b60 commit af14c3a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
34 changes: 34 additions & 0 deletions gsshapyorm/lib/cmt_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,40 @@ def sedimentChunk(key, chunk):
return result


def timeSeriesChunk(key, chunk):
# Global variables
numVars = {'NUM_IDS': None,
'MAX_NUMBER_CELLS': None,
'NUM_SED': None,
'NUM_CONTAM': None,
'MAX_SOIL_ID': None}
valueList = []

for line in chunk:
sline = line.strip().split()
token = sline[0]

if token == key:
mtName = sline[0]
elif token in numVars:
# Extract NUM type variables
numVars[sline[0]] = sline[1]
elif token == 'ID':
pass
else:
valueList.append({'index': sline[0], 'ts_name': sline[1]})

# Create return/result object
result = {'name': mtName,
'indexMapName': None,
'numVars': numVars,
'varList': None,
'valueList': valueList,
'contaminants': None}

return result


def _buildVarList(sline, mapTableName, numVars):
# Global constant
IGNORE = ['ID', 'DESCRIPTION1', 'DESCRIPTION2']
Expand Down
67 changes: 66 additions & 1 deletion gsshapyorm/orm/cmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _read(self, directory, filename, session, path, name, extension,
'EVAPOTRANSPIRATION': mtc.mapTableChunk,
'WELL_TABLE': mtc.mapTableChunk,
'OVERLAND_BOUNDARY': mtc.mapTableChunk,
'TIME_SERIES_INDEX': mtc.mapTableChunk,
'TIME_SERIES_INDEX': mtc.timeSeriesChunk,
'GROUNDWATER': mtc.mapTableChunk,
'GROUNDWATER_BOUNDARY': mtc.mapTableChunk,
'AREA_REDUCTION': mtc.mapTableChunk,
Expand Down Expand Up @@ -198,6 +198,10 @@ def _write(self, session, openFile, replaceParamFile=None, writeIndexMaps=True):
mapTable=mapTable,
contaminants=contaminants,
replaceParamFile=replaceParamFile)
elif mapTable.name == 'TIME_SERIES_INDEX':
self._writeTimeSeriesIndex(session=session,
fileObject=openFile,
mapTable=mapTable)
else:
self._writeMapTable(session=session,
fileObject=openFile,
Expand Down Expand Up @@ -290,6 +294,14 @@ def _createGsshaPyObjects(self, mapTables, indexMaps, replaceParamFile, director
# Associate the MTSediment with the MapTable
sediment.mapTable = mapTable

elif mt['name'] == 'TIME_SERIES_INDEX':
for line in mt['valueList']:
ts_index = MTTimeSeriesIndex(
index=line['index'],
timeSeriesName=line['ts_name']
)
ts_index.mapTable = mapTable

# All other map table handler
else:
indexMap = indexMaps[mt['indexMapName']]
Expand Down Expand Up @@ -474,6 +486,21 @@ def _writeSedimentTable(self, session, fileObject, mapTable, replaceParamFile):
fileObject.write('%s%s%s%s%s%s%s\n' % (
sediment.description, ' ' * space1, specGrav, ' ' * 5, partDiam, ' ' * 6, sediment.outputFilename))

def _writeTimeSeriesIndex(self, session, fileObject, mapTable):
# Write the sediment mapping table header
fileObject.write('%s\n' % (mapTable.name))
fileObject.write('NUM_IDS %s\n' % (mapTable.numIDs))

# Write the value header line
fileObject.write('ID Time series name\n')

# Retrieve the time series mapping table values
time_series_indices = mapTable.time_series_indices

for ts in time_series_indices:
spaces = 6 - len(ts.index)
fileObject.write(f'{ts.index}{" " * spaces}{ts.time_series_name}\n')

def _valuePivot(self, session, mapTable, contaminant, replaceParaFile):
"""
This function retrieves the values of a mapping table from the database and pivots them into the format that is
Expand Down Expand Up @@ -777,6 +804,8 @@ class MapTable(DeclarativeBase):
values = relationship('MTValue', back_populates='mapTable', cascade='all, delete, delete-orphan') #: RELATIONSHIP
sediments = relationship('MTSediment', back_populates='mapTable',
cascade='all, delete, delete-orphan') #: RELATIONSHIP
time_series_indices = relationship('MTTimeSeriesIndex', back_populates='mapTable',
cascade='all, delete, delete-orphan') #: RELATIONSHIP

def __init__(self, name, numIDs=None, maxNumCells=None, numSed=None, numContam=None, maxSoilID=None):
"""
Expand Down Expand Up @@ -984,3 +1013,39 @@ def __eq__(self, other):
self.specificGravity == other.specificGravity and
self.particleDiameter == other.particleDiameter and
self.outputFilename == other.outputFilename)


class MTTimeSeriesIndex(DeclarativeBase):
"""
Object containing data in time series index type mapping tables.
See: https://gsshawiki.com/Mapping_Table:Mapping_Tables#12.3.11_Time_Series_Index
"""
__tablename__ = 'cmt_time_series_index'

tableName = __tablename__ #: Database tablename

# Primary and Foreign Keys
id = Column(Integer, autoincrement=True, primary_key=True) #: PK
mapTableID = Column(Integer, ForeignKey('cmt_map_tables.id')) #: FK

# Value Columns
index = Column(String) #: STRING
time_series_name = Column(String) #: STRING

# Relationship Properties
mapTable = relationship('MapTable', back_populates='time_series_indices') #: RELATIONSHIP

def __init__(self, index, timeSeriesName):
"""
Constructor
"""
self.index = index
self.time_series_name = timeSeriesName

def __repr__(self):
return f'<MTTimeSeriesIndex: Index={self.index} TimeSeriesName={self.time_series_name}>'

def __eq__(self, other):
return (self.index == other.index and
self.time_series_name == other.time_series_name)

0 comments on commit af14c3a

Please sign in to comment.