earth 0.12.3 → 0.12.4

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 (78) hide show
  1. data/Gemfile +1 -0
  2. data/TODO +1 -0
  3. data/bin/earth_tester.rb +39 -47
  4. data/data/air/airlines.csv +45 -0
  5. data/data/air/airports.csv +11 -0
  6. data/data/hospitality/lodging_classes.csv +4 -0
  7. data/data/locality/national_electricity_efs.csv +137 -0
  8. data/earth.gemspec +1 -1
  9. data/errata/airline/bts_carrier_codes_errata.csv +2 -0
  10. data/lib/earth.rb +3 -0
  11. data/lib/earth/active_record_base_class_methods.rb +25 -0
  12. data/lib/earth/air/aircraft.rb +5 -4
  13. data/lib/earth/air/airline.rb +5 -1
  14. data/lib/earth/air/airline/data_miner.rb +1 -1
  15. data/lib/earth/air/airport.rb +10 -2
  16. data/lib/earth/air/airport/data_miner.rb +11 -5
  17. data/lib/earth/air/bts_aircraft.rb +1 -1
  18. data/lib/earth/air/flight_segment.rb +14 -1
  19. data/lib/earth/air/flight_segment/data_miner.rb +6 -13
  20. data/lib/earth/automobile/automobile_activity_year/data_miner.rb +1 -1
  21. data/lib/earth/automobile/automobile_activity_year_type/data_miner.rb +1 -1
  22. data/lib/earth/automobile/automobile_fuel/data_miner.rb +5 -5
  23. data/lib/earth/automobile/automobile_make/data_miner.rb +1 -1
  24. data/lib/earth/automobile/automobile_make_model/data_miner.rb +1 -1
  25. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +1 -1
  26. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +6 -6
  27. data/lib/earth/automobile/automobile_type_fuel/data_miner.rb +2 -2
  28. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +1 -1
  29. data/lib/earth/conversions_ext.rb +7 -6
  30. data/lib/earth/fuel/fuel.rb +1 -1
  31. data/lib/earth/fuel/fuel/data_miner.rb +1 -3
  32. data/lib/earth/hospitality/commercial_building_energy_consumption_survey_response.rb +3 -1
  33. data/lib/earth/hospitality/commercial_building_energy_consumption_survey_response/data_miner.rb +44 -11
  34. data/lib/earth/hospitality/lodging_class.rb +0 -1
  35. data/lib/earth/hospitality/lodging_class/data_miner.rb +3 -9
  36. data/lib/earth/industry/merchant_category/data_miner.rb +14 -0
  37. data/lib/earth/locality/country.rb +33 -7
  38. data/lib/earth/locality/country/data_miner.rb +46 -32
  39. data/lib/earth/locality/egrid_subregion.rb +1 -1
  40. data/lib/earth/locality/egrid_subregion/data_miner.rb +16 -31
  41. data/lib/earth/locality/electricity_mix.rb +37 -0
  42. data/lib/earth/locality/electricity_mix/data_miner.rb +87 -0
  43. data/lib/earth/locality/state.rb +12 -4
  44. data/lib/earth/locality/state/data_miner.rb +33 -18
  45. data/lib/earth/locality/zip_code.rb +9 -1
  46. data/lib/earth/locality/zip_code/data_miner.rb +4 -0
  47. data/lib/earth/rail/country_rail_traction/data_miner.rb +2 -2
  48. data/lib/earth/rail/country_rail_traction_class/data_miner.rb +2 -2
  49. data/lib/earth/rail/national_transit_database_record.rb +4 -0
  50. data/lib/earth/rail/rail_company/data_miner.rb +3 -3
  51. data/lib/earth/rail/rail_fuel/data_miner.rb +1 -1
  52. data/lib/earth/residence/clothes_machine_use/data_miner.rb +1 -1
  53. data/lib/earth/residence/dishwasher_use/data_miner.rb +1 -1
  54. data/lib/earth/residence/residence_fuel_price.rb +1 -1
  55. data/lib/earth/version.rb +1 -1
  56. data/spec/earth/air/airline_spec.rb +1 -1
  57. data/spec/earth/air/airport_spec.rb +7 -0
  58. data/spec/earth/air/flight_segment_spec.rb +26 -12
  59. data/spec/earth/automobile/automobile_type_fuel_spec.rb +1 -1
  60. data/spec/earth/automobile/automobile_type_fuel_year_spec.rb +3 -3
  61. data/spec/earth/fuel/fuel_spec.rb +1 -1
  62. data/spec/earth/hospitality/commercial_building_energy_consumption_survey_response_spec.rb +32 -19
  63. data/spec/earth/hospitality/lodging_class_spec.rb +18 -0
  64. data/spec/earth/industry/mecs_energy_spec.rb +2 -2
  65. data/spec/earth/industry/mecs_ratio_spec.rb +1 -1
  66. data/spec/earth/industry/merchant_category_spec.rb +17 -0
  67. data/spec/earth/locality/country_spec.rb +32 -16
  68. data/spec/earth/locality/egrid_subregion_spec.rb +40 -27
  69. data/spec/earth/locality/electricity_mix_spec.rb +70 -0
  70. data/spec/earth/locality/state_spec.rb +10 -11
  71. data/spec/earth/locality/zip_code_spec.rb +9 -5
  72. data/spec/earth_spec.rb +4 -4
  73. data/spec/spec_helper.rb +3 -0
  74. metadata +19 -10
  75. data/lib/earth/hospitality/country_lodging_class.rb +0 -18
  76. data/lib/earth/hospitality/country_lodging_class/data_miner.rb +0 -40
  77. data/lib/earth/industry/merchant.rb +0 -9
  78. data/spec/earth/hospitality/country_lodging_class_spec.rb +0 -30
