earth 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/bin/earth_tester.rb +1 -0
  2. data/data/automobile/annual_emission_controls.csv +1 -1
  3. data/data/automobile/annual_fuel_consumption.csv +1 -1
  4. data/data/automobile/auto_fuel_data.csv +1 -1
  5. data/data/automobile/emission_control_techs.csv +1 -1
  6. data/data/automobile/trip_distances.csv +5 -0
  7. data/data/locality/country_flight_data.csv +18 -0
  8. data/data/locality/egrid_relationships.csv +27 -0
  9. data/data/locality/misc_zip_data.csv +1 -0
  10. data/data/locality/us_auto_data.csv +2 -0
  11. data/data/locality/wri_hdd_cdd_data.csv +174 -0
  12. data/data/locality/zip_climate_divisions.csv +41359 -0
  13. data/errata/country/wri_errata.csv +24 -24
  14. data/lib/earth/automobile/automobile_activity_year/data_miner.rb +4 -3
  15. data/lib/earth/automobile/automobile_activity_year_type/data_miner.rb +4 -3
  16. data/lib/earth/automobile/automobile_activity_year_type_fuel.rb +1 -1
  17. data/lib/earth/automobile/automobile_activity_year_type_fuel/data_miner.rb +4 -4
  18. data/lib/earth/automobile/automobile_fuel.rb +5 -5
  19. data/lib/earth/automobile/automobile_fuel/data_miner.rb +18 -13
  20. data/lib/earth/automobile/automobile_make/data_miner.rb +4 -3
  21. data/lib/earth/automobile/automobile_make_model/data_miner.rb +6 -7
  22. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +1 -2
  23. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +5 -10
  24. data/lib/earth/automobile/automobile_type_fuel.rb +2 -2
  25. data/lib/earth/automobile/automobile_type_fuel/data_miner.rb +8 -7
  26. data/lib/earth/automobile/automobile_type_fuel_control.rb +1 -1
  27. data/lib/earth/automobile/automobile_type_fuel_control/data_miner.rb +1 -1
  28. data/lib/earth/automobile/automobile_type_fuel_year.rb +5 -5
  29. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +7 -6
  30. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +4 -4
  31. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +2 -2
  32. data/lib/earth/conversions_ext.rb +2 -0
  33. data/lib/earth/industry/industry.rb +0 -2
  34. data/lib/earth/industry/industry/data_miner.rb +5 -2
  35. data/lib/earth/locality/census_division.rb +2 -1
  36. data/lib/earth/locality/country.rb +3 -7
  37. data/lib/earth/locality/country/data_miner.rb +54 -75
  38. data/lib/earth/locality/egrid_country.rb +9 -3
  39. data/lib/earth/locality/egrid_country/data_miner.rb +14 -8
  40. data/lib/earth/locality/egrid_region.rb +15 -6
  41. data/lib/earth/locality/egrid_region/data_miner.rb +14 -18
  42. data/lib/earth/locality/egrid_subregion.rb +21 -19
  43. data/lib/earth/locality/egrid_subregion/data_miner.rb +27 -47
  44. data/lib/earth/locality/zip_code.rb +5 -4
  45. data/lib/earth/locality/zip_code/data_miner.rb +25 -14
  46. data/lib/earth/rail/rail_company/data_miner.rb +1 -1
  47. data/lib/earth/version.rb +1 -1
  48. data/spec/earth/automobile/automobile_activity_year_type_fuel_spec.rb +1 -1
  49. data/spec/earth/automobile/automobile_fuel_spec.rb +1 -1
  50. data/spec/earth/automobile/automobile_type_fuel_spec.rb +1 -1
  51. data/spec/earth/automobile/automobile_type_fuel_year_control_spec.rb +6 -6
  52. data/spec/earth/automobile/automobile_type_fuel_year_spec.rb +6 -6
  53. data/spec/earth/industry/industry_spec.rb +1 -3
  54. data/spec/earth/locality/country_spec.rb +67 -34
  55. data/spec/earth/locality/egrid_country_spec.rb +26 -0
  56. data/spec/earth/locality/egrid_region_spec.rb +27 -0
  57. data/spec/earth/locality/egrid_subregion_spec.rb +49 -0
  58. data/spec/earth/locality/zip_code_spec.rb +13 -24
  59. metadata +17 -4
