earth 0.11.0 → 0.11.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.
Files changed (51) hide show
  1. data/bin/earth_tester.rb +1 -0
  2. data/earth.gemspec +1 -1
  3. data/lib/earth.rb +1 -1
  4. data/lib/earth/air/flight_segment/data_miner.rb +5 -9
  5. data/lib/earth/automobile/automobile_size_class_year.rb +1 -0
  6. data/lib/earth/automobile/automobile_size_class_year/data_miner.rb +1 -0
  7. data/lib/earth/data_miner.rb +1 -0
  8. data/lib/earth/fuel/fuel_type.rb +1 -0
  9. data/lib/earth/fuel/fuel_type/data_miner.rb +1 -0
  10. data/lib/earth/hospitality.rb +2 -0
  11. data/lib/earth/hospitality/census_region_lodging_class.rb +16 -0
  12. data/lib/earth/hospitality/census_region_lodging_class/data_miner.rb +40 -0
  13. data/lib/earth/hospitality/country_lodging_class.rb +16 -0
  14. data/lib/earth/hospitality/country_lodging_class/data_miner.rb +40 -0
  15. data/lib/earth/hospitality/data_miner.rb +2 -0
  16. data/lib/earth/hospitality/lodging_class.rb +3 -1
  17. data/lib/earth/hospitality/lodging_class/data_miner.rb +6 -5
  18. data/lib/earth/industry/data_miner.rb +1 -0
  19. data/lib/earth/industry/industry.rb +1 -1
  20. data/lib/earth/industry/industry/data_miner.rb +12 -0
  21. data/lib/earth/industry/merchant_category.rb +1 -1
  22. data/lib/earth/locality.rb +5 -7
  23. data/lib/earth/locality/census_division/data_miner.rb +7 -6
  24. data/lib/earth/locality/country.rb +25 -2
  25. data/lib/earth/locality/country/data_miner.rb +74 -23
  26. data/lib/earth/locality/data_miner.rb +5 -7
  27. data/lib/earth/locality/egrid_country.rb +17 -0
  28. data/lib/earth/locality/egrid_country/data_miner.rb +13 -0
  29. data/lib/earth/locality/egrid_region.rb +2 -14
  30. data/lib/earth/locality/egrid_region/data_miner.rb +15 -14
  31. data/lib/earth/locality/egrid_subregion.rb +3 -64
  32. data/lib/earth/locality/egrid_subregion/data_miner.rb +39 -25
  33. data/lib/earth/locality/zip_code/data_miner.rb +1 -0
  34. data/lib/earth/rail.rb +6 -0
  35. data/lib/earth/{locality → rail}/country_rail_class.rb +0 -0
  36. data/lib/earth/{locality → rail}/country_rail_class/data_miner.rb +0 -0
  37. data/lib/earth/{locality → rail}/country_rail_traction.rb +0 -0
  38. data/lib/earth/{locality → rail}/country_rail_traction/data_miner.rb +0 -0
  39. data/lib/earth/{locality → rail}/country_rail_traction_class.rb +0 -0
  40. data/lib/earth/{locality → rail}/country_rail_traction_class/data_miner.rb +0 -0
  41. data/lib/earth/rail/data_miner.rb +6 -0
  42. data/lib/earth/rail/national_transit_database_record/data_miner.rb +1 -0
  43. data/lib/earth/rail/rail_company/data_miner.rb +6 -5
  44. data/lib/earth/residence.rb +1 -2
  45. data/lib/earth/residence/data_miner.rb +1 -2
  46. data/lib/earth/residence/residence_fuel_type.rb +5 -2
  47. data/lib/earth/{locality → residence}/urbanity.rb +0 -0
  48. data/lib/earth/{locality → residence}/urbanity/data_miner.rb +0 -0
  49. data/lib/earth/version.rb +1 -1
  50. data/spec/earth_spec.rb +6 -6
  51. metadata +216 -209
data/bin/earth_tester.rb CHANGED
@@ -55,6 +55,7 @@ require 'earth'
55
55
  domain = ARGV[0]
56
56
 
57
57
  Earth.init domain, :load_data_miner => true, :apply_schemas => true
58
+ DataMiner::Run.create_tables
58
59
 
59
60
  ActiveRecord::Base.logger = Logger.new $stderr
60
61
  ActiveRecord::Base.logger.level = Logger::INFO
data/earth.gemspec CHANGED
@@ -5,7 +5,7 @@ require "earth/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "earth"
7
7
  s.version = Earth::VERSION
8
- s.date = "2011-10-05"
8
+ s.date = "2011-12-06"
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.authors = ["Seamus Abshere", "Derek Kastner", "Andy Rossmeissl"]
11
11
  s.email = %q{andy@rossmeissl.net}
data/lib/earth.rb CHANGED
@@ -131,7 +131,7 @@ module Earth
131
131
  end
132
132
 
133
133
  TAPS_STEP = 'Tap the Brighter Planet data server'