@@ -1,12 +1,20 @@
1
1
  class State < ActiveRecord::Base
2
2
  self.primary_key = "postal_abbreviation"
3
3
 
4
+ has_many :zip_codes, :foreign_key => 'state_postal_abbreviation'
4
5
  has_many :climate_divisions, :foreign_key => 'state_postal_abbreviation'
5
- has_many :zip_codes, :foreign_key => 'state_postal_abbreviation'
6
6
  belongs_to :census_division, :foreign_key => 'census_division_number'
7
7
  has_one :census_region, :through => :census_division
8
+ has_one :electricity_mix, :foreign_key => 'state_postal_abbreviation'
8
9
  belongs_to :petroleum_administration_for_defense_district, :foreign_key => 'petroleum_administration_for_defense_district_code'
9
10
 
11
+ data_miner do
12
+ process "Ensure Country and ElectricityMix are imported because they're like belongs_to associations" do
13
+ Country.run_data_miner!
14
+ ElectricityMix.run_data_miner!
15
+ end
16
+ end
17
+
10
18
  def country
11
19
  Country.united_states
12
20
  end
@@ -20,11 +28,11 @@ class State < ActiveRecord::Base
20
28
  col :electricity_emission_factor, :type => :float
21
29
  col :electricity_emission_factor_units
22
30
  col :electricity_loss_factor, :type => :float
23
-
31
+
24
32
  warn_if_nulls_except(
25
33
  :census_division_number,
26
34
  :petroleum_administration_for_defense_district_code
27
35
  )
28
-
29
- warn_unless_size 57
36
+
37
+ warn_unless_size 51
30
38
  end
@@ -1,5 +1,9 @@
1
1
  State.class_eval do
2
2
  data_miner do
3
+ process "Start from scratch" do
4
+ delete_all
5
+ end
6
+
3
7
  # state names, FIPS codes, and postal abbreviations
4
8
  import 'the U.S. Census State ANSI Code file',
5
9
  :url => 'http://www.census.gov/geo/www/ansi/state.txt',
