Skip to content

Instantly share code, notes, and snippets.

@drheinheimer
Last active May 1, 2019 13:35
Show Gist options
  • Save drheinheimer/80c6b290de18600bf492df677371020e to your computer and use it in GitHub Desktop.
Save drheinheimer/80c6b290de18600bf492df677371020e to your computer and use it in GitHub Desktop.
import os
from itertools import product
import win32com.client as client # get win32com from Python package pywin32
# See WEAP API at https://www.weap21.org/WebHelp/WEAPApplication.htm
WEAP = client.Dispatch('WEAP.WEAPApplication') # create WEAP object
WEAP.Areas("Shire_ZAMCOM_v27_AB_ROR").Open() # open the model
realizations = [1,2,3]
catchment_names = ['Kholombidzo'] # TODO: fill in the rest
catchments = ['Bas27_{}'.format(c) for c in catchment_names]
temps = [x for x in range(1)]
precips = [x / 10 for x in range(5,6)]
favorites = ["Mpatamanga Flow"]
inflow_template = 'Inflow\\{full_scenario_name}\\Inflow_{catchment}.csv'
results_folder_template = 'Results\\{full_scenario_name}'
results_path_template = '{results_folder}\\{favorite}.csv'
for realization in realizations:
scenario_name = "realization{}".format(realization)
WEAP.ActiveScenario = scenario_name
for DT, DP in list(product(temps, precips)):
full_scenario_name = 'real_{real}_DT={DT}_DP={DP}'.format(real=realization, DT=DT, DP=DP)
results_folder = os.path.abspath(results_folder_template.format(full_scenario_name=full_scenario_name))
if not os.path.exists(results_folder):
os.makedirs(results_folder)
# Update the input file path
for catchment in catchments:
inflow_path = inflow_template.format(
full_scenario_name=full_scenario_name,
catchment=catchment
)
expression = 'ReadFromFile({})'.format(inflow_path)
WEAP.Branch("\Demand Sites\{}".format(catchment)).Variables("Precipitation").Expression = expression
# Run the model
WEAP.Calculate()
# Load favorite
for favorite in favorites:
WEAP.LoadFavorite(favorite)
results_path = results_path_template.format(results_folder=results_folder, favorite=favorite)
WEAP.ExportResults(results_path, False, False, True)
WEAP.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment