/Prototypes/Cotton/munge-experiment-data/scripts/emerald/emerald_soilwater_2015.r

https://github.com/APSIMInitiative/ApsimX · R · 136 lines · 35 code · 74 blank · 27 comment · 0 complexity · 211d4b61b16463d5e3a4a9b192b12dab MD5 · raw file

  1. #! /bin/bash
  2. library(tidyverse)
  3. library(readxl)
  4. library(lubridate)
  5. dir_sourcedata <- file.path("C:","Users","ver078","Dropbox","CottonModel","OldData","Emerald","Observed","Emerald data")
  6. path <- file.path(dir_sourcedata, "Soil Water Measurements 15-16 and 16-17sjy.xlsx")
  7. # nb. year goes by the year of sowing (not the year of harvesting)
  8. # eg. 2015-2016 year would be called 2015.
  9. raw <- read_xlsx(path, sheet = "2015-16", range = "A3:P179")
  10. # remove the calculations for volumetric water and gravametric as the excel spreadsheet formulas are a bit suspect.
  11. data <- raw %>% select(-13,-15,-16)
  12. # rename variable to get rid of spaces in column names
  13. data <- data %>% rename("hill_furrow" = `Hill/Furrow`, "sample_no" = `sample no`, "soil_core_cm" = `Soil core cm`,
  14. "wet_weight_g" = `wet weight (g)`, "dry_weight_g" = `dry weight (g)`, "water_weight_g" = `weight difference`,
  15. "water_gravimetric" = `%` , "bulk_density" = `Bulk Density`)
  16. # do the volumetric calculations yourself.
  17. data <- data %>% mutate(sw_volumetric = water_gravimetric * bulk_density)
  18. # now get rid of columns we don't need
  19. data <- data %>% select(-wet_weight_g, -dry_weight_g, -water_weight_g, -water_gravimetric)
  20. # change the PD column (Planting Date) to sowing.
  21. data <- data %>% rename("sowing" = PD)
  22. data$sowing <- paste("S", data$sowing, sep = "")
  23. # turn "depth" column into layer_no
  24. data <- data %>% separate(col = depth, into = c("top", "bottom"), sep = "-", remove = TRUE, convert = TRUE)
  25. # create a column with the layer number for each layer
  26. group_sample_no <- data %>% group_by(Date, sowing, Rep, hill_furrow, sample_no)
  27. group_sample_no <- group_sample_no %>% mutate(layer_no = row_number())
  28. data <- ungroup(group_sample_no)
  29. # take the average of all the samples
  30. group_layer_no <- data %>% group_by(Date, sowing, Rep, hill_furrow, layer_no)
  31. group_layer_no <- group_layer_no %>% summarise(depth = first(soil_core_cm), sw_volumetric = mean(sw_volumetric), bulk_density = mean(bulk_density) )
  32. data <- ungroup(group_layer_no)
  33. # take the average of all the replicates.
  34. group_layer_no <- data %>% group_by(Date, sowing, hill_furrow, layer_no)
  35. group_layer_no <- group_layer_no %>% summarise(depth = first(depth), sw_volumetric = mean(sw_volumetric), bulk_density = mean(bulk_density) )
  36. data <- ungroup(group_layer_no)
  37. # add year column
  38. data <- data %>% rename("date" = Date)
  39. data <- data %>% mutate(year = 2015)
  40. data <- data %>% select(year, sowing, date, everything())
  41. data <- data %>% rename("sw" = sw_volumetric, "bd" = bulk_density)
  42. # save current state with new variable because we want add this to a worksheet on its own,
  43. # in case we want to simulate hills vs furrows
  44. # turn (depth, sw, bd) rows for each layer number into columns so we can compare with simulation results.
  45. sw2015_hill_furrow <- data %>% pivot_wider(id_cols = c(year, sowing, hill_furrow, date), names_from = layer_no, values_from = c(depth, sw, bd))
  46. # long <- data %>% gather(key = "variable", value = "value", c(depth, sw, bd))
  47. # newcol <- long %>% unite(newname, variable, layer_no, sep = "_")
  48. # sw2015_hill_furrow <- newcol %>% spread(key = newname, value = value)
  49. # sw2015_hill_furrow <- sw2015_hill_furrow %>% arrange(year, sowing, date)
  50. # take average of hill and furrow
  51. group_layer_no <- data %>% group_by(year, sowing, date, layer_no)
  52. group_layer_no <- group_layer_no %>% summarise(depth = first(depth), sw = mean(sw), bd = mean(bd) )
  53. data <- ungroup(group_layer_no)
  54. # turn (depth, sw, bd) rows for each layer number into columns so we can compare with simulation results.
  55. #sw2015 <- data %>% pivot_wider(id_cols = c(year, sowing, date), names_from = layer_no, values_from = c(depth, sw, bd))
  56. sw2015 <- data %>% pivot_wider(names_from = layer_no, values_from = c(depth, sw, bd))
  57. # long <- data %>% gather(key = "variable", value = "value", c(depth, sw, bd))
  58. # newcol <- long %>% unite(newname, variable, layer_no, sep = "_")
  59. # sw2015 <- newcol %>% spread(key = newname, value = value)
  60. # sw2015 <- sw2015 %>% arrange(year, sowing, date)
  61. help("pivot_wider")
  62. help("pivot_longer")