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
@@ -2,73 +2,63 @@ Feature: Data import for CarrierMode
2
2
  As a data user
3
3
  I want to import CarrierMode data
4
4
  So that I can perform carrier mode-based calculations
5
-
5
+
6
6
  Scenario: Successfully verifying that carrier name is never missing
7
7
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
8
8
  When a data import verifies "Carrier name should never be missing"
9
9
  Then the verification should be successful
10
-
10
+
11
11
  Scenario: Failing to verify that carrier name is never missing
12
12
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
13
13
  When a data import verifies "Carrier name should never be missing"
14
14
  Then the verification should not be successful
15
-
15
+
16
16
  Scenario: Successfully verifying that mode name is never missing
17
17
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
18
18
  When a data import verifies "Mode name should never be missing"
19
19
  Then the verification should be successful
20
-
20
+
21
21
  Scenario: Failing to verify that mode name is never missing
22
22
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
23
23
  When a data import verifies "Mode name should never be missing"
24
24
  Then the verification should not be successful
25
-
26
- Scenario: Successfully verifying that include in fallbacks is never missing
25
+
26
+ Scenario: Successfully verifying that transport emission factor units is never missing
27
27
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
28
- When a data import verifies "Include in fallbacks should never be missing"
28
+ When a data import verifies "Transport emission factor units should never be missing"
29
29
  Then the verification should be successful
30
-
31
- Scenario: Failing to verify that include in fallbacks is never missing
30
+
31
+ Scenario: Failing to verify that transport emission factor units is never missing
32
32
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
33
- When a data import verifies "Include in fallbacks should never be missing"
33
+ When a data import verifies "Transport emission factor units should never be missing"
34
34
  Then the verification should not be successful
35
-
35
+
36
36
  Scenario: Successfully verifying that package volume > 0
37
37
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
38
38
  When a data import verifies "Package volume should be greater than zero"
39
39
  Then the verification should be successful
40
-
40
+
41
41
  Scenario: Failing to verify that package volume > 0
42
42
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
43
43
  When a data import verifies "Package volume should be greater than zero"
44
44
  Then the verification should not be successful
45
-
46
- Scenario: Successfully verifying that route inefficiency factor >= 1
47
- Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
48
- When a data import verifies "Route inefficiency factor should be one or more"
49
- Then the verification should be successful
50
-
51
- Scenario: Failing to verify that route inefficiency factor >= 1
52
- Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
53
- When a data import verifies "Route inefficiency factor should be one or more"
54
- Then the verification should not be successful
55
-
45
+
56
46
  Scenario: Successfully verifying that transport emission factor > 0
57
47
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
58
48
  When a data import verifies "Transport emission factor should be greater than zero"
59
49
  Then the verification should be successful
60
-
50
+
61
51
  Scenario: Failing to verify that transport emission factor > 0
62
52
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
63
53
  When a data import verifies "Transport emission factor should be greater than zero"
64
54
  Then the verification should not be successful
65
-
66
- Scenario: Successfully verifying that transport emission factor units is never missing
55
+
56
+ Scenario: Successfully verifying that route inefficiency factor >= 1
67
57
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_good"
68
- When a data import verifies "Transport emission factor units should never be missing"
58
+ When a data import verifies "Route inefficiency factor should be one or more"
69
59
  Then the verification should be successful
70
-
71
- Scenario: Failing to verify that transport emission factor units is never missing
60
+
61
+ Scenario: Failing to verify that route inefficiency factor >= 1
72
62
  Given a "CarrierMode" data import fetches results listed in "carrier_mode_bad"
73
- When a data import verifies "Transport emission factor units should never be missing"
63
+ When a data import verifies "Route inefficiency factor should be one or more"
74
64
  Then the verification should not be successful
@@ -3,9 +3,9 @@ Feature: Data import for FuelYear
3
3
  I want to import FuelYear data
4
4
  So that I can perform fuel year control-based calculations
5
5
 
6
- Scenario: Successfully verifying that fuel name and fuel common name are never missing
6
+ Scenario: Successfully verifying that fuel name is never missing
7
7
  Given a "FuelYear" data import fetches results listed in "fuel_year_good"
8
- When a data import verifies "Fuel name and fuel common name should never be missing"
8
+ When a data import verifies "Fuel name should never be missing"
9
9
  Then the verification should be successful
10
10
 
11
11
  Scenario: Successfully verifying that year is from 1990 to 2008
@@ -13,19 +13,24 @@ Feature: Data import for FuelYear
13
13
  When a data import verifies "Year should be from 1990 to 2008"