@@ -27,34 +31,45 @@ State.class_eval do
27
31
  store 'petroleum_administration_for_defense_district_code', :field_name => 'Code'
28
32
  end
29
33
 
30
- process 'ensure ZipCode, EgridSubregion, and EgridRegion are populated' do
34
+ process 'ensure ZipCode and EgridSubregion are populated' do
31
35
  ZipCode.run_data_miner!
32
36
  EgridSubregion.run_data_miner!
33
- EgridRegion.run_data_miner!
34
37
  end
35
38
 
39
+ # DEPRECATED - when cut this can also cut electricity_emission_factor from EgridSubrgion
36
40
  process 'derive average electricity emission factor and loss factor from zip code and eGRID data' do
37
- update_all %{
38
- electricity_emission_factor = (
39
- SELECT SUM(zip_codes.population * egrid_subregions.electricity_emission_factor) / SUM(zip_codes.population)
40
- FROM zip_codes
41
- INNER JOIN egrid_subregions ON egrid_subregions.abbreviation = zip_codes.egrid_subregion_abbreviation
42
- WHERE zip_codes.state_postal_abbreviation = states.postal_abbreviation
43
- ),
44
- electricity_emission_factor_units = 'kilograms_co2e_per_kilowatt_hour',
45
- electricity_loss_factor = (
46
- SELECT SUM(zip_codes.population * egrid_regions.loss_factor) / SUM(zip_codes.population)
47
- FROM zip_codes
48
- INNER JOIN egrid_subregions ON egrid_subregions.abbreviation = zip_codes.egrid_subregion_abbreviation
49
- INNER JOIN egrid_regions ON egrid_regions.name = egrid_subregions.egrid_region_name
50
- WHERE zip_codes.state_postal_abbreviation = states.postal_abbreviation
41
+ safe_find_each do |state|
42
+ sub_pops = state.zip_codes.known_subregion.sum(:population, :group => :egrid_subregion)
43
+
44
+ ef = sub_pops.inject(0) do |memo, (subregion, population)|
45
+ memo += subregion.electricity_emission_factor * population
46
+ memo
47
+ end / sub_pops.values.sum
48
+
49
+ state.update_attributes!(
50
+ :electricity_emission_factor => ef,
51
+ :electricity_emission_factor_units => 'kilograms_co2e_per_kilowatt_hour'
51
52
  )
52
- }
53
+
54
+ loss_factor = sub_pops.inject(0) do |memo, (subregion, population)|
55
+ memo += subregion.egrid_region.loss_factor * population
56
+ memo
57
+ end / sub_pops.values.sum
58
+
59
+ state.update_attributes! :electricity_loss_factor => loss_factor
60
+ end
53
61
  end
54
62
 
55
63
  # TODO import this from US census? would be slightly different: 0.7% for Alaska, 0.2% for New Mexico, etc.
56
64
  process 'derive population from zip code data' do
57
- update_all "population = (SELECT SUM(population) FROM zip_codes WHERE zip_codes.state_postal_abbreviation = states.postal_abbreviation)"
65
+ safe_find_each do |state|
66
+ state.update_attributes! :population => state.zip_codes.sum(:population)
67
+ end
68
+ end
69
+
70
+ process "Data mine Country and ElectricityMix because they're like belongs_to associations" do
71
+ Country.run_data_miner!
72
+ ElectricityMix.run_data_miner!
58
73
  end
59
74
  end
60
75
  end
@@ -3,12 +3,20 @@ require ::File.join(Earth::VENDOR_DIR, 'geokit-rails', 'lib', 'geokit-rails') #
3
3
  class ZipCode < ActiveRecord::Base
4
4
  self.primary_key = "name"
5
5
 
6
- belongs_to :egrid_subregion, :foreign_key => 'egrid_subregion_abbreviation'
7
6
  belongs_to :climate_division, :foreign_key => 'climate_division_name'
8
7
  belongs_to :state, :foreign_key => 'state_postal_abbreviation'
8
+ belongs_to :egrid_subregion, :foreign_key => 'egrid_subregion_abbreviation'
9
9
  has_many :electric_markets, :foreign_key => 'zip_code_name'
10
10
  has_many :electric_utilities, :through => :electric_markets
11
11
 
12
+ scope :known_subregion, where('egrid_subregion_abbreviation IS NOT NULL')
13
+
14
+ data_miner do
15
+ process "Ensure Country is imported because it's like a belongs_to association" do
16
+ Country.run_data_miner!
17
+ end
18
+ end
19
+
12
20
  def country
13
21
  Country.united_states
14
22
  end
@@ -48,6 +48,10 @@ ZipCode.class_eval do
48
48
  }
