class MecsEnergy
Constants
- FUELS
- TABLE_STRUCTURE
Public Class Methods
find_by_naics_code(code)
click to toggle source
Find the first record whose naics_code matches code and that has valid fuel ratios. If no record found chop off the last character of code and try again, and so on.
# File lib/earth/industry/mecs_energy.rb, line 44 def self.find_by_naics_code(code) find_by_naics_code_and_census_region_number(code, nil) end
find_by_naics_code_and_census_region_number(code, number, original_number = number)
click to toggle source
Find the first record whose census_region_number matches number, whose naics_code matches code, and that has valid fuel ratios. If none found and we know census region number, try looking nationwide If none found and looking nationwide, chop off the last character of code and try again looking in census region And so on
# File lib/earth/industry/mecs_energy.rb, line 52 def self.find_by_naics_code_and_census_region_number(code, number, original_number = number) if code.blank? record = nil else code = Industry.format_naics_code code candidate = where(:census_region_number => number, :naics_code => code).first if candidate.try(:fuel_ratios).present? record = candidate elsif number.present? record = find_by_naics_code_and_census_region_number(code, nil, original_number) else record = find_by_naics_code_and_census_region_number(code[0..-2], original_number) end end record end
Public Instance Methods
fuel_ratios()
click to toggle source
# File lib/earth/industry/mecs_energy.rb, line 70 def fuel_ratios # Don't return a ratio if reported total energy was withheld if energy.to_f > 0 # Calculate the sum of all fuels and note if any were withheld withheld = false fuels_sum = MecsEnergy::FUELS.inject(0) do |sum, fuel| if (v = send(fuel)).nil? withheld = true else sum += v end sum end if energy > fuels_sum and withheld # If energy > sum of all fuels and some fuels were withheld, calculate fuel ratios as fraction of energy # and attribute the disparity between energy and sum of all fuels to the dirtiest fuel that was withheld ratios = MecsEnergy::FUELS.inject({}) do |memo, fuel| memo[fuel] = if (v = send(fuel)).present? v / energy else nil end memo end dirtiest_withheld = ([:coal, :other_fuel, :coke_and_breeze, :residual_fuel_oil, :distillate_fuel_oil, :lpg_and_ngl, :natural_gas] & ratios.select{|k,v| v.nil?}.keys).first ratios[dirtiest_withheld] = (energy - fuels_sum) / energy ratios.delete_if do |fuel, ratio| ratio.to_f == 0.0 end else # Otherwise calculate ratios as fraction of sum of all fuels, skipping any fuels that were withheld ratios = MecsEnergy::FUELS.inject({}) do |memo, fuel| if (v = send(fuel).to_f) > 0 memo[fuel] = v / fuels_sum end memo end ratios.keys.any? ? ratios : nil end end end