14
14
  Then the verification should be successful
15
15
 
16
- Scenario: Successfully verifying that carbon content, energy content, and CO2 emission factor are greater than zero
16
+ Scenario: Successfully verifying that carbon content and energy content are greater than zero
17
17
  Given a "FuelYear" data import fetches results listed in "fuel_year_good"
18
- When a data import verifies "Carbon content, energy content, and CO2 emission factor should be greater than zero"
18
+ When a data import verifies "Carbon content and energy content should be greater than zero"
19
19
  Then the verification should be successful
20
20
 
21
- Scenario: Successfully verifying that units are correct
21
+ Scenario: Successfully verifying that emission factors are zero or more
22
22
  Given a "FuelYear" data import fetches results listed in "fuel_year_good"
23
- When a data import verifies "Units should be correct"
23
+ When a data import verifies "Emission factors should be zero or more"
24
24
  Then the verification should be successful
25
25
 
26
- Scenario: Failing to verify that fuel name and fuel common name are never missing
26
+ # Scenario: Successfully verifying that units are correct
27
+ # Given a "FuelYear" data import fetches results listed in "fuel_year_good"
28
+ # When a data import verifies "Units should be correct"
29
+ # Then the verification should be successful
30
+ #
31
+ Scenario: Failing to verify that fuel name is never missing
27
32
  Given a "FuelYear" data import fetches results listed in "fuel_year_bad"
28
- When a data import verifies "Fuel name and fuel common name should never be missing"
33
+ When a data import verifies "Fuel name should never be missing"
29
34
  Then the verification should not be successful
30
35
 
31
36
  Scenario: Failing to verify that year is from 1990 to 2008
@@ -33,12 +38,17 @@ Feature: Data import for FuelYear
33
38
  When a data import verifies "Year should be from 1990 to 2008"
34
39
  Then the verification should not be successful
35
40
 
36
- Scenario: Failing to verify that carbon content, energy content, and CO2 emission factor are greater than zero
41
+ Scenario: Failing to verify that carbon content and energy content are greater than zero
37
42
  Given a "FuelYear" data import fetches results listed in "fuel_year_bad"
38
- When a data import verifies "Carbon content, energy content, and CO2 emission factor should be greater than zero"
43
+ When a data import verifies "Carbon content and energy content should be greater than zero"
39
44
  Then the verification should not be successful
40
45
 
41
- Scenario: Failing to verify that units are correct
46
+ Scenario: Failing to verify that emission factors are zero or more
42
47
  Given a "FuelYear" data import fetches results listed in "fuel_year_bad"
43
- When a data import verifies "Units should be correct"
48
+ When a data import verifies "Emission factors should be zero or more"
44
49
  Then the verification should not be successful
