From 0cf2c1af663937a8e13a9aec3fd5f8b2edf1bbb3 Mon Sep 17 00:00:00 2001 From: hollorol Date: Tue, 15 May 2018 22:39:14 +0200 Subject: [PATCH] making assistant functions for calibMuso --- RBBGCMuso/R/assistantFunctions.R | 62 ++++++++++++++++++++++++++++++++ RBBGCMuso/R/calibMuso.R | 22 ++++-------- RBBGCMuso/R/setupMuso.R | 2 +- RBBGCMuso/man/getLogs.Rd | 20 +++++++++++ RBBGCMuso/man/getOutFiles.Rd | 20 +++++++++++ RBBGCMuso/man/readErrors.Rd | 20 +++++++++++ 6 files changed, 129 insertions(+), 17 deletions(-) create mode 100644 RBBGCMuso/R/assistantFunctions.R create mode 100644 RBBGCMuso/man/getLogs.Rd create mode 100644 RBBGCMuso/man/getOutFiles.Rd create mode 100644 RBBGCMuso/man/readErrors.Rd diff --git a/RBBGCMuso/R/assistantFunctions.R b/RBBGCMuso/R/assistantFunctions.R new file mode 100644 index 0000000..a86ce87 --- /dev/null +++ b/RBBGCMuso/R/assistantFunctions.R @@ -0,0 +1,62 @@ +#' getLogs +#' +#'This function gives us the muso logfiles with their paths +#' +#'@param outputLoc This is the location of the output files. +#'@param outputNames These are the prefixis of the logfiles +#'@return Logfiles with paths +#'@keywords internal + + +getLogs <- function(outputLoc, outputNames, type = "spinup"){ + switch(type, + "spinup" = return(grep(paste0(outputNames[1], ".log"), list.files(outputLoc), value = TRUE)), + "normal" = return(grep(paste0(outputNames[2], ".log"), list.files(outputLoc), value = TRUE)), + "both" = return(grep(paste0(outputNames[2], ".log"), list.files(outputLoc), value = TRUE))) +} + + +#' readErrors +#' +#'This function reads the spinup and the normal logfiles and gives back the last line which indicates weather there are any errors. +#' +#'@param outputLoc This is the location of the output files. +#'@param logfiles These are the names of the logfiles +#'@return vector with 0 and 1 values, 1, if succed, 0 if not. The first is the spinup run, the second is the normal. +#'@keywords internal + + +readErrors <- function(outputLoc, logfiles){ + return(as.numeric(as.vector(lapply(paste(outputLoc,logfiles,sep = "/"),function(x) tail(readLines(x,-1),1)))) ) +} + +#' getOutFiles +#' +#'This function gives us the muso output files with their paths +#' +#'@param outputLoc This is the location of the output files. +#'@param outputNames These are the prefixis of the logfiles +#'@return Output files with their paths +#'@keywords internal + + +getOutFiles <- function(outputLoc, outputNames){ + + return(grep("out$", grep(paste(paste0(outputNames, "*"), collapse = "|"), list.files(outputLoc), value=TRUE), value = TRUE)) +} + +#' getOutFiles +#' +#'This function gives us the muso output files with their paths +#' +#'@param outputLoc This is the location of the output files. +#'@param outputNames These are the prefixis of the logfiles +#'@return Output files with their paths +#'@keywords internal + +stampAndCopyDir <- function(outputLoc,names,stampDir, stampnum=NULL){ + if(!is.null(stampnum)){file.copy(file.path(outputLoc,names) + ,file.path(stampDir,paste0((stampnum+1),"-",possibleNames))) + } else{file.copy(file.path(outputLoc,names) + ,file.path(stampDir,paste0((stamp(stampDir)+1),"-",possibleNames)))} +} diff --git a/RBBGCMuso/R/calibMuso.R b/RBBGCMuso/R/calibMuso.R index fdf1554..c278a7d 100644 --- a/RBBGCMuso/R/calibMuso.R +++ b/RBBGCMuso/R/calibMuso.R @@ -70,14 +70,6 @@ calibMuso <- function(settings,parameters=NULL, timee="d", debugging=FALSE, logf } } -########################################################################## -###########################Defining Functions######################## -######################################################################## - - - - - ############################################################# ############################spinup run############################ ########################################################## @@ -125,8 +117,8 @@ calibMuso <- function(settings,parameters=NULL, timee="d", debugging=FALSE, logf } - - logspinup <- grep(paste0(outputNames[1],".log"), list.files(outputLoc),value = TRUE) + logspinup <- getLogs(outputLoc,outputNames,type="spinup") + ## logspinup <- grep(paste0(outputNames[1],".log"), list.files(outputLoc),value = TRUE) ## logspinup <- list.files(outputLoc)[grep("log$",list.files(outputLoc))]#load the logfiles if(length(logspinup)==0){ @@ -186,16 +178,14 @@ calibMuso <- function(settings,parameters=NULL, timee="d", debugging=FALSE, logf ) if(keepBinary){ - possibleNames <- grep("out$",grep(paste(paste0(outputNames,"*"), collapse = "|") ,list.files(outputLoc),value=TRUE),value = TRUE) - - print(stamp(binaryPlace)) + possibleNames <- getOutFiles(outputLoc = outputLoc,outputNames = outputNames) file.copy(file.path(outputLoc,possibleNames) ,file.path(binaryPlace,paste0((stamp(binaryPlace)+1),"-",possibleNames))) } } - logfiles <- grep(paste(paste0(outputNames,".log"), collapse = "|"), list.files(outputLoc),value = TRUE) + logfiles <- getLogs(outputLoc,outputNames,type="both") ## list.files(outputLoc)[grep("log$",list.files(outputLoc))]#creating a vector for logfilenames ############################################### @@ -204,7 +194,7 @@ calibMuso <- function(settings,parameters=NULL, timee="d", debugging=FALSE, logf - perror<-as.numeric(as.vector(lapply(paste(outputLoc,logfiles,sep="/"),function(x) tail(readLines(x,-1),1)))) #vector of spinup and normalrun error + perror <- readErrors(outputLoc=outputLoc,logfiles=logfiles) #vector of spinup and normalrun error ##if errorsign is 1 there is error, if it is 0 everything ok @@ -226,7 +216,7 @@ calibMuso <- function(settings,parameters=NULL, timee="d", debugging=FALSE, logf if(keepEpc){#if keepepc option turned on if(length(unique(dirname(epc)))>1){ - print("Why are you playing with my nervs? Seriously? You hold your epc-s in different folders?") + stop("Why are you playing with my nervs? Seriously? You hold your epc-s in different folders?") } else { ## epcfiles <- list.files(epcdir)[grep("epc$",list.files( diff --git a/RBBGCMuso/R/setupMuso.R b/RBBGCMuso/R/setupMuso.R index e5e2648..b177367 100644 --- a/RBBGCMuso/R/setupMuso.R +++ b/RBBGCMuso/R/setupMuso.R @@ -214,7 +214,7 @@ setupMuso <- function(executable=NULL, inputFiles<-c(iniInput,epcInput,metInput) numData<-rep(NA,3) - numYears <- as.numeric(unlist(strsplit(grep("simulation years",iniFiles[[2]],value=TRUE),"[\ \t]"))[1])gfrurgc dhxv + numYears <- as.numeric(unlist(strsplit(grep("simulation years",iniFiles[[2]],value=TRUE),"[\ \t]"))[1]) ## numYears<-unlist(read.table(iniInput[2],skip = 14,nrows = 1)[1]) numValues <- as.numeric(unlist(strsplit(grep("number of daily output variables",iniFiles[[2]],value=TRUE),"[\ \t]"))[1]) ## numValues will be replaced to numVar diff --git a/RBBGCMuso/man/getLogs.Rd b/RBBGCMuso/man/getLogs.Rd new file mode 100644 index 0000000..b0a4621 --- /dev/null +++ b/RBBGCMuso/man/getLogs.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assistantFunctions.R +\name{getLogs} +\alias{getLogs} +\title{getLogs} +\usage{ +getLogs(outputLoc, outputNames, type = "spinup") +} +\arguments{ +\item{outputLoc}{This is the location of the output files.} + +\item{outputNames}{These are the prefixis of the logfiles} +} +\value{ +Logfiles with paths +} +\description{ +This function gives us the muso logfiles with their paths +} +\keyword{internal} diff --git a/RBBGCMuso/man/getOutFiles.Rd b/RBBGCMuso/man/getOutFiles.Rd new file mode 100644 index 0000000..437bda0 --- /dev/null +++ b/RBBGCMuso/man/getOutFiles.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assistantFunctions.R +\name{getOutFiles} +\alias{getOutFiles} +\title{getOutFiles} +\usage{ +getOutFiles(outputLoc, outputNames) +} +\arguments{ +\item{outputLoc}{This is the location of the output files.} + +\item{outputNames}{These are the prefixis of the logfiles} +} +\value{ +Output files with their paths +} +\description{ +This function gives us the muso output files with their paths +} +\keyword{internal} diff --git a/RBBGCMuso/man/readErrors.Rd b/RBBGCMuso/man/readErrors.Rd new file mode 100644 index 0000000..7dfbf95 --- /dev/null +++ b/RBBGCMuso/man/readErrors.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assistantFunctions.R +\name{readErrors} +\alias{readErrors} +\title{readErrors} +\usage{ +readErrors(outputLoc, logfiles) +} +\arguments{ +\item{outputLoc}{This is the location of the output files.} + +\item{logfiles}{These are the names of the logfiles} +} +\value{ +vector with 0 and 1 values, 1, if succed, 0 if not. The first is the spinup run, the second is the normal. +} +\description{ +This function reads the spinup and the normal logfiles and gives back the last line which indicates weather there are any errors. +} +\keyword{internal}