earth 0.3.15 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/Gemfile +1 -0
  2. data/earth.gemspec +4 -4
  3. data/features/automobile_fuel.feature +44 -0
  4. data/features/automobile_type_fuel_age.feature +2 -1
  5. data/features/automobile_type_fuel_year.feature +86 -16
  6. data/features/automobile_type_fuel_year_age.feature +94 -0
  7. data/features/automobile_type_fuel_year_control.feature +44 -4
  8. data/features/automobile_type_year.feature +27 -7
  9. data/features/carrier_mode.feature +20 -30
  10. data/features/fuel_year.feature +22 -12
  11. data/features/support/imports/automobile_fuel_bad.csv +3 -0
  12. data/features/support/imports/automobile_fuel_good.csv +3 -0
  13. data/features/support/imports/automobile_type_fuel_year_age_bad.csv +2 -0
  14. data/features/support/imports/automobile_type_fuel_year_age_good.csv +2 -0
  15. data/features/support/imports/automobile_type_fuel_year_bad.csv +2 -2
  16. data/features/support/imports/automobile_type_fuel_year_control_bad.csv +2 -3
  17. data/features/support/imports/automobile_type_fuel_year_control_good.csv +2 -3
  18. data/features/support/imports/automobile_type_fuel_year_good.csv +2 -2
  19. data/features/support/imports/automobile_type_year_bad.csv +2 -2
  20. data/features/support/imports/automobile_type_year_good.csv +2 -2
  21. data/features/support/imports/carrier_mode_bad.csv +2 -3
  22. data/features/support/imports/carrier_mode_good.csv +2 -3
  23. data/features/support/imports/fuel_year_bad.csv +2 -2
  24. data/features/support/imports/fuel_year_good.csv +2 -2
  25. data/lib/earth.rb +5 -5
  26. data/lib/earth/air/airline/data_miner.rb +1 -0
  27. data/lib/earth/air/airport/data_miner.rb +1 -1
  28. data/lib/earth/air/flight_segment/data_miner.rb +5 -0
  29. data/lib/earth/automobile.rb +2 -0
  30. data/lib/earth/automobile/automobile_fuel.rb +93 -0
  31. data/lib/earth/automobile/automobile_fuel/data_miner.rb +60 -0
  32. data/lib/earth/automobile/automobile_fuel_type.rb +1 -0
  33. data/lib/earth/automobile/automobile_fuel_type/data_miner.rb +2 -1
  34. data/lib/earth/automobile/automobile_make/data_miner.rb +11 -0
  35. data/lib/earth/automobile/automobile_make_fleet_year.rb +2 -2
  36. data/lib/earth/automobile/automobile_make_model.rb +1 -1
  37. data/lib/earth/automobile/automobile_make_model/data_miner.rb +1 -0
  38. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +1 -0
  39. data/lib/earth/automobile/automobile_make_model_year_variant.rb +2 -1
  40. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +21 -7
  41. data/lib/earth/automobile/automobile_make_year.rb +2 -2
  42. data/lib/earth/automobile/automobile_make_year/data_miner.rb +3 -0
  43. data/lib/earth/automobile/automobile_size_class/data_miner.rb +11 -7
  44. data/lib/earth/automobile/automobile_size_class_year/data_miner.rb +2 -1
  45. data/lib/earth/automobile/automobile_type_fuel_age.rb +1 -0
  46. data/lib/earth/automobile/automobile_type_fuel_age/data_miner.rb +52 -34
  47. data/lib/earth/automobile/automobile_type_fuel_control/data_miner.rb +1 -1
  48. data/lib/earth/automobile/automobile_type_fuel_year.rb +7 -0
  49. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +57 -63
  50. data/lib/earth/automobile/automobile_type_fuel_year_age.rb +13 -0
  51. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +184 -0
  52. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +6 -0
  53. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +24 -8
  54. data/lib/earth/automobile/automobile_type_year.rb +2 -0
  55. data/lib/earth/automobile/automobile_type_year/data_miner.rb +28 -11
  56. data/lib/earth/automobile/data_miner.rb +2 -0
  57. data/lib/earth/bus/bus_class.rb +8 -8
  58. data/lib/earth/computation/computation_platform/data_miner.rb +1 -1
  59. data/lib/earth/computation/server_type/data_miner.rb +1 -1
  60. data/lib/earth/computation/server_type_alias/data_miner.rb +1 -1
  61. data/lib/earth/conversions_ext.rb +4 -0
  62. data/lib/earth/fuel.rb +1 -0
  63. data/lib/earth/fuel/data_miner.rb +1 -0
  64. data/lib/earth/fuel/fuel.rb +77 -0
  65. data/lib/earth/fuel/fuel/data_miner.rb +98 -0
  66. data/lib/earth/fuel/fuel_type/data_miner.rb +2 -2
  67. data/lib/earth/fuel/fuel_year/data_miner.rb +95 -26
  68. data/lib/earth/fuel/greenhouse_gas.rb +6 -0
  69. data/lib/earth/fuel/greenhouse_gas/data_miner.rb +1 -1
  70. data/lib/earth/hospitality/lodging_class/data_miner.rb +21 -9
  71. data/lib/earth/locality/census_division.rb +6 -5
  72. data/lib/earth/locality/census_division/data_miner.rb +9 -9
  73. data/lib/earth/locality/country.rb +9 -0
  74. data/lib/earth/locality/country/data_miner.rb +1 -1
  75. data/lib/earth/pet/breed/data_miner.rb +0 -1
  76. data/lib/earth/pet/breed_gender/data_miner.rb +0 -1
  77. data/lib/earth/shipping/carrier.rb +3 -3
  78. data/lib/earth/shipping/carrier/data_miner.rb +2 -2
  79. data/lib/earth/shipping/carrier_mode/data_miner.rb +15 -42
  80. data/lib/earth/shipping/shipment_mode/data_miner.rb +1 -1
  81. data/lib/earth/version.rb +1 -1
  82. data/spec/earth/automobile/automobile_fuel_spec.rb +19 -0
  83. data/spec/earth_spec.rb +3 -3
  84. metadata +141 -33