50
+
51
+ # Scenario: Failing to verify that units are correct
52
+ # Given a "FuelYear" data import fetches results listed in "fuel_year_bad"
53
+ # When a data import verifies "Units should be correct"
54
+ # Then the verification should not be successful
@@ -0,0 +1,3 @@
1
+ name,base_fuel_name,distance_key,ef_key,blend_portion
2
+ regular gasoline
3
+ b20,Distillate Fuel Oil No. 2,diesel,diesel,1.1
@@ -0,0 +1,3 @@
1
+ name,base_fuel_name,distance_key,ef_key,blend_portion
2
+ regular gasoline,Motor Gasoline,gasoline,gasoline
3
+ b20,Distillate Fuel Oil No. 2,diesel,diesel,0.2
@@ -0,0 +1,2 @@
1
+ name,year,age,total_travel_percent,annual_distance,vehicles,annual_distance_units,type_name,fuel_common_name,type_fuel_year_name
2
+ Passenger cars gasoline 2008 age 0,2009,31,1.1,-1,-1,miles
@@ -0,0 +1,2 @@
1
+ name,year,age,total_travel_percent,annual_distance,vehicles,annual_distance_units,type_name,fuel_common_name,type_fuel_year_name
2
+ Passenger cars gasoline 2008 age 0,2008,0,0.5,10000,100000,kilometres,Passenger cars,gasoline,Passenger cars gasoline 2008
@@ -1,2 +1,2 @@
1
- name,year,total_travel,fuel_consumption,ch4_emission_factor,n2o_emission_factor,hfc_emission_factor,total_travel_units,fuel_consumption_units,ch4_emission_factor_units,n2o_emission_factor_units,hfc_emission_factor_units,type_name,fuel_common_name
2
- Light-duty trucks diesel 2010,2010,0,-1,0,-1,-1,miles,gallons,grams_per_mile,grams_per_mile,co2e
1
+ name,year,total_travel,fuel_consumption,ch4_emission_factor,n2o_emission_factor,total_travel_units,fuel_consumption_units,ch4_emission_factor_units,n2o_emission_factor_units,type_name,fuel_common_name,type_year_name
2
+ Passenger cars gasoline 2009,2009,0,-1,0,-1,miles,gallons,grams_per_mile,grams_per_mile
@@ -1,3 +1,2 @@
1
- name,year,total_travel_percent,type_name,fuel_common_name,control_name
2
- Passenger cars gasoline 2008 epa tier 2,2008,0.9,Passenger cars,gasoline,epa tier 2
3
- Light-duty trucks diesel 2008 epa advanced,2009,0.1
1
+ name,year,total_travel_percent,type_name,fuel_common_name,type_fuel_control_name,type_fuel_year_name,control_name
2
+ Light-duty trucks diesel 2008 epa advanced,2009,0.1
@@ -1,3 +1,2 @@
1
- name,year,total_travel_percent,type_name,fuel_common_name,control_name
2
- Passenger cars gasoline 2008 epa tier 2,2008,0.9,Passenger cars,gasoline,epa tier 2
3
- Light-duty trucks diesel 2008 epa advanced,2008,-1,Light-duty trucks,diesel,advanced
1
+ name,year,total_travel_percent,type_name,fuel_common_name,type_fuel_control_name,type_fuel_year_name,control_name
2
+ Passenger cars gasoline 2008 epa tier 2,2008,0.9,Passenger cars,gasoline,Passenger cars gasoline epa tier 2,Passenger cars gasoline 2008,epa tier 2
@@ -1,2 +1,2 @@
1
- name,year,total_travel,fuel_consumption,ch4_emission_factor,n2o_emission_factor,hfc_emission_factor,total_travel_units,fuel_consumption_units,ch4_emission_factor_units,n2o_emission_factor_units,hfc_emission_factor_units,type_name,fuel_common_name
2
- Passenger cars gasoline 2000,2000,1000,500,0.01,0.001,0,kilometres,litres,kilograms_per_litre,kilograms_per_litre,kilograms_co2e_per_litre,Passenger cars,gasoline
1
+ name,year,total_travel,fuel_consumption,ch4_emission_factor,n2o_emission_factor,total_travel_units,fuel_consumption_units,ch4_emission_factor_units,n2o_emission_factor_units,type_name,fuel_common_name,type_year_name
2
+ Passenger cars gasoline 2008,2008,1000,500,0.1,0.01,kilometres,litres,kilograms_per_litre,kilograms_per_litre,Passenger cars,gasoline,Passenger cars 2008
@@ -1,2 +1,2 @@
1
- name,year,hfc_emissions,hfc_emissions_units,type_name
2
- Passenger cars 2008,2009,-1,pounds
1
+ name,year,hfc_emissions,hfc_emission_factor,hfc_emissions_units,hfc_emission_factor_units,type_name
2
+ Passenger cars 2008,2009,-1,-1,pounds,pounds_per_gallon
@@ -1,2 +1,2 @@
1
- name,year,hfc_emissions,hfc_emissions_units,type_name
2
- Passenger cars 2008,2008,1000,kilograms_co2e,Passenger cars
1
+ name,year,hfc_emissions,hfc_emission_factor,hfc_emissions_units,hfc_emission_factor_units,type_name
2
+ Passenger cars 2008,2008,1000,0,kilograms_co2e,kilograms_co2e_per_litre,Passenger cars
@@ -1,3 +1,2 @@
1
- name,package_volume,route_inefficiency_factor,transport_emission_factor,carrier_name,mode_name,include_in_fallbacks,transport_emission_factor_units
2
- FedEx ground,100,1.0,1.2,FedEx,ground,1,kilograms_per_kilogram_kilometre
3
- FedEx air,0,0.9,-2.0
1
+ name,package_volume,transport_emission_factor,route_inefficiency_factor,carrier_name,mode_name,transport_emission_factor_units
2
+ FedEx ground,0,-1,0.9
@@ -1,3 +1,2 @@
1
- name,carrier_name,mode_name,include_in_fallbacks,package_volume,route_inefficiency_factor,transport_emission_factor,transport_emission_factor_units
2
- FedEx ground,FedEx,ground,1,100,1.0,1.2,kilograms_per_kilogram_kilometre
3
- FedEx air,FedEx,air,0,1,2.0,4.0,kilograms_per_kilogram_kilometre
1
+ name,package_volume,transport_emission_factor,route_inefficiency_factor,carrier_name,mode_name,transport_emission_factor_units
2
+ FedEx ground,100,1.2,1.0,FedEx,ground,kilograms_per_kilogram_kilometre
@@ -1,2 +1,2 @@
1
- name,year,carbon_content,energy_content,co2_emission_factor,carbon_content_units,energy_content_units,co2_emission_factor_units,fuel_name,fuel_common_name
2
- Motor Gasoline 2008,2009,0,-1,0,foo,bar,pounds_per_gallon
1
+ name,year,energy_content,carbon_content,co2_emission_factor,co2_biogenic_emission_factor,energy_content_units,carbon_content_units,co2_emission_factor_units,co2_biogenic_emission_factor_units,fuel_name
2
+ Motor Gasoline 2008,2009,0,-1,-1,-2,foo,bar,foo,bar
@@ -1,2 +1,2 @@
1
- name,year,carbon_content,energy_content,co2_emission_factor,carbon_content_units,energy_content_units,co2_emission_factor_units,fuel_name,fuel_common_name
2
- Motor Gasoline 2008,2008,19.5,5.2,2.3,teragrams_per_quadrillion_btu,million_btu_per_barrel,kilograms_per_litre,Motor Gasoline,gasoline
1
+ name,year,energy_content,carbon_content,co2_emission_factor,co2_biogenic_emission_factor,energy_content_units,carbon_content_units,co2_emission_factor_units,co2_biogenic_emission_factor_units,fuel_name
2
+ Motor Gasoline 2008,2008,20,10,2,0,megajoules_per_litre,grams_per_megajoule,kilograms_per_litre,kilograms_per_litre,Motor Gasoline
data/lib/earth.rb CHANGED
@@ -83,7 +83,7 @@ module Earth
83
83
  end
