001/* *********************************************************************** * 002 * project: org.matsim.* 003 * * 004 * *********************************************************************** * 005 * * 006 * copyright : (C) 2012 by the members listed in the COPYING, * 007 * LICENSE and WARRANTY file. * 008 * email : info at matsim dot org * 009 * * 010 * *********************************************************************** * 011 * * 012 * This program is free software; you can redistribute it and/or modify * 013 * it under the terms of the GNU General Public License as published by * 014 * the Free Software Foundation; either version 2 of the License, or * 015 * (at your option) any later version. * 016 * See also COPYING, LICENSE and WARRANTY file * 017 * * 018 * *********************************************************************** */ 019 020package playground.vsp.analysis.modules.ptLines2PaxAnalysis; 021 022import java.io.BufferedWriter; 023import java.io.IOException; 024import java.text.DecimalFormat; 025import java.util.ArrayList; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.log4j.Logger; 030import org.matsim.api.core.v01.Id; 031import org.matsim.api.core.v01.Scenario; 032import org.matsim.core.config.ConfigUtils; 033import org.matsim.core.events.handler.EventHandler; 034import org.matsim.core.scenario.MutableScenario; 035import org.matsim.core.scenario.ScenarioUtils; 036import org.matsim.core.utils.io.IOUtils; 037import org.matsim.counts.Count; 038import org.matsim.counts.Counts; 039import org.matsim.counts.Volume; 040import org.matsim.pt.transitSchedule.api.TransitLine; 041import org.matsim.pt.transitSchedule.api.TransitRouteStop; 042import org.matsim.pt.transitSchedule.api.TransitScheduleReader; 043import org.matsim.vehicles.MatsimVehicleReader; 044import org.matsim.vehicles.Vehicles; 045 046import playground.vsp.analysis.VspAnalyzer; 047import playground.vsp.analysis.modules.AbstractAnalysisModule; 048import playground.vsp.analysis.modules.ptRoutes2paxAnalysis.CreateRscript; 049 050/** 051 * @author aneumann, fuerbas after droeder 052 * 053 */ 054 055public class PtLines2PaxAnalysis extends AbstractAnalysisModule { 056 057 @SuppressWarnings("unused") 058 private static final Logger log = Logger.getLogger(PtLines2PaxAnalysis.class); 059 private final String SEP = "--"; 060 private PtLines2PaxAnalysisHandler handler; 061 private double interval; 062 private int maxSlices; 063 064 public PtLines2PaxAnalysis(Map<Id<TransitLine>, TransitLine> lines, Vehicles vehicles, double interval, int maxSlices) { 065 super(PtLines2PaxAnalysis.class.getSimpleName()); 066 this.handler = new PtLines2PaxAnalysisHandler(interval, maxSlices, lines, vehicles); 067 this.interval = interval; 068 this.maxSlices = maxSlices; 069 } 070 071 @Override 072 public List<EventHandler> getEventHandler() { 073 List<EventHandler> handler = new ArrayList<EventHandler>(); 074 handler.add(this.handler); 075 return handler; 076 } 077 078 @Override 079 public void preProcessData() { 080 // nothing to do here 081 } 082 083 @Override 084 public void postProcessData() { 085 // nothing to do here 086 } 087 088 @Override 089 public void writeResults(String outputFolder) { 090 String dir = outputFolder; 091 for(TransitLines2PaxCounts tl2c: this.handler.getLinesPaxCounts().values()){ 092 writeLineFiles(dir, tl2c); 093 } 094 CreateRscript.createScriptFromTransitLines2PaxCounts(this.handler.getLinesPaxCounts(), dir, interval, maxSlices); 095 } 096 097 private void writeCounts2File(List<TransitRouteStop> transitRouteStops, int maxSlice, Counts counts, String outputFilename) { 098 BufferedWriter w = IOUtils.getBufferedWriter(outputFilename); 099 try { 100 //create header 101 w.write("index;stopId;name"); 102 DecimalFormat rFormat = new DecimalFormat("00"); 103 for(int i = 0; i < maxSlices; i++){ 104 int begin = (int) (i * interval); 105 //Interval begin, eg. "H08M30" : hour 8, minute 30 106 String beginString = "H" + rFormat.format(begin / (60 * 60)) + "M" + rFormat.format((begin / 60) % 60); 107 int end = (int) ((i + 1) * interval); 108 //Interval end 109 String endString = "H" + rFormat.format(end / (60 * 60)) + "M" + rFormat.format((end / 60) % 60); 110 w.write("; " + beginString + "_to_" + endString); 111 } 112 // measurements after the last slice 113 int begin = (int) (maxSlices * interval); 114 String beginString = "H" + rFormat.format(begin / (60 * 60)) + "M" + rFormat.format((begin / 60) % 60); 115 w.write("; " + beginString); 116 117 w.write("\n"); 118 119 for (int noStops = 0; noStops < transitRouteStops.size(); noStops++) { 120 Id stopId = transitRouteStops.get(noStops).getStopFacility().getId(); 121 Count count = counts.getCount(stopId); 122 w.write(String.valueOf(noStops) + ";" + count.getCsLabel() + ";" + transitRouteStops.get(noStops).getStopFacility().getName()); 123 124 for(int j = 0; j <= maxSlice; j++){ 125 Volume volume = count.getVolume(j+1); 126 String value = (volume == null) ? this.SEP : String.valueOf(volume.getValue()); 127 w.write(";" + value); 128 } 129 w.write("\n"); 130 } 131 132 w.flush(); 133 w.close(); 134 } catch (IOException e) { 135 e.printStackTrace(); 136 } 137 } 138 139 private void writeLineFiles(String dir, TransitLines2PaxCounts tl2c) { 140 for(int i = 0; i < tl2c.getRouteList().size() ; i++){ 141 List<TransitRouteStop> transitRouteStops = tl2c.getRouteList().get(i).getStops(); 142 int maxSlice = tl2c.getMaxSlice().intValue(); 143 String filePrefix = dir + tl2c.getId() + this.SEP + tl2c.getRouteList().get(i).getId().toString(); 144 writeCounts2File(transitRouteStops, maxSlice, tl2c.getAlighting(), filePrefix + this.SEP + "alighting.csv"); 145 writeCounts2File(transitRouteStops, maxSlice, tl2c.getBoarding(), filePrefix + this.SEP + "boarding.csv"); 146 writeCounts2File(transitRouteStops, maxSlice, tl2c.getCapacity(), filePrefix + this.SEP + "capacity.csv"); 147 writeCounts2File(transitRouteStops, maxSlice, tl2c.getOccupancy(), filePrefix + this.SEP + "occupancy.csv"); 148 writeCounts2File(transitRouteStops, maxSlice, tl2c.getTotalPax(), filePrefix + this.SEP + "totalPax.csv"); 149 } 150 } 151 152 public static void main(String[] args) { 153// String dir = "Z:\\WinHome\\workspace\\PtRoutes2PaxAna_Daten\\schedule\\"; 154 String dir = "D:\\runs-svn\\intermodal\\r5-w1000\\"; 155 VspAnalyzer analyzer = new VspAnalyzer(dir, dir + "r5-w1000.output_events.xml.gz"); 156 Scenario sc = ScenarioUtils.createScenario(ConfigUtils.createConfig()); 157 sc.getConfig().transit().setUseTransit(true); 158 new TransitScheduleReader(sc).readFile(dir + "r5-w1000.output_transitSchedule.xml.gz"); 159// new TransitScheduleReader(sc).readFile(dir + "tut_10min_0.0.transitSchedule_1.xml"); //for testing 160 new MatsimVehicleReader(((MutableScenario) sc).getTransitVehicles()).readFile(dir + "r5-w1000.output_transitVehicles.xml.gz" ); 161 PtLines2PaxAnalysis ptLinesPax = new PtLines2PaxAnalysis(sc.getTransitSchedule().getTransitLines(), ((MutableScenario) sc).getTransitVehicles(), 3600, 24); 162 analyzer.addAnalysisModule(ptLinesPax); 163 analyzer.run(); 164 } 165}