Skip to content

Commit

Permalink
use CsvOptions.detectDelimiter instead of hard coded ';' (#3466)
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 authored Sep 9, 2024
1 parent fb31f6e commit 575e346
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Coord;
import org.matsim.application.avro.XYTData;
import org.matsim.application.options.CsvOptions;
import org.matsim.core.config.Config;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.core.utils.misc.Time;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void setMaxTime(int maxTime) {
/**
* Merges noise data from multiple files into one file.
*/
public void run() {
public void run() throws IOException {
mergeReceiverPointData(outputDirectory + "/immissions/", "immission");
mergeReceiverPointData(outputDirectory + "/damages_receiverPoint/", "damages_receiverPoint");
mergeLinkData(outputDirectory.toString() + "/emissions/", "emission");
Expand All @@ -116,7 +117,7 @@ private void writeAvro(XYTData xytData, File output) {
}
}

private void mergeLinkData(String pathParameter, String label) {
private void mergeLinkData(String pathParameter, String label) throws IOException {
log.info("Merging emissions data for label {}", label);
Object2DoubleMap<String> mergedData = new Object2DoubleOpenHashMap<>();
Table csvOutputMerged = Table.create(TextColumn.create("Link Id"), DoubleColumn.create("value"));
Expand All @@ -126,9 +127,10 @@ private void mergeLinkData(String pathParameter, String label) {

// Read the file
Table table = Table.read().csv(CsvReadOptions.builder(IOUtils.getBufferedReader(path))
.columnTypesPartial(Map.of("Link Id", ColumnType.TEXT))
.columnTypesPartial(Map.of("Link Id", ColumnType.TEXT,
"Noise Emission " + Time.writeTime(time, Time.TIMEFORMAT_HHMMSS), ColumnType.DOUBLE))
.sample(false)
.separator(';').build());
.separator(CsvOptions.detectDelimiter(path)).build());

for (Row row : table) {
String linkId = row.getString("Link Id");
Expand Down Expand Up @@ -157,7 +159,7 @@ private void mergeLinkData(String pathParameter, String label) {
* @param outputDir path to the receiverPoint data
* @param label label for the receiverPoint data (which kind of data)
*/
private void mergeReceiverPointData(String outputDir, String label) {
private void mergeReceiverPointData(String outputDir, String label) throws IOException {

// data per time step, maps coord to value
Int2ObjectMap<Object2FloatMap<FloatFloatPair>> data = new Int2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -188,7 +190,7 @@ private void mergeReceiverPointData(String outputDir, String label) {
"t", ColumnType.DOUBLE,
valueHeader, ColumnType.DOUBLE))
.sample(false)
.separator(';').build());
.separator(CsvOptions.detectDelimiter(timeDataFile)).build());

// Loop over all rows in the data file
for (Row row : dataTable) {
Expand Down Expand Up @@ -265,7 +267,7 @@ private void mergeReceiverPointData(String outputDir, String label) {
// Merges the immissions data

@Deprecated
private void mergeImmissionsCSV(String pathParameter, String label) {
private void mergeImmissionsCSV(String pathParameter, String label) throws IOException {
log.info("Merging immissions data for label {}", label);
Object2DoubleMap<Coord> mergedData = new Object2DoubleOpenHashMap<>();

Expand All @@ -284,7 +286,7 @@ private void mergeImmissionsCSV(String pathParameter, String label) {
"Receiver Point Id", ColumnType.INTEGER,
"t", ColumnType.DOUBLE))
.sample(false)
.separator(';').build());
.separator(CsvOptions.detectDelimiter(path)).build());

// Loop over all rows in the file
for (Row row : table) {
Expand Down

0 comments on commit 575e346

Please sign in to comment.