39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/urbanopt/scenario/scenario_visualization.rb', line 39
def self.create_visualization(run_dir, feature = true)
@all_results = []
run_dir.each do |folder|
name = folder.split('/')[-1]
if feature == false
csv_dir = File.join(folder, 'default_scenario_report.csv')
elsif feature == true
csv_dir = File.join(folder, 'feature_reports/default_feature_report.csv')
end
if File.exist?(csv_dir)
size = CSV.open(csv_dir).readlines.size
monthly_values = {}
monthly_totals = {}
annual_values = {}
= []
i = 0
CSV.foreach(csv_dir).map do |row|
if i == 0
= row
.each do ||
= .to_s.split('(')[0]
<<
monthly_values[] = []
end
elsif i <= size
.each_index do |j|
monthly_values[[j]] << row[j]
end
end
i += 1
end
if monthly_values['Datetime'][0].split(/\W+/)[0].to_f > 31
format = '%Y/%m/%d %H:%M'
year = monthly_values['Datetime'][0].split(/\W+/)[0]
else
format = '%m/%d/%Y %H:%M'
year = monthly_values['Datetime'][0].split(/\W+/)[2]
end
jan_date = DateTime.new(year.to_i, 1, 1, 1, 0)
feb_date = DateTime.new(year.to_i, 2, 1, 0, 0)
mar_date = DateTime.new(year.to_i, 3, 1, 0, 0)
apr_date = DateTime.new(year.to_i, 4, 1, 0, 0)
may_date = DateTime.new(year.to_i, 5, 1, 0, 0)
jun_date = DateTime.new(year.to_i, 6, 1, 0, 0)
jul_date = DateTime.new(year.to_i, 7, 1, 0, 0)
aug_date = DateTime.new(year.to_i, 8, 1, 0, 0)
sep_date = DateTime.new(year.to_i, 9, 1, 0, 0)
oct_date = DateTime.new(year.to_i, 10, 1, 0, 0)
nov_date = DateTime.new(year.to_i, 11, 1, 0, 0)
dec_date = DateTime.new(year.to_i, 12, 1, 0, 0)
jan_next_year = DateTime.new(year.to_i + 1, 1, 1, 0, 0)
monthly_values['Datetime'].each do |i|
date_obj = DateTime.strptime(i.to_s, format)
index = monthly_values['Datetime'].index(i)
if feb_date == date_obj
@feb_index = index
elsif mar_date == date_obj
@mar_index = index
elsif apr_date == date_obj
@apr_index = index
elsif may_date == date_obj
@may_index = index
elsif jun_date == date_obj
@jun_index = index
elsif jul_date == date_obj
@jul_index = index
elsif aug_date == date_obj
@aug_index = index
elsif sep_date == date_obj
@sep_index = index
elsif oct_date == date_obj
@oct_index = index
elsif nov_date == date_obj
@nov_index = index
elsif dec_date == date_obj
@dec_index = index
elsif jan_next_year == date_obj
@jan_next_year_index = index
end
end
.each_index do |j|
i = 0
k = 0
monthly_sum_jan = monthly_sum_feb = monthly_sum_mar = monthly_sum_apr = monthly_sum_may = monthly_sum_jun = monthly_sum_jul = monthly_sum_aug = monthly_sum_sep = monthly_sum_oct = monthly_sum_nov = monthly_sum_dec = annual_sum = 0
all_values = monthly_values[[j]]
unless @jan_next_year_index.nil? || @feb_index.nil? || @mar_index.nil? || @apr_index.nil? || @may_index.nil? || @jun_index.nil? || @jul_index.nil? || @aug_index.nil? || @sep_index.nil? || @oct_index.nil? || @nov_index.nil? || @dec_index.nil?
all_values.each do |v|
if i < @feb_index
monthly_sum_jan += v.to_f
i += 1
elsif @feb_index <= i && i < @mar_index
monthly_sum_feb += v.to_f
i += 1
elsif @mar_index <= i && i < @apr_index
monthly_sum_mar += v.to_f
i += 1
elsif @apr_index <= i && i < @may_index
monthly_sum_apr += v.to_f
i += 1
elsif @may_index <= i && i < @jun_index
monthly_sum_may += v.to_f
i += 1
elsif @jun_index <= i && i < @jul_index
monthly_sum_jun += v.to_f
i += 1
elsif @jul_index <= i && i < @aug_index
monthly_sum_jul += v.to_f
i += 1
elsif @aug_index <= i && i < @sep_index
monthly_sum_aug += v.to_f
i += 1
elsif @sep_index <= i && i < @oct_index
monthly_sum_sep += v.to_f
i += 1
elsif @oct_index <= i && i < @nov_index
monthly_sum_oct += v.to_f
i += 1
elsif @nov_index <= i && i < @dec_index
monthly_sum_nov += v.to_f
i += 1
elsif @dec_index <= i && i < @jan_next_year_index
monthly_sum_dec += v.to_f
i += 1
end
if k <= size
annual_sum += v.to_f
k += 1
end
end
end
monthly_totals[[j]] = [monthly_sum_jan, monthly_sum_feb, monthly_sum_mar, monthly_sum_apr, monthly_sum_may, monthly_sum_jun, monthly_sum_jul, monthly_sum_aug, monthly_sum_sep, monthly_sum_oct, monthly_sum_nov, monthly_sum_dec]
annual_values[[j]] = annual_sum
end
@results = {}
@results['name'] = name
@results['monthly_values'] = {}
@results['annual_values'] = {}
if @jan_next_year_index.nil? || @feb_index.nil? || @mar_index.nil? || @apr_index.nil? || @may_index.nil? || @jun_index.nil? || @jul_index.nil? || @aug_index.nil? || @sep_index.nil? || @oct_index.nil? || @nov_index.nil? || @dec_index.nil?
@results['complete_simulation'] = false
puts "#{name} did not contain an annual simulation…visualizations will not render for it."
else
@results['complete_simulation'] = true
end
monthly_totals&.each do |key, value|
unless key == 'Datetime'
@results['monthly_values'][key] = value
end
end
annual_values&.each do |key, value|
unless key == 'Datetime'
@results['annual_values'][key] = value
end
end
end
unless @results.nil?
@all_results << @results
end
end
results_path = File.join(run_dir[0], '../scenarioData.js')
File.open(results_path, 'w') do |file|
file << "var scenarioData = #{JSON.pretty_generate(@all_results)};"
end
end
|