# DataMetProcess-vignette

    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.

    file.down <- tempfile()

    info.inmet <- DataMetProcess::list_inmet(
      year="2000",
      filename = file.down
    )
    #> Warning in utils::download.file(url =
    #> base::paste0("https://portal.inmet.gov.br/uploads/dadoshistoricos/", : cannot
    #> open URL 'https://portal.inmet.gov.br/uploads/dadoshistoricos/2000.zip': HTTP
    #> status was '403 Forbidden'
    #> Data for year 2000 is currently unavailable.
    #> Possible reasons: no internet connection, server down, or URL changed.
    #> Please check your connection and try again later.Suggested solution: increase the maximum download time, e.g.:
    #> options(timeout = 1000)
    #> Download failed or file is empty.

    head(info.inmet)
    #> NULL

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.

    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()`.

    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.
    #>         Data  Hora precipita_o_total_hor_rio_mm
    #> 1 2008-01-01 00:00                            0
    #> 2 2008-01-01 01:00                            0
    #> 3 2008-01-01 02:00                            0
    #> 4 2008-01-01 03:00                            0
    #> 5 2008-01-01 04:00                            0
    #> 6 2008-01-01 05:00                            0

    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.
    #>             Date_Hour precipita_o_total_hor_rio_mm
    #> 1 2007-12-31 21:00:00                            0
    #> 2 2007-12-31 22:00:00                            0
    #> 3 2007-12-31 23:00:00                            0
    #> 4 2008-01-01 00:00:00                            0
    #> 5 2008-01-01 01:00:00                            0
    #> 6 2008-01-01 02:00:00                            0

# 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"`.

    df.new <- df
    df.new$Date_Hour <- as.Date(df$Date_Hour)

### Daily

    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.
    #> # A tibble: 6 × 2
    #>   Date       precipita_o_total_hor_rio_mm
    #>   <date>                            <dbl>
    #> 1 2007-12-31                          0
    #> 2 2008-01-01                          2.6
    #> 3 2008-01-02                          1.2
    #> 4 2008-01-03                          0
    #> 5 2008-01-04                          0
    #> 6 2008-01-05                         14.2

### Monthly

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

    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.
    #> # A tibble: 6 × 2
    #>   Date       precipita_o_total_hor_rio_mm
    #>   <date>                            <dbl>
    #> 1 2007-12-01                          0
    #> 2 2008-01-01                        183.
    #> 3 2008-02-01                        191.
    #> 4 2008-03-01                        174.
    #> 5 2008-04-01                         98.8
    #> 6 2008-05-01                         31.6

### Yearly

    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.
    #> # A tibble: 2 × 2
    #>    Date precipita_o_total_hor_rio_mm
    #>   <dbl>                        <dbl>
    #> 1  2007                           0
    #> 2  2008                        1105.

# 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

     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)
    #>         Date       es       ea     Delta      gamma        Rn        u2
    #> 1 2008-01-01 3.137713 2.197654 0.1871193 0.06096587 10.534126 1.3163939
    #> 2 2008-01-02 2.876395 2.200442 0.1734490 0.06101973  9.618539 1.4959022
    #> 3 2008-01-03 2.913023 1.997751 0.1753750 0.06113810 10.745757 1.9072752
    #> 4 2008-01-04 2.942971 1.919112 0.1769474 0.06115673 13.338659 1.6604514
    #> 5 2008-01-05 2.841886 2.108964 0.1716313 0.06105032 10.076548 1.5033817
    #> 6 2008-01-06 2.386476 2.130885 0.1473453 0.06096122  8.308098 0.9947749
    #>        ET0
    #> 1 3.748383
    #> 2 3.269383
    #> 3 3.956928
    #> 4 4.688711
    #> 5 3.448512
    #> 6 2.389488