R/Site_Maker.R

Defines functions pSIMS_Site_Make

Documented in pSIMS_Site_Make

#' pSIMS_Site_Make
#'
#' @param Project_name character name of the site or the project
#' @param Lat   numeric latitude of the site
#' @param Lon numeric longitude of the site
#' @param Param_template_Obj NULL or a list object made by Read_param_template()
#' @param Campaign_json_Obj NULL or a list object made by Read_Campaign_template()
#' @param Campaign_Path NULL or character path to a nc campaign file
#' @param APSIM_Template_Path NULL or character path to an apsim template file
#' @param host NULL or a list of information needed to rsync simulations to the cluster
#' @param camp_list list of items that needs to be changed in the campaign file
#' @param param_list list of items that needs to be changed in the params file
#' @param Bash_control list of information for creating the bash file for running pSIMS
#' @param dirname character path to where the simulation will be created
#'
#' @return
#' @export
#'
#' @examples
pSIMS_Site_Make <- function(dirname=".",
                            Project_name="NewProject",
                            Lat=40.01,
                            Lon=-88.01,
                            Param_template_Obj=NULL,
                            Campaign_json_Obj=NULL,
                            Campaign_Path=NULL,
                            APSIM_Template_Path=NULL,
                            host=NULL, # You can pass the host with tunnel already built to move the file to your desired dir on the cluster
                            camp_list=list(planting=list(depth=35)),# this is for the json,
                            param_list=list(ref_year="2010"), # this is for the yaml
                            Bash_control=list(pSIMS_Data_Path="/projects/aces/mkivi2/psims/",
                                              pSIMS_server_Path="/projects/aces/hamzed/psims/Data",
                                              pSIMS_Sing_Image="/projects/aces/mkivi2/psims/Bash/apsim_psims_image/custom_psims_full.img")
                            ) {

  # File names
  json_fname <- "exp_template.json"
  yaml_fname <- "params.apsim.sample"
  # Making the dir -------------------------------------------------------------------
  if(!dir.exists(file.path(dirname, Project_name))) dir.create(file.path(dirname, Project_name))
  if(!dir.exists(file.path(dirname, Project_name,"Campaign"))) dir.create(file.path(dirname, Project_name,"Campaign"))
  if(!dir.exists(file.path(dirname, Project_name, "Files"))) dir.create(file.path(dirname, Project_name,"Files"))
  # Find and read file templates
  # read json and yaml----------------------------------------------------------------
  if(is.null(Campaign_json_Obj)){
        tmp_json_path <- system.file("templates", json_fname, package = "pSIMSSiteMaker")
        camp_json <- jsonlite::fromJSON(tmp_json_path)
  }else{
    camp_json <- Campaign_json_Obj
  }

  if(is.null(Param_template_Obj)){
    tmp_yml_path <- system.file("templates", yaml_fname, package = "pSIMSSiteMaker")
    param_yml <- yaml::read_yaml(tmp_yml_path)
  }else{
    param_yml <-Param_template_Obj
  }

  # Modify the files
  camp_json <- edit_list_obj(camp_list, camp_json)
  param_yml <- edit_list_obj(param_list, param_yml)

  # Make the Bash file ---------------------------------------------------------------
  deltas <- strsplit(param_yml$delta, ",") %>% unlist %>% as.numeric()
  latlon_psims_grid <- latlon_latxlonx(latneed = Lat, lonneed = Lon,
                  latdelta = deltas[1], londelta = deltas[2])
  # Edit file
  tmp_bash_path <- system.file("templates", "Singularity.sh", package = "pSIMSSiteMaker")
  tmp_bash <- readLines(tmp_bash_path)
  all_bash_tags <- c(latlon_psims_grid, Bash_control)

  all_bash_tags$params_apsim_sample <- file.path(Bash_control$pSIMS_Data_Path,Project_name,"Files", yaml_fname)
  all_bash_tags$Campaign_Path <- file.path(Bash_control$pSIMS_Data_Path,Project_name, "Campaign")
  all_bash_tags$pysims_Path <- file.path(Bash_control$pSIMS_Data_Path, "pysims/pysims.py")

  for (ntags in names(all_bash_tags)) {
    tmp_bash  <- gsub(pattern = paste0("@",ntags), replace =all_bash_tags[[ntags]], x = tmp_bash)
  }
  writeLines(tmp_bash, con=file.path(file.path(dirname, Project_name, "Singularity.sh")))
  #Copy the campaign ncfile ---------------------------------------
  if(is.null(Campaign_Path)){
    tmp_Camp_path <- system.file("templates", "Campaign.nc4", package = "pSIMSSiteMaker")
  }else{
    tmp_Camp_path <- Campaign_Path
  }
  file.copy(tmp_Camp_path,
            file.path(dirname, Project_name,"Campaign",basename(tmp_Camp_path)))
  #---- APSIM Template
  if(is.null(APSIM_Template_Path)){
    tmp_apsim_temp_path <- system.file("templates", "template.apsim", package = "pSIMSSiteMaker")
  }else{
    tmp_apsim_temp_path <- APSIM_Template_Path
  }

  file.copy(tmp_apsim_temp_path,
            file.path(dirname, Project_name,basename(tmp_apsim_temp_path)))


  # Writing json and yaml files -------------------------------------------------------
  jsonlite::write_json(camp_json, file.path(file.path(dirname, Project_name,"Campaign", json_fname)), pretty=TRUE,auto_unbox=TRUE, factor="string")
  yaml::write_yaml(param_yml, file.path(file.path(dirname, Project_name,"Files", yaml_fname)))

  print(paste0("Simulation files were successfully created at ", file.path(dirname, Project_name)))

  if(!is.null(host)){
    remote.copy.to(host,
                   host$from,
                   host$to
    )
    print(paste0("Files were successfully moved  to", host$to))
  }
}
AgronomicForecastingLab/pSIMSSiteMaker documentation built on March 6, 2021, 2:27 p.m.