Beispiel #1
0
def do_cfact(country, env_key):
    base_year = config.STUDY_YEARS[0]
    env_series = config.env_series_names[env_key]

    deflators = Deflators()
    iogen = common.iogen_for_year(base_year)
    envgen = common.envgen_for_year(base_year)
    env_sectors = common.env_sectors_for_year(base_year)

    iogen.set_condition_args(country)
    envgen.set_condition_args(country)

    cfgen = CounterfactGenerator(iogen, envgen)
    if env_key in common_config.ENV_SERIES_TITLES:
        env_title = common_config.ENV_SERIES_TITLES[env_key]
    else:
        env_title = env_key
    cfgen.set_series_code(env_series, env_title)

    for year in config.STUDY_YEARS:
        iogen = cfgen.get_iogen()
        envgen = cfgen.get_envgen()

        iotable = "%s.indbyind_%d" % (config.WIOD_SCHEMA, year)
        iogen.set_table(iotable)
        envtable = "%s.env_%d" % (config.WIOD_SCHEMA, year)
        envgen.set_table(envtable)
        envgen.set_sectors(env_sectors)

        iogen.set_exchange_rate(imfdata.get_2005_deflator(country, year))
        #iogen.set_exchange_rate(deflators.get_gdp_deflator(year))

        cfgen.prepare(year,
                      env_series,
                      io_harmonizer=common.get_io_harmonizer(iogen))

    sector_titles = common.get_sector_names(True)
    # hack to get countries to print instead of sector names
    for key in sector_titles:
        sector_titles[key] = config.countries[country]
    cfgen.set_sector_titles(sector_titles)

    #cfgen.describe()
    for sector in intense_sectors:
        if sector == "sec26" and country == "IDN":
            continue  # this is really just for aesthetics

        values = cfgen.get_sector_values(sector)
        if country in config.eu15:
            color = "#B1E6B1"
        elif country in config.other_eu:
            color = "#9FB5F5"
        elif country in config.north_am:
            color = "#E3B668"
        elif country in config.all_asia:
            color = "#FFFF80"
        else:
            color = "#C0B3C9"

        if values["level"][-1] > 10000:
            leveldiff = values["level"][-1] - values["level"][0]
            style = 7
            if leveldiff < 0:
                style = 6

            bubblecharts[sector].set_point(country, values["intensity"][0],
                                           values["intensity"][-1],
                                           abs(leveldiff), color, style)

    #    cfgen.print_sector(sector)

    series_abbr = env_key.replace(" ", "-")
    if use_levels:
        filename = "%s_%s-abs_%d" % (series_abbr, country, base_year)
    else:
        filename = "%s_%s_%d" % (series_abbr, country, base_year)
    #title = config.countries[country]
    title = None
    (pce_result, export_result) = cfgen.counterfact(base_year,
                                                    "cfact-" + series_abbr,
                                                    filename,
                                                    title,
                                                    compact=False,
                                                    use_levels=use_levels)

    # pce
    result = cfgen.decompose_result(pce_result, base_year,
                                    max(config.STUDY_YEARS))  # A, J, L, Y

    value_pairs = [
        ("Sector intensity", result["J"]),
        ("Industry linkages", result["L"]),
        ("Final demand", result["Y"]),
    ]

    worldmaps["pce"].set_country_value(country, result["A"])
    worldmaps["pce-eu"].set_country_value(country, result["A"])

    if in_eu_rect(WorldMapPlot.get_centroid(country)) and country != "TUR":
        worldmaps["pce-eu"].add_tiny_barchart(country, value_pairs)
    else:
        worldmaps["pce"].add_tiny_barchart(country, value_pairs)

    # exports
    result = cfgen.decompose_result(export_result, base_year,
                                    max(config.STUDY_YEARS))  # A, J, L, Y

    value_pairs = [
        ("Sector intensity", result["J"]),
        ("Industry linkages", result["L"]),
        ("Final demand", result["Y"]),
    ]

    worldmaps["export"].set_country_value(country, result["A"])
    worldmaps["export-eu"].set_country_value(country, result["A"])

    if in_eu_rect(WorldMapPlot.get_centroid(country)) and country != "TUR":
        worldmaps["export-eu"].add_tiny_barchart(country, value_pairs)
    else:
        worldmaps["export"].add_tiny_barchart(country, value_pairs)