49
49
  end
50
50
 
51
+ process "Data mine Country because it's like a belongs_to association" do
52
+ Country.run_data_miner!
53
+ end
54
+
51
55
  # FIXME TODO figure out how to speed this up then re-enable it
52
56
  # process "Look up missing latitude and longitude" do
53
57
  # where("latitude IS NULL OR longitude IS NULL").each do |zip|
@@ -17,7 +17,7 @@ CountryRailTraction.class_eval do
17
17
  end
18
18
 
19
19
  process "Standardize diesel intensity units" do
20
- CountryRailTraction.where(:diesel_intensity_units => 'grams_per_passenger_kilometre').find_each do |record|
20
+ CountryRailTraction.where(:diesel_intensity_units => 'grams_per_passenger_kilometre').safe_find_each do |record|
21
21
  diesel = RailFuel.find_by_name("diesel")
22
22
  record.diesel_intensity = record.diesel_intensity.grams.to(:kilograms) / diesel.density
23
23
  record.diesel_intensity_units = [diesel.density_units.split("_per_")[1].pluralize, record.diesel_intensity_units.split("_per_")[1]].join('_per_')
@@ -26,7 +26,7 @@ CountryRailTraction.class_eval do
26
26
  end
27
27
 
28
28
  process "Standardize co2 emission factor units" do
29
- CountryRailTraction.where(:co2_emission_factor_units => 'grams_per_passenger_kilometre').find_each do |record|
29
+ CountryRailTraction.where(:co2_emission_factor_units => 'grams_per_passenger_kilometre').safe_find_each do |record|
30
30
  record.co2_emission_factor = record.co2_emission_factor.grams.to(:kilograms)
31
31
  record.co2_emission_factor_units = ['kilograms', record.co2_emission_factor_units.split("_per_")[1]].join('_per_')
32
32
  record.save!
@@ -18,7 +18,7 @@ CountryRailTractionClass.class_eval do
18
18
  end
19
19
 
20
20
  process "Standardize diesel intensity units" do
21
- CountryRailTractionClass.where(:diesel_intensity_units => 'grams_per_passenger_kilometre').find_each do |record|
21
+ CountryRailTractionClass.where(:diesel_intensity_units => 'grams_per_passenger_kilometre').safe_find_each do |record|
22
22
  diesel = RailFuel.find_by_name("diesel")
23
23
  record.diesel_intensity = record.diesel_intensity.grams.to(:kilograms) / diesel.density
24
24
  record.diesel_intensity_units = [diesel.density_units.split("_per_")[1].pluralize, record.diesel_intensity_units.split("_per_")[1]].join('_per_')
@@ -27,7 +27,7 @@ CountryRailTractionClass.class_eval do
27
27
  end
28
28
 
29
29
  process "Standardize co2 emission factor units" do
30
- CountryRailTractionClass.where(:co2_emission_factor_units => 'grams_per_passenger_kilometre').find_each do |record|
30
+ CountryRailTractionClass.where(:co2_emission_factor_units => 'grams_per_passenger_kilometre').safe_find_each do |record|
31
31
  record.co2_emission_factor = record.co2_emission_factor.grams.to(:kilograms)
