earth 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (239) hide show
  1. data/Rakefile +9 -21
  2. data/data/rail/ntd_companies.csv +1 -0
  3. data/data/rail/ntd_fuel_consumption.csv +1 -0
  4. data/data/rail/ntd_modes.csv +1 -0
  5. data/data/rail/ntd_service.csv +1 -0
  6. data/errata/flight_segment/bts_errata.csv +6 -0
  7. data/lib/earth.rb +1 -0
  8. data/lib/earth/air/aircraft.rb +3 -5
  9. data/lib/earth/air/airline.rb +2 -3
  10. data/lib/earth/air/airport.rb +4 -5
  11. data/lib/earth/air/bts_aircraft.rb +1 -1
  12. data/lib/earth/air/flight_distance_class.rb +0 -1
  13. data/lib/earth/air/flight_distance_class/data_miner.rb +0 -4
  14. data/lib/earth/air/flight_distance_class_seat_class.rb +0 -1
  15. data/lib/earth/air/flight_segment.rb +12 -13
  16. data/lib/earth/air/flight_segment/data_miner.rb +13 -0
  17. data/lib/earth/automobile/automobile_activity_year.rb +0 -1
  18. data/lib/earth/automobile/automobile_activity_year_type.rb +4 -4
  19. data/lib/earth/automobile/automobile_activity_year_type_fuel.rb +1 -2
  20. data/lib/earth/automobile/automobile_fuel.rb +1 -2
  21. data/lib/earth/automobile/automobile_make.rb +0 -1
  22. data/lib/earth/automobile/automobile_make/data_miner.rb +2 -0
  23. data/lib/earth/automobile/automobile_make_model.rb +1 -2
  24. data/lib/earth/automobile/automobile_make_model_year_variant.rb +0 -1
  25. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +2 -223
  26. data/lib/earth/automobile/automobile_make_model_year_variant/parser.rb +224 -0
  27. data/lib/earth/automobile/automobile_make_year_fleet.rb +0 -1
  28. data/lib/earth/automobile/automobile_model.rb +0 -1
  29. data/lib/earth/automobile/automobile_size_class.rb +0 -1
  30. data/lib/earth/automobile/automobile_type_fuel.rb +0 -1
  31. data/lib/earth/automobile/automobile_type_fuel_control.rb +0 -1
  32. data/lib/earth/automobile/automobile_type_fuel_year.rb +4 -4
  33. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +7 -15
  34. data/lib/earth/automobile/automobile_year.rb +0 -1
  35. data/lib/earth/bus/bus_class.rb +0 -1
  36. data/lib/earth/bus/bus_fuel.rb +1 -72
  37. data/lib/earth/bus/bus_fuel_control.rb +0 -1
  38. data/lib/earth/bus/bus_fuel_control/data_miner.rb +0 -2
  39. data/lib/earth/bus/bus_fuel_year_control.rb +0 -1
  40. data/lib/earth/bus/bus_fuel_year_control/data_miner.rb +0 -2
  41. data/lib/earth/computation/computation_carrier.rb +1 -11
  42. data/lib/earth/computation/computation_carrier_instance_class.rb +4 -25
  43. data/lib/earth/computation/computation_carrier_region.rb +0 -1
  44. data/lib/earth/computation/computation_carrier_region/data_miner.rb +0 -3
  45. data/lib/earth/diet/diet_class.rb +0 -1
  46. data/lib/earth/diet/food_group.rb +1 -1
  47. data/lib/earth/electricity/electric_market.rb +2 -0
  48. data/lib/earth/electricity/electric_utility.rb +11 -4
  49. data/lib/earth/electricity/electric_utility/data_miner.rb +2 -8
  50. data/lib/earth/electricity/green_button_adoption.rb +2 -1
  51. data/lib/earth/fuel/fuel.rb +20 -81
  52. data/lib/earth/fuel/fuel_year.rb +0 -44
  53. data/lib/earth/fuel/greenhouse_gas.rb +0 -40
  54. data/lib/earth/hospitality/lodging_class.rb +0 -1
  55. data/lib/earth/industry/cbecs_energy_intensity.rb +25 -29
  56. data/lib/earth/industry/cbecs_energy_intensity/data_miner.rb +1 -1
  57. data/lib/earth/industry/industry.rb +1 -2
  58. data/lib/earth/industry/mecs_energy.rb +9 -10
  59. data/lib/earth/industry/mecs_ratio.rb +2 -1
  60. data/lib/earth/industry/merchant_category.rb +1 -0
  61. data/lib/earth/industry/naics_2002.rb +2 -0
  62. data/lib/earth/industry/naics_2002_naics_2007_concordance.rb +1 -0
  63. data/lib/earth/industry/naics_2002_sic_1987_concordance.rb +1 -0
  64. data/lib/earth/industry/naics_2007.rb +1 -0
  65. data/lib/earth/industry/sic_1987.rb +1 -0
  66. data/lib/earth/locality/census_division.rb +1 -3
  67. data/lib/earth/locality/climate_division.rb +0 -1
  68. data/lib/earth/locality/country.rb +1 -2
  69. data/lib/earth/locality/country/data_miner.rb +4 -0
  70. data/lib/earth/locality/egrid_country.rb +0 -1
  71. data/lib/earth/locality/egrid_region.rb +1 -3
  72. data/lib/earth/locality/egrid_subregion.rb +1 -3
  73. data/lib/earth/locality/electricity_mix.rb +2 -4
  74. data/lib/earth/locality/petroleum_administration_for_defense_district.rb +0 -1
  75. data/lib/earth/locality/state.rb +0 -1
  76. data/lib/earth/locality/zip_code.rb +0 -1
  77. data/lib/earth/model.rb +1 -1
  78. data/lib/earth/pet/breed.rb +0 -1
  79. data/lib/earth/pet/breed_gender.rb +0 -1
  80. data/lib/earth/pet/gender.rb +0 -1
  81. data/lib/earth/pet/species.rb +1 -3
  82. data/lib/earth/rail/country_rail_class.rb +0 -1
  83. data/lib/earth/rail/country_rail_traction.rb +0 -1
  84. data/lib/earth/rail/country_rail_traction_class.rb +0 -1
  85. data/lib/earth/rail/national_transit_database_company.rb +1 -3
  86. data/lib/earth/rail/national_transit_database_company/data_miner.rb +1 -1
  87. data/lib/earth/rail/national_transit_database_mode.rb +0 -1
  88. data/lib/earth/rail/national_transit_database_mode/data_miner.rb +1 -1
  89. data/lib/earth/rail/national_transit_database_record.rb +0 -1
  90. data/lib/earth/rail/national_transit_database_record/data_miner.rb +2 -2
  91. data/lib/earth/rail/rail_class.rb +0 -1
  92. data/lib/earth/rail/rail_class/data_miner.rb +5 -4
  93. data/lib/earth/rail/rail_company.rb +0 -1
  94. data/lib/earth/rail/rail_fuel.rb +0 -1
  95. data/lib/earth/rail/rail_traction.rb +0 -1
  96. data/lib/earth/rail/rail_traction/data_miner.rb +2 -8
  97. data/lib/earth/residence/air_conditioner_use.rb +0 -1
  98. data/lib/earth/residence/clothes_machine_use.rb +0 -1
  99. data/lib/earth/residence/dishwasher_use.rb +0 -1
  100. data/lib/earth/residence/residence_appliance.rb +0 -1
  101. data/lib/earth/residence/residence_class.rb +0 -1
  102. data/lib/earth/residence/residence_fuel_price.rb +1 -2
  103. data/lib/earth/residence/urbanity.rb +0 -1
  104. data/lib/earth/shipping/carrier.rb +1 -52
  105. data/lib/earth/shipping/carrier_mode.rb +0 -34
  106. data/lib/earth/shipping/shipment_mode.rb +0 -26
  107. data/lib/earth/tasks.rb +14 -4
  108. data/lib/earth/version.rb +1 -1
  109. data/spec/earth/air/aircraft_spec.rb +9 -1
  110. data/spec/earth/air/airline_spec.rb +2 -2
  111. data/spec/earth/air/airport_spec.rb +1 -2
  112. data/spec/earth/air/bts_aircraft_spec.rb +2 -2
  113. data/spec/earth/air/flight_distance_class_seat_class_spec.rb +22 -0
  114. data/spec/earth/air/flight_distance_class_spec.rb +26 -18
  115. data/spec/earth/air/flight_seat_class_spec.rb +12 -0
  116. data/spec/earth/air/flight_segment_spec.rb +38 -33
  117. data/spec/earth/automobile/automobile_activity_year_spec.rb +20 -14
  118. data/spec/earth/automobile/automobile_activity_year_type_fuel_spec.rb +14 -10
  119. data/spec/earth/automobile/automobile_activity_year_type_spec.rb +28 -20
  120. data/spec/earth/automobile/automobile_fuel_spec.rb +106 -52
  121. data/spec/earth/automobile/automobile_make_model_spec.rb +103 -62
  122. data/spec/earth/automobile/automobile_make_model_year_spec.rb +69 -46
  123. data/spec/earth/automobile/automobile_make_model_year_variant_spec.rb +108 -109
  124. data/spec/earth/automobile/automobile_make_spec.rb +8 -8
  125. data/spec/earth/automobile/automobile_make_year_fleet_spec.rb +10 -6
  126. data/spec/earth/automobile/automobile_make_year_spec.rb +10 -9
  127. data/spec/earth/automobile/automobile_model_spec.rb +3 -0
  128. data/spec/earth/automobile/automobile_size_class_spec.rb +12 -11
  129. data/spec/earth/automobile/automobile_type_fuel_control_spec.rb +9 -9
  130. data/spec/earth/automobile/automobile_type_fuel_spec.rb +23 -18
  131. data/spec/earth/automobile/automobile_type_fuel_year_control_spec.rb +37 -29
  132. data/spec/earth/automobile/automobile_type_fuel_year_spec.rb +35 -36
  133. data/spec/earth/automobile/automobile_year_spec.rb +4 -2
  134. data/spec/earth/bus/bus_class_spec.rb +10 -0
  135. data/spec/earth/bus/bus_fuel_control_spec.rb +3 -5
  136. data/spec/earth/bus/bus_fuel_spec.rb +16 -8
  137. data/spec/earth/bus/bus_fuel_year_control_spec.rb +7 -7
  138. data/spec/earth/computation/computation_carrrier_instance_class_spec.rb +17 -0
  139. data/spec/earth/computation/computation_carrrier_region_spec.rb +10 -0
  140. data/spec/earth/computation/computation_carrrier_spec.rb +17 -0
  141. data/spec/earth/diet/diet_class_spec.rb +14 -0
  142. data/spec/earth/diet/food_group_spec.rb +26 -0
  143. data/spec/earth/electricity/electric_market_spec.rb +4 -5
  144. data/spec/earth/electricity/electric_utility_spec.rb +30 -19
  145. data/spec/earth/electricity/green_button_adoption_spec.rb +29 -7
  146. data/spec/earth/fuel/fuel_spec.rb +88 -14
  147. data/spec/earth/fuel/fuel_year_spec.rb +21 -0
  148. data/spec/earth/fuel/greenhouse_gas_spec.rb +23 -0
  149. data/spec/earth/hospitality/commercial_building_energy_consumption_survey_response_spec.rb +13 -10
  150. data/spec/earth/hospitality/lodging_class_spec.rb +1 -1
  151. data/spec/earth/industry/cbecs_energy_intensity_spec.rb +72 -72
  152. data/spec/earth/industry/industry_spec.rb +16 -9
  153. data/spec/earth/industry/mecs_energy_spec.rb +76 -72
  154. data/spec/earth/industry/mecs_ratio_spec.rb +35 -31
  155. data/spec/earth/industry/merchant_category_spec.rb +7 -5
  156. data/spec/earth/industry/naics_2002_naics_2007_concordance_spec.rb +10 -19
  157. data/spec/earth/industry/naics_2002_sic_1987_concordance_spec.rb +9 -19
  158. data/spec/earth/industry/naics_2002_spec.rb +1 -11
  159. data/spec/earth/industry/naics_2007_spec.rb +2 -11
  160. data/spec/earth/industry/sic_1987_spec.rb +3 -14
  161. data/spec/earth/locality/census_division_spec.rb +25 -0
  162. data/spec/earth/locality/census_region_spec.rb +8 -0
  163. data/spec/earth/locality/climate_division_spec.rb +13 -0
  164. data/spec/earth/locality/country_spec.rb +24 -23
  165. data/spec/earth/locality/egrid_country_spec.rb +7 -6
  166. data/spec/earth/locality/egrid_region_spec.rb +6 -6
  167. data/spec/earth/locality/egrid_subregion_spec.rb +14 -14
  168. data/spec/earth/locality/electricity_mix_spec.rb +17 -17
  169. data/spec/earth/locality/padd_spec.rb +21 -0
  170. data/spec/earth/locality/state_spec.rb +8 -11
  171. data/spec/earth/locality/zip_code_spec.rb +29 -22
  172. data/spec/earth/pet/breed_gender_spec.rb +8 -0
  173. data/spec/earth/pet/breed_spec.rb +8 -0
  174. data/spec/earth/pet/gender_spec.rb +8 -0
  175. data/spec/earth/pet/species_spec.rb +11 -5
  176. data/spec/earth/rail/country_rail_class_spec.rb +16 -0
  177. data/spec/earth/rail/country_rail_traction_class_spec.rb +14 -0
  178. data/spec/earth/rail/country_rail_traction_spec.rb +14 -0
  179. data/spec/earth/rail/national_transit_database_company_spec.rb +103 -0
  180. data/spec/earth/rail/national_transit_database_mode_spec.rb +21 -0
  181. data/spec/earth/rail/national_transit_database_record_spec.rb +31 -0
  182. data/spec/earth/rail/rail_class_spec.rb +8 -0
  183. data/spec/earth/rail/rail_company_spec.rb +20 -0
  184. data/spec/earth/rail/rail_fuel_spec.rb +19 -0
  185. data/spec/earth/rail/rail_traction_spec.rb +8 -0
  186. data/spec/earth/residence/air_conditioner_use_spec.rb +17 -0
  187. data/spec/earth/residence/clothes_machine_use_spec.rb +11 -0
  188. data/spec/earth/residence/dishwasher_use_spec.rb +11 -0
  189. data/spec/earth/residence/residence_appliance_spec.rb +19 -0
  190. data/spec/earth/residence/residence_class_spec.rb +15 -0
  191. data/spec/earth/residence/residence_fuel_price_spec.rb +12 -0
  192. data/spec/earth/residence/residence_fuel_type_spec.rb +18 -0
  193. data/spec/earth/residence/residential_energy_consumption_survey_response_spec.rb +13 -0
  194. data/spec/earth/residence/urbanity_spec.rb +8 -0
  195. data/spec/earth/shipping/carrier_mode_spec.rb +18 -0
  196. data/spec/earth/shipping/carrier_spec.rb +27 -0
  197. data/spec/earth/shipping/shipment_mode_spec.rb +16 -0
  198. data/spec/earth_spec.rb +1 -1
  199. data/spec/factories/airline.rb +0 -1
  200. data/spec/factories/airport.rb +0 -1
  201. data/spec/factories/automobile_activity_year.rb +8 -0
  202. data/spec/factories/automobile_activity_year_type.rb +8 -0
  203. data/spec/factories/automobile_activity_year_type_fuel.rb +9 -0
  204. data/spec/factories/automobile_fuel.rb +15 -0
  205. data/spec/factories/automobile_make.rb +9 -0
  206. data/spec/factories/automobile_make_model.rb +10 -0
  207. data/spec/factories/automobile_make_model_year.rb +9 -0
  208. data/spec/factories/automobile_model.rb +9 -0
  209. data/spec/factories/automobile_type_fuel.rb +7 -0
  210. data/spec/factories/automobile_type_fuel_control.rb +16 -0
  211. data/spec/factories/automobile_type_fuel_year.rb +9 -0
  212. data/spec/factories/automobile_type_fuel_year_control.rb +11 -0
  213. data/spec/factories/bus_fuel.rb +7 -0
  214. data/spec/factories/bus_fuel_year_control.rb +9 -0
  215. data/spec/factories/cbecs_energy_intensity.rb +15 -0
  216. data/spec/factories/commercial_building_energy_consumption_survey_response.rb +10 -0
  217. data/spec/factories/electric_utility.rb +8 -0
  218. data/spec/factories/flight_distance_class.rb +8 -0
  219. data/spec/factories/flight_segment.rb +0 -1
  220. data/spec/factories/food_group.rb +8 -0
  221. data/spec/factories/fuel.rb +8 -0
  222. data/spec/factories/fuel_year.rb +8 -0
  223. data/spec/factories/green_button_adoption.rb +8 -0
  224. data/spec/factories/greenhouse_gas.rb +7 -0
  225. data/spec/factories/industry.rb +9 -0
  226. data/spec/factories/mecs_energy.rb +15 -0
  227. data/spec/factories/mecs_ratio.rb +15 -0
  228. data/spec/factories/national_transit_database_company.rb +8 -0
  229. data/spec/factories/national_transit_database_mode.rb +8 -0
  230. data/spec/factories/national_transit_database_record.rb +9 -0
  231. data/spec/factories/residence_appliance.rb +7 -0
  232. data/spec/factories/zip_code.rb +9 -0
  233. data/spec/spec_helper.rb +36 -19
  234. metadata +346 -66
  235. data/lib/earth/acronyms.rb +0 -12
  236. data/lib/earth/rail/rail_company_traction.rb +0 -24
  237. data/lib/earth/rail/rail_company_traction/data_miner.rb +0 -4
  238. data/lib/earth/rail/rail_company_traction_class.rb +0 -26
  239. data/lib/earth/rail/rail_company_traction_class/data_miner.rb +0 -4