Beispiel #2
0
def trade_ratios(base_country, env_key):
    env_series = config.env_series_names[env_key]

    other_countries = sorted(config.countries.keys())
    other_countries.remove(base_country)

    yearstrings = [str(y) for y in config.STUDY_YEARS]
    exported_y = NamedMatrix(rows=other_countries, cols=yearstrings)
    exported_E = NamedMatrix(rows=other_countries, cols=yearstrings)

    def describe_exporters():
        print("Exports by country - " + env_key)

        print("dollars")
        for country in other_countries:
            formatted = [("%.0f" %
                          exported_y.get_element(country, year)).rjust(6)
                         for year in yearstrings]
            print(country, " ".join(formatted))

        print("emissions")
        for country in other_countries:
            formatted = [("%.0f" %
                          exported_E.get_element(country, year)).rjust(6)
                         for year in yearstrings]
            print(country, " ".join(formatted))

        print("intensities")
        intensities = exported_E.divide(exported_y)
        for country in other_countries:
            formatted = [("%.2f" %
                          intensities.get_element(country, year)).rjust(6)
                         for year in yearstrings]
            print(country, " ".join(formatted))

    ### prepare plots
    env_title = env_key.replace(" ", "-")
    plot_group = "import-%s-%s" % (env_title, base_country)
    plots = {}
    for sector in common.default_env_sectors:
        sector_title = common.get_industry_title(sector)
        plots[sector] = GNUPlot("%s_%s" % (env_title, sector), "",
                                #"Imports of %s - %s" % (sector_title, env_key),
                                plot_group)
    plots["import"] = GNUPlot("%s_import" % env_title, "",
                             #"All imports - %s" % env_key,
                             plot_group)
    plots["export"] = GNUPlot("%s_export" % env_title, "",
                              #"All imports - %s" % env_key,
                              plot_group)

    def create_plots():
        for (sector, plot) in plots.items():
            plot.set_series_style(
                import_dollar_series,
                "pointtype 5 linetype rgb '#88BBFF'") # hollow circle
            plot.set_series_style(
                emission_series,
                "pointtype 7 linetype rgb '#1144FF'") # solid circle
            plot.set_series_style(
                self_emission_series,
                "pointtype 12 linetype rgb '#0000FF'")
    
            plot.set_series_style(
                export_dollar_series, "pointtype 4 linetype rgb '#CC9933'")
            plot.set_series_style(
                export_emissions_series, "pointtype 7 linetype rgb '#996600'")
    
            #plot.legend("width -8")
            plot.width = 480
            plot.height = 300

            plot.write_tables()
            plot.generate_plot()

    ### trade balance matrices
    export_balance = NamedMatrix(rows=common.default_env_sectors,
                                 cols=yearstrings)
    import_balance = NamedMatrix(rows=common.default_env_sectors,
                                 cols=yearstrings)
    all_E = NamedMatrix(rows=common.default_env_sectors,
                        cols=yearstrings)

    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 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")

    ###
    iogen = common.iogen_for_year(config.STUDY_YEARS[0])
    envgen = common.envgen_for_year(config.STUDY_YEARS[0])

    strings = {"schema": config.WIOD_SCHEMA}

    for year in config.STUDY_YEARS:
        strings["year"] = year

        base_E = get_wiod_env_vector(base_country, year, env_series)
        all_E.set_column(str(year), base_E)

        base_E_sum = base_E.sum()

        iogen.set_table("%(schema)s.indbyind_%(year)d" % strings)
        iogen.set_condition_args(base_country)

        envgen.set_table("%(schema)s.env_%(year)d" % strings)
        envgen.set_condition_args(base_country)
        envgen.set_sectors(common.default_env_sectors)

        # prepare base country values
        y = iogen.get_Y()
        import_column = common.get_import_vector(iogen)
        base_imports = import_column.sum()
        base_gdp = y.sum()

        harmonizer = common.get_io_harmonizer(iogen)

        base_y = harmonizer.matrix_mult(y.get_total())
        x = iogen.get_x()

        E = envgen.get_env_vector(env_series)
        L = iogen.get_L()
        J = E.divide(harmonizer.matrix_mult(x), ignore_zero_denom=True)
        base_JsL = J.square_matrix_from_diag()\
            .matrix_mult(harmonizer).matrix_mult(L)

        exported = base_JsL.matrix_mult(y.get_exports())
        export_balance.set_column(str(year), exported)

        plots["export"].set_value(
            export_emissions_series, year, exported.sum() / base_E_sum)
        plots["export"].set_value(
            export_dollar_series, year, y.get_exports().sum() / base_gdp)

        # prepare other country values
        stmt = db.prepare("""SELECT from_sector, sum(value)
            FROM wiod_plus.%s_io_import_%d WHERE country = $1
             AND from_sector NOT IN ('ITM', 'IMP', 'Rex')
           GROUP BY from_sector""" % (base_country.lower(), year))

        imported_E = None
        imported_y = None
        domestic_E = None # emissions under domestic technology assumption

        for country in other_countries:
            envgen.set_condition_args(country)
            envgen.set_sectors(common.default_env_sectors)

            iogen.set_condition_args(country)
            sectors = iogen.get_sectors() # number of sectors varies by country
    
            E = envgen.get_env_vector(env_series)
            if E.mat() is None: # this country has no data for env series
                continue

            imports = NamedMatrix(rows=sectors, cols=["imports"])
            db_result = stmt(country)
            if not len(db_result):
                # we can't do any of the following with a blank import vector
                continue

            for row in db_result:
                imports.set_element(row[0], "imports", row[1])

            sel = common.get_io_harmonizer(iogen)
            x = iogen.get_x()
            L = iogen.get_L()

            J = E.divide(sel.matrix_mult(x), ignore_zero_denom=True)
            JsL = J.square_matrix_from_diag().matrix_mult(sel).matrix_mult(L)

            current_E = JsL.matrix_mult(imports)
            current_y = sel.matrix_mult(imports)

            # temporary dumb way to deal with sector mismatch
            compat_imports = NamedMatrix(rows=base_JsL.get_columns(),
                                         cols=["imports"])
            for col in base_JsL.get_columns():
                if col in sectors:
                    compat_imports.set_element(
                        col, "imports", imports.get_element(col))
            current_domestic_E = base_JsL.matrix_mult(compat_imports)

            imported_E = sum_if_not_none(imported_E, current_E)
            imported_y = sum_if_not_none(imported_y, current_y)
            domestic_E = sum_if_not_none(domestic_E, current_domestic_E)

            exported_y.set_element(country, str(year), imports.sum())
            exported_E.set_element(country, str(year), current_E.sum())

        # populate results table
        TradeResultsTable.insert_exports(year, base_country, exported)
        TradeResultsTable.insert_imports(year, base_country, imported_E)

        # generate import plots
        for sector in common.default_env_sectors:
            base_y_val = base_y.get_element(sector)
            if base_y_val > 0:
                plots[sector].set_value(
                    import_dollar_series, year,
                    imported_y.get_element(sector) / base_y_val)

            base_E_val = base_E.get_element(sector)
            if base_E_val > 0:
                plots[sector].set_value(
                    emission_series, year,
                    imported_E.get_element(sector) / base_E_val)
                plots[sector].set_value(
                    self_emission_series, year,
                    domestic_E.get_element(sector) / base_E_val)

        plots["import"].set_value(import_dollar_series, year,
                                  imported_y.sum() / base_y.sum())
        plots["import"].set_value(emission_series, year,
                                  imported_E.sum() / base_E_sum)
        plots["import"].set_value(self_emission_series, year,
                                  domestic_E.sum() / base_E_sum)

        if year in dta_plots and base_country in dta_countries:
            dta_plots[year].set_value("DTA",
                                      config.countries[base_country],
                                      imported_E.sum() / base_E_sum)
            dta_plots[year].set_value("No DTA",
                                      config.countries[base_country],
                                      domestic_E.sum() / base_E_sum)

        # this is for DTA vs non-DTA table
        #print(base_country, year,
        #      imported_E.sum(),
        #      domestic_E.sum(),
        #      imported_E.sum() / base_E_sum,
        #      domestic_E.sum() / base_E_sum)

        plots["export"].set_value(emission_series, year,
                                  imported_E.sum() / base_E_sum)

        import_balance.set_column(str(year), imported_E)

    #describe_exporters()
    #create_plots()
    #describe_balance(True)
    describe_balance_intensity()
