-
Notifications
You must be signed in to change notification settings - Fork 2
/
Spatial_T1_extraction.Rmd
157 lines (104 loc) · 5.06 KB
/
Spatial_T1_extraction.Rmd
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
---
title: "Summarizing T1 metrics for whole climate region"
author: "Amber Runyon"
date: "6/29/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Initials
```{r Initials, echo=FALSE, message=FALSE, warning=FALSE}
rm(list = ls())
library(stars);library(dplyr);library(ggplot2);library(ggthemes);library(viridis);library(here);library(ggrepel);library(rlang);library(ggbiplot)
area <- 'wrst_simple' #wrst_mtns, copper_plateau, coastal_mtns, eastern_coast
data.dir <- "D:/NCAR_AK/met/monthly/BCSD/"
vic.dir <- "D:/NCAR_AK/vic_hydro/monthly/BCSD"
plot.dir <- paste0("C:/Users/achildress/Documents/wrst_temp/T1-extraction/",area)
dir.create(plot.dir,showWarnings=FALSE)
historical.period <- as.character(seq(1950,1999,1))
future.period <- as.character(seq(2025,2055,1))
GCMs <- list.files(path = data.dir)
RCPs <- c("rcp45", "rcp85")
GCM.RCP <- sort(apply(expand.grid(GCMs, RCPs), 1, paste, collapse="."))
```
## Load Spatial Data
```{r Load spatial data}
# Spatial
boundary.dir <- "C:/Users/achildress/DOI/NPS-WRST-Resource Stewardship Strategy - Climate/Climate futures development/Data/Boundary_shapefiles/"
shp <- st_read(paste0(boundary.dir, area, ".shp")) # Wrangell Mountains
shp <- st_transform(shp, 3338)
# plot(st_geometry(shp))
```
Create empty dataframes that will store climate summaries for each GCM.rcp as loop through data
```{r Create empty dataframes}
variables <- c("Tmean_F","Precip_in","DJF_TmeanF", "MAM_TmeanF", "JJA_TmeanF", "SON_TmeanF", "DJF_Precip_in", "MAM_Precip_in", "JJA_Precip_in","SON_Precip_in", "Annual_max_SWE", "MAM_SON_SWE", "water_balance")
Baseline_Means <- as.data.frame(matrix(data=NA,nrow=length(GCMs)*length(RCPs),ncol=length(variables)+2))
names(Baseline_Means) <- c("GCM", "RCP", variables)
Future_Means <- as.data.frame(matrix(data=NA,nrow=length(GCMs)*length(RCPs),ncol=length(variables)+2))
names(Future_Means) <- c("GCM", "RCP", variables)
Deltas <- as.data.frame(matrix(data=NA,nrow=length(GCMs)*length(RCPs),ncol=length(variables)+2))
names(Deltas) <- c("GCM", "RCP", variables)
```
Loop through met GCM.rcp and summarize each variable
```{r Create met variables, results=hide, eval=TRUE}
memory.limit(size = 60000)
source(here::here("Code", "Metric-development", "met-T1-variables.R"),echo = FALSE)
```
Loop through vic GCM.rcp and summarize each variable
```{r Create vic variables}
source(here::here("Code", "Metric-development", "vic-T1-variables.R"),echo = FALSE)
write.csv(Baseline_Means,paste0(plot.dir,"/Baseline_Means-T1.csv"),row.names = FALSE)
write.csv(Future_Means,paste0(plot.dir,"/Future_Means-T1.csv"),row.names = FALSE)
write.csv(Deltas,paste0(plot.dir,"/Deltas-T1.csv"),row.names = FALSE)
```
Run PCA and create scatterplots for T1 variables
```{r Run PCA}
# Run scatterplot
## REDO PLOT AND MULTIPLY PCP BY 30
PlotWidth = 15
PlotHeight = 9
scatterplot <- function(df, X, Y, title,xaxis,yaxis){
plot <- ggplot(data=df, aes(x={{ X }}, y={{ Y }})) +
geom_text_repel(aes(label=paste(GCM,RCP,sep="."))) +
geom_point(colour="black",size=4) +
theme(axis.text=element_text(size=18),
axis.title.x=element_text(size=18,vjust=-0.2),
axis.title.y=element_text(size=18,vjust=0.2),
plot.title=element_text(size=18,face="bold",vjust=2,hjust=0.5),
legend.text=element_text(size=18), legend.title=element_text(size=16)) +
###
labs(title =title,
x = paste0("Changes in ",xaxis), # Change
y = paste0("Changes in ",yaxis)) + #change
scale_color_manual(name="Scenarios", values=c("black")) +
# scale_fill_manual(name="Scenarios",values = c("black")) +
theme(legend.position="none")
plot
}
scatterplot(df=Deltas,X=Tmean_F,Y=Precip_in,title="Tmean vs Precip scatterplot",xaxis="Avg Annual Temp (F)",yaxis="Avg Annual Precip (in)")
ggsave("Scatter_Tmean-pcp.png", path = plot.dir, width = PlotWidth, height = PlotHeight)
scatterplot(df=Deltas,X=DJF_TmeanF,Y=Annual_max_SWE,title="Winter temp vs max SWE scatterplot",xaxis="Avg DJF Temp (F)",yaxis="Avg MAX SWE (in)")
ggsave("Scatter_DJFTmean-MaxSWE.png", path = plot.dir, width = PlotWidth, height = PlotHeight)
scatterplot(df=Deltas,X=JJA_TmeanF,Y=water_balance,title="Summer Tmean vs water balance scatterplot",xaxis="Avg JJA Tmean (F)",yaxis="Avg Annual Water Balance (in)")
ggsave("Scatter_JJATmean-WB.png", path = plot.dir, width = PlotWidth, height = PlotHeight)
# PCA df setup
head(Deltas)
Deltas.rownames <- Deltas[,-c(1:2)]
rownames(Deltas.rownames) <- paste(Deltas$GCM,Deltas$RCP,sep=".")
PCA <- prcomp(Deltas.rownames[,c(1:length(Deltas.rownames))], center = TRUE,scale. = TRUE)
head(PCA$rotation)
head(PCA$x)
summary(PCA)
str(PCA)
ggbiplot(PCA, labels=rownames(Deltas.rownames))
ggsave("PCA-plot.png", path = plot.dir, width = PlotWidth, height = PlotHeight)
pca.x<-as.data.frame(PCA$x)
# This method doesn't return the exact models we selected,
# but we may not have selected the most divergent models
CF1 <- rownames(pca.x)[which.min(pca.x$PC1)]
CF2 <- rownames(pca.x)[which.max(pca.x$PC1)]
CF3 <- rownames(pca.x)[which.min(pca.x$PC2)]
CF4 <- rownames(pca.x)[which.max(pca.x$PC2)]
# Select PCs
```