earth 0.11.20 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. data/data/automobile/annual_emission_controls.csv +143 -0
  2. data/data/automobile/annual_fuel_consumption.csv +81 -0
  3. data/data/automobile/annual_sizes.csv +541 -0
  4. data/data/automobile/auto_fuel_data.csv +13 -0
  5. data/data/automobile/auto_fuel_efs.csv +9 -0
  6. data/data/automobile/cafe_data.csv +1449 -0
  7. data/data/automobile/emission_control_techs.csv +21 -0
  8. data/data/automobile/hfc_emissions.csv +31 -0
  9. data/data/automobile/hybridity_multipliers.csv +8 -0
  10. data/data/automobile/sizes.csv +16 -0
  11. data/earth.gemspec +3 -2
  12. data/errata/aircraft/faa_errata.csv +1 -1
  13. data/errata/airline/bts_carrier_codes_errata.csv +44 -0
  14. data/errata/airport/openflights_errata.csv +450 -1
  15. data/errata/automobile_make_model_year_variant/feg_errata.csv +363 -1
  16. data/errata/automobile_make_year_fleet/cafe_errata.csv +69 -1
  17. data/errata/bts_aircraft/bts_errata.csv +1 -1
  18. data/errata/country/wri_errata.csv +1 -1
  19. data/errata/electricity/electric_market/nrel_errata.csv +13 -1
  20. data/errata/electricity/electric_utility/eia_errata.csv +18 -1
  21. data/errata/flight_segment/bts_errata.csv +4 -1
  22. data/lib/earth.rb +1 -0
  23. data/lib/earth/acronyms.rb +12 -0
  24. data/lib/earth/air/aircraft.rb +16 -0
  25. data/lib/earth/air/airline.rb +3 -0
  26. data/lib/earth/air/airline/data_miner.rb +18 -3
  27. data/lib/earth/air/airport.rb +2 -0
  28. data/lib/earth/air/bts_aircraft.rb +2 -0
  29. data/lib/earth/air/flight_distance_class.rb +2 -0
  30. data/lib/earth/air/flight_distance_class_seat_class.rb +2 -0
  31. data/lib/earth/air/flight_seat_class.rb +2 -0
  32. data/lib/earth/air/flight_segment.rb +10 -0
  33. data/lib/earth/air/flight_segment/data_miner.rb +10 -13
  34. data/lib/earth/automobile/automobile_activity_year.rb +21 -0
  35. data/lib/earth/automobile/automobile_activity_year/data_miner.rb +27 -0
  36. data/lib/earth/automobile/automobile_activity_year_type.rb +27 -0
  37. data/lib/earth/automobile/automobile_activity_year_type/data_miner.rb +27 -0
  38. data/lib/earth/automobile/automobile_activity_year_type_fuel.rb +19 -0
  39. data/lib/earth/automobile/automobile_activity_year_type_fuel/data_miner.rb +57 -0
  40. data/lib/earth/automobile/automobile_fuel.rb +93 -86
  41. data/lib/earth/automobile/automobile_fuel/data_miner.rb +64 -70
  42. data/lib/earth/automobile/automobile_make.rb +5 -1
  43. data/lib/earth/automobile/automobile_make/data_miner.rb +10 -37
  44. data/lib/earth/automobile/automobile_make_model.rb +22 -3
  45. data/lib/earth/automobile/automobile_make_model/data_miner.rb +41 -15
  46. data/lib/earth/automobile/automobile_make_model_year.rb +18 -7
  47. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +43 -27
  48. data/lib/earth/automobile/automobile_make_model_year_variant.rb +44 -8
  49. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +182 -8
  50. data/lib/earth/automobile/automobile_make_year.rb +4 -2
  51. data/lib/earth/automobile/automobile_make_year/data_miner.rb +16 -15
  52. data/lib/earth/automobile/automobile_make_year_fleet.rb +2 -1
  53. data/lib/earth/automobile/automobile_make_year_fleet/data_miner.rb +5 -2
  54. data/lib/earth/automobile/automobile_model.rb +2 -1
  55. data/lib/earth/automobile/automobile_model/data_miner.rb +1 -2
  56. data/lib/earth/automobile/automobile_size_class.rb +7 -68
  57. data/lib/earth/automobile/automobile_size_class/data_miner.rb +16 -51
  58. data/lib/earth/automobile/automobile_type_fuel.rb +23 -0
  59. data/lib/earth/automobile/automobile_type_fuel/data_miner.rb +52 -0
  60. data/lib/earth/automobile/automobile_type_fuel_control.rb +2 -33
  61. data/lib/earth/automobile/automobile_type_fuel_control/data_miner.rb +19 -4
  62. data/lib/earth/automobile/automobile_type_fuel_year.rb +17 -54
  63. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +40 -94
  64. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +25 -23
  65. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +6 -2
  66. data/lib/earth/automobile/automobile_year.rb +38 -1
  67. data/lib/earth/automobile/automobile_year/data_miner.rb +1 -2
  68. data/lib/earth/automobile/dependencies.txt +22 -36
  69. data/lib/earth/bus/bus_class.rb +2 -0
  70. data/lib/earth/bus/bus_fuel.rb +2 -0
  71. data/lib/earth/bus/bus_fuel_control.rb +2 -0
  72. data/lib/earth/bus/bus_fuel_year_control.rb +2 -0
  73. data/lib/earth/computation/computation_carrier.rb +2 -0
  74. data/lib/earth/computation/computation_carrier_instance_class.rb +2 -0
  75. data/lib/earth/computation/computation_carrier_region.rb +2 -0
  76. data/lib/earth/conversions_ext.rb +6 -0
  77. data/lib/earth/diet/diet_class.rb +2 -0
  78. data/lib/earth/diet/food_group.rb +2 -0
  79. data/lib/earth/fuel/fuel.rb +2 -0
  80. data/lib/earth/fuel/fuel/data_miner.rb +15 -0
  81. data/lib/earth/fuel/fuel_price.rb +2 -0
  82. data/lib/earth/fuel/fuel_type.rb +2 -0
  83. data/lib/earth/fuel/fuel_year.rb +2 -0
  84. data/lib/earth/fuel/greenhouse_gas.rb +2 -0
  85. data/lib/earth/hospitality/commercial_building_energy_consumption_survey_response/data_miner.rb +7 -7
  86. data/lib/earth/hospitality/lodging_class.rb +2 -0
  87. data/lib/earth/industry/industry.rb +2 -0
  88. data/lib/earth/industry/mecs_energy/data_miner.rb +1 -1
  89. data/lib/earth/industry/mecs_ratio/data_miner.rb +1 -1
  90. data/lib/earth/locality/census_division.rb +2 -0
  91. data/lib/earth/locality/census_region.rb +2 -0
  92. data/lib/earth/locality/climate_division.rb +2 -0
  93. data/lib/earth/locality/country.rb +4 -0
  94. data/lib/earth/locality/egrid_region.rb +3 -0
  95. data/lib/earth/locality/egrid_subregion.rb +2 -0
  96. data/lib/earth/locality/petroleum_administration_for_defense_district.rb +7 -0
  97. data/lib/earth/locality/state.rb +7 -0
  98. data/lib/earth/locality/zip_code.rb +4 -0
  99. data/lib/earth/pet/breed.rb +7 -0
  100. data/lib/earth/pet/breed/data_miner.rb +1 -1
  101. data/lib/earth/pet/breed_gender.rb +2 -0
  102. data/lib/earth/pet/gender.rb +2 -0
  103. data/lib/earth/pet/species.rb +2 -0
  104. data/lib/earth/rail/country_rail_class.rb +2 -0
  105. data/lib/earth/rail/country_rail_traction.rb +2 -0
  106. data/lib/earth/rail/country_rail_traction_class.rb +2 -0
  107. data/lib/earth/rail/national_transit_database_company.rb +7 -0
  108. data/lib/earth/rail/national_transit_database_mode.rb +2 -0
  109. data/lib/earth/rail/national_transit_database_record.rb +22 -0
  110. data/lib/earth/rail/rail_class.rb +2 -0
  111. data/lib/earth/rail/rail_company.rb +16 -0
  112. data/lib/earth/rail/rail_fuel.rb +2 -0
  113. data/lib/earth/rail/rail_traction.rb +2 -0
  114. data/lib/earth/residence/air_conditioner_use.rb +2 -0
  115. data/lib/earth/residence/clothes_machine_use.rb +2 -0
  116. data/lib/earth/residence/dishwasher_use.rb +2 -0
  117. data/lib/earth/residence/residence_appliance.rb +2 -0
  118. data/lib/earth/residence/residence_class.rb +2 -0
  119. data/lib/earth/residence/residence_fuel_price.rb +2 -0
  120. data/lib/earth/residence/residence_fuel_type.rb +2 -0
  121. data/lib/earth/residence/residential_energy_consumption_survey_response.rb +2 -0
  122. data/lib/earth/residence/urbanity.rb +2 -0
  123. data/lib/earth/shipping/carrier.rb +2 -0
  124. data/lib/earth/shipping/carrier_mode.rb +2 -0
  125. data/lib/earth/shipping/shipment_mode.rb +2 -0
  126. data/lib/earth/version.rb +1 -1
  127. data/spec/earth/air/airline_spec.rb +20 -0
  128. data/spec/earth/air/flight_segment_spec.rb +9 -4
  129. data/spec/earth/automobile/automobile_activity_year_spec.rb +30 -0
  130. data/spec/earth/automobile/automobile_activity_year_type_fuel_spec.rb +25 -0
  131. data/spec/earth/automobile/automobile_activity_year_type_spec.rb +38 -0
  132. data/spec/earth/automobile/automobile_fuel_spec.rb +91 -6
  133. data/spec/earth/automobile/automobile_make_model_spec.rb +74 -0
  134. data/spec/earth/automobile/automobile_make_model_year_spec.rb +90 -0
  135. data/spec/earth/automobile/automobile_make_model_year_variant_spec.rb +139 -90
  136. data/spec/earth/automobile/automobile_make_spec.rb +27 -0
  137. data/spec/earth/automobile/automobile_make_year_fleet_spec.rb +8 -21
  138. data/spec/earth/automobile/automobile_make_year_spec.rb +28 -0
  139. data/spec/earth/automobile/automobile_model_spec.rb +15 -0
  140. data/spec/earth/automobile/automobile_size_class_spec.rb +31 -0
  141. data/spec/earth/automobile/automobile_type_fuel_control_spec.rb +25 -0
  142. data/spec/earth/automobile/automobile_type_fuel_spec.rb +45 -0
  143. data/spec/earth/automobile/automobile_type_fuel_year_control_spec.rb +51 -0
  144. data/spec/earth/automobile/automobile_type_fuel_year_spec.rb +65 -0
  145. data/spec/earth/automobile/automobile_year_spec.rb +23 -0
  146. data/spec/earth/fuel/fuel_spec.rb +1 -1
  147. data/spec/earth/hospitality/commercial_building_energy_consumption_survey_response_spec.rb +11 -3
  148. data/spec/earth_spec.rb +3 -3
  149. data/spec/spec_helper.rb +1 -0
  150. metadata +78 -25
  151. data/lib/earth/automobile/automobile_size_class_year.rb +0 -44
  152. data/lib/earth/automobile/automobile_size_class_year/data_miner.rb +0 -18
  153. data/lib/earth/automobile/automobile_type_fuel_year_age.rb +0 -68
  154. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +0 -116
  155. data/lib/earth/automobile/automobile_type_year.rb +0 -58
  156. data/lib/earth/automobile/automobile_type_year/data_miner.rb +0 -30
  157. data/spec/earth/automobile/automobile_type_fuel_year_age_spec.rb +0 -20
