Xinxin Yang's avatar
Xinxin Yang committed
#This file contains functions related to managing raw_str_map files





#' Merges two raw_str_map files and returns either a list or a file
#'
#' All entries in the new.raw_str_map file replace those on the source.raw_str_map file
#'
#' Both files must be relative to the current data.dir/raw_str_maps
#'
#' @param source.raw_str_map.file the filename of the source raw_str_map. It must be relative the raw_str_maps of the current data.dir
#' @param new.raw_str_map.file the filename of the mask raw_str_map. It will replace any entries of the source file. It must be relative the raw_str_maps of the current data.dir
#' @param return.file If set to T, a temporary full file path that contains the merge is returned. Otherwise a list with the contents of the merge is returned
#'
#' @return FALSE in case of problem / if return.file=T, the temporary full path of a file that contains the merged result in json  / A list with the contents of the merge if return.file=F
#' @export
#'
#' @examples
raw_str_map.merge = function(source.raw_str_map.file=NULL,
                             new.raw_str_map.file=NULL,
                             return.file=F){

  #Check that data.dir exist and raw_str_map files exist ----

  ## check for fadnUtils.data.dir
  if(is.null(get.data.dir())) {
    warning("You have first to set the fadnUtils.data.dir using set.data.dir function. Exiting ....")
    return(invisible(FALSE))
  }

  ##if data.dir is a proper dat.dir
  if(check.data.dir.structure()) {
    data.dir = get.data.dir();
    rds.dir = paste0(data.dir,"/rds/")
    raw_str_map.file = paste0(data.dir,'/raw_str_map.json')
  }

  ##check if raw
  source.raw_str_map.path = paste0(data.dir,"/raw_str_maps/",source.raw_str_map.file)
  new.raw_str_map.path = paste0(data.dir,"/raw_str_maps/",new.raw_str_map.file)

  if(!file.exists(source.raw_str_map.path) | !file.exists(new.raw_str_map.path)) {
    warning("One or more of the raw_str_maps provided, do not exist. Exiting ....")
    return(FALSE)
  }


  #read json files to a list ----
  library("jsonlite")

  source.raw_str_map = fromJSON(paste(readLines(source.raw_str_map.path), collapse="\n"))
  new.raw_str_map = fromJSON(paste(readLines(new.raw_str_map.path), collapse="\n"))


  #merge both files----
  merged.raw_str_map = modifyList(source.raw_str_map,new.raw_str_map)


  #save or export results----
  if(return.file) {
    #save to temporary file as JSON
    f.tmp =  tempfile(pattern = "file", tmpdir = tempdir())
    cat(
      toJSON(merged.raw_str_map, auto_unbox = T),
      "\n",
      file=f.tmp
    )

    return(f.tmp)

  }
  else {
    return(merged.raw_str_map)
  }


}




#' Takes $id, $info, $costs objects of a raw_str_map object and create Source-Description pairs
#'
#' Used internally
#'
#' @param listcontent
#'
#' @return list(COLUMN-NAME = c(SOURCE=csv column name, DESCRIPTION=description of column), ..... )
#'
#' @examples
take.raw_str_map.columns = function( listcontent ) {

  str.cols = names(listcontent)

  splitcol = function(x) {
    if(is.list(x)) {
      return(c("SOURCE"=x$source, "DESCRIPTION"=x$description))
    } else {
      return(c("SOURCE"=x, "DESCRIPTION"=""))}
  }

  return(
    lapply(listcontent,splitcol)
  )

}





#' Checks if the definitions of a raw_str_map are compatible with a fadn.raw.rds for a certain year and country
#'
#' Checks if all values are actual columns of the fadn.raw.rds file
#'
#' @param fadn.country
#' @param fadn.year
#' @param raw_str_map.file The full filepath of the raw_str_map
#'
#' @return
#' @export
#'
#' @examples
check.raw_str_map = function(raw_str_map.file,
                             fadn.country=NA,
                             fadn.year=NA) {

  #TODO
  #check if raw_str_map exist

  #TODO
  #check if fadn.raw.rds exist


  #read file
  raw_str_map.file = paste0(data.dir,"/raw_str_maps/",raw_str_map.file)
  raw_str_map = fromJSON(paste(readLines(raw_str_map.file), collapse="\n"))


  #get all keys
  map.keys.orig = c( unlist(raw_str_map$id),
                     unlist(raw_str_map$info),
                     unlist(raw_str_map$crops,recursive = T)
  )

  #split all characters
  map.keys.orig.split = unlist(
    strsplit(map.keys.orig,
             "\\+|\\-|,|ifelse|\\(|\\)|\\>|\\/|\\<"),
    recursive = T
  )

  #remove whitespaces and empty
  map.keys.orig.split=trimws(map.keys.orig.split)
  map.keys.orig.split=map.keys.orig.split[!map.keys.orig.split==""]

  #get the column listing of fadn.raw.rds
  data.dir = get.data.dir();
  rds.dir = paste0(data.dir,"/rds/")
  data.name  = paste0("fadn.raw.",fadn.year,".",fadn.country,".rds")

  rds.data = readRDS(paste0(rds.dir,data.name))
  rds.data.columns = colnames(rds.data)


  #print what is in raw_str_map but not in fadn.raw.rds
  cat(paste0("What is in raw_str_map but not in ",data.name,"\n"))
  map.keys.orig.split[!map.keys.orig.split%in%rds.data.columns]


}