Beispiel #3
0
def trade_ratios(base_country, env_key):
    env_series = config.env_series_names[env_key]

    other_countries = sorted(config.countries.keys())
    other_countries.remove(base_country)

    yearstrings = [str(y) for y in config.STUDY_YEARS]
    exported_y = NamedMatrix(rows=other_countries, cols=yearstrings)
    exported_E = NamedMatrix(rows=other_countries, cols=yearstrings)

    def describe_exporters():
        print("Exports by country - " + env_key)

        print("dollars")
        for country in other_countries:
            formatted = [
                ("%.0f" % exported_y.get_element(country, year)).rjust(6)
                for year in yearstrings
            ]
            print(country, " ".join(formatted))

        print("emissions")
        for country in other_countries:
            formatted = [
                ("%.0f" % exported_E.get_element(country, year)).rjust(6)
                for year in yearstrings
            ]
            print(country, " ".join(formatted))

        print("intensities")
        intensities = exported_E.divide(exported_y)
        for country in other_countries:
            formatted = [
                ("%.2f" % intensities.get_element(country, year)).rjust(6)
                for year in yearstrings
            ]
            print(country, " ".join(formatted))

    ### prepare plots
    env_title = env_key.replace(" ", "-")
    plot_group = "import-%s-%s" % (env_title, base_country)
    plots = {}
    for sector in common.default_env_sectors:
        sector_title = common.get_industry_title(sector)
        plots[sector] = GNUPlot(
            "%s_%s" % (env_title, sector),
            "",
            #"Imports of %s - %s" % (sector_title, env_key),
            plot_group)
    plots["import"] = GNUPlot(
        "%s_import" % env_title,
        "",
        #"All imports - %s" % env_key,
        plot_group)
    plots["export"] = GNUPlot(
        "%s_export" % env_title,
        "",
        #"All imports - %s" % env_key,
        plot_group)

    def create_plots():
        for (sector, plot) in plots.items():
            plot.set_series_style(
                import_dollar_series,
                "pointtype 5 linetype rgb '#88BBFF'")  # hollow circle
            plot.set_series_style(
                emission_series,
                "pointtype 7 linetype rgb '#1144FF'")  # solid circle
            plot.set_series_style(self_emission_series,
                                  "pointtype 12 linetype rgb '#0000FF'")

            plot.set_series_style(export_dollar_series,
                                  "pointtype 4 linetype rgb '#CC9933'")
            plot.set_series_style(export_emissions_series,
                                  "pointtype 7 linetype rgb '#996600'")

            #plot.legend("width -8")
            plot.width = 480
            plot.height = 300

            plot.write_tables()
            plot.generate_plot()

    ### trade balance matrices
    export_balance = NamedMatrix(rows=common.default_env_sectors,
                                 cols=yearstrings)
    import_balance = NamedMatrix(rows=common.default_env_sectors,
                                 cols=yearstrings)
    all_E = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings)

    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 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")

    ###
    iogen = common.iogen_for_year(config.STUDY_YEARS[0])
    envgen = common.envgen_for_year(config.STUDY_YEARS[0])

    strings = {"schema": config.WIOD_SCHEMA}

    for year in config.STUDY_YEARS:
        strings["year"] = year

        base_E = get_wiod_env_vector(base_country, year, env_series)
        all_E.set_column(str(year), base_E)

        base_E_sum = base_E.sum()

        iogen.set_table("%(schema)s.indbyind_%(year)d" % strings)
        iogen.set_condition_args(base_country)

        envgen.set_table("%(schema)s.env_%(year)d" % strings)
        envgen.set_condition_args(base_country)
        envgen.set_sectors(common.default_env_sectors)

        # prepare base country values
        y = iogen.get_Y()
        import_column = common.get_import_vector(iogen)
        base_imports = import_column.sum()
        base_gdp = y.sum()

        harmonizer = common.get_io_harmonizer(iogen)

        base_y = harmonizer.matrix_mult(y.get_total())
        x = iogen.get_x()

        E = envgen.get_env_vector(env_series)
        L = iogen.get_L()
        J = E.divide(harmonizer.matrix_mult(x), ignore_zero_denom=True)
        base_JsL = J.square_matrix_from_diag()\
            .matrix_mult(harmonizer).matrix_mult(L)

        exported = base_JsL.matrix_mult(y.get_exports())
        export_balance.set_column(str(year), exported)

        plots["export"].set_value(export_emissions_series, year,
                                  exported.sum() / base_E_sum)
        plots["export"].set_value(export_dollar_series, year,
                                  y.get_exports().sum() / base_gdp)

        # prepare other country values
        stmt = db.prepare("""SELECT from_sector, sum(value)
            FROM wiod_plus.%s_io_import_%d WHERE country = $1
             AND from_sector NOT IN ('ITM', 'IMP', 'Rex')
           GROUP BY from_sector""" % (base_country.lower(), year))

        imported_E = None
        imported_y = None
        domestic_E = None  # emissions under domestic technology assumption

        for country in other_countries:
            envgen.set_condition_args(country)
            envgen.set_sectors(common.default_env_sectors)

            iogen.set_condition_args(country)
            sectors = iogen.get_sectors(
            )  # number of sectors varies by country

            E = envgen.get_env_vector(env_series)
            if E.mat() is None:  # this country has no data for env series
                continue

            imports = NamedMatrix(rows=sectors, cols=["imports"])
            db_result = stmt(country)
            if not len(db_result):
                # we can't do any of the following with a blank import vector
                continue

            for row in db_result:
                imports.set_element(row[0], "imports", row[1])

            sel = common.get_io_harmonizer(iogen)
            x = iogen.get_x()
            L = iogen.get_L()

            J = E.divide(sel.matrix_mult(x), ignore_zero_denom=True)
            JsL = J.square_matrix_from_diag().matrix_mult(sel).matrix_mult(L)

            current_E = JsL.matrix_mult(imports)
            current_y = sel.matrix_mult(imports)

            # temporary dumb way to deal with sector mismatch
            compat_imports = NamedMatrix(rows=base_JsL.get_columns(),
                                         cols=["imports"])
            for col in base_JsL.get_columns():
                if col in sectors:
                    compat_imports.set_element(col, "imports",
                                               imports.get_element(col))
            current_domestic_E = base_JsL.matrix_mult(compat_imports)

            imported_E = sum_if_not_none(imported_E, current_E)
            imported_y = sum_if_not_none(imported_y, current_y)
            domestic_E = sum_if_not_none(domestic_E, current_domestic_E)

            exported_y.set_element(country, str(year), imports.sum())
            exported_E.set_element(country, str(year), current_E.sum())

        # populate results table
        TradeResultsTable.insert_exports(year, base_country, exported)
        TradeResultsTable.insert_imports(year, base_country, imported_E)

        # generate import plots
        for sector in common.default_env_sectors:
            base_y_val = base_y.get_element(sector)
            if base_y_val > 0:
                plots[sector].set_value(
                    import_dollar_series, year,
                    imported_y.get_element(sector) / base_y_val)

            base_E_val = base_E.get_element(sector)
            if base_E_val > 0:
                plots[sector].set_value(
                    emission_series, year,
                    imported_E.get_element(sector) / base_E_val)
                plots[sector].set_value(
                    self_emission_series, year,
                    domestic_E.get_element(sector) / base_E_val)

        plots["import"].set_value(import_dollar_series, year,
                                  imported_y.sum() / base_y.sum())
        plots["import"].set_value(emission_series, year,
                                  imported_E.sum() / base_E_sum)
        plots["import"].set_value(self_emission_series, year,
                                  domestic_E.sum() / base_E_sum)

        if year in dta_plots and base_country in dta_countries:
            dta_plots[year].set_value("DTA", config.countries[base_country],
                                      imported_E.sum() / base_E_sum)
            dta_plots[year].set_value("No DTA", config.countries[base_country],
                                      domestic_E.sum() / base_E_sum)

        # this is for DTA vs non-DTA table
        #print(base_country, year,
        #      imported_E.sum(),
        #      domestic_E.sum(),
        #      imported_E.sum() / base_E_sum,
        #      domestic_E.sum() / base_E_sum)

        plots["export"].set_value(emission_series, year,
                                  imported_E.sum() / base_E_sum)

        import_balance.set_column(str(year), imported_E)

    #describe_exporters()
    #create_plots()
    #describe_balance(True)
    describe_balance_intensity()
