---
title: "Modify Weather File"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{modify-weather-file}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This vignette shows how to apply monthly changes into a weather file for climate scenarios.

```{r setup}
library(weaana)
```

The daily weather records are stored in the apsim format.  

```{r read-weather-file}
file <- system.file("extdata/WeatherRecordsDemo1.met", package = "weaana")
# Read weather file
met <- readWeatherRecords(file)
# Get records in data.frame
records <- getWeatherRecords(met)
# Calculate month from weather records
month <- as.numeric(format(records$date, '%m'))
   
```

The monthly changes of future climate can be obtained from future climate scenarios.

```{r month-changes}
set.seed(1)
# maximum and minimum temperature changes are absolute values
tasmax <- (runif(12) - 0.5) * 2
tasmin <- (runif(12) - 0.5) * 2
# rain changes are a ratio
pr <- (runif(12) - 0.5) / 10
```

Add the monthly changes into weather records
```{r modify-weather-file}
new_maxt <- records$maxt + tasmax[month]
new_mint <- records$mint + tasmin[month]
new_rain <- records$rain * pr[month]
changeWeatherRecords(met, maxt = new_maxt)
changeWeatherRecords(met, mint = new_mint)
changeWeatherRecords(met, rain = new_rain)
```

Finally the new met file is saved into a new file
```{r save-file, eval=FALSE}
writeWeatherRecords(met, "new-file.met")
```




