-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUNC_match(LEMO_WorkOrder_LCS, AADT, TRUCK).R
794 lines (777 loc) · 45.9 KB
/
FUNC_match(LEMO_WorkOrder_LCS, AADT, TRUCK).R
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
##########################################################################################################
##########################################################################################################
####################################### join LEMO_WorkOrder/LCS with AADT ################################
AADT_match.func=function(beginCounty, beginPM, endCounty, endPM,
route, rtsfx, from_odom.R, to_odom.R, from_odom.L, to_odom.L, temp_aadt){
#filter the AADT data.frame for route
route_seq.df=temp_aadt[which(temp_aadt$RTE==route),]
#if the given route has a route suffix, filter for it
if (!is.na(rtsfx)){
route_seq.df=route_seq.df[which(route_seq.df$RTE_SFX==rtsfx),]
}else{#if no suffix is given, filter rows that have nor route suffix
route_seq.df=route_seq.df[is.na(route_seq.df$RTE_SFX),]
}
#filter route_seq.df with the right/left alingment for postmile suffix = alignment or NA and corresponding odometer
route_seq_R.df=route_seq.df[which((route_seq.df$PM_PFX=="R" | is.na(route_seq.df$PM_PFX)) & !is.na(route_seq.df$Odometer_Right)),]
route_seq_L.df=route_seq.df[which((route_seq.df$PM_PFX=="L" | is.na(route_seq.df$PM_PFX)) & !is.na(route_seq.df$Odometer_Left)),]
#order the route_seq data.frames by odometer values
#this is because of the find interval function which only works over non-decreasing sequences
route_seq_R.df=route_seq_R.df[order(route_seq_R.df$Odometer_Right),]
route_seq_L.df=route_seq_L.df[order(route_seq_L.df$Odometer_Left),]
#introduce empty vectors for each feature of the AADT database
back_peak_hour.R=c()
back_peak_month.R=c()
back_aadt.R=c()
ahead_peak_hour.R=c()
ahead_peak_month.R=c()
ahead_aadt.R=c()
back_peak_hour.L=c()
back_peak_month.L=c()
back_aadt.L=c()
ahead_peak_hour.L=c()
ahead_peak_month.L=c()
ahead_aadt.L=c()
#### check to see if any matching routes are found
#### or to see if enough information is provided to find a matching
#### e.g., if no odometer value is available, and one og postmile or county values are missing
if (dim(route_seq.df)[1]==0 |
(is.na(from_odom.R) & (is.na(beginPM) | (is.na(beginCounty)))) |
(is.na(from_odom.L) & (is.na(beginPM) | (is.na(beginCounty)))) |
(is.na(to_odom.R) & (is.na(endPM) | (is.na(endCounty)))) |
(is.na(to_odom.L) & (is.na(endPM) | (is.na(endCounty))))
){
return (#if no match was found, return NA
cbind.data.frame("R.back_peak_hour"=NA,
"R.back_peak_month"=NA,
"R.back_aadt"=NA,
"R.ahead_peak_hour"=NA,
"R.ahead_peak_month"=NA,
"R.ahead_aadt"=NA,
"L.back_peak_hour"=NA,
"L.back_peak_month"=NA,
"L.back_aadt"=NA,
"L.ahead_peak_hour"=NA,
"L.ahead_peak_month"=NA,
"L.ahead_aadt"=NA)
)
#### when some matching routes are found in AADT
} else{
begin.ind=NA #identifies the begining of a work order or closure in the sequence of AADTs reported
end.ind=NA #identifies the end of a work order or closure in the sequence of AADTs reported
#### first, the right alingment, pmsfx=R is matched
#### when odometer value is given for the begininig
if (!is.na(from_odom.R)){
#find the section of the road in AADT that contains the begininig of work order or closure
begin.ind=findInterval(from_odom.R, route_seq_R.df$Odometer_Right, rightmost.closed = FALSE)
####some times the given odometer value is before the first interval
####return NAs for workorder or closures that are located before Caltrans's control
if (begin.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA)
# )
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
####some times the given odometer exactly matches one row in the AADT data frame
####for these case, return AADT traffic data as is
if (from_odom.R==route_seq_R.df$Odometer_Right[begin.ind]){
back_peak_hour.R=c(back_peak_hour.R, route_seq_R.df$BACK_PEAK_HOUR[begin.ind])
back_peak_month.R=c(back_peak_month.R, route_seq_R.df$BACK_PEAK_MADT[begin.ind])
back_aadt.R=c(back_aadt.R, route_seq_R.df$BACK_AADT[begin.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seq_R.df$AHEAD_PEAK_HOUR[begin.ind])
ahead_peak_month.R=c(ahead_peak_month.R, route_seq_R.df$AHEAD_PEAK_MADT[begin.ind])
ahead_aadt.R=c(ahead_aadt.R, route_seq_R.df$AHEAD_AADT[begin.ind])
} else {
####for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#####of the begining of the interval and vice versa
back_peak_hour.R=c(back_peak_hour.R, route_seq_R.df$AHEAD_PEAK_HOUR[begin.ind])
back_peak_month.R=c(back_peak_month.R, route_seq_R.df$AHEAD_PEAK_MADT[begin.ind])
back_aadt.R=c(back_aadt.R, route_seq_R.df$AHEAD_AADT[begin.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seq_R.df$BACK_PEAK_HOUR[begin.ind+1])
ahead_peak_month.R=c(ahead_peak_month.R, route_seq_R.df$BACK_PEAK_MADT[begin.ind+1])
ahead_aadt.R=c(ahead_aadt.R, route_seq_R.df$BACK_AADT[begin.ind+1])
}
#### if given odometer values for workorders or closures are missing
#### consider postmile informations
} else{
#### note that both begin county and begin PM are required
if (!is.na(beginCounty) & !is.na(beginPM)){
#filter route_seq_R for county and order it based on postmile values
route_seqPM.df=route_seq_R.df[which(route_seq_R.df$CNTY==beginCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
#find the section of the road in AADT that contains the begininig of work order or closure
begin.ind=findInterval(beginPM, route_seqPM.df$PM, rightmost.closed = FALSE)
####some times the given odometer value is before the first interval
####return NAs for workorder or closures that are located before Caltrans's control
if (begin.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
####some time the given odometer exactly matches one row in the AADT data frame
####for these case, return AADT traffic data as is
if (beginPM==route_seqPM.df$PM[begin.ind]){
back_peak_hour.R=c(back_peak_hour.R, route_seqPM.df$BACK_PEAK_HOUR[begin.ind])
back_peak_month.R=c(back_peak_month.R, route_seqPM.df$BACK_PEAK_MADT[begin.ind])
back_aadt.R=c(back_aadt.R, route_seqPM.df$BACK_AADT[begin.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seqPM.df$AHEAD_PEAK_HOUR[begin.ind])
ahead_peak_month.R=c(ahead_peak_month.R, route_seqPM.df$AHEAD_PEAK_MADT[begin.ind])
ahead_aadt.R=c(ahead_aadt.R, route_seqPM.df$AHEAD_AADT[begin.ind])
} else {
####for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#####of the begining of the interval and vice versa
back_peak_hour.R=c(back_peak_hour.R, route_seqPM.df$AHEAD_PEAK_HOUR[begin.ind])
back_peak_month.R=c(back_peak_month.R, route_seqPM.df$AHEAD_PEAK_MADT[begin.ind])
back_aadt.R=c(back_aadt.R, route_seqPM.df$AHEAD_AADT[begin.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seqPM.df$BACK_PEAK_HOUR[begin.ind+1])
ahead_peak_month.R=c(ahead_peak_month.R, route_seqPM.df$BACK_PEAK_MADT[begin.ind+1])
ahead_aadt.R=c(ahead_aadt.R, route_seqPM.df$BACK_AADT[begin.ind+1])
}
}
}
#### when odometer value is given for the ending
if(!is.na(to_odom.R)){
####find which section of the road contains the end of work order or closure
end.ind=findInterval(to_odom.R, route_seq_R.df$Odometer_Right, rightmost.closed = FALSE)
####some times the given odometer value is before the first interval
####return NAs for workorder or closures that are located before Caltrans's control
if (end.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
#### if end.ind >= begin.ind+1, return all the aadt information in between
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
back_peak_hour.R=c(back_peak_hour.R, route_seq_R.df$BACK_PEAK_HOUR[i])
back_peak_month.R=c(back_peak_month.R, route_seq_R.df$BACK_PEAK_MADT[i])
back_aadt.R=c(back_aadt.R, route_seq_R.df$BACK_AADT[i])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seq_R.df$AHEAD_PEAK_HOUR[i])
ahead_peak_month.R=c(ahead_peak_month.R, route_seq_R.df$AHEAD_PEAK_MADT[i])
ahead_aadt.R=c(ahead_aadt.R, route_seq_R.df$AHEAD_AADT[i])
}
}
####if the end is located after the begining of the lest interval
if (to_odom.R > route_seq_R.df$Odometer_Right[end.ind]){
####when the section that contains end.ind is also the last section of the road
if (length(route_seq_R.df$Odometer_Right)==end.ind){
back_peak_hour.R=c(back_peak_hour.R, route_seq_R.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.R=c(back_peak_month.R, route_seq_R.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.R=c(back_aadt.R, route_seq_R.df$AHEAD_AADT[end.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, NA)
ahead_peak_month.R=c(ahead_peak_month.R, NA)
ahead_aadt.R=c(ahead_aadt.R, NA)
} else{
#### when the route is continued after the ending interval
back_peak_hour.R=c(back_peak_hour.R, route_seq_R.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.R=c(back_peak_month.R, route_seq_R.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.R=c(back_aadt.R, route_seq_R.df$AHEAD_AADT[end.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seq_R.df$BACK_PEAK_HOUR[end.ind+1])
ahead_peak_month.R=c(ahead_peak_month.R, route_seq_R.df$BACK_PEAK_MADT[end.ind+1])
ahead_aadt.R=c(ahead_aadt.R, route_seq_R.df$BACK_AADT[end.ind+1])
}
}
#### when odometer values are NA
#### note that both county and end postmile are required
} else {
if (!is.na(endCounty) & !is.na(endPM)){
#filter route_seq_R for county and order based on postmile values
route_seqPM.df=route_seq_R.df[which(route_seq_R.df$CNTY==endCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
####find which section of the road contains the end of work order or closure
end.ind=findInterval(endPM, route_seqPM.df$PM, rightmost.closed = FALSE)
####some times the given odometer value is before the first interval
####return NAs for workorder or closures that are located before Caltrans's control
if (end.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
####some time the given odometer exactly matches one row in the AADT data frame
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
back_peak_hour.R=c(back_peak_hour.R, route_seqPM.df$BACK_PEAK_HOUR[i])
back_peak_month.R=c(back_peak_month.R, route_seqPM.df$BACK_PEAK_MADT[i])
back_aadt.R=c(back_aadt.R, route_seqPM.df$BACK_AADT[i])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seqPM.df$AHEAD_PEAK_HOUR[i])
ahead_peak_month.R=c(ahead_peak_month.R, route_seqPM.df$AHEAD_PEAK_MADT[i])
ahead_aadt.R=c(ahead_aadt.R, route_seqPM.df$AHEAD_AADT[i])
}
}
#####if the end is located after the begining of the lest interval
if (endPM > route_seqPM.df$PM[end.ind]){
####when the end.ind marks the last interval of the route
if (length(route_seqPM.df$PM)==end.ind){
back_peak_hour.R=c(back_peak_hour.R, route_seqPM.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.R=c(back_peak_month.R, route_seqPM.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.R=c(back_aadt.R, route_seqPM.df$AHEAD_AADT[end.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, NA)
ahead_peak_month.R=c(ahead_peak_month.R, NA)
ahead_aadt.R=c(ahead_aadt.R, NA)
} else{
####when the route is continued after the ending interval
back_peak_hour.R=c(back_peak_hour.R, route_seqPM.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.R=c(back_peak_month.R, route_seqPM.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.R=c(back_aadt.R, route_seqPM.df$AHEAD_AADT[end.ind])
ahead_peak_hour.R=c(ahead_peak_hour.R, route_seqPM.df$BACK_PEAK_HOUR[end.ind+1])
ahead_peak_month.R=c(ahead_peak_month.R, route_seqPM.df$BACK_PEAK_MADT[end.ind+1])
ahead_aadt.R=c(ahead_aadt.R, route_seqPM.df$BACK_AADT[end.ind+1])
}
}
}
}
#################### This section is repeating the above operation for the left alignment ###############
if (!is.na(from_odom.L)){
begin.ind=findInterval(from_odom.L, route_seq_L.df$Odometer_Left, rightmost.closed = FALSE)
if (begin.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
if (from_odom.L==route_seq_L.df$Odometer_Left[begin.ind]){
back_peak_hour.L=c(back_peak_hour.L, route_seq_L.df$BACK_PEAK_HOUR[begin.ind])
back_peak_month.L=c(back_peak_month.L, route_seq_L.df$BACK_PEAK_MADT[begin.ind])
back_aadt.L=c(back_aadt.L, route_seq_L.df$BACK_AADT[begin.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seq_L.df$AHEAD_PEAK_HOUR[begin.ind])
ahead_peak_month.L=c(ahead_peak_month.L, route_seq_L.df$AHEAD_PEAK_MADT[begin.ind])
ahead_aadt.L=c(ahead_aadt.L, route_seq_L.df$AHEAD_AADT[begin.ind])
} else {
#for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#of the begining of the interval and the ahead traffic is equal to the back traffic of the end of the
#interval
back_peak_hour.L=c(back_peak_hour.L, route_seq_L.df$AHEAD_PEAK_HOUR[begin.ind])
back_peak_month.L=c(back_peak_month.L, route_seq_L.df$AHEAD_PEAK_MADT[begin.ind])
back_aadt.L=c(back_aadt.L, route_seq_L.df$AHEAD_AADT[begin.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seq_L.df$BACK_PEAK_HOUR[begin.ind+1])
ahead_peak_month.L=c(ahead_peak_month.L, route_seq_L.df$BACK_PEAK_MADT[begin.ind+1])
ahead_aadt.L=c(ahead_aadt.L, route_seq_L.df$BACK_AADT[begin.ind+1])
}
} else{
if (!is.na(beginCounty) & !is.na(beginPM)){
route_seqPM.df=route_seq_L.df[which(route_seq_L.df$County==beginCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
begin.ind=findInterval(beginPM, route_seqPM.df$PM, rightmost.closed = FALSE)
if (begin.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (beginPM==route_seqPM.df$PM[begin.ind]){
back_peak_hour.L=c(back_peak_hour.L, route_seqPM.df$BACK_PEAK_HOUR[begin.ind])
back_peak_month.L=c(back_peak_month.L, route_seqPM.df$BACK_PEAK_MADT[begin.ind])
back_aadt.L=c(back_aadt.L, route_seqPM.df$BACK_AADT[begin.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seqPM.df$AHEAD_PEAK_HOUR[begin.ind])
ahead_peak_month.L=c(ahead_peak_month.L, route_seqPM.df$AHEAD_PEAK_MADT[begin.ind])
ahead_aadt.L=c(ahead_aadt.L, route_seqPM.df$AHEAD_AADT[begin.ind])
} else {
#for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#of the begining of the interval and the ahead traffic is equal to the back traffic of the end of the
#interval
back_peak_hour.L=c(back_peak_hour.L, route_seqPM.df$AHEAD_PEAK_HOUR[begin.ind])
back_peak_month.L=c(back_peak_month.L, route_seqPM.df$AHEAD_PEAK_MADT[begin.ind])
back_aadt.L=c(back_aadt.L, route_seq_L.df$AHEAD_AADT[begin.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seqPM.df$BACK_PEAK_HOUR[begin.ind+1])
ahead_peak_month.L=c(ahead_peak_month.L, route_seqPM.df$BACK_PEAK_MADT[begin.ind+1])
ahead_aadt.L=c(ahead_aadt.L, route_seqPM.df$BACK_AADT[begin.ind+1])
}
}
}
if(!is.na(to_odom.L)){
#find the interval of to_odom.L in sequence of odometer values
end.ind=findInterval(to_odom.L, route_seq_L.df$Odometer_Left, rightmost.closed = FALSE)
#some times the given odometer value is before the first interval
if (end.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
# if end.ind >= begin.ind+1, return all the aadt information in between
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
back_peak_hour.L=c(back_peak_hour.L, route_seq_L.df$BACK_PEAK_HOUR[i])
back_peak_month.L=c(back_peak_month.L, route_seq_L.df$BACK_PEAK_MADT[i])
back_aadt.L=c(back_aadt.L, route_seq_L.df$BACK_AADT[i])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seq_L.df$AHEAD_PEAK_HOUR[i])
ahead_peak_month.L=c(ahead_peak_month.L, route_seq_L.df$AHEAD_PEAK_MADT[i])
ahead_aadt.L=c(ahead_aadt.L, route_seq_L.df$AHEAD_AADT[i])
}
}
#if the end is located after the begining of the lest interval
if (to_odom.L > route_seq_L.df$Odometer_Left[end.ind]){
#when the end.ind marks the last interval of the route
if (length(route_seq_L.df$Odometer_Left)==end.ind){
back_peak_hour.L=c(back_peak_hour.L, route_seq_L.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.L=c(back_peak_month.L, route_seq_L.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.L=c(back_aadt.L, route_seq_L.df$AHEAD_AADT[end.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, NA)
ahead_peak_month.L=c(ahead_peak_month.L, NA)
ahead_aadt.L=c(ahead_aadt.L, NA)
} else{
#when the route is continued after the ending interval
back_peak_hour.L=c(back_peak_hour.L, route_seq_L.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.L=c(back_peak_month.L, route_seq_L.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.L=c(back_aadt.L, route_seq_L.df$AHEAD_AADT[end.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seq_L.df$BACK_PEAK_HOUR[end.ind+1])
ahead_peak_month.L=c(ahead_peak_month.L, route_seq_L.df$BACK_PEAK_MADT[end.ind+1])
ahead_aadt.L=c(ahead_aadt.L, route_seq_L.df$BACK_AADT[end.ind+1])
}
}
} else {
if (!is.na(endCounty) & !is.na(endPM)){
route_seqPM.df=route_seq_L.df[which(route_seq_L.df$County==endCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
end.ind=findInterval(endPM, route_seqPM.df$PM, rightmost.closed = FALSE)
#this may be required for later use in this if body
if (end.ind==0){
# return (cbind.data.frame("R.back_peak_hour"=NA,
# "R.back_peak_month"=NA,
# "R.back_aadt"=NA,
# "R.ahead_peak_hour"=NA,
# "R.ahead_peak_month"=NA,
# "R.ahead_aadt"=NA,
# "L.back_peak_hour"=NA,
# "L.back_peak_month"=NA,
# "L.back_aadt"=NA,
# "L.ahead_peak_hour"=NA,
# "L.ahead_peak_month"=NA,
# "L.ahead_aadt"=NA))
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
back_peak_hour.L=c(back_peak_hour.L, route_seqPM.df$BACK_PEAK_HOUR[i])
back_peak_month.L=c(back_peak_month.L, route_seqPM.df$BACK_PEAK_MADT[i])
back_aadt.L=c(back_aadt.L, route_seqPM.df$BACK_AADT[i])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seqPM.df$AHEAD_PEAK_HOUR[i])
ahead_peak_month.L=c(ahead_peak_month.L, route_seqPM.df$AHEAD_PEAK_MADT[i])
ahead_aadt.L=c(ahead_aadt.L, route_seqPM.df$AHEAD_AADT[i])
}
}
#if the end is located after the begining of the lest interval
if (endPM > route_seqPM.df$PM[end.ind]){
#when the end.ind marks the last interval of the route
if (length(route_seqPM.df$PM)==end.ind){
back_peak_hour.L=c(back_peak_hour.L, route_seqPM.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.L=c(back_peak_month.L, route_seqPM.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.L=c(back_aadt.L, route_seqPM.df$AHEAD_AADT[end.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, NA)
ahead_peak_month.L=c(ahead_peak_month.L, NA)
ahead_aadt.L=c(ahead_aadt.L, NA)
} else{
#when the route is continued after the ending interval
back_peak_hour.L=c(back_peak_hour.L, route_seqPM.df$AHEAD_PEAK_HOUR[end.ind])
back_peak_month.L=c(back_peak_month.L, route_seqPM.df$AHEAD_PEAK_MADT[end.ind])
back_aadt.L=c(back_aadt.L, route_seqPM.df$AHEAD_AADT[end.ind])
ahead_peak_hour.L=c(ahead_peak_hour.L, route_seqPM.df$BACK_PEAK_HOUR[end.ind+1])
ahead_peak_month.L=c(ahead_peak_month.L, route_seqPM.df$BACK_PEAK_MADT[end.ind+1])
ahead_aadt.L=c(ahead_aadt.L, route_seqPM.df$BACK_AADT[end.ind+1])
}
}
}
}
}
return(cbind.data.frame("R.back_peak_hour"=toString(back_peak_hour.R),
"R.back_peak_month"=toString(back_peak_month.R),
"R.back_aadt"=toString(back_aadt.R),
"R.ahead_peak_hour"=toString(ahead_peak_hour.R),
"R.ahead_peak_month"=toString(ahead_peak_month.R),
"R.ahead_aadt"=toString(ahead_aadt.R),
"L.back_peak_hour"=toString(back_peak_hour.L),
"L.back_peak_month"=toString(back_peak_month.L),
"L.back_aadt"=toString(back_aadt.L),
"L.ahead_peak_hour"=toString(ahead_peak_hour.L),
"L.ahead_peak_month"=toString(ahead_peak_month.L),
"L.ahead_aadt"=toString(ahead_aadt.L)))
}
##########################################################################################################
##########################################################################################################
###################################### join LEMO_WorkOrder/LCS with TRUCK ################################
####this function is also similar to the AADT_match.func
####the only difference is the number of TRUCK data features
TRUCK_match.func=function(beginCounty, beginPM, endCounty, endPM,
route, rtsfx, from_odom.R, to_odom.R, from_odom.L, to_odom.L, temp_truck){
#filter the AADT data.frame for route
route_seq.df=temp_truck[which(temp_tr???uck$RTE==route),]
#if the given route has a route suffix, filter for it
if (!is.na(rtsfx)){
route_seq.df=route_seq.df[which(route_seq.df$RTE_SFX==rtsfx),]
}else{#if no suffix is given, filter rows that have nor route suffix
route_seq.df=route_seq.df[is.na(route_seq.df$RTE_SFX),]
}
#filter route_seq.df with the right/left alingment for postmile suffix = alignment or NA and corresponding odometer
route_seq_R.df=route_seq.df[which(!is.na(route_seq.df$Odometer_Right)),]
route_seq_L.df=route_seq.df[which(!is.na(route_seq.df$Odometer_Left)),]
#order the route_seq data.frames by odometer values
#this is because of the find interval function which only works over non-decreasing sequences
route_seq_R.df=route_seq_R.df[order(route_seq_R.df$Odometer_Right),]
route_seq_L.df=route_seq_L.df[order(route_seq_L.df$Odometer_Left),]
#introduce empty vectors for each feature of the AADT database
#back_truck_aadt.R=c()
#back_truck_percent.R=c()
ahead_truck_aadt.R=c()
#ahead_truck_percent.R=c()
#back_truck_aadt.L=c()
#back_truck_percent.L=c()
ahead_truck_aadt.L=c()
#ahead_truck_percent.L=c()
if (dim(route_seq.df)[1]==0 |
(is.na(from_odom.R) & (is.na(beginPM) | (is.na(beginCounty)))) |
(is.na(from_odom.L) & (is.na(beginPM) | (is.na(beginCounty)))) |
(is.na(to_odom.R) & (is.na(endPM) | (is.na(endCounty)))) |
(is.na(to_odom.L) & (is.na(endPM) | (is.na(endCounty))))
){
return (cbind.data.frame("R.ahead_truck_aadt"=NA,
"L.ahead_truck_aadt"=NA))
} else{
begin.ind=NA
end.ind=NA
if (!is.na(from_odom.R)){
#find the interval of from_odom.R in sequence of odometer values
begin.ind=findInterval(from_odom.R, route_seq_R.df$Odometer_Right, rightmost.closed = FALSE)
#some times the given odometer value is before the first interval
if (begin.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (from_odom.R==route_seq_R.df$Odometer_Right[begin.ind]){
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seq_R.df$TRUCK_AADT[begin.ind])
} else {
#for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#of the begining of the interval and the ahead traffic is equal to the back traffic of the end of the
#interval
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seq_R.df$TRUCK_AADT[begin.ind+1])
}
} else{
if (!is.na(beginCounty) & !is.na(beginPM)){
route_seqPM.df=route_seq_R.df[which(route_seq_R.df$CNTY==beginCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
begin.ind=findInterval(beginPM, route_seqPM.df$PM, rightmost.closed = FALSE)
if (begin.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (beginPM==route_seqPM.df$PM[begin.ind]){
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seqPM.df$TRUCK_AADT[begin.ind])
} else {
#for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#of the begining of the interval and the ahead traffic is equal to the back traffic of the end of the
#interval
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seqPM.df$TRUCK_AADT[begin.ind+1])
}
}
}
if(!is.na(to_odom.R)){
#find the interval of to_odom.R in sequence of odometer values
end.ind=findInterval(to_odom.R, route_seq_R.df$Odometer_Right, rightmost.closed = FALSE)
#some times the given odometer value is before the first interval
if (end.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
# if end.ind >= begin.ind+1, return all the aadt information in between
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seq_R.df$TRUCK_AADT[i])
}
}
#if the end is located after the begining of the lest interval
if (to_odom.R > route_seq_R.df$Odometer_Right[end.ind]){
#when the end.ind marks the last interval of the route
if (length(route_seq_R.df$Odometer_Right)==end.ind){
ahead_truck_aadt.R=c(ahead_truck_aadt.R, NA)
} else{
#when the route is continued after the ending interval
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seq_R.df$TRUCK_AADT[end.ind+1])
}
}
} else {
if (!is.na(endCounty) & !is.na(endPM)){
route_seqPM.df=route_seq_R.df[which(route_seq_R.df$CNTY==endCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
end.ind=findInterval(endPM, route_seqPM.df$PM, rightmost.closed = FALSE)
#this may be required for later use in this if body
if (end.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seqPM.df$TRUCK_AADT[i])
}
}
#if the end is located after the begining of the lest interval
if (endPM > route_seqPM.df$PM[end.ind]){
#when the end.ind marks the last interval of the route
if (length(route_seqPM.df$PM)==end.ind){
ahead_truck_aadt.R=c(ahead_truck_aadt.R, NA)
} else{
#when the route is continued after the ending interval
ahead_truck_aadt.R=c(ahead_truck_aadt.R, route_seqPM.df$`truck.aadt.total B`[end.ind+1])
}
}
}
}
if (!is.na(from_odom.L)){
#find the interval of from_odom.L in sequence of odometer values
begin.ind=findInterval(from_odom.L, route_seq_L.df$Odometer_Left, rightmost.closed = FALSE)
#some times the given odometer value is before the first interval
if (begin.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (from_odom.L==route_seq_L.df$Odometer_Left[begin.ind]){
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seq_L.df$TRUCK_AADT[begin.ind])
} else {
#for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#of the begining of the interval and the ahead traffic is equal to the back traffic of the end of the
#interval
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seq_L.df$TRUCK_AADT[begin.ind+1])
}
} else{
if (!is.na(beginCounty) & !is.na(beginPM)){
route_seqPM.df=route_seq_L.df[which(route_seq_L.df$County==beginCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
begin.ind=findInterval(beginPM, route_seqPM.df$PM, rightmost.closed = FALSE)
if (begin.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (beginPM==route_seqPM.df$PM[begin.ind]){
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seqPM.df$TRUCK_AADT[begin.ind])
} else {
#for the majority that are somewhere in the interval, the back traffic is equal to the ahead traffic
#of the begining of the interval and the ahead traffic is equal to the back traffic of the end of the
#interval
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seqPM.df$TRUCK_AADT[begin.ind+1])
}
}
}
if(!is.na(to_odom.L)){
#find the interval of to_odom.L in sequence of odometer values
end.ind=findInterval(to_odom.L, route_seq_L.df$Odometer_Left, rightmost.closed = FALSE)
#some times the given odometer value is before the first interval
if (end.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
# if end.ind >= begin.ind+1, return all the aadt information in between
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seq_L.df$TRUCK_AADT[i])
}
}
#if the end is located after the begining of the lest interval
if (to_odom.L > route_seq_L.df$Odometer_Left[end.ind]){
#when the end.ind marks the last interval of the route
if (length(route_seq_L.df$Odometer_Left)==end.ind){
ahead_truck_aadt.L=c(ahead_truck_aadt.L, NA)
} else{
#when the route is continued after the ending interval
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seq_L.df$TRUCK_AADT[end.ind+1])
}
}
} else {
if (!is.na(endCounty) & !is.na(endPM)){
route_seqPM.df=route_seq_L.df[which(route_seq_L.df$County==endCounty),]
route_seqPM.df=route_seqPM.df[order(route_seqPM.df$PM),]
end.ind=findInterval(endPM, route_seqPM.df$PM, rightmost.closed = FALSE)
#this may be required for later use in this if body
if (end.ind==0){
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
#some time the given odometer exactly matches one row in the AADT data frame
if (begin.ind < end.ind){
for (i in (begin.ind+1):end.ind){
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seqPM.df$TRUCK_AADT[i])
}
}
#if the end is located after the begining of the lest interval
if (endPM > route_seqPM.df$PM[end.ind]){
#when the end.ind marks the last interval of the route
if (length(route_seqPM.df$PM)==end.ind){
ahead_truck_aadt.L=c(ahead_truck_aadt.L, NA)
} else{
#when the route is continued after the ending interval
ahead_truck_aadt.L=c(ahead_truck_aadt.L, route_seqPM.df$TRUCK_AADT[end.ind+1])
}
}
}
}
}
return(cbind.data.frame("R.ahead_truck_aadt"=toString(ahead_truck_aadt.R),
"L.ahead_truck_aadt"=toString(ahead_truck_aadt.L)))
}
##########################################################################################################
##########################################################################################################
##########################################################################################################
#NOTE:
#check for route_ID
#check for odom
#it should be between two points: x < odom < y
#average aadt= ((odom - x) / (y-x)) * aadt(y) + ((y - odom) / (y-x)) * aadt(x)
#if odom if found exactly, aadt(odom) = aadt(x)