Code

Code to create .txt files for each bus route (Java)

package labBuses;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.LinkedList;

 

public class ReadCSV {

 

public void run() {

String csvFile = “C:/Users/Kaci/Desktop/april_2013.csv”;

BufferedReader br = null;

String line = “”;

String cvsSplitBy = “,”;

System.out.println(“hey1”);

try {

System.out.println(“hey2”);

br = new BufferedReader(new FileReader(csvFile));

while ((line = br.readLine()) != null) {

 

String[] trip = line.split(cvsSplitBy);

if (!trip[3].substring(1, trip[3].length() – 1)

.equalsIgnoreCase(“Unknown”)) {

try {

String content = trip[1] + “,” + trip[2] + “,”

+ trip[3] + “,” + trip[4] + “,” + trip[5] + “,”

+ trip[8] + “,” + trip[9];

 

String tempFileName = trip[3].substring(1,

trip[3].length() – 1);

 

File file = new File(

“C:/Users/Kaci/Desktop/BusData/april”

+ tempFileName + “.txt”);

 

if (!file.exists()) {

file.createNewFile();

}

 

FileWriter fileWritter = new FileWriter(file, true);

BufferedWriter bufferWritter = new BufferedWriter(

fileWritter);

bufferWritter.write(content);

bufferWritter.newLine();

bufferWritter.close();

 

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

}

 

public static void main(String[] args) {

System.out.println(“hey”);

 

ReadCSV obj = new ReadCSV();

obj.run();

 

System.out.println(“done”);

 

}

 

}

 

 

Code to analyze each .txt files (MatLab)

 

function [times , services] = occupancy( bus )

% finds bus occupancy rate of a bus route and generates graph

%   inputs: cell array of data for a bus #

%   Output: array with the number of people on bus after each minute

 

services = unique(cell2mat(bus(:, 4))); %services that service that route

runs=length(unique(cell2mat(april199(:,2)))) %number of runs

times = zeros(1440, length(services));%each cell represents one minute of the day, starting at 12:00AM

oppdays = cell(30, length(services));%column for each service

days = zeros(1,length(services));%# of days each service operates

for j = 1:length(services)

for i =1:length(bus)

if cell2mat(bus(i,4))==services(j)

board=hour(cell2mat(bus(i,6)))*60+ minute(cell2mat(bus(i,6)));% boarding time, in minutes

alight=hour(cell2mat(bus(i,7)))*60+ minute(cell2mat(bus(i,7)));%alighting time, in minutes

if isempty(board) || isempty(alight) %ignore data that does not have a time stamp

;

else

times((board+1):(alight+1),j)= times((board+1):(alight+1),j)+ 1;%add 1 b/c indices start at 1

end

if any(strcmp(bus(i,1),oppdays(:,j))) %this part for finding opperating days

;

else

oppdays(find(cellfun(‘isempty’,oppdays(:,j)),1),j) = bus(i,1);%add date to oppdays cell

end

end

end

days(j)=30-sum(cellfun(‘isempty’,oppdays(:,j)));

times(:,j) = times(:,j)./days(j);

end

days

 

%PLOT DATA

n=1440;

year = repmat(2013,1,n);

month = zeros(1,n);

day = zeros(1,n);

hour = zeros(1,n);

minutes = 1:n;

seconds = zeros(1,n);

sdate = datenum(year,month,day,hour,minutes, seconds);

plot(sdate,times)

datetick(‘x’,’HHPM’)

h = legend(s);

v = get(h,’title’);

set(v,’string’,’Services’);

str = sprintf(‘Average No. of Patrons on Route %f (April2013)’,bus(1,3));

title(str);

ylabel(‘Patrons’)

xlabel(‘Time’)

 

end