WEAPScenario and WEAPScenarios API Classes

The WEAPScenario class represents a single WEAP scenario, whereas WEAPScenarios is the collection of all scenarios in the active area (including Current Accounts).

The WEAPScenarios collection is a property of the WEAPApplication class, e.g., WEAP.Scenarios

You can get access to a WEAPScenario in three different ways:

  1. WEAPApplication.Scenarios(ScenarioName or Index), specifying either the name of the scenario or a number from 1 to WEAPApplication.Scenarios.Count, e.g., WEAP.Scenarios("Current Accounts") or WEAP.Scenarios(1)

  2. WEAPApplication.ActiveScenario, e.g., WEAP.ActiveScenario

  3. Iterate through the collection of scenarios, e.g., For Each Scenario in WEAP.Scenarios

WEAPScenarios Properties and Methods

Example (using VB script)

Add(NewScenarioName, ParentScenarioName or Index): Create a new scenario as a child of the parent scenario specified.  The new scenario will become the selected scenario in the Data View.  To delete a scenario, use the WEAPScenario method Delete (see below).

WEAP.Scenarios.Add("Larger reservoir", "Supply Measures")

Count: Get the number of WEAP scenarios in the active area. Read only.

FOR i = 1 to WEAP.Scenarios.Count
  PRINT WEAP.Scenarios(i).Name
NEXT

Exists(ScenarioName): Returns true if ScenarioName exists.  Read only.

 

IF WEAP.Scenarios.Exists("Supply Measures") THEN
WEAP.ActiveScenario = "Supply Measures"  

END IF

Item(ScenarioName or Index): Get the scenario identified by name or index (from 1 to Scenarios.Count).  Item is the "default" property, and therefore is usually omitted. Thus, the first two examples to the right are equivalent.

WEAP.Scenarios.Item("Current Accounts").Activate
WEAP.Scenarios("Current Accounts").Activate
WEAP.Scenarios(1).Activate

NameList: Get a list of scenarios separated by character specified by optional parameter ListSeparator (which defaults to comma). Read only.

PRINT "Scenarios: " & WEAP.Scenarios.NameList

ResultsShown: Set or get for all the scenarios whether their results will be shown in the Results View (calculating if necessary).

WEAP.Scenarios.ResultsShown = FALSE

 

WEAPScenario Properties and Methods

Example (using VB script)

Activate: Make this scenario the active scenario.  The active scenario will be used when accessing a WEAPVariable.Expression.

WEAP.Scenarios("Current Accounts").Activate

WEAP.Branch("\Demand Sites\South City").Variables("Annual Activity Level").Expression = 100  ' Set the South City Activity Level for Current Accounts to 100
 

Note: This is equivalent to WEAP.ActiveScenario = "Current Accounts"

CalculateNextTimestep: Calculate the next timestep in interactive mode.  Interactive calculations must already be initialized.  Returns True if successfully calculated the next timestep and it is not the last timestep of the scenario. False if there was an error during calculations, or if the last timestep of the scenario was just calculated.  If there was an error, WEAP will automatically terminate interactive calculations, and set WEAPApplication.Status = False.  See InitializeInteractiveCalculations below for information on interactive calculations.

WHILE WEAP.Scenario("Reference").CalculateNextTimestep

  PRINT "Just calculated " & WEAP.TimeStepName(WEAP.CalcTS) & " " & WEAP.CalcYear & " in Scenario " & WEAP.Scenario("Reference").Name

  PRINT "Unmet demand for South City in " & WEAP.ActiveScenario.Name & " " & WEAP.CalcTS & ", " & WEAP.CalcYear & " is " & WEAP.ResultValue("\Demand Sites\South City:Unmet Demand", WEAP.CalcYear, WEAP.CalcTS)

WEND

Delete(DeleteChildren): Delete this scenario.  To delete a scenario that has children, you must set the optional DeleteChildren parameter to True.

WEAP.Scenarios("Integrated Measures").Delete

WEAP.Scenarios("Reference").Delete(True)

FinalizeInteractiveCalculations(ErrorMessage): Stops interactive calculations for this scenario.  If optional parameter ErrorMessage is given, the error message will be shown to user and added to "Messages" tab in Results View.  See InitializeInteractiveCalculations below for information on interactive calculations.

WEAP.Scenario("Reference").FinalizeInteractiveCalculations

 

WEAP.FinalizeInteractiveCalculations("Error reading results from external model")