@@ -1,3 +1,4 @@
1
+ # DEPRECATED - use AutomobileFuel
1
2
  class AutomobileFuelType < ActiveRecord::Base
2
3
  set_primary_key :code
3
4
 
@@ -1,3 +1,4 @@
1
+ # DEPRECATED - use AutomobileFuel
1
2
  AutomobileFuelType.class_eval do
2
3
  data_miner do
3
4
  schema Earth.database_options do
@@ -105,7 +106,7 @@ AutomobileFuelType.class_eval do
105
106
 
106
107
  verify "Emission factor units should never be missing" do
107
108
  AutomobileFuelType.all.each do |fuel_type|
108
- if fuel_type.emission_factor_units.nil?
109
+ unless fuel_type.emission_factor_units.present?
109
110
  raise "Missing emission factor units for AutomobileFuelType #{fuel_type.name}"
110
111
  end
111
112
  end
@@ -19,6 +19,17 @@ AutomobileMake.class_eval do
19
19
  }
20
20
  end
21
21
 
22
+ # sabshere 1/31/11 add Avanti, DaimlerChrysler, IHC, Tesla, etc.
23
+ process "Derive extra manufacturer names from CAFE data" do
24
+ AutomobileMakeFleetYear.run_data_miner!
25
+ connection.execute %{
26
+ INSERT IGNORE INTO automobile_makes(name)
27
+ SELECT DISTINCT automobile_make_fleet_years.make_name
28
+ FROM automobile_make_fleet_years
29
+ }
30
+ end
31
+
32
+ # FIXME TODO make this a method on AutomobileMake?
22
33
  process "Calculate fuel efficiency from automobile make fleet years for makes with CAFE data" do
23
34
  AutomobileMakeFleetYear.run_data_miner!
24
35
  make_fleet_years = AutomobileMakeFleetYear.arel_table
@@ -3,11 +3,11 @@ class AutomobileMakeFleetYear < ActiveRecord::Base
3
3
 
4
4
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
5
  belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
6
-
6
+
7
7
  data_miner do
8
8
  tap "Brighter Planet's sanitized auto make fleet year data", Earth.taps_server
9
9
 
10
- process "bring in dependencies" do
10
+ process "Pull dependencies" do
11
11
  run_data_miner_on_belongs_to_associations
