Modify many
This commit is contained in:
parent
4ab2b60f49
commit
0d9d551fd9
@ -1,6 +1,6 @@
|
||||
Package: RBBGCMuso
|
||||
Title: An R package for BiomeBGC-MuSo ecosystem modelling
|
||||
Version: 0.7.0.0
|
||||
Version: 0.7.0.1
|
||||
Authors@R: person("Roland", "Hollo's", , "hollorol@gmail.com", role = c("aut", "cre"))
|
||||
Description: What the package does (one paragraph).
|
||||
Depends: R (>= 3.3.2)
|
||||
|
||||
@ -23,7 +23,7 @@ export(paramSweep)
|
||||
export(plotMuso)
|
||||
export(plotMusoWithData)
|
||||
export(randEpc)
|
||||
export(readMeasuredMuso)
|
||||
export(readObservedData)
|
||||
export(runMuso)
|
||||
export(rungetMuso)
|
||||
export(saveAllMusoPlots)
|
||||
|
||||
@ -146,7 +146,7 @@ readValuesFromFile <- function(epc, linums){
|
||||
#' @importFrom data.table fread data.table
|
||||
#' @export
|
||||
|
||||
readMeasuredMuso <- function(inFile,
|
||||
readObservedData <- function(inFile,
|
||||
naString = NULL, sep = ",",
|
||||
leapYearHandling = TRUE,
|
||||
convert.var = NULL,
|
||||
@ -181,8 +181,7 @@ readMeasuredMuso <- function(inFile,
|
||||
}
|
||||
head(baseData)
|
||||
if(!is.null(selVar)){
|
||||
baseData <- cbind.data.frame(baseData,convert.fun(baseData[,selVar]))
|
||||
colnames(baseData)[ncol(baseData)]<- paste0("M",selVar)
|
||||
baseData[,selVar] <-convert.fun(baseData[,selVar])
|
||||
}
|
||||
|
||||
return(data.table(baseData))
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
#' @export
|
||||
optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
endDate, formatString = "%Y-%m-%d",
|
||||
leapYear = TRUE,
|
||||
leapYearHandling = TRUE,
|
||||
dataVar, outLoc = "./calib",
|
||||
preTag = "cal-",
|
||||
settings = NULL,
|
||||
@ -40,7 +40,8 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
likelihood = function(x, y){
|
||||
exp(-sqrt(mean((x-y)^2)))
|
||||
},
|
||||
calPar = 3009)
|
||||
continious,
|
||||
modelVar = 3009)
|
||||
{
|
||||
dataCol <- grep(dataVar, colnames(measuredData))
|
||||
|
||||
@ -76,6 +77,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
|
||||
##reading the original epc file at the specified
|
||||
## row numbers
|
||||
print("optiMuso is randomizing the epc parameters now...",quote = FALSE)
|
||||
if(iterations < 3000){
|
||||
randVals <- musoRand(parameters = parameters,constrains = constrains, iterations = 3000)
|
||||
randVals[[2]]<- randVals[[2]][sample(1:3000,iterations),]
|
||||
@ -94,18 +96,22 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
## csv files for each run
|
||||
|
||||
progBar <- txtProgressBar(1,iterations,style=3)
|
||||
colNumb <- which(settings$dailyVarCodes == calPar)
|
||||
colNumb <- which(settings$dailyVarCodes == modelVar)
|
||||
settings$iniInput[2] %>%
|
||||
(function(x) paste0(dirname(x),"/",tools::file_path_sans_ext(basename(x)),"-tmp.",tools::file_ext(x))) %>%
|
||||
unlink
|
||||
randValues <- randVals[[2]]
|
||||
settings$calibrationPar <- randVals[[1]]
|
||||
list2env(alignData(measuredData,dataCol = dataCol,modellSettings = settings,startDate = startDate,endDate = endDate,leapYear = FALSE),envir=environment())
|
||||
list2env(alignData(measuredData,dataCol = dataCol,modellSettings = settings,startDate = startDate,endDate = endDate,leapYear = leapYearHandling, continious = continious),envir=environment())
|
||||
|
||||
modellOut <- numeric(iterations + 1) # single variable solution
|
||||
origModellOut <- calibMuso(settings=settings,silent=TRUE)
|
||||
rmse <- numeric(iterations + 1)
|
||||
origModellOut <- calibMuso(settings=settings,silent=TRUE, skipSpinup = skipSpinup)
|
||||
|
||||
|
||||
write.csv(x=origModellOut, file=paste0(pretag,1,".csv"))
|
||||
modellOut[1] <- likelihood(measuredData,origModellOut[modIndex,colNumb])
|
||||
print("Running the model with the random epc values...", quote = FALSE)
|
||||
for(i in 2:(iterations+1)){
|
||||
tmp <- tryCatch(calibMuso(settings = settings,
|
||||
parameters = randValues[(i-1),],
|
||||
@ -113,10 +119,10 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
skipSpinup = skipSpinup)[modIndex,colNumb], error = function (e) NA)
|
||||
|
||||
modellOut[i]<- likelihood(measuredData,tmp)
|
||||
rmse[i] <- sqrt(mean((measuredData-tmp)^2))
|
||||
write.csv(x=tmp, file=paste0(pretag,(i+1),".csv"))
|
||||
setTxtProgressBar(progBar,i)
|
||||
}
|
||||
modellOut
|
||||
paramLines <- parameters[,2]
|
||||
paramLines <- order(paramLines)
|
||||
randInd <- randVals[[1]][(randVals[[1]] %in% parameters[,2])]
|
||||
@ -128,7 +134,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
randValues[,randVals[[1]] %in% parameters[,2]][,randInd])
|
||||
|
||||
|
||||
preservedCalib <- cbind(epcStrip,
|
||||
preservedCalib <- cbind(epcStrip,rmsr,
|
||||
modellOut)
|
||||
colnames(preservedCalib) <- c(parameterNames[paramLines], "likelihood")
|
||||
p<-list()
|
||||
@ -137,7 +143,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
p[[i]] <- ggplot(as.data.frame(preservedCalib),aes_string(colnames(preservedCalib)[i],"likelihood"))+geom_point(size=0.9)
|
||||
}
|
||||
|
||||
ggsave(plotName,grid.arrange(grobs = p, ncol = floor(sqrt(ncol(preservedCalib)-1))),dpi = 600)
|
||||
ggsave(plotName,grid.arrange(grobs = p, ncol = floor(sqrt(ncol(preservedCalib)-1))),dpi = 3000)
|
||||
write.csv(preservedCalib,"preservedCalib.csv")
|
||||
return(preservedCalib[preservedCalib[,"likelihood"]==max(preservedCalib[,"likelihood"]),])
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
#' @importFrom lubridate leap_year
|
||||
#' @export
|
||||
|
||||
|
||||
musoDate <- function(startYear, endYears = NULL, numYears, combined = TRUE, leapYearHandling = FALSE, prettyOut = FALSE){
|
||||
|
||||
if(is.null(endYears) & is.null(numYears)){
|
||||
@ -60,7 +59,8 @@ musoDate <- function(startYear, endYears = NULL, numYears, combined = TRUE, leap
|
||||
#' This function align the data to the model and the model to the data
|
||||
#' @importFrom lubridate leap_year
|
||||
#' @keywords internal
|
||||
alignData <- function(mdata, dataCol, modellSettings = NULL, startDate, endDate, formatString = "%Y-%m-%d", leapYear = TRUE){
|
||||
alignData <- function(mdata, dataCol, modellSettings = NULL, startDate=NULL, endDate=NULL, formatString = "%Y-%m-%d", leapYear = TRUE, continious = TRUE){
|
||||
|
||||
|
||||
startDate <- as.Date(startDate, format = formatString)
|
||||
endDate <- as.Date(endDate, format = formatString)
|
||||
@ -70,15 +70,24 @@ alignData <- function(mdata, dataCol, modellSettings = NULL, startDate, endDate
|
||||
modellSettings <- setupMuso()
|
||||
}
|
||||
|
||||
if(continious){
|
||||
dates <- seq(startDate, to = endDate, by= "day")
|
||||
if(!leapYear){
|
||||
dates <- dates[which(format(dates,"%m%d") != "0229")]
|
||||
} else{
|
||||
dates <- do.call(c,lapply(seq(nrow(mdata)), function(i){ as.Date(paste0(mdata[i,1],sprintf("%02d",mdata[i,2]),mdata[i,3]),format = "%Y%m%d")}))
|
||||
}
|
||||
|
||||
if(!leapYear){
|
||||
casualDays <- which(format(dates,"%m%d") != "0229")
|
||||
#mdata <- mdata[casualDays,]
|
||||
dates <- dates[casualDays]
|
||||
}
|
||||
|
||||
mdata <- mdata[dates >= as.Date(paste0(modellSettings$startYear,"01","01"),format = "%Y%m%d"),]
|
||||
dates <- dates[dates >= as.Date(paste0(modellSettings$startYear,"01","01"),format = "%Y%m%d")]
|
||||
goodInd <- which(!(leap_year(dates)&
|
||||
(format(dates,"%m") == "12")&
|
||||
(format(dates,"%d") == "31")))
|
||||
## goodInd <- which(!(leap_year(dates)&
|
||||
## (format(dates,"%m") == "12")&
|
||||
## (format(dates,"%d") == "31")))
|
||||
|
||||
if(leapYear){
|
||||
goodInd <- which(!(leap_year(dates)&
|
||||
(format(dates,"%m") == "12")&
|
||||
|
||||
@ -251,7 +251,7 @@ plotMuso <- function(settings = NULL, variable = 1,
|
||||
#' @export
|
||||
plotMusoWithData <- function(mdata, plotName=NULL,
|
||||
startDate, endDate,
|
||||
colour=c("black","blue"),dataVar, modelVar, settings = setupMuso(), silent = TRUE){
|
||||
colour=c("black","blue"),dataVar, modelVar, settings = setupMuso(), silent = TRUE, continious = TRUE){
|
||||
|
||||
dataCol<- grep(paste0("^",dataVar,"$"), colnames(mdata))
|
||||
selVar <- grep(modelVar,(settings$dailyVarCodes))+4
|
||||
@ -259,14 +259,14 @@ plotMusoWithData <- function(mdata, plotName=NULL,
|
||||
list2env(alignData(mdata, dataCol = dataCol,
|
||||
modellSettings = settings,
|
||||
startDate = startDate,
|
||||
endDate = endDate, leapYear = FALSE),envir=environment())
|
||||
endDate = endDate, leapYear = FALSE, continious = continious),envir=environment())
|
||||
|
||||
|
||||
## measuredData is created
|
||||
baseData <- calibMuso(settings = settings, silent = silent, prettyOut = TRUE)[modIndex,]
|
||||
baseData[,1] <- as.Date(baseData[,1],format = "%d.%m.%Y")
|
||||
selVarName <- colnames(baseData)[selVar]
|
||||
if(colnames(baseData) != unique(colnames(baseData))){
|
||||
if(!all.equal(colnames(baseData),unique(colnames(baseData)))){
|
||||
notUnique <- setdiff((unlist(settings$dailyVarCodes)),unique(unlist(settings$dailyVarCodes)))
|
||||
stop(paste0("Error: daily output variable list in the ini file must contain unique numbers. Check your ini files! Not unique codes: ",notUnique))
|
||||
}
|
||||
|
||||
@ -4,8 +4,9 @@
|
||||
\alias{alignData}
|
||||
\title{alignData}
|
||||
\usage{
|
||||
alignData(mdata, dataCol, modellSettings = NULL, startDate, endDate,
|
||||
formatString = "\%Y-\%m-\%d", leapYear = TRUE)
|
||||
alignData(mdata, dataCol, modellSettings = NULL, startDate = NULL,
|
||||
endDate = NULL, formatString = "\%Y-\%m-\%d", leapYear = TRUE,
|
||||
continious = TRUE)
|
||||
}
|
||||
\description{
|
||||
This function align the data to the model and the model to the data
|
||||
|
||||
@ -4,22 +4,16 @@
|
||||
\alias{optiMuso}
|
||||
\title{optiMuso}
|
||||
\usage{
|
||||
optiMuso(measuredDataFile, parameters = NULL, sep = ",", startDate,
|
||||
endDate, formatString = "\%Y-\%m-\%d",
|
||||
naString = getOption("datatable.na.strings", "NA"), leapYear = TRUE,
|
||||
filterCol = NULL, filterVal = 1, selVar, outLoc = "./calib",
|
||||
preTag = "cal-", settings = NULL, outVars = NULL,
|
||||
iterations = 30, skipSpinup = TRUE, constrains = NULL,
|
||||
plotName = "calib.jpg", likelihood = function(x, y) {
|
||||
exp(-sqrt(mean((x - y)^2))) }, calPar = 3009)
|
||||
optiMuso(measuredData, parameters = NULL, startDate, endDate,
|
||||
formatString = "\%Y-\%m-\%d", leapYear = TRUE, dataVar,
|
||||
outLoc = "./calib", preTag = "cal-", settings = NULL,
|
||||
outVars = NULL, iterations = 30, skipSpinup = TRUE,
|
||||
constrains = NULL, plotName = "calib.jpg", likelihood = function(x,
|
||||
y) { exp(-sqrt(mean((x - y)^2))) }, calPar = 3009)
|
||||
}
|
||||
\arguments{
|
||||
\item{measuredDataFile}{a}
|
||||
|
||||
\item{parameters}{b}
|
||||
|
||||
\item{sep}{c}
|
||||
|
||||
\item{startDate}{d}
|
||||
|
||||
\item{endDate}{e}
|
||||
@ -28,12 +22,6 @@ optiMuso(measuredDataFile, parameters = NULL, sep = ",", startDate,
|
||||
|
||||
\item{leapYear}{b}
|
||||
|
||||
\item{filterCol}{a}
|
||||
|
||||
\item{filterVal}{b}
|
||||
|
||||
\item{selVar}{c}
|
||||
|
||||
\item{outLoc}{c}
|
||||
|
||||
\item{settings}{e}
|
||||
@ -50,6 +38,16 @@ optiMuso(measuredDataFile, parameters = NULL, sep = ",", startDate,
|
||||
|
||||
\item{calPar}{a}
|
||||
|
||||
\item{measuredDataFile}{a}
|
||||
|
||||
\item{sep}{c}
|
||||
|
||||
\item{filterCol}{a}
|
||||
|
||||
\item{filterVal}{b}
|
||||
|
||||
\item{selVar}{c}
|
||||
|
||||
\item{pretag}{a}
|
||||
}
|
||||
\description{
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/assistantFunctions.R
|
||||
\name{readMeasuredMuso}
|
||||
\alias{readMeasuredMuso}
|
||||
\title{readMeasuredMuso}
|
||||
\usage{
|
||||
readMeasuredMuso(inFile, naString = getOption("datatable.na.strings",
|
||||
"NA"), sep = ",", leapYearHandling = TRUE, convert.var = NULL,
|
||||
convert.scalar = 1, convert.fun = (function(x) { x *
|
||||
convert.scalar }), convert.file = NULL, filterCol = NULL,
|
||||
filterVal = 1, selVar = NULL)
|
||||
}
|
||||
\description{
|
||||
MuSo data reader
|
||||
}
|
||||
15
RBBGCMuso/man/readObservedData.Rd
Normal file
15
RBBGCMuso/man/readObservedData.Rd
Normal file
@ -0,0 +1,15 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/assistantFunctions.R
|
||||
\name{readObservedData}
|
||||
\alias{readObservedData}
|
||||
\title{readMeasuredMuso}
|
||||
\usage{
|
||||
readObservedData(inFile, naString = NULL, sep = ",",
|
||||
leapYearHandling = TRUE, convert.var = NULL, convert.scalar = 1,
|
||||
convert.fun = (function(x) { x * convert.scalar }),
|
||||
convert.file = NULL, filterCol = NULL, filterVal = 1,
|
||||
selVar = NULL)
|
||||
}
|
||||
\description{
|
||||
MuSo data reader
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user