Skip to content

Commit

Permalink
Schreibe Ergebnisse pro vehicle und vehicleType raus
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Mar 1, 2024
1 parent 94e32f2 commit 3d51fe6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 10 deletions.
115 changes: 108 additions & 7 deletions src/main/java/org/matsim/vsp/emissions/EmissionsWriterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
Expand All @@ -15,6 +16,7 @@
import org.matsim.contrib.emissions.Pollutant;
import org.matsim.contrib.emissions.analysis.EmissionsOnLinkEventHandler;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;

/**
Expand All @@ -23,6 +25,8 @@
public class EmissionsWriterUtils {

private static final Logger log = LogManager.getLogger(EmissionsWriterUtils.class);
private static final String DELIMITER = ";";

/**
* Schreibt CSV Dateien raus mit Emission pro LINK - einmal als Summe für den Link -> kann genutzt
* werden für Gesamtsumme der Emissionen. - einmal pro Meter -> für grafische Analyse
Expand Down Expand Up @@ -57,8 +61,8 @@ static void writePerLinkOutput(String linkEmissionAnalysisFile,
Map<Id<Link>, Map<Pollutant, Double>> link2pollutants = emissionsEventHandler.getLink2pollutants();

for (Pollutant pollutant : Pollutant.values()) {
absolutWriter.write(";" + pollutant);
perMeterWriter.write(";" + pollutant + " [g/m]");
absolutWriter.write(DELIMITER + pollutant);
perMeterWriter.write(DELIMITER + pollutant + " [g/m]");
}

absolutWriter.newLine();
Expand All @@ -73,14 +77,15 @@ static void writePerLinkOutput(String linkEmissionAnalysisFile,
if (link2pollutants.get(linkId).get(pollutant) != null) {
emissionValue = link2pollutants.get(linkId).get(pollutant);
}
absolutWriter.write(";" + nf.format(emissionValue));
absolutWriter.write(DELIMITER + nf.format(emissionValue));


double emissionPerM = Double.NaN;
Link link = scenario.getNetwork().getLinks().get(linkId);
if (link != null) {
emissionPerM = emissionValue / link.getLength();
}
perMeterWriter.write(";" + nf.format(emissionPerM));
perMeterWriter.write(DELIMITER + nf.format(emissionPerM));

}
absolutWriter.newLine();
Expand All @@ -92,12 +97,107 @@ static void writePerLinkOutput(String linkEmissionAnalysisFile,
perMeterWriter.close();
log.info("Output written to " + linkEmissionPerMAnalysisFile);
}
}

/**
* Schreibt CSV Dateien raus mit Emission pro Vehicle
* - einmal als Summe für das Fahrzeug und
* - als Summe für den Fahrzeugtyp-> kann genutzt
* TODO: Vielleiht auch nochmal nach EngineType?
*
* @param vehicleEmissionsAnalysisFile
* @param vehicleTypeEmissionAnalysisFile
* @param scenario
* @param emissionsEventHandler
* @throws IOException
*/
static void writePerVehicleOutput(String vehicleEmissionsAnalysisFile,
String vehicleTypeEmissionAnalysisFile, Scenario scenario,
EmissionsPerVehicleEventHandler emissionsEventHandler)
throws IOException {
log.info("Emission analysis completed.");
log.info("Writing output per VEHICLE (TYPE)...");

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

{
File vehicleFile = new File(vehicleEmissionsAnalysisFile);
File vehicleTypeFile = new File(vehicleTypeEmissionAnalysisFile);

BufferedWriter vehicleWriter = new BufferedWriter(new FileWriter(vehicleFile));
BufferedWriter vehicleTypeWriter = new BufferedWriter(new FileWriter(vehicleTypeFile));

vehicleWriter.write("vehicleId");
vehicleWriter.write("vehicleId" + DELIMITER + "vehicleTypeId");
vehicleTypeWriter.write("vehicleTypeId");

Map<Id<Vehicle>, Map<Pollutant, Double>> vehicle2pollutants = emissionsEventHandler.getVehicle2pollutants();
Map<Id<VehicleType>, Map<Pollutant, Double>> vehicleType2pollutants = new HashMap<>();

for (Pollutant pollutant : Pollutant.values()) {
vehicleWriter.write(DELIMITER + pollutant);
vehicleTypeWriter.write(DELIMITER + pollutant);
}

vehicleWriter.newLine();
vehicleTypeWriter.newLine();

for (Id<Vehicle> vehicleId : vehicle2pollutants.keySet()) {
final Id<VehicleType> vehicleTypeId = VehicleUtils.findVehicle(vehicleId, scenario)
.getType().getId();

vehicleWriter.write(vehicleId.toString());
vehicleWriter.write(DELIMITER + vehicleTypeId.toString());

for (Pollutant pollutant : Pollutant.values()) {
double emissionValue = 0.;
if (vehicle2pollutants.get(vehicleId).get(pollutant) != null) {
emissionValue = vehicle2pollutants.get(vehicleId).get(pollutant);
}
vehicleWriter.write(DELIMITER + nf.format(emissionValue));
}
vehicleWriter.newLine();

{
//Sum up per VehicleType
if (vehicleType2pollutants.get(vehicleTypeId) == null) {
vehicleType2pollutants.put(vehicleTypeId, vehicle2pollutants.get(vehicleId));
} else {
var emissions = vehicle2pollutants.get(vehicleId);
for (Pollutant pollutant : emissions.keySet()) {
vehicleType2pollutants.get(vehicleTypeId)
.merge(pollutant, emissions.get(pollutant), Double::sum);
}
}
}

}
// write it out per VehicleType.
for (Id<VehicleType> vehicleTypeId : vehicleType2pollutants.keySet()) {
vehicleTypeWriter.write(vehicleTypeId.toString());

for (Pollutant pollutant : Pollutant.values()) {
double emissionValue = 0.;
if (vehicleType2pollutants.get(vehicleTypeId).get(pollutant) != null) {
emissionValue = vehicleType2pollutants.get(vehicleTypeId).get(pollutant);
}
vehicleTypeWriter.write(DELIMITER + nf.format(emissionValue));
}
vehicleTypeWriter.newLine();
}

vehicleWriter.close();
log.info("Output written to " + vehicleEmissionsAnalysisFile);
vehicleTypeWriter.close();
log.info("Output written to " + vehicleTypeEmissionAnalysisFile);
}
}

