R/addMapping.R

Defines functions addMapping

Documented in addMapping

  addMapping<-function(func,...,dataset,ignore.args=NULL,choose.args=NULL,internal=FALSE,svn_path=NULL){

  ###################################################################
  #Define the data identifier for the mapping.
  ###################################################################
  #It looks like the function call
  #except that there are no quotes for string arguments
  #And the values of ignore.args arguments is ##IGNORE##
  #And the values of choose.args arguments is ##CHOOSE##
  #The arguments file, level and gdx are not part of the identifier
  #Example: emissions(types=co2_c,y1995=##IGNORE##)
  ###################################################################
  identifier<-.getIdentifier(func,...,ignore.args=ignore.args,choose.args=choose.args,get=FALSE)

  ###################################################################
  #Download the library if requested
  ###################################################################
  if(!internal){
    dirname<-.loadData(svn_path=svn_path)
  }

  ###################################################################
  #Check whether identifier is already present in the mapping for dataset
  ###################################################################
  get_line <- function(){
    # gets characters (line) from the terminal of from a connection
    # and stores it in the return object
    if(interactive()){
      s <- readline()
    } else {
      con <- file("stdin")
      s <- readLines(con, 1, warn=FALSE)
      on.exit(close(con))
    }
    return(s);
  }
  #Check whether dataset exists already
  if(!dataset %in% names(valdata$data)){
    if(!internal).cleanDownload(libdir=dirname,workdir=thisdir)
    stop("The dataset you want to map to does not exist")
  }
  
  matches<-names(unlist(lapply(mapping,grep,pattern=identifier,fixed=TRUE)))
  
  ###########################################################################
  #If identifier is not present in the mapping yet, 
  #check if there are similar identifiers that only differ by ##IGNORE## ed arguments. 
  ###########################################################################
  if(length(matches)==0){
    #Determine the pattern for potential identifiers
    pot_identifier<-getIdentifier(func,...,get=TRUE)
    pot_matches<-unlist(lapply(mapping,grep,pattern=pot_identifier,value=T))
    if(length(pot_matches)>0){
      cat("\n\nThere is no entry for ",identifier,"in the mapping yet.")
      cat("\n\nThere are however entries for the following related identifiers.")
      cat("\n\nDo you want to use one of those?.\n\n")
      cat(paste(1:length(pot_matches),pot_matches,sep=": "),sep="\n")
      cat(length(pot_matches)+1,": Stick to ",identifier,"\n")
      cat("\nNumber: ")
      tmp <- get_line()
      tmp<-as.numeric(strsplit(tmp,",")[[1]])
      if(tmp %in%1:length(pot_matches)){
        identifier<-pot_matches[tmp]
        matches<-names(unlist(lapply(mapping,grep,pattern=identifier,fixed=TRUE)))
      } 
    }
  }
  
  if(length(matches)==0){
    mapping[[dataset]][[length(mapping[[dataset]])+1]]<-identifier
  } else if(dataset %in%matches){
    setwd(thisdir)
    system(paste("rm -rf",dirname))
    stop("This mapping already exists, no action is performed")    
  } else {
    cat("\n\nThe function ",identifier," is already mapped to the following datasets:\n\n")
    cat(matches,sep="\n")
    cat("\n\nAre you sure that you want to add a mapping to ",dataset,"?\n")
    cat("1: Yes","2: No",sep="\n")
    check<-get_line()
    if(check!=1){
      setwd(thisdir)
      system(paste("rm -rf",dirname))
      stop("User interrupt.")
    }
    mapping[[dataset]][[length(mapping[[dataset]])+1]]<-identifier
  }

  ###################################################################
  #Save the mapping object
  ###################################################################
  save(mapping,file="validation/data/mapping.rda")
  
  ###################################################################
  #Build the library with the updated mapping.rda
  ###################################################################
  buildLibrary("validation",svn_path=svn_path)
  
  ###################################################################
  #commit the updated library
  ###################################################################
  setwd(thisdir)
  if(system(paste('svn commit -m "Added new mapping for',identifier,' to ',dataset,'to validation library"',dirname))!=0){
    setwd(thisdir)
    system(paste("rm -rf",dirname))
    stop("Problems committing the updated library.")
  }
  system(paste("rm -rf",dirname))
  print("Mapping successfully added and library committed")
}
pik-piam/validation documentation built on Nov. 5, 2019, 12:50 a.m.