earth 0.7.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. data/certification_changelog.markdown +21 -0
  2. data/lib/earth/air.rb +0 -2
  3. data/lib/earth/air/aircraft.rb +77 -27
  4. data/lib/earth/air/aircraft/data_miner.rb +12 -19
  5. data/lib/earth/air/aircraft_instance.rb +2 -0
  6. data/lib/earth/air/aircraft_instance_seat_class.rb +1 -0
  7. data/lib/earth/air/airport/data_miner.rb +1 -1
  8. data/lib/earth/air/data_miner.rb +0 -2
  9. data/lib/earth/air/flight_distance_class.rb +2 -3
  10. data/lib/earth/air/flight_distance_class/data_miner.rb +0 -13
  11. data/lib/earth/air/flight_distance_class_seat_class.rb +3 -2
  12. data/lib/earth/air/flight_seat_class.rb +0 -6
  13. data/lib/earth/air/flight_seat_class/data_miner.rb +0 -7
  14. data/lib/earth/air/flight_segment.rb +47 -53
  15. data/lib/earth/air/flight_segment/data_miner.rb +2 -2
  16. data/lib/earth/automobile.rb +4 -3
  17. data/lib/earth/automobile/automobile_fuel.rb +56 -119
  18. data/lib/earth/automobile/automobile_fuel/data_miner.rb +17 -4
  19. data/lib/earth/automobile/automobile_make.rb +1 -16
  20. data/lib/earth/automobile/automobile_make/data_miner.rb +25 -25
  21. data/lib/earth/automobile/automobile_make_model.rb +0 -26
  22. data/lib/earth/automobile/automobile_make_model/data_miner.rb +12 -13
  23. data/lib/earth/automobile/automobile_make_model_year.rb +6 -37
  24. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +34 -18
  25. data/lib/earth/automobile/automobile_make_model_year_variant.rb +27 -49
  26. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +111 -140
  27. data/lib/earth/automobile/automobile_make_year.rb +0 -12
  28. data/lib/earth/automobile/automobile_make_year/data_miner.rb +22 -23
  29. data/lib/earth/automobile/automobile_make_year_fleet.rb +11 -0
  30. data/lib/earth/automobile/{automobile_make_fleet_year → automobile_make_year_fleet}/data_miner.rb +1 -2
  31. data/lib/earth/automobile/automobile_model.rb +5 -0
  32. data/lib/earth/automobile/automobile_model/data_miner.rb +19 -0
  33. data/lib/earth/automobile/automobile_size_class.rb +1 -0
  34. data/lib/earth/automobile/automobile_size_class/data_miner.rb +30 -4
  35. data/lib/earth/automobile/automobile_type_fuel_year.rb +1 -1
  36. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +30 -19
  37. data/lib/earth/automobile/automobile_type_fuel_year_age.rb +0 -3
  38. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +13 -15
  39. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +0 -4
  40. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +0 -8
  41. data/lib/earth/automobile/automobile_type_year.rb +2 -1
  42. data/lib/earth/automobile/automobile_type_year/data_miner.rb +12 -10
  43. data/lib/earth/automobile/automobile_year.rb +5 -0
  44. data/lib/earth/automobile/automobile_year/data_miner.rb +15 -0
  45. data/lib/earth/automobile/data_miner.rb +5 -3
  46. data/lib/earth/automobile/dependencies.txt +45 -0
  47. data/lib/earth/bus/bus_class.rb +1 -87
  48. data/lib/earth/bus/bus_class/data_miner.rb +0 -1
  49. data/lib/earth/bus/bus_fuel/data_miner.rb +12 -19
  50. data/lib/earth/bus/bus_fuel_control/data_miner.rb +8 -12
  51. data/lib/earth/conversions_ext.rb +1 -0
  52. data/lib/earth/fuel/fuel/data_miner.rb +28 -38
  53. data/lib/earth/fuel/fuel_year/data_miner.rb +36 -47
  54. data/lib/earth/hospitality/lodging_class/data_miner.rb +12 -6
  55. data/lib/earth/locality.rb +1 -0
  56. data/lib/earth/locality/country.rb +1 -1
  57. data/lib/earth/locality/country/data_miner.rb +36 -28
  58. data/lib/earth/locality/data_miner.rb +1 -0
  59. data/lib/earth/locality/egrid_subregion/data_miner.rb +19 -14
  60. data/lib/earth/locality/urbanity/data_miner.rb +1 -3
  61. data/lib/earth/locality/zip_code/data_miner.rb +1 -1
  62. data/lib/earth/rail/national_transit_database_record/data_miner.rb +10 -14
  63. data/lib/earth/rail/rail_fuel.rb +2 -6
  64. data/lib/earth/residence/air_conditioner_use/data_miner.rb +1 -3
  65. data/lib/earth/residence/clothes_machine_use/data_miner.rb +1 -3
  66. data/lib/earth/residence/dishwasher_use/data_miner.rb +1 -3
  67. data/lib/earth/residence/residence_class/data_miner.rb +1 -3
  68. data/lib/earth/residence/residential_energy_consumption_survey_response/data_miner.rb +3 -1
  69. data/lib/earth/utils.rb +15 -5
  70. data/lib/earth/version.rb +1 -1
  71. data/spec/earth_spec.rb +4 -4
  72. metadata +9 -11
  73. data/features/automobile_type_fuel_age.feature +0 -55
  74. data/lib/earth/air/aircraft_class.rb +0 -59
  75. data/lib/earth/air/aircraft_class/data_miner.rb +0 -14
  76. data/lib/earth/air/aircraft_fuel_use_equation.rb +0 -33
  77. data/lib/earth/air/aircraft_fuel_use_equation/data_miner.rb +0 -13
  78. data/lib/earth/automobile/automobile_make_fleet_year.rb +0 -46
  79. data/lib/earth/automobile/automobile_type_fuel_age.rb +0 -65
  80. data/lib/earth/automobile/automobile_type_fuel_age/data_miner.rb +0 -149
@@ -1,17 +1,18 @@
1
1
  require 'earth/automobile/automobile_fuel'
2
2
  require 'earth/automobile/automobile_make'
3
- require 'earth/automobile/automobile_make_fleet_year'
4
3
  require 'earth/automobile/automobile_make_year'
4
+ require 'earth/automobile/automobile_make_year_fleet'
5
5
  require 'earth/automobile/automobile_make_model'
6
6
  require 'earth/automobile/automobile_make_model_year'
7
+ require 'earth/automobile/automobile_make_model_year_variant'
8
+ require 'earth/automobile/automobile_model'
7
9
  require 'earth/automobile/automobile_size_class'
8
10
  require 'earth/automobile/automobile_size_class_year'
9
- require 'earth/automobile/automobile_make_model_year_variant'
10
11
  require 'earth/automobile/automobile_type_year'
11
- require 'earth/automobile/automobile_type_fuel_age'
12
12
  require 'earth/automobile/automobile_type_fuel_control'
13
13
  require 'earth/automobile/automobile_type_fuel_year'
14
14
  require 'earth/automobile/automobile_type_fuel_year_age'
15
15
  require 'earth/automobile/automobile_type_fuel_year_control'
16
+ require 'earth/automobile/automobile_year'
16
17
 
17
18
  require 'earth/fuel'
@@ -33,150 +33,87 @@ class AutomobileFuel < ActiveRecord::Base
33
33
  end
34
34
  end
35
35
 
36
- # FIXME TODO verify that base_fuel_name and blend_fuel_name are found in Fuel if present
37
- # FIXME TODO verify that distance_key is found in AutomobileTypeFuelYearAge
38
- # FIXME TODO verify that ef_key is found in AutomobileTypeFuelYear
39
-
40
- # TODO convert these to warn blocks
41
- # ["ch4_emission_factor", "n2o_emission_factor", "hfc_emission_factor"].each do |attribute|
42
- # verify "#{attribute.humanize} should be > 0" do
43
- # find_each do |fuel|
44
- # value = fuel.send(attribute)
45
- # unless value > 0
46
- # raise "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{value} (should be > 0)"
47
- # end
48
- # end
49
- # end
50
- # end
51
- #
52
- # [["co2_emission_factor_units", "kilograms_per_litre"],
53
- # ["co2_biogenic_emission_factor_units", "kilograms_per_litre"],
54
- # ["ch4_emission_factor_units", "kilograms_co2e_per_litre"],
55
- # ["n2o_emission_factor_units", "kilograms_co2e_per_litre"],
56
- # ["hfc_emission_factor_units", "kilograms_co2e_per_litre"]].each do |pair|
57
- # attribute = pair[0]
58
- # proper_units = pair[1]
59
- # verify "#{attribute.humanize} should be #{proper_units.humanize.downcase}" do
60
- # find_each do |fuel|
61
- # units = fuel.send(attribute)
62
- # unless units == proper_units
63
- # raise "Invalid #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}: #{units} (should be #{proper_units})"
64
- # end
65
- # end
66
- # end
67
- # end
68
-
69
36
  class << self
70
- def fallback_latest_type_fuel_year_ages
37
+ def fallback_type_fuel_year_ages
71
38
  AutomobileTypeFuelYearAge.where(:year => AutomobileTypeFuelYearAge.maximum('year'))
72
39
  end
73
40
 
74
- def fallback_annual_distance
75
- fallback_latest_type_fuel_year_ages.weighted_average(:annual_distance, :weighted_by => :vehicles)
76
- end
77
-
78
- def fallback_annual_distance_units
79
- fallback_latest_type_fuel_year_ages.first.annual_distance_units
80
- end
81
-
82
- def fallback_blend_portion
83
- latest_year = AutomobileTypeFuelYear.maximum('year')
84
- gas_use = AutomobileTypeFuelYear.where(:year => latest_year, :fuel_common_name => 'gasoline').sum('fuel_consumption')
85
- diesel_use = AutomobileTypeFuelYear.where(:year => latest_year, :fuel_common_name => 'diesel').sum('fuel_consumption')
86
- diesel_use.to_f / (gas_use + diesel_use)
87
- end
88
-
89
- def fallback_co2_emission_factor
90
- (Fuel.find_by_name("Motor Gasoline").co2_emission_factor.to_f * (1 - AutomobileFuel.fallback_blend_portion)) +
91
- (Fuel.find_by_name("Distillate Fuel Oil No. 2").co2_emission_factor.to_f * AutomobileFuel.fallback_blend_portion)
92
- end
93
-
94
- def fallback_co2_emission_factor_units
95
- Fuel.find_by_name("Motor Gasoline").co2_emission_factor_units
96
- end
97
-
98
- def fallback_co2_biogenic_emission_factor
99
- (Fuel.find_by_name("Motor Gasoline").co2_biogenic_emission_factor * (1 - AutomobileFuel.fallback_blend_portion)) +
100
- (Fuel.find_by_name("Distillate Fuel Oil No. 2").co2_biogenic_emission_factor * AutomobileFuel.fallback_blend_portion)
101
- end
102
-
103
- def fallback_co2_biogenic_emission_factor_units
104
- Fuel.find_by_name("Motor Gasoline").co2_biogenic_emission_factor_units
105
- end
106
-
107
- def fallback_latest_type_fuel_years
41
+ def fallback_type_fuel_years
108
42
  AutomobileTypeFuelYear.where(:year => AutomobileTypeFuelYear.maximum('year'))
109
43
  end
110
44
 
111
- def fallback_ch4_emission_factor
112
- fallback_latest_type_fuel_years.weighted_average(:ch4_emission_factor, :weighted_by => :total_travel).to_f * GreenhouseGas[:ch4].global_warming_potential
113
- end
114
-
115
- def fallback_ch4_emission_factor_units
116
- prefix = fallback_latest_type_fuel_years.first.ch4_emission_factor_units.split("_per_")[0]
117
- suffix = fallback_latest_type_fuel_years.first.ch4_emission_factor_units.split("_per_")[1]
118
- prefix + "_co2e_per_" + suffix
119
- end
120
-
121
- def fallback_n2o_emission_factor
122
- fallback_latest_type_fuel_years.weighted_average(:n2o_emission_factor, :weighted_by => :total_travel).to_f * GreenhouseGas[:n2o].global_warming_potential
123
- end
124
-
125
- def fallback_n2o_emission_factor_units
126
- prefix = fallback_latest_type_fuel_years.first.n2o_emission_factor_units.split("_per_")[0]
127
- suffix = fallback_latest_type_fuel_years.first.n2o_emission_factor_units.split("_per_")[1]
128
- prefix + "_co2e_per_" + suffix
45
+ def fallback_type_years
46
+ AutomobileTypeYear.where(:year => AutomobileTypeYear.maximum('year'))
129
47
  end
130
48
 
131
- def fallback_hfc_emission_factor
132
- fallback_latest_type_fuel_years.map do |tfy|
133
- tfy.total_travel.to_f * tfy.type_year.hfc_emission_factor
134
- end.sum / fallback_latest_type_fuel_years.sum('total_travel')
135
- end
136
-
137
- def fallback_hfc_emission_factor_units
138
- fallback_latest_type_fuel_years.first.type_year.hfc_emission_factor_units
49
+ # FIXME TODO for some reason this causes the fallbacks calculation to hang (infinite loop?) if it's defined as a fallback
50
+ def fallback_blend_portion
51
+ gas_use = fallback_type_fuel_years.where(:fuel_common_name => 'gasoline').sum('fuel_consumption').to_f
52
+ diesel_use = fallback_type_fuel_years.where(:fuel_common_name => 'diesel').sum('fuel_consumption').to_f
53
+ diesel_use / (gas_use + diesel_use)
139
54
  end
140
55
  end
141
56
 
142
57
  falls_back_on :name => 'fallback',
143
- :annual_distance => lambda { AutomobileFuel.fallback_annual_distance },
144
- :annual_distance_units => lambda { AutomobileFuel.fallback_annual_distance_units },
145
- :co2_emission_factor => lambda { AutomobileFuel.fallback_co2_emission_factor },
146
- :co2_emission_factor_units => lambda { AutomobileFuel.fallback_co2_emission_factor_units },
147
- :co2_biogenic_emission_factor => lambda { AutomobileFuel.fallback_co2_biogenic_emission_factor },
148
- :co2_biogenic_emission_factor_units => lambda { AutomobileFuel.fallback_co2_biogenic_emission_factor_units },
149
- :ch4_emission_factor => lambda { AutomobileFuel.fallback_ch4_emission_factor },
150
- :ch4_emission_factor_units => lambda { AutomobileFuel.fallback_ch4_emission_factor_units },
151
- :n2o_emission_factor => lambda { AutomobileFuel.fallback_n2o_emission_factor },
152
- :n2o_emission_factor_units => lambda { AutomobileFuel.fallback_n2o_emission_factor_units },
153
- :hfc_emission_factor => lambda { AutomobileFuel.fallback_hfc_emission_factor },
154
- :hfc_emission_factor_units => lambda { AutomobileFuel.fallback_hfc_emission_factor_units }
155
-
58
+ :annual_distance => lambda { fallback_type_fuel_year_ages.weighted_average(:annual_distance, :weighted_by => :vehicles) },
59
+ :annual_distance_units => lambda { fallback_type_fuel_year_ages.first.annual_distance_units },
60
+ :energy_content => lambda {
61
+ (Fuel.find_by_name('Motor Gasoline').energy_content * (1 - fallback_blend_portion)) +
62
+ (Fuel.find_by_name('Distillate Fuel Oil No. 2').energy_content * fallback_blend_portion)
63
+ },
64
+ :co2_emission_factor => lambda {
65
+ (Fuel.find_by_name("Motor Gasoline").co2_emission_factor * (1 - fallback_blend_portion)) +
66
+ (Fuel.find_by_name("Distillate Fuel Oil No. 2").co2_emission_factor * fallback_blend_portion)
67
+ },
68
+ :co2_emission_factor_units => lambda { Fuel.find_by_name("Motor Gasoline").co2_emission_factor_units },
69
+ :co2_biogenic_emission_factor => lambda {
70
+ (Fuel.find_by_name("Motor Gasoline").co2_biogenic_emission_factor * (1 - AutomobileFuel.fallback_blend_portion)) +
71
+ (Fuel.find_by_name("Distillate Fuel Oil No. 2").co2_biogenic_emission_factor * AutomobileFuel.fallback_blend_portion)
72
+ },
73
+ :co2_biogenic_emission_factor_units => lambda { Fuel.find_by_name("Motor Gasoline").co2_biogenic_emission_factor_units },
74
+ :ch4_emission_factor => lambda { fallback_type_fuel_years.weighted_average(:ch4_emission_factor, :weighted_by => :total_travel) * GreenhouseGas[:ch4].global_warming_potential },
75
+ :ch4_emission_factor_units => lambda {
76
+ prefix = fallback_type_fuel_years.first.ch4_emission_factor_units.split("_per_")[0]
77
+ suffix = fallback_type_fuel_years.first.ch4_emission_factor_units.split("_per_")[1]
78
+ prefix + "_co2e_per_" + suffix
79
+ },
80
+ :n2o_emission_factor => lambda {
81
+ fallback_type_fuel_years.weighted_average(:n2o_emission_factor, :weighted_by => :total_travel) * GreenhouseGas[:n2o].global_warming_potential
82
+ },
83
+ :n2o_emission_factor_units => lambda {
84
+ prefix = fallback_type_fuel_years.first.n2o_emission_factor_units.split("_per_")[0]
85
+ suffix = fallback_type_fuel_years.first.n2o_emission_factor_units.split("_per_")[1]
86
+ prefix + "_co2e_per_" + suffix
87
+ },
88
+ :hfc_emission_factor => lambda { fallback_type_years.weighted_average(:hfc_emission_factor, :weighted_by => [:type_fuel_years, :total_travel]) },
89
+ :hfc_emission_factor_units => lambda { fallback_type_years.first.hfc_emission_factor_units }
90
+
156
91
  col :name
