def print_result(result, minyear, maxyear): data = {} sort = {} for row in result: year = row[0] industry = row[1] value = row[2] percent = row[3] if industry not in data: data[industry] = {} data[industry][year] = (value, percent) if year == maxyear: sort[percent] = industry for percent in sorted(sort.keys(), reverse=True): industry = sort[percent] if industry in sector_titles: ind_name = sector_titles[industry] elif "sec" + industry in sector_titles: ind_name = sector_titles["sec" + industry] else: print(industry) data_i = data[industry][minyear] data_f = data[industry][maxyear] if data_f[1] >= 1: # suppress sectors with <1% of total print(ind_name.ljust(20) + " & %s & %.1f & %s & %.1f \\NN" % (utils.add_commas(data_i[0]), data_i[1], utils.add_commas(data_f[0]), data_f[1]))
def print_result(result, minyear, maxyear): data = {} sort = {} for row in result: year = row[0] industry = row[1] value = row[2] percent = row[3] if industry not in data: data[industry] = {} data[industry][year] = (value, percent) if year == maxyear: sort[percent] = industry for percent in sorted(sort.keys(), reverse=True): industry = sort[percent] if industry in sector_titles: ind_name = sector_titles[industry] elif "sec" + industry in sector_titles: ind_name = sector_titles["sec" + industry] else: print(industry) data_i = data[industry][minyear] data_f = data[industry][maxyear] if data_f[1] >= 1: # suppress sectors with <1% of total print( ind_name.ljust(20) + " & %s & %.1f & %s & %.1f \\NN" % (utils.add_commas(data_i[0]), data_i[1], utils.add_commas(data_f[0]), data_f[1]))
def do_import_table(): minyear = min(config.STUDY_YEARS) maxyear = max(config.STUDY_YEARS) sector = 'CONS_h' fd = {} fd_imports = {} for year in (minyear, maxyear): strings = { "schema": config.WIOD_SCHEMA, "year": year, } stmt = db.prepare( """SELECT country, sum(value) FROM %(schema)s.niot_%(year)d WHERE to_ind = $1 AND is_import = $2 GROUP BY country""" % strings) fd[year] = {} fd_imports[year] = {} for (country, value) in stmt(sector, True): fd_imports[year][country] = value fd[year][country] = value for (country, value) in stmt(sector, False): fd[year][country] += value shares = {} for (country, total) in fd[maxyear].items(): share = fd_imports[maxyear][country] / total shares[share] = country sorted_shares = sorted(shares.keys(), reverse=True) midpoint = int(len(sorted_shares) / 2) for i in range(midpoint): values = [] for index in (i, i + midpoint): country = shares[sorted_shares[index]] minval = imfdata.convert_to_2005( fd_imports[minyear][country], country, minyear) maxval = imfdata.convert_to_2005( fd_imports[maxyear][country], country, maxyear) minshare = fd_imports[minyear][country] / fd[minyear][country] maxshare = fd_imports[maxyear][country] / fd[maxyear][country] values += [ config.countries[country], utils.add_commas(minval), utils.add_commas(maxval), "%.1f" % (minshare * 100), "%.1f" % (maxshare * 100), ""] # want blank space between two halves values.pop() # remove trailing empty string print(" & ".join(values) + " \\NN")
def get_key_title(self, key): country = config.countries[key] size = self.data["size"][key] style = self.data["style"][key] if style not in (5, 7, 9, 11, 13, 15): size = -size return "%s: %s" % (country, utils.add_commas(size))
def print_wiod_compare(): usa97 = wiod_comparison_data[1997] usa02 = wiod_comparison_data[2002] envgen97 = wiod.common.envgen_for_year(1997, ["FC_HH"]) envgen97.set_condition_args("USA") wiod97 = envgen97.get_env_vector("CO2") envgen02 = wiod.common.envgen_for_year(2002, ["FC_HH"]) envgen02.set_condition_args("USA") wiod02 = envgen02.get_env_vector("CO2") sectors = sorted(envgen97.get_sectors()) if "FC_HH" not in sectors: sectors.append("FC_HH") for sector in sectors: short_sector = sector.replace("sec", "") if short_sector in usa97: label = wiod_code_map.sector_title(short_sector) vals = [] vals.append(usa97[short_sector]["tons"] / 1000) vals.append(wiod97.get_element(sector)) vals.append(usa02[short_sector]["tons"] / 1000) vals.append(wiod02.get_element(sector)) row = [label] + [utils.add_commas(val) for val in vals] print(" & ".join(row) + " \\NN")
def describe_balance_intensity(): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) balance_ratio = balance.divide(all_total) country_name = config.countries[base_country] years = [str(minyear), str(maxyear)] fields = [] for y in years: balance_val = balance.get_element("sum", y) ratio_val = balance_ratio.get_element("sum", y) (gdp_val, env_val, intensity) = \ common.get_efficiency(base_country, int(y)) if int(y) in balance_plots: plot = balance_plots[int(y)] # ratio = exports to imports plot.set_value("2 ratio", base_country, ratio_val) plot.set_value("1 intensity", base_country, intensity) if int(y) in worldmap: worldmap[int(y)].set_country_value(base_country, balance_val) fields.append(utils.add_commas(balance_val)) fields.append("%.2f" % ratio_val) fields.append("%.2f" % intensity) print(country_name.ljust(15) + " & " + " & ".join(fields) + " \\NN")
def describe_balance(ratios=False): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) country_name = config.countries[base_country] oddyears = [ str(y) for y in filter(lambda x: x % 2 == 1, config.STUDY_YEARS) ] if ratios: balance_ratio = balance.divide(all_total) print( country_name.ljust(15) + " & " + " & ".join([("%.2f" % balance_ratio.get_element("sum", y)).rjust(6) for y in oddyears]) + " \\\\") else: print( country_name.ljust(15) + " & " + " & ".join([ utils.add_commas(balance.get_element("sum", y)).rjust(9) for y in oddyears ]) + " \\\\")
def format_vals(i): p = percents[i] if p < 10: spacer = " ~ " else: spacer = " " return utils.add_commas(vals[i]) + \ spacer + ("(%.1f\\%%)" % p) + \ " & " + "%.3f" % ivals[i]
def do_overview_table(sortby): minyear = min(config.STUDY_YEARS) maxyear = max(config.STUDY_YEARS) data = {} reverse_data = {} for (country, name) in config.countries.items(): (env_i, gdp_i, intensity_i) = common.get_efficiency( country, minyear, "env", "gdp") (env_f, gdp_f, intensity_f) = common.get_efficiency( country, maxyear, "env", "gdp") if sortby == "growth": pop_i = common.get_national_value(country, minyear, "pop") pop_f = common.get_national_value(country, maxyear, "pop") ppp_i = common.get_national_value(country, minyear, "ppppc") ppp_f = common.get_national_value(country, maxyear, "ppppc") percap_i = env_i / pop_i * 1000 percap_f = env_f / pop_f * 1000 growth = intensity_f - intensity_i pgrowth = percap_f - percap_i reverse_data[ppp_i] = name data[name] = [ utils.add_commas(val).rjust(10) for val in (ppp_i, ppp_f)] data[name] += [ "%.2f" % val for val in (intensity_i, intensity_f, growth, percap_i, percap_f, pgrowth)] else: # end year intensity reverse_data[intensity_f] = name data[name] = [ utils.add_commas(val).rjust(10) for val in (gdp_i, gdp_f, env_i, env_f)] data[name] += ["%.2f" % val for val in (intensity_i, intensity_f)] for key in sorted(reverse_data.keys()): country = reverse_data[key] vals = data[country] print(country.ljust(18) + " & " + " & ".join(vals) + " \\NN")
def print_co2_allyears(): headrow = ["sector"] + [str(year) for year in config.STUDY_YEARS] print(" & ".join(headrow) + " \\\\") for sector in sorted(wiod_code_map.codes.keys()): row = [wiod_code_map.sector_title(sector)] for year in config.STUDY_YEARS: agg_data = wiod_comparison_data[year] sector_data = agg_data[sector] row.append(utils.add_commas(sector_data["tons"] / 1000)) #row.append(utils.add_commas(sector_data["dollars"])) #row.append("%.3f" % (sector_data["tons"] / sector_data["dollars"])) print(" & ".join(row) + " \\NN")
def print_table(vector, is_intensities): for group in sorted(vector.keys()): numerator = vector[group] if is_intensities: denominator = pce_dollars[group] values = ["%.2f" % (numerator[year] / denominator[year] * 1000) for year in config.STUDY_YEARS] else: values = [utils.add_commas(numerator[year]) for year in config.STUDY_YEARS] print(group + " & " + " & ".join(values) + " \\NN")
def describe_balance(ratios=False): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) country_name = config.countries[base_country] oddyears = [str(y) for y in filter(lambda x: x % 2 == 1, config.STUDY_YEARS)] if ratios: balance_ratio = balance.divide(all_total) print(country_name.ljust(15) + " & " + " & ".join( [("%.2f" % balance_ratio.get_element("sum", y)).rjust(6) for y in oddyears]) + " \\\\") else: print(country_name.ljust(15) + " & " + " & ".join( [utils.add_commas(balance.get_element("sum", y)).rjust(9) for y in oddyears]) + " \\\\")
def do_kyoto_table(): minyear = min(config.STUDY_YEARS) maxyear = max(config.STUDY_YEARS) minstrings = { "schema": config.WIOD_SCHEMA, "year": minyear, "fd_sectors": sqlhelper.set_repr(config.default_fd_sectors), } maxstrings = minstrings.copy() maxstrings["year"] = maxyear envsql = """SELECT value FROM %(schema)s.env_%(year)d WHERE country = $1 AND measurement = $2 AND industry = 'total'""" envstmt_i = db.prepare(envsql % minstrings) envstmt_f = db.prepare(envsql % maxstrings) un_stmt = db.prepare( "SELECT value FROM %s.mdg_emissions" % config.UN_SCHEMA + " WHERE country = $1 AND year = $2") data = {} (eu_i, eu_f, un_eu_90, un_eu_i, un_eu_f) = (0, 0, 0, 0, 0) for (country, name) in config.countries.items(): env_i = envstmt_i(country, "CO2")[0][0] env_f = envstmt_f(country, "CO2")[0][0] percent = (env_f - env_i) / env_i * 100 (un_env_90, un_env_91, un_env_i, un_env_f, un_percent, un_percent_90) = \ (0, 0, 0, 0, None, None) result = un_stmt(country, 1990) if len(result): un_env_90 = result[0][0] else: # use 1991 as a proxy for 1990 for some countries if applicable # germany is the only annex b country that is applicable # so hopefully it won't mess up eu15 calculation too much result = un_stmt(country, 1991) if len(result): un_env_91 = result[0][0] result = un_stmt(country, minyear) if len(result): un_env_i = result[0][0] result = un_stmt(country, maxyear) if len(result): un_env_f = result[0][0] if un_env_i and un_env_f: un_percent = (un_env_f - un_env_i) / un_env_i * 100 if un_env_90 and un_env_f: un_percent_90 = (un_env_f - un_env_90) / un_env_90 * 100 data[country] = (env_i, env_f, percent, un_percent, un_percent_90) if country in config.eu15: eu_i += env_i eu_f += env_f un_eu_i += un_env_i un_eu_f += un_env_f if un_env_90: un_eu_90 += un_env_90 else: un_eu_90 += un_env_91 eu_percent = (eu_f - eu_i) / eu_i * 100 un_eu_percent = (un_eu_f - un_eu_i) / un_eu_i * 100 un_eu_percent_90 = (un_eu_f - un_eu_90) / un_eu_90 * 100 print("%s & %s & %s & %d\\%% & %.1f\\%% & %.1f\\%% & %.1f \\NN" % ("EU-15".ljust(18), utils.add_commas(eu_i).rjust(9), utils.add_commas(eu_f).rjust(9), -8, eu_percent, un_eu_percent, un_eu_percent_90)) for (target, countries) in config.annex_b_countries.items(): for country in countries: vals = data[country] if vals[4] is None: percent_90 = "" else: percent_90 = "%.1f" % vals[4] print("%s & %s & %s & %d\\%% & %.1f\\%% & %.1f & %s \\NN" % (config.countries[country].ljust(18), utils.add_commas(vals[0]).rjust(9), utils.add_commas(vals[1]).rjust(9), target, vals[2], vals[3], percent_90))