MATSIM
Events2Snapshot.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Events2Snapshot.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.run;
22 
23 import java.io.File;
24 import java.util.Collection;
25 import java.util.Iterator;
26 
27 import org.matsim.api.core.v01.Scenario;
30 import org.matsim.core.config.Config;
41 
43 
50 public class Events2Snapshot {
51 
52  private Config config = null;
53  private Network network = null;
54  private EventsManager events = null;
55  private SnapshotGenerator visualizer = null;
56  private String configfile = null;
57  private String eventsfile;
58  private SnapshotWriter writer = null;
59 
65  private void parseArguments(final String[] args) {
66  if (args.length == 0) {
67  System.out.println("Too few arguments.");
68  printUsage();
69  System.exit(1);
70  }
71  Iterator<String> argIter = new ArgumentParser(args).iterator();
72  while (argIter.hasNext()) {
73  String arg = argIter.next();
74  if (arg.equals("-h") || arg.equals("--help")) {
75  printUsage();
76  System.exit(0);
77  } else {
78  if (arg.contains(".xml"))
79  this.configfile = arg;
80  else if (arg.contains("events"))
81  this.eventsfile = arg;
82  else {
83  System.out.println("Unrecognized file \"" + arg + "\"");
84  printUsage();
85  System.exit(1);
86  }
87  }
88  }
89  }
90 
91  private void printUsage() {
92  System.out.println();
93  System.out.println("Events2Snapshot");
94  System.out.println("Converts an events file to a snapshot file.");
95  System.out.println();
96  System.out.println("usage: Events2Snapshot [OPTIONS] configfile [eventsfile]");
97  System.out.println(" If no eventsfile is given, the in-events-file specified in the config-");
98  System.out.println(" file will be used.");
99  System.out.println(" The snapshots are generated according to the snapshot-settings in the");
100  System.out.println(" simulation-part of the configuration.");
101  System.out.println();
102  System.out.println("Options:");
103  System.out.println("-h, --help: Displays this message.");
104  System.out.println();
105  System.out.println("----------------");
106  System.out.println("2007, matsim.org");
107  System.out.println();
108  }
109 
114  public void run(final String[] args) {
115  parseArguments(args);
116  Scenario scenario;
117  this.config = ConfigUtils.loadConfig(this.configfile);
118  MatsimRandom.reset(this.config.global().getRandomSeed());
119  scenario = ScenarioUtils.createScenario(this.config);
120 
121 // if (((SimulationConfigGroup) this.config.getModule(SimulationConfigGroup.GROUP_NAME)).getSnapshotPeriod() <= 0.0) {
122 // System.out.println("The snapshotPeriod must be larger than 0 seconds.");
123 // return;
124 // }
125 
126  this.network = scenario.getNetwork();
127  new MatsimNetworkReader(scenario.getNetwork()).readFile(this.config.network().getInputFile());
128  prepare();
129 
130  if (this.eventsfile == null) {
131  this.eventsfile = this.config.getParam("events", "inputFile");
132  }
133  System.out.println("reading events from " + this.eventsfile);
134  File file = new File(this.eventsfile);
135  String outputDir = file.getParent() + "/";
136 
137  loadSnapshotWriters(outputDir);
138 
139  new MatsimEventsReader(this.events).readFile(this.eventsfile);
140  this.visualizer.finish();
141  System.out.println("done.");
142  }
143 
152  public void run(final File eventsFile, final Config config, final Network network) {
153  this.eventsfile = eventsFile.getAbsolutePath();
154  this.config = config;
155 
156  if (this.config.qsim().getSnapshotPeriod() <= 0.0) {
157  System.out.println("The snapshotPeriod must be larger than 0 seconds.");
158  return;
159  }
160 
161  this.network = network;
162 
163  prepare();
164 
165  if (this.eventsfile == null) {
166  this.eventsfile = this.config.getParam("events", "inputFile");
167  }
168  System.out.println("reading events from " + this.eventsfile);
169  File file = new File(this.eventsfile);
170  String outputDir = file.getParent() + "/";
171 
172  loadSnapshotWriters(outputDir);
173 
174  try {
175  new MatsimEventsReader(this.events).readFile(this.eventsfile);
176  }
177  catch (OutOfMemoryError e) {
178  System.err.println("OutOfMemoryError while reading all events:");
179  e.printStackTrace();
180  System.err.println("Trying to close visualizer file up to this state, it may not be complete though.");
181  }
182  this.visualizer.finish();
183  System.out.println("done.");
184  }
185 
186 
187  private void prepare() {
188  // create events
189  this.events = EventsUtils.createEventsManager();
190 
191  // create SnapshotGenerator
192  this.visualizer = new SnapshotGenerator(this.network, this.config.qsim().getSnapshotPeriod(),
193  this.config.qsim());
194  this.events.addHandler(this.visualizer);
195  }
196 
197  public void addExternalSnapshotWriter(final SnapshotWriter writer) {
198  this.writer = writer;
199  }
200 
201  private void loadSnapshotWriters(final String outputDir) {
202 
203  if (this.writer != null) {
204  this.visualizer.addSnapshotWriter(this.writer);
205  }
206 
207  Collection<SnapshotFormat> snapshotFormats = this.config.controller().getSnapshotFormat();
208 
209  for( SnapshotFormat snapshotFormat : snapshotFormats ){
210  switch( snapshotFormat ){
211  case transims: {
212  String snapshotFile = outputDir + "T.veh";
213  this.visualizer.addSnapshotWriter(new TransimsSnapshotWriter(snapshotFile));
214  break; }
215  case googleearth:
216  // KML support removed, michalm, may'22
217  case otfvis:
218  // this was not filled in when I found it, but I think it should. kai, feb'20
219  case positionevents:
220  default:
221  throw new IllegalStateException( "Unexpected value: " + snapshotFormat );
222  }
223  }
224  }
225 
226  public static void main(final String[] args) {
227  new Events2Snapshot().run(args);
228  }
229 
230 }
final String getParam(final String moduleName, final String paramName)
Definition: Config.java:318
void run(final File eventsFile, final Config config, final Network network)
void loadSnapshotWriters(final String outputDir)
final NetworkConfigGroup network()
Definition: Config.java:411
static Config loadConfig(final String filename, ConfigGroup... customModules)
void addHandler(final EventHandler handler)
void run(final String[] args)
final void addSnapshotWriter(final SnapshotWriter writer)
void parseArguments(final String[] args)
QSimConfigGroup qsim()
Definition: Config.java:447
static EventsManager createEventsManager()
final ControllerConfigGroup controller()
Definition: Config.java:399
final GlobalConfigGroup global()
Definition: Config.java:395
static Scenario createScenario(final Config config)
static void main(final String[] args)
void addExternalSnapshotWriter(final SnapshotWriter writer)