Skip to content

Commit b21ba7d

Browse files
committed
core: debug: improve csv logging utils
We can now input value as `log("name" to value)`, and stdcm nodes can be logged with their coordinates Signed-off-by: Eloi Charpentier <[email protected]>
1 parent dfa6a29 commit b21ba7d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

core/src/main/kotlin/fr/sncf/osrd/utils/DebugUtils.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package fr.sncf.osrd.utils
22

3+
import fr.sncf.osrd.path.implementations.buildTrainPathFromBlock
4+
import fr.sncf.osrd.sim_infra.api.BlockInfra
5+
import fr.sncf.osrd.sim_infra.api.RawInfra
6+
import fr.sncf.osrd.stdcm.graph.STDCMNode
7+
import fr.sncf.osrd.utils.units.Offset
38
import java.io.BufferedWriter
49
import java.io.File
510

611
/** Small utility class to log values in a csv */
712
class CSVLogger(filename: String, private val keys: List<String>) {
813
private val writer: BufferedWriter = File(filename).bufferedWriter()
914

15+
constructor(filename: String, vararg keys: String) : this(filename, keys.toList())
16+
1017
init {
1118
writer.write(keys.joinToString(";") + "\n")
1219
}
@@ -17,4 +24,29 @@ class CSVLogger(filename: String, private val keys: List<String>) {
1724
val line = keys.joinToString(separator = ";") { entries.getOrDefault(it, "").toString() }
1825
writer.write(line + "\n")
1926
}
27+
28+
/** Log the given entries to the CSV. All keys must appear in the object keys. */
29+
fun log(vararg entries: Pair<String, Any>) {
30+
log(mapOf(*entries))
31+
}
32+
33+
/** Log the given entries to the CSV, associated with the node lat/lon. */
34+
fun logGeoNodeData(
35+
rawInfra: RawInfra,
36+
blockInfra: BlockInfra,
37+
node: STDCMNode,
38+
vararg entries: Pair<String, Any>,
39+
) {
40+
val block = node.infraExplorer.getCurrentBlock()
41+
val geo = buildTrainPathFromBlock(rawInfra, blockInfra, block).getGeo()
42+
val blockLength = blockInfra.getBlockLength(block)
43+
val offset = node.locationOnEdge ?: Offset.zero()
44+
var p = geo.interpolateNormalized(offset.distance.meters / blockLength.distance.meters)
45+
if (p.lat.isNaN() || p.lon.isNaN()) p = geo.getPoints().first()
46+
47+
val data = mutableMapOf(*entries)
48+
data["lat"] = p.lat
49+
data["lon"] = p.lon
50+
log(data)
51+
}
2052
}

0 commit comments

Comments
 (0)