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,6 +1,8 @@
1
1
  class AutomobileTypeYear < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
+ has_many :type_fuel_years, :class_name => 'AutomobileTypeFuelYear', :foreign_key => 'type_year_name'
5
+
4
6
  data_miner do
5
7
  tap "Brighter Planet's sanitized automobile type year data", Earth.taps_server
6
8
  end
@@ -6,6 +6,8 @@ AutomobileTypeYear.class_eval do
6
6
  integer 'year'
7
7
  float 'hfc_emissions'
8
8
  string 'hfc_emissions_units'
9
+ float 'hfc_emission_factor'
10
+ string 'hfc_emission_factor_units'
9
11
  end
10
12
 
11
13
  import "automobile type year air conditioning emissions derived from the 2010 EPA GHG Inventory",
@@ -16,10 +18,19 @@ AutomobileTypeYear.class_eval do
16
18
  store 'hfc_emissions', :units_field_name => 'hfc_emissions_units'
17
19
  end
18
20
 
21
+ process "Calculate HFC emission factor from AutomobileTypeFuelYear" do
22
+ AutomobileTypeFuelYear.run_data_miner!
23
+ AutomobileTypeYear.all.each do |record|
24
+ record.hfc_emission_factor = record.hfc_emissions / record.type_fuel_years.sum('fuel_consumption')
25
+ record.hfc_emission_factor_units = "kilograms_co2e_per_litre"
26
+ record.save
27
+ end
28
+ end
29
+
19
30
  verify "Type name should never be missing" do
20
31
  AutomobileTypeYear.all.each do |record|
21
32
  value = record.send(:type_name)
22
- if value.nil?
33
+ unless value.present?
23
34
  raise "Missing type name for AutomobileTypeYear '#{record.name}'"
24
35
  end
25
36
  end
@@ -34,20 +45,26 @@ AutomobileTypeYear.class_eval do
34
45
  end
35
46
  end
36
47
 
37
- verify "HFC emissions should be zero or more" do
38
- AutomobileTypeYear.all.each do |record|
39
- emissions = record.send(:hfc_emissions)
40
- unless emissions >= 0
41
- raise "Invalid HFC emissions for AutomobileTypeYear '#{record.name}': #{emissions} (should be zero or more)"
48
+ %w{ hfc_emissions hfc_emission_factor }.each do |attribute|
49
+ verify "#{attribute.humanize} should be zero or more" do
50
+ AutomobileTypeYear.all.each do |record|
51
+ value = record.send(:"#{attribute}")
52
+ unless value >= 0
53
+ raise "Invalid #{attribute.humanize.downcase} for AutomobileTypeYear '#{record.name}': #{value} (should be zero or more)"
54
+ end
42
55
  end
43
56
  end
44
57
  end
45
58
 
46
- verify "HFC emissions units should be kilograms CO2e" do
47
- AutomobileTypeYear.all.each do |record|
48
- units = record.send(:hfc_emissions_units)
49
- unless units == "kilograms_co2e"
50
- raise "Invalid HFC emissions units for AutomobileTypeYear '#{record.name}': #{units} (should be kilograms_co2e)"
59
+ [["hfc_emissions_units", "kilograms_co2e"], ["hfc_emission_factor_units", "kilograms_co2e_per_litre"]].each do |pair|
60
+ attribute = pair[0]
61
+ proper_units = pair[1]
62
+ verify "#{attribute.humanize} should be #{proper_units.humanize.downcase}" do
63
+ AutomobileTypeYear.all.each do |record|
64
+ units = record.send(:"#{attribute}")
65
+ unless units == proper_units
66
+ raise "Invalid #{attribute.humanize.downcase} for AutomobileTypeYear '#{record.name}': #{units} (should be #{proper_units})"
67
+ end
51
68
  end
52
69
  end
53
70
  end
@@ -1,3 +1,4 @@
1
+ require 'earth/automobile/automobile_fuel/data_miner'
1
2
  require 'earth/automobile/automobile_fuel_type/data_miner'
2
3
  require 'earth/automobile/automobile_make/data_miner'
3
4
  require 'earth/automobile/automobile_make_fleet_year/data_miner'
@@ -11,4 +12,5 @@ require 'earth/automobile/automobile_type_year/data_miner'
11
12
  require 'earth/automobile/automobile_type_fuel_age/data_miner'