@@ -0,0 +1,224 @@
1
+ require 'earth/automobile/automobile_make_model_year_variant'
2
+
3
+ # To parse the FEG files
4
+ class AutomobileMakeModelYearVariant::Parser
5
+ attr_reader :year
6
+
7
+ TRANSMISSIONS = {
8
+ 'A' => 'Automatic',
9
+ 'L' => 'Automatic',
10
+ 'M' => 'Manual',
11
+ 'S' => 'Semi-Automatic',
12
+ 'C' => 'Continuously Variable',
13
+ 'AM' => 'Automated Manual',
14
+ 'SA' => 'Semi-Automatic',
15
+ 'CVT' => 'Continuously Variable',
16
+ 'SCV' => 'Selectable Continuously Variable',
17
+ 'OT' => 'Other'
18
+ }
19
+
20
+ ENGINE_TYPES = {
21
+ '(GUZZLER)' => nil, # "gas guzzler"
22
+ '(POLICE)' => nil, # police automobile_variant
23
+ '(MPFI)' => 'injection',
24
+ '(MPI*)' => 'injection',
25
+ '(SPFI)' => 'injection',
26
+ '(FFS)' => nil, # doesn't necessarily mean fuel injection
27
+ '(TURBO)' => 'turbo',
28
+ '(TRBO)' => 'turbo',
29
+ '(TC*)' => 'turbo',
30
+ '(FFS,TRBO)' => 'turbo',
31
+ '(S-CHARGE)' => 'supercharger',
32
+ '(SC*)' => 'supercharger',
33
+ '(DIESEL)' => nil, # diesel
34
+ '(DSL)' => nil, # diesel
35
+ '(ROTARY)' => nil, # rotary
36
+ '(VARIABLE)' => nil, # variable displacement
37
+ '(NO-CAT)' => nil, # no catalytic converter
38
+ '(OHC)' => nil, # overhead camshaft
39
+ '(OHV)' => nil, # overhead valves
40
+ '(16-VALVE)' => nil, # 16V
41
+ '(305)' => nil, # 305 cubic inch displacement
42
+ '(307)' => nil, # 307 cubic inch displacement
43
+ '(M-ENG)' => nil,
44
+ '(W-ENG)' => nil,
45
+ '(GM-BUICK)' => nil,
46
+ '(GM-CHEV)' => nil,
47
+ '(GM-OLDS)' => nil,
48
+ '(GM-PONT)' => nil
49
+ }
50
+
51
+ FUEL_CODES = {
52
+ 'BE' => 'EL', # battery electric
53
+ 'CNG' => 'C', # CNG
54
+ 'DU' => 'D', # diesel (ultra-low sulphur)
55
+ 'E' => 'E', # ethanol
56
+ 'EL' => 'EL', # electric
57
+ 'G' => 'R', # regular gasoline
58
+ 'GP' => 'P', # premium gasoline recommended
59
+ 'GPR' => 'P', # premium gasoline required
60
+ 'GM' => 'P', # midgrade gasoline recommended
61
+ 'H' => 'H', # hydrogen
62
+ 'PE' => 'EL' # plug-in electric
63
+ }
64
+
65
+ CLASS_CODES = {
66
+ '1' => 'Two seaters',
67
+ '2' => 'Minicompact cars',
68
+ '3' => 'Subcompact cars',
69
+ '4' => 'Compact cars',
70
+ '5' => 'Midsize cars',
71
+ '6' => 'Large cars',
72
+ '7' => 'Small station wagons',
73
+ '8' => 'Midsize station wagons',
74
+ '9' => 'Large station wagons',
75
+ '10' => 'Small pickup trucks 2WD',
76
+ '11' => 'Small pickup trucks 4WD',
77
+ '12' => 'Standard pickup trucks 2WD',
78
+ '13' => 'Standard pickup trucks 4WD',
79
+ '14' => 'Cargo vans',
80
+ '15' => 'Passenger vans',
81
+ '16' => nil,
82
+ '17' => 'Special purpose vehicles 2WD',
83
+ '18' => 'Special purpose vehicles 4WD',
84
+ '19' => 'Special purpose vehicles'
85
+ }
86
+
87
+ def initialize(options = {})
88
+ options = options.stringify_keys
89
+ @year = options['year']
90
+ end
91
+
92
+ def apply(row)
93
+ # Pre-2010 fuel efficiencies need to be adjusted downwards to reflect real-world driving
94
+ # We do this by applying equations to the *unadjusted* city and highway fuel efficiency
95
+ # Source for the equations is EPA Fuel Economy Trends report Appendix A
96
+ # Starting in 2008 we could use the *adjusted* values from the FEG but this would require writing a new case for 2008 and 2009
97
+ # Starting in 2010 we use the *adjusted* fuel efficiencies from the FEG
98
+ case year
99
+ when (1985..1997)
100
+ row.merge!({
101
+ 'make_name' => row['carline_mfr_name'],
102
+ 'model_name' => row['carline_name'],
103
+ 'year' => year,
104
+ 'transmission' => TRANSMISSIONS[row['model_trans'][0,1].to_s],
105
+ 'speeds' => (row['model_trans'][1,1] == 'V') ? 'variable' : row['model_trans'][1,1],
106
+ 'drive' => row['drive_system'],
107
+ 'fuel_code' => row['fuel_type'],
108
+ 'fuel_efficiency_city' => 1.0 / (0.003259 + (1.1805 / row['unadj_city_mpg'].to_f)), # adjust for real-world driving
109
+ 'fuel_efficiency_highway' => 1.0 / (0.001376 + (1.3466 / row['unadj_hwy_mpg'].to_f)), # adjust for real-world driving
110
+ 'cylinders' => row['no_cyl'],
111
+ 'displacement' => _displacement(row),
112
+ 'turbo' => _turbo(row),
113
+ 'supercharger' => [ENGINE_TYPES[row['engine_desc1'].to_s], ENGINE_TYPES[row['engine_desc2'].to_s]].flatten.include?('supercharger'),
114
+ 'injection' => (row['fuel_system'] == 'FI') ? true : false,
115
+ 'size_class' => CLASS_CODES[row['size_class']]
116
+ })
117
+ when (1998..2009)
118
+ row.merge!({
119
+ 'make_name' => row['Manufacturer'] || row['MFR'],
120
+ 'model_name' => (row['carline name'] || row['CAR LINE']).upcase,
121
+ 'year' => year,
122
+ 'transmission' => TRANSMISSIONS[(row['trans'] || row['TRANS'])[-3,1]],
123
+ 'speeds' => ((row['trans'] || row['TRANS'])[-2,1] == 'V') ? 'variable' : (row['trans'] || row['TRANS'])[-2,1],
124
+ 'drive' => ((row['drv'] || row['DRIVE SYS']) + 'WD').gsub('.0', ''),
125
+ 'fuel_code' => row['fl'] || row['FUEL TYPE'],
126
+ 'fuel_efficiency_city' => 1.0 / (0.003259 + (1.1805 / (row['ucty'] || row['UNRND CITY (EPA)']).to_f)), # adjust for real-world driving
127
+ 'fuel_efficiency_highway' => 1.0 / (0.001376 + (1.3466 / (row['uhwy'] || row['UNRND HWY (EPA)']).to_f)), # adjust for real-world driving
128
+ 'cylinders' => row['cyl'] || row['NUMB CYL'],
129
+ 'displacement' => row['displ'] || row['DISPLACEMENT'],
130
+ 'turbo' => ((row['T'] || row['TURBO']) == 'T' || (row['carline name'] || row['CAR LINE']).downcase.include?('turbo')) ? true : false,
131
+ 'supercharger' => (row['S'] || row['SPCHGR']) == 'S',
132
+ 'injection' => true,
133
+ 'size_class' => row['Class'] || row['CLASS']
134
+ })
135
+ else # 2010..present
136
+ row.merge!({
137
+ 'make_name' => row['Division'],
138
+ 'model_name' => row['Carline'].upcase,
139
+ 'year' => year,
140
+ 'transmission' => TRANSMISSIONS[row['Trans']],
141
+ 'speeds' => row['# Gears'].to_i,
142
+ 'drive' => row['Drive Sys'] + 'WD',
143
+ 'fuel_code' => FUEL_CODES[row['Fuel Usage - Conventional Fuel']],
144
+ 'fuel_efficiency_city' => row['City FE (Guide) - Conventional Fuel'],
145
+ 'fuel_efficiency_highway' => row['Hwy FE (Guide) - Conventional Fuel'],
146
+ 'alt_fuel_code' => FUEL_CODES[row[' Fuel2 Usage - Alternative Fuel']],
147
+ 'alt_fuel_efficiency_city' => row['City2 FE (Guide) - Alternative Fuel'],
148
+ 'alt_fuel_efficiency_highway' => row['Hwy2 Fuel FE (Guide) - Alternative Fuel'],
149
+ 'cylinders' => row['# Cyl'],
150
+ 'displacement' => row['Eng Displ'],
151
+ 'turbo' => row['Air Aspir Method'] == 'TC',
152
+ 'supercharger' => row['Air Aspir Method'] == 'SC',
153
+ 'injection' => row['# Cyl'].present? ? true : false,
154
+ 'size_class' => row['Carline Class Desc']
155
+ })
156
+ end
157
+ end
158
+
159
+ def _displacement(row)
160
+ optional_displacement = row['opt_disp'].gsub(/[\(\)]/, '').strip.to_s
161
+ if optional_displacement =~ /^(\d\.\d)L$/
162
+ $1.to_f
163
+ elsif optional_displacement =~ /^(\d{4})CC$/
164
+ ($1.to_f / 1000).round(1)
165
+ else
166
+ row['disp_cub_in'].to_f.cubic_inches.to(:litres).round(1)
167
+ end
168
+ end
169
+
170
+ def _turbo(row)
171
+ engine_types = [ENGINE_TYPES[row['engine_desc1'].to_s], ENGINE_TYPES[row['engine_desc2'].to_s]]
172
+ engine_types << (row['model_name'].to_s.downcase.include?('turbo') ? 'turbo' : nil)
173
+ engine_types.flatten.include?('turbo')
174
+ end
175
+
176
+ # for the 1985-1997 fuel economy guides
177
+ ::FixedWidth.define :fuel_economy_guide do |d|
178
+ d.rows do |row|
179
+ row.trap { true } # there's only one section
180
+ row.column 'active_year', 4, :type => :integer # ACTIVE YEAR
181
+ row.column 'state_code', 1, :type => :string # STATE CODE: F=49-STATE,C=CALIFORNIA
182
+ row.column 'size_class', 2, :type => :integer # CARLINE CLASS CODE
183
+ row.column 'carline_mfr_code', 3, :type => :integer # CARLINE MANUFACTURER CODE
184
+ row.column 'carline_name', 28, :type => :string # CARLINE NAME
185
+ row.column 'disp_cub_in', 4, :type => :integer # DISP CUBIC INCHES
186
+ row.column 'fuel_system', 2, :type => :string # FUEL SYSTEM: 'FI' FOR FUEL INJECTION, 2-DIGIT INTEGER VALUE FOR #OF VENTURIES IF CARBURETOR SYSTEM.
187
+ row.column 'model_trans', 6, :type => :string # TRANSMISSION TYPE
188
+ row.column 'no_cyl', 2, :type => :integer # NUMBER OF ENGINE CYLINDERS
189
+ row.column 'date_time', 12, :type => :string # DATE AND TIME RECORD ENTERED -YYMMDDHHMMSS (YEAR, MONTH, DAY, HOUR, MINUTE, SECOND)
190
+ row.column 'release_date', 6, :type => :string # RELEASE DATE - YYMMDD (YEAR, MONTH, DAY)
191
+ row.column 'vi_mfr_code', 3, :type => :integer # VI MANUFACTURER CODE
192
+ row.column 'carline_code', 5, :type => :integer # CARLINE CODE
193
+ row.column 'basic_eng_id', 5, :type => :integer # BASIC ENGINE INDEX
194
+ row.column 'carline_mfr_name', 32, :type => :string # CARLINE MANUFACTURER NAME
195
+ row.column 'suppress_code', 1, :type => :integer # SUPPRESSION CODE (NO SUPPRESSED RECORD IF FOR PUBLIC ACCESS)
196
+ row.column 'city_mpg', 3, :type => :integer # ESTIMATED (CITY) MILES PER GALLON - 90% OF UNADJUSTED VALUE
197
+ row.spacer 2
198
+ row.column 'highway_mpg', 3, :type => :integer # ESTIMATED (HWY) MILES PER GALLON - 78% OF UNADJUSTED VALUE
199
+ row.spacer 2
200
+ row.column 'combined_mpg', 3, :type => :integer # COMBINED MILES PER GALLON
201
+ row.spacer 2
202
+ row.column 'unadj_city_mpg', 3, :type => :integer # UNADJUSTED CITY MILES PER GALLON
203
+ row.spacer 2
204
+ row.column 'unadj_hwy_mpg', 3, :type => :integer # UNADJUSTED HIGHWAY MILES PER GALLON
205
+ row.spacer 2
206
+ row.column 'unadj_comb_mpg', 3, :type => :integer # UNADJUSTED COMBINED MILES PER GALLON
207
+ row.spacer 2
208
+ row.column 'ave_anl_fuel', 6, :type => :integer # "$" in col 147, Annual Fuel Cost starting col 148 in I5
209
+ row.column 'opt_disp', 8, :type => :string # OPTIONAL DISPLACEMENT
210
+ row.column 'engine_desc1', 10, :type => :string # ENGINE DESCRIPTION 1
211
+ row.column 'engine_desc2', 10, :type => :string # ENGINE DESCRIPTION 2
212
+ row.column 'engine_desc3', 10, :type => :string # ENGINE DESCRIPTION 3
213
+ row.column 'body_type_2d', 10, :type => :string # BODY TYPE 2 DOOR - IF THE BODY TYPE APPLIES IT WILL TAKE THE FORM '2DR-PPP/LL' WHERE PPP=PASSENGER INTERIOR VOLUME AND LL=LUGGAGE INTERIOR VOLUME.
214
+ row.column 'body_type_4d', 10, :type => :string # BODY TYPE 4 DOOR - IF THE BODY TYPE APPLIES IT WILL TAKE THE FORM '4DR-PPP/LL' WHERE PPP=PASSENGER INTERIOR VOLUME AND LL=LUGGAGE INTERIOR VOLUME.
215
+ row.column 'body_type_hbk', 10, :type => :string # BODY TYPE HBK - IF THE BODY TYPE APPLIES IT WILL TAKE THE FORM 'HBK-PPP/LL' WHERE PPP=PASSENGER INTERIOR VOLUME AND LL=LUGGAGE INTERIOR VOLUME.
216
+ row.column 'puerto_rico', 1, :type => :string # '*' IF FOR PUERTO RICO SALES ONLY
217
+ row.column 'overdrive', 4, :type => :string # OVERDRIVE: ' OD ' FOR OVERDRIVE, 'EOD ' FOR ELECTRICALLY OPERATED OVERDRIVE AND 'AEOD' FOR AUTOMATIC OVERDRIVE
218
+ row.column 'drive_system', 3, :type => :string # FWD=FRONT WHEEL DRIVE, RWD=REAR, 4WD=4-WHEEL
219
+ row.column 'filler', 1, :type => :string # NOT USED
220
+ row.column 'fuel_type', 1, :type => :string # R=REGULAR(UNLEADED), P=PREMIUM, D=DIESEL
221
+ row.column 'trans_desc', 15, :type => :string # TRANSMISSION DESCRIPTORS
222
+ end
223
+ end
224
+ end
@@ -20,6 +20,5 @@ EOS
20
20
 
