---
title: "DataMetProcess-vignette"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{DataMetProcess-vignette}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(DataMetProcess)
```

# Collecting packet information with `list_inmet()`


First, we add a pathway for the file to be downloaded and converted into useful information. The file is retained, and its content can be extracted later. For this example, we will use a temporary file that doesn't have much influence at this stage.

```{r }
file.down <- tempfile()

info.inmet <- DataMetProcess::list_inmet(
  year="2000",
  filename = file.down
)

head(info.inmet)
```

Now we have an object containing a list of files in `$Addresses` within the path shown in `$Saved`, and a structured table with information extracted from the file names.

Having done that, now we can make use of a very useful function from the `utils` package, called `unzip()`. At this point, we can also use the files parameter of the `unzip()` function to extract only the files of interest. Please refer to ?utils::unzip for more details.

```{r eval=FALSE}

file.save <- tempfile()

unzip.file <-
   utils::unzip(
     zipfile = file.down, #or info.inmet$Saved
     exdir = file.save
   )

#specific file
unzip.file <-
   utils::unzip(
     zipfile = file.down, #or info.inmet$Saved
     files = info.inmet$Adresses[2,],
     exdir = file.save
   )
```

# Time zone correction with `adjustDate()`

To perform the time zone correction, we can use the `adjustDate()` function. To do this, we will use an example file provided with the package, At this point, we could indeed use a file obtained in the previous topic. You can check the available time zones by using `OlsonNames()`.

```{r }
address <-
 base::system.file("extdata",
                    "ex1_inmet.CSV",
                    package = "DataMetProcess")

df <-
  read.table(
    address,
    h=TRUE,
    sep = ";",
    dec = ",",
    skip = 8, 
    na.strings = -9999,
    check.names = FALSE
  ) #see ?read.table for more details...

#Converting to R standard (when necessary)
df$Data = as.Date(df$Data,format = "%d/%m/%Y")

head(df[1:3]) #We are only viewing a part of it.

df <-
  adjustDate(df,
             colnames(df)[1],
             colnames(df)[2],
             fuso = "America/Bahia")

#date and time are now in a single column
head(df[1:2]) #We are only viewing a part of it.
```

# Calculation of daily, monthly and annual scales with `calculateDMY()`

We can then calculate daily, monthly, and yearly data using the `calculateDMY()` function. First, we adjust the data_hora column defined by the previous function to ensure there are no differences between the same dates. Then, we populate the parameters with the column names in string format ("string") and define the type as `"Daily," "Monthly," or "Yearly"`.

```{r }
df.new <- df
df.new$Date_Hour <- as.Date(df$Date_Hour)
```

### Daily

```{r }
df.daily <-
  calculateDMY(
    data = df.new,
    col_date = colnames(df)[c(1)],
    col_sum = colnames(df)[c(2,6)], #simplest way to pass column names as string
    col_mean = colnames(df)[-c(1,2,6)], #remove the previous steps in the parameter above
    type = "Daily"
  )

head(df.daily[1:2]) #We are only viewing a part of it.

```

### Monthly

We use the processed `df.daily` file from the previous topic.

```{r }
df.monthly <-
  calculateDMY(
    data = df.daily,
    col_date = colnames(df.daily)[c(1)],
    col_sum = colnames(df.daily)[c(2)],
    col_mean = colnames(df.daily)[-c(1,2)],
    type = "Monthly"
  )

head(df.monthly[1:2]) #We are only viewing a part of it.

```

### Yearly

```{r }
df.yearly <-
  calculateDMY(
    data = df.monthly,
    col_date = colnames(df.monthly)[c(1)],
    col_sum = colnames(df.monthly)[c(2)],
    col_mean = colnames(df.monthly)[-c(1,2)],
    type = "Yearly"
  )

head(df.yearly[1:2]) #We are only viewing a part of it.
```

# Reference evapotranspiration with `calculateETrefPM()`

We can calculate reference evapotranspiration for daily data using the `calculateETrefPM()` function. This function is based on the FAO Penman-Monteith method, according to:"

> Allen, R.G., Pereira, L.S., Raes, D., Smith, M., 1998. Crop evapotranspiration – guidelines for computing crop water requirements – FAO Irrigation and Drainage Paper 56. FAO, 1998. ISBN 92-5-104219-5

```{r }
 address <-
  base::system.file("extdata",
                     "ex2_daily.CSV",
                     package = "DataMetProcess")

 df <- read.table(
 address,
 h = TRUE,
 sep = ";"
 )

 #converting to Mj/m
 df$radiacao_global_kj_m <- df$radiacao_global_kj_m/1000
 colnames(df)[3] <- "radiacao_global_mj_m"

df.Eto <-
 calculateETrefPM(
   data = df,
   lat = -21.980353,
   alt = 859.29,
   za = 10,
   DAP = 1,
   date = colnames(df)[1],
   Ta = colnames(df)[7],
   G = NULL,
   RH = colnames(df)[15],
   Rg = colnames(df)[3],
   AP = colnames(df)[4],
   WS = colnames(df)[18],
   Kc = NULL
 )
 
 head(df.Eto)
 
```