12
13
  require 'earth/automobile/automobile_type_fuel_control/data_miner'
13
14
  require 'earth/automobile/automobile_type_fuel_year/data_miner'
15
+ require 'earth/automobile/automobile_type_fuel_year_age/data_miner'
14
16
  require 'earth/automobile/automobile_type_fuel_year_control/data_miner'
@@ -1,14 +1,14 @@
1
1
  class BusClass < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- # https://brighterplanet.sifterapp.com/projects/30/issues/454
5
- falls_back_on :distance => 5.45.miles.to(:kilometres),
6
- :passengers => 8.25,
7
- :speed => 19.67.miles.to(:kilometres),
8
- :diesel_intensity => 0.143.gallons_per_mile.to(:litres_per_kilometre),
9
- :alternative_fuels_intensity => 0.0517.gallons_per_mile.to(:litres_per_kilometre),
10
- :air_conditioning_emission_factor => 0.0203.pounds_per_mile.to(:kilograms_per_kilometre)
11
-
4
+ # https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHY0R0hjZnhRaTIxTnpvRk1HNThwUmc&hl=en&output=html
5
+ falls_back_on :passengers => 7.485,
6
+ :distance => 4.9726.miles.to(:kilometres),
7
+ :speed => 19.849.miles.to(:kilometres),
8
+ :diesel_intensity => 0.13836.gallons_per_mile.to(:litres_per_kilometre),
9
+ :alternative_fuels_intensity => 0.04632038.gallons_per_mile.to(:litres_per_kilometre),
10
+ :air_conditioning_emission_factor => 0.04779 / 1.miles.to(:kilometres)
11
+
12
12
  data_miner do
13
13
  tap "Brighter Planet's sanitized bus class data", Earth.taps_server
14
14
  end
@@ -13,7 +13,7 @@ ComputationPlatform.class_eval do
13
13
 
14
14
  verify "Data center company name should never be missing" do
15
15
  ComputationPlatform.all.each do |platform|
16
- if platform.data_center_company_name.nil?
16
+ unless platform.data_center_company_name.present?
17
17
  raise "Invalid data center company name for DataCenterCompany #{platform.name}: #{platform.data_center_company_name}"
18
18
  end
19
19
  end
@@ -24,7 +24,7 @@ ServerType.class_eval do
24
24
 
25
25
  verify "Data center company name should never be missing" do
26
26
  ServerType.all.each do |server|
27
- if server.data_center_company_name.nil?
27
+ unless server.data_center_company_name.present?
28
28
  raise "Invalid data center company name for ServerType #{server.name}: #{server.data_center_company_name}"
29
29
  end
30
30
  end
@@ -20,7 +20,7 @@ ServerTypeAlias.class_eval do
20
20
  ServerTypeAlias.all.each do |server_alias|
21
21
  [:server_type_name, :platform_name].each do |x|
22
22
  test_item = server_alias.send(x)
23
- if test_item.nil?
23
+ unless test_item.present?
24
24
  raise "Invalid #{x} for ServerTypeAlias #{server_alias.name}: #{test_item}"
25
25
  end
26
26
  end
@@ -30,7 +30,9 @@ Conversions.register(:square_feet, :square_metres, 0.09290304)
30
30
  Conversions.register(:pounds_per_square_foot, :kilograms_per_square_metre, 4.88242764)
31
31
  Conversions.register(:kilograms_per_kilowatt_hour, :kilograms_per_megawatt_hour, 1_000.0)
32
32
  Conversions.register(:btus, :joules, 1_055.05585)
33
+ Conversions.register(:btus, :megajoules, 0.00105505585)
33
34
  Conversions.register(:kbtus, :joules, 1_000.0 * 1_055.05585)
35
+ Conversions.register(:kbtus, :megajoules, 1.05505585)
34
36
  Conversions.register(:cords, :joules, 2.11011171e10)
35
37
 
36
38
  Conversions.register(:gallons_per_mile, :litres_per_kilometre, 2.35214583)
@@ -43,3 +45,5 @@ Conversions.register :kilocalories_per_pound, :joules_per_kilogram, 9_224.14105
43
45
  Conversions.register :grams_per_kilocalorie, :kilograms_per_joule, 2.39005736e-7
44
46
  # 1 joule = 0.000239005736 kilocalories