134
- TAPS_SOURCE = 'http://carbon:neutral@data.brighterplanet.com'
134
+ TAPS_SOURCE = 'http://carbon:neutral@data.brighterplanet.com:5000'
135
135
  def _prepend_taps_step_to_data_miner(resource)
136
136
  resource_model = resource.constantize
137
137
  return if resource_model.data_miner_config.steps.any? { |step| step.description == TAPS_STEP }
@@ -10,13 +10,15 @@ FlightSegment.class_eval do
10
10
  end
11
11
  end
12
12
 
13
- URL = 'http://www.transtats.bts.gov/DownLoad_Table.asp?Table_ID=293&Has_Group=3&Is_Zipped=0'
13
+ URL = 'http://www.transtats.bts.gov/DownLoad_Table.asp'
14
14
  FORM_DATA = %{
15
+ Table_ID=293&
16
+ Has_Group=3&
15
17
  UserTableName=T_100_Segment__All_Carriers&
16
18
  DBShortName=Air_Carriers&
17
19
  RawDataTable=T_T100_SEGMENT_ALL_CARRIER&
18
- sqlstr=+SELECT+DEPARTURES_SCHEDULED%2CDEPARTURES_PERFORMED%2CPAYLOAD%2CSEATS%2CPASSENGERS%2CFREIGHT%2CMAIL%2CDISTANCE%2CRAMP_TO_RAMP%2CAIR_TIME%2CUNIQUE_CARRIER%2CAIRLINE_ID%2CUNIQUE_CARRIER_NAME%2CUNIQUE_CARRIER_ENTITY%2CREGION%2CCARRIER%2CCARRIER_NAME%2CCARRIER_GROUP%2CCARRIER_GROUP_NEW%2CORIGIN%2CORIGIN_CITY_NAME%2CORIGIN_CITY_NUM%2CORIGIN_STATE_ABR%2CORIGIN_STATE_FIPS%2CORIGIN_STATE_NM%2CORIGIN_COUNTRY%2CORIGIN_COUNTRY_NAME%2CORIGIN_WAC%2CDEST%2CDEST_CITY_NAME%2CDEST_CITY_NUM%2CDEST_STATE_ABR%2CDEST_STATE_FIPS%2CDEST_STATE_NM%2CDEST_COUNTRY%2CDEST_COUNTRY_NAME%2CDEST_WAC%2CAIRCRAFT_GROUP%2CAIRCRAFT_TYPE%2CAIRCRAFT_CONFIG%2CYEAR%2CQUARTER%2CMONTH%2CDISTANCE_GROUP%2CCLASS%2CDATA_SOURCE+FROM++T_T100_SEGMENT_ALL_CARRIER+WHERE+Month+%3D__MONTH_NUMBER__+AND+YEAR%3D__YEAR__&
19
- varlist=DEPARTURES_SCHEDULED%2CDEPARTURES_PERFORMED%2CPAYLOAD%2CSEATS%2CPASSENGERS%2CFREIGHT%2CMAIL%2CDISTANCE%2CRAMP_TO_RAMP%2CAIR_TIME%2CUNIQUE_CARRIER%2CAIRLINE_ID%2CUNIQUE_CARRIER_NAME%2CUNIQUE_CARRIER_ENTITY%2CREGION%2CCARRIER%2CCARRIER_NAME%2CCARRIER_GROUP%2CCARRIER_GROUP_NEW%2CORIGIN%2CORIGIN_CITY_NAME%2CORIGIN_CITY_NUM%2CORIGIN_STATE_ABR%2CORIGIN_STATE_FIPS%2CORIGIN_STATE_NM%2CORIGIN_COUNTRY%2CORIGIN_COUNTRY_NAME%2CORIGIN_WAC%2CDEST%2CDEST_CITY_NAME%2CDEST_CITY_NUM%2CDEST_STATE_ABR%2CDEST_STATE_FIPS%2CDEST_STATE_NM%2CDEST_COUNTRY%2CDEST_COUNTRY_NAME%2CDEST_WAC%2CAIRCRAFT_GROUP%2CAIRCRAFT_TYPE%2CAIRCRAFT_CONFIG%2CYEAR%2CQUARTER%2CMONTH%2CDISTANCE_GROUP%2CCLASS%2CDATA_SOURCE&
20
+ sqlstr=+SELECT+DEPARTURES_SCHEDULED%2CDEPARTURES_PERFORMED%2CPAYLOAD%2CSEATS%2CPASSENGERS%2CFREIGHT%2CMAIL%2CDISTANCE%2CRAMP_TO_RAMP%2CAIR_TIME%2CUNIQUE_CARRIER%2CAIRLINE_ID%2CUNIQUE_CARRIER_NAME%2CUNIQUE_CARRIER_ENTITY%2CREGION%2CCARRIER%2CCARRIER_NAME%2CCARRIER_GROUP%2CCARRIER_GROUP_NEW%2CORIGIN%2CORIGIN_CITY_NAME%2CORIGIN_STATE_ABR%2CORIGIN_STATE_FIPS%2CORIGIN_STATE_NM%2CORIGIN_COUNTRY%2CORIGIN_COUNTRY_NAME%2CORIGIN_WAC%2CDEST%2CDEST_CITY_NAME%2CDEST_STATE_ABR%2CDEST_STATE_FIPS%2CDEST_STATE_NM%2CDEST_COUNTRY%2CDEST_COUNTRY_NAME%2CDEST_WAC%2CAIRCRAFT_GROUP%2CAIRCRAFT_TYPE%2CAIRCRAFT_CONFIG%2CYEAR%2CQUARTER%2CMONTH%2CDISTANCE_GROUP%2CCLASS%2CDATA_SOURCE+FROM++T_T100_SEGMENT_ALL_CARRIER+WHERE+Month+%3D__MONTH_NUMBER__+AND+YEAR%3D__YEAR__&
21
+ varlist=DEPARTURES_SCHEDULED%2CDEPARTURES_PERFORMED%2CPAYLOAD%2CSEATS%2CPASSENGERS%2CFREIGHT%2CMAIL%2CDISTANCE%2CRAMP_TO_RAMP%2CAIR_TIME%2CUNIQUE_CARRIER%2CAIRLINE_ID%2CUNIQUE_CARRIER_NAME%2CUNIQUE_CARRIER_ENTITY%2CREGION%2CCARRIER%2CCARRIER_NAME%2CCARRIER_GROUP%2CCARRIER_GROUP_NEW%2CORIGIN%2CORIGIN_CITY_NAME%2CORIGIN_STATE_ABR%2CORIGIN_STATE_FIPS%2CORIGIN_STATE_NM%2CORIGIN_COUNTRY%2CORIGIN_COUNTRY_NAME%2CORIGIN_WAC%2CDEST%2CDEST_CITY_NAME%2CDEST_STATE_ABR%2CDEST_STATE_FIPS%2CDEST_STATE_NM%2CDEST_COUNTRY%2CDEST_COUNTRY_NAME%2CDEST_WAC%2CAIRCRAFT_GROUP%2CAIRCRAFT_TYPE%2CAIRCRAFT_CONFIG%2CYEAR%2CQUARTER%2CMONTH%2CDISTANCE_GROUP%2CCLASS%2CDATA_SOURCE&
20
22
  grouplist=&
21
23
  suml=&
22
24
  sumRegion=&
@@ -92,9 +94,6 @@ FlightSegment.class_eval do
92
94
  VarName=ORIGIN_CITY_NAME&
93
95
  VarDesc=OriginCityName&
94
96
  VarType=Char&
95
- VarName=ORIGIN_CITY_NUM&
96
- VarDesc=OriginCityNum&
97
- VarType=Num&
98
97
  VarName=ORIGIN_STATE_ABR&
99
98
  VarDesc=OriginState&
100
99
  VarType=Char&
@@ -119,9 +118,6 @@ FlightSegment.class_eval do
119
118
  VarName=DEST_CITY_NAME&
120
119
  VarDesc=DestCityName&
121
120
  VarType=Char&
122
- VarName=DEST_CITY_NUM&
123
- VarDesc=DestCityNum&
124
- VarType=Num&
125
121
  VarName=DEST_STATE_ABR&
126
122
  VarDesc=DestState&
127
123
  VarType=Char&
@@ -1,3 +1,4 @@
1
+ # Not yet in use - for if we want to autocaculate size class stuf - November 10, 2011
1
2
  class AutomobileSizeClassYear < ActiveRecord::Base
2
3
  set_primary_key :name
3
4
 
@@ -1,3 +1,4 @@
1
+ # Not yet in use - for if we want to autocaculate size class stuf - November 10, 2011
1
2
  AutomobileSizeClassYear.class_eval do
2
3
  data_miner do
3
4
  import "automobile size class year fuel efficiencies from the 2010 EPA Fuel Economy Trends report",
@@ -10,3 +10,4 @@ require 'earth/pet/data_miner'
10
10
  require 'earth/rail/data_miner'
11
11
  require 'earth/residence/data_miner'
12
12
  require 'earth/shipping/data_miner'
13
+ require 'earth/industry/data_miner'
@@ -1,3 +1,4 @@
1
+ # DEPRECATED but FuelPurchase still uses this
1
2
  class FuelType < ActiveRecord::Base
2
3
  set_primary_key :name
3
4
 
@@ -1,3 +1,4 @@
1
+ # DEPRECATED but FuelPurchase still uses this
1
2
  FuelType.class_eval do
2
3
  # FIXME TODO phase 2
3
4
  # annual emissions factors
@@ -1 +1,3 @@
1
+ require 'earth/hospitality/census_region_lodging_class'
2
+ require 'earth/hospitality/country_lodging_class'
1
3
  require 'earth/hospitality/lodging_class'
@@ -0,0 +1,16 @@
1
+ class CensusRegionLodgingClass < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ col :name
5
+ col :census_region_number
6
+ col :lodging_class_name
7
+ col :electricity_intensity, :type => :float
8
+ col :electricity_intensity_units
9
+ col :natural_gas_intensity, :type => :float
10
+ col :natural_gas_intensity_units
11
+ col :fuel_oil_intensity, :type => :float
12
+ col :fuel_oil_intensity_units
13
+ col :district_heat_intensity, :type => :float
14
+ col :district_heat_intensity_units
15
+ col :weighting, :type => :float
16
+ end
@@ -0,0 +1,40 @@
1
+ CensusRegionLodgingClass.class_eval do
2
+ data_miner do
3
+ import "US census region lodging class fuel intensities derived from CBECS 2003",
4
+ :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdGkxNExTajZPSjRWU3REVks5SFJ0cmc&output=csv',
5
+ :select => lambda { |row| row['census_region_lodging_class'].present? } do
6
+ key 'name', :field_name => 'census_region_lodging_class'
7
+ store 'census_region_number', :field_name => 'census_region'
8
+ store 'lodging_class_name', :field_name => 'lodging_class'
9
+ store 'electricity_intensity', :units_field_name => 'electricity_intensity_units'
10
+ store 'natural_gas_intensity', :units_field_name => 'natural_gas_intensity_units'
11
+ store 'fuel_oil_intensity', :units_field_name => 'fuel_oil_intensity_units'
12
+ store 'district_heat_intensity', :units_field_name => 'district_heat_intensity_units'
13
+ store 'weighting'
14
+ end
15
+
16
+ process "Convert natural gas intensities to metric units" do
17
+ conversion_factor = 2.83168466 # Google: 2.83168466 cubic m / 100 cubic ft
18
+ where(:natural_gas_intensity_units => 'hundred_cubic_feet_per_room_night').update_all(%{
19
+ natural_gas_intensity = 1.0 * natural_gas_intensity * #{conversion_factor},
20
+ natural_gas_intensity_units = 'cubic_metres_per_room_night'
21
+ })
22
+ end
23
+
24
+ process "Convert fuel oil intensities to metric units" do
25
+ conversion_factor = 3.78541178 # Google: 3.78541178 l / gal
26
+ where(:fuel_oil_intensity_units => 'gallons_per_room_night').update_all(%{
27
+ fuel_oil_intensity = 1.0 * fuel_oil_intensity * #{conversion_factor},
28
+ fuel_oil_intensity_units = 'litres_per_room_night'
29
+ })
30
+ end
31
+
32
+ process "Convert district heat intensities to metric units" do
33
+ conversion_factor = 1.05505585 # Google: 1.05505585 MJ / 1000 Btu
34
+ where(:district_heat_intensity_units => 'thousand_btu_per_room_night').update_all(%{
35
+ district_heat_intensity = 1.0 * district_heat_intensity * #{conversion_factor},
36
+ district_heat_intensity_units = 'megajoules_per_room_night'
37
+ })
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ class CountryLodgingClass < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ col :name
5
+ col :country_iso_3166_code
6
+ col :lodging_class_name
7
+ col :electricity_intensity, :type => :float
8
+ col :electricity_intensity_units
9
+ col :natural_gas_intensity, :type => :float
10
+ col :natural_gas_intensity_units
11
+ col :fuel_oil_intensity, :type => :float
12
+ col :fuel_oil_intensity_units
13
+ col :district_heat_intensity, :type => :float
14
+ col :district_heat_intensity_units
15
+ col :weighting, :type => :float
16
+ end
@@ -0,0 +1,40 @@
1
+ CountryLodgingClass.class_eval do
2
+ data_miner do
3
+ import "US lodging classes and their fuel intensities derived from CBECS 2003",
4
+ :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdGkxNExTajZPSjRWU3REVks5SFJ0cmc&output=csv',
5
+ :select => lambda { |row| row['country_lodging_class'].present? } do
6
+ key 'name', :field_name => 'country_lodging_class'
7
+ store 'country_iso_3166_code', :field_name => 'country'
8
+ store 'lodging_class_name', :field_name => 'lodging_class'
9
+ store 'electricity_intensity', :units_field_name => 'electricity_intensity_units'
10
+ store 'natural_gas_intensity', :units_field_name => 'natural_gas_intensity_units'
11
+ store 'fuel_oil_intensity', :units_field_name => 'fuel_oil_intensity_units'
12
+ store 'district_heat_intensity', :units_field_name => 'district_heat_intensity_units'
13
+ store 'weighting'
14
+ end
15
+
16
+ process "Convert natural gas intensities to metric units" do
17
+ conversion_factor = 2.83168466 # Google: 2.83168466 cubic m / 100 cubic ft
18
+ where(:natural_gas_intensity_units => 'hundred_cubic_feet_per_room_night').update_all(%{
19
+ natural_gas_intensity = 1.0 * natural_gas_intensity * #{conversion_factor},
20
+ natural_gas_intensity_units = 'cubic_metres_per_room_night'
21
+ })
22
+ end
23
+
24
+ process "Convert fuel oil intensities to metric units" do
25
+ conversion_factor = 3.78541178 # Google: 3.78541178 l / gal
26
+ where(:fuel_oil_intensity_units => 'gallons_per_room_night').update_all(%{
27
+ fuel_oil_intensity = 1.0 * fuel_oil_intensity * #{conversion_factor},
28
+ fuel_oil_intensity_units = 'litres_per_room_night'
29
+ })
30
+ end
31
+
32
+ process "Convert district heat intensities to metric units" do
33
+ conversion_factor = 1.05505585 # Google: 1.05505585 MJ / 1000 Btu
34
+ where(:district_heat_intensity_units => 'thousand_btu_per_room_night').update_all(%{
35
+ district_heat_intensity = 1.0 * district_heat_intensity * #{conversion_factor},
36
+ district_heat_intensity_units = 'megajoules_per_room_night'
37
+ })
38
+ end
39
+ end
40
+ end
@@ -1 +1,3 @@
1
+ require 'earth/hospitality/census_region_lodging_class/data_miner'
2
+ require 'earth/hospitality/country_lodging_class/data_miner'
1
3
  require 'earth/hospitality/lodging_class/data_miner'
@@ -2,6 +2,8 @@ class LodgingClass < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
4
  col :name
5
+
6
+ # DEPRECATED - cut once new Lodging has been phased in
5
7
  col :electricity_intensity, :type => :float
6
8
  col :electricity_intensity_units
7
9
  col :natural_gas_intensity, :type => :float
@@ -10,4 +12,4 @@ class LodgingClass < ActiveRecord::Base
10
12
  col :fuel_oil_intensity_units
11
13
  col :district_heat_intensity, :type => :float
12
14
  col :district_heat_intensity_units
13
- end
15
+ end
@@ -1,11 +1,12 @@
1
1
  LodgingClass.class_eval do
2
2
  data_miner do
3
- import "a list of lodging classes and pre-calculated emission factors",
4
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGZZWmZtWEJlYzhRNXlPdWpBTldlcUE&hl=en&gid=0&output=csv' do
3
+ # DEPRECATED - once new Lodging is phased in replace this with an import of lodging class names from the CountryLodgingClass google doc
4
+ import "a list of lodging classes and pre-calculated energy intensities",
5
+ :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdGZZWmZtWEJlYzhRNXlPdWpBTldlcUE&output=csv' do
5
6
  key 'name'
6
- store 'natural_gas_intensity', :units_field_name => 'natural_gas_intensity_units'
7
- store 'fuel_oil_intensity', :units_field_name => 'fuel_oil_intensity_units'
8
- store 'electricity_intensity', :units_field_name => 'electricity_intensity_units'
7
+ store 'electricity_intensity', :units_field_name => 'electricity_intensity_units'
8
+ store 'natural_gas_intensity', :units_field_name => 'natural_gas_intensity_units'
9
+ store 'fuel_oil_intensity', :units_field_name => 'fuel_oil_intensity_units'
9
10
  store 'district_heat_intensity', :units_field_name => 'district_heat_intensity_units'
10
11
  end
11
12
 
@@ -0,0 +1 @@
1
+ require 'earth/industry/industry/data_miner'
@@ -10,7 +10,7 @@ class Industry < ActiveRecord::Base
10
10
  has_many :sectors, :through => :industry_sectors
11
11
 
12
12
  col :naics_code
13
- col :description, :type => :text
13
+ col :description
14
14
 
15
15
  def trade_industry?
16
16
  prefix = naics_code.to_s[0,2]
@@ -0,0 +1,12 @@
1
+ Industry.class_eval do
2
+ data_miner do
3
+ import "the U.S. Census 2002 NAICS code list",
4
+ :url => 'http://www.census.gov/epcd/naics02/naicod02.txt',
5
+ :skip => 4,
6
+ :headers => false,
7
+ :delimiter => ' ' do
8
+ key 'naics_code', :field_number => 0
9
+ store 'description', :field_number => 1
10
+ end
11
+ end
12
+ end
@@ -9,5 +9,5 @@ class MerchantCategory < ActiveRecord::Base
9
9
  end
10
10
 
11
11
  col :mcc
12
- col :description, :type => :text
12
+ col :description
13
13
  end
@@ -2,16 +2,14 @@ require 'earth/locality/census_division'
2
2
  require 'earth/locality/census_region'
3
3
  require 'earth/locality/climate_division'
4
4
  require 'earth/locality/country'
5
- require 'earth/locality/country_rail_traction'
6
- require 'earth/locality/country_rail_class'
7
- require 'earth/locality/country_rail_traction_class'
5
+ require 'earth/locality/egrid_country'
8
6
  require 'earth/locality/egrid_region'
9
7
  require 'earth/locality/egrid_subregion'
10
8
  require 'earth/locality/petroleum_administration_for_defense_district'
11
9
  require 'earth/locality/state'
12
- require 'earth/locality/urbanity'
13
10
  require 'earth/locality/zip_code'
14
11
 
15
- require 'earth/automobile'
16
- require 'earth/residence'
17
- require 'earth/rail'
12
+ require 'earth/automobile' # for automobile data on Country
13
+ require 'earth/fuel' # for GWPs for EgridSubregion
14
+ require 'earth/hospitality' # for lodging data on Country
15
+ require 'earth/rail' # for rail data on Country
@@ -29,13 +29,14 @@ CensusDivision.class_eval do
29
29
  store 'meeting_building_district_heat_intensity', :from_units => :thousand_btu_per_square_foot_hour, :to_units => :megajoules_per_square_metre_hour
30
30
  end
31
31
 
32
- import 'lodging building fuel intensities calculated from CBECS 2003',
33
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHZ1QjdNTGVpbWxmZDVLbjlpTHlFVHc&hl=en&gid=0&output=csv' do
32
+ import 'lodging building fuel intensities derived from CBECS 2003',
33
+ :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdGkxNExTajZPSjRWU3REVks5SFJ0cmc&output=csv',
34
+ :select => lambda { |row| row['census_division'].to_i.between?(1, 9) } do
34
35
  key 'number', :field_name => 'census_division'
35
- store 'lodging_building_natural_gas_intensity', :from_units => :hundred_cubic_feet_per_room_night, :to_units => :cubic_metres_per_room_night
36
- store 'lodging_building_fuel_oil_intensity', :from_units => :gallons_per_room_night, :to_units => :litres_per_room_night
37
- store 'lodging_building_electricity_intensity', :units_field_name => 'lodging_building_electricity_intensity_units'
38
- store 'lodging_building_district_heat_intensity', :from_units => :thousand_btu_per_room_night, :to_units => :megajoules_per_room_night
36
+ store 'lodging_building_electricity_intensity', :field_name => 'electricity_intensity', :units_field_name => 'electricity_intensity_units'
37
+ store 'lodging_building_natural_gas_intensity', :field_name => 'natural_gas_intensity', :from_units => :hundred_cubic_feet_per_room_night, :to_units => :cubic_metres_per_room_night
38
+ store 'lodging_building_fuel_oil_intensity', :field_name => 'fuel_oil_intensity', :from_units => :gallons_per_room_night, :to_units => :litres_per_room_night
39
+ store 'lodging_building_district_heat_intensity', :field_name => 'district_heat_intensity', :from_units => :thousand_btu_per_room_night, :to_units => :megajoules_per_room_night
39
40
  end
40
41
  end
41
42
  end
@@ -1,7 +1,8 @@
1
1
  class Country < ActiveRecord::Base
2
2
  set_primary_key :iso_3166_code
3
3
 
4
- has_many :rail_companies, :foreign_key => 'country_iso_3166_code'
4
+ has_many :rail_companies, :foreign_key => 'country_iso_3166_code' # used to calculate rail data
5
+ has_many :lodging_classes, :foreign_key => 'country_iso_3166_code', :class_name => 'CountryLodgingClass'
5
6
 
6
7
  falls_back_on :name => 'fallback',
7
8
  :automobile_urbanity => lambda { united_states.automobile_urbanity }, # for now assume US represents world
@@ -13,7 +14,17 @@ class Country < ActiveRecord::Base
13
14
  :automobile_highway_speed_units => lambda { united_states.automobile_highway_speed_units }, # for now assume US represents world
14
15
  :automobile_trip_distance => lambda { united_states.automobile_trip_distance }, # for now assume US represents world
15
16
  :automobile_trip_distance_units => lambda { united_states.automobile_trip_distance_units }, # for now assume US represents world
17
+ :electricity_emission_factor => lambda { maximum(:electricity_emission_factor) }, # default to the largest electricity emission factor
18
+ :electricity_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour', # FIXME TODO derive this
16
19
  :flight_route_inefficiency_factor => lambda { maximum(:flight_route_inefficiency_factor) }, # default to the largest inefficiency factor
20
+ :lodging_natural_gas_intensity => lambda { united_states.lodging_natural_gas_intensity }, # for now assume US represents world
21
+ :lodging_natural_gas_intensity_units => lambda { united_states.lodging_natural_gas_intensity_units }, # for now assume US represents world
22
+ :lodging_fuel_oil_intensity => lambda { united_states.lodging_fuel_oil_intensity }, # for now assume US represents world
23
+ :lodging_fuel_oil_intensity_units => lambda { united_states.lodging_fuel_oil_intensity_units }, # for now assume US represents world
24
+ :lodging_electricity_intensity => lambda { united_states.lodging_electricity_intensity }, # for now assume US represents world
25
+ :lodging_electricity_intensity_units => lambda { united_states.lodging_electricity_intensity_units }, # for now assume US represents world
26
+ :lodging_district_heat_intensity => lambda { united_states.lodging_district_heat_intensity }, # for now assume US represents world
27
+ :lodging_district_heat_intensity_units => lambda { united_states.lodging_district_heat_intensity_units }, # for now assume US represents world
17
28
  :rail_trip_distance => lambda { weighted_average(:rail_trip_distance, :weighted_by => :rail_passengers) },
18
29
  :rail_trip_distance_units => 'kilometres', # FIXME TODO derive this
19
30
  :rail_speed => lambda { weighted_average(:rail_speed, :weighted_by => :rail_passengers) },
@@ -31,7 +42,9 @@ class Country < ActiveRecord::Base
31
42
  end
32
43
  end
33
44
 
34
- col :iso_3166_code
45
+ col :iso_3166_code # alpha-2 2-letter like GB
46
+ col :iso_3166_numeric_code, :type => :integer # numeric like 826; aka UN M49 code
47
+ col :iso_3166_alpha_3_code # 3-letter like GBR
35
48
  col :name
36
49
  col :automobile_urbanity, :type => :float # float from 0 to 1
37
50
  col :automobile_fuel_efficiency, :type => :float
@@ -42,7 +55,17 @@ class Country < ActiveRecord::Base
42
55
  col :automobile_highway_speed_units
43
56
  col :automobile_trip_distance, :type => :float
44
57
  col :automobile_trip_distance_units
58
+ col :electricity_emission_factor, :type => :float
59
+ col :electricity_emission_factor_units
45
60
  col :flight_route_inefficiency_factor, :type => :float
61
+ col :lodging_natural_gas_intensity, :type => :float
62
+ col :lodging_natural_gas_intensity_units
63
+ col :lodging_fuel_oil_intensity, :type => :float
64
+ col :lodging_fuel_oil_intensity_units
65
+ col :lodging_electricity_intensity, :type => :float
66
+ col :lodging_electricity_intensity_units
67
+ col :lodging_district_heat_intensity, :type => :float
68
+ col :lodging_district_heat_intensity_units
46
69
  col :rail_passengers, :type => :float
47
70
  col :rail_trip_distance, :type => :float
48
71
  col :rail_trip_distance_units
@@ -1,26 +1,25 @@
1
1
  Country.class_eval do
2
2
  data_miner do
3
- import 'the official ISO country list',
4
- :url => 'http://www.iso.org/iso/list-en1-semic-3.txt',
5
- :skip => 2,
6
- :headers => false,
3
+ # http://www.iso.org/iso/list-en1-semic-3.txt
4
+ # http://unstats.un.org/unsd/methods/m49/m49alpha.htm
5
+ import "OpenGeoCode.org's Country Codes to Country Names list",
6
+ :url => 'http://opengeocode.org/download/countrynames.txt',
7
+ :format => :delimited,
7
8
  :delimiter => ';',
8
- :encoding => 'ISO-8859-1' do
9
- key 'iso_3166_code', :field_number => 1
10
- store 'name', :field_number => 0
11
- end
12
-
13
- import "country-specific flight route inefficiency factors derived from Kettunen et al. (2005)",
14
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdEJoRVBZaGhnUmlhX240VXE3X0F3WkE&output=csv' do
15
- key 'iso_3166_code'
16
- store 'flight_route_inefficiency_factor'
9
+ :headers => false,
10
+ :skip => 22 do
11
+ key 'iso_3166_code', :field_number => 0
12
+ store 'iso_3166_alpha_3_code', :field_number => 1
13
+ store 'iso_3166_numeric_code', :field_number => 2
14
+ store 'name', :field_number => 5 # romanized version
17
15
  end
18
16
 
17
+ # AUTOMOBILE
19
18
  import "automobile-related data for the US",
20
19
  :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdDdZRm1tNjY0c2dYNG00bXJ3TXRqUVE&gid=0&output=csv' do
21
20
  key 'iso_3166_code'
22
21
  store 'automobile_urbanity'
23
- store 'automobile_city_speed', :units_field_name => 'automobile_city_speed_units'
22
+ store 'automobile_city_speed', :units_field_name => 'automobile_city_speed_units'
24
23
  store 'automobile_highway_speed', :units_field_name => 'automobile_highway_speed_units'
25
24
  store 'automobile_trip_distance', :units_field_name => 'automobile_trip_distance_units'
26
25
  end
@@ -29,7 +28,6 @@ Country.class_eval do
29
28
  AutomobileTypeFuelYear.run_data_miner!
30
29
  end
31
30
 
32
- # FIXME TODO eventually need to do this for all countries
33
31
  process "Derive US average automobile fuel efficiency from AutomobileTypeFuelYear" do
34
32
  fuel_years = AutomobileTypeFuelYear.where(:year => AutomobileTypeFuelYear.maximum(:year))
35
33
  where(:iso_3166_code => 'US').update_all(
@@ -62,8 +60,67 @@ Country.class_eval do
62
60
  })
