Overview

To view variables in the REPL, you must first activate the model's Julia environment, load ENERGY 2100's Julia package, import the packages you want to use, and import the model's global structures you want to use.

Once you have a VS Code session set up, you are ready to view variables from the REPL.

There are multiple ways to view variables from the Julia REPL, including: 

The sections below provide instructions for each of these methods. 

The instructions that follow assume you have performed all the steps above to import the required functions and packages into your Julia workspace.

Write Variable to a CSV file using a DataFrame

This section provides instructions on writing to CSV files by first assigning the variable to a DataFrame, then using CSV.write to save it to a file.


๐Ÿ›ˆ

A DataFrame holds the variable in an easy-to-view format with set identifiers. The instructions below apply only to variables that are saved on the database (DB).

To write variables to a CSV file from the Julia REPL, follow the instructions below:

  1. Assign the desired variable to a DataFrame:

df = ReadDisk(DataFrame,DB,"DBPartition/VariableName")

The example below shows a DataFrame of the residential xPkSav variable, which is stored on RInput in the database:


๐Ÿ•ฎ

Example

df = ReadDisk(DataFrame,DB,"RInput/xPkSav")

Julia Output

  1. Use the CSV.write function to save the DataFrame (df) to a file:

CSV.write("output.csv",df)


๏‚

By default, the .csv file will be saved to the root model directory. If you want to store the .csv files to a separate folder, first create the folder, then revise the command include the folder name:  CSV.write("newfolder/output.csv", df)

    • You can append the variable to an existing csv file by using append = true.

CSV.write("output.csv", df, append=true)

    • If you donโ€™t want to read any zero values, you can set the skip_zeros keyword argument to true:

df = ReadDisk(DataFrame,DB,"DBPartition/VariableName",skip_zeros = true)


๐Ÿ•ฎ

Example

df = ReadDisk(DataFrame,DB,"RInput/xPkSav",skip_zeros = true)


Julia Output


View Variable in REPL Using a DataFrame Subset

Most variable arrays in ENERGY 2100 are too large to view in their entirety as a DataFrame directly in the REPL. However, you can see a slice of the variable directly in the REPL using the @rsubset macro (from the DataFramesMeta package). 

The @rsubset macro allows you to filter rows of a DataFrame. To view a slice of a variable in the REPL, follow the instructions below:

  1. Save the variable as a DataFrame:

df = ReadDisk(DataFrame,DB,"DBPartition/VariableName")

  1. Create a subset of the DataFrame by selecting specific elements of the variable's sets: 

df_subset = @rsubset df begin

:set1 == "SetKey"

:set2 == "SetKey"

...

end


๐Ÿ•ฎ

Exercise

Create a subset of a residential xPkSav DataFrame that selects British Columbia, Single Family Detached, Lighting for year 2030.

View Solution
df_subset = @rsubset df begin
:EC == "SingleFamilyDetached"
:Enduse == "Light"
:Area == "BC"
:Year == 2030
end


๏

You must select the set elements using set keys rather than set descriptors.

To select more than one set element in a given set, use the logical "or" operator: (||):

df_subset = @rsubset df begin

:set1 == "Set1Key1" || :set1 == "Set1Key2" || :set1 == "Set1Key3"

:set2 == "Set2Key"

...

end


๐Ÿ•ฎ

Exercise

Create a DataFrame subset of the residential variable, xPkSav, using @rsubset. Select 3 ECs: SingleFamilyDetached, SingleFamilyAttached, and MultiFamily for British Columbia Lighting in the year 2030.

View Solution
df_subset = @rsubset df begin
:EC == "SingleFamilyDetached" || "SingleFamilyAttached" || "MultiFamily"
:Enduse == "Light"
:Area == "BC"
:Year == 2030
end

Alternatively, to select more than one set element in a given set, you can also use the in operator, which acts as a filter to check if elements in a DataFrame column exist within a specified collection:

df_subset = @rsubset df begin