@@ -5,11 +5,11 @@ class AutomobileTypeFuelYearControl < ActiveRecord::Base
5
5
  belongs_to :type_fuel_control, :foreign_key => :type_fuel_control_name, :class_name => 'AutomobileTypeFuelControl'
6
6
 
7
7
  # Used by AutomobileTypeFuelYear
8
- def self.find_all_by_type_name_and_fuel_common_name_and_closest_year(type_name, fuel_common_name, year)
8
+ def self.find_all_by_type_name_and_fuel_group_and_closest_year(type_name, fuel_group, year)
9
9
  if year > maximum(:year)
10
- where(:type_name => type_name, :fuel_common_name => fuel_common_name, :year => maximum(:year))
10
+ where(:type_name => type_name, :fuel_group => fuel_group, :year => maximum(:year))
11
11
  else
12
- where(:type_name => type_name, :fuel_common_name => fuel_common_name, :year => [year, minimum(:year)].max)
12
+ where(:type_name => type_name, :fuel_group => fuel_group, :year => [year, minimum(:year)].max)
13
13
  end
14
14
  end
15
15
 
@@ -26,7 +26,7 @@ class AutomobileTypeFuelYearControl < ActiveRecord::Base
26
26
 
27
27
  col :name
28
28
  col :type_name
29
- col :fuel_common_name
29
+ col :fuel_group
30
30
  col :year, :type => :integer
31
31
  col :control_name
32
32
  col :type_fuel_control_name
@@ -8,10 +8,10 @@ AutomobileTypeFuelYearControl.class_eval do
8
8
  :url => "file://#{Earth::DATA_DIR}/automobile/annual_emission_controls.csv" do
9
9
  key 'name'
10
10
  store 'type_name'
11
- store 'fuel_common_name'
11
+ store 'fuel_group'
12
12
  store 'year'
13
13
  store 'control_name'
14
- store 'type_fuel_control_name', :synthesize => proc { |row| [row['type_name'], row['fuel_common_name'], row['control_name']].join(' ') }
14
+ store 'type_fuel_control_name', :synthesize => proc { |row| [row['type_name'], row['fuel_group'], row['control_name']].join(' ') }
15
15
  store 'total_travel_percent'
16
16
  end
17
17
  end
@@ -59,6 +59,8 @@ Conversions.register :megajoules_per_room_night, :kbtus_per_room_nigh
59
59
  Conversions.register :million_btu_per_barrel, :megajoules_per_litre, (1_000_000.btus.to(:megajoules) / 1.barrels.to(:litres)) # Fuel
60
60
  Conversions.register :btus_per_cubic_foot, :megajoules_per_cubic_metre, (1.btus.to(:megajoules) / 1.cubic_feet.to(:cubic_metres)) # Fuel
61
61
  Conversions.register :teragrams_per_quadrillion_btu, :grams_per_megajoule, (1_000_000_000_000 / 1_000_000_000_000_000.btus.to(:megajoules)) # Fuel
62
+ Conversions.register :kilometres_per_hour, :miles_per_hour, 1.kilometres.to(:miles)
63
+
62
64
 
63
65
  # Odd units for EPA fuel economy guide
64
66
  # Conversions.register :epa_gallon_gasoline_equivalents, :kilowatt_hours, 33.705
@@ -1,5 +1,3 @@
1
- require 'earth/locality'
2
-
3
1
  # TODO replace this with NAICS 2002?
4
2
  class Industry < ActiveRecord::Base
5
3
  self.primary_key = "naics_code"
@@ -1,8 +1,11 @@
1
- require 'earth/locality/data_miner'
2
1
  Industry.class_eval do
3
2
  data_miner do
3
+ process "Start from scratch" do
4
+ delete_all
5
+ end
6
+
4
7
  import "the U.S. Census 2002 NAICS code list",
5
- :url => 'http://www.census.gov/epcd/naics02/naicod02.txt',
8
+ :url => 'http://www.census.gov/eos/www/naics/reference_files_tools/2002/naics_2_6_02.txt',
6
9
  :skip => 4,
7
10
  :headers => false,
8
11
  :delimiter => ' ' do
@@ -9,7 +9,8 @@ class CensusDivision < ActiveRecord::Base
9
9
  has_many :residential_energy_consumption_survey_responses, :foreign_key => 'census_division_number'