84
84
 
85
85
  load_domains(domains, options)
86
- load_schemas if options[:apply_schemas]
86
+ load_schemas(options) if options[:apply_schemas]
87
87
  end
88
88
 
89
89
  def database_options
@@ -121,9 +121,9 @@ private
121
121
  end
122
122
  end
123
123
 
124
- def load_schemas
124
+ def load_schemas(options = {})
125
125
  force_fallback_table
126
- load_data_miner_schemas
126
+ load_data_miner_schemas(options)
127
127
  end
128
128
 
129
129
  # sabshere 9/17/10 this sucks. the falls_back_on gem sucks.
@@ -144,7 +144,7 @@ private
144
144
  end
145
145
  end
146
146
 
147
- def load_data_miner_schemas
147
+ def load_data_miner_schemas(options = {})
148
148
  models = Module.constants.select do |k|
149
149
  const = Object.const_get(k) if Object.const_defined?(k)
150
150
  if const.instance_of?(Class)
@@ -156,7 +156,7 @@ private
156
156
  end
157
157
  models.sort.each do |model|
158
158
  klass = Object.const_get(model)
159
- if klass.respond_to?(:execute_schema) and !klass.table_exists?
159
+ if klass.respond_to?(:execute_schema) and (!klass.table_exists? || options[:force_schema])
160
160
  klass.execute_schema
161
161
  end
162
162
  end
@@ -47,6 +47,7 @@ Airline.class_eval do
47
47
 
48
48
  conditional_relation = airlines[:iata_code].eq(segments[:airline_iata_code])
49
49
 
50
+ # sabshere 2/1/11 these need units
50
51
  update_all "seats = (#{FlightSegment.weighted_average_relation(:seats, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
51
52
  update_all "distance = (#{FlightSegment.weighted_average_relation(:distance, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
52
53
  update_all "load_factor = (#{FlightSegment.weighted_average_relation(:load_factor, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
@@ -20,7 +20,7 @@ Airport.class_eval do
20
20
  end
21
21
 
22
22
  import "the OpenFlights.org airports database",
23
- :url => 'http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/data/airports.dat',
23
+ :url => 'https://openflights.svn.sourceforge.net/svnroot/openflights/openflights/data/airports.dat',
24
24
  :headers => false,
25
25
  :select => lambda { |row| row[4].present? } do
26
26
  key 'iata_code', :field_number => 4
@@ -184,6 +184,7 @@ FlightSegment.class_eval do
184
184
  integer 'year'
185
185
  integer 'quarter'
