Skip to content

Commit

Permalink
Replace hard-coded timestamp with function parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bartromgens committed Feb 28, 2016
1 parent 50dcc27 commit e51c1d4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
5 changes: 3 additions & 2 deletions bin/create_traveltime_data.py
Expand Up @@ -37,8 +37,9 @@ def main():
n_stations += 1
print(station)

stations.create_traveltimes_data(stations_todo)
stations.recreate_missing_destinations(DATA_DIR, False)
timestamp = "19-04-2016 08:00"
stations.create_traveltimes_data(stations_todo, timestamp)
stations.recreate_missing_destinations(DATA_DIR, timestamp, False)


if __name__ == "__main__":
Expand Down
13 changes: 7 additions & 6 deletions nsmaps/station.py
Expand Up @@ -119,13 +119,14 @@ def get_stations_for_types(self, station_types):
selected_stations.append(station)
return selected_stations

def create_traveltimes_data(self, stations_from):
def create_traveltimes_data(self, stations_from, timestamp):
""" timestamp format: DD-MM-YYYY hh:mm """
for station_from in stations_from:
filename_out = station_from.get_travel_time_filepath()
if os.path.exists(filename_out):
logger.warning('File ' + filename_out + ' already exists. Will not overwrite. Return.')
continue
json_data = self.create_trip_data_from_station(station_from)
json_data = self.create_trip_data_from_station(station_from, timestamp)
with open(filename_out, 'w') as fileout:
fileout.write(json_data)

Expand All @@ -135,8 +136,8 @@ def get_station_code(self, station_name):
return station.get_code()
return None

def create_trip_data_from_station(self, station_from):
timestamp = "13-01-2016 08:00"
def create_trip_data_from_station(self, station_from, timestamp):
""" timestamp format: DD-MM-YYYY hh:mm """
via = ""
data = {'stations': []}
data['stations'].append({'name': station_from.get_name(),
Expand Down Expand Up @@ -186,7 +187,7 @@ def get_missing_destinations(self, filename_json):
missing_stations.append(station)
return missing_stations

def recreate_missing_destinations(self, dry_run=False):
def recreate_missing_destinations(self, departure_timestamp, dry_run=False):
ignore_station_ids = ['HRY', 'WTM', 'KRW', 'VMW', 'RTST', 'WIJ', 'SPV', 'SPH']
for station in self.stations:
if not station.has_travel_time_data():
Expand All @@ -198,7 +199,7 @@ def recreate_missing_destinations(self, dry_run=False):
stations_missing_filtered.append(stations_missing)
logger.info(station.get_name() + ' has missing station: ' + station_missing.get_name())
if stations_missing_filtered and not dry_run:
json_data = self.create_trip_data_from_station(station)
json_data = self.create_trip_data_from_station(station, departure_timestamp)
with open(station.get_travel_time_filepath(), 'w') as fileout:
fileout.write(json_data)
else:
Expand Down
14 changes: 7 additions & 7 deletions test.py
Expand Up @@ -99,16 +99,17 @@ def test_get_station_for_types(self):

def test_create_travel_times_data(self):
utrecht = self.stations.find_station("Utrecht Centraal")
departure_timestamp = "19-04-2016 08:00"
self.assertTrue(utrecht)
self.stations.create_traveltimes_data([utrecht])
self.stations.create_traveltimes_data([utrecht], departure_timestamp)
self.assertTrue(os.path.exists(utrecht.get_travel_time_filepath()))
self.assertTrue(utrecht.has_travel_time_data())
self.stations.travel_times_from_json(utrecht.get_travel_time_filepath())
for station in self.stations:
self.assertNotEqual(station.travel_time_min, None)
if station.get_code() != "UT":
self.assertTrue(station.travel_time_min > 0)
self.stations.recreate_missing_destinations()
self.stations.recreate_missing_destinations(departure_timestamp)
os.remove(utrecht.get_travel_time_filepath())
self.assertFalse(utrecht.has_travel_time_data())

Expand All @@ -121,6 +122,7 @@ class TestContourMap(unittest.TestCase):

@classmethod
def setUpClass(cls):
os.mkdir(cls.data_dir)
if os.path.exists(cls.filename_out):
os.remove(cls.filename_out) # remove file from any previous tests
# taken from http://matplotlib.org/examples/pylab_examples/contour_demo.html
Expand All @@ -138,8 +140,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
if os.path.exists(cls.filename_out):
os.remove(cls.filename_out) # remove file from any previous tests
shutil.rmtree(cls.data_dir)

def test_create_json(self):
min_angle = 10
Expand All @@ -151,17 +152,16 @@ def test_create_json(self):
self.assertEqual(checksum, self.checksum)

def test_contour(self):
os.mkdir(self.data_dir)
stations = nsmaps.station.Stations(self.data_dir, test=True)
utrecht = stations.find_station('Utrecht Centraal')
stations.create_traveltimes_data([utrecht])
departure_timestamp = "19-04-2016 08:00"
stations.create_traveltimes_data([utrecht], departure_timestamp)
config = nsmaps.contourmap.ContourPlotConfig()
config = nsmaps.contourmap.TestConfig()
contour = nsmaps.contourmap.Contour(utrecht, stations, config, self.data_dir)
contour_filepath = os.path.join(self.data_dir, self.filename_out)
contour.create_contour_data(contour_filepath)
self.assertTrue(os.path.exists(contour_filepath))
shutil.rmtree(self.data_dir)


class TestUtilGeo(unittest.TestCase):
Expand Down

0 comments on commit e51c1d4

Please sign in to comment.