157
92
  col :code
158
93
  col :base_fuel_name
159
94
  col :blend_fuel_name
160
- col :blend_portion, :type => :float # the portion of the blend that is the blend fuel
161
- col :distance_key # used to look up annual distance from AutomobileTypeFuelYear
162
- col :ef_key # used to look up ch4 n2o and hfc emission factors from AutomobileTypeFuelYear
163
- col :annual_distance, :type => :float
95
+ col :blend_portion, :type => :float # the portion of the blend that is the blend fuel
96
+ col :distance_key # used to look up annual distance from AutomobileTypeFuelYear
97
+ col :ef_key # used to look up ch4 n2o and hfc emission factors from AutomobileTypeFuelYear
98
+ col :annual_distance, :type => :float
164
99
  col :annual_distance_units
165
- col :co2_emission_factor, :type => :float
100
+ col :energy_content, :type => :float
101
+ col :energy_content_units
102
+ col :co2_emission_factor, :type => :float
166
103
  col :co2_emission_factor_units
167
104
  col :co2_biogenic_emission_factor, :type => :float
168
105
  col :co2_biogenic_emission_factor_units
169
- col :ch4_emission_factor, :type => :float
106
+ col :ch4_emission_factor, :type => :float
170
107
  col :ch4_emission_factor_units
171
- col :n2o_emission_factor, :type => :float
108
+ col :n2o_emission_factor, :type => :float
172
109
  col :n2o_emission_factor_units
173
- col :hfc_emission_factor, :type => :float
110
+ col :hfc_emission_factor, :type => :float
174
111
  col :hfc_emission_factor_units
175
- col :emission_factor, :type => :float # DEPRECATED but motorcycle needs this
176
- col :emission_factor_units # DEPRECATED but motorcycle needs this
177
-
112
+ col :emission_factor, :type => :float # DEPRECATED but motorcycle needs this
113
+ col :emission_factor_units # FIXME TODO DEPRECATED but motorcycle needs this
114
+
178
115
  CODES = {
179
116
  :electricity => 'El',
180
117
  :diesel => 'D'
181
118
  }
182
- end
119
+ end
@@ -36,6 +36,18 @@ AutomobileFuel.class_eval do
36
36
  end
37
37
  end
38
38
 
39
+ process "Derive energy content" do
40
+ find_each do |record|
41
+ if record.blend_fuel.present?
42
+ record.energy_content = (record.base_fuel.energy_content * (1 - record.blend_portion)) + (record.blend_fuel.energy_content * record.blend_portion)
43
+ else
44
+ record.energy_content = record.base_fuel.energy_content
45
+ end
46
+ record.energy_content_units = record.base_fuel.energy_content_units
47
+ record.save!
48
+ end
49
+ end
50
+
39
51
  process "Derive co2 emission factor and co2 biogenic emission factors" do
