Commit e43abbfd authored by unknown's avatar unknown
Browse files

[airGR_dev_0.7.2] Dépot initial

parent 7e7b9657
DIR_WD <- "C:/Data/Hydrologie/Codes/airGR_dev/airGR_dev_0.7/";
DIR_EXPORT_RDA <- paste(DIR_WD,"files_data/",sep="");
DIR_EXPORT_TXT <- paste(DIR_WD,"files_misc/airGR_AdvancedExample/Test_DataInput/",sep="");
### DIR_DATA_INPUT_FR <- "C:/Data/Hydrologie/BD/BD_FR_IRSTEA2010/BD_BASSINS/";
DIR_DATA_INPUT_FR <- paste(DIR_WD,"tmp_data_input_DO_NOT_CIRCULATE/",sep="");
......
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR.Rcheck"
:: rd %DIR1% /s /q
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\src-i386"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\src-x64"
rd %DIR1% /s /q
rd %DIR2% /s /q
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\data\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\inst\"
SET DIR3="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\man\"
SET DIR4="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\R\"
SET DIR5="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\src\"
SET DIR6="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\tests\"
DEL %DIR1%*.* /q
DEL %DIR2%*.* /q
DEL %DIR3%*.* /q
DEL %DIR4%*.* /q
DEL %DIR5%*.* /q
DEL %DIR6%*.* /q
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_data"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\data"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_R"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\R"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_src"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\src"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_tests"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\tests"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\"
SET FIL1="DESCRIPTION"
SET FIL2="DESCRIPTION"
COPY %DIR1%%FIL1% %DIR2%%FIL2% /Y
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR_package\man\"
SET FIL1="airGR-package.Rd"
SET FIL2="airGR-package.Rd"
COPY %DIR1%%FIL1% %DIR2%%FIL2% /Y
::-----------------------------------------------------------------------------
:: pause
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR.Rcheck"
:: rd %DIR1% /s /q
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\src-i386"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\src-x64"
rd %DIR1% /s /q
rd %DIR2% /s /q
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\data\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\inst\"
SET DIR3="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\man\"
SET DIR4="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\R\"
SET DIR5="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\src\"
SET DIR6="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\tests\"
DEL %DIR1%*.* /q
DEL %DIR2%*.* /q
DEL %DIR3%*.* /q
DEL %DIR4%*.* /q
DEL %DIR5%*.* /q
DEL %DIR6%*.* /q
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_data"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\data"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_R"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\R"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_src"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\src"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_tests"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\tests"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_R_PLUS"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\R"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_src_PLUS"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\src"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_tests_PLUS"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\tests"
..\..\robocopy.exe %DIR1% %DIR2% /E /TEE
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\"
SET FIL1="DESCRIPTION_PLUS"
SET FIL2="DESCRIPTION"
COPY %DIR1%%FIL1% %DIR2%%FIL2% /Y
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR_package\man\"
SET FIL1="airGR-package.Rd"
SET FIL2="airGR-package.Rd"
COPY %DIR1%%FIL1% %DIR2%%FIL2% /Y
::-----------------------------------------------------------------------------
:: pause
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\"
SET DIR3="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\"
SET FIL0="ReadMe.txt"
COPY %DIR1%%FIL0% %DIR2%%FIL0% /Y
COPY %DIR1%%FIL0% %DIR3%%FIL0% /Y
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\"
SET DIR3="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\"
SET FIL0="ReleaseNotes.txt"
COPY %DIR1%%FIL0% %DIR2%%FIL0% /Y
COPY %DIR1%%FIL0% %DIR3%%FIL0% /Y
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\airGR.Rcheck\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\"
SET FIL0="airGR-manual.pdf"
COPY %DIR1%%FIL0% %DIR2%%FIL0% /Y
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\airGR.Rcheck\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\"
SET FIL0="airGR-manual.pdf"
COPY %DIR1%%FIL0% %DIR2%%FIL0% /Y
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\"
SET DIR3="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\"
SET FIL1="14-07-09 airGR_0.7 Description_FR.pdf"
SET FIL2="airGR-description-complement-FR.pdf"
COPY %DIR1%%FIL1% %DIR2%%FIL2% /Y
COPY %DIR1%%FIL1% %DIR3%%FIL2% /Y
::-----------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2\files_misc\"
SET DIR2="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\"
SET DIR3="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS\"
SET FIL0="airGR_AdvancedExample.zip"
COPY %DIR1%%FIL0% %DIR2%%FIL0% /Y
COPY %DIR1%%FIL0% %DIR3%%FIL0% /Y
::-----------------------------------------------------------------------------
:: pause
@echo off
echo - -
echo ---------------------------------------------------------
echo - Copie miroir de repertoires avec ROBOCOPY -
echo ---------------------------------------------------------
echo - -
echo - ATTENTION : Ecrasement des fichiers -
echo - -
:: pause
:: pause
:: :: ------------------------------------------------------------------------------
:: SET ENTREE="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.6.2"
:: SET SORTIE="\\anasdata.cemagref.fr\Projets\hban\partage\laurent.coron\Codes\airGR_0.6.2"
:: ..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE /LOG:%DIR_EXE%MIRROR_COPY_A_B.log
:: :: ------------------------------------------------------------------------------
:: SET ENTREE="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.6.2\airGR_0.6_GettingStarted"
:: SET SORTIE="C:\Data\MyWorkingDir\airGR_0.6_GettingStarted"
:: ..\..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE /LOG:%DIR_EXE%MIRROR_COPY_A_B.log
:: :: ------------------------------------------------------------------------------
:: SET ENTREE="C:\Data\MyWorkingDir\airGR_0.6_GettingStarted\Package_Install_Zip"
:: SET SORTIE="\\anasdata.cemagref.fr\Projets\hban\Partage\laurent.coron\airGR_GettingStarted\Package_Install_Zip"
:: ..\..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE /LOG:%DIR_EXE%MIRROR_COPY_A_B.log
:: :: ------------------------------------------------------------------------------
:: SET ENTREE="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.1"
:: SET SORTIE="\\anasdata.cemagref.fr\Projets\hban\partage\laurent.coron\Codes\airGR_0.7.1"
:: ..\..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE
:: :: ------------------------------------------------------------------------------
:: ------------------------------------------------------------------------------
SET DIR1="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2\"
SET DIR2="\\anasdata.cemagref.fr\Projets\hban\partage\laurent.coron\airGR\"
SET FIL1="airGR_0.7.2.tar.gz"
SET FIL2="airGR_0.7.2.zip"
SET FIL3="airGR_AdvancedExample.zip"
SET FIL4="airGR-description-complement-FR.pdf"
SET FIL5="airGR-manual.pdf"
SET FIL6="ReadMe.txt"
SET FIL7="ReleaseNotes.txt"
COPY %DIR1%%FIL1% %DIR2%%FIL1%
COPY %DIR1%%FIL2% %DIR2%%FIL2%
COPY %DIR1%%FIL3% %DIR2%%FIL3%
COPY %DIR1%%FIL4% %DIR2%%FIL4%
COPY %DIR1%%FIL5% %DIR2%%FIL5%
COPY %DIR1%%FIL6% %DIR2%%FIL6%
COPY %DIR1%%FIL7% %DIR2%%FIL7%
:: ------------------------------------------------------------------------------
SET ENTREE="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2"
SET SORTIE="\\anasdata.cemagref.fr\Projets\hban\partage\laurent.coron\Codes\airGR_0.7.2"
..\..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE
:: ------------------------------------------------------------------------------
SET ENTREE="C:\Data\Hydrologie\Codes\airGR_dev\airGR_0.7.2_PLUS"
SET SORTIE="\\anasdata.cemagref.fr\Projets\hban\partage\laurent.coron\Codes\airGR_0.7.2_PLUS"
..\..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE
:: ------------------------------------------------------------------------------
SET ENTREE="C:\Data\Hydrologie\Codes\airGR_dev\airGR_dev_0.7.2"
SET SORTIE="\\anasdata.cemagref.fr\Projets\hban\partage\laurent.coron\Codes\airGR_dev_0.7.2"
..\..\robocopy.exe %ENTREE% %SORTIE% /MIR /XO /R:2 /TEE
:: ------------------------------------------------------------------------------
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
#' @name BasinInfo
#' @docType data
#' @title Data sample: characteristics of a fictional catchment (either L0123001 or L0123002)
#' @description
#' R-object containing the code, station's name, area and hypsometric curve of the catchment.
#' @encoding UTF-8
#' @format
#' List named 'BasinInfo' containing
#' \itemize{
#' \item two strings: catchment's code and station's name
#' \item one float: catchment's area in km2
#' \item one numeric vector: catchment's hypsometric curve (min, quantiles 01 to 99 and max) in metres
#' }
#' @examples
#' require(airGR)
#' data(L0123001)
#' str(BasinInfo)
NULL
#' @name BasinObs
#' @docType data
#' @title Data sample: time series of observations of a fictional catchment (either L0123001 or L0123002)
#' @description
#' R-object containing the times series of precipitation, temperature, potential evapotranspiration and discharges. \cr
#' Times series are at the daily time-step for use with daily models such as GR4J, GR5J, GR6J, CemaNeigeGR4J, CemaNeigeGR5J and CemaNeigeGR6J.
#' @encoding UTF-8
#' @format
#' Data frame named 'BasinObs' containing
#' \itemize{
#' \item one POSIXlt vector: time series dates in the POSIXlt format
#' \item five numeric vectors: time series of catchment average precipitation [mm], catchment average air temperature [degC], catchment average potential evapotranspiration [mm], outlet discharge [l/s], outlet discharge [mm]
#' }
#' @examples
#' require(airGR)
#' data(L0123001)
#' str(BasinObs)
NULL
#*************************************************************************************************
#' Calibration algorithm which minimises the error criterion using the provided functions. \cr
#*************************************************************************************************
#' @title Calibration algorithm which minimises an error criterion on the model outputs using the provided functions
#' @author Laurent Coron (June 2014)
#' @seealso \code{\link{Calibration_HBAN}}, \code{\link{Calibration_optim}},
#' \code{\link{RunModel}}, \code{\link{ErrorCrit}}, \code{\link{TransfoParam}},
#' \code{\link{CreateInputsModel}}, \code{\link{CreateRunOptions}},
#' \code{\link{CreateInputsCrit}}, \code{\link{CreateCalibOptions}}.
#' @example tests/example_Calibration.R
#' @export
#' @encoding UTF-8
#_FunctionInputs__________________________________________________________________________________
#' @param InputsModel [object of class \emph{InputsModel}] see \code{\link{CreateInputsModel}} for details
#' @param RunOptions [object of class \emph{RunOptions}] see \code{\link{CreateRunOptions}} for details
#' @param InputsCrit [object of class \emph{InputsCrit}] see \code{\link{CreateInputsCrit}} for details
#' @param CalibOptions [object of class \emph{CalibOptions}] see \code{\link{CreateCalibOptions}} for details
#' @param FUN_MOD [function] hydrological model function (e.g. RunModel_GR4J, RunModel_CemaNeigeGR4J)
#' @param FUN_CRIT [function] error criterion function (e.g. ErrorCrit_RMSE, ErrorCrit_NSE)
#' @param FUN_CALIB (optional) [function] calibration algorithm function (e.g. Calibration_HBAN, Calibration_optim), default=Calibration_HBAN
#' @param FUN_TRANSFO (optional) [function] model parameters transformation function, if the FUN_MOD used is native in the package FUN_TRANSFO is automatically defined
#' @param quiet (optional) [boolean] boolean indicating if the function is run in quiet mode or not, default=FALSE
#_FunctionOutputs_________________________________________________________________________________
#' @return [list] see \code{\link{Calibration_HBAN}} or \code{\link{Calibration_optim}}
#**************************************************************************************************
Calibration <- function(InputsModel,RunOptions,InputsCrit,CalibOptions,FUN_MOD,FUN_CRIT,FUN_CALIB=Calibration_HBAN,FUN_TRANSFO=NULL,quiet=FALSE){
return( FUN_CALIB(InputsModel,RunOptions,InputsCrit,CalibOptions,FUN_MOD,FUN_CRIT,FUN_TRANSFO,quiet=quiet) )
}
This diff is collapsed.
#*************************************************************************************************
#' Calibration algorithm which minimises the error criterion. \cr
#' \cr
#' The algorithm is based on the "optim" function from the "stats" R-package
#' (using method="L-BFGS-B", i.e. a local optimization quasi-Newton method).
#'
#' To optimise the exploration of the parameter space, transformation functions are used to convert
#' the model parameters. This is done using the TransfoParam functions.
#*************************************************************************************************
#' @title Calibration algorithm which minimises the error criterion using the stats::optim function
#' @author Laurent Coron (August 2013)
#' @example tests/example_Calibration_optim.R
#' @seealso \code{\link{Calibration}}, \code{\link{Calibration_HBAN}},
#' \code{\link{RunModel_GR4J}}, \code{\link{TransfoParam_GR4J}}, \code{\link{ErrorCrit_RMSE}},
#' \code{\link{CreateInputsModel}}, \code{\link{CreateRunOptions}},
#' \code{\link{CreateInputsCrit}}, \code{\link{CreateCalibOptions}}.
#' @encoding UTF-8
#' @export
#_FunctionInputs__________________________________________________________________________________
#' @param InputsModel [object of class \emph{InputsModel}] see \code{\link{CreateInputsModel}} for details
#' @param RunOptions [object of class \emph{RunOptions}] see \code{\link{CreateRunOptions}} for details
#' @param InputsCrit [object of class \emph{InputsCrit}] see \code{\link{CreateInputsCrit}} for details
#' @param CalibOptions [object of class \emph{CalibOptions}] see \code{\link{CreateCalibOptions}} for details
#' @param FUN_MOD [function] hydrological model function (e.g. RunModel_GR4J, RunModel_CemaNeigeGR4J)
#' @param FUN_CRIT [function] error criterion function (e.g. ErrorCrit_RMSE, ErrorCrit_NSE)
#' @param FUN_TRANSFO (optional) [function] model parameters transformation function, if the FUN_MOD used is native in the package FUN_TRANSFO is automatically defined
#' @param quiet (optional) [boolean] boolean indicating if the function is run in quiet mode or not, default=FALSE
#_FunctionOutputs_________________________________________________________________________________
#' @return [list] list containing the function outputs organised as follows:
#' \tabular{ll}{
#' \emph{$ParamFinalR } \tab [numeric] parameter set obtained at the end of the calibration \cr
#' \emph{$CritFinal } \tab [numeric] error criterion obtained at the end of the calibration \cr
#' \emph{$Nruns } \tab [numeric] number of model runs done during the calibration \cr
#' \emph{$CritName } \tab [character] name of the calibration criterion \cr
#' \emph{$CritBestValue} \tab [numeric] theoretical best criterion value \cr
#' }
#**************************************************************************************************
Calibration_optim <- function(InputsModel,RunOptions,InputsCrit,CalibOptions,FUN_MOD,FUN_CRIT,FUN_TRANSFO=NULL,quiet=FALSE){
##_check_class
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("InputsModel must be of class 'InputsModel' \n"); return(NULL); }
if(inherits(RunOptions,"RunOptions")==FALSE){ stop("RunOptions must be of class 'RunOptions' \n"); return(NULL); }
if(inherits(InputsCrit,"InputsCrit")==FALSE){ stop("InputsCrit must be of class 'InputsCrit' \n"); return(NULL); }
if(inherits(CalibOptions,"CalibOptions")==FALSE){ stop("CalibOptions must be of class 'CalibOptions' \n"); return(NULL); }
if(inherits(CalibOptions,"optim")==FALSE){ stop("CalibOptions must be of class 'optim' if Calibration_optim is used \n"); return(NULL); }
##_check_FUN_TRANSFO
if(is.null(FUN_TRANSFO)){
if(identical(FUN_MOD,RunModel_GR4J )){ FUN_TRANSFO <- TransfoParam_GR4J ; }
if(identical(FUN_MOD,RunModel_GR5J )){ FUN_TRANSFO <- TransfoParam_GR5J ; }
if(identical(FUN_MOD,RunModel_GR6J )){ FUN_TRANSFO <- TransfoParam_GR6J ; }
if(identical(FUN_MOD,RunModel_CemaNeige )){ FUN_TRANSFO <- TransfoParam_CemaNeige; }
if(identical(FUN_MOD,RunModel_CemaNeigeGR4J) | identical(FUN_MOD,RunModel_CemaNeigeGR5J) | identical(FUN_MOD,RunModel_CemaNeigeGR6J)){
if(identical(FUN_MOD,RunModel_CemaNeigeGR4J)){ FUN1 <- TransfoParam_GR4J; FUN2 <- TransfoParam_CemaNeige; }
if(identical(FUN_MOD,RunModel_CemaNeigeGR5J)){ FUN1 <- TransfoParam_GR5J; FUN2 <- TransfoParam_CemaNeige; }
if(identical(FUN_MOD,RunModel_CemaNeigeGR6J)){ FUN1 <- TransfoParam_GR6J; FUN2 <- TransfoParam_CemaNeige; }
FUN_TRANSFO <- function(ParamIn,Direction){
Bool <- is.matrix(ParamIn);
if(Bool==FALSE){ ParamIn <- rbind(ParamIn); }
ParamOut <- NA*ParamIn;
NParam <- ncol(ParamIn);
ParamOut[, 1:(NParam-2)] <- FUN1(ParamIn[, 1:(NParam-2)],Direction);
ParamOut[,(NParam-1):NParam ] <- FUN2(ParamIn[,(NParam-1):NParam ],Direction);
if(Bool==FALSE){ ParamOut <- ParamOut[1,]; }
return(ParamOut);
}
}
if(is.null(FUN_TRANSFO)){ stop("FUN_TRANSFO was not found (in Calibration function) \n"); return(NULL); }
}
##_RunModelAndCrit
RunModelAndCrit <- function(par,InputsModel,RunOptions,InputsCrit,CalibOptions,FUN_MOD,FUN_CRIT,FUN_TRANSFO){
ParamT <- NA*CalibOptions$FixedParam;
ParamT[CalibOptions$OptimParam] <- par;
Param <- FUN_TRANSFO(ParamIn=ParamT,Direction="TR");
Param[!CalibOptions$OptimParam] <- CalibOptions$FixedParam[!CalibOptions$OptimParam];
OutputsModel <- FUN_MOD(InputsModel=InputsModel,RunOptions=RunOptions,Param=Param);
OutputsCrit <- FUN_CRIT(InputsCrit=InputsCrit,OutputsModel=OutputsModel);
return(OutputsCrit$CritValue*OutputsCrit$Multiplier);
}
##_temporary_change_of_Outputs_Sim
RunOptions$Outputs_Sim <- RunOptions$Outputs_Cal; ### this reduces the size of the matrix exchange with fortran and therefore speeds the calibration
##_screenPrint
if(!quiet){
cat(paste("\t Calibration in progress (function optim from the stats package) \n",sep=""));
}
##_lower_and_upper_limit_values (transformed)
RangesR <- CalibOptions$SearchRanges;
RangesT <- FUN_TRANSFO(RangesR,"RT");
lower <- RangesT[1,CalibOptions$OptimParam];
upper <- RangesT[2,CalibOptions$OptimParam];
##_starting_values (transformed)
ParamStartT <- FUN_TRANSFO(CalibOptions$StartParam,"RT");
par_start <- ParamStartT[CalibOptions$OptimParam];
##_calibration
RESULT <- optim(par=par_start,fn=RunModelAndCrit,gr=NULL,
InputsModel,RunOptions,InputsCrit,CalibOptions,FUN_MOD,FUN_CRIT,FUN_TRANSFO, ## arguments for the RunModelAndCrit function (other than par)
method="L-BFGS-B",lower=lower,upper=upper,control=list(),hessian=FALSE)
##_outputs_preparation
ParamFinalT <- NA*ParamStartT;
ParamFinalT[CalibOptions$OptimParam] <- RESULT$par;
ParamFinalR <- FUN_TRANSFO(ParamFinalT,"TR");
ParamFinalR[!CalibOptions$OptimParam] <- CalibOptions$FixedParam[!CalibOptions$OptimParam];
CritFinal <- RESULT$value;
##_storage_of_crit_info
OutputsModel <- FUN_MOD(InputsModel=InputsModel,RunOptions=RunOptions,Param=ParamFinalR);
OutputsCrit <- FUN_CRIT(InputsCrit=InputsCrit,OutputsModel=OutputsModel);
CritName <- OutputsCrit$CritName;
CritBestValue <- OutputsCrit$CritBestValue;
Multiplier <- OutputsCrit$Multiplier;
##_screenPrint
if(!quiet){
if(RESULT$convergence==0){
cat(paste("\t Calibration completed: \n",sep=""));
cat(paste("\t Param = ",paste(formatC(ParamFinalR,format="f",width=8,digits=3),collapse=" , "),"\n",sep=""));
cat(paste("\t Crit ",format(CritName,width=12,justify="left")," = ",formatC(CritFinal*Multiplier,format="f",digits=4),"\n",sep=""));
} else {
cat(paste("\t Calibration failed: \n",sep=""));
cat(paste("\t ",RESULT$message,sep=""));
}
}
##_function_output
OutputsCalib <- list(as.double(ParamFinalR),CritFinal*Multiplier,as.integer(RESULT$counts[1]),CritName,CritBestValue);
names(OutputsCalib) <- c("ParamFinalR","CritFinal","NRuns","CritName","CritBestValue");
class(OutputsCalib) <- c("OutputsCalib","optim");
return(OutputsCalib);
}
#*************************************************************************************************
#' Creation of the CalibOptions object required to the Calibration functions.
#'
#' Users wanting to use FUN_MOD, FUN_CALIB or FUN_TRANSFO functions that are not included in
#' the package must create their own CalibOptions object accordingly.
#*************************************************************************************************
#' @title Creation of the CalibOptions object required to the Calibration functions
#' @author Laurent Coron (June 2014)
#' @seealso \code{\link{RunModel}}, \code{\link{CreateInputsModel}}, \code{\link{CreateRunOptions}}, \code{\link{CreateInputsCrit}}
#' @example tests/example_Calibration.R
#' @encoding UTF-8
#' @export
#_FunctionInputs__________________________________________________________________________________
#' @param FUN_MOD [function] hydrological model function (e.g. RunModel_GR4J, RunModel_CemaNeigeGR4J)
#' @param FUN_CALIB (optional) [function] calibration algorithm function (e.g. Calibration_HBAN, Calibration_optim), default=Calibration_HBAN
#' @param FUN_TRANSFO (optional) [function] model parameters transformation function, if the FUN_MOD used is native in the package FUN_TRANSFO is automatically defined
#' @param OptimParam (optional) [boolean] vector of booleans indicating which parameters must be optimised (NParam columns, 1 line)
#' @param FixedParam (optional) [numeric] vector giving the values to allocate to non-optimised parameter values (NParam columns, 1 line)
#' @param SearchRanges (optional) [numeric] matrix giving the ranges of real parameters (NParam columns, 2 lines)
#' \tabular{llllll}{
#' \tab [X1] \tab [X2] \tab [X3] \tab [...] \tab [Xi] \cr
#' [1,] \tab 0 \tab -1 \tab 0 \tab ... \tab 0.0 \cr
#' [2,] \tab 3000 \tab +1 \tab 100 \tab ... \tab 3.0 \cr
#' }
#' @param StartParam (optional) [numeric] vector of parameter values used to start global search calibration procedure (e.g. Calibration_optim)
#' \tabular{llllll}{
#' \tab [X1] \tab [X2] \tab [X3] \tab [...] \tab [Xi] \cr
#' \tab 1000 \tab -0.5 \tab 22 \tab ... \tab 1.1 \cr
#' }
#' @param StartParamList (optional) [numeric] matrix of parameter sets used for grid-screening calibration procedure (values in columns, sets in line)
#' \tabular{llllll}{
#' \tab [X1] \tab [X2] \tab [X3] \tab [...] \tab [Xi] \cr
#' [set1] \tab 800 \tab -0.7 \tab 25 \tab ... \tab 1.0 \cr
#' [set2] \tab 1000 \tab -0.5 \tab 22 \tab ... \tab 1.1 \cr
#' [...] \tab ... \tab ... \tab ... \tab ... \tab ... \cr
#' [set n] \tab 200 \tab -0.3 \tab 17 \tab ... \tab 1.0 \cr
#' }
#' @param StartParamDistrib (optional) [numeric] matrix of parameter values used for grid-screening calibration procedure (values in columns, percentiles in line) \cr
#' \tabular{llllll}{
#' \tab [X1] \tab [X2] \tab [X3] \tab [...] \tab [Xi] \cr
#' [value1] \tab 800 \tab -0.7 \tab 25 \tab ... \tab 1.0 \cr
#' [value2] \tab 1000 \tab NA \tab 50 \tab ... \tab 1.2 \cr
#' [value3] \tab 1200 \tab NA \tab NA \tab ... \tab 1.6 \cr
#' }
#_FunctionOutputs_________________________________________________________________________________
#' @return [list] object of class \emph{CalibOptions} containing the data required to evaluate the model outputs; it can include the following:
#' \tabular{ll}{
#' \emph{$OptimParam } \tab [boolean] vector of booleans indicating which parameters must be optimised \cr
#' \emph{$FixedParam } \tab [numeric] vector giving the values to allocate to non-optimised parameter values \cr
#' \emph{$SearchRanges } \tab [numeric] matrix giving the ranges of real parameters \cr
#' \emph{$StartParam } \tab [numeric] vector of parameter values used to start global search calibration procedure \cr
#' \emph{$StartParamList } \tab [numeric] matrix of parameter sets used for grid-screening calibration procedure \cr
#' \emph{$StartParamDistrib} \tab [numeric] matrix of parameter values used for grid-screening calibration procedure \cr
#' }
#**************************************************************************************************
CreateCalibOptions <- function(FUN_MOD,FUN_CALIB=Calibration_HBAN,FUN_TRANSFO=NULL,OptimParam=NULL,FixedParam=NULL,SearchRanges=NULL,
StartParam=NULL,StartParamList=NULL,StartParamDistrib=NULL){
ObjectClass <- NULL;
##check_FUN_MOD
BOOL <- FALSE;
if(identical(FUN_MOD,RunModel_GR4J )){ ObjectClass <- c(ObjectClass,"GR4J" ); BOOL <- TRUE; }
if(identical(FUN_MOD,RunModel_GR5J )){ ObjectClass <- c(ObjectClass,"GR5J" ); BOOL <- TRUE; }
if(identical(FUN_MOD,RunModel_GR6J )){ ObjectClass <- c(ObjectClass,"GR6J" ); BOOL <- TRUE; }
if(identical(FUN_MOD,RunModel_CemaNeige )){ ObjectClass <- c(ObjectClass,"CemaNeige" ); BOOL <- TRUE; }
if(identical(FUN_MOD,RunModel_CemaNeigeGR4J)){ ObjectClass <- c(ObjectClass,"CemaNeigeGR4J"); BOOL <- TRUE; }
if(identical(FUN_MOD,RunModel_CemaNeigeGR5J)){ ObjectClass <- c(ObjectClass,"CemaNeigeGR5J"); BOOL <- TRUE; }
if(identical(FUN_MOD,RunModel_CemaNeigeGR6J)){ ObjectClass <- c(ObjectClass,"CemaNeigeGR6J"); BOOL <- TRUE; }
if(!BOOL){ stop("incorrect FUN_MOD for use in CreateCalibOptions \n"); return(NULL); }
##check_FUN_CALIB
BOOL <- FALSE;
if(identical(FUN_CALIB,Calibration_HBAN )){ ObjectClass <- c(ObjectClass,"HBAN" ); BOOL <- TRUE; }
if(identical(FUN_CALIB,Calibration_optim)){ ObjectClass <- c(ObjectClass,"optim"); BOOL <- TRUE; }
if(!BOOL){ stop("incorrect FUN_CALIB for use in CreateCalibOptions \n"); return(NULL); }
##check_FUN_TRANSFO
if(is.null(FUN_TRANSFO)){
##_set_FUN1
if(identical(FUN_MOD,RunModel_GR4J ) | identical(FUN_MOD,RunModel_CemaNeigeGR4J) ){ FUN1 <- TransfoParam_GR4J ; }
if(identical(FUN_MOD,RunModel_GR5J ) | identical(FUN_MOD,RunModel_CemaNeigeGR5J) ){ FUN1 <- TransfoParam_GR5J ; }
if(identical(FUN_MOD,RunModel_GR6J ) | identical(FUN_MOD,RunModel_CemaNeigeGR6J) ){ FUN1 <- TransfoParam_GR6J ; }
if(identical(FUN_MOD,RunModel_CemaNeige) ){ FUN1 <- TransfoParam_CemaNeige; }
if(is.null(FUN1)){ stop("FUN1 was not found \n"); return(NULL); }
##_set_FUN2
FUN2 <- TransfoParam_CemaNeige;
##_set_FUN_TRANSFO
if(identical(FUN_MOD,RunModel_GR4J) | identical(FUN_MOD,RunModel_GR5J) | identical(FUN_MOD,RunModel_GR6J) | identical(FUN_MOD,RunModel_CemaNeige)){
FUN_TRANSFO <- FUN1;
} else {
FUN_TRANSFO <- function(ParamIn,Direction){
Bool <- is.matrix(ParamIn);
if(Bool==FALSE){ ParamIn <- rbind(ParamIn); }
ParamOut <- NA*ParamIn;
NParam <- ncol(ParamIn);
if(NParam <= 3){
ParamOut[, 1:(NParam-2)] <- FUN1(cbind(ParamIn[,1:(NParam-2)]),Direction);
} else {
ParamOut[, 1:(NParam-2)] <- FUN1(ParamIn[,1:(NParam-2) ],Direction); }
ParamOut[,(NParam-1):NParam ] <- FUN2(ParamIn[,(NParam-1):NParam],Direction);
if(Bool==FALSE){ ParamOut <- ParamOut[1,]; }
return(ParamOut);
}
}
}
if(is.null(FUN_TRANSFO)){ stop("FUN_TRANSFO was not found \n"); return(NULL); }
##NParam
if("GR4J" %in% ObjectClass){ NParam <- 4; }
if("GR5J" %in% ObjectClass){ NParam <- 5; }
if("GR6J" %in% ObjectClass){ NParam <- 6; }
if("CemaNeige" %in% ObjectClass){ NParam <- 2; }
if("CemaNeigeGR4J" %in% ObjectClass){ NParam <- 6; }
if("CemaNeigeGR5J" %in% ObjectClass){ NParam <- 7; }
if("CemaNeigeGR6J" %in% ObjectClass){ NParam <- 8; }