lib/earth/locality/egrid_subregion/data_miner.rb in earth-0.11.0 vs lib/earth/locality/egrid_subregion/data_miner.rb in earth-0.11.1

- old
+ new

@@ -1,34 +1,48 @@ EgridSubregion.class_eval do data_miner do - # FIXME TODO for some reason this doesn't work... - # import "eGRID regions and electricity emission factors derived from eGRID 2007 data", - # :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2007_Version1-1.zip', - # :filename => 'eGRID2007_Version1-1/eGRID2007V1_1_year05_aggregation.xls', - # :sheet => 'SRL05', - # :skip => 3, - # :select => lambda { |row| row['eGRID2007 2005 file eGRID subregion location (operator)-based sequence number'].to_i.between?(1, 26) } do - # key 'abbreviation', :field_name => 'eGRID subregion acronym' - # store 'name', :field_name => 'eGRID subregion name associated with eGRID subregion acronym' - # store 'nerc_abbreviation', :field_name => 'NERC region acronym associated with the eGRID subregion acronym' - # store 'net_generation', :field_name => 'eGRID subregion annual net generation (MWh)', :units => 'megawatt_hours' - # store 'electricity_co2_emission_factor', :field_name => 'eGRID subregion annual CO2 output emission rate (lb/MWh)', :units => 'pounds_per_megawatt_hour' - # store 'electricity_co2_biogenic_emission_factor', :static => '0.0', :units => 'kilograms_per_kilowatt_hour' - # store 'electricity_ch4_emission_factor', :field_name => 'eGRID subregion annual CH4 output emission rate (lb/GWh)', :units => 'pounds_per_gigawatt_hour' - # store 'electricity_n2o_emission_factor', :field_name => 'eGRID subregion annual N2O output emission rate (lb/GWh)', :units => 'pounds_per_gigawatt_hour' - # end + import "eGRID 2010 subregions and electricity emission factors", + :url => 'http://www.epa.gov/cleanenergy/documents/egridzips/eGRID2010_Version1-1_xls_only.zip', + :filename => 'eGRID2010V1_1_year07_AGGREGATION.xls', + :sheet => 'SRL07', + :skip => 4, + :select => lambda { |row| row['SEQSRL07'].to_i.between?(1, 26) } do + key 'abbreviation', :field_name => 'SUBRGN' + store 'name', :field_name => 'SRNAME' + store 'nerc_abbreviation', :field_name => 'NERC' + store 'net_generation', :field_name => 'SRNGENAN', :units => :megawatt_hours + store 'electricity_co2_emission_factor', :field_name => 'SRCO2RTA', :units => :pounds_per_megawatt_hour + store 'electricity_co2_biogenic_emission_factor', :static => '0.0', :units => :kilograms_per_kilowatt_hour + store 'electricity_ch4_emission_factor', :field_name => 'SRCH4RTA', :units => :pounds_per_gigawatt_hour + store 'electricity_n2o_emission_factor', :field_name => 'SRN2ORTA', :units => :pounds_per_gigawatt_hour + end - import "eGRID subregion data", - :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGRORTJNSWRMQ1puRVprYlAtZHhDaFE&hl=en&gid=0&output=csv' do + # FIXME TODO once 'US' subregion is no longer needed remove it from this source file + import "eGRID subregion to region associations", + :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGRORTJNSWRMQ1puRVprYlAtZHhDaFE&output=csv' do key 'abbreviation' - store 'name' - store 'nerc_abbreviation' store 'egrid_region_name' - store 'net_generation', :units => 'megawatt_hours' - store 'electricity_co2_emission_factor', :field_name => 'electricty_ef_co2', :units => 'pounds_per_megawatt_hour' - store 'electricity_co2_biogenic_emission_factor', :static => '0.0', :units => 'kilograms_per_kilowatt_hour' - store 'electricity_ch4_emission_factor', :field_name => 'electricity_ef_ch4', :units => 'pounds_per_gigawatt_hour' - store 'electricity_n2o_emission_factor', :field_name => 'electricity_ef_n2o', :units => 'pounds_per_gigawatt_hour' + end + + # DEPRECATED but don't remove until confirmed that all emitters use EgridSubregion.fallback rather than EgridSubregion.find_by_abbreviation 'US' + process "Calculate national averages" do + us_average = find_by_abbreviation 'US' + subregions = where("abbreviation != 'US'") + + us_average.name = 'United States' + us_average.nerc_abbreviation = 'US' + us_average.egrid_region_name = 'US' + us_average.net_generation = subregions.sum(:net_generation) + us_average.electricity_co2_emission_factor = subregions.weighted_average(:electricity_co2_emission_factor, :weighted_by => :net_generation) + us_average.electricity_co2_biogenic_emission_factor = subregions.weighted_average(:electricity_co2_biogenic_emission_factor, :weighted_by => :net_generation) + us_average.electricity_ch4_emission_factor = subregions.weighted_average(:electricity_ch4_emission_factor, :weighted_by => :net_generation) + us_average.electricity_n2o_emission_factor = subregions.weighted_average(:electricity_n2o_emission_factor, :weighted_by => :net_generation) + us_average.net_generation_units = 'megawatt_hours' + us_average.electricity_co2_emission_factor_units = 'pounds_per_megawatt_hour' + us_average.electricity_co2_biogenic_emission_factor_units = 'kilograms_per_kilowatt_hour' + us_average.electricity_ch4_emission_factor_units = 'pounds_per_gigawatt_hour' + us_average.electricity_n2o_emission_factor_units = 'pounds_per_gigawatt_hour' + us_average.save! end process "Convert co2 emission factor to metric units" do conversion_factor = 1.pounds.to(:kilograms) / 1_000.0 # kg / lbs * MWh / kWh where(:electricity_co2_emission_factor_units => 'pounds_per_megawatt_hour').update_all(%{