12
12
  end
13
13
  end
@@ -7,7 +7,7 @@ class AutomobileMakeModel < ActiveRecord::Base
7
7
  data_miner do
8
8
  tap "Brighter Planet's auto model data", Earth.taps_server
9
9
 
10
- process "bring in dependencies" do
10
+ process "Pull dependencies" do
11
11
  run_data_miner_on_belongs_to_associations
12
12
  end
13
13
  end
@@ -21,6 +21,7 @@ AutomobileMakeModel.class_eval do
21
21
  }
22
22
  end
23
23
 
24
+ # FIXME TODO make this a method on AutomobileMakeModel?
24
25
  # TODO not weighted until we get weightings on auto variants
25
26
  process "Derive average fuel economy from automobile make model year variants" do
26
27
  models = AutomobileMakeModel.arel_table
@@ -24,6 +24,7 @@ AutomobileMakeModelYear.class_eval do
24
24
  }
25
25
  end
26
26
 
27
+ # FIXME TODO make this a method on AutomobileMakeModelYear?
27
28
  # TODO: weight by volume somehow
28
29
  # note that we used to derive averages from make years, but here we get it from variants
29
30
  # even without volume-weighting, the values are much better.
@@ -5,7 +5,8 @@ class AutomobileMakeModelYearVariant < ActiveRecord::Base
5
5
  belongs_to :make_model, :class_name => 'AutomobileMakeModel', :foreign_key => 'make_model_name'
6
6
  belongs_to :make_model_year, :class_name => 'AutomobileMakeModelYear', :foreign_key => 'make_model_year_name'
7
7
  belongs_to :fuel_type, :class_name => 'AutomobileFuelType', :foreign_key => 'fuel_type_code'
8
-
8
+ belongs_to :fuel, :class_name => 'AutomobileFuel', :foreign_key => 'fuel_code', :primary_key => 'code'
9
+
9
10
  data_miner do
10
11
  tap "Brighter Planet's sanitized automobile make model year variant data", Earth.taps_server
11
12
 
@@ -272,6 +272,7 @@ AutomobileMakeModelYearVariant.class_eval do
272
272
  float 'fuel_efficiency_highway'
273
273
  string 'fuel_efficiency_highway_units'
274
274
  string 'fuel_type_code'
275
+ string 'fuel_code'
275
276
  string 'transmission'
276
277
  string 'drive'
277
278
  boolean 'turbo'
@@ -306,7 +307,7 @@ AutomobileMakeModelYearVariant.class_eval do
306
307
  :format => :fixed_width,
307
308
  :cut => ((yy == 95) ? '13-' : nil),
308
309
  :schema_name => :fuel_economy_guide_b,
309
- :select => lambda { |row| (row['suppress_code'].blank? or row['suppress_code'].to_f == 0) and row['state_code'] == 'F' },
310
+ :select => lambda { |row| row['model'].present? and (row['suppress_code'].blank? or row['suppress_code'].to_f == 0) and row['state_code'] == 'F' },
310
311
  :filename => filename,
311
312
  :transform => { :class => AutomobileMakeModelYearVariant::ParserB, :year => "19#{yy}".to_i },
312
313
  :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' }) do
@@ -315,6 +316,7 @@ AutomobileMakeModelYearVariant.class_eval do
315
316
  store 'make_name', :field_name => 'make'
316
317
  store 'year'
317
318
  store 'fuel_type_code', :field_name => 'fuel_type'
319
+ store 'fuel_code', :field_name => 'fuel_type'
318
320
  store 'fuel_efficiency_highway', :static => nil, :units => :kilometres_per_litre
319
321
  store 'fuel_efficiency_city', :static => nil, :units => :kilometres_per_litre
320
322
  store 'raw_fuel_efficiency_highway', :field_name => 'unadj_hwy_mpg', :from_units => :miles_per_gallon, :to_units => :kilometres_per_litre
@@ -347,11 +349,13 @@ AutomobileMakeModelYearVariant.class_eval do
347
349
  }.sort { |a, b| a.first <=> b.first }.each do |year, options|
348
350
  import "#{ year } Fuel Economy Guide",