10
10
 
11
11
  # https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdFJ3U3VaSG1oQW1yclY3M3FsRzRDNFE&hl=en&output=html
12
- falls_back_on :meeting_building_natural_gas_intensity => 0.0004353615 * 100.cubic_feet.to(:cubic_metres) / 1.square_feet.to(:square_metres),
12
+ falls_back_on :name => 'fallback',
13
+ :meeting_building_natural_gas_intensity => 0.0004353615 * 100.cubic_feet.to(:cubic_metres) / 1.square_feet.to(:square_metres),
13
14
  :meeting_building_fuel_oil_intensity => 0.0000925593.gallons.to(:litres) / 1.square_feet.to(:square_metres),
14
15
  :meeting_building_electricity_intensity => 0.0084323684 / 1.square_feet.to(:square_metres),
15
16
  :meeting_building_district_heat_intensity => 0.0004776370.kbtus.to(:megajoules) / 1.square_feet.to(:square_metres)
@@ -1,4 +1,5 @@
1
1
  require 'earth/automobile'
2
+ require 'earth/fuel'
2
3
  require 'earth/hospitality'
3
4
  require 'earth/rail'
4
5
 
@@ -46,10 +47,6 @@ class Country < ActiveRecord::Base
46
47
  find_by_iso_3166_code('US')
47
48
  end
48
49
 
49
- def united_states?
50
- iso_3166_code == 'US'
51
- end
52
-
53
50
  col :iso_3166_code # alpha-2 2-letter like GB
54
51
  col :iso_3166_numeric_code, :type => :integer # numeric like 826; aka UN M49 code
55
52
  col :iso_3166_alpha_3_code # 3-letter like GBR
@@ -91,8 +88,7 @@ class Country < ActiveRecord::Base
91
88
  col :rail_trip_diesel_intensity_units
92
89
  col :rail_trip_co2_emission_factor, :type => :float
93
90
  col :rail_trip_co2_emission_factor_units
94
-
95
- warn_if_nulls_except /heating/, /cooling/
96
-
91
+
97
92
  warn_unless_size 249
93
+ warn_if_nulls_except /heating/, /cooling/
98
94
  end
@@ -1,4 +1,5 @@
1
1
  require 'earth/automobile/data_miner'
2
+ require 'earth/fuel/data_miner'
2
3
  require 'earth/hospitality/data_miner'
3
4
  require 'earth/rail/data_miner'
4
5
 
@@ -19,7 +20,7 @@ Country.class_eval do
19
20
  end
20
21
 
21
22
  import "heating and cooling degree day data from WRI CAIT",
22
- :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdDN4MkRTSWtWRjdfazhRdWllTkVSMkE&output=csv',
23
+ :url => "file://#{Earth::DATA_DIR}/locality/wri_hdd_cdd_data.csv",
23
24
  :select => proc { |record| record['country'] != 'European Union (27)' },
24
25
  :errata => { :url => "file://#{Earth::ERRATA_DIR}/country/wri_errata.csv" } do
25
26
  key 'name', :field_name => 'country'
@@ -28,61 +29,38 @@ Country.class_eval do
28
29
  end
29
30
 
30
31
  process "set Montenegro's heating and cooling degree days to the same as Serbia's" do
31
- montenegro = Country.find 'ME'
32
32
  serbia = Country.find 'RS'
33
- montenegro.heating_degree_days = serbia.heating_degree_days
34
- montenegro.heating_degree_days_units = serbia.heating_degree_days_units
35
- montenegro.cooling_degree_days = serbia.cooling_degree_days
36
- montenegro.cooling_degree_days_units = serbia.cooling_degree_days_units
37
- montenegro.save!
33
+ Country.find('ME').update_attributes!(
34
+ :heating_degree_days => serbia.heating_degree_days,
35
+ :heating_degree_days_units => serbia.heating_degree_days_units,
36
+ :cooling_degree_days => serbia.cooling_degree_days,
37
+ :cooling_degree_days_units => serbia.cooling_degree_days_units
38
+ )
38
39
  end
39
40
 
40
41
  # AUTOMOBILE
41
42
  import "automobile-related data for the US",