32
32
  record.co2_emission_factor_units = ['kilograms', record.co2_emission_factor_units.split("_per_")[1]].join('_per_')
33
33
  record.save!
@@ -41,10 +41,14 @@ class NationalTransitDatabaseRecord < ActiveRecord::Base
41
41
  col :other_fuel_description
42
42
 
43
43
  warn_if_nulls_except(
44
+ :passenger_distance,
45
+ :passenger_distance_units,
44
46
  :electricity,
45
47
  :electricity_units,
46
48
  :diesel,
47
49
  :diesel_units,
50
+ :gasoline,
51
+ :gasoline_units,
48
52
  :lpg,
49
53
  :lpg_units,
50
54
  :lng,
@@ -57,7 +57,7 @@ RailCompany.class_eval do
57
57
  end
58
58
 
59
59
  process "Calculate average trip distance" do
60
- find_each do |company|
60
+ safe_find_each do |company|
61
61
  if company.passenger_distance.present? and company.passengers.present? and company.passengers > 0
62
62
  company.trip_distance = company.passenger_distance / company.passengers
63
63
  company.trip_distance_units = company.passenger_distance_units
@@ -67,7 +67,7 @@ RailCompany.class_eval do
67
67
  end
68
68
 
69
69
  process "Calculate average trip speed" do
70
- find_each do |company|
70
+ safe_find_each do |company|
71
71
  if company.train_distance.present? and company.train_time.present? and company.train_time > 0
72
72
  company.speed = company.train_distance / company.train_time
73
73
  company.speed_units = "#{company.train_distance_units}_per_#{company.train_time_units.singularize}"
@@ -82,7 +82,7 @@ RailCompany.class_eval do
82
82
  end
83
83
 
84
84
  process "Calculate co2 emission factor for US rail companies" do
85
- where(:country_iso_3166_code => 'US').find_each do |company|
85
+ where(:country_iso_3166_code => 'US').safe_find_each do |company|
86
86
  company.co2_emission_factor = 0
87
87
 
88
88
  if company.diesel_intensity.present?
@@ -15,7 +15,7 @@ RailFuel.class_eval do
15
15
  end
16
16
 
17
17
  process "Convert ch4 and n2o emission factor units to kg co2e / l" do
18
- RailFuel.find_each do |fuel|
18
+ RailFuel.safe_find_each do |fuel|
19
19
  if fuel.ch4_emission_factor_units == "grams_per_kilogram" and fuel.density_units == "kilograms_per_litre"
20
20
  fuel.ch4_emission_factor = GreenhouseGas["ch4"].global_warming_potential * fuel.density * fuel.ch4_emission_factor / 1000.0
21
21
  fuel.ch4_emission_factor_units = "kilograms_co2e_per_litre"
@@ -15,7 +15,7 @@ ClothesMachineUse.class_eval do
15
15
 
16
16
  # sabshere 5/20/10 weird that this uses cohort
17
17
  process "precalculate annual energy use" do
18
- find_each do |record|
18
+ safe_find_each do |record|
19
19
  record.annual_energy_from_electricity_for_clothes_driers = ResidentialEnergyConsumptionSurveyResponse.cohort(:clothes_machine_use_id => record.name).weighted_average :annual_energy_from_electricity_for_clothes_driers
20
20
  record.annual_energy_from_electricity_for_clothes_driers_units = 'joules'
21
21
  record.save!
@@ -15,7 +15,7 @@ DishwasherUse.class_eval do
15
15
 
16
16
  # sabshere 5/25/10 weird that this uses cohort
17
17
  process "precalculate annual energy" do
18
- find_each do |record|
18
+ safe_find_each do |record|
19
19
  record.annual_energy_from_electricity_for_dishwashers = ResidentialEnergyConsumptionSurveyResponse.cohort(:dishwasher_use_id => record.name).weighted_average :annual_energy_from_electricity_for_dishwashers
20
20
  record.annual_energy_from_electricity_for_dishwashers_units = 'joules'
21
21
  record.save!
@@ -17,5 +17,5 @@ class ResidenceFuelPrice < ActiveRecord::Base
17
17
  add_index [:price, :residence_fuel_type_name, :month, :year, :locatable_type, :locatable_id]
18
18
  add_index [:price, :residence_fuel_type_name]
19
19
 
20
- warn_unless_size 13005
20
+ warn_unless_size 13639
21
21
  end
data/lib/earth/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Earth
2
- VERSION = "0.12.3"
2
+ VERSION = "0.12.4"
3
3
  end
@@ -14,7 +14,7 @@ describe Airline do
14
14
 
15
15
  describe "verify imported data", :sanity => true do
16
16
  it "should have all the data" do
17
- Airline.count.should == 1522
17
+ Airline.count.should == 1523
18
18
  end
19
19
  end
20
20
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  require 'spec_helper'
2
4
  require 'earth/air/airport'
3
5
 
@@ -17,12 +19,17 @@ describe Airport do
17
19
  Airport.count.should == 5324
18
20
  end
19
21
 
22
+ it "should use utf-8 encoding" do
23
+ Airport.find('PDA').city.should == 'Puerto Inírida'
24
+ end
25
+
20
26
  it "should have name" do
21
27
  Airport.where(:name => nil).count.should == 0
22
28
  end
23
29
 
24
30
  it "should have city" do
25
31
  Airport.where(:city => nil).count.should == 0
32
+ Airport.where(:city => '').count.should == 0
26
33
  end
27
34
 
28
35
  it "should have country code" do
@@ -12,41 +12,55 @@ describe FlightSegment do
12
12
  end
13
13
  end
14
14
 
15
- describe "verify imported data", :sanity => true do
15
+ describe "verify imported data", :sanity => true, :flight_segment => true do
16
16
  it "should have all the data" do
17
- FlightSegment.where(:year => 2009).count.should == 403_959
18
- FlightSegment.where(:year => 2010).count.should == 421_725
19
- FlightSegment.where(:year => 2011).count.should > 357_687
17
+ FlightSegment.where(:year => 2009).count.should == 403_980
18
+ FlightSegment.where(:year => 2010).count.should == 421_884
19
+ FlightSegment.where(:year => 2011).count.should == 428_550
20
20
  end
21
21
 
22
22
  it "should have year from 2009 to present" do
23
- FlightSegment.where("year IS NULL OR year < 2009 OR year > #{::Time.now.year}").count.should == 0
23
+ FlightSegment.where("NOT(year >= 2009 AND year <= #{::Time.now.year})").count.should == 0
24
24
  end
25
25
 
26
26
  it "should have data through 7 months ago" do
27
- latest = Date.today << 7
27
+ if (today = Date.today).day < 23
28
+ latest = today << 7
29
+ else
30
+ latest = today << 6
31
+ end
28
32
  FlightSegment.maximum(:year).should == latest.year
29
33
  FlightSegment.where(:year => latest.year).maximum(:month).should == latest.month
30
34
  end
31
35
 
32
36
  it "should have origin airport in airports" do
33
- # FIXME TODO
37
+ FlightSegment.connection.select_values("SELECT DISTINCT origin_airport_iata_code FROM flight_segments").each do |code|
38
+ Airport.exists?(:iata_code => code).should == true
39
+ end
34
40
  end
35
41
 
36
42
  it "should have destination airport in airports" do
37
- # FIXME TODO
43
+ FlightSegment.connection.select_values("SELECT DISTINCT destination_airport_iata_code FROM flight_segments").each do |code|
44
+ Airport.exists?(:iata_code => code).should == true
45
+ end
38
46
  end
39
47
 
40
48
  it "should have origin country iso code in countries" do
41
- # FIXME TODO
49
+ FlightSegment.connection.select_values("SELECT DISTINCT origin_country_iso_3166_code FROM flight_segments").each do |code|
50
+ Country.exists?(:iso_3166_code => code).should == true
51
+ end
42
52
  end
43
53
 
44
54
  it "should have destination country iso code in countries" do
45
- # FIXME TODO
55
+ FlightSegment.connection.select_values("SELECT DISTINCT destination_country_iso_3166_code FROM flight_segments").each do |code|
56
+ Country.exists?(:iso_3166_code => code).should == true
57
+ end
46
58
  end
47
59
 
48
- it "should have airline name" do
49
- FlightSegment.where(:airline_name => nil).count.should == 0
60
+ it "should have airline name in airlines" do
61
+ FlightSegment.connection.select_values("SELECT DISTINCT airline_name FROM flight_segments").each do |name|
62
+ Airline.exists?(:name => name).should == true
63
+ end
50
64
  end
51
65
 
52
66
  it "should have aircraft description" do
@@ -35,7 +35,7 @@ describe AutomobileTypeFuel do
35
35
 
36
36
  describe '#latest_activity_year_type_fuel' do
37
37
  it 'should always be a match from 2009' do
38
- AutomobileTypeFuel.find_each do |atf|
38
+ AutomobileTypeFuel.safe_find_each do |atf|
39
39
  atf.latest_activity_year_type_fuel.type_name.should == atf.type_name
40
40
  atf.latest_activity_year_type_fuel.fuel_family.should == atf.fuel_family
41
41
  atf.latest_activity_year_type_fuel.activity_year.should == 2009
@@ -29,11 +29,11 @@ describe AutomobileTypeFuelYear do
29
29
  it { ATFY.where("n2o_emission_factor > 0").count.should == ATFY.count }
30
30
 
31
31
  # spot checks
32
- it { test_atfy.annual_distance.should == be_within(0.01).of(19598.59) }
32
+ it { test_atfy.annual_distance.should be_within(0.05).of(19598.6) }
33
33
  it { test_atfy.annual_distance_units.should == 'kilometres' }
34
- it { test_atfy.ch4_emission_factor.should == be_within(1e-8).of(2.286e-4) }
34
+ it { test_atfy.ch4_emission_factor.should be_within(5e-7).of(0.000229) }
35
35
  it { test_atfy.ch4_emission_factor_units.should == 'kilograms_co2e_per_kilometre' }
36
- it { test_atfy.n2o_emission_factor.should == be_within(1e-7).of(1.4688e-3) }
36
+ it { test_atfy.n2o_emission_factor.should be_within(5e-6).of(0.00147) }
37
37
  it { test_atfy.n2o_emission_factor_units.should == 'kilograms_co2e_per_kilometre' }
38
38
  end
39
39
 
@@ -20,7 +20,7 @@ describe Fuel do
20
20
 
21
21
  it 'should have a record for district heat' do
22
22
  district_heat = Fuel.find 'District Heat'
23
- district_heat.co2_emission_factor.should be_within(0.00001).of(0.0766528)
23
+ district_heat.co2_emission_factor.should be_within(5e-6).of(0.07598)
24
24
  end
25
25
 
26
26
  # TODO is there a way to check whether the value in the fuels table is NULL?
@@ -2,42 +2,55 @@ require 'spec_helper'
2
2
  require 'earth/hospitality/commercial_building_energy_consumption_survey_response'
3
3
 
4
4
  describe CommercialBuildingEnergyConsumptionSurveyResponse do
5
+ let(:cbecs) { CommercialBuildingEnergyConsumptionSurveyResponse }
6
+
5
7
  describe "when importing data", :data_miner => true do
6
8
  before do
7
9
  Earth.init :hospitality, :load_data_miner => true, :skip_parent_associations => :true
8
10
  end
9
11
 
10
12
  it "imports all naics codes" do
11
- CommercialBuildingEnergyConsumptionSurveyResponse.run_data_miner!
13
+ cbecs.run_data_miner!
12
14
  end
13
15
  end
14
16
 
15
17
  describe "verify imported data", :sanity => true do
16
- it { CommercialBuildingEnergyConsumptionSurveyResponse.count.should == 5215 }
18
+ let(:total) { cbecs.count }
19
+ let(:first_lodging) { cbecs.lodging_records.first }
17
20
 
18
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:electricity_use => nil).count.should == 0 }
19
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:electricity_energy => nil).count.should == 0 }
20
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:natural_gas_use => nil).count.should == 0 }
21
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:natural_gas_energy => nil).count.should == 0 }
22
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:fuel_oil_use => nil).count.should == 0 }
23
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:fuel_oil_energy => nil).count.should == 0 }
24
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:district_heat_use => nil).count.should == 0 }
25
- it { CommercialBuildingEnergyConsumptionSurveyResponse.where(:district_heat_energy => nil).count.should == 0 }
21
+ it { total.should == 5215 }
22
+ it { cbecs.where("electricity_use >= 0").count.should == total }
23
+ it { cbecs.where("electricity_energy >= 0").count.should == total }
24
+ it { cbecs.where("natural_gas_use >= 0").count.should == total }
25
+ it { cbecs.where("natural_gas_energy >= 0").count.should == total }
26
+ it { cbecs.where("fuel_oil_use >= 0").count.should == total }
27
+ it { cbecs.where("fuel_oil_energy >= 0").count.should == total }
28
+ it { cbecs.where("district_heat_use >= 0").count.should == total }
29
+ it { cbecs.where("district_heat_energy >= 0").count.should == total }
26
30
 
