#####
##### STATE VARIABLE SCRIPT INITIALIZATION SECTION
#####

#
# Build the statistical inflow data and save for later use
#
def initStateVariable(currentVariable, network):
	from hec.heclib.dss import HecDss
	from hec.script     import ResSim
	curRun = network.getRssRun()
	dssFilename = curRun.makeAbsolutePathFromWatershed("shared/ventura.dss")
	p25Pathname   = "//CRAZY MOUNTAIN/FLOW-RES IN-P25//1HOUR//"
	p50Pathname   = "//CRAZY MOUNTAIN/FLOW-RES IN-P50//1HOUR//"
	rtw = curRun.getRunTimeWindow()
	startTime = rtw.getStartTimeString()
	endTime = rtw.getEndTimeString()
	dssFile = HecDss.open(dssFilename)
	dssFile.setTimeWindow(startTime, endTime)
	p25Data = dssFile.get(p25Pathname)
	p50Data = dssFile.get(p50Pathname)
	dssFile.done()
	#
	# Build dictionaries of the statistical inflow values keyed
	# by HecTime minute values
	#
	p25ValueByMinutes = {}
	for i in range(p25Data.numberValues) :
		p25ValueByMinutes[p25Data.times[i]] = p25Data.values[i]
	p50ValueByMinutes = {}
	for i in range(p50Data.numberValues) :
		p50ValueByMinutes[p50Data.times[i]] = p50Data.values[i]
	inflowData = {25 : p25ValueByMinutes, 50 : p50ValueByMinutes}
	#
	# Store the data for use on subsequent iterations
	#
	currentVariable.varPut("p25ValueByMinutes", p25ValueByMinutes)
	currentVariable.varPut("p50ValueByMinutes", p50ValueByMinutes)
	currentVariable.varPut("inflowData", inflowData)
	return True

#####
##### STATE VARIABLE SCRIPT COMPUTATION SECTION
#####

#
# Retrieve statistical inflow data
#
p25ValueByMinutes = currentVariable.varGet("p25ValueByMinutes")
p50ValueByMinutes = currentVariable.varGet("p50ValueByMinutes")
inflowData        = currentVariable.varGet("inflowData")
#
# Retrieve the inflow and HecTime minutes values for the current time step
#
inflowTS = network.getTimeSeries("Reservoir","Crazy Mountain", "Pool", "Flow-IN")
inflow = inflowTS.getCurrentValue(currentRuntimestep)
minutes = currentRuntimestep.valueMinutes()
#
# Get the statistical inflow vaules for the current time step
#
p25Inflow = p25ValueByMinutes[minutes]
p50Inflow = p50ValueByMinutes[minutes]
#
# Set the max release based on comparing the current inflow with statistical values
#
if inflow < p25Inflow :
	maxRelease = 4000
elif inflow < p50Inflow :
	maxRelease = 6000
else :
	maxRelease = inflow
#
# set the value of this state variable to our computed value
#
currentVariable.setValue(currentRuntimestep, maxRelease)

#####
##### STATE VARIABLE SCRIPT CLEANUP SECTION
#####

from hec.script import Constants
#
# script to be run only once, at the end of the compute. optional.

# variables that are available to this script during the compute:
# 	currentVariable - the StateVariable that holds this script
# 	network - the ResSim network

# The following represents an undefined value in a time series:
# 	Constants.UNDEFINED

# add your code here...