FirstYear: Get the first year of the scenario.  If the scenario is Current Accounts, this is equivalent to WEAP.BaseYear; if not Current Accounts, this is equivalent to WEAP.BaseYear + 1.  Read only.

SET S = WEAP.Scenario("Reference")

PRINT "The period of scenario " & S.Name & " is " & S.FirstYear & " to " & S.LastYear

ID: Get WEAP's internal ID code of the scenario. Most users will not ever need this information. Read only.

PRINT "ID code for "; WEAP.ActiveScenario.Name; "is "; WEAP.ActiveScenario.ID

InitializeInteractiveCalculations: Starts interactive calculations for a scenario.  Returns True if successful, False if there was a problem.  

Interactive calculations make it possible to calculate a single timestep in WEAP and then perform other tasks in a script before calculating the next timestep, such as transferring WEAP results for the timestep just calculated to another model that is run and results from it are imported back into WEAP.  During the pause between timestep calculations, you may change data expressions; you can change any CSV files that WEAP is reading via the ReadFromFile expression and that new file will be used for subsequent timesteps' calculations; and you can get access to result values that have been calculated in this or a previous timestep, using WEAP.ResultValue.

See also: WEAPApplication.InitializeInteractiveCalculations, WEAPApplication.FinalizeInteractiveCalculations, WEAPApplication.IsCalculatingInteractively, WEAPScenario.CalculateNextTimestep, WEAPScenario.IsCalculatingInteractively and WEAPScenario.FinalizeInteractiveCalculations.

IF WEAP.InitializeInteractiveCalculations THEN

  FOR EACH Scen IN WEAP.Scenarios

    IF WEAP.Status AND Scen.ResultsShown AND Scen.InitializeInteractiveCalculations THEN

      KeepCalculating = TRUE   

      WHILE KeepCalculating

        KeepCalculating = Scen.CalculateNextTimestep    ' CalculateNextTimestep = FALSE after last timestep in scenario is calculated, or if there was an error

        You may perform some other tasks...    ' Use WEAP.CalcYear and WEAP.CalcTS to determine which timestep was just calculated

      WEND

      Scen.FinalizeInteractiveCalculations

    END IF

  NEXT

  WEAP.FinalizeInteractiveCalculations

  IF WEAP.Status = FALSE THEN

    PRINT "There was an error calculating."

  END IF

END IF

 

SET S = WEAP.Scenario("Reference")

IF WEAP.InitializeInteractiveCalculations AND S.InitializeInteractiveCalculations THEN

  FOR y = S.FirstYear to S.LastYear

    FOR ts = 1 TO WEAP.NumTimeSteps

      S.CalculateNextTimestep

      Perform some other tasks...

    NEXT

  NEXT

  S.FinalizeInteractiveCalculations

  WEAP.FinalizeInteractiveCalculations

END IF

IsCalculatingInteractively: True if WEAP is calculating interactively this scenario, False if not.  See InitializeInteractiveCalculations above for information on interactive calculations.  Read Only.

IF NOT WEAP.Scenario("Reference").IsCalculatingInteractively THEN

   WEAP.Scenario("Reference").InitializeInteractiveCalculations

IsCurrentAccounts: Is TRUE if the scenario is the Current Accounts, FALSE otherwise. Read only.

IF WEAP.ActiveScenario.IsCurrentAccounts = FALSE THEN WEAP.ActiveScenario.ResultsShown = TRUE

LastYear: Get the last year of the scenario.  This is equivalent to WEAP.EndYear.  Read only.

SET S = WEAP.Scenario("Reference")

PRINT "The period of scenario " & S.Name & " is " & S.FirstYear & " to " & S.LastYear

Name: Get the name of the scenario. Read only.

PRINT WEAP.Scenarios(1).Name

NeedsCalculation: Set or get whether a scenario needs to be recalculated.  USE WITH CAUTION -- results may be incorrect if calculations needs to be run but you mark them as calculated.

NumScenariosToCalculate = 0

FOR i = 1 to WEAP.Scenarios.Count;
  IF WEAP.Scenarios(i).NeedsCalculation THEN NumScenariosToCalculate = NumScenariosToCalculate + 1

NEXT

' To set all scenarios as calculated:

FOR EACH Scen IN WEAP.Scenarios
Scen.NeedsCalculation = FALSE

NEXT

ResultsShown: Set or get whether the scenario's results will be shown in the Results View (calculating if necessary).

WEAP.ActiveScenario.ResultsShown = TRUE