Class: URBANopt::Scenario::ScenarioCSV

Inherits:
ScenarioBase show all
Defined in:
lib/urbanopt/scenario/scenario_csv.rb

Instance Attribute Summary collapse

Attributes inherited from ScenarioBase

#feature_file, #name, #root_dir, #run_dir

Instance Method Summary collapse

Methods inherited from ScenarioBase

#clear

Constructor Details

#initialize(name, root_dir, run_dir, feature_file, mapper_files_dir, csv_file, num_header_rows) ⇒ ScenarioCSV

ScenarioCSV is a ScenarioBase which assigns a Simulation Mapper to each Feature in a FeatureFile using a simple CSV format. The CSV file has three columns 1) feature_id, 2) feature_name, and 3) mapper_class_name. There is one row for each Feature.

parameters:

name - String - Human readable scenario name. root_dir - String - Root directory for the scenario, contains Gemfile describing dependencies. run_dir - String - Directory for simulation of this scenario, deleting run directory clears the scenario. feature_file - URBANopt::Core::FeatureFile - FeatureFile containing features to simulate. mapper_files_dir - String - Directory containing all mapper class files containing MapperBase definitions. csv_file - String - Path to CSV file assigning a MapperBase class to each feature in feature_file. num_header_rows - Strng - Number of header rows to skip in CSV file.



53
54
55
56
57
58
59
60
61
62
# File 'lib/urbanopt/scenario/scenario_csv.rb', line 53

def initialize(name, root_dir, run_dir, feature_file, mapper_files_dir, csv_file, num_header_rows)
  super(name, root_dir, run_dir, feature_file)
  @mapper_files_dir = mapper_files_dir
  @csv_file = csv_file
  @num_header_rows = num_header_rows

  @@logger ||= URBANopt::Scenario.logger

  load_mapper_files
end

Instance Attribute Details

#csv_fileObject (readonly)

Path to CSV file



65
66
67
# File 'lib/urbanopt/scenario/scenario_csv.rb', line 65

def csv_file
  @csv_file
end

#mapper_files_dirObject (readonly)

Directory containing all mapper class files



71
72
73
# File 'lib/urbanopt/scenario/scenario_csv.rb', line 71

def mapper_files_dir
  @mapper_files_dir
end

#num_header_rowsObject (readonly)

Number of header rows to skip in CSV file



68
69
70
# File 'lib/urbanopt/scenario/scenario_csv.rb', line 68

def num_header_rows
  @num_header_rows
end

Instance Method Details

#load_mapper_filesObject

Require all simulation mappers in mapper_files_dir



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/urbanopt/scenario/scenario_csv.rb', line 74

def load_mapper_files
  dirs = Dir.glob(File.join(@mapper_files_dir, '/*.rb'))
  # order is not guaranteed...attempt to add Baseline first, then High Efficiency
  ordered_dirs = []
  bindex = dirs.find_index { |i| i.include? 'Baseline.rb' }
  if bindex
    ordered_dirs << dirs[bindex]
    dirs.delete_at(bindex)
  end
  hindex = dirs.find_index { |i| i.include? 'HighEfficiency.rb' }
  if hindex
    ordered_dirs << dirs[hindex] if hindex
    dirs.delete_at(hindex)
  end
  # then the rest
  ordered_dirs += dirs

  ordered_dirs.each do |f|
    require(f)
  rescue LoadError => e
    @@logger.error(e.message)
    raise
  end
end

#simulation_dirsObject

Gets all the simulation directories



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
# File 'lib/urbanopt/scenario/scenario_csv.rb', line 100

def simulation_dirs
  # DLM: TODO use HeaderConverters from CSV module

  rows_skipped = 0
  result = []
  CSV.foreach(@csv_file) do |row|
    if rows_skipped < @num_header_rows
      rows_skipped += 1
      next
    end

    break if row[0].nil?

    # gets +feature_id+ , +feature_name+ and +mapper_class+ from csv_file
    feature_id = row[0].chomp
    feature_name = row[1].chomp
    mapper_class = row[2].chomp

    # gets +features+ from the feature_file.
    features = []
    feature = feature_file.get_feature_by_id(feature_id)
    features << feature

    feature_names = []
    feature_names << feature_name

    simulation_dir = SimulationDirOSW.new(self, features, feature_names, mapper_class)

    result << simulation_dir
  end

  return result
end