Download Class 22 WEAP Automation and Scenario Analysis

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Screenwriting wikipedia , lookup

Transcript
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
Overview
Up to this point in the module you have built your Salt Lake City WEAP model for
the historical period 1990-1999. Now we want to analyze the system for future
conditions under climate change using our CMIP5 database. In Class session 22 we
will be focused on automating scenario analysis in WEAP and providing a link to the
climate database we created for future projections from CMIP5. We will accomplish
this by using internal scripting in WEAP. The following steps help to guide you
through the steps.
SETTING UP THE SCENARIO ANALYSIS
1. Change the Time Horizon for the Area
Open your WEAP model for the Salt Lake City water system. In the Data or
Schematic view, under the General\Years and Time Steps menu, you can change the
“Time Horizon” of the Area for your scenarios. But we do not want to change them
here.
Current Accounts Year 1990 and Last Year of Scenarios 1999 (unchanged)
2. Describe the Reference Scenario
The “Reference” Scenario always exists. Change its description in the Area\Manage Scenarios
menu to reflect its actual role. Note that you must be in the Data View or Schematic View to
have access to the “Manage Scenarios” option in the Area menu.
Add two new scenarios based on the reference scenario by the name of rcp26 and rcp85. Like
below:
1
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
3. Change the precipitation
To do this manually one would go to the Data View, change the “precipitation” time series file
with the new one you have already created, and then continue to the simulations. For each
scenario the user would switch to the related scenario and enter the new precipitation files. These
scenarios actually present the precipitation for 2091-2099 for two different emission scenarios.
But we do not want to enter data from here! Go to the next part to understand how you can
automate inputting data to WEAP.
2
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
AUTOMATING WEAP DATA INPUT
1. Scripting in WEAP
Scripts can be used with WEAP in two different ways:
I.
Internally: to create more powerful expressions and functions for a WEAP model (e.g.,
create a script to calculate reservoir water quality, and Call the script from a WEAP
expression).
II.
Externally: to automate WEAP via its Application Programming Interface (API) to
perform a sequence of actions (e.g., create and run 100 WEAP scenarios by varying the
value of several parameters (sensitivity analysis), and export the results to Excel for
further analysis).
Go to the Advanced menu and select Scripting>Edit Scripts. Use the Open button to open a
script to be edited. You can open scripts stored in the current Area folder (the area script named
Functions.vbs (Visual Basic Scripting) is a special script that can be called using Call without
specifying the name). Use the Save or Save As buttons to save the script. Use the Clear button to
delete the current script. The editor also supports standard editing options such as cut, copy,
paste, undo, find (Ctrl-F) and find again (F3). Use the Run Script button or press Ctrl-Enter to
run the script. The script editor supports integrated debugging. Syntax or run-time errors in the
script will cause the script to stop running. The type of error, and the line and column at which it
occurred will be reported, and the edit cursor will be placed at the place in the script where the
error occurred, helping you to fix the problem.
At the bottom of the window is a PRINT output pane. Any PRINT messages in your script will
appear here. You can clear this pane using the Clear button on the right of the pane or by issuing
a CLS or CLEAR command in your script.
On the right of the window is a pane with two tabs. These tabs give access to WEAP API objects
and to WEAP Branch and Variable names, which you may want to include in your scripts. Select
an object in the tree and then click the Add button, or double click it, to add it to the edit pane on
the left. Bear in mind that in addition to referring to WEAP objects you can also create
3
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
references to many other COM objects. Many Windows based programs support COM, allowing
you to connect WEAP to other programs such as Excel, Word, PowerPoint, etc.
2. Automating WEAP (API)
WEAP can act as a standard "COM Automation Server," meaning that other programs (e.g.,
Excel via VBA), programming languages (e.g., Visual Basic, C) or scripts (e.g., VB script,
JavaScript, Perl, Python) can control WEAP directly--changing data values, calculating results,
and exporting them to text files or Excel spreadsheets. This can be enormously powerful. For
example, you could write a 10-line script that would run WEAP calculations 100 times, each
time with a different value of an input assumption, and output the results to Excel for later
analysis. WEAP can also call scripts directly (from the Call function in an expression, or from
the Advanced, Scripting menu item), and these scripts can use the WEAP API.
Note: You can find more information about the commands and libraries in WEAP
manual>advanced topics.
3. Connecting WEAP to Database
For the climate change scenarios we defined, we need to pull into WEAP precipitation we
already stored in the database. Therefore, we should first connect WEAP to the MySQL database
server. For this purpose, this code can be used:
Const adOpenStatic = 3
Const adLockOptimistic = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open "DSN=erfan;UID=burian;PWD=burian;Database=cmip5_slc"
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
4
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
You should be careful that when you want to connect to the database you should modify the
properties of your connection based on what you did in the “Preclass 22 Handout” (DSN is the
data source name you select before in ODBC connection).
5. Query and Save Data from Database
Here we want to use the 2090-2099 period to present the precipitation for two scenarios “rcp26”
and “rcp85”. So we need to first query these data and then save them in format of WEAP which
is *.csv files. We will name the files: “pc_pr_rcp26” which means these are precipitations for
2090-2099 for rcp26 scenario and for Parleys Creek. We assume that all months have 30 days
which makes the query simpler. Your code would be like follows for all creeks and different
scenarios:
SQL_Command_String1 = " Select year, month , 30*value_rcp26 from pc_pr where" & _
" 2090<=year and year<=2099 INTO OUTFILE 'pc_pr_rcp26.csv'" & _
"FIELDS TERMINATED BY ','" & _
"ENCLOSED BY ' '" & _
"LINES TERMINATED BY '\n';"
<<<need to repeat for other watersheds and scenarios>>>
Set Rs1 = objConnection.Execute(SQL_Command_String1)
<<<need to repeat for other watersheds and scenarios>>>
The CSV files are saved in (C:\ProgramData\MySQL\MySQL Server 5.6\data\cmip5_slc) by
default and you can change directory but we do not really need to do that (Note: ProgramData
folder is hidden in drive C:\).
5
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
6. Import data to the WEAP
In order to use these files, one simple job is that you go to the Data tab and define the
precipitation files for each creek like you did in the previous class for historical data. But now
based on what you learned in class we want to define them by scripting.
'Define the scenarios'
Dim ScenInfo(1,3)
'WEAP Scenario name in column 1 (index 0)'
ScenInfo(0,0) = "Current Accounts" 'Represents first base year. Data is defined'
ScenInfo(0,1) = "Reference" 'Historical conditions provided in previous class'
ScenInfo(0,2) = "rcp26" 'rcp26'
ScenInfo(0,3) = "rcp85" 'rcp85'
'CSV file name affix in column 2 (index 1)'
ScenInfo(1,0) = ""
ScenInfo(1,1) = ""
ScenInfo(1,2) = ""
ScenInfo(1,3) = ""
'Define watersheds'
Dim WatershedInfo(1,3)
'WEAP watershed name in Column 1 (index 0)'
WatershedInfo(0,0) = "City Creek Watershed"
WatershedInfo(0,1) = "Dell Creek Watershed"
WatershedInfo(0,2) = "Lambs Creek Watershed"
WatershedInfo(0,3) = "Big Cottonwood Creek Watershed"
'CSV data file name in Column 2 that goes with watershed name'
'in Column 1 (index 1)'
WatershedInfo(1,0) = "cc_pr" ' CSV file name '
WatershedInfo(1,1) = "pc_pr"
6
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
WatershedInfo(1,2) = "pc_pr"
WatershedInfo(1,3) = "bc_pr"
Dim i 'watershed index'
Dim s 'Scneario index'
'Loop over the scenarios'
FOR s = 2 to ubound(ScenInfo,2) 'we already defined'
'the historical CSV files for Current and Refrence scenarios'
Set Sc = WEAP.Scenarios("" & ScenInfo(0,s))
'The first double quotes forces the parameter to a text string'
PRINT ""
PRINT SC.Name & " is now active"
Sc.Activate 'Activate the scenario so data inputs are for that scenario only'
'Work on the watersheds'
FOR i=0 to ubound( WatershedInfo,2)
Set CurrWts = WEAP.Branch("\Demand Sites\" & WatershedInfo(0,i))
CurrWts.Variables("Precipitation").Expression =
"ReadFromFile(C:\ProgramData\MySQL\MySQL Server 5.6\data\cmip5_slc\" &
WatershedInfo(1,i) & "_" & SC.Name & ".csv, , 2090)"
PRINT "Updated " & CurrWts.Name & " with Precipitation " &
CurrWts.Variables("Precipitation").Expression
Next
NEXT
7. Compare Results for the Reference and Climate Change Scenarios
7
Hydroinformatics
Class 22 – WEAP Automation and Scenario Analysis
In-Class Exercise Handout
Compare, graphically, the results for the two scenarios we have established so far (rcp26 and
rcp85). (You can select demand sites and exclude watersheds from the pull-down menu in the
bottom of the chart).
For your homework report you should attach your script and also analyze and compare the result
of scenarios with reference period.
Discussion Questions
We did not use temperature data from the climate change projections in the WEAP simulations.
Why are we not using them here? What will be changed if you want to use the temperature in
your simulation?
8