Beispiel #4
0
def do_cfact(country, env_key):
    base_year = config.STUDY_YEARS[0]
    env_series = config.env_series_names[env_key]

    deflators = Deflators()
    iogen = common.iogen_for_year(base_year)
    envgen = common.envgen_for_year(base_year)
    env_sectors = common.env_sectors_for_year(base_year)

    iogen.set_condition_args(country)
    envgen.set_condition_args(country)

    cfgen = CounterfactGenerator(iogen, envgen)
    if env_key in common_config.ENV_SERIES_TITLES:
        env_title = common_config.ENV_SERIES_TITLES[env_key]
    else:
        env_title = env_key
    cfgen.set_series_code(env_series, env_title)
    
    for year in config.STUDY_YEARS:
        iogen = cfgen.get_iogen()
        envgen = cfgen.get_envgen()

        iotable = "%s.indbyind_%d" % (config.WIOD_SCHEMA, year)
        iogen.set_table(iotable)
        envtable = "%s.env_%d" % (config.WIOD_SCHEMA, year)
        envgen.set_table(envtable)
        envgen.set_sectors(env_sectors)

        iogen.set_exchange_rate(imfdata.get_2005_deflator(country, year))
        #iogen.set_exchange_rate(deflators.get_gdp_deflator(year))

        cfgen.prepare(year, env_series,
                      io_harmonizer=common.get_io_harmonizer(iogen))

    sector_titles = common.get_sector_names(True)
    # hack to get countries to print instead of sector names
    for key in sector_titles:
        sector_titles[key] = config.countries[country]
    cfgen.set_sector_titles(sector_titles)

    #cfgen.describe()
    for sector in intense_sectors:
        if sector == "sec26" and country == "IDN":
            continue # this is really just for aesthetics

        values = cfgen.get_sector_values(sector)
        if country in config.eu15:
            color = "#B1E6B1"
        elif country in config.other_eu:
            color = "#9FB5F5"
        elif country in config.north_am:
            color = "#E3B668"
        elif country in config.all_asia:
            color = "#FFFF80"
        else:
            color = "#C0B3C9"

        if values["level"][-1] > 10000:
            leveldiff = values["level"][-1] - values["level"][0]
            style = 7
            if leveldiff < 0:
                style = 6

            bubblecharts[sector].set_point(
                country, values["intensity"][0],
                values["intensity"][-1], abs(leveldiff), color, style)

    #    cfgen.print_sector(sector)


    series_abbr = env_key.replace(" ", "-")
    if use_levels:
        filename = "%s_%s-abs_%d" % (series_abbr, country, base_year)
    else:
        filename = "%s_%s_%d" % (series_abbr, country, base_year)
    #title = config.countries[country]
    title = None
    (pce_result, export_result) = cfgen.counterfact(
        base_year, "cfact-" + series_abbr,
        filename, title, compact=False,
        use_levels=use_levels)

    # pce
    result = cfgen.decompose_result(
        pce_result, base_year, max(config.STUDY_YEARS)) # A, J, L, Y

    value_pairs = [
        ("Sector intensity", result["J"]),
        ("Industry linkages", result["L"]),
        ("Final demand", result["Y"]),
        ]

    worldmaps["pce"].set_country_value(country, result["A"])
    worldmaps["pce-eu"].set_country_value(country, result["A"])

    if in_eu_rect(WorldMapPlot.get_centroid(country)) and country != "TUR":
        worldmaps["pce-eu"].add_tiny_barchart(country, value_pairs)
    else:
        worldmaps["pce"].add_tiny_barchart(country, value_pairs)

    # exports
    result = cfgen.decompose_result(
        export_result, base_year, max(config.STUDY_YEARS)) # A, J, L, Y

    value_pairs = [
        ("Sector intensity", result["J"]),
        ("Industry linkages", result["L"]),
        ("Final demand", result["Y"]),
       ]

    worldmaps["export"].set_country_value(country, result["A"])
    worldmaps["export-eu"].set_country_value(country, result["A"])

    if in_eu_rect(WorldMapPlot.get_centroid(country)) and country != "TUR":
        worldmaps["export-eu"].add_tiny_barchart(country, value_pairs)
    else:
        worldmaps["export"].add_tiny_barchart(country, value_pairs)
