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.
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
@@ -1,56 +1,36 @@
1
1
  require 'earth/fuel/data_miner'
2
+
2
3
  EgridSubregion.class_eval do
3
4
  data_miner do
4
- import "eGRID 2010 subregions and electricity emission factors",
5
- :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2010_Version1-1_xls_only.zip',
6
- :filename => 'eGRID2010V1_1_year07_AGGREGATION.xls',
7
- :sheet => 'SRL07',
8
- :skip => 4,
9
- :select => proc { |row| row['SEQSRL07'].to_i.between?(1, 26) } do
10
- key 'abbreviation', :field_name => 'SUBRGN'
11
- store 'name', :field_name => 'SRNAME'
5
+ process "Start from scratch" do
6
+ delete_all
7
+ end
8
+
9
+ import "eGRID 2012 subregion data",
10
+ :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2012V1_0_year09_DATA.xls',
11
+ :sheet => 'SRL09',
12
+ :skip => 4 do
13
+ key 'abbreviation', :field_name => 'SUBRGN'
14
+ store 'name', :field_name => 'SRNAME'
12
15
  store 'nerc_abbreviation', :field_name => 'NERC'
13
16
  store 'net_generation', :field_name => 'SRNGENAN', :units => :megawatt_hours
14
- store 'electricity_co2_emission_factor', :field_name => 'SRCO2RTA', :units => :pounds_per_megawatt_hour
15
- store 'electricity_co2_biogenic_emission_factor', :static => '0.0', :units => :kilograms_per_kilowatt_hour
16
- store 'electricity_ch4_emission_factor', :field_name => 'SRCH4RTA', :units => :pounds_per_gigawatt_hour
17
- store 'electricity_n2o_emission_factor', :field_name => 'SRN2ORTA', :units => :pounds_per_gigawatt_hour
17
+ store 'co2_emission_factor', :field_name => 'SRCO2RTA', :units => :pounds_per_megawatt_hour
18
+ store 'co2_biogenic_emission_factor', :static => '0.0', :units => :kilograms_per_kilowatt_hour
19
+ store 'ch4_emission_factor', :field_name => 'SRCH4RTA', :units => :pounds_per_gigawatt_hour
20
+ store 'n2o_emission_factor', :field_name => 'SRN2ORTA', :units => :pounds_per_gigawatt_hour
18
21
  end
19
22
 
20
- # FIXME TODO once 'US' subregion is no longer needed remove it from this source file
21
23
  import "eGRID subregion to region associations",
22
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGRORTJNSWRMQ1puRVprYlAtZHhDaFE&output=csv' do
24
+ :url => "file://#{Earth::DATA_DIR}/locality/egrid_relationships.csv" do
23
25
  key 'abbreviation'
24
26
  store 'egrid_region_name'
25
27
  end
26
28
 
27
- # DEPRECATED but don't remove until confirmed that all emitters use EgridSubregion.fallback rather than EgridSubregion.find_by_abbreviation 'US'
28
- # (ElectricityUse and Meeting)
29
- process "Calculate national averages" do
30
- us_average = find_by_abbreviation 'US'
31
- subregions = where("abbreviation != 'US'")
32
-
33
- us_average.name = 'United States'
34
- us_average.nerc_abbreviation = 'US'
35
- us_average.egrid_region_name = 'US'
36
- us_average.net_generation = subregions.sum(:net_generation)
37
- us_average.electricity_co2_emission_factor = subregions.weighted_average(:electricity_co2_emission_factor, :weighted_by => :net_generation)
38
- us_average.electricity_co2_biogenic_emission_factor = subregions.weighted_average(:electricity_co2_biogenic_emission_factor, :weighted_by => :net_generation)
39
- us_average.electricity_ch4_emission_factor = subregions.weighted_average(:electricity_ch4_emission_factor, :weighted_by => :net_generation)
40
- us_average.electricity_n2o_emission_factor = subregions.weighted_average(:electricity_n2o_emission_factor, :weighted_by => :net_generation)
41
- us_average.net_generation_units = 'megawatt_hours'
42
- us_average.electricity_co2_emission_factor_units = 'pounds_per_megawatt_hour'
43
- us_average.electricity_co2_biogenic_emission_factor_units = 'kilograms_per_kilowatt_hour'
44
- us_average.electricity_ch4_emission_factor_units = 'pounds_per_gigawatt_hour'
45
- us_average.electricity_n2o_emission_factor_units = 'pounds_per_gigawatt_hour'
46
- us_average.save!
47
- end
48
-
49
- process "Convert co2 emission factor to metric units" do
29
+ process "Convert co2 emission factors to metric units" do
50
30
  conversion_factor = 1.pounds.to(:kilograms) / 1_000.0 # kg / lbs * MWh / kWh