45
47
  Conversions.register :joules, :kilocalories, 0.000239005736
48
+
49
+ Conversions.register :carbon, :co2, (44.0 / 12.0)
data/lib/earth/fuel.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'earth/fuel/fuel'
1
2
  require 'earth/fuel/fuel_price'
2
3
  require 'earth/fuel/fuel_type'
3
4
  require 'earth/fuel/fuel_year'
@@ -1,3 +1,4 @@
1
+ require 'earth/fuel/fuel/data_miner'
1
2
  require 'earth/fuel/fuel_price/data_miner'
2
3
  require 'earth/fuel/fuel_type/data_miner'
3
4
  require 'earth/fuel/fuel_year/data_miner'
@@ -0,0 +1,77 @@
1
+ class Fuel < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ has_many :fuel_years, :foreign_key => 'fuel_name'
5
+
6
+ data_miner do
7
+ tap "Brighter Planet's fuels data", Earth.taps_server
8
+ end
9
+
10
+ def latest_year
11
+ fuel_years.find_by_year(fuel_years.maximum('year'))
12
+ end
13
+
14
+ def energy_content
15
+ if content = super
16
+ content
17
+ elsif fuel_years.present?
18
+ latest_year.energy_content
19
+ end
20
+ end
21
+
22
+ def energy_content_units
23
+ if units = super
24
+ units
25
+ elsif fuel_years.present?
26
+ latest_year.energy_content_units
27
+ end
28
+ end
29
+
30
+ def carbon_content
31
+ if content = super
32
+ content
33
+ elsif fuel_years.present?
34
+ latest_year.carbon_content
35
+ end
36
+ end
37
+
38
+ def carbon_content_units
39
+ if units = super
40
+ units
41
+ elsif fuel_years.present?
42
+ latest_year.carbon_content_units
43
+ end
44
+ end
45
+
46
+ def co2_emission_factor
47
+ if ef = super
48
+ ef
49
+ elsif fuel_years.present?
50
+ latest_year.co2_emission_factor
51
+ end
52
+ end
53
+
54
+ def co2_emission_factor_units
55
+ if units = super
56
+ units
57
+ elsif fuel_years.present?
58
+ latest_year.co2_emission_factor_units
59
+ end
60
+ end
61
+
62
+ def co2_biogenic_emission_factor
63
+ if ef = super
64
+ ef
65
+ elsif fuel_years.present?
66
+ latest_year.co2_biogenic_emission_factor
67
+ end
68
+ end
69
+
70
+ def co2_biogenic_emission_factor_units
71
+ if units = super
72
+ units
73
+ elsif fuel_years.present?
74
+ latest_year.co2_biogenic_emission_factor_units
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,98 @@
1
+ Fuel.class_eval do
2
+ data_miner do
3
+ schema Earth.database_options do
4
+ string 'name'
5
+ float 'density'
6
+ string 'density_units'
7
+ float 'energy_content'
8
+ string 'energy_content_units'
9
+ float 'carbon_content'
10
+ string 'carbon_content_units'
11
+ float 'oxidation_factor'
12
+ float 'biogenic_fraction'
13
+ float 'co2_emission_factor'
14
+ string 'co2_emission_factor_units'
15
+ float 'co2_biogenic_emission_factor'
16
+ string 'co2_biogenic_emission_factor_units'
17
+ end
18
+
19
+ process "Derive fuel names from FuelYear" do
20
+ FuelYear.run_data_miner!
21
+ connection.execute %{
22
+ INSERT IGNORE INTO fuels(name)
23
+ SELECT DISTINCT fuel_years.fuel_name FROM fuel_years
24
+ }
25
+ end
26
+
27
+ import "fuels with non-variable characteristics",
28
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGJmYkdtajZyV3Byb0lrd21xLVhXUGc&hl=en&output=csv' do
29
+ key 'name'
30
+ store 'energy_content', :units_field_name => 'energy_content_units'
31
+ store 'carbon_content', :units_field_name => 'carbon_content_units'
32
+ store 'oxidation_factor'
33
+ store 'biogenic_fraction'
34
+ end
35
+
36
+ import "densities for aircraft fuels",
37
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHBjTVE4NmRlc05iUHVZR1E3eEJwOGc&hl=en&output=csv' do
38
+ key 'name'
39
+ store 'density', :units_field_name => 'density_units'
40
+ end
41
+
42
+ process "Convert energy content of liquid fuels to metric units" do
43
+ conversion_factor = (1_055.05585 / 1.0) * (1.0 / 158.987295) # Google: 1_055.05585 MJ / 1 MMBtu * 1 barrel / 158.987295 l
44
+ connection.execute %{
45
+ UPDATE fuels
46
+ SET energy_content = energy_content * #{conversion_factor}, energy_content_units = 'megajoules_per_litre'
47
+ WHERE energy_content_units = 'million_btu_per_barrel'
48
+ }
49
+ end
50
+
51
+ process "Convert energy content of gaseous fuels to metric units" do
52
+ conversion_factor = (1.0 / 947.81712) * (35.3146667 / 1.0) # Google: 1.0 MJ / 947.81712 Btu * 35.3146667 cubic feet / 1 cubic m
53
+ connection.execute %{
54
+ UPDATE fuels
55
+ SET energy_content = energy_content * #{conversion_factor}, energy_content_units = 'megajoules_per_cubic_metre'
56
+ WHERE energy_content_units = 'btu_per_cubic_foot'
57
+ }
58
+ end
59
+
60
+ process "Convert carbon content to metric units" do
61
+ conversion_factor = (1_000_000_000_000.0 / 1.0) * (1.0 / 1_055_055_852_620.0) # Google: 1e12 g / Tg * 1 QBtu / 1.055e12 MJ
62
+ connection.execute %{
63
+ UPDATE fuels
64
+ SET carbon_content = carbon_content * #{conversion_factor}, carbon_content_units = 'grams_per_megajoule'
65
+ WHERE carbon_content_units = 'teragrams_per_quadrillion_btu'
66
+ }
67
+ end
68
+
69
+ process "Calculate CO2 and CO2 biogenic emission factors" do
70
+ conversion_factor = (1.0 / 1_000.0) * (44.0 / 12.0) # Google: 1 kg / 1e3 g * 44 CO2 / 12 C
71
+ connection.execute %{
72
+ UPDATE fuels
73
+ SET co2_emission_factor = carbon_content * energy_content * oxidation_factor * (1 - biogenic_fraction) * #{conversion_factor},
74
+ co2_biogenic_emission_factor = carbon_content * energy_content * oxidation_factor * biogenic_fraction * #{conversion_factor}
75
+ }
76
+ end
77
+
78
+ process "Update emission factor units for liquid fuels" do
79
+ connection.execute %{
80
+ UPDATE fuels
81
+ SET co2_emission_factor_units = 'kilograms_per_litre',
82
+ co2_biogenic_emission_factor_units = 'kilograms_per_litre'
83
+ WHERE energy_content_units = 'megajoules_per_litre'
84
+ }
85
+ end
86
+
87
+ process "Update emission factor units for gaseous fuels" do
88
+ connection.execute %{
89
+ UPDATE fuels
90
+ SET co2_emission_factor_units = 'kilograms_per_cubic_metre',
91
+ co2_biogenic_emission_factor_units = 'kilograms_per_cubic_metre'
92
+ WHERE energy_content_units = 'megajoules_per_cubic_metre'
93
+ }
94
+ end
95
+
96
+ # FIXME TODO verify this stuff
97
+ end
98
+ end
@@ -3,7 +3,7 @@ FuelType.class_eval do
3
3
  # annual emissions factors
