Class: URBANopt::Scenario::ResultVisualization

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/scenario/scenario_visualization.rb

Class Method Summary collapse

Class Method Details

.create_visualization(run_dir, feature = true, feature_names = false) ⇒ Object



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
234
235
236
237
238
239
240
241
242
243
# File 'lib/urbanopt/scenario/scenario_visualization.rb', line 48

def self.create_visualization(run_dir, feature = true, feature_names = false)
  @all_results = []

  run_dir.each do |folder|
    # create visualization for scenarios
    if feature == false
      name = folder.split('/')[-1]
      csv_dir = File.join(folder, 'default_scenario_report.csv')
    # create visualization for features
    elsif feature == true
      index = run_dir.index(folder)
      name = folder.split('/')[-1] + '-' + feature_names[index]
      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 = {}

      headers_unitless = []
      i = 0
      CSV.foreach(csv_dir).map do |row|
        if i == 0
          # store header values from csv
          headers = row
          headers.each do |header|
            header_unitless = header.to_s.split('(')[0]
            headers_unitless << header_unitless
            monthly_values[header_unitless] = []
          end
        # store values from csv for each row
        elsif i <= size
          headers_unitless.each_index do |j|
            monthly_values[headers_unitless[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

      # create dates for each month
      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)

        # store index of each date from the csv
        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

      headers_unitless.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

        # loop through values for each header
        all_values = monthly_values[headers_unitless[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?

          # for each header store monthly sums of values
          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

          end
        end

        # sum up monthly values for annual aggregate
        annual_sum = 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

        # store headers as key and monthly sums as values for each header
        monthly_totals[headers_unitless[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[headers_unitless[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
  # create json with required data stored in a variable
  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