349
351
  options.merge(:transform => { :class => AutomobileMakeModelYearVariant::ParserC, :year => year },
350
- :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' }) do
352
+ :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' },
353
+ :select => lambda { |row| row['model'].present? }) do
351
354
  key 'row_hash'
352
355
  store 'name', :field_name => 'model'
353
356
  store 'make_name', :field_name => 'make'
354
357
  store 'fuel_type_code', :field_name => 'fl'
358
+ store 'fuel_code', :field_name => 'fl'
355
359
  store 'fuel_efficiency_highway', :static => nil, :units => :kilometres_per_litre
356
360
  store 'fuel_efficiency_city', :static => nil, :units => :kilometres_per_litre
357
361
  store 'raw_fuel_efficiency_highway', :field_name => 'uhwy', :from_units => :miles_per_gallon, :to_units => :kilometres_per_litre
@@ -380,11 +384,13 @@ AutomobileMakeModelYearVariant.class_eval do
380
384
  }.sort { |a, b| a.first <=> b.first }.each do |year, options|
381
385
  import "#{ year } Fuel Economy Guide",
382
386
  options.merge(:transform => { :class => AutomobileMakeModelYearVariant::ParserD, :year => year },
383
- :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' }) do
387
+ :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' },
388
+ :select => lambda { |row| row['model'].present? }) do
384
389
  key 'row_hash'
385
390
  store 'name', :field_name => 'model'
386
391
  store 'make_name', :field_name => 'make'
387
392
  store 'fuel_type_code', :field_name => 'FUEL TYPE'
393
+ store 'fuel_code', :field_name => 'FUEL TYPE'
388
394
  store 'fuel_efficiency_highway', :static => nil, :units => :kilometres_per_litre
389
395
  store 'fuel_efficiency_city', :static => nil, :units => :kilometres_per_litre
390
396
  store 'raw_fuel_efficiency_highway', :field_name => 'UNRND HWY (EPA)', :from_units => :miles_per_gallon, :to_units => :kilometres_per_litre
@@ -409,11 +415,13 @@ AutomobileMakeModelYearVariant.class_eval do
409
415
  }.sort { |a, b| a.first <=> b.first }.each do |year, options|
410
416
  import "#{ year } Fuel Economy Guide",
411
417
  options.merge(:transform => { :class => AutomobileMakeModelYearVariant::ParserE, :year => year },
412
- :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' }) do
418
+ :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/fuel_economy_guide/errata.csv' },
419
+ :select => lambda { |row| row['model'].present? }) do
413
420
  key 'row_hash'
414
421
  store 'name', :field_name => 'model'
415
422
  store 'make_name', :field_name => 'make'
416
423
  store 'fuel_type_code'
424
+ store 'fuel_code', :field_name => 'fuel_type_code'
417
425
  store 'fuel_efficiency_highway', :static => nil, :units => :kilometres_per_litre
418
426
  store 'fuel_efficiency_city', :static => nil, :units => :kilometres_per_litre
419
427
  store 'raw_fuel_efficiency_highway', :field_name => 'Hwy Unadj FE - Conventional Fuel', :from_units => :miles_per_gallon, :to_units => :kilometres_per_litre
@@ -433,9 +441,15 @@ AutomobileMakeModelYearVariant.class_eval do
433
441
  end
434
442
 
435
443
  process "Derive model and model year names" do
436
- update_all "make_model_name = CONCAT(make_name, ' ', name)"
437
- update_all "make_year_name = CONCAT(make_name, ' ', year)"
438
- update_all "make_model_year_name = CONCAT(make_name, ' ', name, ' ', year)"
444
+ if ActiveRecord::Base.connection.adapter_name == 'sqlite'
445
+ update_all "make_model_name = make_name || ' ' || name"
446
+ update_all "make_year_name = make_name || ' ' || year"
447
+ update_all "make_model_year_name = make_name || ' ' || name || ' ' || year"
448
+ else
449
+ update_all "make_model_name = CONCAT(make_name, ' ', name)"
450
+ update_all "make_year_name = CONCAT(make_name, ' ', year)"
451
+ update_all "make_model_year_name = CONCAT(make_name, ' ', name, ' ', year)"
452
+ end
439
453
  end
440
454
 
441
455
  # Note: need to divide by 0.425143707 b/c equation is designed for miles / gallon not km / l