4
4
  # annual energy contents
5
5
  # gas-specific emissions factors
6
-
6
+
7
7
  data_miner do
8
8
  schema Earth.database_options do
9
9
  string 'name'
@@ -27,7 +27,7 @@ FuelType.class_eval do
27
27
  # Conversions.register :carbon, :carbon_dioxide, 3.66666667
28
28
  # end
29
29
 
30
- import "a list of fuels and their emissions factors and densities",
30
+ import "a list of fuels and their emission factors and densities",
31
31
  :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdDR3RjlTcWlsLTc2TzQ0cERTMElJbHc&single=true&gid=0&output=csv' do
32
32
  key 'name', :field_name => 'fuel'
33
33
  store 'emission_factor', :units_field_name => 'emission_factor_units'
@@ -4,38 +4,107 @@ FuelYear.class_eval do
4
4
  string 'name'
5
5
  string 'fuel_name'
6
6
  integer 'year'
7
- string 'fuel_common_name'
8
- float 'carbon_content'
9
- string 'carbon_content_units'
10
7
  float 'energy_content'
11
8
  string 'energy_content_units'
9
+ float 'carbon_content'
10
+ string 'carbon_content_units'
11
+ float 'oxidation_factor'
12
+ float 'biogenic_fraction'
12
13
  float 'co2_emission_factor'
