Search in sources :

Example 6 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class ProcessWriter method writeReferenceFlows.

@SuppressWarnings("unchecked")
private void writeReferenceFlows() {
    // order flows by their type
    int n = ElementaryFlowType.values().length;
    List<Flow>[] buckets = new List[n];
    for (int i = 0; i < n; i++) {
        buckets[i] = new ArrayList<>();
    }
    for (Map.Entry<Flow, Compartment> e : flowCompartments.entrySet()) {
        ElementaryFlowType type = e.getValue().type();
        buckets[type.ordinal()].add(e.getKey());
    }
    // write the flow information
    for (var type : ElementaryFlowType.values()) {
        writeln(type.blockHeader());
        // duplicate names are not allowed here
        HashSet<String> handledNames = new HashSet<>();
        for (Flow flow : buckets[type.ordinal()]) {
            String name;
            String unit = null;
            FlowMapEntry mapEntry = mappedFlow(flow);
            if (mapEntry != null) {
                // handle mapped flows
                name = mapEntry.targetFlow().flow.name;
                if (mapEntry.targetFlow().unit != null) {
                    unit = unit(mapEntry.targetFlow().unit.name);
                }
            } else {
                // handle unmapped flows
                name = flow.name;
                Unit refUnit = null;
                if (flow.referenceFlowProperty != null) {
                    if (flow.referenceFlowProperty.unitGroup != null) {
                        refUnit = flow.referenceFlowProperty.unitGroup.referenceUnit;
                    }
                }
                unit = unit(refUnit);
            }
            if (name == null || unit == null)
                continue;
            String id = name.trim().toLowerCase();
            if (handledNames.contains(id))
                continue;
            handledNames.add(id);
            writeln(name, unit, flow.casNumber, "");
        }
        writeln();
        writeln("End");
        writeln();
        writeln();
    }
}
Also used : SubCompartment(org.openlca.simapro.csv.enums.SubCompartment) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow) ElementaryFlowType(org.openlca.simapro.csv.enums.ElementaryFlowType) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) FlowMap(org.openlca.io.maps.FlowMap) HashSet(java.util.HashSet)

Example 7 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class IOSheet method readExchange.

private Exchange readExchange(int row) {
    RefData refData = config.refData;
    String name = config.getString(sheet, row, 0);
    if (name == null) {
        return null;
    }
    String category = config.getString(sheet, row, 1);
    Flow flow = refData.getFlow(name, category);
    if (flow == null) {
        return refDataError(row, "flow: " + name + "/" + category);
    }
    String propName = config.getString(sheet, row, 2);
    FlowProperty property = refData.getFlowProperty(propName);
    if (property == null) {
        return refDataError(row, "flow property: " + propName);
    }
    if (flow.getFactor(property) == null) {
        return refDataError(row, "flow property factor: " + propName);
    }
    String unitName = config.getString(sheet, row, 3);
    Unit unit = refData.getUnit(unitName);
    if (unit == null) {
        return refDataError(row, "unit: " + unitName);
    }
    var exchange = config.process.add(Exchange.of(flow, property, unit));
    exchange.isInput = forInputs;
    exchange.amount = config.getDouble(sheet, row, 4);
    String formula = config.getString(sheet, row, 5);
    if (!Strings.nullOrEmpty(formula)) {
        exchange.formula = formula;
    }
    String description = config.getString(sheet, row, 6);
    if (!Strings.nullOrEmpty(description)) {
        exchange.description = description;
    }
    exchange.uncertainty = config.getUncertainty(sheet, row, 7);
    if ("Yes".equals(config.getString(sheet, row, 12)))
        exchange.isAvoided = true;
    return exchange;
}
Also used : Unit(org.openlca.core.model.Unit) FlowProperty(org.openlca.core.model.FlowProperty) Flow(org.openlca.core.model.Flow)

Example 8 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class UnitSheets method syncUnitGroup.

private UnitGroup syncUnitGroup(UnitGroup unitGroup, UnitGroupRecord record) {
    HashMap<String, Unit> sheetUnits = getUnits(record.name);
    Unit refUnit = unitGroup.referenceUnit;
    boolean canAdd = refUnit != null && Objects.equals(refUnit.name, record.refUnit);
    boolean updated = false;
    for (Unit sheetUnit : sheetUnits.values()) {
        Unit realUnit = unitGroup.getUnit(sheetUnit.name);
        if (realUnit != null) {
            continue;
        }
        if (!canAdd) {
            log.error("unit {} not exists in unit group {} but cannot be" + "added as the reference unit is different to the " + "reference unit in the Excel file", sheetUnit, unitGroup);
            continue;
        }
        unitGroup.units.add(sheetUnit);
        updated = true;
    }
    if (updated) {
        unitGroup.lastChange = Calendar.getInstance().getTimeInMillis();
        Version.incUpdate(unitGroup);
        unitGroup = groupDao.update(unitGroup);
    }
    return unitGroup;
}
Also used : Unit(org.openlca.core.model.Unit)

Example 9 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class UnitSheets method getUnits.

private HashMap<String, Unit> getUnits(String unitGroup) {
    HashMap<String, Unit> units = new HashMap<>();
    for (UnitRecord record : unitRecords) {
        if (!Objects.equals(record.unitGroup, unitGroup)) {
            continue;
        }
        Unit unit = new Unit();
        unit.conversionFactor = record.conversionFactor;
        unit.description = record.description;
        unit.refId = record.uuid;
        unit.name = record.name;
        unit.synonyms = record.synonyms;
        units.put(record.name, unit);
    }
    return units;
}
Also used : HashMap(java.util.HashMap) Unit(org.openlca.core.model.Unit)

Example 10 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class UnitExport method doIt.

@Override
protected void doIt(CSVPrinter printer, IDatabase db) throws IOException {
    log.trace("write units");
    UnitGroupDao dao = new UnitGroupDao(db);
    int count = 0;
    for (UnitGroup unitGroup : dao.getAll()) {
        for (Unit unit : unitGroup.units) {
            Object[] line = createLine(unitGroup, unit);
            printer.printRecord(line);
        }
        count++;
    }
    log.trace("{} units written", count);
}
Also used : UnitGroup(org.openlca.core.model.UnitGroup) Unit(org.openlca.core.model.Unit) UnitGroupDao(org.openlca.core.database.UnitGroupDao)

Aggregations

Unit (org.openlca.core.model.Unit)50 UnitGroup (org.openlca.core.model.UnitGroup)18 Flow (org.openlca.core.model.Flow)13 FlowProperty (org.openlca.core.model.FlowProperty)13 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)11 UnitGroupDao (org.openlca.core.database.UnitGroupDao)9 ArrayList (java.util.ArrayList)6 FlowDao (org.openlca.core.database.FlowDao)4 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)4 List (java.util.List)3 IDatabase (org.openlca.core.database.IDatabase)3 Collections (java.util.Collections)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Objects (java.util.Objects)2 Test (org.junit.Test)2 M (org.openlca.app.M)2 ProcessDao (org.openlca.core.database.ProcessDao)2 UnitDao (org.openlca.core.database.UnitDao)2 ImpactFactor (org.openlca.core.model.ImpactFactor)2