data-raw/data-transfer.R

# reads in model parameters from data-raw/ and convert to package objects
library(tidyverse)

#################################
# read in and name
#################################
soilc_params_df <- read_csv("data-raw/model-params/model-parameters.csv", col_types = "cnnnnc")
crop_agrc_df <- read_csv("data-raw/model-params/above-ground-residue-coefficients.csv", col_types = "cnnnnn") #nolint
crop_bgrc_df <- read_csv("data-raw/model-params/below-ground-residue-coefficients.csv", col_types = "cnnn") #nolint
crop_fractions_df <- read_csv("data-raw/model-params/crop-n-and-lignin-fractions.csv", col_types = "cnn") #nolint
man_fractions_df <- read_csv("data-raw/model-params/manure-coefficients.csv", col_types = "cnnn")

#################################
# write to package as named list
# for use in functions
#################################

# main model parameters
soilc_params <- soilc_params_df %>%
  select(-parameter) %>%
  rename(be = best_estimate, sd = std_dev, desc = description) %>%
  as.list() %>%
  transpose() %>%
  set_names(soilc_params_df$parameter)

# crop residue coefficients
crop_agrc <- crop_agrc_df %>%
  select(-crop) %>%
  as.list() %>%
  transpose() %>%
  set_names(crop_agrc_df$crop)

crop_bgrc <- crop_bgrc_df %>%
  select(-crop) %>%
  as.list() %>%
  transpose() %>%
  set_names(crop_agrc_df$crop)

# crop and manure n / lignin fractions
crop_fractions <- crop_fractions_df %>%
  select(-crop) %>%
  as.list() %>%
  transpose() %>%
  set_names(crop_fractions_df$crop)

man_fractions <- man_fractions_df %>%
  select(-livestock_type) %>%
  as.list() %>%
  transpose() %>%
  set_names(man_fractions_df$livestock_type)

# write to package
usethis::use_data(
  soilc_params,
  crop_agrc,
  crop_bgrc,
  crop_fractions,
  man_fractions,
  internal = FALSE,
  overwrite = TRUE
)

usethis::use_data(
  soilc_params,
  crop_agrc,
  crop_bgrc,
  crop_fractions,
  man_fractions,
  internal = TRUE,
  overwrite = TRUE
)

#################################
# small, random, believable input
# data which works for run_model()
#################################
set.seed(2605)
toy_input <- tibble(year = as.integer(rep(2011:2020, each = 12)),
                    temp = rnorm(12*10, 15, 2),
                    precip = rnorm(12*10, 55, 2),
                    pet = rnorm(12*10, 45, 2)) %>%
  nest(climdata = -year) %>%
  mutate(c_input = rnorm(10, 5, 0.3),
         lignin_frac = rnorm(10, 0.006, 0.0001),
         n_frac = rnorm(10, 0.05, 0.0005),
         sand_frac = rep(0.43, 10),
         till_type = rep("full", 10))

usethis::use_data(
  toy_input,
  internal = FALSE,
  overwrite = TRUE
)
aj-sykes92/soilc.ipcc documentation built on March 19, 2021, 11:52 a.m.