21
21
  self.primary_key = "name"
22
22
 
23
-
24
23
  warn_unless_size 1349
25
24
  end
@@ -14,6 +14,5 @@ EOS
14
14
 
15
15
  self.primary_key = "name"
16
16
 
17
-
18
17
  warn_unless_size 2299
19
18
  end
@@ -31,7 +31,6 @@ EOS
31
31
  :conventional_fuel_efficiency_city_multiplier => 0.987,
32
32
  :conventional_fuel_efficiency_highway_multiplier => 0.996
33
33
 
34
-
35
34
  warn_unless_size 15
36
35
  warn_if_nulls_except(
37
36
  :hybrid_fuel_efficiency_city_multiplier,
@@ -32,6 +32,5 @@ EOS
32
32
  AutomobileActivityYearTypeFuel.latest.where(:type_name => type_name, :fuel_family => fuel_family).first
33
33
  end
34
34
 
35
-
36
35
  warn_unless_size 4
37
36
  end
@@ -20,7 +20,6 @@ CREATE TABLE automobile_type_fuel_controls
20
20
  EOS
21
21
 
22
22
  self.primary_key = "name"
23
-
24
23
 
25
24
  warn_unless_size 20
26
25
  end
@@ -28,10 +28,11 @@ EOS
28
28
 
29
29
  # Used by Automobile and AutomobileTrip
30
30
  def self.find_by_type_name_and_fuel_family_and_closest_year(type_name, fuel_family, year)
31
- if year > maximum(:year)
32
- where(:type_name => type_name, :fuel_family => fuel_family, :year => maximum(:year)).first
31
+ candidates = where(:type_name => type_name, :fuel_family => fuel_family)
32
+ if year > (max_year = candidates.maximum(:year))
33
+ candidates.where(:year => max_year).first
33
34
  else
34
- where(:type_name => type_name, :fuel_family => fuel_family, :year => [year, minimum(:year)].max).first
35
+ candidates.where(:year => [year, candidates.minimum(:year)].max).first
35
36
  end
36
37
  end
37
38
 
@@ -40,6 +41,5 @@ EOS
40
41
  AutomobileTypeFuelYearControl.find_all_by_type_name_and_fuel_family_and_closest_year(type_name, fuel_family, year)
41
42
  end
42
43
 
43
-
44
44
  warn_unless_size 124
45
45
  end
@@ -25,26 +25,18 @@ EOS
25
25
  # Needs to be a belongs_to so that it gets imported with taps for AutomobileTypeFuelYear ch4 and n2o ef calculation
26
26
  belongs_to :type_fuel_control, :foreign_key => :type_fuel_control_name, :class_name => 'AutomobileTypeFuelControl'
27
27
 
28
+ delegate :ch4_emission_factor, :ch4_emission_factor_units, :n2o_emission_factor, :n2o_emission_factor_units,
29
+ :to => :type_fuel_control, :allow_nil => true
30
+
28
31
  # Used by AutomobileTypeFuelYear
29
32
  def self.find_all_by_type_name_and_fuel_family_and_closest_year(type_name, fuel_family, year)
30
- if year > maximum(:year)
31
- where(:type_name => type_name, :fuel_family => fuel_family, :year => maximum(:year))
33
+ candidates = where(:type_name => type_name, :fuel_family => fuel_family)
34
+ if year > (max_year = candidates.maximum(:year))
35
+ candidates.where :year => max_year
32
36
  else
33
- where(:type_name => type_name, :fuel_family => fuel_family, :year => [year, minimum(:year)].max)
34
- end
35
- end
36
-
37
- %w{ ch4_emission_factor n2o_emission_factor }.each do |method|
38
- define_method method do
39
- type_fuel_control.send(method)
40
- end
41
-
42
- units_method = method + '_units'
43
- define_method units_method do
44
- type_fuel_control.send(units_method)
37
+ candidates.where :year => [year, candidates.minimum(:year)].max
45
38
  end
46
39
  end
47
40
 
48
-
49
41
  warn_unless_size 142
50
42
  end
@@ -50,6 +50,5 @@ EOS
50
50
  }[year]
