Skip to content

Commit

Permalink
Merge branch 'master' into fleetSizing
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther authored Sep 26, 2024
2 parents 82fc278 + 12642b2 commit 126ab73
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Common files to ignore when working with MATSim
output/
scenarios/output
test/output
test/

*.gz
*.pbf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class NoiseAverageAnalysis implements MATSimAppCommand {
private static final String ANALYSIS_DIR = "/analysis/noise";
private static final String LINK_ID = "Link Id";
private static final String VALUE = "value";
private List<GenericRecord> imissionsPerDay = new ArrayList<>();
private List<GenericRecord> imissionsPerHour = new ArrayList<>();
private List<GenericRecord> immissionsPerDay = new ArrayList<>();
private List<GenericRecord> immissionsPerHour = new ArrayList<>();
private Map<String, List<Double>> emissionsPerDay = new HashMap<>();
private Map<String, Double> meanEmissionsPerDay = new HashMap<>();
private Map<String, List<Double>> totalStats = new HashMap<>();
Expand Down Expand Up @@ -86,8 +86,8 @@ public Integer call() throws Exception {
for (String folder : foldersSeeded) {
final Path analysisDir = Path.of(folder + ANALYSIS_DIR);
String emissionsCsv = globFile(analysisDir, "*emission_per_day.csv*").toString();
String imissionsPerDayAvro = globFile(analysisDir, "*immission_per_day.avro*").toString();
String imissionsPerHourAvro = globFile(analysisDir, "*immission_per_hour.avro*").toString();
String immissionsPerDayAvro = globFile(analysisDir, "*immission_per_day.avro*").toString();
String immissionsPerHourAvro = globFile(analysisDir, "*immission_per_hour.avro*").toString();
String totalStatsCsv = globFile(analysisDir, "*noise_stats.csv*").toString();
String damagesPerDayAvro = globFile(analysisDir, "*damages_receiverPoint_per_day.avro*").toString();
String damagesPerHourAvro = globFile(analysisDir, "*damages_receiverPoint_per_hour.avro*").toString();
Expand All @@ -105,8 +105,8 @@ public Integer call() throws Exception {
.separator(CsvOptions.detectDelimiter(totalStatsCsv)).build());

// read avro files
readAvroFile(imissionsPerDayAvro, imissionsPerDay);
readAvroFile(imissionsPerHourAvro, imissionsPerHour);
readAvroFile(immissionsPerDayAvro, immissionsPerDay);
readAvroFile(immissionsPerHourAvro, immissionsPerHour);
readAvroFile(damagesPerDayAvro, damagesPerDay);
readAvroFile(damagesPerHourAvro, damagesPerHour);

Expand Down Expand Up @@ -137,8 +137,8 @@ public Integer call() throws Exception {
calcCsvMeans(totalStats, meanTotalStatsPerDay);

// calc avro means
XYTData imissionsPerDayMean = calcAvroMeans(imissionsPerDay, "imissions");
XYTData imissionsPerHourMean = calcAvroMeans(imissionsPerHour, "imissions");
XYTData immissionsPerDayMean = calcAvroMeans(immissionsPerDay, "immission");
XYTData immissionsPerHourMean = calcAvroMeans(immissionsPerHour, "immission");
XYTData damagesPerDayMean = calcAvroMeans(damagesPerDay, "damages_receiverPoint");
XYTData damagesPerHourMean = calcAvroMeans(damagesPerHour, "damages_receiverPoint");

Expand All @@ -161,8 +161,8 @@ public Integer call() throws Exception {
}

// write avro mean files
writeAvro(imissionsPerDayMean, new File(output.getPath("mean_immission_per_day.avro").toString()));
writeAvro(imissionsPerHourMean, new File(output.getPath("mean_immission_per_hour.avro").toString()));
writeAvro(immissionsPerDayMean, new File(output.getPath("mean_immission_per_day.avro").toString()));
writeAvro(immissionsPerHourMean, new File(output.getPath("mean_immission_per_hour.avro").toString()));
writeAvro(damagesPerDayMean, new File(output.getPath("mean_damages_receiverPoint_per_day.avro").toString()));
writeAvro(damagesPerHourMean, new File(output.getPath("mean_damages_receiverPoint_per_hour.avro").toString()));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.matsim.analysis.postAnalysis;
package org.matsim.analysis.postAnalysis.emissions;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
Expand Down Expand Up @@ -198,8 +198,8 @@ public Integer call() throws Exception {
}

// write grid mean stats
writeGridFile("mean_emissions_grid_per_day.xyt.csv", meanGridPerDay);
writeGridFile("mean_emissions_grid_per_hour.csv", meanGridPerHour);
writeGridFile("mean_emissions_grid_per_day.xyt.csv", meanGridPerDay, nf);
writeGridFile("mean_emissions_grid_per_hour.csv", meanGridPerHour, nf);

return 0;
}
Expand All @@ -223,15 +223,15 @@ private void getGridData(Table gridTable, Map<Map.Entry<Double, Coord>, List<Dou
}
}

private void writeGridFile(String fileName, Map<Map.Entry<Double, Coord>, Double> values) throws IOException {
private void writeGridFile(String fileName, Map<Map.Entry<Double, Coord>, Double> values, NumberFormat numberFormat) throws IOException {
try (CSVPrinter printer = new CSVPrinter(Files.newBufferedWriter(output.getPath(fileName)), CSVFormat.DEFAULT)) {

//set the projection in the YAML instead, as this is put out with a quote atm...
//printer.printRecord("# EPSG:25832");
printer.printRecord("time", "x", "y", VALUE);

for (Map.Entry<Map.Entry<Double, Coord>, Double> e : values.entrySet()) {
printer.printRecord(e.getKey().getKey(), e.getKey().getValue().getX(), e.getKey().getValue().getY(), e.getValue());
printer.printRecord(e.getKey().getKey(), e.getKey().getValue().getX(), e.getKey().getValue().getY(), numberFormat.format(e.getValue()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* *
* *********************************************************************** */

package org.matsim.analysis.emissions;
package org.matsim.analysis.postAnalysis.emissions;

import it.unimi.dsi.fastutil.objects.Object2DoubleLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
Expand Down Expand Up @@ -428,23 +428,25 @@ private void writeTotal(Network network, EmissionsOnLinkEventHandler emissionsEv
//so we need to do some stupid filtering afterwards, which means that we produce and calculate more data than we dump out....
private void writeRaster(Network fullNetwork, Network filteredNetwork, Config config, EmissionsOnLinkEventHandler emissionsEventHandler) {



Map<Pollutant, Raster> rasterMap = FastEmissionGridAnalyzer.processHandlerEmissions(emissionsEventHandler.getLink2pollutants(), fullNetwork, gridSize, 20);

Raster raster = rasterMap.values().stream().findFirst().orElseThrow();

try (CSVPrinter printer = new CSVPrinter(Files.newBufferedWriter(output.getPath("emissions_grid_per_day.xyt.csv")),
CSVFormat.DEFAULT.builder().setCommentMarker('#').build())) {

NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMaximumFractionDigits(4);
nf.setGroupingUsed(false);

String crs = ProjectionUtils.getCRS(fullNetwork);
if (crs == null)
crs = config.network().getInputCRS();
if (crs == null)
crs = config.global().getCoordinateSystem();

// print coordinate system
printer.printComment(crs);
// printer.printComment(crs);

// print header
printer.print("time");
Expand Down Expand Up @@ -478,7 +480,7 @@ private void writeRaster(Network fullNetwork, Network filteredNetwork, Config co
printer.print(coord.getY());

double value = rasterMap.get(Pollutant.CO2_TOTAL).getValueByIndex(xi, yi);
printer.print(value);
printer.print(nf.format(value));

printer.println();
}
Expand Down Expand Up @@ -515,14 +517,18 @@ private void writeTimeDependentRaster(Network fullNetwork, Network filteredNetwo
try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(output.getPath("emissions_grid_per_hour.csv").toString()),
CSVFormat.DEFAULT.builder().setCommentMarker('#').build())) {

NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMaximumFractionDigits(4);
nf.setGroupingUsed(false);

String crs = ProjectionUtils.getCRS(fullNetwork);
if (crs == null)
crs = config.network().getInputCRS();
if (crs == null)
crs = config.global().getCoordinateSystem();

// print coordinate system
printer.printComment(crs);
// print coordinate system
// printer.printComment(crs);

// print header
printer.print("time");
Expand Down Expand Up @@ -560,7 +566,7 @@ private void writeTimeDependentRaster(Network fullNetwork, Network filteredNetwo
printer.print(coord.getX());
printer.print(coord.getY());

printer.print(value);
printer.print(nf.format(value));

printer.println();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@

package org.matsim.dashboard;

import org.matsim.analysis.postAnalysis.EmissionsPostProcessingAverageAnalysis;
import org.matsim.analysis.postAnalysis.emissions.EmissionsPostProcessingAverageAnalysis;
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.Data;
import org.matsim.simwrapper.Header;
import org.matsim.simwrapper.Layout;
import org.matsim.simwrapper.viz.GridMap;
import org.matsim.simwrapper.viz.Links;
import org.matsim.simwrapper.viz.Table;
import org.matsim.simwrapper.viz.*;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.matsim.dashboard.AverageKelheimNoiseDashboard.*;

/**
* Average emissions dashboard for several runs with the same config but a different random seed.
*/
Expand All @@ -51,10 +50,14 @@ public AverageKelheimEmissionsDashboard(List<String> dirs, Integer noRuns, Strin
this.dirs = dirs;
this.noRuns = noRuns;

if (!pathToBaseRun.endsWith("/")) {
pathToBaseRun += "/";
if (pathToBaseRun == null || pathToBaseRun.equals("null")){
this.pathToCsvBase = null;
} else {
if (!pathToBaseRun.endsWith("/")) {
pathToBaseRun += "/";
}
this.pathToCsvBase = pathToBaseRun + "analysis/emissions/emissions_per_link_per_m.csv";
}
this.pathToCsvBase = pathToBaseRun + "analysis/emissions/emissions_per_link_per_m.csv";
}

private String postProcess(Data data, String outputFile) {
Expand All @@ -68,8 +71,8 @@ private String postProcess(Data data, String outputFile) {
* Produces the dashboard.
*/
public void configure(Header header, Layout layout) {
header.title = "Average Emissions";
header.description = "Shows the average emissions footprint and spatial distribution for several simulation runs.";
header.title = "Average Air Pollution";
header.description = "Shows the average air pollution and spatial distribution for several simulation runs.";

String linkDescription = "Displays the emissions for each link per meter. Be aware that emission values are provided in the simulation sample size!";
if (pathToCsvBase != null){
Expand All @@ -86,35 +89,62 @@ public void configure(Header header, Layout layout) {
viz.showAllRows = true;
viz.width = 1.0;
})
.el(Links.class, (viz, data) -> {
viz.title = "Emissions per Link per Meter";
/*
* Commented out link panel, because the MapPlot can show a legend and seems to be the development head.
* However, it doesn't seem to have the pointer to the base case
*/
/*.el(Links.class, (viz, data) -> {
viz.title = "Emitted Pollutant in Gram per Meter";
viz.description = finalLinkDescription;
viz.height = 12.0;
viz.datasets.csvFile = postProcess(data, "mean_emissions_per_link_per_m.csv");
viz.datasets.csvBase = Path.of(this.dirs.get(0)).getParent().relativize(Path.of(pathToCsvBase)).toString();
viz.datasets.csvBase = pathToCsvBase == null ? "" : Path.of(this.dirs.get(0)).getParent().relativize(Path.of(pathToCsvBase)).toString();
viz.network = new CreateAverageDashboards().copyVizNetwork(dirs, ".avro");
viz.display.color.columnName = "CO2_TOTAL [g/m]";
viz.display.color.dataset = "csvFile";
viz.display.width.scaleFactor = 100;
viz.display.width.columnName = "CO2_TOTAL [g/m]";
viz.display.width.dataset = "csvFile";
viz.center = data.context().getCenter();
viz.width = 1.0;
})*/
.el(MapPlot.class, (viz, data) -> {
viz.title = "Emitted Pollutant in Gram per Meter";
viz.description = finalLinkDescription;
viz.height = 12.0;
viz.center = data.context().getCenter();
viz.zoom = data.context().mapZoomLevel;
viz.setShape(new CreateAverageDashboards().copyVizNetwork(dirs, ".avro"), "id");
viz.addDataset("emissions_per_m", postProcess(data, "mean_emissions_per_link_per_m.csv"));
viz.display.lineColor.dataset = "emissions_per_m";
viz.display.lineColor.columnName = "CO2_TOTAL [g/m]";
viz.display.lineColor.join = "linkId";
viz.display.lineColor.setColorRamp(ColorScheme.Oranges, 8, false, "35, 45, 55, 65, 75, 85, 95");
viz.display.lineWidth.dataset = "emissions_per_m";
viz.display.lineWidth.columnName = "CO2_TOTAL [g/m]";
viz.display.lineWidth.scaleFactor = 100d;
viz.display.lineWidth.join = "linkId";
viz.width = 3.0;
});
});

layout.row("second").el(GridMap.class, (viz, data) -> {
viz.title = "CO₂ Emissions";
viz.description = "per day. Be aware that CO2 values are provided in the simulation sample size!";
viz.projection = "EPSG:25832";
viz.height = 12.0;
setGridMapStandards(viz);
viz.file = postProcess(data, "mean_emissions_grid_per_day.xyt.csv");
});
layout.row("third")
.el(GridMap.class, (viz, data) -> {
viz.title = "CO₂ Emissions";
viz.projection = "EPSG:25832";
viz.description = "per hour. Be aware that CO2 values are provided in the simulation sample size!";
viz.height = 12.;
setGridMapStandards(viz);
viz.file = postProcess(data, "mean_emissions_grid_per_hour.csv");
});
}

private static void setGridMapStandards(GridMap viz) {
viz.projection = "EPSG:25832";
viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED});
viz.height = 12.0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;

/**
* Shows emission in the scenario.
* Averages and displays the noise outcome of single runs.
*/
public class AverageKelheimNoiseDashboard implements Dashboard {

Expand All @@ -23,12 +23,12 @@ public class AverageKelheimNoiseDashboard implements Dashboard {
private final List<String> dirs;
private final Integer noRuns;
private static final String NOISE = "noise";
private static final String DARK_BLUE = "#1175b3";
private static final String LIGHT_BLUE = "#95c7df";
private static final String ORANGE = "#f4a986";
private static final String RED = "#cc0c27";
private static final String SAND = "#dfb095";
private static final String YELLOW = "#dfdb95";
static final String DARK_BLUE = "#1175b3";
static final String LIGHT_BLUE = "#95c7df";
static final String ORANGE = "#f4a986";
static final String RED = "#cc0c27";
static final String SAND = "#dfb095";
static final String YELLOW = "#dfdb95";

public AverageKelheimNoiseDashboard(List<String> dirs, Integer noRuns) {
this.dirs = dirs;
Expand Down Expand Up @@ -78,55 +78,38 @@ public void configure(Header header, Layout layout) {
.el(GridMap.class, (viz, data) -> {
viz.title = "Noise Immissions (Grid)";
viz.description = "Total Noise Immissions per day";
viz.height = 12.0;
viz.cellSize = 250;
viz.opacity = 0.2;
viz.maxHeight = 20;
viz.projection = "EPSG:25832";
viz.center = data.context().getCenter();
viz.zoom = data.context().mapZoomLevel;
viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED});
setGridMapStandards(viz, data);
viz.file = postProcess(data, "mean_immission_per_day.avro");
})
.el(GridMap.class, (viz, data) -> {
viz.title = "Hourly Noise Immissions (Grid)";
viz.description = "Noise Immissions per hour";
viz.height = 12.0;
viz.cellSize = 250;
viz.opacity = 0.1;
viz.maxHeight = 40;
viz.projection = "EPSG:25832";
viz.center = data.context().getCenter();
viz.zoom = data.context().mapZoomLevel;
viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED});
setGridMapStandards(viz, data);
viz.file = postProcess(data, "mean_immission_per_hour.avro");
});
layout.row("damages")
.el(GridMap.class, (viz, data) -> {
viz.title = "Daily Noise Damages (Grid)";
viz.description = "Total Noise Damages per day [€]";
viz.height = 12.0;
viz.cellSize = 250;
viz.opacity = 0.1;
viz.maxHeight = 40;
viz.center = data.context().getCenter();
viz.zoom = data.context().mapZoomLevel;
viz.projection = "EPSG:25832";
viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED});
setGridMapStandards(viz, data);
viz.file = postProcess(data, "mean_damages_receiverPoint_per_day.avro");
})
.el(GridMap.class, (viz, data) -> {
viz.title = "Hourly Noise Damages (Grid)";
viz.description = "Noise Damages per hour [€]";
viz.height = 12.0;
viz.cellSize = 250;
viz.opacity = 0.2;
viz.maxHeight = 40;
viz.projection = "EPSG:25832";
viz.center = data.context().getCenter();
viz.zoom = data.context().mapZoomLevel;
viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED});
setGridMapStandards(viz, data);
viz.file = postProcess(data, "mean_damages_receiverPoint_per_hour.avro");
});
}

private static void setGridMapStandards(GridMap viz, Data data) {
viz.height = 12.0;
viz.cellSize = 250;
viz.opacity = 0.1;
viz.maxHeight = 40;
viz.projection = "EPSG:25832";
viz.center = data.context().getCenter();
viz.zoom = data.context().mapZoomLevel;
viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED});
}
}
Loading

0 comments on commit 126ab73

Please sign in to comment.