Esempio n. 1
0
File: parser.py Progetto: sonya/eea
def parse_env():
    filename = "rftghgemissions.xls"
    path = fileutils.getcache(filename, "uk")
    wb = xlrd.open_workbook(path)
    sheets = wb.sheets()

    tables = HybridTableCreator(config.SCHEMA)
    codes = tables.new_sector_codes(prefix="env_ind")

    codes.add_curated_codes({
            "Manufacture of petrochemicals": "20.1[467]+20.6",
            "Manufacture of other basic metals & casting (excl. Nuclear fuel & Aluminium)": "24.4[^26]-5",
            "Rest of repair; Installation": "33.1[^56]",
            })

    for sheet in sheets:
        series = sheet.name
        years = None
        for i in range(sheet.nrows):
            row = sheet.row_values(i)
            if len(row) < 3 or type(row[2]) is str and not len(row[2]):
                continue
            if years is None:
                if type(row[2]) is float:
                    years = row
                    for year in row[2:]:
                        #envtable.add_env_table("env", year)
                        tables.add_env_table(year)
            else:
                code = codes.set_code(row[0], row[1])
                if code:
                    for i in range(2, len(row)):
                        tables.insert_env(years[i], code, series, row[i])

    codes.update_codes()
Esempio n. 2
0
File: parser.py Progetto: sonya/eea
def parse_env():
    filename = "rftghgemissions.xls"
    path = fileutils.getcache(filename, "uk")
    wb = xlrd.open_workbook(path)
    sheets = wb.sheets()

    tables = HybridTableCreator(config.SCHEMA)
    codes = tables.new_sector_codes(prefix="env_ind")

    codes.add_curated_codes({
        "Manufacture of petrochemicals":
        "20.1[467]+20.6",
        "Manufacture of other basic metals & casting (excl. Nuclear fuel & Aluminium)":
        "24.4[^26]-5",
        "Rest of repair; Installation":
        "33.1[^56]",
    })

    for sheet in sheets:
        series = sheet.name
        years = None
        for i in range(sheet.nrows):
            row = sheet.row_values(i)
            if len(row) < 3 or type(row[2]) is str and not len(row[2]):
                continue
            if years is None:
                if type(row[2]) is float:
                    years = row
                    for year in row[2:]:
                        #envtable.add_env_table("env", year)
                        tables.add_env_table(year)
            else:
                code = codes.set_code(row[0], row[1])
                if code:
                    for i in range(2, len(row)):
                        tables.insert_env(years[i], code, series, row[i])

    codes.update_codes()
Esempio n. 3
0
File: parser.py Progetto: sonya/eea
def parse_env():

    files = {
        # 2005 only has 細分類 while
        1990: "ei90187p.xls",
        1995: "ei95186p.xls",
        2000: "ei2000p104v01j.xls",
        2005: "ei2005pc403jp_wt_bd.xlsx",
    }

    def series_names_from_rows(names, units):
        # since these tables are structured identically
        # we'll just do some hard coding
        series_names = []
        for i in range(3, len(names)):
            if len(names[i]):
                name = "%s (%s)" % (names[i], units[i])
            else:
                name = None
            series_names.append(name)
        return series_names

    tables = HybridTableCreator(config.SCHEMA)

    for (year, filename) in files.items():
        tables.add_env_table(year, series_max_length=255)
        codes = tables.new_sector_codes(year, "env_ind")
        codes.curate_code_from_desc("総合計", "total")
        codes.blacklist_code("total")

        path = fileutils.getcache(filename, "jp", str(year))
        if filename.endswith("xls"):
            wb = xlrd.open_workbook(path)
            # each xls file starts with ToC listing tables A-E.
            # E1: 部門別直接エネルギー消費量,エネルギー原単位を掲載
            # E2: 部門別直接CO2排出量,CO2排出原単位を掲載
            for sheetname in ("E1", "E2"):
                sheet = wb.sheet_by_name(sheetname)
                min_series_col = 4  # first col whose values interest us
                if sheetname == "E1":
                    min_series_col = 3  # GDP - only want this once

                series_names = series_names_from_rows(sheet.row_values(0),
                                                      sheet.row_values(1))

                for i in range(2, sheet.nrows):
                    row = sheet.row_values(i)
                    code = row[1]
                    if type(code) is float:
                        code = str(int(code)).rjust(3, "0")
                    code = codes.set_code(code, row[2])
                    if code:
                        for (series, value) in zip(series_names, row[3:]):
                            if type(value) is float:
                                tables.insert_env(year, code, series, value)

        elif filename.endswith("xlsx"):
            wb = openpyxl.load_workbook(filename=path, use_iterators=True)
            # E: 部門別直接エネルギー消費量および各種GHG排出量,
            #    エネルギー原単位およびGHG原単位を掲載
            sheet = wb.get_sheet_by_name("E")
            rows = sheet.iter_rows()
            series_names = series_names_from_rows(
                [cell.internal_value for cell in next(rows)],
                [cell.internal_value for cell in next(rows)])
            for row in rows:
                code = codes.set_code(row[1].internal_value,
                                      row[2].internal_value)
                if code:
                    for (series, cell) in zip(series_names, row[3:]):
                        if cell.internal_value is not None:
                            tables.insert_env(year, code, series,
                                              cell.internal_value)

        codes.update_codes()
