Skip to content

Commit 5bb9eb4

Browse files
authored
Format files using DocumentFormat
1 parent 8a86f87 commit 5bb9eb4

File tree

5 files changed

+146
-110
lines changed

5 files changed

+146
-110
lines changed

src/components/RegionAggregatorSum.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Mimi
22

33
@defcomp RegionAggregatorSum begin
4-
4+
55
inputregions = Index()
66
outputregions = Index()
77

@@ -14,17 +14,17 @@ using Mimi
1414
input = Parameter(index=[time, inputregions])
1515
output = Variable(index=[time, outputregions])
1616

17-
function init(p,v,d)
17+
function init(p, v, d)
1818
idxs = indexin(p.input_output_mapping, p.output_region_names)
1919
!isnothing(findfirst(i -> isnothing(i), idxs)) ? error("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.") : nothing
2020
v.input_output_mapping_int[:] = idxs
2121
end
2222

23-
function run_timestep(p,v,d,t)
23+
function run_timestep(p, v, d, t)
2424
v.output[t, :] .= 0.
2525

2626
for i in d.inputregions
27-
v.output[t, v.input_output_mapping_int[i]] += p.input[t,i]
27+
v.output[t, v.input_output_mapping_int[i]] += p.input[t, i]
2828
end
2929
end
3030
end

src/components/SSPs.jl

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations
55

66
country = Index()
77

8-
SSP_source = Parameter{String}() # can be one of IIASA GDP, OECD Env-Growth, PIK GDP_32, and Benveniste
9-
SSP = Parameter{String}() # can be one of SSP1, SSP2, SSP3, SSP4, SSP5
10-
emissions_source = Parameter{String}() # can be one of Leach
11-
emissions_scenario = Parameter{String}() # can be one of SSP119, SSP126, SSP245, SSP370, SSP585
8+
SSP_source = Parameter{String}() # can be one of IIASA GDP, OECD Env-Growth, PIK GDP_32, and Benveniste
9+
SSP = Parameter{String}() # can be one of SSP1, SSP2, SSP3, SSP4, SSP5
10+
emissions_source = Parameter{String}() # can be one of Leach
11+
emissions_scenario = Parameter{String}() # can be one of SSP119, SSP126, SSP245, SSP370, SSP585
1212

13-
country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension
13+
country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension
1414

15-
population = Variable(index=[time, country], unit="million")
16-
population_global = Variable(index=[time], unit="million")
17-
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
18-
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
15+
population = Variable(index=[time, country], unit="million")
16+
population_global = Variable(index=[time], unit="million")
17+
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
18+
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
1919

20-
co2_emissions = Variable(index=[time], unit="GtC/yr")
21-
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
22-
n2o_emissions = Variable(index=[time], unit="MtN/yr")
23-
sf6_emissions = Variable(index=[time], unit="MtSF6/yr")
20+
co2_emissions = Variable(index=[time], unit="GtC/yr")
21+
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
22+
n2o_emissions = Variable(index=[time], unit="MtN/yr")
23+
sf6_emissions = Variable(index=[time], unit="MtSF6/yr")
2424

25-
function init(p,v,d)
25+
function init(p, v, d)
2626

2727
# ----------------------------------------------------------------------
2828
# Checks
2929

3030
ssp_model_options = ["IIASA GDP", "OECD Env-Growth", "PIK GDP_23", "Benveniste"]
3131
!(p.SSP_source in ssp_model_options) && error("Model $(p.SSP_source) provided to SSPs component SSP_source parameter not found in available list: $(ssp_model_options)")
32-
32+
3333
ssp_options = ["SSP1", "SSP2", "SSP3", "SSP4", "SSP5"]
3434
!(p.SSP in ssp_options) && error("SSP $(p.SSP) provided to SSPs component SSP parameter not found in available list: $(ssp_options)")
35-
35+
3636
emissions_source_options = ["Leach"]
3737
!(p.emissions_source in emissions_source_options) && error("Model $(p.emissions_source) provided to SSPs component emissions_source parameter not found in available list: $(emissions_source_options)")
38-
38+
3939
emissions_scenario_options = ["SSP119", "SSP126", "SSP245", "SSP370", "SSP585"]
4040
!(p.emissions_scenario in emissions_scenario_options) && error("emissions_scenario $(p.emissions_scenario) provided to SSPs component emissions_scenario parameter not found in available list: $(emissions_scenario_options)")
4141

@@ -73,21 +73,21 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations
7373

7474
if !haskey(g_emissions_scenario_datasets, emissions_scenario_dict_key)
7575
if p.emissions_source == "Leach"
76-
emissions_data = load(emissions_path, skiplines_begin = 6)|>
77-
DataFrame |>
78-
i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |>
79-
DataFrame |>
80-
@select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |>
81-
DataFrame
76+
emissions_data = load(emissions_path, skiplines_begin=6) |>
77+
DataFrame |>
78+
i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |>
79+
DataFrame |>
80+
@select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |>
81+
DataFrame
8282
else # already perfect formatted
83-
emissions_data = load(emissions_path)|> DataFrame
83+
emissions_data = load(emissions_path) |> DataFrame
8484
end
8585

