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_national_value(country, year, measurement, env_series="CO2"): strings = { "schema": config.WIOD_SCHEMA, "year": year, "fd_sectors": sqlhelper.set_repr(config.default_fd_sectors), } if measurement == "env": envsql = """SELECT value FROM %(schema)s.env_%(year)d WHERE country = $1 AND measurement = $2 AND industry = 'total'""" envstmt = db.prepare(envsql % strings) return envstmt(country, env_series)[0][0] if measurement == "gdp": gdpsql = """SELECT sum(value) FROM %(schema)s.indbyind_%(year)d WHERE country = $1 AND to_ind in %(fd_sectors)s""" gdpstmt = db.prepare(gdpsql % strings) gdp = gdpstmt(country)[0][0] return imfdata.convert_to_2005(gdp, country, year) if measurement == "ppppc": ppppc = imfdata.get_imf_value(country, year, "ppp_pc") if ppppc is None: # above is worldbank version. imf version might not be chained ppppc = imfdata.get_imf_value(country, year, "PPPPC") return ppppc if measurement == "pop": return imfdata.get_imf_value(country, year, "pop") return imfdata.get_imf_value(country, year, measurement)