def extract(cls, filepath, delimiter=";", encoding='cp1252'): assert os.path.exists(filepath), "Can't find file %s" % filepath log, logfile = get_io_logger(u"SimaPro-LCIA-extractor") log.info(INTRODUCTION % ( filepath, repr(delimiter), )) with open(filepath, "r", encoding=encoding) as csv_file: reader = csv.reader(csv_file, delimiter=delimiter) lines = [ strip_delete(line) if not all(i == '' for i in line) else [] for line in reader ] # Check if valid SimaPro file assert u'SimaPro' in lines[0][0], "File is not valid SimaPro export" datasets = [] index = cls.get_next_method_index(lines, 0) while True: try: ds, index = cls.read_method_data_set(lines, index, filepath) datasets.extend(ds) index = cls.get_next_method_index(lines, index) except EndOfDatasets: break close_log(log) return datasets
def delete_exchanges_missing_activity(db): """Delete exchanges that weren't linked correctly by ecoinvent. These exchanges are missing the "activityLinkId" attribute, and the flow they want to consume is not produced as the reference product of any activity. See the `known data issues <http://www.ecoinvent.org/database/ecoinvent-version-3/reports-of-changes/known-data-issues/>`__ report. """ log, logfile = get_io_logger("Ecospold2-import-error") count = 0 for ds in db: exchanges = ds.get('exchanges', []) if not exchanges: continue skip = [] for exc in exchanges: if exc.get('input'): continue if (not exc.get('activity') and exc['type'] in {'technosphere', 'production', 'substitution'}): log.critical(u"Purging unlinked exchange:\nFilename: {}\n{}"\ .format(ds[u'filename'], format_for_logging(exc))) count += 1 skip.append(exc) ds[u'exchanges'] = [exc for exc in exchanges if exc not in skip] close_log(log) if count: print((u"{} exchanges couldn't be linked and were deleted. See the " u"logfile for details:\n\t{}").format(count, logfile)) return db
def delete_ghost_exchanges(db): """Delete technosphere which can't be linked due to ecoinvent errors. A ghost exchange is one which links to a combination of *activity* and *flow* which aren't provided in the database.""" log, logfile = get_io_logger("Ecospold2-import-error") count = 0 for ds in db: exchanges = ds.get("exchanges", []) if not exchanges: continue skip = [] for exc in exchanges: if exc.get("input") or exc.get("type") != "technosphere": continue log.critical( u"Purging unlinked exchange:\nFilename: {}\n{}".format( ds[u"filename"], format_for_logging(exc))) count += 1 skip.append(exc) ds[u"exchanges"] = [exc for exc in exchanges if exc not in skip] close_log(log) if count: print((u"{} exchanges couldn't be linked and were deleted. See the " u"logfile for details:\n\t{}").format(count, logfile)) return db
def extract(cls, filepath, delimiter=default_delimiter(), name=None, encoding='cp1252'): assert os.path.exists(filepath), "Can't find file %s" % filepath log, logfile = get_io_logger("SimaPro-extractor") log.info(INTRODUCTION % ( filepath, repr(delimiter), name, )) with UnicodeCSVReader(filepath, encoding=encoding, delimiter=delimiter) as csv_file: lines = [[strip_whitespace_and_delete(obj) for obj in line] for line in csv_file] # Check if valid SimaPro file assert ('SimaPro' in lines[0][0] or 'CSV separator' in lines[0][0]), "File is not valid SimaPro export" project_name = name or cls.get_project_name(lines) datasets = [] project_metadata = cls.get_project_metadata(lines) global_parameters = cls.get_global_parameters(lines, project_metadata) index = cls.get_next_process_index(lines, 0) while True: try: ds, index = cls.read_data_set( lines, index, project_name, filepath, global_parameters, project_metadata, ) datasets.append(ds) index = cls.get_next_process_index(lines, index) except EndOfDatasets: break close_log(log) return datasets, global_parameters, project_metadata