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

https://github.com/APSIMInitiative/ApsimX · R · 95 lines · 29 code · 53 blank · 13 comment · 0 complexity · 559e005548b0e4dc16eff4640fe581e4 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. raw1 <- read_xlsx(path, sheet = "2016-17", range = "A4:M76")
  10. raw2 <- read_xlsx(path, sheet = "2016-17", range = "P4:AB52")
  11. raw <- bind_rows(raw1,raw2)
  12. # remove the calculations for volumetric water and gravametric as the excel spreadsheet formulas are a bit suspect.
  13. data <- raw %>% select(-10,-12,-13)
  14. # rename variable to get rid of spaces in column names
  15. data <- data %>% rename("Date" = DATE, "soil_core_cm" = `Soil Core cm`, "PD" = `Planting Date`, "depth" = DEPTH,
  16. "wet_weight_g" = `WET WEIGHT`, "dry_weight_g" = `DRY WEIGHT`, "water_weight_g" = DIFFERENCE,
  17. "water_gravimetric" = `%` , "bulk_density" = `Bulk Density`)
  18. # do the volumetric calculations yourself.
  19. data <- data %>% mutate(sw_volumetric = water_gravimetric * bulk_density)
  20. # now get rid of columns we don't need
  21. data <- data %>% select(-wet_weight_g, -dry_weight_g, -water_weight_g, -water_gravimetric)
  22. # change the PD column (Planting Date) to sowing.
  23. data <- data %>% rename("sowing" = PD)
  24. data$sowing <- paste("S", data$sowing, sep = "")
  25. # turn "depth" column into layer_no
  26. data <- data %>% separate(col = depth, into = c("top", "bottom"), sep = "-", remove = TRUE, convert = TRUE)
  27. # create a column with the layer number for each layer
  28. group_rep <- data %>% group_by(Date, sowing, Rep)
  29. group_rep <- group_rep %>% mutate(layer_no = row_number())
  30. data <- ungroup(group_rep)
  31. # take the average of all the replicates.
  32. group_layer_no <- data %>% group_by(Date, sowing, layer_no)
  33. group_layer_no <- group_layer_no %>% summarise(depth = first(soil_core_cm), sw_volumetric = mean(sw_volumetric), bulk_density = mean(bulk_density) )
  34. data <- ungroup(group_layer_no)
  35. # add year column
  36. data <- data %>% rename("date" = Date)
  37. data <- data %>% mutate(year = 2016)
  38. data <- data %>% select(year, sowing, date, everything())
  39. data <- data %>% rename("sw" = sw_volumetric, "bd" = bulk_density)
  40. # turn (depth, sw, bd) rows for each layer number into columns so we can compare with simulation results.
  41. sw2016 <- data %>% pivot_wider(names_from = layer_no, values_from = c(depth, sw, bd))
  42. sw2016 <- sw2016 %>% arrange(year, sowing, date)