## ----setup--------------------------------------------------------------------
library(dplyr)
library(ggplot2)
library(apsimwise)


## flowchart LR
## 
##     A[apsimwise<br/>User-facing interface]
## 
##     B[rapsimng.*<br/>Crop specific parameters]
## 
##     C[tidyweather<br/>Generic weather and thermal time functions]
## 
##     A --> B
##     B --> C

## ----read-weather-------------------------------------------------------------
met_files <- system.file(
	c("extdata/ppd_72150.met", "extdata/ppd_40082.met"),
	package = "tidyweather"
)
records <- bind_rows(
	read_weather(met_files[1]),
	read_weather(met_files[2])
)


## ----tt-mean------------------------------------------------------------------
tt_mean <- records |>
    mutate(
		tt = tidyweather::thermal_time(
			mint = mint,
			maxt = maxt,
			x_temp = c(0, 26, 37),
			y_temp = c(0, 26, 0)
		)
    ) |>
	mutate(method = "mean")


## ----tt-3hour-----------------------------------------------------------------
tt_3hour <- records |>
	mutate(
		tt = tidyweather::thermal_time(
			mint = mint,
			maxt = maxt,
			x_temp = c(0, 26, 37),
			y_temp = c(0, 26, 0),
			method = "3hr"
		)
	) |>
	mutate(method = "3hr")


## ----tt-hourly----------------------------------------------------------------
tt_hourly <- records |>
	group_by(name) |>
	mutate(
		tt = tidyweather::thermal_time(
			mint = mint,
			maxt = maxt,
			x_temp = c(0, 26, 37),
			y_temp = c(0, 26, 0),
			date = date,
			latitude = latitude[1],
			method = "HourlySinPpAdjusted"
		)
	) |>
	mutate(method = "hourly")


## ----fig-tt, fig.width = 8, fig.height = 6------------------------------------
tt_all <- bind_rows(tt_mean, tt_3hour, tt_hourly)
tt_all |>
	ggplot() +
	geom_point(aes(x = date, y = tt)) +
	theme_bw() +
	facet_grid(method~name)



## ----crop-params--------------------------------------------------------------
rapsimng.wheat::wheat$get("phenology.thermal_time")
rapsimng.canola::canola$get("phenology.thermal_time")
rapsimng.lupin::lupin$get("phenology.thermal_time")


## ----tt-crop-wheat------------------------------------------------------------

tt_wheat <- records |>
	mutate(
		tt = rapsimng.wheat::thermal_time(
			mint = mint,
			maxt = maxt
		)
	) |>
	mutate(crop = "wheat")


## ----tt-crop-lupin------------------------------------------------------------

tt_lupin <- records |>
	group_by(name) |>
	mutate(
		tt = rapsimng.lupin::thermal_time(
			mint = mint,
			maxt = maxt,
			date = date,
			latitude = latitude[1]
		)
	) |>
	mutate(crop = "lupin")


## ----tt-crop-canola-----------------------------------------------------------
tt_canola <- records |>
	mutate(
		tt = rapsimng.canola::thermal_time(
			mint = mint,
			maxt = maxt
		)
	) |>
	mutate(crop = "canola")


## ----fig-tt-crop, fig.width = 8, fig.height = 6-------------------------------
tt_crop <- bind_rows(tt_wheat, tt_lupin, tt_canola)
tt_crop |>
	ggplot() +
	geom_point(aes(x = date, y = tt)) +
	theme_bw() +
	facet_grid(crop~name)


## ----tt2-crop-----------------------------------------------------------------
tt_wheat <- records |>
	mutate(
		tt = thermal_time(
			mint = mint,
			maxt = maxt,
			crop = "wheat"
		)
	) |>
	mutate(crop = "wheat")
tt_lupin <- records |>
	group_by(name) |>
	mutate(
		tt = thermal_time(
			mint = mint,
			maxt = maxt,
			date = date,
			latitude = latitude[1],
			crop = "lupin"
		)
	) |>
	mutate(crop = "lupin")
tt_canola <- records |>
	mutate(
		tt = thermal_time(
			mint = mint,
			maxt = maxt,
			crop = "canola"
		)
	) |>
	mutate(crop = "canola")


## ----fig-tt2-crop, fig.width = 8, fig.height = 6------------------------------
tt_all <- bind_rows(tt_wheat, tt_lupin, tt_canola)
tt_all |>
	ggplot() +
	geom_point(aes(x = date, y = tt)) +
	theme_bw() +
	facet_grid(crop~name)



## ----tt-override-function-call, eval = FALSE----------------------------------
# records |>
# 	group_by(name) |>
# 	mutate(
# 		tt = thermal_time(
# 			mint = mint,
# 			maxt = maxt,
# 			date = date,
# 			latitude = latitude[1],
# 			crop = "lupin",
# 			x_temp = c(0, 25, 35),
# 			y_temp = c(0, 25, 0)
# 		)
# 	)


## ----tt-override-crop-package, eval = FALSE-----------------------------------
# crop("lupin")$set(
# 	"phenology.thermal_time.x" = c(0, 25, 35),
# 	"phenology.thermal_time.y" = c(0, 25, 0)
# )
# records |>
# 	group_by(name) |>
# 	mutate(
# 		tt = thermal_time(
# 			mint = mint,
# 			maxt = maxt,
# 			date = date,
# 			latitude = latitude[1],
# 			crop = "lupin"
# 		)
# 	)