/**
* Schreibt die Zuordnung von Fzg-Ids und den zugeordnenten emissions-concepts raus.
*
*
* @param vehicleTypeFileStr
* @param scenario
* @param emissionsEventHandler
Expand All @@ -110,7 +210,7 @@ static void writeEmissionConceptAssignmentOutput(String vehicleTypeFileStr,
//TODO: Das ist nur zuordnugn von Fzg zu Fzg-Typen. Das brauche ich aber gar nicht.
//TODO: Gut bzw. wichtig wären eigentlich emissions pro Fzg / FzgTyp.
log.info("Writing assignemnt of emissionConcepts to vehicles...");

File vehicleTypeFile = new File(vehicleTypeFileStr);

BufferedWriter vehicleTypeWriter = new BufferedWriter(new FileWriter(vehicleTypeFile));
Expand All @@ -124,7 +224,8 @@ static void writeEmissionConceptAssignmentOutput(String vehicleTypeFileStr,
emissionsConcept = VehicleUtils.getHbefaEmissionsConcept(vehicle.getType().getEngineInformation());
}

vehicleTypeWriter.write(vehicle.getId() + ";" + vehicle.getType().getId().toString() + ";" + emissionsConcept);
vehicleTypeWriter.write(vehicle.getId() + DELIMITER
+ vehicle.getType().getId().toString() + DELIMITER + emissionsConcept);
vehicleTypeWriter.newLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void run() throws IOException {
final String linkEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerLink.csv";
final String linkEmissionPerMAnalysisFile = analysisOutputDirectory + "/emissionsPerLinkPerM.csv";
final String vehicleTypeFile = analysisOutputDirectory + "/emissionVehicleInformation.csv";
final String vehicleEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerVerhicle.csv";
final String vehicleTypeEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerVehicleType.csv";

Scenario scenario = ScenarioUtils.loadScenario(config);
// network
Expand Down Expand Up @@ -323,9 +325,7 @@ public void install(){

EmissionsWriterUtils.writePerLinkOutput(linkEmissionAnalysisFile, linkEmissionPerMAnalysisFile, scenario, emissionsEventHandler);
EmissionsWriterUtils.writeEmissionConceptAssignmentOutput(vehicleTypeFile, scenario, emissionsEventHandler);

//Neu: pro Fzg(Typ)

EmissionsWriterUtils.writePerVehicleOutput(vehicleEmissionAnalysisFile,vehicleTypeEmissionAnalysisFile,scenario,emissionsPerVehicleEventHandler);


int totalVehicles = scenario.getVehicles().getVehicles().size();
Expand Down

0 comments on commit 3d51fe6

Please sign in to comment.