Beispiel #5
0
    },
    "exports": {
        "title": "Direct emissions / $ gross export",
        "style": 'pointtype 6 linetype rgb "red"',  # empty circle
    },
    "exports+": {
        "title": "Direct+indirect / $ gross export",
        "style": 'pointtype 7 linetype rgb "red"',  # solid circle
    },
}

for year in config.STUDY_YEARS:
    # show some output since this script takes awhile
    print(year)

    iogen = common.iogen_for_year(year)
    envgen = common.envgen_for_year(year)
    env_sectors = common.env_sectors_for_year(year)

    for (country, country_name) in config.countries.items():
        if country not in plots:
            plots[country] = GNUPlot("%s_%s" % (env_series, country),
                                     "%s in %s" % (env_series, country_name),
                                     "%s-totals" % env_series)
            plot = plots[country]
            for (series, specs) in series_specs.items():
                plot.set_series_style(specs["title"], specs["style"])

            marginal_plots[country] = GNUPlot(
                "%s_%s" % (env_series, country),
                "%s in %s" % (env_series, country_name), env_series)
Beispiel #6
0
        },
    "exports": {
        "title": "Direct emissions / $ gross export",
        "style": 'pointtype 6 linetype rgb "red"', # empty circle
        },
    "exports+": {
        "title": "Direct+indirect / $ gross export",
        "style": 'pointtype 7 linetype rgb "red"', # solid circle
        },
    }

for year in config.STUDY_YEARS:
    # show some output since this script takes awhile
    print(year)

    iogen = common.iogen_for_year(year)
    envgen = common.envgen_for_year(year)
    env_sectors = common.env_sectors_for_year(year)

    for (country, country_name) in config.countries.items():
        if country not in plots:
            plots[country] = GNUPlot(
                "%s_%s" % (env_series, country),
                "%s in %s" % (env_series, country_name),
                "%s-totals" % env_series)
            plot = plots[country]
            for (series, specs) in series_specs.items():
                plot.set_series_style(specs["title"], specs["style"])

            marginal_plots[country] = GNUPlot(
                "%s_%s" % (env_series, country),