:set1 in ["Set1Key1", "Set1Key2", "Set1Key3"]

:set2 == "Set2Key"

...

end


๐Ÿ•ฎ

Exercise

Modify the DataFrame subset you created above for the residential variable, xPkSav, using @rsubset. Select 3 ECs: SingleFamilyDetached, SingleFamilyAttached, and MultiFamily using the ":EC in [...]" method to select the set elements.

View Solution
df_subset = @rsubset df begin
:EC in ["SingleFamilyDetached","SingleFamilyAttached","MultiFamily"]
:Enduse == "Light"
:Area == "BC"
:Year == 2030
end

To select a range of years, use the less than or equal to operator (<=): 

df_subset = @rsubset df begin

:set1 in ["SetKey","SetKey","SetKey",...]

:set2 == "SetKey"

...

firstyear <= :Year <= lastyear

end


๐Ÿ•ฎ

Exercise

Modify the DataFrame subset you created above for the residential variable, xPkSav, using @rsubset. Replace the year selection of 2030 with a range of years: 2028 to 2030.

View Solution
df_subset = @rsubset df begin
:EC in ["SingleFamilyDetached","SingleFamilyAttached","MultiFamily"]
:Enduse == "Light"
:Area == "BC"
2028 <= :Year <= 2030
end

  1. View a DataFrame or DataFrame subset just by typing the name of the DataFrame into the REPL:

dfname <enter>

df_subsetname <enter>


๐Ÿ•ฎ

Exercise

View the DataFrame and the DataFrame subset you created in the previous exercises.

View Solution
julia> df
46900ร—5 DataFrame
   Row โ”‚ Enduse   EC                    Area    Year   Value   
       โ”‚ String   String                String  Int64  Float32
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
     1 โ”‚ Heat     SingleFamilyDetached  ON       1985     99.0
     2 โ”‚ HW       SingleFamilyDetached  ON       1985     99.0
     3 โ”‚ OthSub   SingleFamilyDetached  ON       1985     99.0
     4 โ”‚ Refrig   SingleFamilyDetached  ON       1985     99.0
     5 โ”‚ Light    SingleFamilyDetached  ON       1985     99.0
   โ‹ฎ   โ”‚    โ‹ฎ              โ‹ฎ              โ‹ฎ       โ‹ฎ       โ‹ฎ
 46897 โ”‚ Refrig   OtherResidential      ROW      2051     99.0
 46898 โ”‚ Light    OtherResidential      ROW      2051     99.0
 46899 โ”‚ AC       OtherResidential      ROW      2051     99.0
 46900 โ”‚ OthNSub  OtherResidential      ROW      2051     99.0
                                             46891 rows omitted
julia> df_subset
9ร—5 DataFrame
 Row โ”‚ Enduse  EC                    Area    Year   Value   
     โ”‚ String  String                String  Int64  Float32
โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
   1 โ”‚ Light   SingleFamilyDetached  BC       2028  4.77305
   2 โ”‚ Light   SingleFamilyAttached  BC       2028  1.12756
   3 โ”‚ Light   MultiFamily           BC       2028  1.89535
   4 โ”‚ Light   SingleFamilyDetached  BC       2029  5.02847
   5 โ”‚ Light   SingleFamilyAttached  BC       2029  1.20767
   6 โ”‚ Light   MultiFamily           BC       2029  2.05367
   7 โ”‚ Light   SingleFamilyDetached  BC       2030  5.00835
   8 โ”‚ Light   SingleFamilyAttached  BC       2030  1.22271
   9 โ”‚ Light   MultiFamily           BC       2030  2.102

Write println Statements in the REPL

To view a variable in the REPL using println, first read the variable and its sets into your workspace, then loop through the variable's sets and use the Julia println as described in the instructions below.

  1. Identify the sets of the variable you want to view. 

You can find out more about a variable's sets by importing and using the attributes function data_attrs:

import EnergyModel.data_attrs

data_attrs(DB, "DBPartition/VariableName")

