GreenDelta / olca-app

Source code of openLCA
openlca.org
Mozilla Public License 2.0
193 stars 43 forks source link

support damage categories and normalization/weighting sets in the SimaPro CSV import #277

Closed msrocka closed 1 year ago

msrocka commented 1 year ago

image

The import can be tested by generating test processes that contain random values for elementary flows for different compartments. openLCA should give the same results as SimaPro. The script below takes a SimaPro CSV file and generates such a process:

import java.io.File;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import org.openlca.simapro.csv.CsvDataSet;
import org.openlca.simapro.csv.Numeric;
import org.openlca.simapro.csv.SimaProCsv;
import org.openlca.simapro.csv.enums.ElementaryFlowType;
import org.openlca.simapro.csv.enums.ProcessCategory;
import org.openlca.simapro.csv.enums.ProcessType;
import org.openlca.simapro.csv.enums.SubCompartment;
import org.openlca.simapro.csv.process.ElementaryExchangeRow;
import org.openlca.simapro.csv.process.ProcessBlock;
import org.openlca.simapro.csv.process.ProductOutputRow;

public class StepTest {

    public static void main(String[] args) {
        var dir = new File("C:/Users/ms/Desktop/rems/stepw");
        var methodFile = "ei99_method.csv";

        var subComps = new EnumMap<ElementaryFlowType, List<SubCompartment>>(
            ElementaryFlowType.class);
        for (var type : ElementaryFlowType.values()) {
            var subs = new ArrayList<SubCompartment>();
            subs.add(SubCompartment.UNSPECIFIED);
            for (var subComp : SubCompartment.values()) {
                if (subComp.flowType() == type) {
                    subs.add(subComp);
                }
            }
            subComps.put(type, subs);
        }

        var method = SimaProCsv.read(new File(dir, methodFile));

        var product = new ProductOutputRow()
            .name("Test product")
            .amount(Numeric.of(1))
            .allocation(Numeric.of(100))
            .unit("kg")
            .wasteType("not defined")
            .category("Others");

        var out = new CsvDataSet();
        out.header().formatVersion("7.0.0");
        out.quantities().addAll(method.quantities());
        out.units().addAll(method.units());

        var process = new ProcessBlock()
            .name("Test process")
            .identifier("GreenDel000006567400002")
            .processType(ProcessType.UNIT_PROCESS)
            .category(ProcessCategory.MATERIAL);
        process.products().add(product);

        var rand = ThreadLocalRandom.current();
        for (var type : ElementaryFlowType.values()) {
            for (var flow : method.getElementaryFlows(type)) {
                out.getElementaryFlows(type).add(flow);
                var subs = subComps.get(type);
                int isub = rand.nextInt(subs.size());
                var exchange = new ElementaryExchangeRow()
                    .name(flow.name())
                    .subCompartment(subs.get(isub).toString())
                    .unit(flow.unit())
                    .amount(Numeric.of(rand.nextDouble()));
                process.exchangesOf(type).add(exchange);
            }
        }
        out.processes().add(process);

        out.write(new File(dir, methodFile + "_process.csv"));
    }
}