86-
g_emissions_scenario_datasets[emissions_scenario_dict_key]= emissions_data
87-
end
86+
g_emissions_scenario_datasets[emissions_scenario_dict_key] = emissions_data
87+
end
8888
end
8989

90-
function run_timestep(p,v,d,t)
90+
function run_timestep(p, v, d, t)
9191

9292
ssp_dict_key = Symbol(p.SSP_source, "-", p.SSP)
9393
emissions_scenario_dict_key = Symbol(p.emissions_source, "-", p.emissions_scenario)
@@ -109,27 +109,27 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations
109109
# t and only the SSP countries found in the model countries list,
110110
# already checked that all model countries are in SSP countries list
111111
subset = g_ssp_datasets[ssp_dict_key] |>
112-
@filter(_.year == gettime(t) && _.country in p.country_names) |>
113-
DataFrame
112+
@filter(_.year == gettime(t) && _.country in p.country_names) |>
113+
DataFrame
114114

115115
# get the ordered indices of the SSP countries within the parameter
116116
# of the model countries, already checked that all model countries
117117
# are in SSP countries list
118118
order = indexin(p.country_names, subset.country)
119119

120-
v.population[t,:] = subset.pop[order]
121-
v.gdp[t,:] = subset.gdp[order]
122-
120+
v.population[t, :] = subset.pop[order]
121+
v.gdp[t, :] = subset.gdp[order]
122+
123123
# add global data for future accessibility and quality control
124-
v.population_global[t] = sum(v.population[t,:])
125-
v.gdp_global[t] = sum(v.gdp[t,:])
124+
v.population_global[t] = sum(v.population[t, :])
125+
v.gdp_global[t] = sum(v.gdp[t, :])
126126

127127
# ----------------------------------------------------------------------
128128
# Emissions
129129

130130
subset = g_emissions_scenario_datasets[emissions_scenario_dict_key] |>
131-
@filter(_.year == gettime(t)) |>
132-
DataFrame
131+
@filter(_.year == gettime(t)) |>
132+
DataFrame
133133

134134
v.co2_emissions[t] = subset.carbon_dioxide[1]
135135
v.ch4_emissions[t] = subset.methane[1]

test/test_Coupled.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ m = Model()
99
set_dimension!(m, :time, 1750:2300)
1010

1111
# Handle the MimiSSPs.SSPs component
12-
add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300)
12+
add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300)
1313
set_dimension!(m, :country, inputregions)
1414
update_param!(m, :SSPs, :country_names, inputregions)
1515

@@ -19,7 +19,7 @@ update_param!(m, :SSPs, :emissions_source, "Leach")
1919
update_param!(m, :SSPs, :emissions_scenario, "SSP119")
2020

2121
# Handle the MimiSSPs.RegionAggregatorSum component
22-
add_comp!(m, MimiSSPs.RegionAggregatorSum, first = 2010, last = 2300)
22+
add_comp!(m, MimiSSPs.RegionAggregatorSum, first=2010, last=2300)
2323

2424
set_dimension!(m, :inputregions, inputregions)
2525
set_dimension!(m, :outputregions, outputregions)
@@ -33,7 +33,7 @@ connect_param!(m, :RegionAggregatorSum, :input, :SSPs, :population)
3333
run(m)
3434

3535
# Should also work if Aggregator runs long, using backup data
36-
Mimi.set_first_last!(m, :RegionAggregatorSum, first = 1750)
36+
Mimi.set_first_last!(m, :RegionAggregatorSum, first=1750)
3737
backup = zeros(551, 184)
3838
connect_param!(m, :RegionAggregatorSum, :input, :SSPs, :population, backup, ignoreunits=true)
3939

test/test_RegionAggregatorSum.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ run(m)
2121

2222
expected_output = zeros(length(2000:2010), length(outputregions))
2323
for (i, output_region) in enumerate(outputregions)
24-
idxs = findall(i -> i == output_region, dummy_input_output[:,2])
25-
expected_output[:, i] = sum(data[:, idxs], dims = 2)
24+
idxs = findall(i -> i == output_region, dummy_input_output[:, 2])
25+
expected_output[:, i] = sum(data[:, idxs], dims=2)
2626
end
2727

2828
@test (m[:RegionAggregatorSum, :output]) expected_output atol = 1e-9
@@ -31,5 +31,11 @@ bad_mapping = dummy_input_output.Output_Region
3131
bad_mapping[5] = "Region-MISSING" # this does not exist in the output regions list
3232
update_param!(m, :RegionAggregatorSum, :input_output_mapping, bad_mapping)
3333

34-
error_msg = (try eval(run(m)) catch err err end).msg
34+
error_msg = (
35+
try
36+
eval(run(m))
37+
catch err
38+
err
39+
end
40+
).msg
3541
@test occursin("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.", error_msg)

0 commit comments

Comments
 (0)