Skip to content

Commit

Permalink
Functional, no mapping done. Tests to be added and fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedesroches committed Jul 24, 2023
1 parent c4aa798 commit d21c120
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AssessProcessAction(

override fun run(indicator: ProgressIndicator) {
val trace = traceSystemWithIndicator(indicator, file, processName, matchLabels)
val order = trace.getProductOrder()
val order = trace.getObservableOrder()
val inventory = Assessment(trace.getSystemValue(), trace.getEntryPoint()).inventory()
this.data = Pair(inventory, order)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ class EvaluationTrace {
private var currentStage = HashSet<MatrixRowIndex>()
private val processes = HashSet<ProcessValue>()
private val substanceCharacterizations = HashSet<SubstanceCharacterizationValue>()
private val productDepthMap = HashMap<MatrixColumnIndex, Int>()
private val observableDepthMap = HashMap<MatrixColumnIndex, Int>()

companion object {
fun empty(): EvaluationTrace {
return EvaluationTrace()
}
}

fun getProductOrder(): Comparator<MatrixColumnIndex> {
fun getObservableOrder(): Comparator<MatrixColumnIndex> {
return object : Comparator<MatrixColumnIndex> {
override fun compare(o1: MatrixColumnIndex, o2: MatrixColumnIndex): Int {
val d1 = productDepthMap[o1] ?: throw EvaluatorException("unknown ${o1}")
val d2 = productDepthMap[o2] ?: throw EvaluatorException("unknown ${o2}")
val d1 = observableDepthMap[o1] ?: throw EvaluatorException("unknown ${o1}")
val d2 = observableDepthMap[o2] ?: throw EvaluatorException("unknown ${o2}")
if (d1 < d2) {
return -1
}
Expand Down Expand Up @@ -94,13 +94,13 @@ class EvaluationTrace {
.filterIsInstance<ProcessValue>()
.forEach { process ->
process.products.forEach { exchange ->
productDepthMap[exchange.product] = currentDepth
observableDepthMap[exchange.product] = currentDepth
}
}
currentStage
.filterIsInstance<SubstanceCharacterizationValue>()
.forEach { sc ->
productDepthMap[sc.referenceExchange.substance] = currentDepth
observableDepthMap[sc.referenceExchange.substance] = currentDepth
}

stages.add(currentStage)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ch.kleis.lcaplugin.imports.ecospold

import ch.kleis.lcaplugin.core.lang.expression.SubstanceType
import ch.kleis.lcaplugin.imports.ModelWriter
import ch.kleis.lcaplugin.imports.ecospold.lcia.EcospoldImporter.Companion.unitToStr
import ch.kleis.lcaplugin.imports.ecospold.model.ActivityDataset
import ch.kleis.lcaplugin.imports.ecospold.model.ElementaryExchange
import ch.kleis.lcaplugin.imports.ecospold.model.IntermediateExchange
import ch.kleis.lcaplugin.imports.ecospold.model.Uncertainty
import ch.kleis.lcaplugin.imports.model.ExchangeBlock
import ch.kleis.lcaplugin.imports.model.ImportedBioExchange
import ch.kleis.lcaplugin.imports.model.ImportedProcess
import ch.kleis.lcaplugin.imports.model.ImportedProductExchange
import ch.kleis.lcaplugin.imports.simapro.sanitizeSymbol
Expand Down Expand Up @@ -36,7 +39,7 @@ open class EcoSpold2ProcessMapper(val process: ActivityDataset) {
mapInputs()
mapLandUse()
mapResources()
mapEmission()
mapEmissions()

return result
}
Expand All @@ -48,11 +51,46 @@ open class EcoSpold2ProcessMapper(val process: ActivityDataset) {
result.productBlocks = mutableListOf(ExchangeBlock("Products", products))
}

open fun mapEmission() {}
open fun mapInputs() {}
open fun mapLandUse() {}
open fun mapResources() {}
private fun mapEmissions() {
result.emissionBlocks += ExchangeBlock("FIXME",
process.flowData.elementaryExchanges
.filter { it.substanceType == SubstanceType.EMISSION }
.map(::elementaryExchangeToImportedBioExchange).toMutableList()
)

// TODO: Remove when closing #261
val bio = ImportedBioExchange(listOf(), "1.0", "u", pUid, "")
result.emissionBlocks += ExchangeBlock("Virtual Substance for Impact Factors", mutableListOf(bio))

}

private fun mapLandUse() {
result.landUseBlocks = mutableListOf(ExchangeBlock("FIXME",
process.flowData.elementaryExchanges
.filter { it.substanceType == SubstanceType.LAND_USE }
.map(::elementaryExchangeToImportedBioExchange).toMutableList()
))
}

private fun mapResources() {
result.resourceBlocks = mutableListOf(ExchangeBlock("FIXME",
process.flowData.elementaryExchanges
.filter { it.substanceType == SubstanceType.RESOURCE }
.map(::elementaryExchangeToImportedBioExchange).toMutableList()
))
}

private fun elementaryExchangeToImportedBioExchange(elementaryExchange: ElementaryExchange): ImportedBioExchange =
ImportedBioExchange(
comments = elementaryExchange.comment?.let { listOf(it) } ?: listOf(),
qty = elementaryExchange.amount.toString(),
unit = elementaryExchange.unit,
uid = ModelWriter.sanitizeAndCompact(elementaryExchange.name),
compartment = elementaryExchange.compartment,
subCompartment = elementaryExchange.subCompartment,
)

open fun mapInputs() {}

open fun mapMetas() {
val metas = result.meta
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ch.kleis.lcaplugin.imports.ecospold.lci

import ch.kleis.lcaplugin.core.lang.expression.SubstanceType
import ch.kleis.lcaplugin.imports.ecospold.model.ElementaryExchange

typealias UID = String

// This voluntarily does not include flows that are "ecoinvent orphans": the mapper between EF31 and EI391 will return
// None(null), and it is to the caller to decide what to do with an unmapped substance.

// FIXME: proxy mapped comps
// FIXME: unmapped comps
// FIXME: substance type
object EF31ToEI391SubstanceMapper {
const val method = "EF v3.1"
const val ecoinvent_database_version = "3.9.1"
private val mapping: Map<UID, ElementaryExchange?> = buildSubstanceMap()

fun mapElementaryExchange(eiElementaryExchange: ElementaryExchange): ElementaryExchange? =
mapping[eiElementaryExchange.elementaryExchangeId]?.let {
it.copy(
amount = eiElementaryExchange.amount * it.amount,
substanceType = eiElementaryExchange.substanceType
)
}

// FIXME: build cleanly
// sqlite3 ef-31-mapping-3.9.1.sqlite "select id, conversion_factor, method_name, method_unit, method_compartment, method_subcompartment from mapping WHERE compartment_status is 'mapped';" | awk -F '|' '{ print "\"" $1 "\" to ElementaryExchange(\"" $1 "\",", $2 ",", "\"" $3 "\",", "\"" $4 "\",", "\"" $5 "\",", "\"" $6 "\",", "SubstanceType.EMISSION, null)," }' | sed 's/""/null/'
private fun buildSubstanceMap(): Map<UID, ElementaryExchange?> = mapOf(
"584ffb1c-036d-417b-a9d1-1ec694dc2cdc" to ElementaryExchange("584ffb1c-036d-417b-a9d1-1ec694dc2cdc", 1.0, "1,2-dichlorobenzene", "kg", "Emissions to air", "Emissions to air, unspecified (long-term)", SubstanceType.EMISSION, null),
"77db8bd1-5a69-465c-b51f-7b27fbb574a5" to ElementaryExchange("77db8bd1-5a69-465c-b51f-7b27fbb574a5", 1.0, "1,2-dichlorobenzene", "kg", "Emissions to air", "Emissions to lower stratosphere and upper troposphere", SubstanceType.EMISSION, null),
"06a42317-47bd-481d-b5ce-e091843497c6" to ElementaryExchange("06a42317-47bd-481d-b5ce-e091843497c6", 1.0, "1,2-dichlorobenzene", "kg", "Emissions to air", "Emissions to non-urban air or from high stacks", SubstanceType.EMISSION, null),
"b1c36287-329c-49f0-93c2-68246d34007c" to ElementaryExchange("b1c36287-329c-49f0-93c2-68246d34007c", 1.0, "1,2-dichlorobenzene", "kg", "Emissions to air", "Emissions to air, unspecified", SubstanceType.EMISSION, null),
"9645e02f-855a-4b9f-8baf-f34a08fa80c4" to ElementaryExchange("9645e02f-855a-4b9f-8baf-f34a08fa80c4", 1.0, "1,2-dichlorobenzene", "kg", "Emissions to air", "Emissions to urban air close to ground", SubstanceType.EMISSION, null),
// ... and so on
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.kleis.lcaplugin.imports.ecospold.lcia

import ch.kleis.lcaplugin.imports.ModelWriter
import ch.kleis.lcaplugin.imports.ecospold.EcoSpold2ProcessMapper
import ch.kleis.lcaplugin.imports.ecospold.lcia.EcospoldImporter.ProcessDictRecord
import ch.kleis.lcaplugin.imports.ecospold.model.ActivityDataset
import ch.kleis.lcaplugin.imports.shared.serializer.ProcessSerializer
Expand All @@ -19,28 +20,28 @@ class Ecospold2ProcessRenderer {
val category = category(data)

val subFolder = if (category == null) "" else "${category}${File.separatorChar}"
val process = LciaProcessMapper(data).map()
val process = EcoSpold2ProcessMapper(data).map()
process.comments.add(processComment)
val strProcess = ProcessSerializer.serialize(process)

w.write(
"processes${File.separatorChar}$subFolder${process.uid}.lca",
strProcess, index = false, closeAfterWrite = false
"processes${File.separatorChar}$subFolder${process.uid}.lca",
strProcess, index = false, closeAfterWrite = false
)
val substance = EcoSpold2SubstanceMapper.map(data, methodName)
val strSubstance = SubstanceSerializer.serialize(substance)
w.write(
"processes${File.separatorChar}$subFolder${process.uid}.lca",
strSubstance, index = false, closeAfterWrite = true
"processes${File.separatorChar}$subFolder${process.uid}.lca",
strSubstance, index = false, closeAfterWrite = true
)


}

private fun category(data: ActivityDataset): String? {
val desc = data.description.classifications
.firstOrNull { it.system == "EcoSpold01Categories" }
?.value
.firstOrNull { it.system == "EcoSpold01Categories" }
?.value
return desc?.let { ModelWriter.sanitizeAndCompact(it) }
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.kleis.lcaplugin.imports.ecospold.model

import ch.kleis.lcaplugin.core.lang.expression.SubstanceType

data class ElementaryExchange(
val elementaryExchangeId: String,
val amount: Double,
val name: String,
val unit: String,
val compartment: String,
val subCompartment: String?,
val substanceType: SubstanceType,
val comment: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.kleis.lcaplugin.imports.ecospold.model


data class FlowData(
val intermediateExchanges: List<IntermediateExchange> = ArrayList(),
val impactIndicators: List<ImpactIndicator>
val intermediateExchanges: List<IntermediateExchange> = ArrayList(),
val impactIndicators: List<ImpactIndicator>,
val elementaryExchanges: Sequence<ElementaryExchange>,
)
Loading

0 comments on commit d21c120

Please sign in to comment.