42
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdDdZRm1tNjY0c2dYNG00bXJ3TXRqUVE&gid=0&output=csv' do
43
+ :url => "file://#{Earth::DATA_DIR}/locality/us_auto_data.csv" do
43
44
  key 'iso_3166_code'
44
45
  store 'automobile_urbanity'
45
- store 'automobile_city_speed', :units_field_name => 'automobile_city_speed_units'
46
- store 'automobile_highway_speed', :units_field_name => 'automobile_highway_speed_units'
47
- store 'automobile_trip_distance', :units_field_name => 'automobile_trip_distance_units'
46
+ store 'automobile_city_speed', :from_units => :miles_per_hour, :to_units => :kilometres_per_hour
47
+ store 'automobile_highway_speed', :from_units => :miles_per_hour, :to_units => :kilometres_per_hour
48
+ store 'automobile_trip_distance', :from_units => :miles, :to_units => :kilometres
48
49
  end
49
50
 
50
- process "Ensure AutomobileTypeFuelYear is populated" do
51
- AutomobileTypeFuelYear.run_data_miner!
51
+ process "Ensure AutomobileActivityYearTypeFuel is populated" do
52
+ AutomobileActivityYearTypeFuel.run_data_miner!
52
53
  end
53
54
 
54
- process "Derive US average automobile fuel efficiency from AutomobileTypeFuelYear" do
55
- fuel_years = AutomobileTypeFuelYear.where(:year => AutomobileTypeFuelYear.maximum(:year))
55
+ process "Derive US average automobile fuel efficiency from AutomobileActivityYearTypeFuel" do
56
+ max_year = AutomobileActivityYearTypeFuel.maximum(:activity_year)
57
+ fuel_years = AutomobileActivityYearTypeFuel.where("activity_year = #{max_year} AND fuel_consumption IS NOT NULL")
56
58
  where(:iso_3166_code => 'US').update_all(
57
- :automobile_fuel_efficiency => (fuel_years.sum(:total_travel).to_f / fuel_years.sum(:fuel_consumption)),
58
- :automobile_fuel_efficiency_units => (fuel_years.first.total_travel_units + '_per_' + fuel_years.first.fuel_consumption_units.singularize)
59
+ :automobile_fuel_efficiency => (fuel_years.sum(:distance).to_f / fuel_years.sum(:fuel_consumption)),
60
+ :automobile_fuel_efficiency_units => (fuel_years.first.distance_units + '_per_' + fuel_years.first.fuel_consumption_units.singularize)
59
61
  )
60
62
  end
61
63
 
62
- process "Convert automobile city speed from miles per hour to kilometres per hour" do
63
- conversion_factor = 1.miles.to(:kilometres)
64
- where(:automobile_city_speed_units => 'miles_per_hour').update_all(%{
65
- automobile_city_speed = 1.0 * automobile_city_speed * #{conversion_factor},
66
- automobile_city_speed_units = 'kilometres_per_hour'
67
- })
68
- end
69
-
70
- process "Convert automobile highway speed from miles per hour to kilometres per hour" do
71
- conversion_factor = 1.miles.to(:kilometres)
72
- where(:automobile_highway_speed_units => 'miles_per_hour').update_all(%{
73
- automobile_highway_speed = 1.0 * automobile_highway_speed * #{conversion_factor},
74
- automobile_highway_speed_units = 'kilometres_per_hour'
75
- })
76
- end
77
-
78
- process "Convert automobile trip distance from miles to kilometres" do
79
- conversion_factor = 1.miles.to(:kilometres)
80
- where(:automobile_trip_distance_units => 'miles').update_all(%{
81
- automobile_trip_distance = 1.0 * automobile_trip_distance * #{conversion_factor},
82
- automobile_trip_distance_units = 'kilometres'
83
- })
84
- end
85
-
86
64
  # ELECTRICITY
87
65
  process "Ensure GreehouseGas is populated" do
88
66
  GreenhouseGas.run_data_miner!
@@ -106,25 +84,23 @@ Country.class_eval do
106
84
  end
107
85
 
108
86
  process "Derive average US electricity emission factor and loss factor from eGRID" do