40
52
  find_each do |record|
41
53
  if record.blend_fuel.present?
@@ -74,13 +86,14 @@ AutomobileFuel.class_eval do
74
86
  end
75
87
  end
76
88
 
89
+ # FIXME TODO DEPRECATED motorcycle needs this
77
90
  process "Derive emission factor" do
78
- update_all "emission_factor = co2_emission_factor + ch4_emission_factor + n2o_emission_factor + hfc_emission_factor,
79
- emission_factor_units = 'kilograms_co2e_per_litre'"
91
+ update_all(%{
92
+ emission_factor = 1.0 * co2_emission_factor + ch4_emission_factor + n2o_emission_factor + hfc_emission_factor,
93
+ emission_factor_units = 'kilograms_co2e_per_litre'
94
+ })
80
95
  end
81
96
 
82
97
  # FIXME TODO verify code somehow
83
-
84
-
85
98
  end
86
99
  end
@@ -1,22 +1,7 @@
1
1
  class AutomobileMake < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- has_many :make_years, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_name'
5
- has_many :models, :class_name => 'AutomobileMakeModel', :foreign_key => 'make_name'
6
- has_many :fleet_years, :class_name => 'AutomobileMakeFleetYear', :foreign_key => 'make_name'
7
- has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_name'
8
-
9
4
  col :name
10
5
  col :fuel_efficiency, :type => :float
11
6
  col :fuel_efficiency_units
12
-
13
- # TODO convert to table_warnings
14
- # verify "Fuel efficiency should be greater than zero" do
15
- # AutomobileMake.all.each do |make|
16
- # unless make.fuel_efficiency > 0
17
- # raise "Invalid fuel efficiency for AutomobileMake #{make.name}: #{make.fuel_efficiency} (should be > 0)"
18
- # end
19
- # end
20
- # end
21
-
22
- end
7
+ end
@@ -4,50 +4,50 @@ AutomobileMake.class_eval do
4
4
  delete_all
5
5
  end
6
6
 
7
- process "Ensure AutomobileMakeModelYearVariant and AutomobileMakeFleetYear are populated" do
7
+ process "Ensure AutomobileMakeModelYearVariant and AutomobileMakeYearFleet are populated" do
8
8
  AutomobileMakeModelYearVariant.run_data_miner!
9
- AutomobileMakeFleetYear.run_data_miner!
9
+ AutomobileMakeYearFleet.run_data_miner!
10
10
  end
11
11
 
12
12
  process "Derive manufacturer names from automobile make model year variants" do
13
13
  ::Earth::Utils.insert_ignore(
14
14
  :src => AutomobileMakeModelYearVariant,
15
15
  :dest => AutomobileMake,
16
- :cols => {
17
- :make_name => :name,
18
- }
16
+ :cols => { :make_name => :name }
19
17
  )
20
18
  end
21
19
 
22
20
  # sabshere 1/31/11 add Avanti, DaimlerChrysler, IHC, Tesla, etc.
23
21
  process "Derive extra manufacturer names from CAFE data" do
24
22
  ::Earth::Utils.insert_ignore(
25
- :src => AutomobileMakeFleetYear,
23
+ :src => AutomobileMakeYearFleet,
26
24
  :dest => AutomobileMake,
27
- :cols => {
28
- :make_name => :name,
29
- }
25
+ :cols => { :make_name => :name }
30
26
  )
31
27
  end
32
28
 
