-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask5.py
179 lines (156 loc) · 6.9 KB
/
task5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
import csv
import sys
import datetime
import json
import ast
import os
from os import listdir
import os.path
from os import listdir
from os.path import isfile, join
import os
from distutils.dir_util import copy_tree
from shutil import copytree, copy2
import random
import json
import operator
import os
import http.client
import time
import gzip
from io import BytesIO
import json
from threading import Thread
import threading
from time import sleep
import http.client
import gzip
from io import BytesIO
import json
import random
conn = ''
headersDynamic = []
import _thread
import datetime
from multiprocessing import Process, Lock, Manager, Value
import collections
from difflib import SequenceMatcher
import _thread
import itertools
from itertools import islice
import multiprocessing
import gc
from io import StringIO
from multiprocessing import Pool
import glob
from os import listdir
from os.path import isfile, join
import statistics
import shutil, os
import re
# services = ['Bird', 'Circ', 'Jump', 'Lime', 'LyftScooter', 'Movo', 'Scoot', 'Skip', 'Spin', 'Tier', 'Voi', 'Wind']
# citys = ['DC', 'Detroit', 'Lisbon', 'Madrid', 'MexicoCity', 'Paris', 'SanFrancisco', 'TelAviv', 'Zurich' ]
citys=['Brussels']
# service = 'Tier2019-10-30'
dpath = 'E:/P1'
# full_path = os.path.realpath(__file__)
# dpath, realfilename = os.path.split(full_path)
coordinatesDict = {}
for city in citys:
dataFilesPath = dpath + '/' + city
onlyfiles = [f for f in listdir(dataFilesPath) if isfile(join(dataFilesPath, f))]
service_city_date2num_of_scotter_in_use = {}
service_city_date2all_scotter = {}
for file in onlyfiles:
trips = 0
try:
date = re.findall("\d{4}-\d{2}-\d{2}", file)[0]
service = re.findall("\D{4}", file)[0]
except IndexError:
date = re.findall("\d{4}-\d{2}", file)[0]
service = re.findall("\D{4}", file)[0]
txtFile = dataFilesPath + '/'+file
date2num_of_scotter_in_use = {}
if '.txt' in txtFile and 'lock' not in txtFile:
f = open(txtFile, 'r',encoding='utf-8', errors='ignore')#encoding='utf-8', errors='ignore'
dataDictionary = f.read()
f.close()
print('Reading File: ', txtFile)
sumtime = 0
sumdistance = [0, 0]
avertime = []
averdist = []
scootersDict = json.loads(dataDictionary) # json to python
# scootersDict = list(dataDictionary.keys()) # return keys of datadict to list
tripsDictionary1 = []
tripsDictionary2 = []
for id in scootersDict:
currentlocation = (0,0)
previousEpoch = 0
previousBattery = 0
for epoch in scootersDict[id]:
# location = (latitude,longitude)
latitute = float(scootersDict[id][epoch][1])
longitude = float(scootersDict[id][epoch][2])
location = (latitute, longitude)
if currentlocation == (0, 0):
currentlocation = location
previousEpoch = int(epoch)
previousBattery = float(scootersDict[id][epoch][3])
else:
if location != currentlocation:
# distance
distance = (currentlocation[0] - location[0], currentlocation[1] - location[1])
# trip total time = epoch - previousEpoch
epoch1 = epoch[:10]
previousEpoch1 = previousEpoch[:10]
epoch1 = int(epoch1) # str to int
previousEpoch1 = int(previousEpoch1)
#print(epoch1, previousEpoch1)
try:
epoch2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(epoch1)) # MS to time
previousEpoch2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(previousEpoch1))
#print(epoch2, previousEpoch2)
d1 = datetime.datetime.strptime(epoch2, '%Y-%m-%d %H:%M:%S') # use time to subtract
d2 = datetime.datetime.strptime(previousEpoch2, '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
time = delta.total_seconds() # per seconds
#print(time)
except:
pass
# distance between location and currentlocation
# time difference between epoch, previousEpoch
# difference between batteries previousBattery & scootersDict[id][epoch][5]
if time < 5*3600 and (previousBattery - float(scootersDict[id][epoch][3])) < 0:
dictElement = dict()
# dictElement['source'] = currentlocation
# dictElement['destination'] = location
# # trip total time
dictElement['totaltime'] = time
sumtime += time
# distance
dictElement['distance'] = distance
sumdistance[0] += distance[0]
sumdistance[1] += distance[1]
# day time of the trip
dictElement['daytime'] = time/(3600)
# # scooter company(services)
# dictElement['company'] = service
# # scooter id
# dictElement['scooterid'] = id # MS
tripsDictionary1.append(time)
tripsDictionary2.append(distance)
averagetriptime = sumtime/len(tripsDictionary1)
avertime.append(averagetriptime)
averagetripdistance = (sumdistance[0]/len(tripsDictionary2),sumdistance[1]/len(tripsDictionary2))
averdist.append(averagetripdistance)
f.close()
currentlocation = location
previousEpoch = epoch
previousBattery = float(scootersDict[id][epoch][3])
print("daytime",time/(3600))
print("averagetriptime:", averagetriptime)
#print(avertime)
print("averagetripdistance:",averagetripdistance)
#print(averdist)