109
- us = united_states
110
- us.electricity_emission_factor = EgridSubregion.fallback.electricity_emission_factor
111
- us.electricity_emission_factor_units = EgridSubregion.fallback.electricity_emission_factor_units
112
- us.electricity_loss_factor = EgridRegion.fallback.loss_factor
113
- us.save!
87
+ united_states.update_attributes!(
88
+ :electricity_emission_factor => EgridSubregion.fallback.electricity_emission_factor,
89
+ :electricity_emission_factor_units => EgridSubregion.fallback.electricity_emission_factor_units,
90
+ :electricity_loss_factor => EgridRegion.fallback.loss_factor
91
+ )
114
92
  end
115
93
 
116
94
  # FLIGHT
117
95
  import "country-specific flight route inefficiency factors derived from Kettunen et al. (2005)",
118
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdEJoRVBZaGhnUmlhX240VXE3X0F3WkE&output=csv' do
96
+ :url => "file://#{Earth::DATA_DIR}/locality/country_flight_data.csv" do
119
97
  key 'iso_3166_code'
120
98
  store 'flight_route_inefficiency_factor'
121
99
  end
122
100
 
123
101
  # HOSPITALITY
124
102
  process "Define US average lodging occupancy rate" do
125
- country = united_states
126
- country.lodging_occupancy_rate = 0.601 # per http://www.pwc.com/us/en/press-releases/2012/pwc-us-lodging-industry-forecast.jhtml
127
- country.save!
103
+ united_states.update_attributes! :lodging_occupancy_rate => 0.601 # per http://www.pwc.com/us/en/press-releases/2012/pwc-us-lodging-industry-forecast.jhtml
128
104
  end
129
105
 
130
106
  process "Ensure CountryLodgingClass is populated" do
@@ -133,16 +109,17 @@ Country.class_eval do
133
109
 
134
110
  process "Derive average hotel characteristics from CountryLodgingClass" do
135
111
  find_each do |country|
136
- if country.lodging_classes.any?
137
- country.lodging_natural_gas_intensity = country.lodging_classes.weighted_average(:natural_gas_intensity)
138
- country.lodging_natural_gas_intensity_units = 'cubic_metres_per_occupied_room_night' # FIXME TODO derive this
139
- country.lodging_fuel_oil_intensity = country.lodging_classes.weighted_average(:fuel_oil_intensity)
140
- country.lodging_fuel_oil_intensity_units = 'gallons_per_occupied_room_night' # FIXME TODO derive this
141
- country.lodging_electricity_intensity = country.lodging_classes.weighted_average(:electricity_intensity)
142
- country.lodging_electricity_intensity_units = 'kilowatt_hours_per_occupied_room_night' # FIXME TODO derive this
143
- country.lodging_district_heat_intensity = country.lodging_classes.weighted_average(:district_heat_intensity)
144
- country.lodging_district_heat_intensity_units = 'megajoules_per_occupied_room_night' # FIXME TODO derive this
145
- country.save!
112
+ if (lodging_classes = country.lodging_classes).any?
113
+ country.update_attributes!(
114
+ :lodging_natural_gas_intensity => lodging_classes.weighted_average(:natural_gas_intensity),
115
+ :lodging_fuel_oil_intensity => lodging_classes.weighted_average(:fuel_oil_intensity),
116
+ :lodging_electricity_intensity => lodging_classes.weighted_average(:electricity_intensity),
117
+ :lodging_district_heat_intensity => lodging_classes.weighted_average(:district_heat_intensity),
118
+ :lodging_natural_gas_intensity_units => 'cubic_metres_per_occupied_room_night', # FIXME TODO derive this
119
+ :lodging_fuel_oil_intensity_units => 'litres_per_occupied_room_night', # FIXME TODO derive this
120
+ :lodging_electricity_intensity_units => 'kilowatt_hours_per_occupied_room_night', # FIXME TODO derive this
121
+ :lodging_district_heat_intensity_units => 'megajoules_per_occupied_room_night' # FIXME TODO derive this
122
+ )
146
123
  end
147
124
  end
148
125
  end
@@ -155,13 +132,14 @@ Country.class_eval do
155
132
 
156
133
  process "Calculate rail passengers, trip distance, and speed from RailCompany" do
157
134
  find_each do |country|