33
- # FIXME TODO make this a method on AutomobileMake?
34
- process "Calculate fuel efficiency from automobile make fleet years for makes with CAFE data" do
35
- make_fleet_years = AutomobileMakeFleetYear.arel_table
36
- makes = AutomobileMake.arel_table
37
- conditional_relation = makes[:name].eq(make_fleet_years[:make_name])
38
- relation = AutomobileMakeFleetYear.weighted_average_relation(:fuel_efficiency, :weighted_by => :volume).where(conditional_relation)
39
- update_all "fuel_efficiency = (#{relation.to_sql})"
29
+ # FIXME TODO derive units here
30
+ process "Calculate fuel efficiency from CAFE data" do
31
+ makes = arel_table
32
+ year_fleets = AutomobileMakeYearFleet.arel_table
33
+ conditional_relation = makes[:name].eq(year_fleets[:make_name])
34
+ relation = AutomobileMakeYearFleet.weighted_average_relation(:fuel_efficiency, :weighted_by => :volume).where(conditional_relation)
35
+ update_all(%{
36
+ fuel_efficiency = (#{relation.to_sql}),
37
+ fuel_efficiency_units = 'kilometres_per_litre'
38
+ })
40
39
  end
41
40
 
42
- process "Calculate fuel effeciency from automobile make model year variants for makes without CAFE data" do
43
- update_all(
44
- %{fuel_efficiency = (SELECT AVG(automobile_make_model_year_variants.fuel_efficiency) FROM automobile_make_model_year_variants WHERE automobile_makes.name = automobile_make_model_year_variants.make_name)},
45
- 'fuel_efficiency IS NULL'
46
- )
47
- end
48
-
49
- process "Set units" do
50
- update_all :fuel_efficiency_units => 'kilometres_per_litre'
41
+ # FIXME TODO derive units here
42
+ process "Calculate any missing fuel effeciencies from automobile make model year variants" do
43
+ makes = arel_table
44
+ variants = AutomobileMakeModelYearVariant.arel_table
45
+ conditional_relation = variants[:make_name].eq(makes[:name])
46
+ relation = variants.project(variants[:fuel_efficiency].average).where(conditional_relation)
47
+ where(:fuel_efficiency => nil).update_all(%{
48
+ fuel_efficiency = (#{relation.to_sql}),
49
+ fuel_efficiency_units = 'kilometres_per_litre'
50
+ })
51
51
  end
52
52
  end
53
53
  end
@@ -1,9 +1,6 @@
1
1
  class AutomobileMakeModel < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
- has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_model_name'
6
-
7
4
  col :name # make + model
8
5
  col :make_name
9
6
  col :model_name # model only
@@ -11,27 +8,4 @@ class AutomobileMakeModel < ActiveRecord::Base
11
8
  col :fuel_efficiency_city_units
12
9
  col :fuel_efficiency_highway, :type => :float
13
10
  col :fuel_efficiency_highway_units
14
-
15
- # TODO convert to table_warnings
16
- # verify "Fuel efficiencies should be greater than zero" do
17
- # AutomobileMakeModel.all.each do |model|
18
- # %w{ city highway }.each do |type|
19
- # fuel_efficiency = model.send(:"fuel_efficiency_#{type}")
20
- # unless fuel_efficiency > 0
21
- # raise "Invalid fuel efficiency #{type} for AutomobileMakeModel #{model.name}: #{fuel_efficiency} (should be > 0)"
22
- # end
23
- # end
24
- # end
25
- # end
26
- #
27
- # verify "Fuel efficiency units should be kilometres per litre" do
28
- # AutomobileMakeModel.all.each do |model|
29
- # %w{ city highway }.each do |type|
30
- # units = model.send(:"fuel_efficiency_#{type}_units")
31
- # unless units == "kilometres_per_litre"
32
- # raise "Invalid fuel efficiency #{type} units for AutomobileMakeModel #{model.name}: #{units} (should be kilometres_per_litre)"
33
- # end
34
- # end
35
- # end
36
- # end
37
11
  end
@@ -13,26 +13,25 @@ AutomobileMakeModel.class_eval do
13
13
  :src => AutomobileMakeModelYearVariant,
14
14
  :dest => AutomobileMakeModel,
15
15
  :cols => {
16
- :make_model_name => :name,
16
+ [:make_name, :model_name] => :name,
17
17
  :make_name => :make_name,
18
- :name => :model_name
18
+ :model_name => :model_name
19
19
  }
20
- # :where => 'LENGTH(src.make_name) > 0 AND LENGTH(src.make_model_name) > 0'
21
20
  )
22
21
  end
23
22
 
24
- # FIXME TODO make this a method on AutomobileMakeModel?
25
- # TODO not weighted until we get weightings on auto variants
23
+ # FIXME TODO not weighted until we get weightings on auto variants
26
24
  process "Derive average fuel economy from automobile make model year variants" do
27
- models = AutomobileMakeModel.arel_table
25
+ models = arel_table
28
26
  variants = AutomobileMakeModelYearVariant.arel_table
29
- conditional_relation = models[:name].eq(variants[:make_model_name])
30
- %w{ city highway }.each do |i|
31
- null_check = variants[:"fuel_efficiency_#{i}"].not_eq(nil)
32
- # sabshere 12/6/10 careful, don't use AutomobileMakeModelYearVariant.where here or you will be forced into projecting *
33
- relation = variants.project(variants[:"fuel_efficiency_#{i}"].average).where(conditional_relation).where(null_check)
34
- update_all "fuel_efficiency_#{i} = (#{relation.to_sql})"
35
- update_all "fuel_efficiency_#{i}_units" => 'kilometres_per_litre'
27
+ conditional_relation = models[:make_name].eq(variants[:make_name]).and(models[:model_name].eq(variants[:model_name]))
28
+ %w{ city highway }.each do |type|
29
+ null_check = variants[:"fuel_efficiency_#{type}"].not_eq(nil)
30
+ relation = variants.project(variants[:"fuel_efficiency_#{type}"].average).where(conditional_relation).where(null_check)
31
+ update_all(%{
32
+ fuel_efficiency_#{type} = (#{relation.to_sql}),
33
+ fuel_efficiency_#{type}_units = 'kilometres_per_litre'
34
+ })
36
35
  end
37
36
  end
38
37
  end
@@ -1,48 +1,17 @@
1
1
  class AutomobileMakeModelYear < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
5
- has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_model_year_name'
4
+ # Need this so Automobile and AutomobileTrip can do characteristics[:make_model_year].automobile_fuel
5
+ belongs_to :automobile_fuel, :foreign_key => 'fuel_code', :primary_key => 'code'
6
6
 
7
7
  col :name # make + model + year
8
8
  col :make_name
9
9
  col :model_name
10
- col :make_model_name
11
- col :year, :type => :integer
12
- col :make_year_name
13
- col :fuel_efficiency_city, :type => :float
10
+ col :year, :type => :integer
11
+ col :fuel_code
12
+ col :hybridity, :type => :boolean
13
+ col :fuel_efficiency_city, :type => :float
14
14
  col :fuel_efficiency_city_units
15
15
  col :fuel_efficiency_highway, :type => :float
16
16
  col :fuel_efficiency_highway_units
17
-
18
- # TODO convert to table_warnings
19
- # verify "Year should be from 1985 to 2011" do
20
- # AutomobileMakeModelYear.all.each do |model_year|
21
- # unless model_year.year.to_i > 1984 and model_year.year.to_i < 2012
22
- # raise "Invalid year for AutomobileMakeModelYear #{model_year.name}: #{model_year.year} (should be from 1985 to 2011)"
23
- # end
24
- # end
25
- # end
26
- #
27
- # verify "Fuel efficiencies should be greater than zero" do
28
- # AutomobileMakeModelYear.all.each do |model_year|
29
- # %w{ city highway }.each do |type|
30
- # fuel_efficiency = model_year.send(:"fuel_efficiency_#{type}")
31
- # unless fuel_efficiency.to_f > 0
32
- # raise "Invalid fuel efficiency #{type} for AutomobileMakeModelYear #{model_year.name}: #{fuel_efficiency} (should be > 0)"
33
- # end
34
- # end
35
- # end
36
- # end
37
- #
38
- # verify "Fuel efficiency units should be kilometres per litre" do
39
- # AutomobileMakeModelYear.all.each do |model_year|
40
- # %w{ city highway }.each do |type|
41
- # units = model_year.send(:"fuel_efficiency_#{type}_units")
42
- # unless units == "kilometres_per_litre"
43
- # raise "Invalid fuel efficiency #{type} units for AutomobileMakeModelYear #{model_year.name}: #{units} (should be kilometres_per_litre)"
44
- # end
45
- # end
46
- # end
47
- # end
48
17
  end