Esempio n. 4
0
File: parser.py Progetto: sonya/eea
def parse_env():

    files = {
        # 2005 only has 細分類 while
        1990: "ei90187p.xls",
        1995: "ei95186p.xls",
        2000: "ei2000p104v01j.xls",
        2005: "ei2005pc403jp_wt_bd.xlsx",
        }

    def series_names_from_rows(names, units):
        # since these tables are structured identically
        # we'll just do some hard coding
        series_names = []
        for i in range(3, len(names)):
            if len(names[i]):
                name = "%s (%s)" % (names[i], units[i])
            else:
                name = None
            series_names.append(name)
        return series_names

    tables = HybridTableCreator(config.SCHEMA)

    for (year, filename) in files.items():
        tables.add_env_table(year, series_max_length=255)
        codes = tables.new_sector_codes(year, "env_ind")
        codes.curate_code_from_desc("総合計", "total")
        codes.blacklist_code("total")

        path = fileutils.getcache(filename, "jp", str(year))
        if filename.endswith("xls"):
            wb = xlrd.open_workbook(path)
            # each xls file starts with ToC listing tables A-E.
            # E1: 部門別直接エネルギー消費量,エネルギー原単位を掲載
            # E2: 部門別直接CO2排出量,CO2排出原単位を掲載
            for sheetname in ("E1", "E2"):
                sheet = wb.sheet_by_name(sheetname)
                min_series_col = 4 # first col whose values interest us
                if sheetname == "E1":
                    min_series_col = 3 # GDP - only want this once
    
                series_names = series_names_from_rows(
                    sheet.row_values(0),
                    sheet.row_values(1))

                for i in range(2, sheet.nrows):
                    row = sheet.row_values(i)
                    code = row[1]
                    if type(code) is float:
                        code = str(int(code)).rjust(3, "0")
                    code = codes.set_code(code, row[2])
                    if code:
                        for (series, value) in zip(series_names, row[3:]):
                            if type(value) is float:
                                tables.insert_env(year, code, series, value)
    
        elif filename.endswith("xlsx"):
            wb = openpyxl.load_workbook(filename=path, use_iterators=True)
            # E: 部門別直接エネルギー消費量および各種GHG排出量,
            #    エネルギー原単位およびGHG原単位を掲載
            sheet = wb.get_sheet_by_name("E")
            rows = sheet.iter_rows()
            series_names = series_names_from_rows(
                [cell.internal_value for cell in next(rows)],
                [cell.internal_value for cell in next(rows)])
            for row in rows:
                code = codes.set_code(row[1].internal_value,
                                      row[2].internal_value)
                if code:
                    for (series, cell) in zip(series_names, row[3:]):
                        if cell.internal_value is not None:
                            tables.insert_env(year, code, series,
                                              cell.internal_value)

        codes.update_codes()