-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplemental_code.R
1576 lines (1433 loc) · 61.8 KB
/
Supplemental_code.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
##############################################################################
#
# A probabilistic model for soil populations of Fusarium culmorum in agricultural soil across the Inland Pacific Northwest based on local climate and future predictions under climate change.
#
# Section 1: Loading in dataframes
# Section 2: Multilevel model with varying intercepts by sampling iteration (N=72)
# Section 3: Generating Figure 2
# Section 4: Generating Figure 3
# Section 5: Generating Figure 4
# Section 6: Multilevel model with offsets for field (N=9) and sampling iteration (N=72)
# Section 7: Generating Figure 5
# Section 8: Generating Figure 6
# Section 9: Multilevel model with varying effects by field (N=9)
# Section 10: Generating Figure 7
# Section 11: Generating Figure 8
# Section 12: Downloading downscaled GCM datasets
# Section 13: Calculating potential evapotranspiration
# Section 14: Generating Figures 8 and 9
# Section 15: Generating Supplemental Figures 1-9
#
# Author: Andrew Lloyd Robinson
#
##############################################################################
#dependent packages
library(rethinking)
library(zoo)
library(lubridate)
library(RNetCDF)
#Section 1: Loading in dataframes----
#climate frame of historical data for each field
af = read.csv(file = "hist_precip_evap_diff.csv", stringsAsFactors = FALSE)
#sampling date records
sampling.dates = as.Date(c("2016-06-11","2016-06-18","2016-06-25",
"2016-09-03","2016-09-10","2016-09-17",
"2016-12-03","2016-12-10","2016-12-15",
"2017-03-04","2017-03-11","2017-03-18",
"2017-06-03","2017-06-10","2017-06-17",
"2017-09-02","2017-09-09","2017-09-16",
"2017-12-02","2017-12-09","2017-12-16",
"2018-03-03","2018-03-10","2018-03-17"))
#converting into zoo object
df.zoo = read.zoo(af, format = "%Y-%m-%d")
#calculating rolling sums for seasonal term (prior 90 days)
sum.90 <- rollapply(df.zoo, 90, sum, align = c("right"))
df.final <- sum.90
df.final <- df.final[complete.cases(df.final),]
final.index = index(df.final)
#starting what will become the date sequence vector
ds <- 0
#looking up index values and adding to planting date vector
for (d in sampling.dates){
pointer = which.min(abs(as.Date(d) - final.index))
ds <- append(ds,pointer,after=length(ds))
}
#removing the 0 used at the beginning
ds <- ds[-1]
#converting into dataframe
bf <- data.frame(df.final)
#total precip 90 d prior for every field & sampling date
p.90 <- c(bf$p.1[ds[1]],bf$p.2[ds[1]],bf$p.3[ds[1]],bf$p.4[ds[2]],bf$p.5[ds[2]],bf$p.6[ds[2]],bf$p.7[ds[3]],bf$p.8[ds[3]],bf$p.9[ds[3]],
bf$p.1[ds[4]],bf$p.2[ds[4]],bf$p.3[ds[4]],bf$p.4[ds[5]],bf$p.5[ds[5]],bf$p.6[ds[5]],bf$p.7[ds[6]],bf$p.8[ds[6]],bf$p.9[ds[6]],
bf$p.1[ds[7]],bf$p.2[ds[7]],bf$p.3[ds[7]],bf$p.4[ds[8]],bf$p.5[ds[8]],bf$p.6[ds[8]],bf$p.7[ds[9]],bf$p.8[ds[9]],bf$p.9[ds[9]],
bf$p.1[ds[10]],bf$p.2[ds[10]],bf$p.3[ds[10]],bf$p.4[ds[11]],bf$p.5[ds[11]],bf$p.6[ds[11]],bf$p.7[ds[12]],bf$p.8[ds[12]],bf$p.9[ds[12]],
bf$p.1[ds[13]],bf$p.2[ds[13]],bf$p.3[ds[13]],bf$p.4[ds[14]],bf$p.5[ds[14]],bf$p.6[ds[14]],bf$p.7[ds[15]],bf$p.8[ds[15]],bf$p.9[ds[15]],
bf$p.1[ds[16]],bf$p.2[ds[16]],bf$p.3[ds[16]],bf$p.4[ds[17]],bf$p.5[ds[17]],bf$p.6[ds[17]],bf$p.7[ds[18]],bf$p.8[ds[18]],bf$p.9[ds[18]],
bf$p.1[ds[19]],bf$p.2[ds[19]],bf$p.3[ds[19]],bf$p.4[ds[20]],bf$p.5[ds[20]],bf$p.6[ds[20]],bf$p.7[ds[21]],bf$p.8[ds[21]],bf$p.9[ds[21]],
bf$p.1[ds[22]],bf$p.2[ds[22]],bf$p.3[ds[22]],bf$p.4[ds[23]],bf$p.5[ds[23]],bf$p.6[ds[23]],bf$p.7[ds[24]],bf$p.8[ds[24]],bf$p.9[ds[24]])
#total evap 90 d prior for every field & sampling date
e.90 <- c(bf$evap.1[ds[1]],bf$evap.2[ds[1]],bf$evap.3[ds[1]],bf$evap.4[ds[2]],bf$evap.5[ds[2]],bf$evap.6[ds[2]],bf$evap.7[ds[3]],bf$evap.8[ds[3]],bf$evap.9[ds[3]],
bf$evap.1[ds[4]],bf$evap.2[ds[4]],bf$evap.3[ds[4]],bf$evap.4[ds[5]],bf$evap.5[ds[5]],bf$evap.6[ds[5]],bf$evap.7[ds[6]],bf$evap.8[ds[6]],bf$evap.9[ds[6]],
bf$evap.1[ds[7]],bf$evap.2[ds[7]],bf$evap.3[ds[7]],bf$evap.4[ds[8]],bf$evap.5[ds[8]],bf$evap.6[ds[8]],bf$evap.7[ds[9]],bf$evap.8[ds[9]],bf$evap.9[ds[9]],
bf$evap.1[ds[10]],bf$evap.2[ds[10]],bf$evap.3[ds[10]],bf$evap.4[ds[11]],bf$evap.5[ds[11]],bf$evap.6[ds[11]],bf$evap.7[ds[12]],bf$evap.8[ds[12]],bf$evap.9[ds[12]],
bf$evap.1[ds[13]],bf$evap.2[ds[13]],bf$evap.3[ds[13]],bf$evap.4[ds[14]],bf$evap.5[ds[14]],bf$evap.6[ds[14]],bf$evap.7[ds[15]],bf$evap.8[ds[15]],bf$evap.9[ds[15]],
bf$evap.1[ds[16]],bf$evap.2[ds[16]],bf$evap.3[ds[16]],bf$evap.4[ds[17]],bf$evap.5[ds[17]],bf$evap.6[ds[17]],bf$evap.7[ds[18]],bf$evap.8[ds[18]],bf$evap.9[ds[18]],
bf$evap.1[ds[19]],bf$evap.2[ds[19]],bf$evap.3[ds[19]],bf$evap.4[ds[20]],bf$evap.5[ds[20]],bf$evap.6[ds[20]],bf$evap.7[ds[21]],bf$evap.8[ds[21]],bf$evap.9[ds[21]],
bf$evap.1[ds[22]],bf$evap.2[ds[22]],bf$evap.3[ds[22]],bf$evap.4[ds[23]],bf$evap.5[ds[23]],bf$evap.6[ds[23]],bf$evap.7[ds[24]],bf$evap.8[ds[24]],bf$evap.9[ds[24]])
#adding historic observations to dataframe
df$p.h.90 <- rep(p.90,each=9)
df$e.h.90 <- rep(e.90,each=9)
#calculating difference, also called "atmospheric water balance"
df$p.d.90 <- df$p.h.90-df$e.h.90
#historic grand mean and sd
historic_diff_mu <- mean(as.matrix(bf[,19:27]))
historic_diff_sd <- sd(as.matrix(bf[,19:27]))
#standardizing atmospheric water balance using grand mean and standard deviation
df$prior_diff <- (df$p.d.90-historic_diff_mu)/historic_diff_sd
#loading in soil survey data frame
df = read.csv(file = "soilpop.csv", stringsAsFactors = FALSE)
#adding a reference column
df$number <- as.integer(rep( seq( 1 , 72 , 1 ), each=9 ))
#replacing 1 quadrat in field 9 with the LOD (50 F. culmorum ppg) to include field 9 in the analysis
#field 9 December 2016 had no detectable F. culmorum in all 9 quadrats sampled
df$f.c.ppg[235] = 50
#field 9 December 2017 had no detectable F. culmorum in all 9 quadrats sampled
df$f.c.ppg[559] = 50
#Section 2: Multilevel model with varying intercepts by sampling iteration (N=72)----
#list for Stan
test.list <- list(ppg = df$f.c.ppg , field = df$field , sample = df$number)
#code for model, used as a reference
m.reference <- map2stan(
alist(
ppg ~ dexp(lambda),
log(lambda) <- a_sample[sample],
a_sample[sample] ~ dnorm(a,sigma_sample),
a ~ dnorm(0,1),
sigma_sample ~ dexp(1)
) ,
data=test.list,
warmup=1000,
iter=3500,
chains=4,
cores=4,
control = list(adapt_delta = 0.99)
)
#viewing chains
plot(m.reference)
#parameter summary
precis(m.reference , depth = 2)
#visualization of parameter summary
plot(precis(m.reference , depth = 2))
#extracting samples
e.1 <- extract.samples(m.reference,n=10000)
#Section 3: Generating Figure 2----
#name sequence for fields
ns <- c("Field 1","Field 2","Field 3","Field 4","Field 5","Field 6","Field 7","Field 8","Field 9",
rep(" ",63))
#season sequence labels
seasons <- as.vector(c("Jun 2016",rep(" ",8),
"Sep 2016",rep(" ",8),
"Dec 2016",rep(" ",8),
"Mar 2017",rep(" ",8),
"Jun 2017",rep(" ",8),
"Sep 2017",rep(" ",8),
"Dec 2017",rep(" ",8),
"Mar 2018",rep(" ",8)))
#logical sequence for x axis labels
xs <- as.vector(c(rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE))
#logical sequence for y axis labels
ys <- as.vector(c(rep(TRUE,9),rep(FALSE,63)))
#index sequence for raw data
fs <- seq(1,648,9)
#plotting
par(oma=c(5,5,1,1))
par(mar=c(0.5,0.5,0.5,0.5))
par(mfcol=c(9,8))
for(n in 1:72){
plot(0,type="n",xlim=c(0,1),ylim=c(0,7500),xlab=" ",ylab=" ",axes=FALSE)
box()
axis(1,at=seq(0,1,0.25),las=2,labels=xs[n])
axis(2,at=seq(0,7500,2500),las=1,labels=ys[n])
#plotting 100 exceedance curves from posterior distribution
for(i in 1:100){
lines(rev(seq(0.01,0.99,0.01)),-log(1-seq(0.01,0.99,0.01))/exp(e.1$a_sample[i,n]),col=col.alpha("red",0.05))
}
x= df$f.c.ppg[fs[n]:(fs[n]+8)]
#plotting empirical dataset as points
points((1:length(x)-0.5)/length(x),sort(x,decreasing=TRUE),type="p",pch=16,col="black")
mtext(ns[n],line=-1.5,side=3)
mtext(seasons[n],line=0,side=3)
}
mtext("Fraction of field exceeded", side = 1, outer = TRUE, line = 3)
mtext("PPG", side = 2, outer = TRUE, line = 3)
#Section 4: Generating Figure 3----
#function to simulate a N by N quadrat field with mean M and standard deviation S
field_simulator <- function(N,M,S){
#creating a distribution of rate parameters
L <- rnorm(N^2,mean=M,sd=S)
#Generating 1 random draw from an exponential distribution for each rate parameter
P <- rexp(N^2,rate=exp(L))
#converting from vector into matrix
field <- matrix(data=P,nrow=N,ncol=N)
#rounding to whole number
field <- round(field)
#plotting
par(mar=c(0,0,0,0))
plot(0,type="n",xlim=c(0,N),ylim=c(0,N),axes=FALSE)
for( i in 1:N){
for( j in 1:N){
rect(i-0.5,j-0.5,i+0.5,j+0.5,col=col.alpha("red",alpha=(field[i,j]/max(field))/2))
text(x=i,y=j,labels=field[i,j],cex=0.5)
}
}
}
#Plotting a field using the posterior distribution of the population average parameter (alpha)
field_simulator(30,-4.8,0.22)
#Section 5: Generating Figure 4----
#turning samples into dataframe
ref <- as.data.frame(e.1$a_sample)
#calculating percent return for seasonal change in ln(lambda)
f1.1 <- (ref[,10]/ref[,1])-1
f2.1 <- (ref[,11]/ref[,2])-1
f3.1 <- (ref[,12]/ref[,3])-1
f4.1 <- (ref[,13]/ref[,4])-1
f5.1 <- (ref[,14]/ref[,5])-1
f6.1 <- (ref[,15]/ref[,6])-1
f7.1 <- (ref[,16]/ref[,7])-1
f8.1 <- (ref[,17]/ref[,8])-1
f9.1 <- (ref[,18]/ref[,9])-1
f1.2 <- (ref[,19]/ref[,10])-1
f2.2 <- (ref[,20]/ref[,11])-1
f3.2 <- (ref[,21]/ref[,12])-1
f4.2 <- (ref[,22]/ref[,13])-1
f5.2 <- (ref[,23]/ref[,14])-1
f6.2 <- (ref[,24]/ref[,15])-1
f7.2 <- (ref[,25]/ref[,16])-1
f8.2 <- (ref[,26]/ref[,17])-1
f9.2 <- (ref[,27]/ref[,18])-1
f1.3 <- (ref[,28]/ref[,19])-1
f2.3 <- (ref[,29]/ref[,20])-1
f3.3 <- (ref[,30]/ref[,21])-1
f4.3 <- (ref[,31]/ref[,22])-1
f5.3 <- (ref[,32]/ref[,23])-1
f6.3 <- (ref[,33]/ref[,24])-1
f7.3 <- (ref[,34]/ref[,25])-1
f8.3 <- (ref[,35]/ref[,26])-1
f9.3 <- (ref[,36]/ref[,27])-1
f1.4 <- (ref[,37]/ref[,28])-1
f2.4 <- (ref[,38]/ref[,29])-1
f3.4 <- (ref[,39]/ref[,30])-1
f4.4 <- (ref[,40]/ref[,31])-1
f5.4 <- (ref[,41]/ref[,32])-1
f6.4 <- (ref[,42]/ref[,33])-1
f7.4 <- (ref[,43]/ref[,34])-1
f8.4 <- (ref[,44]/ref[,35])-1
f9.4 <- (ref[,45]/ref[,36])-1
f1.5 <- (ref[,46]/ref[,37])-1
f2.5 <- (ref[,47]/ref[,38])-1
f3.5 <- (ref[,48]/ref[,39])-1
f4.5 <- (ref[,49]/ref[,40])-1
f5.5 <- (ref[,50]/ref[,41])-1
f6.5 <- (ref[,51]/ref[,42])-1
f7.5 <- (ref[,52]/ref[,43])-1
f8.5 <- (ref[,53]/ref[,44])-1
f9.5 <- (ref[,54]/ref[,45])-1
f1.6 <- (ref[,55]/ref[,46])-1
f2.6 <- (ref[,56]/ref[,47])-1
f3.6 <- (ref[,57]/ref[,48])-1
f4.6 <- (ref[,58]/ref[,49])-1
f5.6 <- (ref[,59]/ref[,50])-1
f6.6 <- (ref[,60]/ref[,51])-1
f7.6 <- (ref[,61]/ref[,52])-1
f8.6 <- (ref[,62]/ref[,53])-1
f9.6 <- (ref[,63]/ref[,54])-1
f1.7 <- (ref[,64]/ref[,55])-1
f2.7 <- (ref[,65]/ref[,56])-1
f3.7 <- (ref[,66]/ref[,57])-1
f4.7 <- (ref[,67]/ref[,58])-1
f5.7 <- (ref[,68]/ref[,59])-1
f6.7 <- (ref[,69]/ref[,60])-1
f7.7 <- (ref[,70]/ref[,61])-1
f8.7 <- (ref[,71]/ref[,62])-1
f9.7 <- (ref[,72]/ref[,63])-1
#compiling into model frame
mf <- data.frame(f1.1,f2.1,f3.1,f4.1,f5.1,f6.1,f7.1,f8.1,f9.1,
f1.2,f2.2,f3.2,f4.2,f5.2,f6.2,f7.2,f8.2,f9.2,
f1.3,f2.3,f3.3,f4.3,f5.3,f6.3,f7.3,f8.3,f9.3,
f1.4,f2.4,f3.4,f4.4,f5.4,f6.4,f7.4,f8.4,f9.4,
f1.5,f2.5,f3.5,f4.5,f5.5,f6.5,f7.5,f8.5,f9.5,
f1.6,f2.6,f3.6,f4.6,f5.6,f6.6,f7.6,f8.6,f9.6,
f1.7,f2.7,f3.7,f4.7,f5.7,f6.7,f7.7,f8.7,f9.7)
#adjusting scale of percentages
mf <- mf*100
#calculating probability of seasonal increase
f1.1 <- sum((ref[,10]-ref[,1])<0)/10000
f2.1 <- sum((ref[,11]-ref[,2])<0)/10000
f3.1 <- sum((ref[,12]-ref[,3])<0)/10000
f4.1 <- sum((ref[,13]-ref[,4])<0)/10000
f5.1 <- sum((ref[,14]-ref[,5])<0)/10000
f6.1 <- sum((ref[,15]-ref[,6])<0)/10000
f7.1 <- sum((ref[,16]-ref[,7])<0)/10000
f8.1 <- sum((ref[,17]-ref[,8])<0)/10000
f9.1 <- sum((ref[,18]-ref[,9])<0)/10000
f1.2 <- sum((ref[,19]-ref[,10])<0)/10000
f2.2 <- sum((ref[,20]-ref[,11])<0)/10000
f3.2 <- sum((ref[,21]-ref[,12])<0)/10000
f4.2 <- sum((ref[,22]-ref[,13])<0)/10000
f5.2 <- sum((ref[,23]-ref[,14])<0)/10000
f6.2 <- sum((ref[,24]-ref[,15])<0)/10000
f7.2 <- sum((ref[,25]-ref[,16])<0)/10000
f8.2 <- sum((ref[,26]-ref[,17])<0)/10000
f9.2 <- sum((ref[,27]-ref[,18])<0)/10000
f1.3 <- sum((ref[,28]-ref[,19])<0)/10000
f2.3 <- sum((ref[,29]-ref[,20])<0)/10000
f3.3 <- sum((ref[,30]-ref[,21])<0)/10000
f4.3 <- sum((ref[,31]-ref[,22])<0)/10000
f5.3 <- sum((ref[,32]-ref[,23])<0)/10000
f6.3 <- sum((ref[,33]-ref[,24])<0)/10000
f7.3 <- sum((ref[,34]-ref[,25])<0)/10000
f8.3 <- sum((ref[,35]-ref[,26])<0)/10000
f9.3 <- sum((ref[,36]-ref[,27])<0)/10000
f1.4 <- sum((ref[,37]-ref[,28])<0)/10000
f2.4 <- sum((ref[,38]-ref[,29])<0)/10000
f3.4 <- sum((ref[,39]-ref[,30])<0)/10000
f4.4 <- sum((ref[,40]-ref[,31])<0)/10000
f5.4 <- sum((ref[,41]-ref[,32])<0)/10000
f6.4 <- sum((ref[,42]-ref[,33])<0)/10000
f7.4 <- sum((ref[,43]-ref[,34])<0)/10000
f8.4 <- sum((ref[,44]-ref[,35])<0)/10000
f9.4 <- sum((ref[,45]-ref[,36])<0)/10000
f1.5 <- sum((ref[,46]-ref[,37])<0)/10000
f2.5 <- sum((ref[,47]-ref[,38])<0)/10000
f3.5 <- sum((ref[,48]-ref[,39])<0)/10000
f4.5 <- sum((ref[,49]-ref[,40])<0)/10000
f5.5 <- sum((ref[,50]-ref[,41])<0)/10000
f6.5 <- sum((ref[,51]-ref[,42])<0)/10000
f7.5 <- sum((ref[,52]-ref[,43])<0)/10000
f8.5 <- sum((ref[,53]-ref[,44])<0)/10000
f9.5 <- sum((ref[,54]-ref[,45])<0)/10000
f1.6 <- sum((ref[,55]-ref[,46])<0)/10000
f2.6 <- sum((ref[,56]-ref[,47])<0)/10000
f3.6 <- sum((ref[,57]-ref[,48])<0)/10000
f4.6 <- sum((ref[,58]-ref[,49])<0)/10000
f5.6 <- sum((ref[,59]-ref[,50])<0)/10000
f6.6 <- sum((ref[,60]-ref[,51])<0)/10000
f7.6 <- sum((ref[,61]-ref[,52])<0)/10000
f8.6 <- sum((ref[,62]-ref[,53])<0)/10000
f9.6 <- sum((ref[,63]-ref[,54])<0)/10000
f1.7 <- sum((ref[,64]-ref[,55])<0)/10000
f2.7 <- sum((ref[,65]-ref[,56])<0)/10000
f3.7 <- sum((ref[,66]-ref[,57])<0)/10000
f4.7 <- sum((ref[,67]-ref[,58])<0)/10000
f5.7 <- sum((ref[,68]-ref[,59])<0)/10000
f6.7 <- sum((ref[,69]-ref[,60])<0)/10000
f7.7 <- sum((ref[,70]-ref[,61])<0)/10000
f8.7 <- sum((ref[,71]-ref[,62])<0)/10000
f9.7 <- sum((ref[,72]-ref[,63])<0)/10000
#vector containing probability of seasonal increase
pr.si <- as.vector(c(f1.1,f2.1,f3.1,f4.1,f5.1,f6.1,f7.1,f8.1,f9.1,
f1.2,f2.2,f3.2,f4.2,f5.2,f6.2,f7.2,f8.2,f9.2,
f1.3,f2.3,f3.3,f4.3,f5.3,f6.3,f7.3,f8.3,f9.3,
f1.4,f2.4,f3.4,f4.4,f5.4,f6.4,f7.4,f8.4,f9.4,
f1.5,f2.5,f3.5,f4.5,f5.5,f6.5,f7.5,f8.5,f9.5,
f1.6,f2.6,f3.6,f4.6,f5.6,f6.6,f7.6,f8.6,f9.6,
f1.7,f2.7,f3.7,f4.7,f5.7,f6.7,f7.7,f8.7,f9.7))
#function to collect quantiles
QUANT <- function(x) quantile(x,probs = c(0.1,0.25,0.5,0.75,0.9))
#calculating seasonal return quantiles
mf.q <- apply(mf,2,QUANT)
#sequences for season labels
seasons <- c("Sep 2016","Dec 2016","Mar 2017","Jun 2017","Sep 2017","Dec 2017","Mar 2018")
blank <- rep(" ",7)
#function to plot seasonal return with probability of seasonal increase for a given field (n) with axis scale (min,max) and label (lab)
SEASONAL_RETURN_PLOT <- function(n,max,min,lab){
plot(0,type="n",xlim=c(1,7),ylim=c(min,max),xlab=" ",ylab=" ",
main=NULL,axes=FALSE)
box()
axis(1,at=seq(1,7,1),las=1,labels=lab)
axis(2,at=seq(min,max,25),las=2)
#shading in inter-decile range
shade(mf.q[c(1,5),seq(n,63,9)],lim=seq(1,7,1),col=col.alpha("black",0.15))
#shading in inter-quantile range
shade(mf.q[c(2,4),seq(n,63,9)],lim=seq(1,7,1),col=col.alpha("black",0.15))
#plotting line for median
lines(y=mf.q[3,seq(n,63,9)],x=seq(1:7),col="red",lwd=2)
#adding probability of seasonal increase
text(labels=paste("P = ",round(pr.si[seq(n,63,9)],digits=2)),x=seq(1,7,1),y=rep(min,7),pos=3)
mtext(paste(" Field",n),side=3,line=-2,adj=0,cex=1.5)
abline(h=0)
}
#producing final plot
par(mfrow=c(9,1))
par(oma=c(5,5,1,1))
par(mar=c(0.5,0.5,0.5,0.5))
SEASONAL_RETURN_PLOT(1,50,-50,blank)
SEASONAL_RETURN_PLOT(2,50,-50,blank)
SEASONAL_RETURN_PLOT(3,50,-50,blank)
SEASONAL_RETURN_PLOT(4,50,-50,blank)
SEASONAL_RETURN_PLOT(5,50,-50,blank)
SEASONAL_RETURN_PLOT(6,125,-75,blank)
SEASONAL_RETURN_PLOT(7,125,-75,blank)
SEASONAL_RETURN_PLOT(8,100,-100,blank)
SEASONAL_RETURN_PLOT(9,100,-100,seasons)
mtext("Season", side = 1, outer = TRUE, line = 3)
mtext("Changes in estimated logarithmic rate parameters, expressed as seasonal return (%)", side = 2, outer = TRUE, line = 3)
#Section 6: Multilevel model with offsets for field (N=9) and sampling iteration (N=72)----
#list for Stan
test.list <- list(ppg = df$f.c.ppg , field = df$field , sample = df$number)
#model code
m.field.sample <- map2stan(
alist(
ppg ~ dexp(lambda),
log(lambda) <- a + a_field[field] + a_sample[sample],
a_field[field] ~ dnorm(0,sigma_field),
a_sample[sample] ~ dnorm(0,sigma_sample),
#informative prior for "a" using the posterior from m.reference
a ~ dnorm(-4.8,0.22),
sigma_field ~ dexp(1),
sigma_sample ~ dexp(1)
) ,
data = test.list ,
warmup = 1000 ,
iter = 3500 ,
chains = 4 ,
cores = 4 ,
control = list(adapt_delta = 0.99)
)
#parameter summary
precis(m.field.sample,depth=2)
#visualization
plot(precis(m.field.sample,depth=2))
#extracting samples
e.2 <- extract.samples(m.field.sample,n=10000)
#Section 7: Generating Figure 5----
#distribution of expected values for the population average
meta_ev <- 1/exp(e.2$a[1:10000])
#calculating distributions of expected values for each field (N=9)
for(n in 1:9){
assign(paste0("field_",n,"_ev"),1/exp(e.2$a[1:10000] + e.2$a_field[,n]))
}
#adding a year column to the historical climate data dataframe
af$Y <- lubridate::year(af$Date)
#annual precipitation by year
p.1 <- sapply(split(af$p.1,af$Y),sum)
p.2 <- sapply(split(af$p.2,af$Y),sum)
p.3 <- sapply(split(af$p.3,af$Y),sum)
p.4 <- sapply(split(af$p.4,af$Y),sum)
p.5 <- sapply(split(af$p.5,af$Y),sum)
p.6 <- sapply(split(af$p.6,af$Y),sum)
p.7 <- sapply(split(af$p.7,af$Y),sum)
p.8 <- sapply(split(af$p.8,af$Y),sum)
p.9 <- sapply(split(af$p.9,af$Y),sum)
#annual potential exapotranspiration by year
evap.1 <- sapply(split(af$evap.1,af$Y),sum)
evap.2 <- sapply(split(af$evap.2,af$Y),sum)
evap.3 <- sapply(split(af$evap.3,af$Y),sum)
evap.4 <- sapply(split(af$evap.4,af$Y),sum)
evap.5 <- sapply(split(af$evap.5,af$Y),sum)
evap.6 <- sapply(split(af$evap.6,af$Y),sum)
evap.7 <- sapply(split(af$evap.7,af$Y),sum)
evap.8 <- sapply(split(af$evap.8,af$Y),sum)
evap.9 <- sapply(split(af$evap.9,af$Y),sum)
#plotting the results
par(mfrow=c(1,3))
par(cex=1.25)
#Figure 5A
plot(0,type="n",xlim=c(1,10),ylim=c(0,2000),xlab="Field",ylab="PPG",main="A",axes=FALSE)
box()
axis(1,at=seq(1,10,1),las=1,labels=c("A",1,2,3,4,5,6,7,8,9))
axis(2,at=seq(0,2000,100),las=1)
points(x=rep(1,100),y=sample(meta_ev,100),col=(col.alpha("black",0.1)),pch=15,cex=2)
points(x=rep(2,100),y=sample(field_1_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(3,100),y=sample(field_2_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(4,100),y=sample(field_3_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(5,100),y=sample(field_4_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(6,100),y=sample(field_5_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(7,100),y=sample(field_6_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(8,100),y=sample(field_7_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(9,100),y=sample(field_8_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
points(x=rep(10,100),y=sample(field_9_ev,100),col=(col.alpha("green3",0.1)),pch=15,cex=2)
#Figure 5B
plot(0,type="n",xlim=c(1,9),ylim=c(350,900),
xlab="Field",ylab="Annual precipitation (mm)",main="B",axes=FALSE)
box()
axis(1,at=seq(1,9,1),las=1,labels=c(1,2,3,4,5,6,7,8,9))
axis(2,at=seq(350,900,50),las=1)
for (n in 1:39){
lines(x=seq(1,9,1),y=c(p.1[n],p.2[n],p.3[n],p.4[n],p.5[n],p.6[n],p.7[n],p.8[n],p.9[n]),
col=(col.alpha("blue",0.25)),pch=15,cex=2)
}
#Figure 5C
plot(0,type="n",xlim=c(1,9),ylim=c(-1150,-900),
xlab="Field",ylab="Annual potential evapotranspiration (-mm)",main="C",axes=FALSE)
box()
axis(1,at=seq(1,9,1),las=1,labels=c(1,2,3,4,5,6,7,8,9))
axis(2,at=seq(-1150,900,50),las=1)
for (n in 1:39){
lines(x=seq(1,9,1),y=-c(evap.1[n],evap.2[n],evap.3[n],evap.4[n],evap.5[n],evap.6[n],evap.7[n],evap.8[n],evap.9[n]),
col=(col.alpha("red",0.25)),pch=15,cex=2)
}
#Section 8: Generating Figure 6----
#old function to standardize values
standardizer <- function(x) (x-mean(x))/(sd(x))
#soil moisture reference frame
gf = read.csv(file = "soilmoist.csv", stringsAsFactors = FALSE)
gf$sm.1.s <- standardizer(gf$sm.1)
gf$sm.2.s <- standardizer(gf$sm.2)
gf$sm.3.s <- standardizer(gf$sm.3)
gf$sm.4.s <- standardizer(gf$sm.4)
gf$sm.5.s <- standardizer(gf$sm.5)
gf$sm.6.s <- standardizer(gf$sm.6)
gf$sm.7.s <- standardizer(gf$sm.7)
gf$sm.8.s <- standardizer(gf$sm.8)
gf$sm.9.s <- standardizer(gf$sm.9)
#calculateing rolling sums
sum.10 <- rollapply(df.zoo, 10, sum, align = c("right"))
sum.20 <- rollapply(df.zoo, 20, sum, align = c("right"))
sum.30 <- rollapply(df.zoo, 30, sum, align = c("right"))
sum.40 <- rollapply(df.zoo, 40, sum, align = c("right"))
sum.50 <- rollapply(df.zoo, 50, sum, align = c("right"))
sum.60 <- rollapply(df.zoo, 60, sum, align = c("right"))
sum.70 <- rollapply(df.zoo, 70, sum, align = c("right"))
sum.80 <- rollapply(df.zoo, 80, sum, align = c("right"))
sum.90 <- rollapply(df.zoo, 90, sum, align = c("right"))
#merging together
df.final <- merge.zoo(sum.10,sum.20,sum.30,sum.40,sum.50,sum.60,sum.70,sum.80,sum.90)
df.final <- df.final[complete.cases(df.final),]
final.index = index(df.final)
test.frame <- data.frame(df.final)
#only keeping past 2010-01-01
test.frame <- test.frame[11235:14246,]
#standardizing 90 day AWB
test.frame$diff.1.sum.90.s <- standardizer(test.frame$diff.1.sum.90)
test.frame$diff.2.sum.90.s <- standardizer(test.frame$diff.2.sum.90)
test.frame$diff.3.sum.90.s <- standardizer(test.frame$diff.3.sum.90)
test.frame$diff.4.sum.90.s <- standardizer(test.frame$diff.4.sum.90)
test.frame$diff.5.sum.90.s <- standardizer(test.frame$diff.5.sum.90)
test.frame$diff.6.sum.90.s <- standardizer(test.frame$diff.6.sum.90)
test.frame$diff.7.sum.90.s <- standardizer(test.frame$diff.7.sum.90)
test.frame$diff.8.sum.90.s <- standardizer(test.frame$diff.8.sum.90)
test.frame$diff.9.sum.90.s <- standardizer(test.frame$diff.9.sum.90)
#year seqence label
ys <- c(2010,2011,2012,2013,2014,2015,2016,2017,2018)
#generating figure
par(oma=c(5,5,1,1))
par(mar=c(0.5,0.5,0.5,0.5))
par(mfrow=c(9,1))
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.1.s,col="black",lwd=1)
lines(test.frame$diff.1.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 1",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.2.s,col="black",lwd=1)
lines(test.frame$diff.2.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 2",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviatinn",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.3.s,col="black",lwd=1)
lines(test.frame$diff.3.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 3",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.4.s,col="black",lwd=1)
lines(test.frame$diff.4.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 4",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.5.s,col="black",lwd=1)
lines(test.frame$diff.5.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 5",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.6.s,col="black",lwd=1)
lines(test.frame$diff.6.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 6",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.7.s,col="black",lwd=1)
lines(test.frame$diff.7.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 7",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=rep(" ",9))
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.8.s,col="black",lwd=1)
lines(test.frame$diff.8.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 8",cex=1.5)
abline(h=0)
plot(0,type="n",xlim=c(0,3012),ylim=c(-3,3),xlab="Year",ylab="Standard deviation",main=NULL,axes=FALSE)
box()
axis(1,at=seq(0,3012,365),las=1,labels=ys)
axis(2,at=seq(-3,3,1),las=2)
lines(gf$sm.9.s,col="black",lwd=1)
lines(test.frame$diff.9.sum.90.s,col="red",lwd=2)
text(x=0,y=2.5,labels = "Field 9",cex=1.5)
abline(h=0)
mtext("Year", side = 1, outer = TRUE, line = 3)
mtext("Standard deviation", side = 2, outer = TRUE, line = 3)
#Section 9: Multilevel model with varying effects by field (N=9)----
#list for model
test.list <- list(
ppg = df$f.c.ppg,
field = as.numeric(df$field),
prior_diff = df$prior_diff)
#model equation
m.1 <- map2stan(
alist(
ppg ~ dexp(lambda),
log(lambda) <- A + B*(prior_diff) ,
A <- a + a_field[field] ,
B <- b + b_field[field] ,
c(a_field,b_field)[field] ~ dmvnormNC( sigma_field , Rho ),
a ~ dnorm(-4.80,0.22) ,
b ~ dnorm(0,1) ,
sigma_field ~ dcauchy(0, 1) ,
Rho ~ dlkjcorr(7)
) ,
data = test.list ,
warmup = 1000 ,
iter = 3500 ,
chains = 4 ,
cores = 4 ,
control = list(adapt_delta = 0.99, max_treedepth = 15)
)
#parameter summary
precis(m.1,prob=0.95,depth=2)
#extracting samples
e.3 <- extract.samples(m.1,n=10000)
#Section 10:Generating Figure 7----
#collecting model output
link.cm <- link(m.1,n=10000)
#only keeping one set of rates per sampling iteration (link makes a column for every observation)
sim.rates <- link.cm$lambda[,seq(1,648,9)]
#name sequence for fields
ns <- c("Field 1","Field 2","Field 3","Field 4","Field 5","Field 6","Field 7","Field 8","Field 9",
rep(" ",63))
#season sequence
seasons <- as.vector(c("Jun 2016",rep(" ",8),
"Sep 2016",rep(" ",8),
"Dec 2016",rep(" ",8),
"Mar 2017",rep(" ",8),
"Jun 2017",rep(" ",8),
"Sep 2017",rep(" ",8),
"Dec 2017",rep(" ",8),
"Mar 2018",rep(" ",8)))
#logical sequence for x axis labels
xs <- as.vector(c(rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE,
rep(FALSE,8),TRUE))
#logical sequence for y axis labels
ys <- as.vector(c(rep(TRUE,9),rep(FALSE,63)))
#index sequence for raw data
fs <- seq(1,648,9)
#plotting
par(oma=c(5,5,1,1))
par(mar=c(0.5,0.5,0.5,0.5))
par(mfcol=c(9,8))
for(n in 1:72){
plot(0,type="n",xlim=c(0,1),ylim=c(0,7500),xlab=" ",ylab=" ",axes=FALSE)
box()
axis(1,at=seq(0,1,0.25),las=2,labels=xs[n])
axis(2,at=seq(0,7500,2500),las=1,labels=ys[n])
for(i in 1:100){
lines(rev(seq(0.01,0.99,0.01)),-log(1-seq(0.01,0.99,0.01))/sim.rates[i,n],col=col.alpha("green3",0.05))
}
x= df$f.c.ppg[fs[n]:(fs[n]+8)]
points((1:length(x)-0.5)/length(x),sort(x,decreasing=TRUE),type="p",pch=16,col="black")
mtext(ns[n],line=-1.5,side=3)
mtext(seasons[n],line=0,side=3)
}
mtext("Fraction of field exceeded", side = 1, outer = TRUE, line = 3)
mtext("PPG", side = 2, outer = TRUE, line = 3)
#Section 12: Generating Figure 8----
#function to plot predicted trends by field (n)
TREND_SIMULATOR <- function(n){
#finding relevant pieces of model
intercept <- sample(e.3$a+e.3$a_field[,n],100)
slope <- sample(e.3$b+e.3$b_field[,n],100)
#sample sequence
range <- seq(-2,2,0.5)
reference <- historic_diff_mu+range*historic_diff_sd
ev_matrix <- matrix(0,9,100)
for (i in 1:9){
for (j in 1:100){
ev_matrix[i,j] <- 1/exp(intercept[j] + slope[j]*range[i])
}
}
plot(0,type="n",xlim=c(-2,2),ylim=c(0,2000),xlab="Prior 90 day P - PET (mm)",ylab="PPG",
main=NULL,axes=FALSE)
box()
axis(1,at=seq(-2,2,0.5),las=1,labels=round(reference,digits=0))
axis(3,at=seq(-2,2,0.5),las=1)
axis(2,at=seq(0,2000,500),las=2)
for(j in 1:100){
lines(x=range,y=ev_matrix[,j],col=col.alpha("green3",0.2))
}
mtext(paste(" Field",n),side=3,line=-2,adj=0,cex=1.5)
}
#generating plot
par(mfrow=c(3,3))
for (n in 1:9){
TREND_SIMULATOR(n)
}
#Section 13: Downloading downscaled GCM datasets----
#dataframe containing GPS coordinates of all sampled fields
locations <- read.csv("gps_ref.csv")
#column for naming files
locations$town <- c("FIELD_1","FIELD_2","FIELD_3","FIELD_4","FIELD_5","FIELD_6","FIELD_7","FIELD_8","FIELD_9")
#number of sites in location file
N <- nrow(locations)
#adding elevation data
elevation_grid <- open.nc("metdata_elevationdata.nc")
elevation_ref <- var.get.nc(elevation_grid,variable=2)
#adding elevation column
locations$elevation <- rep(NA,N)
#adding elevation values
for (n in 1:N){
x <- locations$lon[n]
y <- locations$lat[n]
lat <- var.get.nc(elevation_grid,"lat")
lon <- var.get.nc(elevation_grid,"lon")
flat = match(abs(lat - y) < 1/48, 1)
latindex = which(flat %in% 1)
flon = match(abs(lon - x) < 1/48, 1)
lonindex = which(flon %in% 1)
locations$elevation[n] <- 0.1*elevation_ref[lonindex,latindex]
}
#base URL
url_1 <- "http://thredds.northwestknowledge.net:8080/thredds/dodsC/NWCSC_INTEGRATED_SCENARIOS_ALL_CLIMATE/macav2livneh/"
#model list
model <- c("bcc-csm1-1", "bcc-csm1-1-m","BNU-ESM","CanESM2",
"CSIRO-Mk3-6-0","GFDL-ESM2G","GFDL-ESM2M","HadGEM2-CC365",
"HadGEM2-ES365","inmcm4","IPSL-CM5A-LR","IPSL-CM5A-MR",
"IPSL-CM5B-LR","MIROC5","MIROC-ESM","MIROC-ESM-CHEM",
"MRI-CGCM3","NorESM1-M","CNRM-CM5","CCSM4")
#model condition list becuase CCSM4 is different
condition <- c(rep("_r1i1p1_",19),"_r6i1p1_")
#timestep lists
timestep_historical <- c("historical_1950_1969_CONUS_daily.nc",
"historical_1970_1989_CONUS_daily.nc",
"historical_1990_2005_CONUS_daily.nc")
timestep_45 <- c("rcp45_2006_2025_CONUS_daily.nc",
"rcp45_2026_2045_CONUS_daily.nc",
"rcp45_2046_2065_CONUS_daily.nc",
"rcp45_2066_2085_CONUS_daily.nc",
"rcp45_2086_2099_CONUS_daily.nc")
timestep_85 <- c("rcp85_2006_2025_CONUS_daily.nc",
"rcp85_2026_2045_CONUS_daily.nc",
"rcp85_2046_2065_CONUS_daily.nc",
"rcp85_2066_2085_CONUS_daily.nc",
"rcp85_2086_2099_CONUS_daily.nc")
#variable list
variable_1 <- c("_tasmax_","_tasmin_","_pr_","_huss_","_was_","_rsds_")
#reference longitude and latitude sequence
lon <- seq(235.40625,292.96875,0.0625)
lat <- seq(25.15625,52.84375,0.0625)
#building 3-d arrays [variable, model, time] for 9 fields for 1950-2005
for (n in 1:N){
#assinging lon and lat from csv
x <- locations$lon[n]
y <- locations$lat[n]
#changing into coordinates
coord <- c(360+x,y)
#locating appropriate latitude index
flat = match(abs(lat - coord[2]) < 1/32, 1)
latindex = which(flat %in% 1)
#locating appropriate longitude index
flon = match(abs(lon - coord[1]) < 1/32, 1)
lonindex = which(flon %in% 1)
#creating a blank 3-d array
db_hist <- array(dim=c(20,20,20454))
#looping through variables and models
for(i in 1:6){
for(j in 1:20){
nc_1 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_historical[1]))
var_1 <- as.numeric(var.get.nc(nc_1, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_2 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_historical[2]))
var_2 <- as.numeric(var.get.nc(nc_2, variable=4,
start = c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_3 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_historical[3]))
var_3 <- as.numeric(var.get.nc(nc_3, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,5844)))
#combining results
var <- c(var_1,var_2,var_3)
#filling the 3-d array
db_hist[i,j,] <- var
#crudely printing the progress
print(paste(locations$town[n],variable_1[i],model[j],"historic simulation completed"))
}
}
#saving the 3-d array with the appropriate town name
assign(paste0(locations$town[n],"_hist"),db_hist)
}
#building 3-d arrays [variable, model, time] for 9 fields for RCP 4.5
for (n in 1:N){
#assinging lon and lat from csv
x <- locations$lon[n]
y <- locations$lat[n]
#changing into coordinates
coord <- c(360+x,y)
#locating appropriate latitude index
flat = match(abs(lat - coord[2]) < 1/32, 1)
latindex = which(flat %in% 1)
#locating appropriate longitude index
flon = match(abs(lon - coord[1]) < 1/32, 1)
lonindex = which(flon %in% 1)
#creating a blank 3-d array
db_45 <- array(dim=c(20,20,34333))
#looping through variables and models
for(i in 1:6){
for(j in 1:20){
nc_1 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_45[1]))
var_1 <- as.numeric(var.get.nc(nc_1, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_2 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_45[2]))
var_2 <- as.numeric(var.get.nc(nc_2, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_3 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_45[3]))
var_3 <- as.numeric(var.get.nc(nc_3, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_4 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_45[4]))
var_4 <- as.numeric(var.get.nc(nc_4, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_5 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_45[5]))
var_5 <- as.numeric(var.get.nc(nc_5, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,5113)))
#combining results
var <- c(var_1,var_2,var_3,var_4,var_5)
#filling the 3-d array
db_45[i,j,] <- var
#crudely printing the progress
print(paste(locations$town[n],variable_1[i],model[j],"RCP 4.5 completed"))
}
}
#saving the 3-d array with the appropriate town name
assign(paste0(locations$town[n],"_45"),db_45)
}
#building 3-d arrays [variable, model, time] for 9 fields for RCP 8.5
for (n in 1:N){
#assinging lon and lat from csv
x <- locations$lon[n]
y <- locations$lat[n]
#changing into coordinates
coord <- c(360+x,y)
#locating appropriate latitude index
flat = match(abs(lat - coord[2]) < 1/32, 1)
latindex = which(flat %in% 1)
#locating appropriate longitude index
flon = match(abs(lon - coord[1]) < 1/32, 1)
lonindex = which(flon %in% 1)
#creating a blank 3-d array
db_85 <- array(dim=c(20,20,34333))
#looping through variables and models
for(i in 1:6){
for(j in 1:20){
nc_1 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_85[1]))
var_1 <- as.numeric(var.get.nc(nc_1, variable=4,
start=c(lonindex, latindex, 1),
count=c(1,1,7305)))
nc_2 <- open.nc(paste0(url_1,model[j],"/macav2livneh",variable_1[i],
model[j],condition[j],timestep_85[2]))
var_2 <- as.numeric(var.get.nc(nc_2, variable=4,
start=c(lonindex, latindex, 1),