63
61
  end
64
62
 
65
- process "Ensure RailCompany is populated" do
63
+ # ELECTRICITY
64
+ import "calculate national electricity emission factors from Brander et al. (2011)",
65
+ :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdDZmWHFjLVdBZGRBdGxVdDdqd1YtYWc&output=csv' do
66
+ key 'iso_3166_code', :field_name => 'country_iso_3166_code'
67
+ store 'electricity_emission_factor', :synthesize => lambda { |row|
68
+ (
69
+ row['electricity_co2_emission_factor'].to_f +
70
+ row['electricity_ch4_emission_factor'].to_f +
71
+ row['electricity_n2o_emission_factor'].to_f
72
+ ) / (1 - row['loss_factor'].to_f) }, :units_field_name => 'electricity_ch4_emission_factor_units'
73
+ end
74
+
75
+ process "Ensure EgridSubregion is populated" do
76
+ EgridSubregion.run_data_miner!
77
+ end
78
+
79
+ process "Derive average US electricity emission factor from eGRID" do
80
+ us = united_states
81
+ subregion = EgridSubregion.find_by_abbreviation 'US'
82
+ us.electricity_emission_factor =
83
+ (
84
+ subregion.electricity_co2_emission_factor +
85
+ subregion.electricity_ch4_emission_factor +
86
+ subregion.electricity_n2o_emission_factor
87
+ ) / (1 - subregion.egrid_region.loss_factor)
88
+ us.electricity_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour'
89
+ us.save!
90
+ end
91
+
92
+ # FLIGHT
93
+ import "country-specific flight route inefficiency factors derived from Kettunen et al. (2005)",
94
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdEJoRVBZaGhnUmlhX240VXE3X0F3WkE&output=csv' do
95
+ key 'iso_3166_code'
96
+ store 'flight_route_inefficiency_factor'
97
+ end
98
+
99
+ # HOSPITALITY
100
+ process "Ensure CountryLodgingClass is populated" do
101
+ CountryLodgingClass.run_data_miner!
102
+ end
103
+
104
+ process "Derive average hotel characteristics from CountryLodgingClass" do
105
+ find_each do |country|
106
+ if country.lodging_classes.any?
107
+ country.lodging_natural_gas_intensity = country.lodging_classes.weighted_average(:natural_gas_intensity)
108
+ country.lodging_natural_gas_intensity_units = 'cubic_metres_per_room_night' # FIXME TODO derive this
109
+ country.lodging_fuel_oil_intensity = country.lodging_classes.weighted_average(:fuel_oil_intensity)
110
+ country.lodging_fuel_oil_intensity_units = 'gallons_per_room_night' # FIXME TODO derive this
111
+ country.lodging_electricity_intensity = country.lodging_classes.weighted_average(:electricity_intensity)
112
+ country.lodging_electricity_intensity_units = 'kilowatt_hours_per_room_night' # FIXME TODO derive this
113
+ country.lodging_district_heat_intensity = country.lodging_classes.weighted_average(:district_heat_intensity)
114
+ country.lodging_district_heat_intensity_units = 'megajoules_per_room_night' # FIXME TODO derive this
115
+ country.save!
116
+ end
117
+ end
118
+ end
119
+
120
+ # RAIL
121
+ process "Ensure RailCompany and RailFuel are populated" do
66
122
  RailCompany.run_data_miner!
