• 378 views | 6 messages Discussion: LEAP
    Topic: Mass extraction of all resultsSubscribe | Previous | Next
  • Gregers Larsen 12/14/2015

    705 Views

    Hi LEAP people!

    I am working on a large project that uses three different models, where one of them in made in LEAP. All the results then have to go through Tableau to make dynamic result visualisation.

    To do this, I need to extract all the results from my model, preferably in Pivot-friendly datasets.

    I there a way I can extract all the results from the Result view, meaning every energy demand from evert sub-branch, fuel and more? Then we can aggregate the data in Tableau or through the database.

    I know this may be a complicated issue, but I hope anyone has experience with this.


    Gregers
  • Taylor Binnington 12/14/2015
      Best Response

    704 Views

    Hi Gregers,

    Although LEAP's result-displaying capabilities are quite flexible, it's possible that you may need to export a large number of results to perform some additional analysis outside of LEAP. If this is necessary, I would approach this in one of two ways.

    First, and since it sounds like you're actually only trying to export final energy demands, I would suggest viewing Demand: Energy Demand Final Units, having selected the top-level Demand branch of your tree. Display by branches with years along the x-axis, and change the number of branch "levels" until the necessary level of detail has been achieved. You'll want to make sure that "Subtotals" is unchecked. You may then export the table view to Excel following the usual procedure, for each fuel separately (see attached image). This will give you several wide-format databases in separate Excel worksheets.

    The second option is to use LEAP's API. It's relatively straightforward to loop through the various demand branches and print the scenario, year, branch, fuel and energy demand as separate columns in a long-format database. This operation requires familiarity with the LEAP API, which you can begin learning about here:

    http://www.energycommunity.org/WebHelpPro/API/API.htm

    The simplest way to run calls to the API is through the built-in script editor:

    http://www.energycommunity.org/WebHelpPro/API/Using_the_Script_Editor.htm

    Hope this sets you on the rights track,

    Taylor

    export_preparation.jpg
  • Gregers Larsen 12/14/2015
      Best Response

    698 Views

    Looking at it a little further when I had written the post, I concluded something similar.

    As I want one big database with all scenarios and fuels, which is not written in the output data, I think the best solution is to programme it in Excel VBA with extraction through the LEAP object. I will work on that for the next couple of weeks and post my code when I am done so other might be able to use it too.

    Thank you for your help,


    Gregers
  • Taylor Binnington 12/15/2015
      Best Response

    697 Views

    Hi Gregers,

    Hopefully this helps you get started. You should be able to drop everything between the dashed ("-") lines into LEAP's internal script editor and click "Run Script".

    ----------------------------------------------------

    ' ============================================
    ' Bulk energy demand export tool for LEAP
    ' Taylor Binnington, Stockholm Environment Institute
    ' Copyright 2015
    ' ============================================

    ' Instantiate objects
    ' ============================================
    If Not IsObject(LEAP) Then
    set LEAP = CreateObject("Leap.LEAPApplication")
    End If

    set xla = CreateObject("Excel.Application")
    ' ============================================

    dim tech_branches
    set tech_branches = CreateObject("Scripting.Dictionary")

    for each b in LEAP.Branches
    if b.Branchtype = 4 then
    tech_branches.Add b.BranchID, b.Name
    end if
    next

    xla.Visible = false
    xla.ScreenUpdating = true

    set xlw = xla.Workbooks.Add

    LEAP.Verbose = 4
    LEAP.Visible = true

    set xls = xla.sheets.add
    xls.name = "Exported from LEAP"

    ' Add headers to Excel worksheet
    r = 1
    xls.Cells(r, 1).Value = "Scenario"
    xls.Cells(r, 2).Value = "Year"
    xls.Cells(r, 3).Value = "Branch"
    xls.Cells(r, 4).Value = "Fuel"
    xls.Cells(r, 5).Value = "Value (GJ)"

    ' Loop through scenarios, years, branches and export final energy demand in gigajoules
    for each s in LEAP.Scenarios
    for y = LEAP.BaseYear to LEAP.EndYear
    for each key in tech_branches.keys
    r = r + 1
    xls.Cells(r, 1).Value = s
    xls.Cells(r, 2).Value = y
    xls.Cells(r, 3).Value = LEAP.Branch(key).FullName
    xls.Cells(r, 4).Value = LEAP.Branch(key).Fuel.Name
    xls.Cells(r, 5).Value = LEAP.Branch(key).Variable("Energy Demand Final Units").Value(y, "Gigajoule")
    next
    next
    next

    xla.screenupdating = true
    xla.visible = true

    ----------------------------------------------------

    Take care,
    Taylor
  • Gregers Larsen 1/18/2016
      Best Response

    520 Views

    I have been trying to work with this a bit, but have run into a problem You may have an answer to.

    I would like to loop through the different fuels in the result of a scenario, with some specifications on the level of the deepness of the result tables.

    Going through the API in the script editor, the Level of results in Branch objects is read-only. Can it really be true that I cannot set the branch level of my results before I copy the tables in the API?
  • Taylor Binnington 1/19/2016
      Best Response

    519 Views

    Hi Gregers,

    You're correct that Branch.Level is readonly. Allowing the user to set a branch level could be ambiguous.

    Can you get around this by simply setting the active branch equal to the branch for which you want to export results? For a batch export, you might consider creating a dictionary of branches and exporting results from each, through a loop.

    Taylor