31
+ # spot check
27
32
  it "should have room nights and fuel intensities per room night for lodging_records" do
28
- spot_check = CommercialBuildingEnergyConsumptionSurveyResponse.lodging_records.first
29
- spot_check.room_nights.should == 6205
30
- spot_check.natural_gas_per_room_night.should be_within(0.00001).of(1.62234)
31
- spot_check.natural_gas_per_room_night_units.should == 'cubic_metres_per_room_night'
33
+ first_lodging.room_nights.should == 6205
34
+
35
+ first_lodging.electricity_energy.should be_within(0.5).of(221809)
36
+ first_lodging.electricity_per_room_night.should be_within(5e-4).of(37.711)
37
+ first_lodging.electricity_per_room_night_units.should == 'megajoules_per_room_night'
38
+
39
+ first_lodging.natural_gas_energy.should be_within(0.5).of(386700)
40
+ first_lodging.natural_gas_per_room_night.should be_within(5e-4).of(79.343)
41
+ first_lodging.natural_gas_per_room_night_units.should == 'megajoules_per_room_night'
42
+
43
+ first_lodging.fuel_oil_energy.should be_within(5e-4).of(0)
44
+ first_lodging.fuel_oil_per_room_night.should be_within(5e-4).of(12.97)
45
+ first_lodging.fuel_oil_per_room_night_units.should == 'megajoules_per_room_night'
32
46
  end
33
47
  end
34
48
 
35
49
  describe ".lodging_records" do
36
- it "should return only records representing Hotels, Motels, and Inns with no other activity" do
37
- lodging_records = CommercialBuildingEnergyConsumptionSurveyResponse.lodging_records
38
- lodging_records.map(&:detailed_activity).uniq.sort.should == ['Hotel', 'Motel or inn']
39
- lodging_records.map(&:first_activity).uniq.should == [nil]
40
- lodging_records.count.should == 192
50
+ it "should return records representing Hotels, Motels, and Inns with no other activity" do
51
+ cbecs.lodging_records.map(&:detailed_activity).uniq.sort.should == ['Hotel', 'Motel or inn']
52
+ cbecs.lodging_records.map(&:first_activity).uniq.should == [nil]
53
+ cbecs.lodging_records.count.should == 192
41
54
  end
42
55
  end
43
56
  end