@@ -3,11 +3,11 @@ class AutomobileMakeYear < ActiveRecord::Base
3
3
 
4
4
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
5
  has_many :fleet_years, :class_name => 'AutomobileMakeFleetYear', :foreign_key => 'make_year_name'
6
-
6
+
7
7
  data_miner do
8
8
  tap "Brighter Planet's make year data", Earth.taps_server
9
9
 
10
- process "bring in dependencies" do
10
+ process "Pull dependencies" do
11
11
  run_data_miner_on_belongs_to_associations
12
12
  end
13
13
  end
@@ -25,6 +25,7 @@ AutomobileMakeYear.class_eval do
25
25
  }
26
26
  end
27
27
 
28
+ # FIXME TODO make this a method on AutomobileMakeYear?
28
29
  process "Calculate fuel efficiency from make fleet years for makes with CAFE data" do
29
30
  AutomobileMakeFleetYear.run_data_miner!
30
31
  make_fleet_years = AutomobileMakeFleetYear.arel_table
@@ -54,6 +55,8 @@ AutomobileMakeYear.class_eval do
54
55
  }
55
56
  end
56
57
 
58
+ # FIXME TODO verify make_name and volume?
59
+
57
60
  verify "Year should be from 1985 to 2010" do
58
61
  AutomobileMakeYear.all.each do |make_year|
59
62
  unless make_year.year > 1984 and make_year.year < 2011
@@ -15,7 +15,8 @@ AutomobileSizeClass.class_eval do
15
15
  float 'conventional_fuel_efficiency_highway_multiplier'
16
16
  end
17
17
 
18
- import "A list of size classes and pre-calculated fuel efficiencies",
18
+ # FIXME TODO pull this from AutomobileSizeClassYear
19
+ import "a list of size classes and pre-calculated fuel efficiencies",
19
20
  :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHlRUE5IcWlrRENhN0EtUldPTy1rX1E&single=true&gid=0&output=csv' do
20
21
  key 'name'
21
22
  store 'type_name'
@@ -23,16 +24,17 @@ AutomobileSizeClass.class_eval do
23
24
  store 'fuel_efficiency_highway', :units_field_name => 'fuel_efficiency_highway_units'
24
25
  end
25
26
 
26
- process "Calculate annual distance from AutomobileTypeFuelAge" do
27
- AutomobileTypeFuelAge.run_data_miner!
28
- ages = AutomobileTypeFuelAge.arel_table
27
+ # FIXME TODO make this a method on AutomobileSizeClass?
28
+ process "Calculate annual distance from AutomobileTypeFuelYearAge" do
29
+ AutomobileTypeFuelYearAge.run_data_miner!
30
+ ages = AutomobileTypeFuelYearAge.arel_table
29
31
  classes = AutomobileSizeClass.arel_table
30
32
  conditional_relation = ages[:type_name].eq(classes[:type_name])
31
- update_all "annual_distance = (#{AutomobileTypeFuelAge.weighted_average_relation(:annual_distance, :weighted_by => :vehicles).where(conditional_relation).to_sql})"
33
+ update_all "annual_distance = (#{AutomobileTypeFuelYearAge.weighted_average_relation(:annual_distance, :weighted_by => :vehicles).where(conditional_relation).to_sql})"
32
34
  update_all "annual_distance_units = 'kilometres'"
33
35
  end
34
36
 
35
- import "pre-calculated hybridity multipliers",
37
+ import "pre-calculated fuel efficiency multipliers",
36
38
  :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGt2NnhXLXUxNFRJSzczU3BkSHB3enc&hl=en&single=true&gid=0&output=csv' do
37
39
  key 'name'
38
40
  store 'hybrid_fuel_efficiency_city_multiplier'
@@ -41,6 +43,8 @@ AutomobileSizeClass.class_eval do
41
43
  store 'conventional_fuel_efficiency_highway_multiplier'
42
44
  end
43
45
 
46
+ # FIXME TODO verify type_name?
47
+
44
48
  verify "Annual distance should be greater than zero" do
45
49
  AutomobileSizeClass.all.each do |size_class|
46
50
  unless size_class.annual_distance > 0
