diff --git a/DESCRIPTION b/DESCRIPTION index 83b09c9..6483a42 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: BBWPC Type: Package Title: Calculator for BedrijfsBodemWaterPlan (BBWP) -Version: 0.3.2 +Version: 0.4.0 Authors@R: c( person("Gerard", "Ros", email = "gerard.ros@nmi-agro.nl", role = c("aut","cre")), person("Sven", "Verweij", email = "sven.verweij@nmi-agro.nl", role = c("aut")), diff --git a/NAMESPACE b/NAMESPACE index e76d84d..9e1b8dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(bbwp) export(bbwp_check_meas) export(bbwp_farm_score) export(bbwp_field_indicators) @@ -8,6 +9,7 @@ export(bbwp_field_scores) export(bbwp_meas_rank) export(bbwp_meas_score) export(cdf_rank) +export(ecoregeling) export(er_croprotation) export(er_farm_aim) export(er_farm_score) diff --git a/NEWS.md b/NEWS.md index dbbcee9..0ee0d6a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # Changelog BBWPC +## 0.4.0 +### Added +- add wrapper function `bbwp` to run BBWP for a series of fields +- add wrapper function `ecoregeling` to run Ecoregeling for a series of fields +- add associated test functions for `bbwp` and `ecoregeling` + ## 0.3.2 ### Added - add argument `B_SLOPE` to `bbwp_field_properties`,`bbwp_field_indicators`, issue #4 diff --git a/R/bbwp_main.R b/R/bbwp_main.R new file mode 100644 index 0000000..89a3f5c --- /dev/null +++ b/R/bbwp_main.R @@ -0,0 +1,200 @@ +#' Calculate the BBWP scores on field and farm level +#' +#' Estimate the potential to contribute to agronomic and environmental challenges in a region for a farm and assess the impact of farm measures taken. +#' A high BBWP score is indicative for the number of opportunities to improve soil quality and land use. +#' +#' @param B_SOILTYPE_AGR (character) The type of soil +#' @param B_LU_BRP (numeric) The crop type (conform BRP coding, preferable the most frequent crop on the field) +#' @param B_LU_BBWP (numeric) The BBWP category used for allocation of measures to BBWP crop categories +#' @param B_GWL_CLASS (character) The groundwater table class +#' @param B_SC_WENR (character) The risk for subsoil compaction as derived from risk assessment study of Van den Akker (2006) +#' @param B_HELP_WENR (character) The soil type abbreviation, derived from 1:50.000 soil map +#' @param B_SLOPE (numeric) The slope of the field (degrees) +#' @param B_GWP (boolean) is the field located in a groundwater protected area (options: TRUE or FALSE) +#' @param B_AREA_DROUGHT (boolean) is the field located in an area with high risks for water deficiencies (options: TRUE or FALSE) +#' @param B_CT_PSW (numeric) the critical target for required reduction in P loss from agriculture (kg P / ha) to reach targets of KRW +#' @param B_CT_NSW (numeric) the critical target for required reduction in N loss from agriculture (kg N / ha) to reach targets of KRW +#' @param B_CT_PSW_MAX (numeric) the max critical target for P reduction loss (kg P / ha) +#' @param B_CT_NSW_MAX (numeric) the max critical target for N reduction loss (kg N / ha) +#' @param A_CLAY_MI (numeric) The clay content of the soil (\%) +#' @param A_SAND_MI (numeric) The sand content of the soil (\%) +#' @param A_SILT_MI (numeric) The silt content of the soil (\%) +#' @param A_SOM_LOI (numeric) The organic matter content of the soil (\%) +#' @param A_N_RT (numeric) The organic nitrogen content of the soil (mg N / kg) +#' @param A_FE_OX (numeric) The aluminium content of soil (mmol+ / kg) +#' @param A_AL_OX (numeric) The iron content of soil (mmol+ / kg) +#' @param A_P_CC (numeric) The plant available P content, measured via 0.01M CaCl2 extraction (mg / kg) +#' @param A_P_AL (numeric) The plant extractable P content, measured via ammonium lactate extraction (mg / kg) +#' @param A_P_WA (numeric) The P-content of the soil extracted with water +#' @param A_P_SG (numeric) The P-saturation index (\%) +#' @param D_WP (numeric) The fraction of the parcel that is surrounded by surface water +#' @param D_RO_R (numeric) The risk that surface water runs off the parcel +#' @param D_AREA (numeric) the area of the field (\ m2 or \ ha) +#' @param M_DRAIN (boolean) is there tube drainage present in the field +#' @param LSW (data.table) The surface water polygon for catchment or polder +#' @param measures (data.table) the measures planned / done per fields +#' @param sector (string) a vector with the farm type given the agricultural sector (options: options: 'diary', 'arable', 'tree_nursery', 'bulbs') +#' @param output (string) a vector specifying the output type of the function. Options: scores, measures +#' +#' @import data.table +#' @import OBIC +#' +#' @export +bbwp <- function(B_SOILTYPE_AGR, B_LU_BRP, B_LU_BBWP,B_GWL_CLASS, B_SC_WENR, B_HELP_WENR,B_SLOPE, + A_CLAY_MI, A_SAND_MI, A_SILT_MI, A_SOM_LOI, A_N_RT,A_FE_OX, A_AL_OX, A_P_CC, A_P_AL, A_P_WA, A_P_SG, + B_GWP, B_AREA_DROUGHT, B_CT_PSW, B_CT_NSW,B_CT_PSW_MAX = 0.5, B_CT_NSW_MAX = 5.0, + D_WP, D_RO_R, D_AREA, + M_DRAIN, LSW, + measures, sector,output = 'scores'){ + + # add visual binding + field_id = NULL + + # check wrapper inputs that are not checked in the bbwp functions + checkmate::assert_character(output) + checkmate::assert_subset(output,choices = c('scores','measures')) + + # convert soil properties to a BBWP risk indicator + dt <- bbwp_field_properties(B_SOILTYPE_AGR = B_SOILTYPE_AGR, + B_LU_BRP = B_LU_BRP, + B_GWL_CLASS = B_GWL_CLASS, + B_SC_WENR = B_SC_WENR, + B_HELP_WENR = B_HELP_WENR, + B_SLOPE = B_SLOPE, + A_CLAY_MI = A_CLAY_MI, + A_SAND_MI = A_SAND_MI, + A_SILT_MI = A_SILT_MI, + A_SOM_LOI = A_SOM_LOI, + A_N_RT = A_N_RT, + A_FE_OX = A_FE_OX, + A_AL_OX = A_AL_OX, + A_P_CC = A_P_CC, + A_P_AL = A_P_AL, + A_P_WA = A_P_WA, + A_P_SG = A_P_SG, + D_WP = D_WP, + D_RO_R = D_RO_R, + LSW = LSW) + + # Aggregate BBWP risk indicators into five indicators + dt.ind <- bbwp_field_indicators(D_NGW_SCR = dt$ngw_scr, + D_NGW_LEA = dt$ngw_lea, + D_NGW_NLV = dt$ngw_nlv, + D_NSW_SCR = dt$nsw_scr, + D_NSW_GWT = dt$nsw_gwt, + D_NSW_RO = dt$nsw_ro, + D_NSW_SLOPE = dt$nsw_slope, + D_NSW_WS = dt$nsw_ws, + D_NSW_NLV = dt$nsw_nlv, + D_PSW_SCR = dt$psw_scr, + D_PSW_GWT= dt$psw_gwt, + D_PSW_RO = dt$psw_ro, + D_PSW_SLOPE = dt$psw_slope, + D_PSW_WS = dt$psw_ws, + D_PSW_PCC = dt$psw_pcc, + D_PSW_PSG = dt$psw_psg, + D_PSW_PRET = dt$psw_pret, + D_NUE_WRI = dt$npe_wri, + D_NUE_PBI = dt$npe_pbi, + D_NUE_WDRI = dt$npe_wdri, + D_NUE_NLV = dt$npe_nlv, + D_WUE_WWRI = dt$wue_wwri, + D_WUE_WDRI = dt$wue_wdri, + D_WUE_WHC = dt$wue_whc + ) + + # Calculate BBWP field scores + + # when measures are requested as output, then BBWP field scores are derived for situation without measures + if(output == 'measures'){measures <- NULL} + + # calculate BBWP field scores + dt.fields <- bbwp_field_scores(B_SOILTYPE_AGR = B_SOILTYPE_AGR, + B_GWL_CLASS = B_GWL_CLASS, + A_P_SG = A_P_SG, + B_SLOPE = B_SLOPE, + B_LU_BRP = B_LU_BRP, + B_LU_BBWP = B_LU_BBWP, + M_DRAIN = M_DRAIN, + D_WP = D_WP, + D_RISK_NGW = dt.ind$D_RISK_NGW, + D_RISK_NSW = dt.ind$D_RISK_NSW, + D_RISK_PSW = dt.ind$D_RISK_PSW, + D_RISK_NUE = dt.ind$D_RISK_NUE, + D_RISK_WB = dt.ind$D_RISK_WB, + B_GWP = B_GWP, + B_AREA_DROUGHT = B_AREA_DROUGHT, + B_CT_PSW = B_CT_PSW, + B_CT_NSW = B_CT_NSW, + B_CT_PSW_MAX = B_CT_PSW_MAX, + B_CT_NSW_MAX = B_CT_NSW_MAX, + measures = measures, + sector = sector + ) + + # Calculate the BBWP farm score + dt.farm <- bbwp_farm_score(D_OPI_TOT = dt.fields$D_OPI_TOT, + D_OPI_NGW = dt.fields$D_OPI_NGW, + D_OPI_NSW = dt.fields$D_OPI_NSW, + D_OPI_PSW = dt.fields$D_OPI_PSW, + D_OPI_NUE = dt.fields$D_OPI_NUE, + D_OPI_WB = dt.fields$D_OPI_WB, + D_AREA = D_AREA) + + + # Retreive the best measures to improve + dt.meas <- bbwp_meas_rank(B_SOILTYPE_AGR = B_SOILTYPE_AGR, + B_GWL_CLASS = B_GWL_CLASS, + A_P_SG = A_P_SG, + B_SLOPE = B_SLOPE, + B_LU_BRP = B_LU_BRP, + B_LU_BBWP = B_LU_BBWP, + M_DRAIN = M_DRAIN, + D_WP = D_WP, + D_OPI_NGW = dt.fields$D_OPI_NGW, + D_OPI_NSW = dt.fields$D_OPI_NSW, + D_OPI_PSW = dt.fields$D_OPI_PSW, + D_OPI_NUE = dt.fields$D_OPI_NUE, + D_OPI_WB = dt.fields$D_OPI_WB, + measures = NULL, + sector = sector + ) + + # return output when preferred measures are requested + if(output == 'measures'){ + + # convert names of dt.meas + setnames(dt.meas,gsub('\\.','_',colnames(dt.meas))) + + # convert dt.meas to a splitted list + out <- split(dt.meas,by='id',keep.by = FALSE) + + # covnert each list again to a list + out <- lapply(out,function(x) as.list(na.omit(x))) + + # set output object + out <- data.table(field_id = sort(unique(dt.meas$id)), + measures = out) + + } + + # return output when BBWP field and farm scores are requested + if(output == 'scores'){ + + # Set the column names to lowercase + setnames(dt.fields, colnames(dt.fields), tolower(colnames(dt.fields))) + setnames(dt.farm, colnames(dt.farm), tolower(colnames(dt.farm))) + + # Add field id + dt.fields[,field_id := .I] + + # set output object + out <- list(farm = as.list(dt.farm),fields = dt.fields) + + } + + + # return output + return(out) + +} diff --git a/R/er_main.R b/R/er_main.R new file mode 100644 index 0000000..0e12908 --- /dev/null +++ b/R/er_main.R @@ -0,0 +1,118 @@ +#' Calculate the Ecoregeling scores on field and farm level +#' +#' Estimate the potential to contribute to agronomic and environmental challenges in a region for a farm and assess the impact of farm measures taken. +#' A high Ecoregeling score is indicative for the number of opportunities to improve soil quality, water quality, climate biodiversity and landscape. +#' +#' @param B_SOILTYPE_AGR (character) The type of soil +#' @param B_LU_BRP (numeric) The crop type (conform BRP coding, preferable the most frequent crop on the field) +#' @param B_LU_BBWP (numeric) The BBWP category used for allocation of measures to BBWP crop categories +#' @param B_GWL_CLASS (character) The groundwater table class +#' @param B_SLOPE (numeric) The slope of the field (degrees) +#' @param A_P_SG (numeric) The P-saturation index (\%) +#' @param D_WP (numeric) The fraction of the parcel that is surrounded by surface water +#' @param D_AREA (numeric) the area of the field (\ m2 or \ ha) +#' @param M_DRAIN (boolean) is there tube drainage present in the field +#' @param farmscore (numeric) The desired total ER score on farm level +#' @param measures (data.table) the measures planned / done per fields +#' @param sector (string) a vector with the farm type given the agricultural sector (options: options: 'diary', 'arable', 'tree_nursery', 'bulbs') +#' @param output (string) a vector specifying the output type of the function. Options: scores, measures +#' +#' @import data.table +#' @import OBIC +#' +#' @export +ecoregeling <- function(B_SOILTYPE_AGR, B_LU_BRP, B_LU_BBWP,B_GWL_CLASS, B_SLOPE, + A_P_SG,D_WP, D_AREA,M_DRAIN, farmscore, + measures, sector,output = 'scores'){ + + # check wrapper inputs that are not checked in the bbwp functions + checkmate::assert_character(output) + checkmate::assert_subset(output,choices = c('scores','measures')) + + # Calculate the minimum required ER scores on Farm level + dt.farm.aim <- er_farm_aim(B_SOILTYPE_AGR = B_SOILTYPE_AGR, + D_AREA = D_AREA, + farmscore = 100) + + # when measures are requested as output, then field scores are derived for situation without measures + if(output == 'measures'){measures <- NULL} + + # Calculate the aggregated ER scores per field + dt.fields <- er_field_scores(B_SOILTYPE_AGR = B_SOILTYPE_AGR, + B_LU_BRP = B_LU_BRP, + B_LU_BBWP = B_LU_BBWP, + D_AREA = D_AREA, + B_CT_SOIL = dt.farm.aim$B_CT_SOIL, + B_CT_WATER = dt.farm.aim$B_CT_WATER, + B_CT_CLIMATE = dt.farm.aim$B_CT_CLIMATE, + B_CT_BIO = dt.farm.aim$B_CT_BIO, + B_CT_LANDSCAPE = dt.farm.aim$B_CT_LANDSCAPE, + measures = measures, + sector) + + # Calculate the ER farm score + dt.farm <- er_farm_score(D_OPI_TOT = dt.fields$D_OPI_TOT, + D_OPI_SOIL = dt.fields$D_OPI_SOIL, + D_OPI_WATER = dt.fields$D_OPI_WATER, + D_OPI_CLIMATE = dt.fields$D_OPI_CLIMATE, + D_OPI_BIO = dt.fields$D_OPI_BIO, + D_OPI_LANDSCAPE = dt.fields$D_OPI_LANDSCAPE, + D_AREA = D_AREA) + + # Retreive the best measures to improve + dt.meas <- er_meas_rank(B_SOILTYPE_AGR = B_SOILTYPE_AGR, + B_GWL_CLASS = B_GWL_CLASS, + A_P_SG = A_P_SG, + B_SLOPE = B_SLOPE, + B_LU_BRP = B_LU_BRP, + B_LU_BBWP = B_LU_BBWP, + M_DRAIN = M_DRAIN, + D_WP = D_WP, + D_AREA = D_AREA, + B_CT_SOIL = dt.farm.aim$B_CT_SOIL, + B_CT_WATER = dt.farm.aim$B_CT_WATER, + B_CT_CLIMATE = dt.farm.aim$B_CT_CLIMATE, + B_CT_BIO = dt.farm.aim$B_CT_BIO, + B_CT_LANDSCAPE = dt.farm.aim$B_CT_LANDSCAPE, + measures = measures, + sector = sector + ) + + # return output when preferred measures are requested + if(output == 'measures'){ + + # convert names of dt.meas + setnames(dt.meas,gsub('\\.','_',colnames(dt.meas))) + + # convert dt.meas to a splitted list + out <- split(dt.meas,by='id',keep.by = FALSE) + + # covnert each list again to a list + out <- lapply(out,function(x) as.list(na.omit(x))) + + # set output object + out <- data.table(field_id = sort(unique(dt.meas$id)), + measures = out) + + } + + # return output when BBWP field and farm scores are requested + if(output == 'scores'){ + + # Set the column names to lowercase + setnames(dt.fields, colnames(dt.fields), tolower(colnames(dt.fields))) + setnames(dt.farm, colnames(dt.farm), tolower(colnames(dt.farm))) + + # Add field id + setnames(dt.fields,'id','field_id') + + # set output object + out <- list(farm = as.list(dt.farm),fields = dt.fields) + + } + + + # return output + return(out) + +} diff --git a/man/bbwp.Rd b/man/bbwp.Rd new file mode 100644 index 0000000..455ee76 --- /dev/null +++ b/man/bbwp.Rd @@ -0,0 +1,110 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bbwp_main.R +\name{bbwp} +\alias{bbwp} +\title{Calculate the BBWP scores on field and farm level} +\usage{ +bbwp( + B_SOILTYPE_AGR, + B_LU_BRP, + B_LU_BBWP, + B_GWL_CLASS, + B_SC_WENR, + B_HELP_WENR, + B_SLOPE, + A_CLAY_MI, + A_SAND_MI, + A_SILT_MI, + A_SOM_LOI, + A_N_RT, + A_FE_OX, + A_AL_OX, + A_P_CC, + A_P_AL, + A_P_WA, + A_P_SG, + B_GWP, + B_AREA_DROUGHT, + B_CT_PSW, + B_CT_NSW, + B_CT_PSW_MAX = 0.5, + B_CT_NSW_MAX = 5, + D_WP, + D_RO_R, + D_AREA, + M_DRAIN, + LSW, + measures, + sector, + output = "scores" +) +} +\arguments{ +\item{B_SOILTYPE_AGR}{(character) The type of soil} + +\item{B_LU_BRP}{(numeric) The crop type (conform BRP coding, preferable the most frequent crop on the field)} + +\item{B_LU_BBWP}{(numeric) The BBWP category used for allocation of measures to BBWP crop categories} + +\item{B_GWL_CLASS}{(character) The groundwater table class} + +\item{B_SC_WENR}{(character) The risk for subsoil compaction as derived from risk assessment study of Van den Akker (2006)} + +\item{B_HELP_WENR}{(character) The soil type abbreviation, derived from 1:50.000 soil map} + +\item{B_SLOPE}{(numeric) The slope of the field (degrees)} + +\item{A_CLAY_MI}{(numeric) The clay content of the soil (\%)} + +\item{A_SAND_MI}{(numeric) The sand content of the soil (\%)} + +\item{A_SILT_MI}{(numeric) The silt content of the soil (\%)} + +\item{A_SOM_LOI}{(numeric) The organic matter content of the soil (\%)} + +\item{A_N_RT}{(numeric) The organic nitrogen content of the soil (mg N / kg)} + +\item{A_FE_OX}{(numeric) The aluminium content of soil (mmol+ / kg)} + +\item{A_AL_OX}{(numeric) The iron content of soil (mmol+ / kg)} + +\item{A_P_CC}{(numeric) The plant available P content, measured via 0.01M CaCl2 extraction (mg / kg)} + +\item{A_P_AL}{(numeric) The plant extractable P content, measured via ammonium lactate extraction (mg / kg)} + +\item{A_P_WA}{(numeric) The P-content of the soil extracted with water} + +\item{A_P_SG}{(numeric) The P-saturation index (\%)} + +\item{B_GWP}{(boolean) is the field located in a groundwater protected area (options: TRUE or FALSE)} + +\item{B_AREA_DROUGHT}{(boolean) is the field located in an area with high risks for water deficiencies (options: TRUE or FALSE)} + +\item{B_CT_PSW}{(numeric) the critical target for required reduction in P loss from agriculture (kg P / ha) to reach targets of KRW} + +\item{B_CT_NSW}{(numeric) the critical target for required reduction in N loss from agriculture (kg N / ha) to reach targets of KRW} + +\item{B_CT_PSW_MAX}{(numeric) the max critical target for P reduction loss (kg P / ha)} + +\item{B_CT_NSW_MAX}{(numeric) the max critical target for N reduction loss (kg N / ha)} + +\item{D_WP}{(numeric) The fraction of the parcel that is surrounded by surface water} + +\item{D_RO_R}{(numeric) The risk that surface water runs off the parcel} + +\item{D_AREA}{(numeric) the area of the field (\ m2 or \ ha)} + +\item{M_DRAIN}{(boolean) is there tube drainage present in the field} + +\item{LSW}{(data.table) The surface water polygon for catchment or polder} + +\item{measures}{(data.table) the measures planned / done per fields} + +\item{sector}{(string) a vector with the farm type given the agricultural sector (options: options: 'diary', 'arable', 'tree_nursery', 'bulbs')} + +\item{output}{(string) a vector specifying the output type of the function. Options: scores, measures} +} +\description{ +Estimate the potential to contribute to agronomic and environmental challenges in a region for a farm and assess the impact of farm measures taken. +A high BBWP score is indicative for the number of opportunities to improve soil quality and land use. +} diff --git a/man/ecoregeling.Rd b/man/ecoregeling.Rd new file mode 100644 index 0000000..17e4ed4 --- /dev/null +++ b/man/ecoregeling.Rd @@ -0,0 +1,53 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/er_main.R +\name{ecoregeling} +\alias{ecoregeling} +\title{Calculate the Ecoregeling scores on field and farm level} +\usage{ +ecoregeling( + B_SOILTYPE_AGR, + B_LU_BRP, + B_LU_BBWP, + B_GWL_CLASS, + B_SLOPE, + A_P_SG, + D_WP, + D_AREA, + M_DRAIN, + farmscore, + measures, + sector, + output = "scores" +) +} +\arguments{ +\item{B_SOILTYPE_AGR}{(character) The type of soil} + +\item{B_LU_BRP}{(numeric) The crop type (conform BRP coding, preferable the most frequent crop on the field)} + +\item{B_LU_BBWP}{(numeric) The BBWP category used for allocation of measures to BBWP crop categories} + +\item{B_GWL_CLASS}{(character) The groundwater table class} + +\item{B_SLOPE}{(numeric) The slope of the field (degrees)} + +\item{A_P_SG}{(numeric) The P-saturation index (\%)} + +\item{D_WP}{(numeric) The fraction of the parcel that is surrounded by surface water} + +\item{D_AREA}{(numeric) the area of the field (\ m2 or \ ha)} + +\item{M_DRAIN}{(boolean) is there tube drainage present in the field} + +\item{farmscore}{(numeric) The desired total ER score on farm level} + +\item{measures}{(data.table) the measures planned / done per fields} + +\item{sector}{(string) a vector with the farm type given the agricultural sector (options: options: 'diary', 'arable', 'tree_nursery', 'bulbs')} + +\item{output}{(string) a vector specifying the output type of the function. Options: scores, measures} +} +\description{ +Estimate the potential to contribute to agronomic and environmental challenges in a region for a farm and assess the impact of farm measures taken. +A high Ecoregeling score is indicative for the number of opportunities to improve soil quality, water quality, climate biodiversity and landscape. +} diff --git a/tests/testthat/test-bbwp.R b/tests/testthat/test-bbwp.R new file mode 100644 index 0000000..bf83c72 --- /dev/null +++ b/tests/testthat/test-bbwp.R @@ -0,0 +1,232 @@ +require(testthat) + + # # default input for testing + # B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei') + # B_GWL_CLASS = c('GtIII', 'GtI', 'GtV') + # B_SC_WENR = c(4, 2,2) + # B_HELP_WENR = c('AZW1AwF', 'AZW1AwF','AZW1AwF') + # A_P_SG = c(0.4, 0.8, 1) + # A_CLAY_MI = c(15, 5,8) + # A_SAND_MI = c(45, 65,15) + # A_SILT_MI = c(40, 30,45) + # A_SOM_LOI = c(5, 15,3) + # A_N_RT = c(4200, 1000,3000) + # A_FE_OX = c(500, 500,800) + # A_AL_OX = c(150, 150,145) + # A_P_CC = c(5, 1,6) + # A_P_AL = c(65, 5,40) + # A_P_WA = c(52, 5,45) + # B_SLOPE = c(1.5,4,1.5) + # B_LU_BRP = c(265, 1932, 266) + # B_LU_BBWP = c(1,4,1) + # M_DRAIN = c(TRUE, FALSE, TRUE) + # D_WP = c(0, 0.5, 1) + # D_RO_R = c(0.5, 0,0.9) + # D_AREA = c(100,80,2.5) + # B_GWP = c(TRUE, FALSE, TRUE) + # B_AREA_DROUGHT = c(TRUE, FALSE, TRUE) + # B_CT_PSW = c(0, 25, 50) + # B_CT_NSW = c(0, 50, 100) + # B_CT_PSW_MAX = 0.5 + # B_CT_NSW_MAX = 5.0 + # measures = NULL + # sector = c('dairy', 'arable') + # output = 'scores' + + LSW = data.table(sd_wp = rep(0.27, 3), + mean_wp = 0.33, + sd_n_rt = 371.4, + sd_p_al = 10.6, + sd_p_cc = 1.34, + sd_p_sg = 7.92, + sd_p_wa = 12.5, + sd_ro_r = 0.305, + sd_al_ox = 3.99, + sd_fe_ox = 6.25, + mean_n_rt = 1599.9, + mean_p_al= 63.2, + mean_p_cc =3.27, + mean_p_sg = 59.5, + mean_p_wa = 56.781, + mean_ro_r = 0.471, + mean_al_ox = 36.401, + mean_fe_ox = 27.877, + sd_clay_mi = 0.583, + sd_sand_mi = 5.93, + sd_silt_mi = 4.158, + sd_som_loi = 0.799, + mean_clay_mi = 2.00, + mean_sand_mi = 83.96, + mean_silt_mi = 12.81, + mean_som_loi = 3.92 + ) +# run example 1 without any measures taken +test <- bbwp(B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei'), + B_GWL_CLASS = c('GtIII', 'GtI', 'GtV'), + B_SC_WENR = c(4, 2,2), + B_HELP_WENR = c('AZW1AwF', 'AZW1AwF','AZW1AwF'), + A_P_SG = c(0.4, 0.8, 1), + A_CLAY_MI = c(15, 5,8), + A_SAND_MI = c(45, 65,15), + A_SILT_MI = c(40, 30,45), + A_SOM_LOI = c(5, 15,3) , + A_N_RT = c(4200, 1000,3000), + A_FE_OX = c(500, 500,800), + A_AL_OX = c(150, 150,145), + A_P_CC = c(5, 1,6), + A_P_AL = c(65, 5,40), + A_P_WA = c(52, 5,45), + B_SLOPE = c(1.5,4,1.5), + B_LU_BRP = c(265, 1932, 266), + B_LU_BBWP = c(1,4,1), + M_DRAIN = c(TRUE, FALSE, TRUE), + D_WP = c(0, 0.5, 1), + D_RO_R = c(0.5, 0,0.9), + D_AREA = c(100,80,2.5), + B_GWP = c(TRUE, FALSE, TRUE), + B_AREA_DROUGHT = c(TRUE, FALSE, TRUE), + B_CT_PSW = c(0, 25, 50), + B_CT_NSW = c(0, 50, 100), + B_CT_PSW_MAX = 0.5, + B_CT_NSW_MAX = 5.0, + measures = NULL, + sector = c('dairy', 'arable'), + output = 'scores', + LSW = LSW + ) + + # run tests on format and output values + test_that("check bbwp", { + expect_equal( + object = names(test), + expected = c('farm','fields')) + }) + + test_that("check bbwp", { + expect_equal( + object = colnames(test$fields), + expected = c("d_opi_ngw", "d_opi_nsw", "d_opi_psw", "d_opi_nue", "d_opi_wb" , "d_opi_tot", "field_id")) + }) + + test_that("check bbwp", { + expect_equal( + object = test$fields$d_opi_tot, + expected = c(35,17,8), + tolerance = 0.01) + }) + + test_that("check bbwp", { + expect_equal( + object = as.numeric(unlist(test$farm)), + expected = c(27,42,54,54,24,92), + tolerance = 0.01) + }) + + +# run example 2 without any measures taken +test <- bbwp(B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei'), + B_GWL_CLASS = c('GtIII', 'GtI', 'GtV'), + B_SC_WENR = c(4, 2,2), + B_HELP_WENR = c('AZW1AwF', 'AZW1AwF','AZW1AwF'), + A_P_SG = c(0.4, 0.8, 1), + A_CLAY_MI = c(15, 5,8), + A_SAND_MI = c(45, 65,15), + A_SILT_MI = c(40, 30,45), + A_SOM_LOI = c(5, 15,3) , + A_N_RT = c(4200, 1000,3000), + A_FE_OX = c(500, 500,800), + A_AL_OX = c(150, 150,145), + A_P_CC = c(5, 1,6), + A_P_AL = c(65, 5,40), + A_P_WA = c(52, 5,45), + B_SLOPE = c(1.5,4,1.5), + B_LU_BRP = c(265, 1932, 266), + B_LU_BBWP = c(1,4,1), + M_DRAIN = c(TRUE, FALSE, TRUE), + D_WP = c(0, 0.5, 1), + D_RO_R = c(0.5, 0,0.9), + D_AREA = c(100,80,2.5), + B_GWP = c(TRUE, FALSE, TRUE), + B_AREA_DROUGHT = c(TRUE, FALSE, TRUE), + B_CT_PSW = c(0, 25, 50), + B_CT_NSW = c(0, 50, 100), + B_CT_PSW_MAX = 0.5, + B_CT_NSW_MAX = 5.0, + measures = NULL, + sector = c('dairy', 'arable'), + output = 'measures', + LSW = LSW +) + + # run tests on format and output values + test_that("check bbwp", { + expect_equal( + object = names(test$measures[[1]]), + expected = c("top_tot","top_ngw","top_nsw","top_psw","top_wb", "top_nue")) + }) + + test_that("check bbwp", { + expect_equal( + object = test$measures[[1]]$top_tot, + expected = c("G17","G16", "G18", "G19", "G88")) + }) + + + +# get internal table with measures +dt.measures <- as.data.table(BBWPC::bbwp_measures) +dt.measures <- dt.measures[!is.na(effect_psw)] + +# make measurement list for 2 of the 4 fields +measures <- rbind(data.table(id = 1, dt.measures[c(2,5,18,28,32,3,38,43,62)]), + data.table(id = 3, dt.measures[c(7,21,30,46,5)])) + +# run example 3 without any measures taken +test <- bbwp(B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei'), + B_GWL_CLASS = c('GtIII', 'GtI', 'GtV'), + B_SC_WENR = c(4, 2,2), + B_HELP_WENR = c('AZW1AwF', 'AZW1AwF','AZW1AwF'), + A_P_SG = c(0.4, 0.8, 1), + A_CLAY_MI = c(15, 5,8), + A_SAND_MI = c(45, 65,15), + A_SILT_MI = c(40, 30,45), + A_SOM_LOI = c(5, 15,3) , + A_N_RT = c(4200, 1000,3000), + A_FE_OX = c(500, 500,800), + A_AL_OX = c(150, 150,145), + A_P_CC = c(5, 1,6), + A_P_AL = c(65, 5,40), + A_P_WA = c(52, 5,45), + B_SLOPE = c(1.5,4,1.5), + B_LU_BRP = c(265, 1932, 266), + B_LU_BBWP = c(1,4,1), + M_DRAIN = c(TRUE, FALSE, TRUE), + D_WP = c(0, 0.5, 1), + D_RO_R = c(0.5, 0,0.9), + D_AREA = c(100,80,2.5), + B_GWP = c(TRUE, FALSE, TRUE), + B_AREA_DROUGHT = c(TRUE, FALSE, TRUE), + B_CT_PSW = c(0, 25, 50), + B_CT_NSW = c(0, 50, 100), + B_CT_PSW_MAX = 0.5, + B_CT_NSW_MAX = 5.0, + measures = measures, + sector = c('dairy', 'arable'), + output = 'scores', + LSW = LSW +) + + # run tests on format and output values + test_that("check bbwp", { + expect_equal( + object = test$fields$d_opi_tot, + expected = c(100,17,8), + tolerance = 0.01) + }) + + test_that("check bbwp", { + expect_equal( + object = as.numeric(unlist(test$farm)), + expected = c(62,92,55,55,72,96), + tolerance = 0.01) + }) diff --git a/tests/testthat/test-er.R b/tests/testthat/test-er.R new file mode 100644 index 0000000..3ee820b --- /dev/null +++ b/tests/testthat/test-er.R @@ -0,0 +1,130 @@ +require(testthat) + + # # default input for testing + # B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei') + # B_GWL_CLASS = c('GtIII', 'GtI', 'GtV') + # A_P_SG = c(0.4, 0.8, 1) + # B_SLOPE = c(1.5,4,1.5) + # B_LU_BRP = c(265, 1932, 266) + # B_LU_BBWP = c(1,4,1) + # M_DRAIN = c(TRUE, FALSE, TRUE) + # D_WP = c(0, 0.5, 1) + # D_AREA = c(100,80,2.5) + # measures = NULL + # farmscore = 100 + # sector = c('dairy', 'arable') + # output = 'measures' + +# run example 1 without any measures taken +test <- ecoregeling(B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei'), + B_GWL_CLASS = c('GtIII', 'GtI', 'GtV'), + A_P_SG = c(0.4, 0.8, 1), + B_SLOPE = c(1.5,4,1.5), + B_LU_BRP = c(265, 1932, 266), + B_LU_BBWP = c(1,4,1), + M_DRAIN = c(TRUE, FALSE, TRUE), + D_WP = c(0, 0.5, 1), + D_AREA = c(100,80,2.5), + farmscore = 100, + measures = NULL, + sector = c('dairy', 'arable'), + output = 'scores' + ) + + # run tests on format and output values + test_that("check ecoregeling", { + expect_equal( + object = names(test), + expected = c('farm','fields')) + }) + + test_that("check ecoregeling", { + expect_equal( + object = colnames(test$fields), + expected = c("field_id","d_opi_soil","d_opi_water","d_opi_climate","d_opi_bio","d_opi_landscape","d_opi_tot")) + }) + + test_that("check ecoregeling", { + expect_equal( + object = test$fields$d_opi_tot, + expected = c(6,6,6), + tolerance = 0.01) + }) + + test_that("check ecoregeling", { + expect_equal( + object = as.numeric(unlist(test$farm)), + expected = c(6,9,6,11,6,0), + tolerance = 0.01) + }) + + +# get internal table with measures +dt.measures <- as.data.table(BBWPC::bbwp_measures) +dt.measures <- dt.measures[!is.na(eco_id)] + +# make measurement list for 2 of the 4 fields +measures <- rbind(data.table(id = 1, dt.measures[c(2,5,18,28,32,3,38,43,62)]), + data.table(id = 3, dt.measures[c(7,21,30,46,5)])) + + +# run example 2 with any measures taken +test <- ecoregeling(B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei'), + B_GWL_CLASS = c('GtIII', 'GtI', 'GtV'), + A_P_SG = c(0.4, 0.8, 1), + B_SLOPE = c(1.5,4,1.5), + B_LU_BRP = c(265, 1932, 266), + B_LU_BBWP = c(1,4,1), + M_DRAIN = c(TRUE, FALSE, TRUE), + D_WP = c(0, 0.5, 1), + D_AREA = c(100,80,2.5), + farmscore = 100, + measures = measures, + sector = c('dairy', 'arable'), + output = 'scores' +) + + # run tests on format and output values + test_that("check ecoregeling", { + expect_equal( + object = test$fields$d_opi_tot, + expected = c(42,6,42), + tolerance = 0.01) + }) + + test_that("check ecoregeling", { + expect_equal( + object = as.numeric(unlist(test$farm)), + expected = c(26,34,30,61,16,15), + tolerance = 0.01) + }) + +# run example 3 with any measures taken + test <- ecoregeling(B_SOILTYPE_AGR = c('dekzand', 'loess', 'rivierklei'), + B_GWL_CLASS = c('GtIII', 'GtI', 'GtV'), + A_P_SG = c(0.4, 0.8, 1), + B_SLOPE = c(1.5,4,1.5), + B_LU_BRP = c(265, 1932, 266), + B_LU_BBWP = c(1,4,1), + M_DRAIN = c(TRUE, FALSE, TRUE), + D_WP = c(0, 0.5, 1), + D_AREA = c(100,80,2.5), + farmscore = 100, + measures = measures, + sector = c('dairy', 'arable'), + output = 'measures' + ) + + # run tests on format and output values + test_that("check ecoregeling", { + expect_equal( + object = names(test$measures[[1]]), + expected = c("top_tot","top_soil","top_water","top_climate","top_biodiversity", "top_landscape")) + }) + + test_that("check ecoregeling", { + expect_equal( + object = test$measures[[1]]$top_tot, + expected = c('B156','B133','G50')) + }) + \ No newline at end of file