#' This function converts the given RDC comma seperated value files into Rdata. It is important to verify before if the national RDC provides the data
#' in the form as desired by this function.
#'
#' This function is written for the German Farm Structure Survey data as it was provided in the year 2018.This means, that the RDC provides to data sets. One is the
#' 'old' data set which has variables in the former declination (EF codes) and contains data at maximum data from 1999, 2003 and 2007. The second data set
#' is declinated in C/C0 codes and has data from 2010, 2013 (only sample), 2016 and 2020 (as of 2021).
#'
#' How much variables the researcher has requested or how many years depends. This function works fine for all years and all variables up to 2016.
#'
#' This function needs of course the data as well as a specific folder structure, at least the RDC data file names and the specific folder names where these files
#' are located and where they should be exported as Rdata files.
#'
#' @description
#' This function converts the given RDC comma seperated value files into Rdata.
#'
#' @param
#' name datafiles.dir.
#' name intermediate.dir
#' name filename.old
#' filename.new
#'
#' @return
#' Nothing returned, but Rdata export.
#'
#'@import data.table
#'
#' @export
#'
#' @examples \dontrun{
#' convertCSVtoRdata_DE(datafiles.dir="D:/data/in/",intermediate.dir="D:/data/temp/",filename.old="Panel_old",filename.new="Panel_new")
#' }
convertCSVtoRdata_DE <- function(datafiles.dir=NULL,intermediate.dir=NULL,filename.old=NULL,filename.new=NULL){

  require(data.table)

  if(any(is.null(c(datafiles.dir,intermediate.dir,filename.old,filename.new)))) stop("Not all parameters provided")

  #-------------------------------------------------------------
  cat("\n# Step 1: Read in RDC data in CSV format\n")
  #-------------------------------------------------------------
  newdata <- fread(input = paste0(datafiles.dir,"\\",filename.new,".csv"),
                              colClasses = list(character=c("AGS","C0010UG1")))
  olddata <- fread(input =  paste0(datafiles.dir,"\\",filename.old,".csv"),
                              colClasses = list(character=c("EF2UG2")))
  #-------------------------------------------------------------
  cat("\n# Step 2: Write RDC data in Rdata format\n")
  #-------------------------------------------------------------
  ## rename objects corresponding to file names
  string_eval <- sprintf("%s <- newdata; %s <- olddata",
                         filename.new,filename.old)
  eval(parse(text = string_eval))

  save(list = c(filename.old,filename.new), file=paste(intermediate.dir,filename.old,"_",filename.new,"_rohdaten.rdata",collapse=NULL, sep=""))

}