The example below shows the results of calling the data attributes function for the variable xPkSav in the industrial sector (stored on IInput):


๐Ÿ•ฎ

Example

julia> import EnergyModel.data_attrs
julia> data_attrs(DB, "IInput/xPkSav")

๐Ÿ—Ž Julia Output

Dict{String, Any} with 7 entries:
  "units"             => "MW"
  "doc"               => "Exogenous Peak Savings (MW)"
  "setdesc_locations" => ["IInput/EnduseDS", "IInput/ECDS", "MainDB/AreaDS", "MainDB/YearDS"]
  "dims"              => ["Enduse", "EC", "Area", "Year"]
  "size"              => (6, 42, 25, 67)
  "ndims"             => 4
  "type"              => "variable"
  

From the output of data_attrs, we see that xPkSav is defined by the sets  "Enduse", "EC", "Area", and "Year".

  1. Read the desired variable and sets into your workspace. 

By importing and using ReadDiskAndSets, you will read in the variable and sets to the names you specify on the left-hand-side of the equation as follows:

import EnergyModel.ReadDiskAndSets
(;VariableName, Set1Name, Set2Name, ...) = ReadDiskAndSets(DB, "DBPartition/VariableName")


๐Ÿ•ฎ

Exercise

Import Read the variable and sets for xPkSav from the industrial sector. If you don't know the sets, first find them using the data.attrs function.

View Solution
julia> import EnergyModel.ReadDiskAndSets
julia> (;xPkSav,Enduse,EC,Area,Year) = ReadDiskAndSets(DB, "IInput/xPkSav")

  1. Select sets and write desired variable values using println:

set1s = Select(Set1,["Set1Key","Set2Key",...])
set2s = Select(Set2,"Set2Key")
years = collect(Yr(year1):Yr(yearlast))
for enduse in set1 in set1s, set2 in set2s,...,year in years
    println("VariableName(",Set1[set1],",",Set2[set2],",",Year[year],")=", VariableName[set1,set2,...,year])
end


๐Ÿ•ฎ

Exercise

Generate the .dta outputs created by ElectricSummary.jl (stored in Output/ExcelOutput). Locate and view the .dta files that were generated.

View Solution
enduses = Select(Enduse,"Heat")
ecs = Select(EC,["PulpPaperMills","IronSteel"])
areas = Select(Area,"BC")
years = collect(Yr(2024):Yr(2026))
for enduse in enduses, ec in ecs, area in areas, year in years
    println("xPkSav(",Enduse[enduse],",",EC[ec],",",Area[area],",",Year[year],")=", xPkSav[enduse,ec,area,year])
end

๐Ÿ—Ž Julia Output

xPkSav(Heat,PulpPaperMills,BC,2024) = 0.0
xPkSav(Heat,PulpPaperMills,BC,2025) = 1.2080482
xPkSav(Heat,PulpPaperMills,BC,2026) = 1.1113358
xPkSav(Heat,IronSteel,BC,2024) = 0.0
xPkSav(Heat,IronSteel,BC,2025) = 0.1035406
xPkSav(Heat,IronSteel,BC,2026) = 0.0951572


Run an Existing Output .jl File from the REPL

To run an output file from the REPL, follow the instructions below:

  1. Include the path to the desired output file in your workspace:

include(Output/ExcelOutput/FileName.jl")

include(Output/AccessOutput/SubfolderName/FileName.jl")

  1. Call the control function that is at the bottom of the output file:

ModuleName_DtaControl(DB)

Output will be generated and saved with .dta extensions in the \2020Model\out folder.



๐Ÿ•ฎ

Exercise

Generate the .dta outputs created by ElectricSummary.jl (stored in Output/ExcelOutput). Locate and view the .dta files that were generated.

View Solution
include(Output/ExcelOutput/ElectricSummary.jl")
ElectricSummary_DtaControl(DB)

๐Ÿ—Ž Julia Output


  Output Folder