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();
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations