compile_plots: Compile tree data by plot

View source: R/compile_plots.R

compile_plotsR Documentation

Compile tree data by plot

Description

Supply data.frame of tree data with plot ids and a data.frame of plot records. The function works by iterating through the tree data plot by plot. The compile_plots() function accepts a list of functions which compute attributes. Several example functions are provided, but they are probably not sufficient for operational usage. Hopefully more will be added over time. This function is meant to work hand in hand with compile_trees() but is still a work in progress.

Usage

compile_plots(
  cl = NA,
  df_tree = list(NA, data.frame(PLT_CN = 1, year = 2020, dbh = NA, spp = NA, ac =
    0.1))[[1]],
  df_plot = list(NA, data.frame(PLT_CN = c(1, 2), PLOT = 1, YEAR = 2020, STATE = 1,
    COUNTY = 1, PROJECT = "Test"))[[1]],
  tree_nms = list(plot_ids = c("PLOT", "YEAR"), tree_ids = c("tr_cn"), dbh = "DIA", ht =
    "HT", spp = "SPCD", expansion = "TPA"),
  plot_nms = list(plot_ids = c("PLOT", "YEAR")),
  tree_filter = c(NA, "select * from df_tree where dbh > 2 "),
  plot_filter = c(NA,
    "select * from df_plot where YEAR = 2018 and STATE = 'WA' and CONDITION = 1"),
  dir_out = file.path("c:/temp/RSForInvt/Compile", format(Sys.Date())),
  nm_out = "compile_plots",
  sql_tbl = "plot_metrics",
  append = T,
  fns_compute = list(plot_wtsum, plot_lor_qmd),
  return = F,
  do_debug = F,
  nclus = 4,
  ...
)

plot_wtsum(df_tree, tree_nms, sum_nms, ...)

plot_lor_qmd(df_tree, tree_nms, ...)

spp_y_plot(df_tree, tree_nms, n_dom_spp = 3, spp_y, ...)

Arguments

df_tree

data frame of tree records

df_plot

data frame of plot records

tree_nms

map expected tree column names onto df_tree. These are the minimum expected names: c( plot_ids = "?", tr_ids = c("?","?") , dbh = "?" , ht = "?" , spp = "?" , expansion = "?"). Feel free to provide others used by custom functions.

plot_nms

map expected plot column names onto df_plot. These are the minimum expected names: c( plot_ids = c("?","?"), plt_wt = c(NA,"?")[1] ). Feel free to provide others used by custom functions.

tree_filter

sqldf string to filter out trees

plot_filter

sqldf string to filter out plots

dir_out

where to write results of plot level compilation

fns_compute

list of optional functions to compute plot level attributes. Optional functions must accept "df_tree" and "..." arguments and generally also (typically) accept "tree_nms" argument, e.g.,

>fn_ba_ac = function(df_tree, tree_nms, ...) sum(df_tree[,tree_nms["ba_ac"]])

This example also uses a custom value "ba_ac" value in the tree_nms argument, e.g.,

>tree_nms = c( ba_ac = "BA_AC") )

where "BA_AC" is presumably equal to ba_tree*TPA_tree ...

return

return compiled data

do_debug

stop and debug function

nclus

if nclus > 1 then nclus parallel nodes are split off to process in parallel

...

additional arguments provided to fns_compute e.g textString="hello" argument in silly function someFun = function(tl,tlNms,textString)data.frame(note=textString)

sum_nms

<...> argument passed to optional plot_wtsum function, passed generically by compile_plots through '...'

n_dom_spp

<...> argument passed to optional spp_y_plot function, passed generically by compile_plots through '...'

spp_y

<...> argument passed to optional spp_y_plot function, passed generically by compile_plots through '...'

Details

This program is free software but it is provided WITHOUT WARRANTY and with ABSOLUTELY NO GUARANTEE of fitness or functionality for any purpose; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.


Revision History

1.0 6/12/2020 Created
1.1 8/23/2024 Modify checks on data.frame inputs to be more generic (e.g., allow tibbles)
1.1 8/23/2024 Add more debug checks
1.2 10/09/2024 complete overhaul, simpler and consistent with compile_trees

Value

NULL (return=F) or a data.frame of plot attributes (return=T)

Author(s)

Jacob Strunk <someone@somewhere.com>

See Also

compile_trees
parLapplyLB

Examples



   #generate data
      test0 = data.frame(plot=1:10, id=1:50,dbh=1:50,spp=sample(letters[1:5],50,T),acres=0.1,ntrees=1)
      test0$ht = abs(75*test0$dbh/12 + rnorm(nrow(test0))*3)

    #compile trees : processes A and B are equivalent
     #A
      test1 = ba_ft(test0,tree_nms=c(tree_ids="id",dbh="dbh",dbcl="dbcl",spp="spp"))
      test2 = dbcl(test1,tree_nms=c(tree_ids="id",dbh="dbh",dbcl="dbcl",spp="spp") )
      test3 = dbcl_y(test2,tree_nms=c(tree_ids="id",dbh="dbh",dbcl="dbcl"), vars_group= c("ba_ft","dbh"))
      test4 = spp_y(test3,tree_nms=c(tree_ids="id",dbh="dbh",dbcl="dbcl",spp="spp"), vars_group= c("ba_ft"))

    #B
    test5 =
      compile_trees(
        #
        test0
        ,tree_nms = c(tree_ids="id",dbh="dbh",dbcl="dbcl",spp="spp",acres="acres")
        ,vars_group = c("ba_ft")
        #optional functions to run against tree data must accept elipsis argument: "..."
        ,fns_compute =
          list(
            tpa
            ,ba_ft
            ,dbcl
            ,dbcl_y
            ,spp_y
            ,dbcl_spp_y
          )

      )

  test5

  #compile plots
  res_pl =   compile_plots(
    df_tree = test5
    , tree_nms = list(plot_ids = c("plot") , tr_ids = c("id") , dbh = "dbh" , ht = "ht" , spp = "spp" , expansion = "TPA" )
    , plot_nms = list( plot_ids = c( "plot" ), plt_wt = NA )
    , dir_out= file.path("c:/temp/RSForInvt/Compile",format(Sys.Date()))
    , fns_compute = list(
      plot_lor_qmd
       ,plot_wtsum
    )
    ,return = T
    ,do_debug = F
    ,nclus = 1
    #arguments to custom functions - in this case plot_wtsum
    ,sum_nms = c("ntrees",grep("^ba",names(test5),value=T))
    ,append = F

  )





jstrunk001/RForInvt documentation built on April 17, 2025, 5:02 p.m.