158
- if country.rail_companies.any?
159
- country.rail_passengers = country.rail_companies.sum(:passengers)
160
- country.rail_trip_distance = country.rail_companies.weighted_average(:trip_distance, :weighted_by => :passengers)
161
- country.rail_trip_distance_units = 'kilometres' if country.rail_trip_distance.present?
162
- country.rail_speed = country.rail_companies.weighted_average(:speed, :weighted_by => :passengers)
163
- country.rail_speed_units = 'kilometres_per_hour' if country.rail_speed.present?
164
- country.save!
135
+ if (rail_companies = country.rail_companies).any?
136
+ country.update_attributes!(
137
+ :rail_passengers => rail_companies.sum(:passengers),
138
+ :rail_trip_distance => rail_companies.weighted_average(:trip_distance, :weighted_by => :passengers),
139
+ :rail_speed => rail_companies.weighted_average(:speed, :weighted_by => :passengers),
140
+ :rail_trip_distance_units => ('kilometres' if country.rail_trip_distance.present?),
141
+ :rail_speed_units => ('kilometres_per_hour' if country.rail_speed.present?)
142
+ )
165
143
  end
166
144
  end
167
145
  end
@@ -190,14 +168,15 @@ Country.class_eval do
190
168
  end
191
169
 
192
170
  process "Derive US rail fuel and emission data from RailCompany" do
193
- country = united_states
194
- country.rail_trip_electricity_intensity = country.rail_companies.weighted_average(:electricity_intensity, :weighted_by => :passengers)
195
- country.rail_trip_electricity_intensity_units = 'kilowatt_hours_per_passenger_kilometre'
196
- country.rail_trip_diesel_intensity = country.rail_companies.weighted_average(:diesel_intensity, :weighted_by => :passengers)
197
- country.rail_trip_diesel_intensity_units = 'litres_per_passenger_kilometre'
198
- country.rail_trip_co2_emission_factor = country.rail_companies.weighted_average(:co2_emission_factor, :weighted_by => :passengers)
199
- country.rail_trip_co2_emission_factor_units = 'kilograms_per_passenger_kilometre'
200
- country.save!
171
+ rail_companies = united_states.rail_companies
172
+ united_states.update_attributes!(
173
+ :rail_trip_electricity_intensity => rail_companies.weighted_average(:electricity_intensity, :weighted_by => :passengers),
174
+ :rail_trip_diesel_intensity => rail_companies.weighted_average(:diesel_intensity, :weighted_by => :passengers),
175
+ :rail_trip_co2_emission_factor => rail_companies.weighted_average(:co2_emission_factor, :weighted_by => :passengers),
176
+ :rail_trip_electricity_intensity_units => 'kilowatt_hours_per_passenger_kilometre',
177
+ :rail_trip_diesel_intensity_units => 'litres_per_passenger_kilometre',
178
+ :rail_trip_co2_emission_factor_units => 'kilograms_per_passenger_kilometre'
179
+ )
201
180
  end
202
181
  end
203
182
  end
@@ -3,15 +3,21 @@ class EgridCountry < ActiveRecord::Base
3
3
 
4
4
  class << self
5
5
  def us
6
- find_by_name 'US'
6
+ find_by_name 'U.S.'
7
7
  end
8
8
  end
9
9
 
10
10
  col :name
11
11
  col :generation, :type => :float
12
12
  col :generation_units
13
- col :imports, :type => :float
14
- col :imports_units
13
+ col :foreign_interchange, :type => :float
14
+ col :foreign_interchange_units
15
+ col :domestic_interchange, :type => :float
16
+ col :domestic_interchange_units
15
17
  col :consumption, :type => :float
16
18
  col :consumption_units
19
+ col :loss_factor, :type => :float
20
+
21
+ warn_unless_size 1
22
+ warn_if_any_nulls
17
23
  end
@@ -1,13 +1,19 @@
1
1
  EgridCountry.class_eval do
2
2
  data_miner do