@@ -84,7 +88,7 @@ AutomobileSizeClass.class_eval do
84
88
  %w{ hybrid conventional }.each do |hybridity|
85
89
  %w{ city highway }.each do |type|
86
90
  multiplier = size_class.send(:"#{hybridity}_fuel_efficiency_#{type}_multiplier")
87
- if not multiplier.nil?
91
+ if multiplier.present?
88
92
  unless multiplier > 0
89
93
  raise "Invalid #{hybridity} fuel efficiency #{type} multiplier for AutomobileSizeClass #{size_class.name}: #{multiplier} (should be > 0)"
90
94
  end
@@ -21,7 +21,8 @@ AutomobileSizeClassYear.class_eval do
21
21
  store 'fuel_efficiency_highway', :units_field_name => 'fuel_efficiency_highway_units'
22
22
  end
23
23
 
24
- # FIXME TODO verify that type_name appears in AutomobileTypeFuelAges
24
+ # FIXME TODO verify that size_class_name is never missing?
25
+ # FIXME TODO verify that type_name appears in AutomobileTypeFuelYearAges
25
26
 
26
27
  verify "Year should be from 1975 to 2010" do
27
28
  AutomobileSizeClassYear.all.each do |record|
@@ -1,3 +1,4 @@
1
+ # DEPRECATED - use AutomobileTypeFuelYearAge
1
2
  class AutomobileTypeFuelAge < ActiveRecord::Base
2
3
  set_primary_key :name
3
4
 
@@ -1,3 +1,4 @@
1
+ # DEPRECATED - use AutomobileTypeFuelYearAge
1
2
  AutomobileTypeFuelAge.class_eval do
2
3
  data_miner do
3
4
  schema Earth.database_options do
@@ -15,8 +16,9 @@ AutomobileTypeFuelAge.class_eval do
15
16
  import "age distribution of gasoline passenger cars from the 2010 EPA GHG Inventory",
16
17
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
17
18
  :filename => 'Annex Tables/Annex 3/Table A-91.csv',
