---
title: ascotraceR
output:
  rmarkdown::html_document:
    theme: journal
vignette: >
  %\VignetteIndexEntry{ascotraceR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width = 7, fig.height = 7)
```

## Introduction to _ascotraceR_

_ascotraceR_ is an R package of the 'ascotraceR' model developed to simulate the spread of Ascochyta blight in a chickpea field over a growing season.
Parameters and variables used in the model were mostly derived from the literature and were subjected to validation in the field.
The model uses daily weather data to simulate disease spread and a set of weather data is included with the package for demonstration purposes.

### Getting started

Load the libraries.

```{r, load-libs, message=FALSE}
library("ascotraceR")
library("lubridate")
library("ggplot2")
library("data.table")
```

Import the weather data

```{r load-weather}
# weather data
Billa_Billa <- fread(
  system.file(
    "extdata",
    "2020_Billa_Billa_weather_data_ozforecast.csv",
    package = "ascotraceR"
  )
)

# format time column
Billa_Billa[, local_time := dmy_hm(local_time)]

# specify the station coordinates of the Billa Billa weather station
Billa_Billa[, c("lat", "lon") := .(-28.1011505, 150.3307084)]

head(Billa_Billa)
```

### Wrangle weather data 

A function, `format_weather()`, is provided to convert raw weather data into the format appropriate for the model.
It is mandatory to format weather data before running the model.
Time zone can also be set manually using `time_zone` argument.
If latitude and longitude are not supplied in the raw weather data, a a separate CSV file listing latitude and longitude can be supplied to meet this requirement (see `?format_weather` for more details).

```{r format-weather}
Billa_Billa <- format_weather(
  x = Billa_Billa,
  POSIXct_time = "local_time",
  temp = "mean_daily_temp",
  ws = "ws",
  wd_sd = "wd_sd",
  rain = "rain_mm",
  wd = "wd",
  station = "location",
  time_zone = "Australia/Brisbane",
  lon = "lon",
  lat = "lat"
)
```

### Simulate Ascochyta blight spread

A function, `trace_asco()`, is provided to simulate the spread of Ascochyta blight in a chickpea field over a growing season.
The inputs needed to run the function include weather data, paddock length and width, sowing and harvest dates, chickpea growing points replication rate, primary infection intensity, latent period, number of conidia produced per lesion and the location of primary infection foci (centre or random).
The model output is a nested list with items for each day of the model run.
See the help file for `trace_asco()` for more information.

```{r trace-asco}
# Predict Ascochyta blight spread for the year 2020 at Billa Billa
traced <- trace_asco(
  weather = Billa_Billa,
  paddock_length = 20,
  paddock_width = 20,
  initial_infection = "2020-07-17",
  sowing_date = "2020-06-04",
  harvest_date = "2020-10-27",
  time_zone = "Australia/Brisbane",
  seeding_rate = 40,
  gp_rr = 0.0065,
  spores_per_gp_per_wet_hour = 0.6,
  latent_period_cdd = 150,
  primary_inoculum_intensity = 100,
  primary_infection_foci = "centre"
)
```

### Tidy up or summarise the model output

Functions `tidy_trace()` and `summarise_trace()` have been provided to tidy up and summarise the model output

## Tidy up the model output 

```{r tidy}
tidied <- tidy_trace(traced)
tidied
```


### Summarise the model output

```{r summarise}
summarised <- summarise_trace(traced)
summarised
```

## Plot using _ggplot2_

Plot the number of infectious growing points on day 132.

```{r plot}
ggplot(data = subset(tidied, i_day == 132),
       aes(x = x, y = y, fill = infectious_gp)) +
  geom_tile()
```