3
- import "the US average grid loss factor derived eGRID 2007 data",
4
- :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2010V1_1_STIE_USGC.xls',
5
- :sheet => 'USGC',
6
- :skip => 5 do
7
- key 'name', :static => 'US'
8
- store 'generation', :field_name => 'USTNGN07', :units => :gigawatt_hours
9
- store 'imports', :field_name => 'USTNFI07', :units => :gigawatt_hours
10
- store 'consumption', :field_name => 'USTCON07', :units => :gigawatt_hours
3
+ import "eGRID 2012 national-level data",
4
+ :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2012V1_0_year09_DATA.xls',
5
+ :sheet => 'GGL09',
6
+ :skip => 4,
7
+ :select => proc { |row| row['REGION'] == 'U.S.' } do
8
+ key 'name', :field_name => 'REGION'
9
+ store 'generation', :field_name => 'GENERAT', :units => :megawatt_hours
10
+ store 'foreign_interchange', :field_name => 'FRGNINTC', :units => :megawatt_hours
11
+ store 'domestic_interchange', :field_name => 'INTRCHNG', :units => :megawatt_hours
12
+ store 'consumption', :field_name => 'CONSUMP', :units => :megawatt_hours
13
+ end
14
+
15
+ process "Calculate loss factor" do
16
+ update_all "loss_factor = (1.0 * generation + foreign_interchange + domestic_interchange - consumption) / generation"
11
17
  end
12
18
  end
13
19
  end
@@ -1,17 +1,26 @@
1
+ require 'earth/locality/egrid_country'
2
+
1
3
  class EgridRegion < ActiveRecord::Base
2
4
  self.primary_key = "name"
3
5
 
4
- # make EgridCountry a parent so that it automatically gets data_mined (need it for fallback calculation)
6
+ # EgridCountry must be a parent so that it automatically gets data_mined (needed for fallback calculation)
5
7
  belongs_to :egrid_country, :foreign_key => 'country_name'
6
8
  has_many :egrid_subregions, :foreign_key => 'egrid_region_name'
7
9
 
8
10
  falls_back_on :name => 'fallback',
9
- :loss_factor => lambda { (EgridCountry.us.generation + EgridCountry.us.imports - EgridCountry.us.consumption) / EgridCountry.us.generation }
11
+ :loss_factor => lambda { EgridCountry.us.loss_factor }
10
12
 
11
13
  col :name
12
- col :country_name
14
+ col :generation, :type => :float
15
+ col :generation_units
16
+ col :foreign_interchange, :type => :float
17
+ col :foreign_interchange_units
18
+ col :domestic_interchange, :type => :float
19
+ col :domestic_interchange_units
20
+ col :consumption, :type => :float
21
+ col :consumption_units
13
22
  col :loss_factor, :type => :float
14
-
15
- warn_unless_size 6
16
- warn_if_blanks_except :country_name, :conditions => { :name => 'US' }
23
+
24
+ warn_unless_size 5
25
+ warn_if_any_nulls
17
26
  end
@@ -1,27 +1,23 @@
1
1
  EgridRegion.class_eval do
2
2
  data_miner do
3
- import "eGRID 2010 regions and loss factors",
4
- :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2010V1_1_STIE_USGC.xls',
5
- :sheet => 'STIE07',
6
- :skip => 4,
7
- :select => proc { |row| row['eGRID2010 year 2007 file state sequence number'].to_i.between?(1, 51) } do
8
- key 'name', :field_name => 'Grid region (E=Eastern grid, W=Western grid, AK=Alaska, HI=Hawaii, TX=Texas)'
9
- store 'country_name', :static => 'US'
10
- store 'loss_factor', :field_name => 'Year 2007 grid gross loss factor'
3
+ process "Start from scratch" do
4
+ delete_all
11
5
  end
12
6
 
13
- # DEPRECATED but don't remove until confirmed that all emitters use EgridRegion.fallback rather than EgridRegion.find_by_abbreviation 'US'
14
- # (ElectricityUse and Meeting)
15
- process "Ensure EgridCountry is populated" do
16
- EgridCountry.run_data_miner!
7
+ import "eGRID 2012 region data",
8
+ :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2012V1_0_year09_DATA.xls',
9
+ :sheet => 'GGL09',
10
+ :skip => 4,
11
+ :select => proc { |row| row['REGION'] != 'U.S.' } do
12
+ key 'name', :field_name => 'REGION'
13
+ store 'generation', :field_name => 'GENERAT', :units => :megawatt_hours
14
+ store 'foreign_interchange', :field_name => 'FRGNINTC', :units => :megawatt_hours
15
+ store 'domestic_interchange', :field_name => 'INTRCHNG', :units => :megawatt_hours
16
+ store 'consumption', :field_name => 'CONSUMP', :units => :megawatt_hours
17
17
  end
18
18
 