51
51
  end
52
52
 
53
-
54
53
  warn_unless_size 28
55
54
  end
@@ -64,6 +64,5 @@ EOS
64
64
  :air_conditioning_emission_factor => 0.04779 / 1.miles.to(:kilometres),
65
65
  :air_conditioning_emission_factor_units => 'kilograms_co2e_per_kilometre'
66
66
 
67
-
68
67
  warn_unless_size 2
69
68
  end
@@ -32,79 +32,8 @@ EOS
32
32
  has_many :fuel_year_controls, :class_name => 'BusFuelYearControl', :foreign_key => 'bus_fuel_name'
33
33
 
34
34
  def latest_fuel_year_controls
35
- fuel_year_controls.where(:year => fuel_year_controls.maximum('year'))
35
+ fuel_year_controls.where :year => fuel_year_controls.maximum(:year)
36
36
  end
37
37
 
38
-
39
- # # FIXME TODO verify fuel_name appears in fuels
40
- # verify "Fuel name should never be missing" do
41
- # BusFuel.all.each do |fuel|
42
- # value = fuel.send(:fuel_name)
43
- # unless value.present?
44
- # raise "Missing fuel name for BusFuel #{fuel.name}"
45
- # end
46
- # end
47
- # end
48
- #
49
- # verify "Energy content should be greater than zero if present" do
50
- # BusFuel.all.each do |fuel|
51
- # value = fuel.send(:energy_content)
52
- # if value.present?
53
- # unless value > 0
54
- # raise "Invalid energy content for BusFuel #{fuel.name}: #{value} (should be > 0)"
55
- # end
56
- # end
57
- # end
58
- # end
59
- #
60
- # %w{ co2_emission_factor co2_biogenic_emission_factor }.each do |attribute|
61
- # verify "#{attribute.humanize} should be 0 or more" do
62
- # BusFuel.all.each do |fuel|
63
- # value = fuel.send(attribute)
64
- # unless value >= 0
65
- # raise "Invalid #{attribute.humanize.downcase} for BusFuel #{fuel.name}: #{value} (should be 0 or more)"
66
- # end
67
- # end
68
- # end
69
- # end
70
- #
71
- # %w{ ch4_emission_factor n2o_emission_factor }.each do |attribute|
72
- # verify "#{attribute.humanize} should be > 0" do
73
- # BusFuel.all.each do |fuel|
74
- # value = fuel.send(attribute)
75
- # unless value > 0
76
- # raise "Invalid #{attribute.humanize.downcase} for BusFuel #{fuel.name}: #{value} (should be > 0)"
77
- # end
78
- # end
79
- # end
80
- # end
81
- #
82
- # verify "Energy content units should be megajoules per litre if present" do
83
- # BusFuel.all.each do |fuel|
84
- # units = fuel.send(:energy_content_units)
85
- # if units.present?
86
- # unless units == "megajoules_per_litre"
87
- # raise "Invalid energy content units for BusFuel #{fuel.name}: #{units} (should be megajoules_per_litre)"
88
- # end
89
- # end
90
- # end
91
- # end
92
- #
93
- # [["co2_emission_factor_units", "kilograms_per_litre"],
94
- # ["co2_biogenic_emission_factor_units", "kilograms_per_litre"],
95
- # ["ch4_emission_factor_units", "kilograms_co2e_per_kilometre"],
96
- # ["n2o_emission_factor_units", "kilograms_co2e_per_kilometre"]].each do |pair|
97
- # attribute = pair[0]
98
- # proper_units = pair[1]
99
- # verify "#{attribute.humanize} should be #{proper_units.humanize.downcase}" do
100
- # BusFuel.all.each do |fuel|
101
- # units = fuel.send(:"#{attribute}")
102
- # unless units == proper_units
103
- # raise "Invalid #{attribute.humanize.downcase} for BusFuel #{fuel.name}: #{units} (should be #{proper_units})"
104
- # end
105
- # end
106
- # end
107
- # end
108
-
109
38
  warn_unless_size 7
110
39
  end
@@ -1,6 +1,5 @@
1
1
  require 'earth/model'
2
2
 
3
- require 'earth/fuel'
4
3
  class BusFuelControl < ActiveRecord::Base
5
4
  extend Earth::Model
6
5
 
@@ -21,7 +21,5 @@ BusFuelControl.class_eval do
21
21
  n2o_emission_factor_units = 'kilograms_per_kilometre'
22
22
  })
23
23
  end
24
-
25
- # FIXME TODO verify this
26
24
  end
27
25
  end
@@ -23,6 +23,5 @@ EOS
23
23
 
24
24
  belongs_to :fuel_control, :class_name => 'BusFuelControl', :foreign_key => 'bus_fuel_control_name'
25
25
 
26
-
27
26
  warn_unless_size 67
28
27
  end