-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunMyLakeScript-ManualParameterCalibration_forGitHub.R
65 lines (48 loc) · 2.1 KB
/
RunMyLakeScript-ManualParameterCalibration_forGitHub.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
### Run MyLake model Matlab script via R,
### including changing parameters in parameter file
### for manual calibration
### RMP (last modified 2019-MAR-27)
## load necessary packages
library(matlabr)
library(xlsx)
## function to run MyLake Matlab script via R,
## with arguments of folder directory (path),
## parameter value to update for the model run (param.value),
## and row location in the parameter file for this value (row.loc)
run_MyLake_model <- function(path, param.value, row.loc) {
# set folder directory where all necessary files are located;
# this is also were the MODELLED WATER TEMPERATURE & SNOW/ICE OUTPUT will be saved
# as a .csv file (you can change the output variable of interest
# directly, at the end of the Matlab script file)
setwd(path)
# load in parameter file sheet
workbook = loadWorkbook("GILES_para_v12.xls")
sheet = getSheets(workbook)[[1]]
# update parameter file sheet with specified parameter
addDataFrame(
param.value,
sheet = sheet,
startRow = row.loc,
startColumn = 2,
col.names = FALSE,
row.names = FALSE
)
# save over with the new parameter value in the file
saveWorkbook(workbook, "GILES_para_v12.xls")
# run the Matlab script
# (a new window of Matlab Commander will pop up during this;
# you do NOT need to interact with this window, it will
# automatically close upon completion of model run)
run_matlab_script(fname = "RMC_modelGILES_v12_rmp.m")
}
## example run of function
## (can be used in for-loops to test multiple parameter values,
## or to manually adjust multiple parameters)
run_MyLake_model(path = dirname(rstudioapi::getSourceEditorContext()$path),
param.value = 41.3765, # this would change the latitude value
row.loc = 8) # location of latitude parameter is row 8 of parameter file
## option to rename output csv files:
# file.rename(from = "ModelledTemp-Giles_modelOutput.csv",
# to = "NewFileName-Giles-ModelledTemp.csv")
# file.rename(from = "ModelledHis-Giles_modelOutput.csv",
# to = "NewFileName-Giles-ModelledHis.csv")