19
- # DEPRECATED but don't remove until confirmed that all emitters use EgridRegion.fallback rather than EgridRegion.find_by_abbreviation 'US'
20
- # (ElectricityUse and Meeting)
21
- process "Calculate national averages" do
22
- us_average = find_or_create_by_name 'US'
23
- us_average.loss_factor = (EgridCountry.us.generation + EgridCountry.us.imports - EgridCountry.us.consumption) / EgridCountry.us.generation
24
- us_average.save!
19
+ process "Calculate loss factor" do
20
+ update_all "loss_factor = (1.0 * generation + foreign_interchange + domestic_interchange - consumption) / generation"
25
21
  end
26
22
  end
27
23
  end
@@ -1,4 +1,5 @@
1
- require 'earth/fuel'
1
+ require 'earth/locality/egrid_region'
2
+
2
3
  class EgridSubregion < ActiveRecord::Base
3
4
  self.primary_key = "abbreviation"
4
5
 
@@ -7,14 +8,14 @@ class EgridSubregion < ActiveRecord::Base
7
8
 
8
9
  falls_back_on :name => 'fallback',
9
10
  :egrid_region => lambda { EgridRegion.fallback },
10
- :electricity_co2_emission_factor => lambda { weighted_average(:electricity_co2_emission_factor, :weighted_by => :net_generation) },
11
- :electricity_co2_emission_factor_units => 'kilograms_per_kilowatt_hour',
12
- :electricity_co2_biogenic_emission_factor => lambda { weighted_average(:electricity_co2_biogenic_emission_factor, :weighted_by => :net_generation) },
13
- :electricity_co2_biogenic_emission_factor_units => 'kilograms_per_kilowatt_hour',
14
- :electricity_ch4_emission_factor => lambda { weighted_average(:electricity_ch4_emission_factor, :weighted_by => :net_generation) },
15
- :electricity_ch4_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour',
16
- :electricity_n2o_emission_factor => lambda { weighted_average(:electricity_n2o_emission_factor, :weighted_by => :net_generation) },
17
- :electricity_n2o_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour',
11
+ :co2_emission_factor => lambda { weighted_average(:co2_emission_factor, :weighted_by => :net_generation) },
12
+ :co2_emission_factor_units => 'kilograms_per_kilowatt_hour',
13
+ :co2_biogenic_emission_factor => lambda { weighted_average(:co2_biogenic_emission_factor, :weighted_by => :net_generation) },
14
+ :co2_biogenic_emission_factor_units => 'kilograms_per_kilowatt_hour',
15
+ :ch4_emission_factor => lambda { weighted_average(:ch4_emission_factor, :weighted_by => :net_generation) },
16
+ :ch4_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour',
17
+ :n2o_emission_factor => lambda { weighted_average(:n2o_emission_factor, :weighted_by => :net_generation) },
18
+ :n2o_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour',
18
19
  :electricity_emission_factor => lambda { weighted_average(:electricity_emission_factor, :weighted_by => :net_generation) },
19
20
  :electricity_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour'
20
21
 
@@ -24,16 +25,17 @@ class EgridSubregion < ActiveRecord::Base
24
25
  col :egrid_region_name
25
26
  col :net_generation, :type => :float
26
27
  col :net_generation_units
27
- col :electricity_co2_emission_factor, :type => :float
28
- col :electricity_co2_emission_factor_units
29
- col :electricity_co2_biogenic_emission_factor, :type => :float
30
- col :electricity_co2_biogenic_emission_factor_units
31
- col :electricity_ch4_emission_factor, :type => :float
32
- col :electricity_ch4_emission_factor_units
33
- col :electricity_n2o_emission_factor, :type => :float
34
- col :electricity_n2o_emission_factor_units
28
+ col :co2_emission_factor, :type => :float
29
+ col :co2_emission_factor_units
30
+ col :co2_biogenic_emission_factor, :type => :float
31
+ col :co2_biogenic_emission_factor_units
32
+ col :ch4_emission_factor, :type => :float
33
+ col :ch4_emission_factor_units
34
+ col :n2o_emission_factor, :type => :float
35
+ col :n2o_emission_factor_units
35
36
  col :electricity_emission_factor, :type => :float
36
37
  col :electricity_emission_factor_units
37
-
38
- warn_unless_size 27
38
+
39
+ warn_unless_size 26
40
+ warn_if_any_nulls
39
41
  end