13
14
  string 'co2_emission_factor_units'
15
+ float 'co2_biogenic_emission_factor'
16
+ string 'co2_biogenic_emission_factor_units'
14
17
  end
15
18
 
16
- import "a list of fuel years and their carbon and energy contents, derived from the 2010 EPA GHG Inventory",
17
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdFZVSlZ3SUZsTzZLVTB5bVk5THdBN2c&hl=en&single=true&gid=0&output=csv' do
19
+ import "fuels with annually variable characteristics, derived from the 2010 EPA GHG Inventory",
20
+ :url => 'https://spreadsheets.google.com/pub?hl=en&hl=en&key=0AoQJbWqPrREqdFZVSlZ3SUZsTzZLVTB5bVk5THdBN2c&output=csv' do
18
21
  key 'name'
19
22
  store 'fuel_name'
20
23
  store 'year'
21
- store 'fuel_common_name'
22
- store 'carbon_content', :units_field_name => 'carbon_content_units'
23
24
  store 'energy_content', :units_field_name => 'energy_content_units'
25
+ store 'carbon_content', :units_field_name => 'carbon_content_units'
26
+ store 'oxidation_factor'
27
+ store 'biogenic_fraction'
28
+ end
29
+
30
+ process "Convert energy content of solid fuels to metric units" do
31
+ conversion_factor = (1_055.05585 / 1.0) * (1.0 / 907.18474) # Google: 1_055.05585 MJ / 1 MMBtu * 1 short ton / 907.18474 kg
32
+ connection.execute %{
33
+ UPDATE fuel_years
34
+ SET energy_content = energy_content * #{conversion_factor}, energy_content_units = 'megajoules_per_kilogram'
35
+ WHERE energy_content_units = 'million_btu_per_short_ton'
36
+ }
37
+ end
38
+
39
+ process "Convert energy content of liquid fuels to metric units" do
40
+ conversion_factor = (1_055.05585 / 1.0) * (1.0 / 158.987295) # Google: 1_055.05585 MJ / 1 MMBtu * 1 barrel / 158.987295 l
41
+ connection.execute %{
42
+ UPDATE fuel_years
43
+ SET energy_content = energy_content * #{conversion_factor}, energy_content_units = 'megajoules_per_litre'
44
+ WHERE energy_content_units = 'million_btu_per_barrel'
45
+ }
46
+ end
47
+
48
+ process "Convert energy content of gaseous fuels to metric units" do
49
+ conversion_factor = (1.0 / 947.81712) * (35.3146667 / 1.0) # Google: 1.0 MJ / 947.81712 Btu * 35.3146667 cubic feet / 1 cubic m
50
+ connection.execute %{
51
+ UPDATE fuel_years
52
+ SET energy_content = energy_content * #{conversion_factor}, energy_content_units = 'megajoules_per_cubic_metre'
53
+ WHERE energy_content_units = 'btu_per_cubic_foot'
54
+ }
24
55
  end
25
56
 
26
- process "Calculate CO2 emission factor" do
27
- conversion_factor = (1000000.0 / 1000000000000000.0) * (1000000000000.0 / 1000.0) * (1.0 / 158.987295) * (44.0 / 12.0)
28
- update_all "co2_emission_factor = carbon_content * energy_content * #{conversion_factor}"
29
- update_all "co2_emission_factor_units = 'kilograms_per_litre'"
57
+ process "Convert carbon content to metric units" do
58
+ conversion_factor = (1_000_000_000_000.0 / 1.0) * (1.0 / 1_055_055_852_620.0) # Google: 1e12 g / Tg * 1 QBtu / 1.055e12 MJ
59
+ connection.execute %{
60
+ UPDATE fuel_years
61
+ SET carbon_content = carbon_content * #{conversion_factor}, carbon_content_units = 'grams_per_megajoule'
62
+ WHERE carbon_content_units = 'teragrams_per_quadrillion_btu'
63
+ }
30
64
  end
31
65
 
