-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomate_Raven.R
68 lines (52 loc) · 3.66 KB
/
Automate_Raven.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
######################################################################################
# Automated Raven Hydrological Model Run #
# Created By: Harry Singh #
# Date: July-19-2019 #
######################################################################################
######################## Pre-Requisite Block ##########################
# Raven model directory setup
setwd('D:/PAPER2/')
wshed_name <- 'Kootenay_Lake_ws' # Name of watershed
raven_dir <- 'D:/PAPER2/Raven_biv/' # Parent directory
raven_in_dir <- 'D:/PAPER2/Raven_biv/dt/' # Data directory where input file will be created
raven_out_dir <- 'D:/PAPER2/Raven_biv/res/' # Output directory where results will be created
# Parent input file - Named "data.tb0"
# There needs to be a parent input file in the raven_dir directory containing
# the top 25 lines of the raven input format. This file will be copied to input
# directory in each run and input values of tmp and pr will be appended to run the model.
######################## Pre-Requisite Block ends here ################
######################## Helper Function Block ########################
# Command batch file
# To run Raven from R, a command batch file will be created in the directory
batch_string <- paste(raven_dir, 'Raven.exe ', raven_dir, wshed_name, ' -o ',raven_out_dir, sep="")
write(batch_string, file = paste(raven_dir,'rvn.cmd',sep=""), append = F) # Write the data to the input file
######################## Helper Function Block ends here ###############
######################## Primary function Block ########################
run_raven <- function(input_data) {
# Input data is a matrix with two rows (tmp, pr)
data_to_write <- paste(input_data[,1],' ',input_data[,2]) # Convert data to space separated character format
file.copy(from = paste(raven_dir,'data.tb0',sep=""), to = raven_in_dir, overwrite = T) # Copy the parent input file to data directory
write(data_to_write, file = paste(raven_in_dir,'data.tb0',sep=""), append = T) # Write the data to the input file
system(paste(raven_dir,'rvn.cmd',sep="")) # Run the raven hydrological model from the batch file created earlier
raw_data <- read.table(list.files(path = raven_out_dir, pattern = 'Hydro', full.names = T),
header = F, skip = 22, sep=" ") # Read the raw hydrograph data
raw_data <- raw_data[,-1]
# Create final dataframe with input and output to return
res_df <- data.frame(
'tmp' = input_data[,1],
'pr' = input_data[,2],
'sim_pr' = raw_data$V2,
'sim_qq' = raw_data$V3
)
return(res_df)
}
######################## Primary function Block ends here ##############
######################## Interactive Block #############################
orig_data_list <- readRDS(paste(raven_dir,'raw_klk_tmp_pr.rds', sep=""))[2:7] # Read tmp, pr data, original
qdm_data_list <- readRDS(paste(raven_dir,'qdm_mbc_data.rds', sep=""))[[1]] # Read tmp, pr data, qdm corrected
mbc_data_list <- readRDS(paste(raven_dir,'qdm_mbc_data.rds', sep=""))[[2]] # Read tmp, pr data, mbc corrected
# This is a list of 7 datasets (1 Guage, 1 AHCCD, 5 gridded products)
orig_res_list <- lapply(orig_data_list, run_raven) # Run the raven model over each dataset of original data
qdm_res_list <- lapply(qdm_data_list, run_raven) # Run the raven model over each dataset of mbc corrected data
mbc_res_list <- lapply(mbc_data_list, run_raven) # Run the raven model over each dataset of mbc corrected data
saveRDS(list(orig_res_list,qdm_res_list,mbc_res_list), 'D:/PAPER2/Raven_biv/orig_qdm_mbc_raven_results.rds',compress = F)