@@ -1,4 +1,3 @@
1
- require 'earth/fuel/data_miner'
2
1
  AutomobileModel.class_eval do
3
2
  data_miner do
4
3
  process "Start from scratch" do
@@ -9,7 +8,7 @@ AutomobileModel.class_eval do
9
8
  AutomobileMakeModelYearVariant.run_data_miner!
10
9
  end
11
10
 
12
- process "Derive model names from automobile make model year variants" do
11
+ process "Derive model names from AutomobileMakeModelYearVariant" do
13
12
  ::Earth::Utils.insert_ignore(
14
13
  :src => AutomobileMakeModelYearVariant,
15
14
  :dest => AutomobileModel,
@@ -1,4 +1,3 @@
1
- require 'earth/fuel'
2
1
  class AutomobileSizeClass < ActiveRecord::Base
3
2
  self.primary_key = "name"
4
3
 
@@ -10,8 +9,6 @@ class AutomobileSizeClass < ActiveRecord::Base
10
9
 
11
10
  col :name
12
11
  col :type_name
13
- col :annual_distance, :type => :float
14
- col :annual_distance_units
15
12
  col :fuel_efficiency_city, :type => :float
16
13
  col :fuel_efficiency_city_units
17
14
  col :fuel_efficiency_highway, :type => :float
@@ -21,69 +18,11 @@ class AutomobileSizeClass < ActiveRecord::Base
21
18
  col :conventional_fuel_efficiency_city_multiplier, :type => :float
22
19
  col :conventional_fuel_efficiency_highway_multiplier, :type => :float
23
20
 
24
- # FIXME TODO verify type_name?
25
-
26
- # verify "Annual distance should be greater than zero" do
27
- # AutomobileSizeClass.all.each do |size_class|
28
- # unless size_class.annual_distance > 0
29
- # raise "Invalid annual distance for AutomobileSizeClass #{size_class.name}: #{size_class.annual_distance} (should be > 0)"
30
- # end
31
- # end
32
- # end
33
- #
34
- # verify "Annual distance units should be kilometres" do
35
- # AutomobileSizeClass.all.each do |size_class|
36
- # unless size_class.annual_distance_units == "kilometres"
37
- # raise "Invalid annual distance units for AutomobileSizeClass #{size_class.name}: #{size_class.annual_distance_units} (should be kilometres)"
38
- # end
39
- # end
40
- # end
41
- #
42
- # verify "Fuel efficiencies should be greater than zero" do
43
- # AutomobileSizeClass.all.each do |size_class|
44
- # %w{ city highway }.each do |type|
45
- # fuel_efficiency = size_class.send(:"fuel_efficiency_#{type}")
46
- # unless fuel_efficiency > 0
47
- # raise "Invalid fuel efficiency #{type} for AutomobileSizeClass #{size_class.name}: #{fuel_efficiency} (should be > 0)"
48
- # end
49
- # end
50
- # end
51
- # end
52
- #
53
- # verify "Fuel efficiency units should be kilometres per litre" do
54
- # AutomobileSizeClass.all.each do |size_class|
55
- # %w{ city highway }.each do |type|
56
- # units = size_class.send(:"fuel_efficiency_#{type}_units")
57
- # unless units == "kilometres_per_litre"
58
- # raise "Invalid fuel efficiency #{type} units for AutomobileSizeClass #{size_class.name}: #{units} (should be kilometres_per_litre)"
59
- # end
60
- # end
61
- # end
62
- # end
63
- #
64
- # verify "Any fuel efficiency multipliers should be greater than zero" do
65
- # AutomobileSizeClass.all.each do |size_class|
66
- # %w{ hybrid conventional }.each do |hybridity|
67
- # %w{ city highway }.each do |type|
68
- # multiplier = size_class.send(:"#{hybridity}_fuel_efficiency_#{type}_multiplier")
69
- # if multiplier.present?
70
- # unless multiplier > 0
71
- # raise "Invalid #{hybridity} fuel efficiency #{type} multiplier for AutomobileSizeClass #{size_class.name}: #{multiplier} (should be > 0)"
72
- # end
73
- # end
74
- # end
75
- # end
76
- # end
77
- # end
78
- #
79
- # verify "Fallback fuel efficiency multipliers should be greater than zero" do
80
- # %w{ hybrid conventional }.each do |hybridity|
81
- # %w{ city highway }.each do |type|
82
- # multiplier = AutomobileSizeClass.fallback.send(:"#{hybridity}_fuel_efficiency_#{type}_multiplier")
83
- # unless multiplier > 0
84
- # raise "Invalid AutomobileSizeClass fallback #{hybridity} fuel efficiency #{type} multiplier: #{multiplier} (should be > 0)"
85
- # end
86
- # end
87
- # end
88
- # end
21
+ warn_unless_size 15
22
+ warn_if_nulls_except(
23
+ :hybrid_fuel_efficiency_city_multiplier,
24
+ :hybrid_fuel_efficiency_highway_multiplier,
25
+ :conventional_fuel_efficiency_city_multiplier,
26
+ :conventional_fuel_efficiency_highway_multiplier
27
+ )
89
28
  end
@@ -1,61 +1,26 @@
1
- require 'earth/fuel/data_miner'
2
1
  AutomobileSizeClass.class_eval do
3
2
  data_miner do
4
- import "a list of size classes and pre-calculated fuel efficiencies",
5
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHlRUE5IcWlrRENhN0EtUldPTy1rX1E&gid=0&output=csv' do
6
- key 'name'
7
- store 'type_name'
8
- store 'fuel_efficiency_city', :units_field_name => 'fuel_efficiency_city_units'
9
- store 'fuel_efficiency_highway', :units_field_name => 'fuel_efficiency_highway_units'
3
+ process "Start from scratch" do
4
+ delete_all
10
5
  end
11
6
 
12
- # FIXME TODO derive everything from AutomobileSizeClassYear and AutomobileTypeFuelYearAge
13
- # process "Start from scratch" do
14
- # delete_all
15
- # end
16
- #
17
- # process "Ensure AutomobileSizeClassYear is populated" do
18
- # AutomobileSizeClassYear.run_data_miner!
19
- # AutomobileTypeFuelYearAge.run_data_miner!
20
- # end
21
- #
22
- # process "Derive size classes from AutomobileSizeClassYear" do
23
- # ::Earth::Utils.insert_ignore(
24
- # :src => AutomobileSizeClassYear,
25
- # :dest => AutomobileSizeClass,
26
- # :cols => {
27
- # :size_class_name => :name,
28
- # :type_name => :type_name,
29
- # }
30
- # )
31
- # end
32
- #
33
- # process "somehow calculate fuel efficiency from size class year and type fuel year age" do
34
- #
35
- # end
36
-
37
- process "Ensure AutomobileTypeFuelYearAge is populated" do
38
- AutomobileTypeFuelYearAge.run_data_miner!
39
- end
40
-
41
- # FIXME TODO make this a method on AutomobileSizeClass?
42
- process "Calculate annual distance from AutomobileTypeFuelYearAge" do
43
- classes = arel_table
44
- ages = AutomobileTypeFuelYearAge.arel_table
45
- conditional_relation = ages[:type_name].eq(classes[:type_name])
46
- update_all(%{
47
- annual_distance = (#{AutomobileTypeFuelYearAge.weighted_average_relation(:annual_distance, :weighted_by => :vehicles).where(conditional_relation).to_sql}),
48
- annual_distance_units = 'kilometres'
49
- })
7
+ import "pre-calculated size class data derived from the 2010 EPA Fuel Economy Trends report",
8
+ :url => "file://#{Earth::DATA_DIR}/automobile/sizes.csv" do
9
+ key :name
10
+ store :type_name
11
+ store :fuel_efficiency_city
12
+ store :fuel_efficiency_city_units
13
+ store :fuel_efficiency_highway
14
+ store :fuel_efficiency_highway_units
50
15
  end
51
16
 
52
17
  import "pre-calculated fuel efficiency multipliers",
53
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGt2NnhXLXUxNFRJSzczU3BkSHB3enc&hl=en&gid=0&output=csv' do
54
- key 'name'
55
- store 'hybrid_fuel_efficiency_city_multiplier'
56
- store 'hybrid_fuel_efficiency_highway_multiplier'
57
- store 'conventional_fuel_efficiency_city_multiplier'
58
- store 'conventional_fuel_efficiency_highway_multiplier'
18
+ :url => "file://#{Earth::DATA_DIR}/automobile/hybridity_multipliers.csv" do
19
+ key :name
20
+ store :hybrid_fuel_efficiency_city_multiplier
21
+ store :hybrid_fuel_efficiency_highway_multiplier
22
+ store :conventional_fuel_efficiency_city_multiplier
23
+ store :conventional_fuel_efficiency_highway_multiplier
59
24
  end
60
25
  end
61
26
  end
@@ -0,0 +1,23 @@
1
+ class AutomobileTypeFuel < ActiveRecord::Base
2
+ self.primary_key = "name"
3
+
4
+ # for calculating vehicles
5
+ def latest_activity_year_type_fuel
6
+ AutomobileActivityYearTypeFuel.latest.where(:type_name => type_name, :fuel_common_name => fuel_common_name).first
7
+ end
8
+
9
+ col :name
10
+ col :type_name
11
+ col :fuel_common_name
12
+ col :annual_distance, :type => :float
13
+ col :annual_distance_units
14
+ col :fuel_consumption, :type => :float
15
+ col :fuel_consumption_units
16
+ col :ch4_emission_factor, :type => :float
17
+ col :ch4_emission_factor_units
18
+ col :n2o_emission_factor, :type => :float
19
+ col :n2o_emission_factor_units
20
+ col :vehicles, :type => :float
21
+
22
+ warn_unless_size 4
23
+ end
@@ -0,0 +1,52 @@
1
+ AutomobileTypeFuel.class_eval do
2
+ data_miner do
3
+ process "Start from scratch" do
4
+ delete_all
5
+ end
6
+
7
+ process "Ensure AutomobileTypeFuelYear is populated" do
8
+ AutomobileTypeFuelYear.run_data_miner!
9
+ end
10
+
11
+ process "Derive from AutomobileTypeFuelYear" do
12
+ ::Earth::Utils.insert_ignore(
13
+ :src => AutomobileTypeFuelYear,
14
+ :dest => AutomobileTypeFuel,
15
+ :cols => {
16
+ [:type_name, :fuel_common_name] => :name,
17
+ :type_name => :type_name,
18
+ :fuel_common_name => :fuel_common_name
19
+ }
20
+ )
21
+ end
22
+
23
+ process "Derive annual distance and emission factors from AutomobileTypeFuelYear" do
24
+ type_fuels = arel_table
25
+ type_fuel_years = AutomobileTypeFuelYear.arel_table
26
+ join_relation = type_fuel_years[:type_name].eq(type_fuels[:type_name]).and(type_fuel_years[:fuel_common_name].eq(type_fuels[:fuel_common_name]))
27
+
28
+ %w{ annual_distance ch4_emission_factor n2o_emission_factor }.each do |item|
29
+ item_sql = AutomobileTypeFuelYear.where(join_relation).weighted_average_relation(:"#{item}", :weighted_by => :share_of_type).to_sql
30
+ item_units = AutomobileTypeFuelYear.first.send("#{item}_units")
31
+
32
+ update_all %{
33
+ #{item} = (#{item_sql}),
34
+ #{item}_units = '#{item_units}'
35
+ }
36
+ end
37
+ end
38
+
39
+ process "Ensure AutomobileActivityYearTypeFuel is populated" do
40
+ AutomobileActivityYearTypeFuel.run_data_miner!
41
+ end
42
+
43
+ process "Derive number of vehicles and fuel consumption from AutomobileActivityYearTypeFuel" do
44
+ find_each do |atf|
45
+ atf.vehicles = atf.latest_activity_year_type_fuel.distance / atf.annual_distance
46
+ atf.fuel_consumption = atf.latest_activity_year_type_fuel.fuel_consumption
47
+ atf.fuel_consumption_units = atf.latest_activity_year_type_fuel.fuel_consumption_units
48
+ atf.save!
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,6 +1,6 @@
1
- require 'earth/fuel'
2
1
  class AutomobileTypeFuelControl < ActiveRecord::Base
3
2
  self.primary_key = "name"
3
+
4
4
  col :name
5
5
  col :type_name
6
6
  col :fuel_common_name
@@ -10,36 +10,5 @@ class AutomobileTypeFuelControl < ActiveRecord::Base
10
10
  col :n2o_emission_factor, :type => :float
11
11
  col :n2o_emission_factor_units
12
12
 
13
- # verify "Type name, fuel common name, and control name should never be missing" do
14
- # AutomobileTypeFuelControl.all.each do |record|
15
- # %w{ type_name fuel_common_name control_name }.each do |attribute|
16
- # value = record.send(:"#{attribute}")
17
- # unless value.present?
18
- # raise "Missing #{attribute} for AutomobileTypeFuelControl '#{record.name}'"
19
- # end
20
- # end
21
- # end
22
- # end
23
- #
24
- # verify "Emission factors should be greater than zero" do
25
- # AutomobileTypeFuelControl.all.each do |record|
26
- # %w{ ch4_emission_factor n2o_emission_factor }.each do |factor|
27
- # value = record.send(:"#{factor}")
28
- # unless value > 0
29
- # raise "Invalid #{factor} for AutomobileTypeFuelControl '#{record.name}': #{valuel} (should be > 0)"
30
- # end
31
- # end
32
- # end
33
- # end
34
- #
35
- # verify "Emission factor units should be kilograms per kilometre" do
36
- # AutomobileTypeFuelControl.all.each do |record|
37
- # %w{ ch4_emission_factor_units n2o_emission_factor_units }.each do |attribute|
38
- # units = record.send(:"#{attribute}")
39
- # unless units == "kilograms_per_kilometre"
40
- # raise "Invalid #{attribute} for AutomobileTypeFuelControl '#{record.name}': #{units} (should be kilograms_per_kilometre)"
41
- # end
42
- # end
43
- # end
44
- # end
13
+ warn_unless_size 20
45
14
  end
@@ -1,14 +1,29 @@
1
- require 'earth/fuel/data_miner'
1
+ require 'earth/fuel/greenhouse_gas'
2
+ require 'earth/fuel/greenhouse_gas/data_miner'
3
+
2
4
  AutomobileTypeFuelControl.class_eval do
3
5
  data_miner do
4
6
  import "automobile type fuel control data derived from the 2010 EPA GHG Inventory",
5
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdEloSTU5YUNOUXRFRUcxWHlTUi1GMkE&hl=en&gid=0&output=csv' do
7
+ :url => "file://#{Earth::DATA_DIR}/automobile/emission_control_techs.csv" do
6
8
  key 'name'
7
9
  store 'type_name'
8
10
  store 'fuel_common_name'
9
11
  store 'control_name'
10
- store 'ch4_emission_factor', :units_field_name => 'ch4_emission_factor_units'
11
- store 'n2o_emission_factor', :units_field_name => 'n2o_emission_factor_units'
12
+ store 'ch4_emission_factor', :from_units => :grams_per_mile, :to_units => :kilograms_per_kilometre
13
+ store 'n2o_emission_factor', :from_units => :grams_per_mile, :to_units => :kilograms_per_kilometre
14
+ end
15
+
16
+ process "Ensure GreenhouseGas is populated" do
17
+ GreenhouseGas.run_data_miner!
18
+ end
19
+
20
+ process "Convert emission factors to co2e" do
21
+ %w{ ch4 n2o }.each do |gas|
22
+ where("#{gas}_emission_factor_units = 'kilograms_per_kilometre'").update_all %{
23
+ #{gas}_emission_factor = #{gas}_emission_factor * #{GreenhouseGas[gas].global_warming_potential},
24
+ #{gas}_emission_factor_units = 'kilograms_co2e_per_kilometre'
25
+ }
26
+ end
12
27
  end
13
28
  end
14
29
  end
@@ -1,68 +1,31 @@
1
- require 'earth/fuel'
2
1
  class AutomobileTypeFuelYear < ActiveRecord::Base
3
2
  self.primary_key = "name"
4
3
 
5
- # FIXME TODO keep this until fix AutomobileFuel import so that it doesn't call type_fuel_year.type_year
6
- belongs_to :type_year, :class_name => 'AutomobileTypeYear', :foreign_key => 'type_year_name'
4
+ # Used by Automobile and AutomobileTrip
5
+ def self.find_by_type_name_and_fuel_common_name_and_closest_year(type_name, fuel_common_name, year)
6
+ if year > maximum(:year)
7
+ where(:type_name => type_name, :fuel_common_name => fuel_common_name, :year => maximum(:year)).first
8
+ else
9
+ where(:type_name => type_name, :fuel_common_name => fuel_common_name, :year => [year, minimum(:year)].max).first
10
+ end
11
+ end
12
+
13
+ # for calculating ch4 and n2o ef
14
+ def type_fuel_year_controls
15
+ AutomobileTypeFuelYearControl.find_all_by_type_name_and_fuel_common_name_and_closest_year(type_name, fuel_common_name, year)
16
+ end
7
17
 
8
18
  col :name
9
19
  col :type_name
10
20
  col :fuel_common_name
11
21
  col :year, :type => :integer
12
- col :type_year_name
13
- col :total_travel, :type => :float
14
- col :total_travel_units
15
- col :fuel_consumption, :type => :float
16
- col :fuel_consumption_units
22
+ col :share_of_type, :type => :float
23
+ col :annual_distance, :type => :float
24
+ col :annual_distance_units
17
25
  col :ch4_emission_factor, :type => :float
18
26
  col :ch4_emission_factor_units
19
27
  col :n2o_emission_factor, :type => :float
20
28
  col :n2o_emission_factor_units
21
29
 
22
- # %w{ type_name fuel_common_name type_year_name }.each do |attribute|
23
- # verify "#{attribute.humanize} should never be missing" do
24
- # AutomobileTypeFuelYear.all.each do |record|
25
- # value = record.send(:"#{attribute}")
26
- # unless value.present?
27
- # raise "Missing #{attribute.humanize.downcase} for AutomobileTypeFuelYear '#{record.name}'"
28
- # end
29
- # end
30
- # end
31
- # end
32
- #
33
- # verify "Year should be from 1990 to 2008" do
34
- # AutomobileTypeFuelYear.all.each do |record|
35
- # year = record.send(:year)
36
- # unless year > 1989 and year < 2009
37
- # raise "Invalid year for AutomobileTypeFuelYear '#{record.name}': #{year} (should be from 1990 to 2008)"
38
- # end
39
- # end
40
- # end
41
- #
42
- # %w{ total_travel fuel_consumption ch4_emission_factor n2o_emission_factor }.each do |attribute|
43
- # verify "#{attribute.humanize} should be greater than zero" do
44
- # AutomobileTypeFuelYear.all.each do |record|
45
- # value = record.send(:"#{attribute}")
46
- # unless value > 0
47
- # raise "Invalid #{attribute.humanize.downcase} for AutomobileTypeFuelYear '#{record.name}': #{value} (should be > 0)"
48
- # end
49
- # end
50
- # end
51
- # end
52
- #
53
- # [["total_travel_units", "kilometres"],
54
- # ["fuel_consumption_units", "litres"],
55
- # ["ch4_emission_factor_units", "kilograms_per_litre"],
56
- # ["n2o_emission_factor_units", "kilograms_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
- # AutomobileTypeFuelYear.all.each do |record|
61
- # units = record.send(:"#{attribute}")
62
- # unless units == proper_units
63
- # raise "Invalid #{attribute.humanize.downcase} for AutomobileTypeFuelYear '#{record.name}': #{units} (should be #{proper_units})"
64
- # end
65
- # end
66
- # end
67
- # end
30
+ warn_unless_size 124
68
31
  end
@@ -1,104 +1,50 @@
1
- require 'earth/fuel/data_miner'
2
1
  AutomobileTypeFuelYear.class_eval do
3
2
  data_miner do
4
- import "total vehicle miles travelled by gasoline passenger cars from the 2010 EPA GHG Inventory",
5
- :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
6
- :filename => 'Annex Tables/Annex 3/Table A-87.csv',
7
- :skip => 1,
8
- :select => proc { |row| row['Year'].to_i.to_s == row['Year'] } do
9
- key 'name', :synthesize => proc { |row| "Passenger cars gasoline #{row['Year']}" }
10
- store 'type_name', :static => 'Passenger cars'
11
- store 'fuel_common_name', :static => 'gasoline'
12
- store 'year', :field_name => 'Year'
13
- store 'total_travel', :field_name => 'Passenger Cars', :units => :billion_miles
14
- end
15
-
16
- import "total vehicle miles travelled by gasoline light-duty trucks from the 2010 EPA GHG Inventory",
17
- :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
18
- :filename => 'Annex Tables/Annex 3/Table A-87.csv',
19
- :skip => 1,
20
- :select => proc { |row| row['Year'].to_i.to_s == row['Year'] } do
21
- key 'name', :synthesize => proc { |row| "Light-duty trucks gasoline #{row['Year']}" }
22
- store 'type_name', :static => 'Light-duty trucks'
23
- store 'fuel_common_name', :static => 'gasoline'
24
- store 'year', :field_name => 'Year'
25
- store 'total_travel', :field_name => 'Light-Duty Trucks', :units => :billion_miles
26
- end
27
-
28
- import "total vehicle miles travelled by diesel passenger cars from the 2010 EPA GHG Inventory",
29
- :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
30
- :filename => 'Annex Tables/Annex 3/Table A-88.csv',
31
- :skip => 1,
32
- :select => proc { |row| row['Year'].to_i.to_s == row['Year'] } do
33
- key 'name', :synthesize => proc { |row| "Passenger cars diesel #{row['Year']}" }
34
- store 'type_name', :static => 'Passenger cars'
35
- store 'fuel_common_name', :static => 'diesel'
36
- store 'year', :field_name => 'Year'
37
- store 'total_travel', :field_name => 'Passenger Cars', :units => :billion_miles
38
- end
39
-
40
- import "total vehicle miles travelled by diesel light-duty trucks from the 2010 EPA GHG Inventory",
41
- :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
42
- :filename => 'Annex Tables/Annex 3/Table A-88.csv',
43
- :skip => 1,
44
- :select => proc { |row| row['Year'].to_i.to_s == row['Year'] } do
45
- key 'name', :synthesize => proc { |row| "Light-duty trucks diesel #{row['Year']}" }
46
- store 'type_name', :static => 'Light-duty trucks'
47
- store 'fuel_common_name', :static => 'diesel'
48
- store 'year', :field_name => 'Year'
49
- store 'total_travel', :field_name => 'Light-Duty Trucks', :units => :billion_miles
50
- end
51
-
52
- process "Convert total travel from billion miles to kilometres" do
53
- conversion_factor = 1_000_000_000.miles.to(:kilometres)
54
- where(:total_travel_units => 'billion_miles').update_all(%{
55
- total_travel = 1.0 * total_travel * #{conversion_factor},
56
- total_travel_units = 'kilometres'
57
- })
58
- end
59
-
60
- import "fuel consumption derived from the 2010 EPA GHG Inventory",
61
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHBCMFhLRTFTZENsd0dPUGUyYlJna0E&hl=en&gid=0&output=csv' do
62
- key 'name'
63
- store 'fuel_consumption', :units_field_name => 'fuel_consumption_units'
64
- end
65
-
66
- process "Derive type year name for association with AutomobileTypeYear" do
67
- update_all "type_year_name = type_name || ' ' || year"
3
+ process "Start from scratch" do
4
+ delete_all
5
+ end
6
+
7
+ [['gasoline', 'G'], ['diesel', 'D']].each do |fuel, fuel_code|
8
+ [['Passenger cars', 'V'], ['Light-duty trucks', 'T']].each do |type, type_code|
9
+ code = "LD" + fuel_code + type_code
10
+
11
+ import "age distribution of #{fuel} #{type.downcase} from the 2011 EPA GHG Inventory",
12
+ :url => 'http://www.epa.gov/climatechange/emissions/downloads11/Annex%20Tables.zip',
13
+ :filename => 'Annex Tables/Table A-93.csv',
14
+ :skip => 1,
15
+ :headers => %w{ age LDGV LDGT HDGV LDDV LDDT HDDT MC },
16
+ :select => proc { |row| row['age'].to_i.to_s == row['age'] } do
17
+ key 'name', :synthesize => proc { |row| "#{type} #{fuel} #{2009 - row['age'].to_i}" }
18
+ store 'type_name', :static => type
19
+ store 'fuel_common_name', :static => fuel
20
+ store 'year', :synthesize => proc { |row| 2009 - row['age'].to_i }
21
+ store 'share_of_type', :synthesize => proc { |row| row[code].to_f / (code == 'LDDV' ? 57.5 : 100) } # total percent only sums to 57.5 for diesel cars
22
+ end
23
+
24
+ import "average distance per vehicle for gasoline passenger cars from the 2011 EPA GHG Inventory",
25
+ :url => 'http://www.epa.gov/climatechange/emissions/downloads11/Annex%20Tables.zip',
26
+ :filename => 'Annex Tables/Table A-94.csv',
27
+ :skip => 1,
28
+ :headers => %w{ age LDGV LDGT HDGV LDDV LDDT HDDT MC },
29
+ :select => proc { |row| row['age'].to_i.to_s == row['age'] } do
30
+ key 'name', :synthesize => proc { |row| "#{type} #{fuel} #{2009 - row['age'].to_i}" }
31
+ store 'annual_distance', :field_name => code, :from_units => :miles, :to_units => :kilometres
32
+ end
33
+ end
68
34
  end
69
35
 
70
- process "Ensure AutomobileTypeFuelYearControl and AutomobileTypeFuelControl are populated" do
36
+ process "Ensure AutomobileTypeFuelYearControl is populated" do
71
37
  AutomobileTypeFuelYearControl.run_data_miner!
72
- AutomobileTypeFuelControl.run_data_miner!
73
38
  end
74
39
 
75
- process "Calculate CH4 and N2O emision factors from AutomobileTypeFuelYearControl and AutomobileTypeFuelControl" do
76
- # FIXME TODO tried to do this with arel but '*' method is not defined
77
- # fuel_years = arel_table
78
- # year_controls = AutomobileTypeFuelYearControl.arel_table
79
- # controls = AutomobileTypeFuelControl.arel_table
80
- # year_controls.project((year_controls[:total_travel_percent] * controls[:ch4_emission_factor]).sum)
81
- # .join(controls)
82
- # .on(year_controls[:type_name].eq(controls[:type_name])
83
- # .and(year_controls[:fuel_common_name].eq(controls[:fuel_common_name]))
84
- # .and(year_controls[:control_name].eq(controls[:control_name])))
85
- %w{ ch4 n2o }.each do |gas|
86
- emission_factor = %{
87
- ( SELECT SUM(1.0 * t1.total_travel_percent * t2.#{gas}_emission_factor)
88
- FROM #{AutomobileTypeFuelYearControl.quoted_table_name} AS t1
89
- INNER JOIN #{AutomobileTypeFuelControl.quoted_table_name} AS t2
90
- ON t1.type_name = t2.type_name
91
- AND t1.fuel_common_name = t2.fuel_common_name
92
- AND t1.control_name = t2.control_name
93
- WHERE
94
- t1.type_name = #{quoted_table_name}.type_name
95
- AND t1.fuel_common_name = #{quoted_table_name}.fuel_common_name
96
- AND t1.year = #{quoted_table_name}.year )
97
- }
98
- update_all(%{
99
- #{gas}_emission_factor = 1.0 * #{emission_factor} * total_travel / fuel_consumption,
100
- #{gas}_emission_factor_units = 'kilograms_per_litre'
101
- })
40
+ # Can't use an update all here b/c efs are methods defined on ATFYC that look up the ef from ATFC
41
+ process "Derive ch4 and n2o emission factor from AutomobileTypeFuelYearControl" do
42
+ find_each do |atfy|
43
+ atfy.ch4_emission_factor = atfy.type_fuel_year_controls.sum{ |atfyc| atfyc.total_travel_percent * atfyc.ch4_emission_factor }
44
+ atfy.n2o_emission_factor = atfy.type_fuel_year_controls.sum{ |atfyc| atfyc.total_travel_percent * atfyc.n2o_emission_factor }
45
+ atfy.ch4_emission_factor_units = atfy.type_fuel_year_controls.first.ch4_emission_factor_units
46
+ atfy.n2o_emission_factor_units = atfy.type_fuel_year_controls.first.n2o_emission_factor_units
47
+ atfy.save!
102
48
  end
103
49
  end
104
50
  end