earth 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/earth_tester.rb +1 -0
- data/data/automobile/annual_emission_controls.csv +1 -1
- data/data/automobile/annual_fuel_consumption.csv +1 -1
- data/data/automobile/auto_fuel_data.csv +1 -1
- data/data/automobile/emission_control_techs.csv +1 -1
- data/data/automobile/trip_distances.csv +5 -0
- data/data/locality/country_flight_data.csv +18 -0
- data/data/locality/egrid_relationships.csv +27 -0
- data/data/locality/misc_zip_data.csv +1 -0
- data/data/locality/us_auto_data.csv +2 -0
- data/data/locality/wri_hdd_cdd_data.csv +174 -0
- data/data/locality/zip_climate_divisions.csv +41359 -0
- data/errata/country/wri_errata.csv +24 -24
- data/lib/earth/automobile/automobile_activity_year/data_miner.rb +4 -3
- data/lib/earth/automobile/automobile_activity_year_type/data_miner.rb +4 -3
- data/lib/earth/automobile/automobile_activity_year_type_fuel.rb +1 -1
- data/lib/earth/automobile/automobile_activity_year_type_fuel/data_miner.rb +4 -4
- data/lib/earth/automobile/automobile_fuel.rb +5 -5
- data/lib/earth/automobile/automobile_fuel/data_miner.rb +18 -13
- data/lib/earth/automobile/automobile_make/data_miner.rb +4 -3
- data/lib/earth/automobile/automobile_make_model/data_miner.rb +6 -7
- data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +1 -2
- data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +5 -10
- data/lib/earth/automobile/automobile_type_fuel.rb +2 -2
- data/lib/earth/automobile/automobile_type_fuel/data_miner.rb +8 -7
- data/lib/earth/automobile/automobile_type_fuel_control.rb +1 -1
- data/lib/earth/automobile/automobile_type_fuel_control/data_miner.rb +1 -1
- data/lib/earth/automobile/automobile_type_fuel_year.rb +5 -5
- data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +7 -6
- data/lib/earth/automobile/automobile_type_fuel_year_control.rb +4 -4
- data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +2 -2
- data/lib/earth/conversions_ext.rb +2 -0
- data/lib/earth/industry/industry.rb +0 -2
- data/lib/earth/industry/industry/data_miner.rb +5 -2
- data/lib/earth/locality/census_division.rb +2 -1
- data/lib/earth/locality/country.rb +3 -7
- data/lib/earth/locality/country/data_miner.rb +54 -75
- data/lib/earth/locality/egrid_country.rb +9 -3
- data/lib/earth/locality/egrid_country/data_miner.rb +14 -8
- data/lib/earth/locality/egrid_region.rb +15 -6
- data/lib/earth/locality/egrid_region/data_miner.rb +14 -18
- data/lib/earth/locality/egrid_subregion.rb +21 -19
- data/lib/earth/locality/egrid_subregion/data_miner.rb +27 -47
- data/lib/earth/locality/zip_code.rb +5 -4
- data/lib/earth/locality/zip_code/data_miner.rb +25 -14
- data/lib/earth/rail/rail_company/data_miner.rb +1 -1
- data/lib/earth/version.rb +1 -1
- data/spec/earth/automobile/automobile_activity_year_type_fuel_spec.rb +1 -1
- data/spec/earth/automobile/automobile_fuel_spec.rb +1 -1
- data/spec/earth/automobile/automobile_type_fuel_spec.rb +1 -1
- data/spec/earth/automobile/automobile_type_fuel_year_control_spec.rb +6 -6
- data/spec/earth/automobile/automobile_type_fuel_year_spec.rb +6 -6
- data/spec/earth/industry/industry_spec.rb +1 -3
- data/spec/earth/locality/country_spec.rb +67 -34
- data/spec/earth/locality/egrid_country_spec.rb +26 -0
- data/spec/earth/locality/egrid_region_spec.rb +27 -0
- data/spec/earth/locality/egrid_subregion_spec.rb +49 -0
- data/spec/earth/locality/zip_code_spec.rb +13 -24
- 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.
|
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, :
|
10
|
+
where(:type_name => type_name, :fuel_group => fuel_group, :year => maximum(:year))
|
11
11
|
else
|
12
|
-
where(:type_name => type_name, :
|
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 :
|
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 '
|
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['
|
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,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/
|
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 :
|
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 =>
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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 =>
|
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', :
|
46
|
-
store 'automobile_highway_speed', :
|
47
|
-
store 'automobile_trip_distance', :
|
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
|
51
|
-
|
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
|
55
|
-
|
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(:
|
58
|
-
:automobile_fuel_efficiency_units => (fuel_years.first.
|
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
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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 =>
|
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
|
-
|
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.
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
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.
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
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
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
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 '
|
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 :
|
14
|
-
col :
|
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 "
|
4
|
-
:url => 'http://www.epa.gov/cleanenergy/documents/egridzips/
|
5
|
-
:sheet => '
|
6
|
-
:skip =>
|
7
|
-
|
8
|
-
|
9
|
-
store '
|
10
|
-
store '
|
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
|
-
#
|
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 {
|
11
|
+
:loss_factor => lambda { EgridCountry.us.loss_factor }
|
10
12
|
|
11
13
|
col :name
|
12
|
-
col :
|
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
|
16
|
-
|
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
|
-
|
4
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
20
|
-
|
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/
|
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
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
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 :
|
28
|
-
col :
|
29
|
-
col :
|
30
|
-
col :
|
31
|
-
col :
|
32
|
-
col :
|
33
|
-
col :
|
34
|
-
col :
|
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
|
38
|
+
|
39
|
+
warn_unless_size 26
|
40
|
+
warn_if_any_nulls
|
39
41
|
end
|