-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoadCarData.m
331 lines (261 loc) · 12.5 KB
/
LoadCarData.m
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
function LoadCarData(filename)
global nummins starttime formatIn formatIn2 formatIn3 inc_min_step offset_time current current_end min_quality
global search_region max_search_time prev_time alldataset dataset newdataset
global indFrictionValue indFrictionQuality ind1PrevDistFriction ind1PrevTimeFriction ind1PrevFrictionValue ind1PrevFrictionQuality
global indTempSMHI indTempRoadVV indTempVV indHumidityVV indDewVV indRainVV indSnowVV indWindVV indWiperSpeedCar indLog
global indLat indMappedLog indMappedLat ind2PrevDistFriction ind2PrevTimeFriction ind2PrevFrictionValue ind3PrevDistFriction
global ind3PrevTimeFriction ind3PrevFrictionValue indTempCar
% Allocate some space
alldataset = zeros(fix(nummins),50);
fid = fopen(filename, 'rt');
raw = textscan(fid, ['%s %s %s',repmat('%f',[1,10])],'Delimiter',',','headerLines', 0);
fclose(fid);
%% Fix the temperature from car data (not used)
% Fill in the gaps in the dataset.
lastTemp = mean(raw{7}(~isnan(raw{7}(:))));
for i=1:length(raw{7}(:))
if ~isnan(raw{7}(i))
lastTemp = raw{7}(i);
end
raw{7}(i) = lastTemp;
end
%% Fix wiperspeed values (not used)
% Fill in the gaps in the dataset.
lastTemp = 0;
for i = 1:length(raw{8})
if(~isnan(raw{8}(i)))
lastTemp = raw{8}(i);
end
raw{8}(i) = lastTemp;
end
%% Load friction values from car data...
disp('Load friction values from car data...')
% Allocate some space for the dataset
dataset = zeros(fix(nummins),64);
% Get start date
current = datenum(starttime,formatIn);
% Dummy variable to optimize the search algorithm
lowestIndex=1;
% Loop through every time interval
for mins=1:nummins
% Progress output
if mod(mins,1000) == 0
fprintf('%f %% done\n',mins/nummins*100);
end
current_end = addtodate(current, inc_min_step, 'minute');
% Set an offset, used to build datasets for the forecast models
current_offset = addtodate(current, -offset_time, 'minute');
current_end_offset = addtodate(current_end, -offset_time, 'minute');
% Dummy variables used to calculate the average
numfriction = 0;
numwiperspeedvalues = 0;
numtempvalues=0;
for m=lowestIndex:length(raw{3}(:))
% Find matching measurement
if (datenum(raw{3}(m),formatIn) >= current) && ...
(datenum(raw{3}(m),formatIn) < current_end) && ...
~isnan(raw{5}(m)) && ...
(raw{6}(m) >= min_quality)
% Save index to dataset
dataset(mins,2) = m;
% Store friction value and quality value
dataset(mins,indFrictionValue) = dataset(mins,indFrictionValue)+raw{5}(m);
dataset(mins,indFrictionQuality) = dataset(mins,indFrictionQuality)+raw{6}(m);
% Save log/lat and mapped log/lat
dataset(mins,indLog) = raw{9}(m);
dataset(mins,indLat) = raw{10}(m);
dataset(mins,indMappedLog) = raw{11}(m);
dataset(mins,indMappedLat) = raw{12}(m);
numfriction = numfriction + 1;
end
% Looking for wiperspeed value
if (datenum(raw{3}(m),formatIn) >= current_offset) && ...
(datenum(raw{3}(m),formatIn) < current_end_offset)
dataset(mins,indWiperSpeedCar) = dataset(mins,indWiperSpeedCar)+raw{8}(m);
numwiperspeedvalues = numwiperspeedvalues + 1;
end
% Look for temperature values
if (datenum(raw{3}(m),formatIn) >= current_offset) && ...
(datenum(raw{3}(m),formatIn) < current_end_offset)
dataset(mins,indTempCar) = dataset(mins,indTempCar)+raw{7}(m);
numtempvalues = numtempvalues + 1;
end
% Optimize the search algorithm
if datenum(raw{3}(m),formatIn) < current_offset
lowestIndex = m;
end
if datenum(raw{3}(m),formatIn) > current_end
dataset(mins,2) = m;
break
end
end
if dataset(mins,indFrictionValue) ~= 0 % Set mean of friction value
dataset(mins,indFrictionValue) = dataset(mins,indFrictionValue)/numfriction;
dataset(mins,indFrictionQuality) = dataset(mins,indFrictionQuality)/numfriction;
end
if dataset(mins,indWiperSpeedCar) ~= 0 % Set mean of wiper speed
dataset(mins,indWiperSpeedCar) = dataset(mins,indWiperSpeedCar)/numwiperspeedvalues;
end
if dataset(mins,indTempCar) ~= 0 % Set mean of temperature
dataset(mins,indTempCar) = dataset(mins,indTempCar)/numtempvalues;
end
% Store the current time
dataset(mins,1) = current;
% Save time stamp
[~,~,~,dataset(mins,8),~,~] = datevec(datestr(current),formatIn2);
% Update the time
current = current_end;
end
hold off
% Copy dataset into newdataset
newdataset = dataset;
size(newdataset)
size(dataset)
%% Find the middle of the road segment (Not used)
middleLog = mean(dataset((dataset(:,indMappedLog) > 0),indMappedLog));
middleLat = mean(dataset((dataset(:,indMappedLat) > 0),indMappedLat));
%% LOAD data from all friction values
fid = fopen('GetAllFrictionValues.csv', 'rt'); %Query5_onlyfriction.csv
raw_all = textscan(fid, ['%s %s %s',repmat('%f',[1,10])],'Delimiter',',','headerLines', 0);
fclose(fid);
% Allocate some space
alldataset = zeros(fix(nummins),50);
disp('Load friction values from all friction values...')
current = datenum(starttime,formatIn);
% Dummy optimizer variable
lowestIndex=1;
% Loop through all time intervals
for mins=1:nummins
% Progress output
if mod(mins,40) == 0
fprintf('%f %% done\n',mins/nummins*100);
end
current_end = addtodate(current, inc_min_step, 'minute');
% Set an offset, used to build datasets for the forecast models
current_offset = addtodate(current, -offset_time, 'minute');
current_end_offset = addtodate(current_end, -offset_time, 'minute');
numfriction = 0;
% Loop through all friction measurements
for m=lowestIndex:length(raw_all{3}(:))
% Find matching measurement
if (datenum(raw_all{3}(m),formatIn) >= current_offset) && ...
(datenum(raw_all{3}(m),formatIn) < current_end_offset) && ...
(raw_all{6}(m) >= min_quality)
alldataset(mins,indFrictionValue) = alldataset(mins,indFrictionValue)+raw_all{5}(m);
alldataset(mins,indFrictionQuality) = alldataset(mins,indFrictionQuality)+raw_all{6}(m);
alldataset(mins,49) = raw_all{9}(m);
alldataset(mins,50) = raw_all{10}(m);
numfriction = numfriction + 1;
end
% Optimize the search algorithm
if datenum(raw_all{3}(m),formatIn) < current_offset
lowestIndex = m;
end
if datenum(raw_all{3}(m),formatIn) > current_end_offset
break
end
end
if alldataset(mins,indFrictionValue) ~= 0 % Set mean friction value and quality
alldataset(mins,indFrictionValue) = alldataset(mins,indFrictionValue)/numfriction;
alldataset(mins,indFrictionQuality) = alldataset(mins,indFrictionQuality)/numfriction;
end
% Save the current time
alldataset(mins,1) = current;
% Update the time mark
current = current_end;
end
%% FIND LAST GLOBAL FRICTIONVALUE (TEST CASE)
disp('Look for global friction values')
% Loop through all time intervals
for mins=1:nummins
% Progress output
if mod(mins,40) == 0
fprintf('%f %% done\n',mins/nummins*100);
end
found_frictionvalue = false;
for searchhour = mins-1:-1:2
if (mins-searchhour)*inc_min_step > 5*60
break;
end
% Find matching measurement
if (alldataset(searchhour,indFrictionValue) ~= 0) && ...
(sqrt((newdataset(mins,indMappedLog)-alldataset(searchhour,49))^2+...
(newdataset(mins,indMappedLat)-alldataset(searchhour,50))^2) < search_region) && ...
((mins-searchhour)*inc_min_step < max_search_time*60) && ... % Max 5 hours
(alldataset(searchhour,indFrictionQuality) >= min_quality) % Qulity needs to be better or equal to min_quality
newdataset(mins,ind1PrevFrictionValue) = alldataset(searchhour,indFrictionValue);
newdataset(mins,ind1PrevFrictionQuality) = alldataset(searchhour,indFrictionQuality);
newdataset(mins,ind1PrevTimeFriction) = mins-searchhour;
if alldataset(searchhour,49) + alldataset(searchhour,50) > 0
newdataset(mins,ind1PrevDistFriction) = sqrt((newdataset(mins,indMappedLog)-alldataset(searchhour,49))^2+...
(newdataset(mins,indMappedLat)-alldataset(searchhour,50))^2);
else
newdataset(mins,ind1PrevDistFriction) = 0;
end
found_frictionvalue = true;
break;
end
end
if found_frictionvalue == false
newdataset(mins,ind1PrevDistFriction) = 0;
newdataset(mins,ind1PrevTimeFriction) = 0;
newdataset(mins,ind1PrevFrictionValue) = 0;
end
found_frictionvalue = false;
for searchhour = searchhour-1:-1:2
if (alldataset(searchhour,indFrictionValue) ~= 0) && ...
(sqrt((newdataset(mins,indMappedLog)-alldataset(searchhour,49))^2+...
(newdataset(mins,indMappedLat)-alldataset(searchhour,50))^2) < search_region) && ...
((mins-searchhour)*inc_min_step < max_search_time*60) && ... % Max 5 hours
(alldataset(searchhour,indFrictionQuality) >= min_quality) % Qulity needs to be better or equal to min_quality
newdataset(mins,ind2PrevFrictionValue) = alldataset(searchhour,indFrictionValue);
newdataset(mins,ind2PrevTimeFriction) = mins-searchhour;
if alldataset(searchhour,49) + alldataset(searchhour,50) > 0
newdataset(mins,ind2PrevDistFriction) = sqrt((newdataset(mins,indMappedLog)-alldataset(searchhour,49))^2+...
(newdataset(mins,indMappedLat)-alldataset(searchhour,50))^2);
else
newdataset(mins,ind2PrevDistFriction) = 0;
end
found_frictionvalue = true;
break;
end
end
if found_frictionvalue == false
newdataset(mins,ind2PrevDistFriction) = 0;
newdataset(mins,ind2PrevTimeFriction) = 2;
newdataset(mins,ind2PrevFrictionValue) = 0.5;
end
found_frictionvalue = false;
for searchhour = searchhour-1:-1:2
if (alldataset(searchhour,indFrictionValue) ~= 0) && ...
(sqrt((newdataset(mins,indMappedLog)-alldataset(searchhour,49))^2+...
(newdataset(mins,indMappedLat)-alldataset(searchhour,50))^2) < search_region) && ...
((mins-searchhour)*inc_min_step < max_search_time*60) && ... % Max 5 hours
(alldataset(searchhour,indFrictionQuality) >= min_quality) % Qulity needs to be better or equal to min_quality
newdataset(mins,ind3PrevFrictionValue) = alldataset(searchhour,indFrictionValue);
newdataset(mins,ind3PrevTimeFriction) = mins-searchhour;
if alldataset(searchhour,49) + alldataset(searchhour,50) > 0
newdataset(mins,ind3PrevDistFriction) = sqrt((newdataset(mins,indMappedLog)-alldataset(searchhour,49))^2+...
(newdataset(mins,indMappedLat)-alldataset(searchhour,50))^2);
else
newdataset(mins,ind3PrevDistFriction) = 0;
end
found_frictionvalue = true;
break;
end
end
if found_frictionvalue == false
newdataset(mins,ind3PrevDistFriction) = 0;
newdataset(mins,ind3PrevTimeFriction) = 2;
newdataset(mins,ind3PrevFrictionValue) = 0.5;
end
end
% Clear every friction value when distance is further then 10 (not used)
%newdataset(newdataset(:,ind1PrevDistFriction) > 10,ind1PrevDistFriction) = 0;
%newdataset(newdataset(:,ind2PrevDistFriction) > 10,ind2PrevDistFriction) = 0;
%newdataset(newdataset(:,ind3PrevDistFriction) > 10,ind3PrevDistFriction) = 0;
% Clear every friction value from 5 hours ago
%newdataset(newdataset(:,ind1PrevTimeFriction) > 300,ind1PrevTimeFriction) = 0;
%newdataset(newdataset(:,ind2PrevTimeFriction) > 300,ind2PrevTimeFriction) = 0;
%newdataset(newdataset(:,ind3PrevTimeFriction) > 300,ind3PrevTimeFriction) = 0;
end