18
- :skip => 1 do
19
- key 'name', :synthesize => lambda { |row| "Passenger cars gasoline age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
19
+ :skip => 1,
20
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
21
+ key 'name', :synthesize => lambda { |row| "Passenger cars gasoline age #{row['Vehicle Age']}" }
20
22
  store 'type_name', :static => 'Passenger cars'
21
23
  store 'fuel_common_name', :static => 'gasoline'
22
24
  store 'age', :field_name => 'Vehicle Age'
@@ -26,8 +28,9 @@ AutomobileTypeFuelAge.class_eval do
26
28
  import "age distribution of gasoline light-duty trucks from the 2010 EPA GHG Inventory",
27
29
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
28
30
  :filename => 'Annex Tables/Annex 3/Table A-91.csv',
29
- :skip => 1 do
30
- key 'name', :synthesize => lambda { |row| "Light-duty trucks gasoline age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
31
+ :skip => 1,
32
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
33
+ key 'name', :synthesize => lambda { |row| "Light-duty trucks gasoline age #{row['Vehicle Age']}" }
31
34
  store 'type_name', :static => 'Light-duty trucks'
32
35
  store 'fuel_common_name', :static => 'gasoline'
33
36
  store 'age', :field_name => 'Vehicle Age'
@@ -37,8 +40,9 @@ AutomobileTypeFuelAge.class_eval do
37
40
  import "age distribution of diesel passenger cars from the 2010 EPA GHG Inventory",
38
41
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
39
42
  :filename => 'Annex Tables/Annex 3/Table A-91.csv',
40
- :skip => 1 do
41
- key 'name', :synthesize => lambda { |row| "Passenger cars diesel age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
43
+ :skip => 1,
44
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
45
+ key 'name', :synthesize => lambda { |row| "Passenger cars diesel age #{row['Vehicle Age']}" }
42
46
  store 'type_name', :static => 'Passenger cars'
43
47
  store 'fuel_common_name', :static => 'diesel'
44
48
  store 'age', :field_name => 'Vehicle Age'
@@ -48,8 +52,9 @@ AutomobileTypeFuelAge.class_eval do
48
52
  import "age distribution of diesel light-duty trucks from the 2010 EPA GHG Inventory",
49
53
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
50
54
  :filename => 'Annex Tables/Annex 3/Table A-91.csv',
51
- :skip => 1 do
52
- key 'name', :synthesize => lambda { |row| "Light-duty trucks diesel age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
55
+ :skip => 1,
56
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
57
+ key 'name', :synthesize => lambda { |row| "Light-duty trucks diesel age #{row['Vehicle Age']}" }
53
58
  store 'type_name', :static => 'Light-duty trucks'
54
59
  store 'fuel_common_name', :static => 'diesel'
55
60
  store 'age', :field_name => 'Vehicle Age'
@@ -59,64 +64,72 @@ AutomobileTypeFuelAge.class_eval do
59
64
  import "total travel distribution of gasoline passenger cars from the 2010 EPA GHG Inventory",
60
65
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
61
66
  :filename => 'Annex Tables/Annex 3/Table A-93.csv',
62
- :skip => 1 do
63
- key 'name', :synthesize => lambda { |row| "Passenger cars gasoline age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
67
+ :skip => 1,
68
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
69
+ key 'name', :synthesize => lambda { |row| "Passenger cars gasoline age #{row['Vehicle Age']}" }
64
70
  store 'total_travel_percent', :synthesize => lambda { |row| row['LDGV'].to_f / 100 }
65
71
  end
66
72
 
67
73
  import "total travel distribution of gasoline light-duty trucks from the 2010 EPA GHG Inventory",
68
74
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
69
75
  :filename => 'Annex Tables/Annex 3/Table A-93.csv',
70
- :skip => 1 do
71
- key 'name', :synthesize => lambda { |row| "Light-duty trucks gasoline age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
76
+ :skip => 1,
77
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
78
+ key 'name', :synthesize => lambda { |row| "Light-duty trucks gasoline age #{row['Vehicle Age']}" }
72
79
  store 'total_travel_percent', :synthesize => lambda { |row| row['LDGT'].to_f / 100 }
73
80
  end
74
81
 
75
82
  import "total travel distribution of diesel passenger cars from the 2010 EPA GHG Inventory",
76
83
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
77
84
  :filename => 'Annex Tables/Annex 3/Table A-93.csv',
78
- :skip => 1 do
79
- key 'name', :synthesize => lambda { |row| "Passenger cars diesel age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
85
+ :skip => 1,
86
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
87
+ key 'name', :synthesize => lambda { |row| "Passenger cars diesel age #{row['Vehicle Age']}" }
80
88
  store 'total_travel_percent', :synthesize => lambda { |row| row['LDDV'].to_f / 100 }
81
89
  end
82
90
 
83
91
  import "total travel distribution of diesel light-duty trucks from the 2010 EPA GHG Inventory",
84
92
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
85
93
  :filename => 'Annex Tables/Annex 3/Table A-93.csv',
86
- :skip => 1 do
87
- key 'name', :synthesize => lambda { |row| "Light-duty trucks diesel age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
94
+ :skip => 1,
95
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
96
+ key 'name', :synthesize => lambda { |row| "Light-duty trucks diesel age #{row['Vehicle Age']}" }
88
97
  store 'total_travel_percent', :synthesize => lambda { |row| row['LDDT'].to_f / 100 }
89
98
  end
90
99
 
91
100
  import "average annual distance for gasoline passenger cars from the 2010 EPA GHG Inventory",
92
101
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
93
102
  :filename => 'Annex Tables/Annex 3/Table A-92.csv',
94
- :skip => 1 do
95
- key 'name', :synthesize => lambda { |row| "Passenger cars gasoline age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
103
+ :skip => 1,
104
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
105
+ key 'name', :synthesize => lambda { |row| "Passenger cars gasoline age #{row['Vehicle Age']}" }
96
106
  store 'annual_distance', :synthesize => lambda { |row| row['LDGV'].to_s.sub(',', '') }, :units => :miles
97
107
  end
98
108
 
99
109
  import "average annual distance for gasoline light-duty trucks from the 2010 EPA GHG Inventory",
100
110
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
101
111
  :filename => 'Annex Tables/Annex 3/Table A-92.csv',
102
- :skip => 1 do
103
- key 'name', :synthesize => lambda { |row| "Light-duty trucks gasoline age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
112
+ :skip => 1,
113
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
114
+ key 'name', :synthesize => lambda { |row| "Light-duty trucks gasoline age #{row['Vehicle Age']}" }
104
115
  store 'annual_distance', :synthesize => lambda { |row| row['LDGT'].to_s.sub(',', '') }, :units => :miles
105
116
  end
106
117
 
107
118
  import "average annual distance for diesel passenger cars from the 2010 EPA GHG Inventory",
108
119
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
109
120
  :filename => 'Annex Tables/Annex 3/Table A-92.csv',
110
- :skip => 1 do
111
- key 'name', :synthesize => lambda { |row| "Passenger cars diesel age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
121
+ :skip => 1,
122
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
123
+ key 'name', :synthesize => lambda { |row| "Passenger cars diesel age #{row['Vehicle Age']}" }
112
124
  store 'annual_distance', :synthesize => lambda { |row| row['LDDV'].to_s.sub(',', '') }, :units => :miles
113
125
  end
114
126
 
115
127
  import "average annual distance for diesel light-duty trucks from the 2010 EPA GHG Inventory",
116
128
  :url => 'http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
117
129
  :filename => 'Annex Tables/Annex 3/Table A-92.csv',
118
- :skip => 1 do
119
- key 'name', :synthesize => lambda { |row| "Light-duty trucks diesel age #{row['Vehicle Age']}" if row['Vehicle Age'].length < 3 }
130
+ :skip => 1,
131
+ :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] } do
132
+ key 'name', :synthesize => lambda { |row| "Light-duty trucks diesel age #{row['Vehicle Age']}" }
120
133
  store 'annual_distance', :synthesize => lambda { |row| row['LDDT'].to_s.sub(',', '') }, :units => :miles
121
134
  end
122
135
 
@@ -126,18 +139,23 @@ AutomobileTypeFuelAge.class_eval do
126
139
  update_all "annual_distance_units = 'kilometres'"
127
140
  end
128
141
 
129
- process "Calculate number of vehicles from total travel and total travel percent" do
142
+ # FIXME TODO maybe make this a method on AutomobileTypeFuelAge?
143
+ process "Calculate number of vehicles from total travel percent and AutomobileTypeFuelYear" do
130
144
  AutomobileTypeFuelYear.run_data_miner!
131
145
  connection.execute %{
132
146
  UPDATE automobile_type_fuel_ages
133
- SET automobile_type_fuel_ages.vehicles =
134
- ((SELECT automobile_type_fuel_years.total_travel
135
- FROM automobile_type_fuel_years
136
- WHERE automobile_type_fuel_years.year =
137
- (SELECT max(automobile_type_fuel_years.year) FROM automobile_type_fuel_years)
138
- AND automobile_type_fuel_years.type_name = automobile_type_fuel_ages.type_name
139
- AND automobile_type_fuel_years.fuel_common_name = automobile_type_fuel_ages.fuel_common_name
140
- ) * automobile_type_fuel_ages.total_travel_percent / automobile_type_fuel_ages.annual_distance )
147
+ SET vehicles =
148
+ (
149
+ (
150
+ SELECT automobile_type_fuel_years.total_travel
151
+ FROM automobile_type_fuel_years
152
+ WHERE automobile_type_fuel_years.`year` =
153
+ (SELECT max(automobile_type_fuel_years.`year`) FROM automobile_type_fuel_years)
154
+ AND automobile_type_fuel_years.`type_name` = automobile_type_fuel_ages.`type_name`
155
+ AND automobile_type_fuel_years.fuel_common_name = automobile_type_fuel_ages.fuel_common_name
156
+ ) *
157
+ automobile_type_fuel_ages.total_travel_percent / automobile_type_fuel_ages.annual_distance
158
+ )
141
159
  }
142
160
  end
143
161
 
@@ -145,7 +163,7 @@ AutomobileTypeFuelAge.class_eval do
145
163
  AutomobileTypeFuelAge.all.each do |record|
146
164
  %w{ type_name fuel_common_name }.each do |attribute|
147
165
  value = record.send(:"#{attribute}")
148
- if value.nil?
166
+ unless value.present?
149
167
  raise "Missing #{attribute} for AutomobileTypeFuelAge '#{record.name}'"
150
168
  end
151
169
  end