123
+ RailFuel.run_data_miner!
67
124
  end
68
125
 
69
126
  process "Calculate rail passengers, trip distance, and speed from RailCompany" do
@@ -87,10 +144,6 @@ Country.class_eval do
87
144
  store 'rail_trip_co2_emission_factor', :units_field_name => 'rail_trip_co2_emission_factor_units'
88
145
  end
89
146
 
90
- process "Ensure RailFuel is populated" do
91
- RailFuel.run_data_miner!
92
- end
93
-
94
147
  process "Unit conversion for European rail diesel intensity" do
95
148
  diesel = RailFuel.find_by_name("diesel")
96
149
  where(:rail_trip_diesel_intensity_units => 'grams_per_passenger_kilometre').update_all(%{
@@ -107,7 +160,7 @@ Country.class_eval do
107
160
  end
108
161
 
109
162
  process "Derive US rail fuel and emission data from RailCompany" do
110
- country = Country.united_states
163
+ country = united_states
111
164
  country.rail_trip_electricity_intensity = country.rail_companies.weighted_average(:electricity_intensity, :weighted_by => :passengers)
112
165
  country.rail_trip_electricity_intensity_units = 'kilowatt_hours_per_passenger_kilometre'
113
166
  country.rail_trip_diesel_intensity = country.rail_companies.weighted_average(:diesel_intensity, :weighted_by => :passengers)
@@ -116,7 +169,5 @@ Country.class_eval do
116
169
  country.rail_trip_co2_emission_factor_units = 'kilograms_per_passenger_kilometre'
117
170
  country.save!
118
171
  end
119
-
120
- # FIXME TODO verify this
121
172
  end
122
173
  end