Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next version update #16

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
keep original run output files
simei94 committed Dec 2, 2024
commit 9dc6a4fd2a668da75638a2d4bee8b67d6d5cd509
45 changes: 36 additions & 9 deletions src/main/java/org/matsim/dashboards/LausitzSimWrapperRunner.java
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@

import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

@@ -69,13 +70,17 @@ public final class LausitzSimWrapperRunner implements MATSimAppCommand {
@CommandLine.Option(names = "--pt-line-base-dir", description = "create pt line dashboard with base run dir as input")
private String baseDir;

private static final String FILE_TYPE = "_emissions.xml.gz";
private static final String FILE_TYPE = "_before_emissions.xml";


public LausitzSimWrapperRunner(){
// public constructor needed for testing purposes.
}

public static void main(String[] args) {
new LausitzSimWrapperRunner().execute(args);
}

@Override
public Integer call() throws Exception {

@@ -120,22 +125,31 @@ public Integer call() throws Exception {
String networkPath = ApplicationUtils.matchInput("output_network.xml.gz", runDirectory).toString();
String vehiclesPath = ApplicationUtils.matchInput("output_vehicles.xml.gz", runDirectory).toString();
String transitVehiclesPath = ApplicationUtils.matchInput("output_transitVehicles.xml.gz", runDirectory).toString();
String populationPath = ApplicationUtils.matchInput("output_plans.xml.gz", runDirectory).toString();

config.network().setInputFile(networkPath);
config.vehicles().setVehiclesFile(vehiclesPath);
config.transit().setVehiclesFile(transitVehiclesPath);
config.plans().setInputFile(populationPath);

Scenario scenario = ScenarioUtils.loadScenario(config);

// adapt network and veh types for emissions analysis like in LausitzScenario base run class
PrepareNetwork.prepareEmissionsAttributes(scenario.getNetwork());
LausitzScenario.prepareVehicleTypesForEmissionAnalysis(scenario);

// write outputs with adapted files
ConfigUtils.writeConfig(config, configPath.split(".xml")[0] + "_emissions.xml");
NetworkUtils.writeNetwork(scenario.getNetwork(), networkPath.split(".xml")[0] + FILE_TYPE);
new MatsimVehicleWriter(scenario.getVehicles()).writeFile(vehiclesPath.split(".xml")[0] + FILE_TYPE);
new MatsimVehicleWriter(scenario.getTransitVehicles()).writeFile(transitVehiclesPath.split(".xml")[0] + FILE_TYPE);
// write outputs with adapted files.
// original output files need to be overwritten as AirPollutionAnalysis searches for "config.xml".
// copy old files to separate files
Files.copy(Path.of(configPath), getUniqueTargetPath(Path.of(configPath.split(".xml")[0] + FILE_TYPE)));
Files.copy(Path.of(networkPath), getUniqueTargetPath(Path.of(networkPath.split(".xml")[0] + FILE_TYPE + ".gz")));
Files.copy(Path.of(vehiclesPath), getUniqueTargetPath(Path.of(vehiclesPath.split(".xml")[0] + FILE_TYPE + ".gz")));
Files.copy(Path.of(transitVehiclesPath), getUniqueTargetPath(Path.of(transitVehiclesPath.split(".xml")[0] + FILE_TYPE + ".gz")));

ConfigUtils.writeConfig(config, configPath);
NetworkUtils.writeNetwork(scenario.getNetwork(), networkPath);
new MatsimVehicleWriter(scenario.getVehicles()).writeFile(vehiclesPath);
new MatsimVehicleWriter(scenario.getTransitVehicles()).writeFile(transitVehiclesPath);
}

if (baseDir != null) {
@@ -153,9 +167,22 @@ public Integer call() throws Exception {
return 0;
}

public static void main(String[] args) {
new LausitzSimWrapperRunner().execute(args);
private static Path getUniqueTargetPath(Path targetPath) {
int counter = 1;
Path uniquePath = targetPath;

// Add a suffix if the file already exists
while (Files.exists(uniquePath)) {
String originalPath = targetPath.toString();
int dotIndex = originalPath.lastIndexOf(".");
if (dotIndex == -1) {
uniquePath = Path.of(originalPath + "_" + counter);
} else {
uniquePath = Path.of(originalPath.substring(0, dotIndex) + "_" + counter + originalPath.substring(dotIndex));
}
counter++;
}

return uniquePath;
}

}