Class: URBANopt::RNM::RnmUsCatalogConversion

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/rnm/rnm_us_catalog_conversion.rb

Overview

creating a class to convert the extended catalog into the RNM-US catalog which is needed by the model

Instance Method Summary collapse

Constructor Details

#initialize(extended_catalog_path, run_dir, rnm_dirname) ⇒ RnmUsCatalogConversion

Returns a new instance of RnmUsCatalogConversion.



13
14
15
16
17
# File 'lib/urbanopt/rnm/rnm_us_catalog_conversion.rb', line 13

def initialize(extended_catalog_path, run_dir, rnm_dirname)
  @extended_catalog_path = extended_catalog_path
  @run_dir = run_dir
  @rnm_dirname = rnm_dirname
end

Instance Method Details

#matrix_processing(csv, matrix, v, row, section, wires, k) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/urbanopt/rnm/rnm_us_catalog_conversion.rb', line 19

def matrix_processing(csv, matrix, v, row, section, wires, k)
  if matrix.is_a?(Array)
    csv << row
    headings = []
    for j in 0..matrix.length - 1
      if section == 'LINES'
        if matrix[j]['Line geometry'].is_a? Array
          line_creation = URBANopt::RNM::CarsonEq.new(matrix[j])
          matrix[j] = line_creation.creation(wires) # passing the info about each power line
        else
          matrix[j].delete('Line geometry')
        end
      end
      ii = 0
      matrix[j].each do |keys, values|
        matrix[j].delete('connection')
        matrix[j].delete('resistance(Ohm)')
        matrix[j].delete('control_type')
        if ii == 0
          headings[ii] = "##{keys}" # to ensure it works with the catalog
        else
          headings[ii] = keys
        end
        row[ii] = values
        ii += 1
      end
      if j == 0
        csv << headings
      end
      csv << row

    end
  else
    if k == 'Mismatch voltages S (kV):'.to_s || k == 'Mismatches convergence S (kVA):' || k == 'Minimum allowable voltages (pu):' || k == 'Maximum allowable voltages (pu):'
      for i in 0..v.split(',').length - 1
        row.push(v.split(',')[i])
      end
    else
      row.push(v)
    end
    csv << row
  end
end

#processing_data(utm_zone) ⇒ Object



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
# File 'lib/urbanopt/rnm/rnm_us_catalog_conversion.rb', line 63

def processing_data(utm_zone)
  # parsing lines and wires info:
  row = Array.new(25)
  ext_catalog = JSON.parse(File.read(@extended_catalog_path))
  CSV.open(File.join(@run_dir, @rnm_dirname, 'udcons.csv'), 'w') do |csv|
    ext_catalog.each do |key, v|
      if key != 'WIRES'
        csv << ["<#{key}>"]
        if ext_catalog[key].is_a?(Hash) # defining the section under consideration is an Hash or an Array
          if key == 'OTHERS'
            ext_catalog[key]['UTM Zone'] = utm_zone.to_s
          end
          ext_catalog[key].each do |k, v|
            row = []
            row.push(k) # title of the array
            matrix_processing(csv, ext_catalog[key][k], v, row, key, ext_catalog['WIRES'], k)
          end
        else
          if ext_catalog[key].empty?
            csv << ['END']
          else
            for i in 0..ext_catalog[key].length - 1
              row = []
              if ext_catalog[key][i].is_a?(Hash)
                ext_catalog[key][i].each do |k, v|
                  row.push(k)  # title of the array
                  matrix_processing(csv, ext_catalog[key][i][k], v, row, key, ext_catalog['WIRES'], k)
                  if key == 'LINES' || key == 'SUBSTATIONS AND DISTRIBUTION TRANSFORMERS' || key == 'CAPACITORS'
                    csv << ['END']
                  end
                end
              end
              end
          end
        end
        csv << ["</#{key}>"]
      end
    end
  end
end