wdFunctions.R 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # * Author: Bangyou Zheng ([email protected])
  2. # * Created: 04:04 PM Tuesday, 21 May 2013
  3. # * Copyright: AS IS
  4. # *
  5. # Crown temperature
  6. wdCrownTemperature <- function()
  7. {
  8. library(lattice)
  9. h_snow <- seq(0, 10, by=2)
  10. res <- NULL
  11. for (i in 1:length(h_snow))
  12. {
  13. x <- -10:0
  14. y <- 2+x*(0.4+0.0018*(h_snow[i]-15)^2)
  15. x <- c(x,0:10)
  16. y <- c(y,0:10)
  17. r <- NULL
  18. r$x <- x
  19. r$y <- y
  20. r$h <- h_snow[i]
  21. r <- as.data.frame(r)
  22. res <- rbind(res,r)
  23. }
  24. res <- as.data.frame(res)
  25. key <- list(lines = list(col = length(h_snow):1,lty = length(h_snow):1),
  26. text = list(lab = as.character(rev(h_snow))),
  27. corner = c(1,0))
  28. p <- xyplot(y ~ x, groups = h, data = res, type = "l", key = key, col = 1:length(h_snow),
  29. lty = 1:length(h_snow),
  30. xlab = expression(paste("Air Temperature", ~"("*degree*"C)")),
  31. ylab = expression(paste("Crown Temperature", ~"("*degree*"C)")))
  32. p
  33. }
  34. # Photoperiod
  35. wdPhotoPeriod <- function()
  36. {
  37. library(lattice)
  38. ps <- c(0,0.5,1,2,3,4,5)
  39. res <- NULL
  40. for (i in 1:length(ps))
  41. {
  42. x <- 0:20
  43. y <- 1-0.002*ps[i]*(20-x)*(20-x)
  44. r <- NULL
  45. r$x <- x
  46. r$y <- y
  47. r$ps <- ps[i]
  48. r <- as.data.frame(r)
  49. res <- rbind(res,r)
  50. }
  51. res <- as.data.frame(res)
  52. res$y[res$y<0] <- 0
  53. key <- list(lines = list(col = 1:length(ps),lty = 1:length(ps)),
  54. text = list(lab = as.character(ps)),
  55. corner = c(1,0))
  56. p <- xyplot(y ~ x, groups = ps, data = res, type = "l", key = key, col = 1:length(ps),
  57. lty = 1:length(ps),
  58. xlab = "Day length (h)",
  59. ylab = expression(Photoperiod~factor~(f[p])))
  60. p
  61. }
  62. # Vernalisation
  63. wdVernalisation <- function()
  64. {
  65. library(lattice)
  66. minT <- seq(-10, 15, 0.2)
  67. maxT <- seq(0, 40, 0.2)
  68. gridt <- expand.grid(mint = minT, maxt = maxT)
  69. gridt <- gridt[gridt$maxt - gridt$mint > 3 & gridt$maxt > 0 & gridt$mint < 15,]
  70. cmint <- gridt$mint
  71. cmint[cmint<0] <- 2 + cmint[cmint<0] * (0.4 + 0.0018 * 15 * 15)
  72. gridt$crownt <- (cmint + gridt$maxt) / 2
  73. gridt$v = pmin(1.4 - 0.0778 * gridt$crownt,
  74. 0.5 + 13.44 * (13.44 * gridt$crownt) / ((gridt$maxt - gridt$mint + 3)^2 ))
  75. gridt$v[gridt$v<0] <- 0
  76. # key <- list(lines = list(col = 1:length(ps),lty = 1:length(ps)),
  77. # text = list(lab = as.character(ps)),
  78. # corner = c(1,0))
  79. p <- levelplot(gridt$v ~ gridt$maxt + gridt$mint,
  80. colorkey = T,
  81. cuts = 10,
  82. xlim = c(0,40),
  83. ylim = c(-10,15),
  84. xlab = expression(paste("Maximum Temperature", ~"("*degree*"C)")),
  85. ylab = expression(paste("Minimum Temperature", ~"("*degree*"C)")),
  86. region = TRUE,
  87. contour =TRUE,
  88. col.regions = rev(heat.colors(20))
  89. )
  90. p
  91. }
  92. # devernalisation
  93. wdDevernalisation <- function()
  94. {
  95. library(lattice)
  96. x <- 30:50
  97. y <- 0.5*(x - 30)
  98. p <- xyplot(y ~ x, type = "l",
  99. xlab = expression(paste("Maximum Temperature", ~"("*degree*"C)")),
  100. ylab = expression(Devernalization~(~Delta~V)))
  101. p
  102. }
  103. # Vernalisation factor
  104. wdVernalisationFactor <- function()
  105. {
  106. library(lattice)
  107. rs <- c(-0.055,0,0.5,1,1.5,2,3,4,5)
  108. res <- NULL
  109. for (i in 1:length(rs))
  110. {
  111. x <- 0:50
  112. y <- 1-(0.0054545 * rs[i] + 0.0003) * (50-x)
  113. r <- NULL
  114. r$x <- x
  115. r$y <- y
  116. r$rs <- rs[i]
  117. r <- as.data.frame(r)
  118. res <- rbind(res,r)
  119. }
  120. res <- as.data.frame(res)
  121. res$y[res$y<0] <- 0
  122. key <- list(lines = list(col = 1:length(rs),lty = 1:length(rs)),
  123. text = list(lab = as.character(rs)),
  124. corner = c(1,0))
  125. p <- xyplot(y ~ x, groups = rs, data = res, type = "l", key = key, col = 1:length(rs),
  126. lty = 1:length(rs),
  127. xlab = "Cumulated vernalization (V)",
  128. ylab = expression(Vernalization~factor~(f[v])))
  129. p
  130. }
  131. # Carbon dioxide factor
  132. wdCarbonDioxideFactor <- function()
  133. {
  134. library(lattice)
  135. tmean <- seq(from = 0, to = 30, by = 10)
  136. co2 <- seq(350, 700, 10)
  137. tmean.len <- length(tmean)
  138. tmean <- rep(tmean, each = length(co2))
  139. co2 <- rep(co2, times = tmean.len)
  140. c1 <- (163 - tmean) / (5 - 0.1 * tmean)
  141. fc <- (co2 - c1) * (350 + 2 * c1) / ((co2 + 2 * c1) * (350 - c1))
  142. pd <- cbind(tmean = tmean, co2 = co2, fc = fc)
  143. pd <- as.data.frame(pd)
  144. key <- list(lines = list(col = 1:tmean.len,lty = 1:tmean.len),
  145. text = list(lab = as.character(seq(from = 0, to = 30, by = 10 ))),
  146. corner = c(0,1))
  147. p <- xyplot(fc ~ co2, data = pd, groups = tmean, type = "l", lty = 1:tmean.len,
  148. xlab = 'Carbon dioxide concentration (ppm)',
  149. ylab = expression(Factor~of~cardon~dioxide~~(f[c])),
  150. key = key, col = 1:tmean.len
  151. )
  152. p
  153. }
  154. # Function for leaf/stem/pod nitrogen
  155. wdNitrogenConcentration <- function()
  156. {
  157. leaf <- wdVisXY(wheat_xml,
  158. "x_stage_code",
  159. c("y_n_conc_max_leaf",
  160. "y_n_conc_crit_leaf",
  161. "y_n_conc_min_leaf"),
  162. xlab = "Stage code",
  163. ylab = "Nitrogen concentration",
  164. keylab = c("maximum", "critical","minimum"),
  165. mtext = 'Leaf')
  166. stem <- wdVisXY(wheat_xml,
  167. "x_stage_code",
  168. c("y_n_conc_max_stem",
  169. "y_n_conc_crit_stem",
  170. "y_n_conc_min_stem"),
  171. xlab = "Stage code",
  172. ylab = "Nitrogen concentration",
  173. keylab = c("maximum", "critical","minimum"),
  174. mtext = 'Stem')
  175. pod <- wdVisXY(wheat_xml,
  176. "x_stage_code",
  177. c("y_n_conc_max_pod",
  178. "y_n_conc_crit_pod",
  179. "y_n_conc_min_pod"),
  180. xlab = "Stage code",
  181. ylab = "Nitrogen concentration",
  182. keylab = c("maximum", "critical","minimum"),
  183. mtext = 'Pod')
  184. return (list(leaf=leaf, stem=stem, pod=pod))
  185. }
  186. # stemGrowthStructuralFractionStage
  187. wdStemGrowthStructuralFraction <- function()
  188. {
  189. library(lattice)
  190. x <- c(0, 7, 7.01, 11)
  191. y <- c(.65, .65, 0.0, 0)
  192. p <- xyplot(y ~ x, type = "b",
  193. xlab = "Stage codes",
  194. ylab = "Fraction of structural biomass of stem" )
  195. p
  196. }
  197. # Plot kl factoring
  198. wdKLFactoring <- function(doc)
  199. {
  200. varKl <- function(doc, name, values, label = name)
  201. {
  202. getValue <- function(doc, name)
  203. {
  204. temp <- xpathApply(doc, paste("//", name, sep = ""))
  205. xvalue <- as.numeric(xmlValue(temp[[1]]))
  206. xvalue
  207. }
  208. res <- pmin(1, getValue(doc, sprintf('%sA', name)) *
  209. exp(getValue(doc, sprintf('%sB', name)) * values))
  210. res <- as.data.frame(list(var = label,
  211. x = values,
  212. y = res))
  213. return(res)
  214. }
  215. df <- rbind(varKl(doc, 'Cl', 0:1500, 'CL'),
  216. varKl(doc, 'ESP', 0:80),
  217. varKl(doc, 'EC', seq(0, 4, by = 0.1)))
  218. library(ggplot2)
  219. p <- ggplot(df) + geom_line(aes(x, y)) +
  220. facet_wrap(~var, ncol = 1, scales = 'free_x') +
  221. ylab('KL factor') + xlab('Variable values') +
  222. theme_bw()
  223. p
  224. }