From 2be14e7938123d5266f1865f460411459272ca1a Mon Sep 17 00:00:00 2001
From: biopsichas <svajunas.plunge@gmail.com>
Date: Wed, 8 Feb 2023 18:23:51 +0200
Subject: [PATCH] adjusted plot_variable_at_harvkill

---
 R/helper.R        |  4 +++-
 R/plot_mgt_harv.R | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/R/helper.R b/R/helper.R
index a4849c8..b0c7e4d 100644
--- a/R/helper.R
+++ b/R/helper.R
@@ -19,7 +19,9 @@ add_kill_op <- function(project_path){
     i <- grep("hvkl", mgt_sch)[1]
     kill_line <- str_replace(mgt_sch[i], "hvkl", "kill") %>%
       str_replace(c("forest_cut|grain1|grain|grass_bag|grass_mulch|hay_cut_high|hay_cut_low|orchard|peanuts|
-                silage|stover_high|stover_los|stover_med|tuber|vegetables"), "null")
+                silage|stover_high|stover_los|stover_med|tuber|vegetables"), "null") %>%
+      str_replace_all("[:digit:]", "0") %>%
+      str_replace("0.00000", "0.00001") ## Kill operation at 0.00001 HU, same day as harvest.
     mgt_sch[i] <- str_replace(mgt_sch[i], "hvkl", "harv")
     mgt_sch <- insert_line_at(mgt_sch, kill_line, insert_after=i)
     l <- l+1
diff --git a/R/plot_mgt_harv.R b/R/plot_mgt_harv.R
index b70941c..c5d6db7 100644
--- a/R/plot_mgt_harv.R
+++ b/R/plot_mgt_harv.R
@@ -29,6 +29,17 @@ plot_variable_at_harvkill <- function(sim_verify, variable, years = 1900:2100) {
     ungroup() %>%
     filter(n == 2, operation == 'HARVEST')
 
+  ##Just in case dates of harvest and kill differ a bit
+  if(dim(tbl_harv)[1] == 0){
+    tbl_harv <- sim_verify$mgt_out %>%
+      filter(year %in% years) %>%
+      filter(operation %in% c('HARVEST', 'KILL')) %>%
+      group_by(hru,year, mon) %>%
+      mutate(n = n()) %>%
+      ungroup() %>%
+      filter(n == 2, operation == 'HARVEST')
+  }
+
   if(variable == 'phu') {
     tbl_harv <- tbl_harv %>%
       select(op_typ, phuplant) %>%
-- 
GitLab