186
186
  integer 'month'
187
+ date 'approximate_date'
187
188
  # integer 'bts_aircraft_group_code'
188
189
  # string 'configuration_id'
189
190
  # integer 'bts_aircraft_configuration_code'
@@ -315,6 +316,10 @@ FlightSegment.class_eval do
315
316
  update_all 'seats = total_seats / departures_performed', 'departures_performed > 0'
316
317
  end
317
318
 
319
+ process "Add a useful date field" do
320
+ update_all 'approximate_date = DATE(CONCAT_WS("-", year, month, "14"))'
321
+ end
322
+
318
323
  # FIXME TODO make this verification check actual aircraft codes in Aircraft
319
324
  verify 'All segments have an associated aircraft' do
320
325
  FlightSegment.where(:aircraft_bts_code => [nil, '']).first.nil?
@@ -1,3 +1,4 @@
1
+ require 'earth/automobile/automobile_fuel'
1
2
  require 'earth/automobile/automobile_fuel_type'
2
3
  require 'earth/automobile/automobile_make'
3
4
  require 'earth/automobile/automobile_make_fleet_year'
@@ -11,4 +12,5 @@ require 'earth/automobile/automobile_type_year'
11
12
  require 'earth/automobile/automobile_type_fuel_age'
12
13
  require 'earth/automobile/automobile_type_fuel_control'
13
14
  require 'earth/automobile/automobile_type_fuel_year'
15
+ require 'earth/automobile/automobile_type_fuel_year_age'
14
16
  require 'earth/automobile/automobile_type_fuel_year_control'
