• 455 views | 4 messages Discussion: LEAP
    Topic: LEAP API: Exporting energy by branch with multiple fuels Subscribe | Previous | Next
  • Sharad Bharadwaj 10/3/2018

    Hi all,

    I'm trying to automate export of energy demand by branch and fuel, but for tech branches with multiple fuels. For example, if I have a stock technology with multiple fuels (e.g. natural gas furnace which burns natural gas and biogas) has anyone been able to automate export of the fuel burn within each branch type? I found a script posted previously for automating fuel demand at the technology branch level, which is very helpful - I'd like to go one level detail if possible, but I'm not sure how to do so. Here is the script which automates export at the tech branch level:

    Thanks!
    Sharad

    '============================================
    ' 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")
    'dim tagnames, taggroups As String

    set xlw = xla.Workbooks.Add
    For i = 2 to Leap.Regions.Count
    Leap.Regions(i).Active = True
    V = Leap.Regions(i).Active
    tech_branches.RemoveAll
    for each b in LEAP.Branches
    if b.Branchtype = 4 then
    tech_branches.Add b.BranchID, b.FullName
    end if
    next

    xla.Visible = true
    xla.ScreenUpdating = true

    LEAP.Verbose = 4
    LEAP.Visible = true

    set xls = xla.sheets.add
    xls.name = LEAP.Regions(i).Name

    ' 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 = "Region"
    xls.Cells(r, 5).Value = "Energy"

    LEAP.ActiveView = "Results"

    For s = 4 to 4
    LEAP.Scenarios(s).Active = true
    for y = LEAP.BaseYear to LEAP.EndYear
    for each item in tech_branches.items
    r = r + 1
    xls.Cells(r, 1).Value = s
    xls.Cells(r, 2).Value = y
    xls.Cells(r, 3).Value = LEAP.Branch(item).FullName
    xls.Cells(r, 4).Value = LEAP.Regions(i).Name
    xls.Cells(r, 5).Value = LEAP.Branch(item).Variable("Energy Demand Final Units").Value(y, "Million BTU")
    next
    next
    next
    next

    xla.screenupdating = true
    xla.visible = true
    LEAP.ActiveView = "Analysis"

  • Taylor Binnington 10/10/2018
      Best Response

    Hi Sharad -

    Try replacing this line:

    xls.Cells(r, 5).Value = LEAP.Branch(item).Variable("Energy Demand Final Units").Value(y, "Million BTU")

    ...with something like this:

    xls.Cells(r, 5).Value = LEAP.Branch(item).Variable("Energy Demand Final Units").Value(y, "Million BTU", "fuel=Electricity")

    ...where the "fuel=..." arguement can be modified to include the name of the specific fuel demand you're trying to export.

    Hope this helps,
    Taylor

  • Gregers Larsen 11/1/2018
      Best Response

    Hello Taylor!

    I am trying to do something similar, mostly to gain experience with the API and understand it. I have updated the expression to using "Branches" and "Variables", as they seem to be the updated method.

    I have made the short macro below in VBA, based on the Freedonia model. With this code, I get no errors, but my result is always 0. Removing fuel filter, year and unit changes nothing. What am I doing wrong?

    Sub SimpleLEAPTest()

    Dim L As LEAPApplication
    Dim FuelType As String

    Set L = CreateObject("LEAP.LEAPApplication")

    'L.ActiveView = "Results"

    FuelType = "Charcoal" 'Define Fuel for the branch
    Cells(1, 2).Value = L.Branches(44).Variables.Item("Energy Demand Final Units").Value(2015, "Gigajoule", "fuel=" & FuelType) 'Try to get the Energy Demand result, returns 0
    Cells(1, 1).Value = L.Branches(44).FullName 'Gets the path of the branch (works)


    End Sub


  • Gregers Larsen 11/5/2018
      Best Response

    Never mind! I figured it out!

    I needed to specify the scenario first, otherwise I don't get any results. Freedonia only has 1 Region, otherwise I suspect there would be the same issue with regions, just FYI.


    Gregers Larsen