32
- verify "Fuel name and fuel common name should never be missing" do
66
+ process "Calculate CO2 and CO2 biogenic emission factors" do
67
+ conversion_factor = (1.0 / 1_000.0) * (44.0 / 12.0) # Google: 1 kg / 1e3 g * 44 CO2 / 12 C
68
+ connection.execute %{
69
+ UPDATE fuel_years
70
+ SET co2_emission_factor = carbon_content * energy_content * oxidation_factor * (1 - biogenic_fraction) * #{conversion_factor},
71
+ co2_biogenic_emission_factor = carbon_content * energy_content * oxidation_factor * biogenic_fraction * #{conversion_factor}
72
+ }
73
+ end
74
+
75
+ process "Update emission factor units for solid fuels" do
76
+ connection.execute %{
77
+ UPDATE fuel_years
78
+ SET co2_emission_factor_units = 'kilograms_per_kilogram',
79
+ co2_biogenic_emission_factor_units = 'kilograms_per_kilogram'
80
+ WHERE energy_content_units = 'megajoules_per_kilogram'
81
+ }
82
+ end
83
+
84
+ process "Update emission factor units for liquid fuels" do
85
+ connection.execute %{
86
+ UPDATE fuel_years
87
+ SET co2_emission_factor_units = 'kilograms_per_litre',
88
+ co2_biogenic_emission_factor_units = 'kilograms_per_litre'
89
+ WHERE energy_content_units = 'megajoules_per_litre'
90
+ }
91
+ end
92
+
93
+ process "Update emission factor units for gaseous fuels" do
94
+ connection.execute %{
95
+ UPDATE fuel_years
96
+ SET co2_emission_factor_units = 'kilograms_per_cubic_metre',
97
+ co2_biogenic_emission_factor_units = 'kilograms_per_cubic_metre'
98
+ WHERE energy_content_units = 'megajoules_per_cubic_metre'
99
+ }
100
+ end
101
+
102
+ # FIXME TODO verify fuel name is in Fuel
103
+ verify "Fuel name should never be missing" do
33
104
  FuelYear.all.each do |record|
34
- %w{ fuel_name fuel_common_name }.each do |attribute|
35
- value = record.send(:"#{attribute}")
36
- if value.nil?
37
- raise "Missing #{attribute} for FuelYear '#{record.name}'"
38
- end
105
+ fuel_name = record.send(:fuel_name)
106
+ if fuel_name.nil?
107
+ raise "Missing fuel name for FuelYear '#{record.name}'"
39
108
  end
40
109
  end
41
110
  end
@@ -49,9 +118,9 @@ FuelYear.class_eval do
49
118
  end
50
119
  end
51
120
 
52
- verify "Carbon content, energy content, and CO2 emission factor should be greater than zero" do
121
+ verify "Carbon content and energy content should be greater than zero" do
53
122
  FuelYear.all.each do |record|
54
- %w{ carbon_content energy_content co2_emission_factor }.each do |attribute|
123
+ %w{ carbon_content energy_content }.each do |attribute|
55
124
  value = record.send(:"#{attribute}")
56
125
  unless value > 0
57
126
  raise "Invalid #{attribute} for FuelYear '#{record.name}': #{value} (should be > 0)"
@@ -60,17 +129,17 @@ FuelYear.class_eval do
60
129
  end
61
130
  end
62
131
 
63
- verify "Units should be correct" do
132
+ verify "Emission factors should be zero or more" do
64
133
  FuelYear.all.each do |record|
65
- ["carbon_content_units teragrams_per_quadrillion_btu", "energy_content_units million_btu_per_barrel", "co2_emission_factor_units kilograms_per_litre"].each do |pair|
66
- attribute = pair.split[0]
67
- proper_units = pair.split[1]
68
- units = record.send(:"#{attribute}")
69
- unless units == proper_units
70
- raise "Invalid #{attribute} for FuelYear '#{record.name}': #{units} (should be #{proper_units})"
134
+ %w{ co2_emission_factor co2_biogenic_emission_factor }.each do |attribute|
135
+ value = record.send(:"#{attribute}")
136
+ unless value >= 0
137
+ raise "Invalid #{attribute} for FuelYear '#{record.name}': #{value} (should be >= 0)"
71
138
  end
72
139
  end
73
140
  end
74
141
  end
142
+
143
+ # FIXME TODO verify units
75
144
  end
76
145
  end