forked from nationalparkservice/WRST-climate-futures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF-selection.Rmd
166 lines (113 loc) · 5.49 KB
/
CF-selection.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
157
158
159
160
161
162
163
164
165
---
title: "Selecting CFs for park"
author: "Amber Runyon"
date: "6/29/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Initials
To run:
1. Update SiteID
2. Run through data extraction (takes 30-45 mins)
3. At end and select CFs after completed
```{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)
SiteID <- "LACL" #*UPDATE*
data.dir <- "E:/NCAR_AK/met/monthly/BCSD/"
vic.dir <- "E:/NCAR_AK/vic_hydro/monthly/BCSD"
plot.dir <- "C:/Users/arunyon/3D Objects/Local-files/RCF_Testing/LACL/" #*UPDATE*
dir.create(plot.dir,showWarnings=FALSE)
FigDir <- paste0(plot.dir,"/Figures")
dir.create(FigDir,showWarnings=FALSE)
DataOut <- paste0(plot.dir,"/Data")
dir.create(DataOut,showWarnings=FALSE)
historical.period <- as.character(seq(1950,1999,1))
future.period <- as.character(seq(2035,2065,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
nps_boundary <- st_read('./Data/nps_boundary/nps_boundary.shp')
park <- filter(nps_boundary, UNIT_CODE == SiteID)
shp <- st_transform(park, 3338)
```
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)
```
```{r Create vic variables}
source(here::here("Code", "Metric-development", "vic-T1-variables.R"),echo = FALSE)
write.csv(Baseline_Means,paste0(DataOut,"/Baseline_Means.csv"),row.names=FALSE)
write.csv(Future_Means,paste0(DataOut,"/Future_Means.csv"),row.names=FALSE)
write.csv(Deltas,paste0(DataOut,"/Deltas.csv"),row.names=FALSE)
```
Run PCA and create scatterplots for T1 variables
```{r Run PCA}
# Run scatterplot
## REDO PLOT AND MULTIPLY PCP BY 30
Deltas <- read.csv(paste0(DataOut,"/Deltas.csv"))
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 = xaxis, # Change
y = 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=paste0("Changes in climate means in 2050 by GCM run at ", SiteID),
xaxis="Changes in annual average temperature (\u00B0F)",
yaxis="Changes in annual average precipitation (in)")
ggsave("Scatter_Tmean-pcp.png", path = FigDir, 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 = FigDir, width = PlotWidth, height = PlotHeight)
# Select PCs
pca.df<-as.data.frame(PCA$x)
PC1 <- factor(c(rownames(pca.df)[which.min(pca.df$PC1)],rownames(pca.df)[which.max(pca.df$PC1)]))
PC2<- c(rownames(pca.df)[which.min(pca.df$PC2)],rownames(pca.df)[which.max(pca.df$PC2)])
CFs <- c("MRI-CGCM3.rcp85","CanESM2.rcp85") #*UPDATE*
# colors2 <- c("#6EB2D4", "#CA0020")
colors2 <- c("#6EB2D4","#CA0020") #*UPDATE*
#Updated Scatter
scatterplot(df=Deltas,X=Tmean_F,Y=Precip_in,title="Tmean vs Precip scatterplot",xaxis="Avg Annual Temp (F)",yaxis="Avg Annual Precip (in)") + geom_point(aes(x=mean(Tmean_F[which(paste(GCM,RCP,sep=".")==CFs[1])]), y=mean(Precip_in[which(paste(GCM,RCP,sep=".")==CFs[1])])), shape=21, size=10, stroke=3, colour=colors2[1]) +
geom_point(aes(x=mean(Tmean_F[which(paste(GCM,RCP,sep=".")==CFs[2])]), y=mean(Precip_in[which(paste(GCM,RCP,sep=".")==CFs[2])])), shape=21, size=10, stroke=3, colour=colors2[2]) +
geom_point(aes(x=mean(Tmean_F[which(paste(GCM,RCP,sep=".")==CFs[3])]), y=mean(Precip_in[which(paste(GCM,RCP,sep=".")==CFs[3])])), shape=21, size=10, stroke=3, colour=colors2[3])
ggsave("Selected-CFs-scatterplot.png", width = PlotWidth, height = PlotHeight, path = FigDir)
```