51
- where(:electricity_co2_emission_factor_units => 'pounds_per_megawatt_hour').update_all(%{
52
- electricity_co2_emission_factor = 1.0 * electricity_co2_emission_factor * #{conversion_factor},
53
- electricity_co2_emission_factor_units = 'kilograms_per_kilowatt_hour'
31
+ where(:co2_emission_factor_units => 'pounds_per_megawatt_hour').update_all(%{
32
+ co2_emission_factor = co2_emission_factor * #{conversion_factor},
33
+ co2_emission_factor_units = 'kilograms_per_kilowatt_hour'
54
34
  })
55
35
  end
56
36
 
@@ -61,24 +41,24 @@ EgridSubregion.class_eval do
61
41
  process "Convert ch4 emission factor to metric units and co2e" do
62
42
  conversion_factor = 1.pounds.to(:kilograms) / 1_000_000.0 # kg / lbs * GWh / kWh
63
43
  gwp = GreenhouseGas[:ch4].global_warming_potential
64
- where(:electricity_ch4_emission_factor_units => 'pounds_per_gigawatt_hour').update_all(%{
65
- electricity_ch4_emission_factor = 1.0 * electricity_ch4_emission_factor * #{conversion_factor} * #{gwp},
66
- electricity_ch4_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour'
44
+ where(:ch4_emission_factor_units => 'pounds_per_gigawatt_hour').update_all(%{
45
+ ch4_emission_factor = ch4_emission_factor * #{conversion_factor} * #{gwp},
46
+ ch4_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour'
67
47
  })
68
48
  end
69
49
 
70
50
  process "Convert n2o emission factor to metric units and co2e" do
71
51
  conversion_factor = 1.pounds.to(:kilograms) / 1_000_000.0 # kg / lbs * GWh / kWh
72
52
  gwp = GreenhouseGas[:n2o].global_warming_potential
73
- where(:electricity_n2o_emission_factor_units => 'pounds_per_gigawatt_hour').update_all(%{
74
- electricity_n2o_emission_factor = 1.0 * electricity_n2o_emission_factor * #{conversion_factor} * #{gwp},
75
- electricity_n2o_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour'
53
+ where(:n2o_emission_factor_units => 'pounds_per_gigawatt_hour').update_all(%{
54
+ n2o_emission_factor = n2o_emission_factor * #{conversion_factor} * #{gwp},
55
+ n2o_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour'
76
56
  })
77
57
  end
78
58
 
79
59
  process "Calculate combined emission factor" do
80
60
  update_all(%{
81
- electricity_emission_factor = 1.0 * electricity_co2_emission_factor + electricity_ch4_emission_factor + electricity_n2o_emission_factor,
61
+ electricity_emission_factor = co2_emission_factor + ch4_emission_factor + n2o_emission_factor,
82
62
  electricity_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour'
83
63
  })
84
64
  end
@@ -1,4 +1,4 @@
1
- require ::File.join(Earth::VENDOR_DIR, 'geokit-rails', 'lib', 'geokit-rails')
1
+ require ::File.join(Earth::VENDOR_DIR, 'geokit-rails', 'lib', 'geokit-rails') # for acts_as_mappable
2
2
 
3
3
  class ZipCode < ActiveRecord::Base
4
4
  self.primary_key = "name"
@@ -13,10 +13,12 @@ class ZipCode < ActiveRecord::Base
13
13
  Country.united_states
14
14
  end
15
15
 
16
+ # Used by LodgingProperty custom find to find properties near to a zip code
16
17
  def latitude_longitude
17
18
  [latitude, longitude]
18
19
  end
19
20
 
21
+ # Used by LodgingProperty custom find to find properties near to a zip code
20
22
  acts_as_mappable :default_units => :kilometres,
21
23
  :lat_column_name => :latitude,
22
24
  :lng_column_name => :longitude
@@ -29,8 +31,7 @@ class ZipCode < ActiveRecord::Base
29
31
  col :egrid_subregion_abbreviation
30
32
  col :climate_division_name
31
33
  col :population, :type => :integer
32
-
33
- warn_if_nonexistent_owner_except :egrid_subregion
34
-
34
+
35
35
  warn_unless_size 43770
36
+ warn_if_nonexistent_owner_except :egrid_subregion
36
37
  end
@@ -1,15 +1,11 @@
1
- # FIXME TODO try to clean up this data
2
1
  ZipCode.class_eval do
3
- # sabshere 9/20/10 this isn't called anywhere
4
- # def set_latitude_and_longitude
5
- # return if latitude.present? and longitude.present?
6
- # a = Geokit::Geocoders::YahooGeocoder.geocode name
7
- # update_attributes! :latitude => a.lat, :longitude => a.lng
8
- # end
9
-
10
2
  data_miner do
3
+ process "Start from scratch" do
4
+ delete_all
5
+ end
6
+
11
7
  import 'the Mapping Hacks zipcode database',
12
- :url => 'http://archive.data.brighterplanet.com/zipcode.zip', # http://mappinghacks.com/data/zipcode.zip'
8
+ :url => 'http://mappinghacks.com/data/zipcode.zip', # http://archive.data.brighterplanet.com/zipcode.zip
13
9
  :filename => 'zipcode.csv' do
14
10
  key 'name', :field_name => 'zip', :sprintf => '%05d'
15
11
  store 'state_postal_abbreviation', :field_name => 'state'
@@ -19,7 +15,7 @@ ZipCode.class_eval do
19
15
  end
20
16
 
21
17
  import 'a list of zipcode states and eGRID Subregions from the US EPA',
22
- :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/Power_Profiler_Zipcode_Tool_v3-2.xlsx',
18
+ :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/Power_Profiler_Zipcode_Tool_v4-0.xlsx',
23
19
  :sheet => 'Zip-subregion' do
24
20
  key 'name', :field_name => 'ZIP (character)'
25
21
  store 'state_postal_abbreviation', :field_name => 'State'
@@ -27,7 +23,7 @@ ZipCode.class_eval do
27
23
  end
28
24
 
29
25
  import 'a Brighter Planet-created list of zipcode Climate Divisions',
30
- :url => 'http://static.brighterplanet.com/science/data/geography/zip_code_name-climate_division_name.csv' do
26
+ :url => "file://#{Earth::DATA_DIR}/locality/zip_climate_divisions.csv" do
31
27
  key 'name', :field_name => 'zip_code_name', :sprintf => '%05d'
32
28
  store 'climate_division_name'
33
29
  end
@@ -40,10 +36,25 @@ ZipCode.class_eval do
40
36
  end
41
37
 
42
38
  import 'misc zip code data not included in other sources',
43
- :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdHFYaUE1cEdHTzZCcTFQOEZOTGVUemc&output=csv' do
39
+ :url => "file://#{Earth::DATA_DIR}/locality/misc_zip_data.csv" do
44
40
  key 'name', :sprintf => '%05d'
45
- store 'state_postal_abbreviation'
46
- store 'egrid_subregion_abbreviation'
41
+ store 'state_postal_abbreviation', :nullify => true
42
+ store 'egrid_subregion_abbreviation', :nullify => true
47
43
  end
44
+
45
+ process "Derive missing state postal abbreviations from climate division name" do
46
+ where("state_postal_abbreviation IS NULL AND climate_division_name IS NOT NULL").update_all %{
47
+ state_postal_abbreviation = LEFT(climate_division_name, 2)
48
+ }
49
+ end
50
+
51
+ # FIXME TODO figure out how to speed this up then re-enable it
52
+ # process "Look up missing latitude and longitude" do
53
+ # where("latitude IS NULL OR longitude IS NULL").each do |zip|
54
+ # if (location = Geokit::Geocoders::MultiGeocoder.geocode zip.name).success
55
+ # zip.update_attributes! :latitude => location.lat, :longitude => location.lng
56
+ # end
57
+ # end
58
+ # end
48
59
  end
49
60
  end
@@ -90,7 +90,7 @@ RailCompany.class_eval do
90
90
  end
91
91
 
92
92
  if company.electricity_intensity.present?
93
- company.co2_emission_factor += company.electricity_intensity * (EgridSubregion.fallback.electricity_co2_emission_factor / (1 - EgridRegion.fallback.loss_factor))
93
+ company.co2_emission_factor += company.electricity_intensity * (EgridSubregion.fallback.co2_emission_factor / (1 - EgridRegion.fallback.loss_factor))
94
94
  end
95
95
 
96
96
  company.co2_emission_factor_units = 'kilograms_per_passenger_kilometre'
data/lib/earth/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Earth
2
- VERSION = "0.12.0"
2
+ VERSION = "0.12.1"
3
3
  end
@@ -16,7 +16,7 @@ describe AutomobileActivityYearTypeFuel do
16
16
  describe 'verify', :sanity => true do
17
17
  it { AAYTF.count.should == 120 }
18
18
  it { AAYTF.where(:distance => nil).count.should == 0 }
19
- it { AAYTF.where("fuel_common_name != 'alternative' AND fuel_consumption IS NULL").count.should == 0 }
19
+ it { AAYTF.where("fuel_group != 'alternative' AND fuel_consumption IS NULL").count.should == 0 }
20
20
  end
21
21
 
22
22
  describe '.latest' do
@@ -54,7 +54,7 @@ describe AutomobileFuel do
54
54
 
55
55
  it "all grades of gasoline should have same annual distance and emission factors" do
56
56
  AutomobileFuel.where("name LIKE '%gasoline'").each do |fuel|
57
- fuel.common_name.should == 'gasoline'
57
+ fuel.group.should == 'gasoline'
58
58
  fuel.annual_distance.should == AutomobileFuel.gasoline.annual_distance
59
59
  fuel.co2_emission_factor.should == AutomobileFuel.gasoline.co2_emission_factor
60
60
  fuel.ch4_emission_factor.should == AutomobileFuel.gasoline.ch4_emission_factor
@@ -37,7 +37,7 @@ describe AutomobileTypeFuel do
37
37
  it 'should always be a match from 2009' do
38
38
  AutomobileTypeFuel.find_each do |atf|
39
39
  atf.latest_activity_year_type_fuel.type_name.should == atf.type_name
40
- atf.latest_activity_year_type_fuel.fuel_common_name.should == atf.fuel_common_name
40
+ atf.latest_activity_year_type_fuel.fuel_group.should == atf.fuel_group
41
41
  atf.latest_activity_year_type_fuel.activity_year.should == 2009
42
42
  end
43
43
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'earth/automobile/automobile_type_fuel_year_control'
3
3
 
4
4
  describe AutomobileTypeFuelYearControl do
5
- let(:test_year_control) { ATFYC.where(:type_name => 'Passenger cars', :fuel_common_name => 'gasoline', :year => 2005).first }
5
+ let(:test_year_control) { ATFYC.where(:type_name => 'Passenger cars', :fuel_group => 'gasoline', :year => 2005).first }
6
6
 
7
7
  before :all do
8
8
  Earth.init :automobile, :load_data_miner => true
@@ -19,7 +19,7 @@ describe AutomobileTypeFuelYearControl do
19
19
  it { ATFYC.count.should == 142 }
20
20
 
21
21
  it 'total travel percent should sum to 1' do
22
- ATFYC.group([:type_name, :fuel_common_name, :year]).sum(:total_travel_percent).each do |grouping_criteria, total|
22
+ ATFYC.group([:type_name, :fuel_group, :year]).sum(:total_travel_percent).each do |grouping_criteria, total|
23
23
  total.should be_within(1e-5).of(1.0)
24
24
  end
25
25
  end
@@ -27,10 +27,10 @@ describe AutomobileTypeFuelYearControl do
27
27
  it { ATFYC.where(:type_fuel_control_name => nil).count.should == 0 }
28
28
  end
29
29
 
30
- describe '.find_all_by_type_name_and_fuel_common_name_and_closest_year' do
31
- it { ATFYC.find_all_by_type_name_and_fuel_common_name_and_closest_year('Passenger cars', 'gasoline', 1980).should == ATFYC.find_all_by_type_name_and_fuel_common_name_and_year('Passenger cars', 'gasoline', 1985) }
32
- it { ATFYC.find_all_by_type_name_and_fuel_common_name_and_closest_year('Passenger cars', 'gasoline', 2005).should == ATFYC.find_all_by_type_name_and_fuel_common_name_and_year('Passenger cars', 'gasoline', 2005) }
33
- it { ATFYC.find_all_by_type_name_and_fuel_common_name_and_closest_year('Passenger cars', 'gasoline', 2012).should == ATFYC.find_all_by_type_name_and_fuel_common_name_and_year('Passenger cars', 'gasoline', 2010) }
30
+ describe '.find_all_by_type_name_and_fuel_group_and_closest_year' do
31
+ it { ATFYC.find_all_by_type_name_and_fuel_group_and_closest_year('Passenger cars', 'gasoline', 1980).should == ATFYC.find_all_by_type_name_and_fuel_group_and_year('Passenger cars', 'gasoline', 1985) }
32
+ it { ATFYC.find_all_by_type_name_and_fuel_group_and_closest_year('Passenger cars', 'gasoline', 2005).should == ATFYC.find_all_by_type_name_and_fuel_group_and_year('Passenger cars', 'gasoline', 2005) }
33
+ it { ATFYC.find_all_by_type_name_and_fuel_group_and_closest_year('Passenger cars', 'gasoline', 2012).should == ATFYC.find_all_by_type_name_and_fuel_group_and_year('Passenger cars', 'gasoline', 2010) }
34
34
  end
35
35
 
36
36
  describe '#ch4_emission_factor' do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'earth/automobile/automobile_type_fuel_year'
3
3
 
4
4
  describe AutomobileTypeFuelYear do
5
- let(:test_atfy) { ATFY.where(:type_name => 'Passenger cars', :fuel_common_name => 'gasoline', :year => 2005).first }
5
+ let(:test_atfy) { ATFY.where(:type_name => 'Passenger cars', :fuel_group => 'gasoline', :year => 2005).first }
6
6
 
7
7
  before :all do
8
8
  Earth.init :automobile, :load_data_miner => true
@@ -19,7 +19,7 @@ describe AutomobileTypeFuelYear do
19
19
  it { ATFY.count.should == 124 }
20
20
 
21
21
  it 'shares should sum to 1' do
22
- ATFY.sum(:share_of_type, :group => [:type_name, :fuel_common_name]).each do |groupers, total|
22
+ ATFY.sum(:share_of_type, :group => [:type_name, :fuel_group]).each do |groupers, total|
23
23
  total.should be_within(1e-2).of(1.0)
24
24
  end
25
25
  end
@@ -37,10 +37,10 @@ describe AutomobileTypeFuelYear do
37
37
  it { test_atfy.n2o_emission_factor_units.should == 'kilograms_co2e_per_kilometre' }
38
38
  end
39
39
 
40
- describe '.find_by_type_name_and_fuel_common_name_and_closest_year' do
41
- it { ATFY.find_by_type_name_and_fuel_common_name_and_closest_year('Passenger cars', 'gasoline', 1970).should == ATFY.find_by_type_name_and_fuel_common_name_and_year('Passenger cars', 'gasoline', 1979) }
42
- it { ATFY.find_by_type_name_and_fuel_common_name_and_closest_year('Passenger cars', 'gasoline', 2005).should == ATFY.find_by_type_name_and_fuel_common_name_and_year('Passenger cars', 'gasoline', 2005) }
43
- it { ATFY.find_by_type_name_and_fuel_common_name_and_closest_year('Passenger cars', 'gasoline', 2012).should == ATFY.find_by_type_name_and_fuel_common_name_and_year('Passenger cars', 'gasoline', 2009) }
40
+ describe '.find_by_type_name_and_fuel_group_and_closest_year' do
41
+ it { ATFY.find_by_type_name_and_fuel_group_and_closest_year('Passenger cars', 'gasoline', 1970).should == ATFY.find_by_type_name_and_fuel_group_and_year('Passenger cars', 'gasoline', 1979) }
42
+ it { ATFY.find_by_type_name_and_fuel_group_and_closest_year('Passenger cars', 'gasoline', 2005).should == ATFY.find_by_type_name_and_fuel_group_and_year('Passenger cars', 'gasoline', 2005) }
43
+ it { ATFY.find_by_type_name_and_fuel_group_and_closest_year('Passenger cars', 'gasoline', 2012).should == ATFY.find_by_type_name_and_fuel_group_and_year('Passenger cars', 'gasoline', 2009) }
44
44
  end
45
45
 
46
46
  describe '#type_fuel_year_controls' do
@@ -13,9 +13,7 @@ describe Industry do
13
13
  end
14
14
 
15
15
  describe "verify imported data", :sanity => true do
16
- it "should have all the data" do
17
- Industry.count.should == 2341
18
- end
16
+ it { Industry.count.should == 2341 }
19
17
  end
20
18
 
21
19
  describe "methods" do
@@ -10,55 +10,79 @@ describe Country do
10
10
  end
11
11
 
12
12
  it 'should import data' do
13
+ Country.delete_all if Country.table_exists?
13
14
  Country.run_data_miner!
14
15
  end
15
16
  end
16
17
 
17
18
  describe 'verify imported data', :sanity => true do
18
- it 'should have all the data' do
19
- Country.count.should == 249
20
- end
21
- it 'uses UTF-8 encoding' do
22
- Country.find('AX').name.should == "Åland Islands"
23
- Country.find('CI').name.should == "Côte d'Ivoire"
19
+ let(:us) { Country.united_states }
20
+ let(:uk) { Country.find 'GB' }
21
+
22
+ it { Country.count.should == 249 }
23
+
24
+ describe 'uses UTF-8 encoding' do
25
+ it { Country.find('AX').name.should == "Åland Islands" }
26
+ it { Country.find('CI').name.should == "Côte d'Ivoire" }
24
27
  end
25
28
 
26
- it 'has degree day data for 175 countries' do
27
- Country.where('heating_degree_days IS NOT NULL AND cooling_degree_days IS NOT NULL').count.should == 173
29
+ it { Country.where('heating_degree_days >= 0 AND cooling_degree_days > 0').count.should == 173 }
30
+
31
+ describe 'US automobile data' do
32
+ it { us.automobile_urbanity.should == 0.43 }
33
+ it { us.automobile_city_speed.should be_within(5e-5).of(32.0259) }
34
+ it { us.automobile_highway_speed.should be_within(5e-5).of(91.8935) }
35
+ it { us.automobile_trip_distance.should be_within(5e-5).of(16.3348) }
28
36
  end
29
37
 
30
- it 'has valid electricity emission factor and electricity loss factor for most countries' do
31
- Country.where('electricity_emission_factor IS NOT NULL').count.should == 136
32
- Country.where(:electricity_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour').count.should == 136
33
- Country.where('electricity_loss_factor IS NOT NULL').count.should == 136
34
-
35
- Country.minimum(:electricity_emission_factor).should >= 0.0
36
- Country.minimum(:electricity_loss_factor).should >= 0.0
37
- Country.maximum(:electricity_loss_factor).should < 0.3
38
+ describe 'electricity data' do
39
+ it { Country.where('electricity_emission_factor >= 0').count.should == 136 }
40
+ it { Country.where(:electricity_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour').count.should == 136 }
41
+ it { Country.where('electricity_loss_factor >= 0').count.should == 136 }
42
+ it { Country.maximum(:electricity_loss_factor).should < 0.3 }
38
43
 
39
- us = Country.united_states
40
- us.electricity_emission_factor.should be_within(0.00001).of(0.58946)
41
- us.electricity_emission_factor_units.should == 'kilograms_co2e_per_kilowatt_hour'
42
- us.electricity_loss_factor.should be_within(0.001).of(0.062)
44
+ # spot checks
45
+ it { us.electricity_emission_factor.should be_within(5e-6).of(0.55437) }
46
+ it { us.electricity_emission_factor_units.should == 'kilograms_co2e_per_kilowatt_hour' }
47
+ it { us.electricity_loss_factor.should be_within(5e-6).of(0.06503) }
43
48
 
44
- uk = Country.find 'GB'
45
- uk.electricity_emission_factor.should be_within(0.00001).of(0.51020)
46
- uk.electricity_emission_factor_units.should == 'kilograms_co2e_per_kilowatt_hour'
47
- uk.electricity_loss_factor.should be_within(0.001).of(0.073)
49
+ it { uk.electricity_emission_factor.should be_within(1e-5).of(0.51020) }
50
+ it { uk.electricity_emission_factor_units.should == 'kilograms_co2e_per_kilowatt_hour' }
51
+ it { uk.electricity_loss_factor.should be_within(1e-3).of(0.073) }
48
52
  end
49
53
 
50
- it 'has fallback electricity emission factor and electricity loss factor' do
51
- fallback = Country.fallback
52
- fallback.electricity_emission_factor.should be_within(0.00001).of(0.62609)
53
- fallback.electricity_emission_factor_units.should == 'kilograms_co2e_per_kilowatt_hour'
54
- fallback.electricity_loss_factor.should be_within(0.001).of(0.096)
54
+ describe 'flight data' do
55
+ it { Country.where("flight_route_inefficiency_factor > 0").count.should == 17 }
56
+ it { us.flight_route_inefficiency_factor.should == 1.07 }
57
+ it { uk.flight_route_inefficiency_factor.should == 1.1 }
58
+ end
59
+
60
+ describe 'lodging data' do
61
+ it { us.lodging_occupancy_rate.should be_within(0.001).of(0.601) }
62
+ it { us.lodging_natural_gas_intensity.should be_within(0.00001).of(1.93316) }
63
+ it { us.lodging_natural_gas_intensity_units.should == 'cubic_metres_per_occupied_room_night' }
55
64
  end
56
65
 
57
- it 'has lodging data for the US' do
58
- us = Country.united_states
59
- us.lodging_occupancy_rate.should be_within(0.001).of(0.601)
60
- us.lodging_natural_gas_intensity.should be_within(0.00001).of(1.93316)
61
- us.lodging_natural_gas_intensity_units.should == 'cubic_metres_per_occupied_room_night'
66
+ describe 'rail data' do
67
+ it { Country.where("rail_passengers > 0").count.should == 26 }
68
+ it { Country.where("rail_trip_distance > 0").count.should == 26 }
69
+ it { Country.where("rail_trip_electricity_intensity > 0").count.should == 26 }
70
+ it { Country.where("rail_trip_diesel_intensity > 0").count.should == 26 }
71
+ it { Country.where("rail_trip_co2_emission_factor > 0").count.should == 26 }
72
+
73
+ # spot checks
74
+ it { us.rail_passengers.should == 4467000000 }
75
+ it { us.rail_trip_distance.should be_within(5e-5).of(12.9952) }
76
+ it { us.rail_speed.should be_within(5e-5).of(32.4972) }
77
+ it { us.rail_trip_electricity_intensity.should be_within(5e-5).of(0.140512) }
78
+ it { us.rail_trip_diesel_intensity.should be_within(5e-5).of(0.0194) }
79
+ it { us.rail_trip_co2_emission_factor.should be_within(5e-5).of(0.0958) }
80
+
81
+ it { uk.rail_passengers.should == 1352150000 }
82
+ it { uk.rail_trip_distance.should be_within(5e-5).of(40.6904) }
83
+ it { uk.rail_trip_electricity_intensity.should be_within(5e-5).of(0.09) }
84
+ it { uk.rail_trip_diesel_intensity.should be_within(5e-5).of(0.0028) }
85
+ it { uk.rail_trip_co2_emission_factor.should be_within(5e-5).of(0.0458) }
62
86
  end
63
87
  end
64
88
 
@@ -67,4 +91,13 @@ describe Country do
67
91
  Country.united_states.should == Country.find('US')
68
92
  end
69
93
  end
94
+
95
+ describe '.fallback' do
96
+ it 'has fallback electricity emission factor and electricity loss factor' do
97
+ fallback = Country.fallback
98
+ fallback.electricity_emission_factor.should be_within(0.00001).of(0.62609)
99
+ fallback.electricity_emission_factor_units.should == 'kilograms_co2e_per_kilowatt_hour'
100
+ fallback.electricity_loss_factor.should be_within(0.001).of(0.096)
101
+ end
102
+ end
70
103
  end