@@ -0,0 +1,93 @@
1
+ require 'earth/automobile/automobile_type_fuel_year_age'
2
+ require 'earth/automobile/automobile_type_fuel_year'
3
+ require 'earth/automobile/automobile_type_year'
4
+ require 'earth/fuel/greenhouse_gas'
5
+
6
+ class AutomobileFuel < ActiveRecord::Base
7
+ set_primary_key :name
8
+
9
+ has_many :type_fuel_year_ages, :class_name => 'AutomobileTypeFuelYearAge', :foreign_key => 'fuel_common_name', :primary_key => 'distance_key'
10
+ has_many :type_fuel_years, :class_name => 'AutomobileTypeFuelYear', :foreign_key => 'fuel_common_name', :primary_key => 'ef_key'
11
+ belongs_to :base_fuel, :class_name => 'Fuel', :foreign_key => 'base_fuel_name'
12
+ belongs_to :blend_fuel, :class_name => 'Fuel', :foreign_key => 'blend_fuel_name'
13
+
14
+ class << self
15
+ def fallback_blend_portion
16
+ latest_year = AutomobileTypeFuelYear.maximum('year')
17
+ gas_use = AutomobileTypeFuelYear.where(:year => latest_year, :fuel_common_name => 'gasoline').sum('fuel_consumption')
18
+ diesel_use = AutomobileTypeFuelYear.where(:year => latest_year, :fuel_common_name => 'diesel').sum('fuel_consumption')
19
+ diesel_use / (gas_use + diesel_use)
20
+ end
21
+ end
22
+
23
+ falls_back_on :name => 'fallback',
24
+ :base_fuel_name => 'Motor Gasoline',
25
+ :blend_fuel_name => 'Distillate Fuel Oil No. 2',
26
+ :blend_portion => lambda { AutomobileFuel.fallback_blend_portion },
27
+ :distance_key => 'fallback',
28
+ :ef_key => 'fallback'
29
+
30
+ data_miner do
31
+ tap "Brighter Planet's sanitized automobile fuel type data", Earth.taps_server
32
+
33
+ process "pull dependencies" do
34
+ run_data_miner_on_belongs_to_associations
35
+ end
36
+ end
37
+
38
+ # FIXME TODO should I run data miner on has_many associations?
39
+ # AutomobileTypeFuelYearAge
40
+ # => AutomobileTypeFuelYearControl
41
+ # => AutomobileTypeFuelControl
42
+ # AutomobileTypeYear
43
+
44
+ # FIXME TODO should I run data miner on other classes we need?
45
+ # Fuel
46
+ # => FuelYear
47
+ # GreenhouseGas
48
+
49
+ def annual_distance # returns km
50
+ scope = type_fuel_year_ages.any? ? type_fuel_year_ages : AutomobileTypeFuelYearAge
51
+ scope.where(:year => scope.maximum('year')).weighted_average(:annual_distance, :weighted_by => :vehicles)
52
+ end
53
+
54
+ def co2_emission_factor # returns kg co2 / litre
55
+ if blend_fuel.present?
56
+ (base_fuel.co2_emission_factor * (1 - blend_portion)) + (blend_fuel.co2_emission_factor * blend_portion)
57
+ else
58
+ base_fuel.co2_emission_factor
59
+ end
60
+ end
61
+
62
+ def co2_biogenic_emission_factor # returns kg co2 / litre
63
+ if blend_fuel.present?
64
+ (base_fuel.co2_biogenic_emission_factor * (1 - blend_portion)) + (blend_fuel.co2_biogenic_emission_factor * blend_portion)
65
+ else
66
+ base_fuel.co2_biogenic_emission_factor
67
+ end
68
+ end
69
+
70
+ def latest_type_fuel_years
71
+ scope = type_fuel_years.any? ? type_fuel_years : AutomobileTypeFuelYear
72
+ scope.where(:year => scope.maximum('year'))
73
+ end
74
+
75
+ def ch4_emission_factor # returns kg co2e / litre
76
+ latest_type_fuel_years.weighted_average(:ch4_emission_factor, :weighted_by => :total_travel) * GreenhouseGas[:ch4].global_warming_potential
77
+ end
78
+
79
+ def n2o_emission_factor # returns kg co2e / litre
80
+ latest_type_fuel_years.weighted_average(:n2o_emission_factor, :weighted_by => :total_travel) * GreenhouseGas[:n2o].global_warming_potential
81
+ end
82
+
83
+ def hfc_emission_factor # returns kg co2e / litre (hfc emission factor is already in co2e)
84
+ latest_type_fuel_years.map do |type_fuel_year|
85
+ type_fuel_year.total_travel * type_fuel_year.type_year.hfc_emission_factor
86
+ end.sum / latest_type_fuel_years.sum('total_travel')
87
+ end
88
+
89
+ CODES = {
90
+ :electricity => 'El',
91
+ :diesel => 'D'
92
+ }
93
+ end
@@ -0,0 +1,60 @@
1
+ AutomobileFuel.class_eval do
2
+ data_miner do
3
+ schema Earth.database_options do
4
+ string 'name'
5
+ string 'code'
6
+ string 'base_fuel_name'
7
+ string 'blend_fuel_name'
8
+ float 'blend_portion' # the portion of the blend that is the blend fuel
9
+ string 'distance_key' # used to look up annual distance from AutomobileTypeFuelYear
10
+ string 'ef_key' # used to look up ch4 n2o and hfc emission factors from AutomobileTypeFuelYear
11
+ end
12
+
13
+ import "a list of pure automobile fuels",
14
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdE9xTEdueFM2R0diNTgxUlk1QXFSb2c&output=csv' do
15
+ key 'name'
16
+ store 'code'
17
+ store 'base_fuel_name'
18
+ store 'distance_key'
19
+ store 'ef_key'
20
+ end
21
+
22
+ import "a list of blended automobile fuels",
23
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdEswNGIxM0U4U0N1UUppdWw2ejJEX0E&output=csv' do
24
+ key 'name'
25
+ store 'code'
26
+ store 'base_fuel_name'
27
+ store 'blend_fuel_name'
28
+ store 'blend_portion'
29
+ store 'distance_key'
30
+ store 'ef_key'
31
+ end
32
+
33
+ # FIXME TODO verify code somehow
34
+
35
+ %w{ base_fuel_name distance_key ef_key }.each do |attribute|
36
+ verify "#{attribute.humanize} should never be missing" do
37
+ AutomobileFuel.all.each do |fuel|
38
+ value = fuel.send(:"#{attribute}")
39
+ unless value.present?
40
+ raise "Missing #{attribute.humanize.downcase} for AutomobileFuel #{fuel.name}"
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ # FIXME TODO verify that base_fuel_name and blend_fuel_name are found in Fuel if present
47
+ # FIXME TODO verify that distance_key is found in AutomobileTypeFuelYearAge
48
+ # FIXME TODO verify that ef_key is found in AutomobileTypeFuelYear
49
+
50
+ verify "Blend portion should be from 0 to 1 if present" do
51
+ AutomobileFuel.all.each do |fuel|
52
+ if fuel.blend_portion.present?
53
+ unless fuel.blend_portion >=0 and fuel.blend_portion <= 1
54
+ raise "Invalid blend portion for AutomobileFuel #{fuel.name}: #{fuel.blend_portion} (should be from 0 to 1)"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end