Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "script now changes path type based on os" #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 27 additions & 36 deletions cmip6-BC_InputData_meanTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,13 @@ library(colorRamps)
library(rgeos)
library(rgdal)

# Configure path based on os type

path_sep <- "/"

if(.Platform$OS.type != "unix") {
path_sep <- "\\"
}



monthdays <- c(31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
monthcodes <- c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
seasons <- c("wt", "sp", "sm", "at")
seasonmonth.mat <- matrix(monthcodes[c(12, 1:11)],4, byrow=T)

dem.pts <- read.csv(paste("outputs", "dem_cmip6eval.csv", sep=path_sep))
dem.pts <- read.csv("./outputs/dem_cmip6eval.csv")
ecoprovs <- c("BC", sort(as.character(unique(dem.pts$id2))))
ecoprov.names <- c("British Columbia", "Boreal Plains", "Central Interior", "Coast and Mountains", "Georgia Depression", "Northern Boreal Mountains", "Sub-Boreal Interior", "Southern Interior Mountains", "Southern Interior", "Taiga Plains")
# elements <- c("Tave", "Tmax", "Tmin", "PPT", "NFFD")
Expand Down Expand Up @@ -63,7 +54,7 @@ compute_seasonal <- function(cvar_name, cvar_monthly_df, func, sep="") {

dd_0 <- function(tm) {

optimized_params <- read.csv(file = paste("optimizedParameterTables","param_DD_S1.csv", sep=path_sep), sep=',', header = TRUE)
optimized_params <- read.csv(file = "./optimizedParameterTables/param_DD_S1.csv", sep=',', header = TRUE)

#optimized_params <- dd_param[dd_param$Month == m,]
#dd_param <- dd_param_above_5[dd_param_above_5$Region == "All"]
Expand All @@ -89,7 +80,7 @@ dd_0 <- function(tm) {

dd5 <- function(tm) {

optimized_params <- read.csv(file = paste("optimizedParameterTables", "param_DD_S2.csv", sep=path_sep), sep=',', header = TRUE)
optimized_params <- read.csv(file = "./optimizedParameterTables/param_DD_S2.csv", sep=',', header = TRUE)
optimized_params <- optimized_params[optimized_params$Region == "All",]

k <- optimized_params$k
Expand All @@ -112,8 +103,8 @@ dd5 <- function(tm) {

dd_18 <- function(tm) {

optimized_params <- read.csv(file = paste("optimizedParameterTables", "param_DD_S3.csv", sep=path_sep), sep=',', header = TRUE)

optimized_params <- read.csv(file = "./optimizedParameterTables/param_DD_S3.csv", sep=',', header = TRUE)
k <- optimized_params$k
a <- optimized_params$a
b <- optimized_params$b
Expand All @@ -132,7 +123,7 @@ dd_18 <- function(tm) {

dd18 <- function(tm) {

optimized_params <- read.csv(file = paste("optimizedParameterTables", "param_DD_S4.csv", sep=path_sep), sep=',', header = TRUE)
optimized_params <- read.csv(file = "./optimizedParameterTables/param_DD_S4.csv", sep=',', header = TRUE)
optimized_params <- optimized_params[optimized_params$Region == "All",]


Expand All @@ -152,8 +143,8 @@ dd18 <- function(tm) {

# NFFD
compute_nffd <- function(t_min) {
nffd_param <- read.csv(file = paste("optimizedParameterTables", "param_NFFD.csv", sep=path_sep), sep=',', header = TRUE)
nffd_param <- read.csv(file = "./optimizedParameterTables/param_NFFD.csv", sep=',', header = TRUE)

a <- nffd_param$a
b <- nffd_param$b
t0 <- nffd_param$T0
Expand Down Expand Up @@ -207,8 +198,8 @@ ffp <-function(effp,bffp) {
# tm: min temperature for that month
pas <- function(t_min_monthly, ppt_monthly) {

pas_param <- read.csv(file = paste("optimizedParameterTables", "param_PAS.csv", sep=path_sep), sep=',', header = TRUE)
pas_param <- read.csv(file = "./optimizedParameterTables/param_PAS.csv", sep=',', header = TRUE)

b <- pas_param$b
t0 <- pas_param$T0

Expand Down Expand Up @@ -270,7 +261,7 @@ rh <- function(t_min_monthly, t_max_monthly) {
# ==========================================
#step 2d: create mean observational time series for province/ecoregion

obs.ts <- read.csv(paste("outputs","obs.ts.csv", sep=path_sep))
obs.ts <- read.csv("./outputs/obs.ts.csv")

## station observations
ecoprov=ecoprovs[2]
Expand Down Expand Up @@ -395,13 +386,13 @@ for(ecoprov in ecoprovs){
##########################
ts <- aggregate(gridded_data, by=list(gridded_data$Year), FUN = mean, na.rm=T)[,-1]

write.csv(ts,paste(paste("gridded_output", "ts.obs.mean.", sep=path_sep), ecoprov, ".csv", sep=""), row.names=FALSE)
write.csv(ts,paste("./gridded_output/ts.obs.mean.", ecoprov, ".csv", sep=""), row.names=FALSE)
print(ecoprov)
}


## ERA5
era5.ts <- read.csv(paste("outputs","era5.ts.csv", sep=path_sep))
era5.ts <- read.csv("./outputs/era5.ts.csv")

ecoprov=ecoprovs[2]
for(ecoprov in ecoprovs){
Expand Down Expand Up @@ -524,7 +515,7 @@ for(ecoprov in ecoprovs){
# Aggregate all years together
##########################
ts <- aggregate(gridded_data, by=list(gridded_data$Year), FUN = mean, na.rm=T)[,-1]
write.csv(ts,paste(paste("gridded_output","ts.era5.mean.", sep=path_sep), ecoprov, ".csv", sep=""), row.names=FALSE)
write.csv(ts,paste("./gridded_output/ts.era5.mean.", ecoprov, ".csv", sep=""), row.names=FALSE)
print(ecoprov)
}

Expand All @@ -535,7 +526,7 @@ for(ecoprov in ecoprovs){
# step 3: GCM Files
# ==========================================

files <- list.files("outputs", pattern=paste("^ts.*", sep="."))
files <- list.files("outputs/", pattern=paste("^ts.*", sep="."))
gcms <- unique(sapply(strsplit(files, "[.]"), "[", 2))
gcms <- gcms[-grep("obs|era", gcms)]

Expand All @@ -547,7 +538,7 @@ for(i in 1:length(gcms)){
# ==========================================
# step 3b: calculate average time series across BC/ecoprovince for each gcm and scenario

files <- list.files("outputs")
files <- list.files("outputs/")
files <- files[grep(paste("ts", gcm, sep="."), files)] #these ts (time series) files have one record for each grid cell for each year.
run.list <- sapply(strsplit(files, "[.]"), "[", 3)
scenario.list <- sapply(strsplit(run.list, "_"), "[", 1)
Expand All @@ -560,7 +551,7 @@ for(i in 1:length(gcms)){
ripfs <- unique(ripf.list[which(scenario.list==scenario)])
ripf <- ripfs
for(ripf in ripfs){
data.full <- read.csv(paste(paste("outputs","ts.", sep=path_sep), gcm, ".", scenario, "_", ripf, ".csv", sep=""))
data.full <- read.csv(paste("./outputs/ts.", gcm, ".", scenario, "_", ripf, ".csv", sep=""))
ecoprov <- ecoprovs[1]
for(ecoprov in ecoprovs){
if(ecoprov=="BC") s <- 1:dim(data.full)[1] else {
Expand Down Expand Up @@ -703,7 +694,7 @@ for(i in 1:length(gcms)){
}
names(ensemble) <- ripfs
ensemble <- data.frame(Year=x, ensemble)
write.csv(ensemble, paste(paste("gridded_output","ensemble", sep=path_sep), gcm, ecoprov, variable, scenario, "csv", sep="."), row.names=FALSE)
write.csv(ensemble,paste("./gridded_output/ensemble", gcm, ecoprov, variable, scenario, "csv", sep="."), row.names=FALSE)
# print(variable)
}
# print(ecoprov)
Expand All @@ -720,9 +711,9 @@ for(i in 1:length(gcms)){
# min, max, and mean for each model and for whole ensemble
# ==========================================

variables <-names(read.csv(paste("gridded_output", "ts.era5.mean.SIM.csv", sep=path_sep)))[-1] # changed to file with all variables
variables <-names(read.csv("./gridded_output/ts.era5.mean.SIM.csv"))[-1]

files <- list.files(paste("gridded_output", path_sep, sep="") , pattern=paste("^ensemble.*", sep="."))
files <- list.files("./gridded_output/", pattern=paste("^ensemble.*", sep="."))
gcms.all <- unique(sapply(strsplit(files, "[.]"), "[", 2))
scenarios <- unique(sapply(strsplit(files, "[.]"), "[", 5))
funs <- c("min", "max", "mean")
Expand All @@ -736,21 +727,21 @@ for(fun in funs){
variable <- variables[1]
for(variable in variables){

files <- list.files(paste("gridded_output", path_sep, sep=""), pattern=paste("^ensemble.*", ecoprov, variable, scenario,"*", sep="."))
files <- list.files("./gridded_output/", pattern=paste("^ensemble.*", ecoprov, variable, scenario,"*", sep="."))
gcms <- unique(sapply(strsplit(files, "[.]"), "[", 2))
gcm <- gcms[1]
data <- read.csv(paste(paste("gridded_output", "ensemble", sep=path_sep), gcm, ecoprov, variable, scenario, "csv", sep="."))
data <- read.csv(paste("./gridded_output/ensemble", gcm, ecoprov, variable, scenario, "csv", sep=".")) ##
temp <- data.frame(data[,1], matrix(NA, dim(data)[1],length(gcms.all)))
names(temp) <- c("Year", gcms.all)
for(gcm in gcms){
data <- read.csv(paste(paste("gridded_output", "ensemble", sep=path_sep), gcm, ecoprov, variable, scenario, "csv", sep=".")) ##
data <- read.csv(paste("./gridded_output/ensemble", gcm, ecoprov, variable, scenario, "csv", sep="."))
stat <- if(dim(data)[2]==2) data[,2] else round(apply(data[,-1], 1, fun),1)
temp[match(data$Year, temp$Year),which(names(temp)==gcm)] <- stat
print(gcm)
}
temp <- cbind(temp, round(apply(temp[,-1], 1, fun, na.rm=T),1))
names(temp) <- c("Year", gcms.all, "ensemble")
write.csv(temp,paste(paste(paste("gridded_output","ens", sep=path_sep), fun, sep=""), ecoprov, variable, scenario, "csv", sep="."), row.names=FALSE)
write.csv(temp,paste(paste("./gridded_output/ens", fun, sep=""), ecoprov, variable, scenario, "csv", sep="."), row.names=FALSE)
print(variable)
}
print(ecoprov)
Expand All @@ -761,7 +752,7 @@ for(fun in funs){
}

## rbind the scenarios together and write out.
files <- list.files(paste("gridded_output", path_sep, sep=""), pattern=paste("^ensemble.*", sep="."))
files <- list.files("./gridded_output/", pattern=paste("^ensemble.*", sep="."))
scenarios <- unique(sapply(strsplit(files, "[.]"), "[", 5))
variables <- unique(sapply(strsplit(files, "[.]"), "[", 4))
ecoprovs <- unique(sapply(strsplit(files, "[.]"), "[", 3))
Expand All @@ -770,11 +761,11 @@ for(fun in funs){
for(ecoprov in ecoprovs){
for(variable in variables){
for(scenario in scenarios){
temp <- read.csv(paste(paste(paste("gridded_output","ens", sep=path_sep), fun, sep=""), ecoprov, variable, scenario, "csv", sep="."))
temp <- read.csv(paste(paste("./gridded_output/ens", fun, sep=""), ecoprov, variable, scenario, "csv", sep="."))
data <- if(scenario==scenarios[1]) data.frame(scenario=rep(scenario, dim(temp)[1]), temp) else rbind(data, data.frame(scenario=rep(scenario, dim(temp)[1]), temp))
# print(scenario)
}
write.csv(data,paste(paste(paste("gridded_output", "ens", sep=path_sep), fun, sep=""), ecoprov, variable, "csv", sep="."), row.names=FALSE)
write.csv(data,paste(paste("./gridded_output/ens", fun, sep=""), ecoprov, variable, "csv", sep="."), row.names=FALSE)
# print(variable)
}
print(ecoprov)
Expand Down