-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossSectionErrorPlotFunctions.R
1161 lines (1121 loc) · 82.8 KB
/
CrossSectionErrorPlotFunctions.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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#This file makes cross section plots based on a SpatialPoints cross section.
#The cross section points must be ordered along the cross section for this function to work.
#Typically, sorting the cross section points by latitude or longitude will properly sort the data for cross sections that are not too complex.
library(rgdal)
library(graphics)
#This function deals with jaggedness along a cross section that results from extraction of raster values to points along a line feature.
#The points may jump from one boundary to another and back, so this function switches the order of points to make a smooth cross section.
#This function can handle up to 4 switchbacks along the same interpolation region. If more are needed, you will need to add additional logic statements, or redefine the cross section.
SortMixedIntBounds = function(CrossSec, #Cross section data
IntBoundNames, #Field name for the interpolation boundaries in CrossSec
PositionName #Field name indicating the position of points (e.g. Length)
){
for (i in 1:(length(CrossSec@data[, IntBoundNames])-1)){
if (CrossSec@data[i, IntBoundNames] != CrossSec@data[i+1, IntBoundNames]){
if (CrossSec@data[i, IntBoundNames] == CrossSec@data[i+2, IntBoundNames]){
if (CrossSec@data[i, IntBoundNames] == CrossSec@data[i+4, IntBoundNames]){
#Switch The Points i+1 and i+2
placehold1 = CrossSec@data[i+1,]
placehold2 = CrossSec@data[i+2,]
placehold3 = CrossSec@data[i+3,]
placehold4 = CrossSec@data[i+4,]
CrossSec@data[i+1,] = placehold2
CrossSec@data[i+1,PositionName] = placehold1[, PositionName]
CrossSec@data[i+2,] = placehold4
CrossSec@data[i+2,PositionName] = placehold2[, PositionName]
CrossSec@data[i+3,] = placehold1
CrossSec@data[i+3,PositionName] = placehold3[, PositionName]
CrossSec@data[i+4,] = placehold3
CrossSec@data[i+4,PositionName] = placehold4[, PositionName]
}
else{
#Switch The Points i+1 and i+2
placehold1 = CrossSec@data[i+1,]
placehold2 = CrossSec@data[i+2,]
CrossSec@data[i+2,] = placehold1
CrossSec@data[i+2,PositionName] = placehold2[, PositionName]
CrossSec@data[i+1,] = placehold2
CrossSec@data[i+1,PositionName] = placehold1[, PositionName]
}
}
}
}
return(CrossSec)
}
#This function plots the mean and the error bars about the mean. If there are interpolation boundaries, these are plotted.
#If the SortMixedIntBounds function is not run before this function, interpolation boundaries may appear as both red dots and red dashes.
ErrorBarXC = function(CrossSec, #Data file containing the cross section information. Should be sorted in order of the cross section
PositionName, #Field name in CrossSec indicating the position of points along the cross section (e.g. Length)
PredVar, #Field name of the prediction variable in CrossSec (e.g. predicted mean)
ErrVar, #Field name of the error in the prediction in CrossSec (standard error is assumed)
xLimMin=0, #Minimum value of the x-axis
xLimMax, #Maximum value of the x-axis
yLimBot, #y-axis value for the bottom of the y-axis
yLimTop, #y-axis value for the top of the y-axis
IntBoundLen=0.25, #Distance vertically beyond error bars that the interpolation bounds should extend. This is useful for extending only those error bars that are small. Set to 0 if no extension is desired.
IntBoundNames, #Field name of the interpolation boundaries. Set to 0 if no interpolation boundaries exist.
LinWidBound=1, #Line width of the interpolation boundaries
BoundExtend=0.05 #Additional distance beyond error bars that the interpolation bounds should extend. Set to 0 if no extension is desired.
){
if (IntBoundNames == 0){
print("Making a new field for Interpolation Boundary Names. All values will be None")
CrossSec$IntBoundNames = "None"
}
if (CrossSec@data[PositionName][1,1] != 0){
print("CrossSec@data[PositionName][,1] must start at 0. You can change to other numbers after the cross section has been computed.")
break
}
if ((length(CrossSec@data[PositionName][,1])-1) != (CrossSec@data[PositionName][,1])[length(CrossSec@data[PositionName][,1])]){
print("CrossSec@data[PositionName][,1] must have increments of 1. You can change to other increments after the cross section has been computed.")
break
}
if (yLimBot > yLimTop){
#Case for inverted y-axis
BoundExtend = -BoundExtend
}
SortIVec = vector('numeric',length=length(CrossSec@data[,IntBoundNames]))
for (i in 1:length(CrossSec@data[,IntBoundNames])){
if (i == 1){
if (CrossSec@data[i,IntBoundNames] != CrossSec@data[i+1,IntBoundNames]){
#These are different. Give first location points.
plot(CrossSec@data[PositionName][i,1], (CrossSec@data[i,PredVar] + 2*CrossSec@data[i,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][i,1], (CrossSec@data[i,PredVar] - 2*CrossSec@data[i,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1+1,1],CrossSec@data[PositionName][i-1+1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1+1,1],CrossSec@data[PositionName][i-1+1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
par(new=T)
SortIVec[i] = 1
}
}
else if (i > 2 && i != length(SortIVec)){
#If the location at i doesn't equal the same as the location before i, then they are different.
if (CrossSec@data[i,IntBoundNames] != CrossSec@data[i-1,IntBoundNames]){
#Indicate that this location is a new boundary
SortIVec[i] = 1
#Determine if lines or points are needed for the segment before i.
if (length(which(SortIVec>0)) > 1){
#There's been a boundary identified already.
if ((CrossSec@data[PositionName][i-1,1] - CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]]) >= 1){
#Use Lines
plot(seq(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]],CrossSec@data[PositionName][i-1,1],1), (CrossSec@data[seq(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]]+1,CrossSec@data[PositionName][i-1,1]+1,1),PredVar] + 2*CrossSec@data[seq(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]]+1,CrossSec@data[PositionName][i-1,1]+1,1),ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(seq(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]],CrossSec@data[PositionName][i-1,1],1), (CrossSec@data[seq(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]]+1,CrossSec@data[PositionName][i-1,1]+1,1),PredVar] - 2*CrossSec@data[seq(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))-1]]+1,CrossSec@data[PositionName][i-1,1]+1,1),ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
if (IntBoundLen*(yLimBot-yLimTop) < (max(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar]))
- min(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar])){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(max(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar]) - BoundExtend*(yLimBot-yLimTop), min(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar]) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
par(new=T)
}
else{
#Use Points
plot(CrossSec@data[PositionName][i-1,1], (CrossSec@data[i-1,PredVar] + 2*CrossSec@data[i-1,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][i-1,1], (CrossSec@data[i-1,PredVar] - 2*CrossSec@data[i-1,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
if (length(which(SortIVec>0)) > 2){
if ((which(SortIVec>0)[length(which(SortIVec>0))-1] - which(SortIVec>0)[length(which(SortIVec>0))-2]) == 1){
#These are two points right next to each other. Plot a line for the first and second.
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1+1,1],CrossSec@data[PositionName][i-1+1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1+1,1],CrossSec@data[PositionName][i-1+1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
}
else{
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
}
}
else {
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
}
par(new=T)
}
}
else{
#There's only one boundary so far, so plot up until that boundary.
#Check if there's at least one other point before the location you're plotting up until at (i-1)
if (((i-1) - 1) > 1){
#Use Lines
plot(CrossSec@data[PositionName][,1][1:i-1], (CrossSec@data[1:i-1,PredVar] + 2*CrossSec@data[1:i-1,ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][,1][1:i-1], (CrossSec@data[1:i-1,PredVar] - 2*CrossSec@data[1:i-1,ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
if (IntBoundLen*(yLimBot-yLimTop) < (max(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar]))
- min(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar])){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(max(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar]) - BoundExtend*(yLimBot-yLimTop), min(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar], CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+2,ErrVar]) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
par(new=T)
}
else{
#Use Points
plot(CrossSec@data[PositionName][i-1,1], (CrossSec@data[i-1,PredVar] + 2*CrossSec@data[i-1,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][i-1,1], (CrossSec@data[i-1,PredVar] - 2*CrossSec@data[i-1,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
if (length(which(SortIVec>0)) > 2){
if ((which(SortIVec>0)[length(which(SortIVec>0))-1] - which(SortIVec>0)[length(which(SortIVec>0))-2]) == 1){
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1+1,1],CrossSec@data[PositionName][i-1+1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1+1,1],CrossSec@data[PositionName][i-1+1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
}
else{
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
}
}
else {
if (IntBoundLen*(yLimBot-yLimTop) < ((CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]) - (CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar]))){
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] + BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + 2*CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,ErrVar] - BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
else{
lines(x=c(CrossSec@data[PositionName][i-1,1],CrossSec@data[PositionName][i-1,1]), y=c(CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] + IntBoundLen/2*(yLimBot-yLimTop) - BoundExtend*(yLimBot-yLimTop),CrossSec@data[CrossSec@data[PositionName][i-1,1]+1,PredVar] - IntBoundLen/2*(yLimBot-yLimTop) + BoundExtend*(yLimBot-yLimTop)),lty=2, lwd=LinWidBound)
}
}
par(new=T)
}
}
}
}
else if (i == length(SortIVec)){
#This is for the last segment
if (length(which(SortIVec > 0)) > 0){
if ((i - which(SortIVec > 0)[length(which(SortIVec > 0))]) > 1){
#Use Lines
plot(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))]:i], (CrossSec@data[which(SortIVec > 0)[length(which(SortIVec > 0))]:i,PredVar] + 2*CrossSec@data[which(SortIVec > 0)[length(which(SortIVec > 0))]:i,ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][,1][which(SortIVec > 0)[length(which(SortIVec > 0))]:i], (CrossSec@data[which(SortIVec > 0)[length(which(SortIVec > 0))]:i,PredVar] - 2*CrossSec@data[which(SortIVec > 0)[length(which(SortIVec > 0))]:i,ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=F)
}
else{
#Use Points
plot(CrossSec@data[PositionName][i,1], (CrossSec@data[i,PredVar] + 2*CrossSec@data[i,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][i,1], (CrossSec@data[i,PredVar] - 2*CrossSec@data[i,ErrVar]), type = 'p', pch=16, col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=F)
}
}
else{
#This is the only interpolation boundary. Simply plot the error bars!
plot(CrossSec@data[PositionName][,1], (CrossSec@data[,PredVar] + 2*CrossSec@data[,ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=T)
plot(CrossSec@data[PositionName][,1], (CrossSec@data[,PredVar] - 2*CrossSec@data[,ErrVar]), type = 'l', col='red', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), lty=2, xlab="", ylab="", lwd=2, axes=FALSE)
par(new=F)
}
}
}
}
#A horizontal line is placed from 0 to length(CrossSec), with y-axis position as the average of yMinLinLoc and yMaxLinLoc.
#The names of the counties are placed along this line. The cross section name is placed at either end, with y-axis location determined by yXCNameLoc.
#Assumes that the name of the cross section is Letter-Letter' (e.g. A-A'), so XCName should be just a letter.
#The county names are separated by vertical lines with y-axis placement according to yMinLinLoc and yMaxLinLoc.
NameCounties = function(CrossSec, #Cross Section data
PositionName, #Field name in CrossSec indicating the position of points along the cross section (e.g. Length)
CountyField, #Field name indicating the county for a cross section point (e.g. COUNTYFP).
CountyName, #Names of the counties as a string vector
yTexLoc, #Text location for the county names
yMaxLinLoc, #Maximum y-axis value for the extension of the county divisor bars along the cross section
yMinLinLoc, #Minimum y-axis value for the extension of the county divisor bars along the cross section
yXCNameLoc, #y-axis location of the cross section letter
XCname #Name/Letter of the cross section
){
SortIVec = vector('numeric',length=length(CrossSec@data[PositionName][,1]))
text(x=0, y=yXCNameLoc, XCname, cex=1.5)
text(x=length(CrossSec@data[PositionName][,1]), y=yXCNameLoc, paste(XCname,"'",sep=""), cex=1.5)
lines(x=c(0,length(CrossSec@data[PositionName][,1])),y=c((yMaxLinLoc + yMinLinLoc)/2,(yMaxLinLoc + yMinLinLoc)/2), lwd=2)
lines(x=c(0,0),y=c(yMaxLinLoc, yMinLinLoc), lwd=2)
lines(x=c(length(CrossSec@data[PositionName][,1]),length(CrossSec@data[PositionName][,1])),y=c(yMaxLinLoc, yMinLinLoc), lwd=2)
for (i in 1:length(CrossSec@data[CountyField][,1])){
if (i == 1){
if (CrossSec@data[CountyField][i,1] != CrossSec@data[CountyField][i+1,1]){
#This is a different county.
SortIVec[i] = 1
lines(x=c(CrossSec@data[PositionName][i,1], CrossSec@data[PositionName][i,1]), y=c(yMaxLinLoc, yMinLinLoc), lty=1, lwd=2)
text(x=CrossSec@data[PositionName][i,1]/2, y=yTexLoc, CountyName[length(which(SortIVec>0))], cex=1.2)
}
}
else if (i > 2 && i != length(SortIVec)){
#Check if there have been any boundaries yet
if (length(which(SortIVec>0)) > 0){
#If the location at i doesn't equal the same as the location before i, then they are different.
if (CrossSec@data[CountyField][i,1] != CrossSec@data[CountyField][i-1,1]){
SortIVec[i] = 1
lines(x=c(CrossSec@data[PositionName][i,1], CrossSec@data[PositionName][i,1]), y=c(yMaxLinLoc, yMinLinLoc), lty=1, lwd=2)
text(x=(CrossSec@data[PositionName][,1][which(SortIVec>0)[length(which(SortIVec>0))-1]] + CrossSec@data[PositionName][i,1])/2, y=yTexLoc, CountyName[length(which(SortIVec>0))], cex=1.2)
}
}
else{
#This is the first boundary
#If the location at i doesn't equal the same as the location before i, then they are different.
if (CrossSec@data[CountyField][i,1] != CrossSec@data[CountyField][i-1,1]){
SortIVec[i] = 1
lines(x=c(CrossSec@data[PositionName][i,1], CrossSec@data[PositionName][i,1]), y=c(yMaxLinLoc, yMinLinLoc), lty=1, lwd=2)
text(x=CrossSec@data[PositionName][i,1]/2, y=yTexLoc, CountyName[length(which(SortIVec>0))], cex=1.2)
}
}
}
else if (i == length(SortIVec)){
if (length(which(SortIVec>0)) != 0){
#Give a name to the last county
text(x=(CrossSec@data[PositionName][,1][which(SortIVec>0)[length(which(SortIVec>0))]] + CrossSec@data[PositionName][i,1])/2, y=yTexLoc, CountyName[length(which(SortIVec>0))+1], cex=1.2)
}
else{
#This is the only county in the cross section. Assign it a name.
text(x=(CrossSec@data[PositionName][i,1])/2, y=yTexLoc, CountyName, cex=1.2)
}
}
}
}
#A horizontal line is placed from 0 to length(CrossSec), with y-axis position as the average of yMinLinLoc and yMaxLinLoc.
#The names of the states are placed along this line, and the cross section name is placed at either end, with y-axis location determined by yXCNameLoc.
#Assumes that the name of the cross section is Letter-Letter' (e.g. A-A'), so XCName should be just a letter.
#The state names are separated by vertical lines with y-axis placement according to yMinLinLoc and yMaxLinLoc.
NameStates = function(CrossSec, #Cross Section data
PositionName, #Field name in CrossSec indicating the position of points along the cross section (e.g. Length)
StateField, #Field name indicating the state for a cross section point (e.g. STATEFP).
StateName, #Names of the states as a string vector
yTexLoc, #Text location for the state names
yMaxLinLoc, #Maximum y-axis value for the extension of the state divisor bars along the cross section
yMinLinLoc, #Minimum y-axis value for the extension of the state divisor bars along the cross section
yXCNameLoc, #y-axis location of the cross section letter
XCname #Name/Letter of the cross section
){
SortIVec = vector('numeric',length=length(CrossSec@data[PositionName][,1]))
text(x=0, y=yXCNameLoc, XCname, cex=1.5)
text(x=length(CrossSec@data[PositionName][,1]), y=yXCNameLoc, paste(XCname,"'",sep=""), cex=1.5)
lines(x=c(0,length(CrossSec@data[PositionName][,1])),y=c((yMaxLinLoc + yMinLinLoc)/2,(yMaxLinLoc + yMinLinLoc)/2), lwd=2)
lines(x=c(0,0),y=c(yMaxLinLoc, yMinLinLoc), lwd=2)
lines(x=c(length(CrossSec@data[PositionName][,1]),length(CrossSec@data[PositionName][,1])),y=c(yMaxLinLoc, yMinLinLoc), lwd=2)
for (i in 1:length(CrossSec@data[StateField][,1])){
if (i == 1){
if (CrossSec@data[StateField][i,1] != CrossSec@data[StateField][i+1,1]){
#This is a different state.
SortIVec[i] = 1
lines(x=c(CrossSec@data[PositionName][i,1], CrossSec@data[PositionName][i,1]), y=c(yMaxLinLoc, yMinLinLoc), lty=1, lwd=2)
text(x=CrossSec@data[PositionName][i,1]/2, y=yTexLoc, StateName[length(which(SortIVec>0))], cex=1.2)
}
}
else if (i > 2 && i != length(SortIVec)){
#Check if there have been any boundaries yet
if (length(which(SortIVec>0)) > 0){
#If the location at i doesn't equal the same as the location before i, then they are different.
if (CrossSec@data[StateField][i,1] != CrossSec@data[StateField][i-1,1]){
SortIVec[i] = 1
lines(x=c(CrossSec@data[PositionName][i,1], CrossSec@data[PositionName][i,1]), y=c(yMaxLinLoc, yMinLinLoc), lty=1, lwd=2)
text(x=(CrossSec@data[PositionName][,1][which(SortIVec>0)[length(which(SortIVec>0))-1]] + CrossSec@data[PositionName][i,1])/2, y=yTexLoc, StateName[length(which(SortIVec>0))], cex=1.2)
}
}
else{
#This is the first boundary
#If the location at i doesn't equal the same as the location before i, then they are different.
if (CrossSec@data[StateField][i,1] != CrossSec@data[StateField][i-1,1]){
SortIVec[i] = 1
lines(x=c(CrossSec@data[PositionName][i,1], CrossSec@data[PositionName][i,1]), y=c(yMaxLinLoc, yMinLinLoc), lty=1, lwd=2)
text(x=CrossSec@data[PositionName][i,1]/2, y=yTexLoc, StateName[length(which(SortIVec>0))], cex=1.2)
}
}
}
else if (i == length(SortIVec)){
if (length(which(SortIVec>0)) != 0){
#Give a name to the last state
text(x=(CrossSec@data[PositionName][,1][which(SortIVec>0)[length(which(SortIVec>0))]] + CrossSec@data[PositionName][i,1])/2, y=yTexLoc, StateName[length(which(SortIVec>0))+1], cex=1.2)
}
else{
#This is the only state in the cross section. Assign it a name.
text(x=(CrossSec@data[PositionName][i,1])/2, y=yTexLoc, StateName, cex=1.2)
}
}
}
}
#Example Cross Section Plot and Processing
setwd()
XSG = readOGR(dsn=getwd(), layer="CrossSectionG")
XSG_sort = XSG[do.call(order, XSG@data['POINT_X']), ]
#Add a PositionName field called Length
XSG_sort$Length = 0
for (i in 1:length(XSG_sort$POINT_X)){
XSG_sort$Length[i] = i-1
}
rm(i)
png(filename="ExampleCrossSection_BeforeRunningSortMixedIntBounds.png", width = 1500, height = 1200, res=150)
plot(XSG_sort$Length, XSG_sort$D80Cp, type = 'l', xlim=c(0,275), ylim = c(6000, -1000), main=expression(paste("Depth to 80 ",degree,"C Along Cross Section G-G'")), xlab="Distance (km)", ylab="Depth (m)", lwd=2, cex.lab=1.5, cex.axis=1.5,cex.main=1.5)
par(new=T)
ErrorBarXC(CrossSec=XSG_sort, PositionName='Length', PredVar="D80Cp", ErrVar="D80Ce", xLimMin=0, xLimMax=275, yLimBot=6000, yLimTop=-1000, IntBoundLen=0.25, IntBoundNames="WormSect", LinWidBound=1, BoundExtend=0.05)
CountyName = c('Potter, PA','Tioga, PA','','Chemung, NY','Tompkins, NY')
text(152,-1000,'Steuben, NY',cex=1.1)
NameCounties(CrossSec=XSG_sort, PositionName='Length', CountyField = 'COUNTYFP', CountyName = CountyName, yTexLoc = -1000, yMaxLinLoc = -700, yMinLinLoc = -1100, yXCNameLoc = -600, XCname = "G")
legend(130,4000,legend=c(expression(paste('Predicted Mean (',hat(mu),")")), expression(paste(hat(mu) %+-% 2, "SE")), "Interpolation Boundary"), lty=c(1,2,2), lwd=c(2,2,1), col=c('black','red','black'), bty="n", cex=1.5, seg.len=1.5, x.intersp = 0.5, y.intersp = 1.1)
dev.off()
#Check if any of the interpolation boundaries switchback
XSG_sort = SortMixedIntBounds(XSG_sort, "WormSect", "Length")
png(filename="ExampleCrossSection_AfterRunningSortMixedIntBounds.png", width = 1500, height = 1200, res=150)
plot(XSG_sort$Length, XSG_sort$D80Cp, type = 'l', xlim=c(0,275), ylim = c(6000, -1000), main=expression(paste("Depth to 80 ",degree,"C Along Cross Section G-G'")), xlab="Distance (km)", ylab="Depth (m)", lwd=2, cex.lab=1.5, cex.axis=1.5,cex.main=1.5)
par(new=T)
ErrorBarXC(CrossSec=XSG_sort, PositionName='Length', PredVar="D80Cp", ErrVar="D80Ce", xLimMin=0, xLimMax=275, yLimBot=6000, yLimTop=-1000, IntBoundLen=0.25, IntBoundNames="WormSect", LinWidBound=1, BoundExtend=0.05)
CountyName = c('Potter, PA','Tioga, PA','','Chemung, NY','Tompkins, NY')
text(152,-1000,'Steuben, NY',cex=1.1)
NameCounties(CrossSec=XSG_sort, PositionName='Length', CountyField = 'COUNTYFP', CountyName = CountyName, yTexLoc = -1000, yMaxLinLoc = -700, yMinLinLoc = -1100, yXCNameLoc = -600, XCname = "G")
legend(130,4000,legend=c(expression(paste('Predicted Mean (',hat(mu),")")), expression(paste(hat(mu) %+-% 2, "SE")), "Interpolation Boundary"), lty=c(1,2,2), lwd=c(2,2,1), col=c('black','red','black'), bty="n", cex=1.5, seg.len=1.5, x.intersp = 0.5, y.intersp = 1.1)
dev.off()
png(filename="ExampleCrossSection_NoBoundaryExtension.png", width = 1500, height = 1200, res=150)
plot(XSG_sort$Length, XSG_sort$D80Cp, type = 'l', xlim=c(0,275), ylim = c(6000, -1000), main=expression(paste("Depth to 80 ",degree,"C Along Cross Section G-G'")), xlab="Distance (km)", ylab="Depth (m)", lwd=2, cex.lab=1.5, cex.axis=1.5,cex.main=1.5)
par(new=T)
ErrorBarXC(CrossSec=XSG_sort, PositionName='Length', PredVar="D80Cp", ErrVar="D80Ce", xLimMin=0, xLimMax=275, yLimBot=6000, yLimTop=-1000, IntBoundLen=0, IntBoundNames="WormSect", LinWidBound=1, BoundExtend=0)
CountyName = c('Potter, PA','Tioga, PA','','Chemung, NY','Tompkins, NY')
text(152,-1000,'Steuben, NY',cex=1.1)
NameCounties(CrossSec=XSG_sort, PositionName='Length', CountyField = 'COUNTYFP', CountyName = CountyName, yTexLoc = -1000, yMaxLinLoc = -700, yMinLinLoc = -1100, yXCNameLoc = -600, XCname = "G")
legend(130,4000,legend=c(expression(paste('Predicted Mean (',hat(mu),")")), expression(paste(hat(mu) %+-% 2, "SE")), "Interpolation Boundary"), lty=c(1,2,2), lwd=c(2,2,1), col=c('black','red','black'), bty="n", cex=1.5, seg.len=1.5, x.intersp = 0.5, y.intersp = 1.1)
dev.off()
png(filename="ExampleCrossSection_SelectiveBoundaryExtension.png", width = 1500, height = 1200, res=150)
plot(XSG_sort$Length, XSG_sort$D80Cp, type = 'l', xlim=c(0,275), ylim = c(6000, -1000), main=expression(paste("Depth to 80 ",degree,"C Along Cross Section G-G'")), xlab="Distance (km)", ylab="Depth (m)", lwd=2, cex.lab=1.5, cex.axis=1.5,cex.main=1.5)
par(new=T)
ErrorBarXC(CrossSec=XSG_sort, PositionName='Length', PredVar="D80Cp", ErrVar="D80Ce", xLimMin=0, xLimMax=275, yLimBot=6000, yLimTop=-1000, IntBoundLen=0.25, IntBoundNames="WormSect", LinWidBound=1, BoundExtend=0)
CountyName = c('Potter, PA','Tioga, PA','','Chemung, NY','Tompkins, NY')
text(152,-1000,'Steuben, NY',cex=1.1)
NameCounties(CrossSec=XSG_sort, PositionName='Length', CountyField = 'COUNTYFP', CountyName = CountyName, yTexLoc = -1000, yMaxLinLoc = -700, yMinLinLoc = -1100, yXCNameLoc = -600, XCname = "G")
legend(130,4000,legend=c(expression(paste('Predicted Mean (',hat(mu),")")), expression(paste(hat(mu) %+-% 2, "SE")), "Interpolation Boundary"), lty=c(1,2,2), lwd=c(2,2,1), col=c('black','red','black'), bty="n", cex=1.5, seg.len=1.5, x.intersp = 0.5, y.intersp = 1.1)
dev.off()
#### Reservoirs ####
#Function for placing reservoirs onto the cross section plots.
#The rectangle option can likely be improved by recording the joints of the lines as points, and then when the reservoir terminates, use lines() to connect them all.
#It's important to note that if this function is used along with the SortMixedIntegerBounds, the points that are switched will need to have their data switched back for the reservoirs to plot properly. Example below for important parameters.
#Currently does not handle if there are two reservoirs at the same depth with different formation thicknesses
PlotReservoirs = function(CrossSec, #Cross section name
ResData, #Database containing the reservoir information
Res, #Field used to indicate presence of a reservoir
Thermal, #Thermal field for plotting.
Depth = NA, #Field giving depth of reservoir (optional)
FormThick = NA, #Field for formation thickness (optional)
NewAxis = NA, #Indicates if a new depth axis should be made on side 4. (optional)
Rects = 0, #Indicates if reservoirs should be plotted as rectangles (1) or as blue/green points (0, default)
yResBot = NA, #Second y-axis value at bottom. Also used for plotting y-axis when sepResPlot is not NA (optional)
yResTop = NA, #Second y-axis value at top. Also used for plotting y-axis when sepResPlot is not NA (optional)
colResTop = 'blue', #Color of the top of the reservoir for points option (Rects = 0)
colResBot = 'green', #Color of the bottom of the reservoir for points option (Rects = 0)
colMean = 'blue', #Color indicating presence of a reservoir on the mean line
colResRectsField = NA, #Field name to color rectangles by
colResRects = NA, #vector or scalar of colors for the rectangles.
colSeps = NA, #Vector of numerical values that determine the color separation in colResRects.
transpar = 0.3, #Transparency parameter for the rectangles
sepResPlot = NA, #Do you want a second plot for reservoirs below the temperature plot? Only possible to use when NewAxis is NA.
sepPlotCexAxis = 1.5, #Character expansion for the axis on the reservoir only plot.
sepPlotCexLab = 1.5, #Character expansion for the labels on the reservoir only plot.
xLimMin, #x-axis start
xLimMax, #x-axis stop
yLimBot, #y-axis value at bottom
yLimTop #y-axis Value at top
){
if (is.na(colResRectsField) == FALSE){
#Make the colors transparent
colors = adjustcolor(colResRects, alpha=transpar)
}
RemResInds = NA
if (is.na(Depth)){
#This is for plotting the blue sections along the mean line.
if (is.na(sepResPlot)){
PlotBack = -1
#Loop through all points along the cross section
for (i in 1:length(CrossSec@data$Length)){
#If there are reservoirs at this location, plot an indicator along the mean.
if (PlotBack != -1){
if (CrossSec@data[Res][i,] == 0){
#This means that the reservoir section has ended. Plot the data back to PlotBack
par(new=T)
plot(CrossSec@data$Length[PlotBack:(i-1)], CrossSec@data[Thermal][PlotBack:(i-1),], col=colMean, type='l', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab="", ylab="", lwd=2, axes=FALSE)
#Set PlotBack to -1
PlotBack = -1
}
}
else if (CrossSec@data[Res][i,] > 0){
#Used to check for how far the reservoirs are present
PlotBack = i
}
}
}
else{
#Plot the temperature on a split plot.
PlotBack = -1
#Loop through all points along the cross section
for (i in 1:length(CrossSec@data$Length)){
#If there are reservoirs at this location, plot an indicator along the mean.
if (PlotBack != -1){
if (CrossSec@data[Res][i,] == 0){
#This means that the reservoir section has ended. Plot the data back to PlotBack
par(mfg=c(1,1))
plot(CrossSec@data$Length[PlotBack:(i-1)], CrossSec@data[Thermal][PlotBack:(i-1),], col=colMean, type='l', xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab="", ylab="", lwd=2, axes=FALSE)
#Set PlotBack to -1
PlotBack = -1
}
}
else if (CrossSec@data[Res][i,] > 0){
#Used to check for how far the reservoirs are present
PlotBack = i
}
}
}
}
else{
#Depth field is specified. Check to see if thermal variable is a depth to temp or temp at depth
if (is.na(NewAxis)){
if (is.na(sepResPlot)){
#No new axis is needed (depth to temp variable) and no new plot is needed. Plot the reservoirs on the existing y-axis
PlotBack = -1
#Loop through all points along the cross section
for (i in 1:length(CrossSec@data$Length)){
#If there are reservoirs at this location, plot them at their depth.
if (PlotBack != -1){
ResSpotCheck = ResData@data[which(ResData@data$Length == CrossSec@data$Length[i]),]
ResSpotCheck$Start = -1
if (length(ResSpotCheck)>0){
Inds2 = which(ResSpotCheck[Depth][,1]==0)
if (length(Inds2) > 0){
#Remove reservoirs with 0 depth
ResSpotCheck = ResSpotCheck[-Inds2,]
}
}
if (CrossSec@data[Res][i,] != 0){
if (Rects == 0){
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), axes=FALSE, xlab='', ylab='', col=colResTop, pch=16)
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1]+ResSpotCheck[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#The reservoirs could go back to ResSpot$Start, but not all reservoirs have to continue. Check to see if all reservoirs in ResSpot exist here.
if (length(ResSpot$Start) > 0){
#The reservoir could be continued from the previous set of reservoirs
for (j in 1:length(ResSpot[Depth][,1])){
if (any((ResSpotCheck[Depth][,1] == ResSpot[Depth][j,1])) == FALSE){
#This reservoir has terminated. Plot it back to the ResSpot$Start location.
if (is.na(colResRectsField) == FALSE){
#Get the color for the reservoir field specified
color = NA
for (k in 1:length(colSeps)){
if (ResSpot[colResRectsField][j,1] < colSeps[k]){
color = colors[k]
break
}
if (k == length(colSeps)){
color = colors[k+1]
}
}
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=color)
}
else{
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=adjustcolor(colResRects, alpha=transpar))
}
#Remove this reservoir from ResSpot after loop concludes
if (length(RemResInds) > 1){
RemResInds = c(RemResInds, j)
}
else if (is.na(RemResInds)){
RemResInds = j
}
else{
RemResInds = c(RemResInds, j)
}
}
}
}
#Now check to see if there are any new reservoirs that begin and add them to the ResSpot.
for (k in 1:length(ResSpotCheck[Depth][,1])){
if (any(ResSpot[Depth][,1] == ResSpotCheck[Depth][k,1]) == FALSE){
#Add this reservoir to ResSpot with the new start location.
ResSpotCheck$Start[k] = (i-1)
ResSpot = rbind(ResSpot, ResSpotCheck[k,])
}
}
#Remove reservoirs that terminated.
if (length(RemResInds) > 1){
ResSpot = ResSpot[-RemResInds,]
#Set RemResInds back to NA
RemResInds = NA
}
else if (is.na(RemResInds) == FALSE){
ResSpot = ResSpot[-RemResInds,]
#Set RemResInds back to NA
RemResInds = NA
}
}
}
if (CrossSec@data[Res][i,] == 0){
if (length(ResSpot$Start) > 0){
#The reservoir could be continued from the previous set of reservoirs
for (j in 1:length(ResSpot[Depth][,1])){
#This reservoir has terminated. Plot it back to the ResSpot$Start location.
if (is.na(colResRectsField) == FALSE){
#Get the color for the reservoir field specified
color = NA
for (k in 1:length(colSeps)){
if (ResSpot[colResRectsField][j,1] < colSeps[k]){
color = colors[k]
break
}
if (k == length(colSeps)){
color = colors[k+1]
}
}
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=color)
}
else{
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=adjustcolor(colResRects, alpha=transpar))
}
}
}
#Set PlotBack to 0 and clear contents of ResSpot
PlotBack = -1
ResSpot = NA
}
}
else if (CrossSec@data[Res][i,] > 0){
#Used to check for how far the reservoirs are present
PlotBack = i
#Get all the reservoirs at this spot from the database
ResSpot = ResData@data[which(ResData$Length == CrossSec@data$Length[i]),]
if (all(ResSpot[Depth][,1] == 0)){
PlotBack = -1
ResSpot = NA
}
else if (any(ResSpot[Depth][,1] == 0)){
Inds = which(ResSpot[Depth][,1]==0)
ResSpot = ResSpot[-Inds,]
if (Rects == 0){
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResTop, pch=16)
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1]+ResSpot[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Do not plot anything. Record the position of the start of the reservoir for each reservoir.
ResSpot$Start = (i-1)
}
}
else{
if (Rects == 0){
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResTop, pch=16)
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1]+ResSpot[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Do not plot anything. Record the position of the start of the reservoir for each reservoir.
ResSpot$Start = (i-1)
}
}
}
}
}
else{
#Plot the reservoirs and temperatures on different plots.
PlotCount = 0
PlotBack = -1
#Loop through all points along the cross section
for (i in 1:length(CrossSec@data$Length)){
#If there are reservoirs at this location, plot them at their depth.
if (PlotBack != -1){
ResSpotCheck = ResData@data[which(ResData@data$Length == CrossSec@data$Length[i]),]
ResSpotCheck$Start = -1
if (length(ResSpotCheck)>0){
Inds2 = which(ResSpotCheck[Depth][,1]==0)
if (length(Inds2) > 0){
#Remove reservoirs with 0 depth
ResSpotCheck = ResSpotCheck[-Inds2,]
}
}
if (CrossSec@data[Res][i,] != 0){
if (Rects == 0){
par(mfg=c(2,1))
if (PlotCount == 0){
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='Distance (km)', ylab='Depth (m)', col=colResTop, pch=16)
PlotCount = 1
}
else{
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1], xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', axes=FALSE, col=colResTop, pch=16)
}
par(mfg=c(2,1))
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1]+ResSpotCheck[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Call a dummy plot to establish plotting position
if (PlotCount == 0){
#cex is larger than 1.8 because of the split plot.
plot(-1000,-1000, xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='Distance (km)', ylab='Reservoir Depth (m)', cex.axis=sepPlotCexAxis, cex.lab = sepPlotCexLab)
PlotCount = 1
}
#The reservoirs could go back to ResSpot$Start, but not all reservoirs have to continue. Check to see if all reservoirs in ResSpot exist here.
if (length(ResSpot$Start) > 0){
#The reservoir could be continued from the previous set of reservoirs
for (j in 1:length(ResSpot[Depth][,1])){
if (any((ResSpotCheck[Depth][,1] == ResSpot[Depth][j,1])) == FALSE){
#This reservoir has terminated. Plot it back to the ResSpot$Start location.
if (is.na(colResRectsField) == FALSE){
#Get the color for the reservoir field specified
color = NA
for (k in 1:length(colSeps)){
if (ResSpot[colResRectsField][j,1] < colSeps[k]){
color = colors[k]
break
}
if (k == length(colSeps)){
color = colors[k+1]
}
}
par(mfg=c(2,1))
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=color)
}
else{
par(mfg=c(2,1))
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=adjustcolor(colResRects, alpha=transpar))
}
#Remove this reservoir from ResSpot after loop concludes
if (length(RemResInds) > 1){
RemResInds = c(RemResInds, j)
}
else if (is.na(RemResInds)){
RemResInds = j
}
else{
RemResInds = c(RemResInds, j)
}
}
}
}
#Now check to see if there are any new reservoirs that begin and add them to the ResSpot.
for (k in 1:length(ResSpotCheck[Depth][,1])){
if (any(ResSpot[Depth][,1] == ResSpotCheck[Depth][k,1]) == FALSE){
#Add this reservoir to ResSpot with the new start location.
ResSpotCheck$Start[k] = (i-1)
ResSpot = rbind(ResSpot, ResSpotCheck[k,])
}
}
#Remove reservoirs that terminated.
if (length(RemResInds) > 1){
ResSpot = ResSpot[-RemResInds,]
#Set RemResInds back to NA
RemResInds = NA
}
else if (is.na(RemResInds) == FALSE){
ResSpot = ResSpot[-RemResInds,]
#Set RemResInds back to NA
RemResInds = NA
}
}
}
if (CrossSec@data[Res][i,] == 0){
if (length(ResSpot$Start) > 0){
#The reservoir could be continued from the previous set of reservoirs
for (j in 1:length(ResSpot[Depth][,1])){
#This reservoir has terminated. Plot it back to the ResSpot$Start location.
if (is.na(colResRectsField) == FALSE){
#Get the color for the reservoir field specified
color = NA
for (k in 1:length(colSeps)){
if (ResSpot[colResRectsField][j,1] < colSeps[k]){
color = colors[k]
break
}
if (k == length(colSeps)){
color = colors[k+1]
}
}
par(mfg=c(2,1))
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=color)
}
else{
par(mfg=c(2,1))
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=adjustcolor(colResRects, alpha=transpar))
}
}
}
#Set PlotBack to 0 and clear contents of ResSpot
PlotBack = -1
ResSpot = NA
}
}
else if (CrossSec@data[Res][i,] > 0){
#Used to check for how far the reservoirs are present
PlotBack = i
#Get all the reservoirs at this spot from the database
ResSpot = ResData@data[which(ResData$Length == CrossSec@data$Length[i]),]
if (all(ResSpot[Depth][,1] == 0)){
PlotBack = -1
ResSpot = NA
}
else if (any(ResSpot[Depth][,1] == 0)){
Inds = which(ResSpot[Depth][,1]==0)
ResSpot = ResSpot[-Inds,]
if (Rects == 0){
par(mfg=c(2,1))
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResTop, pch=16)
par(mfg=c(2,1))
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1]+ResSpot[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Do not plot anything. Record the position of the start of the reservoir for each reservoir.
ResSpot$Start = (i-1)
}
}
else{
if (Rects == 0){
par(mfg=c(2,1))
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResTop, pch=16)
par(mfg=c(2,1))
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1]+ResSpot[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yLimBot,yLimTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Do not plot anything. Record the position of the start of the reservoir for each reservoir.
ResSpot$Start = (i-1)
}
}
}
}
}
}
else{
#A new depth axis is needed for the reservoirs. Add it on the right.
par(new=T)
plot(-200, -200, xlim=c(xLimMin,xLimMax), ylim = c(yResBot,yResTop), xlab="", ylab="", lwd=2, axes=FALSE)
mtext("Reservoir Depth (m)",side=4,line=3, cex=1.8)
axis(4, ylim=c(yResBot,yResTop), cex.axis=1.8)
PlotBack = -1
#Loop through all points along the cross section
for (i in 1:length(CrossSec@data$Length)){
#If there are reservoirs at this location, plot them at their depth.
if (PlotBack != -1){
#Gather reservoirs at the new location.
ResSpotCheck = ResData@data[which(ResData@data$Length == CrossSec@data$Length[i]),]
ResSpotCheck$Start = -1
if (length(ResSpotCheck[,1])>0){
Inds2 = which(ResSpotCheck[Depth][,1]==0)
if (length(Inds2) > 0){
#Remove reservoirs with 0 depth
ResSpotCheck = ResSpotCheck[-Inds2,]
#May need to check if all rows were deleted.
}
}
if (CrossSec@data[Res][,1][i] != 0){
#There are reservoirs in this location.
if (Rects == 0){
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), axes=FALSE, xlab='', ylab='', col=colResTop, pch=16)
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpotCheck[Depth][,1])), ResSpotCheck[Depth][,1]+ResSpotCheck[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#The reservoirs could go back to ResSpot$Start, but not all reservoirs have to continue. Check to see if all reservoirs in ResSpot exist here.
if (length(ResSpot$Start) > 0){
#The reservoir could be continued from the previous set of reservoirs
for (j in 1:length(ResSpot[Depth][,1])){
if (any((ResSpotCheck[Depth][,1] == ResSpot[Depth][j,1])) == FALSE){
#This reservoir has terminated. Plot it back to the ResSpot$Start location.
if (is.na(colResRectsField) == FALSE){
#Get the color for the reservoir field specified
color = NA
for (k in 1:length(colSeps)){
if (ResSpot[colResRectsField][j,1] < colSeps[k]){
color = colors[k]
break
}
if (k == length(colSeps)){
color = colors[k+1]
}
}
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=color)
}
else{
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=adjustcolor(colResRects, alpha=transpar))
}
#Remove this reservoir from ResSpot after loop concludes
if (length(RemResInds) > 1){
RemResInds = c(RemResInds, j)
}
else if (is.na(RemResInds)){
RemResInds = j
}
else{
RemResInds = c(RemResInds, j)
}
}
}
}
#Now check to see if there are any new reservoirs that begin and add them to the ResSpot.
for (k in 1:length(ResSpotCheck[Depth][,1])){
if (any(ResSpot[Depth][,1] == ResSpotCheck[Depth][k,1]) == FALSE){
#Add this reservoir to ResSpot with the new start location.
ResSpotCheck$Start[k] = (i-1)
ResSpot = rbind(ResSpot, ResSpotCheck[k,])
}
}
#Remove reservoirs that terminated.
if (length(RemResInds) > 1){
ResSpot = ResSpot[-RemResInds,]
#Set RemResInds back to NA
RemResInds = NA
}
else if (is.na(RemResInds) == FALSE){
ResSpot = ResSpot[-RemResInds,]
#Set RemResInds back to NA
RemResInds = NA
}
}
}
if (CrossSec@data[Res][i,] == 0){
if (length(ResSpot$Start) > 0){
#The reservoir could be continued from the previous set of reservoirs
for (j in 1:length(ResSpot[Depth][,1])){
#This reservoir has terminated. Plot it back to the ResSpot$Start location.
if (is.na(colResRectsField) == FALSE){
#Get the color for the reservoir field specified
color = NA
for (k in 1:length(colSeps)){
if (ResSpot[colResRectsField][j,1] < colSeps[k]){
color = colors[k]
break
}
if (k == length(colSeps)){
color = colors[k+1]
}
}
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=color)
}
else{
par(new=T)
rect(xleft = ResSpot$Start[j], xright = (i-2), ytop = ResSpot[Depth][j,1], ybottom = ResSpot[Depth][j,1]+ResSpot[FormThick][j,1], xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=adjustcolor(colResRects, alpha=transpar))
}
}
}
#Clearly all reservoirs have terminated. Set PlotBack to 0 and clear contents of ResSpot.
PlotBack = -1
ResSpot = NA
}
}
else if (CrossSec@data[Res][i,] != 0){
#There are reservoirs here. Set PltBack to i. Used to check for how far the reservoirs are present
PlotBack = i
#Get all the reservoirs at this spot from the database
ResSpot = ResData@data[which(ResData@data$Length == CrossSec@data$Length[i]),]
if (all(ResSpot[Depth][,1] == 0)){
#No reservoirs exist here. Error in data input file.
PlotBack = -1
ResSpot = NA
}
else if (any(ResSpot[Depth][,1] == 0)){
#Some reservoirs have 0 depth. Remove them.
Inds = which(ResSpot[Depth][,1]==0)
ResSpot = ResSpot[-Inds,]
if (Rects == 0){
#Plot the reservoirs that do not have 0 depth.
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=colResTop, pch=16)
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1]+ResSpot[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Do not plot anything. Record the position of the start of the reservoir for each reservoir.
ResSpot$Start = (i-1)
}
}
else{
#All depths are fine.
if (Rects == 0){
#Plot the reservoirs
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=colResTop, pch=16)
par(new=T)
plot(rep(CrossSec@data$Length[i], length(ResSpot[Depth][,1])), ResSpot[Depth][,1]+ResSpot[FormThick][,1], axes=FALSE, xlim=c(xLimMin, xLimMax), ylim = c(yResBot,yResTop), xlab='', ylab='', col=colResBot, pch=16)
}
else if (Rects == 1){
#Do not plot anything. Record the position of the start of the reservoir for each reservoir.
ResSpot$Start = (i-1)
}