Problem when trying to fix parameter values in semi-distributed mode
Hi,
I think there is a bug when trying to fix parameters in a semi-distributed mode. It seems to be due to the varying numbers of parameters for each sub-basin (depending on the presence of a lag model or not). Here is an example (reusing some code written by @guillaume.thirel, thanks Guillaume):
library(airGRiwrm)
rm(list = ls())
data(Severn)
data(X0310010)
## hydro network
gauges <- c("54057", "54032", "54001")
nodes <- Severn$BasinsInfo[, c("gauge_id", "downstream_id", "distance_downstream", "area")]
nodes <- nodes[nodes$gauge_id %in% gauges, ]
nodes$model <- "RunModel_CemaNeigeGR4J"
griwrm <- airGRiwrm::CreateGRiwrm(
nodes,
list(id="gauge_id", down="downstream_id", length="distance_downstream")
)
## inputs
basins_obs <- Severn$BasinsObs
dates <- basins_obs[[1]]$DatesR
precip_tot <- cbind(sapply(basins_obs, function(x) {x$precipitation}))
pot_evap_tot <- cbind(sapply(basins_obs, function(x) {x$peti}))
q_obs <- cbind(sapply(basins_obs, function(x) {x$discharge_spec}))
precip <- airGRiwrm::ConvertMeteoSD(griwrm, precip_tot[, griwrm$id])
pot_evap <- airGRiwrm::ConvertMeteoSD(griwrm, pot_evap_tot[, griwrm$id])
hypso <- array(
data=rep(BasinInfo$HypsoData, 3),
dim=c(101,3)
)
colnames(hypso) <- gauges
inputs_model <- airGRiwrm::CreateInputsModel(
griwrm,
DatesR=dates,
Precip=precip,
PotEvap=pot_evap,
TempMean=pot_evap+5, # fake Temp
HypsoData=hypso,
NLayers=setNames(c(5, 6, 7), gauges),
IsHyst=TRUE
)
calib_options <- airGRiwrm::CreateCalibOptions(
inputs_model,
FixedParam=c(
NA, # C (lag)
NA, # X1 (GR4J)
NA, # X2 (GR4J)
NA, # X3 (GR4J)
NA, # X4 (GR4J)
0.25, # cT (CemaNeige)
NA, # Kf (CemaNeige)
10, # Gacc (CemaNeige)
NA # Gseuil (CemaNeige)
)
)
which results for me in the following error:
Error in (function (FUN_MOD, FUN_CALIB = Calibration_Michel, FUN_TRANSFO = NULL, : Incompatibility between 'FixedParam' length and 'FUN_MOD'
The following trick (provided to me by @laurent.strohmenger, thanks Laurent) bypasses the problem:
calib_options <- airGRiwrm::CreateCalibOptions(
inputs_model
)
for (code in names(calib_options))
{
n_param <- length(calib_options[[code]]$FixedParam)
if (n_param == 9)
{
calib_options[[code]]$FixedParam <- c(
NA, # C (lag)
NA, # X1 (GR4J)
NA, # X2 (GR4J)
NA, # X3 (GR4J)
NA, # X4 (GR4J)
0.25, # cT (CemaNeige)
NA, # Kf (CemaNeige)
10, # Gacc (CemaNeige)
NA # Gseuil (CemaNeige)
)
}
else if (n_param == 8)
{
calib_options[[code]]$FixedParam <- c(
NA, # X1 (GR4J)
NA, # X2 (GR4J)
NA, # X3 (GR4J)
NA, # X4 (GR4J)
0.25, # cT (CemaNeige)
NA, # Kf (CemaNeige)
10, # Gacc (CemaNeige)
NA # Gseuil (CemaNeige)
)
}
else
{
stop("sub-basin with number of parameters not equal to 8 or 9")
}
}