---
title: "fifo"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{fifo}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding
  %\VignetteDepends{dplyr}
---



## Load Libraries


``` r
library(fifo)
library(dplyr)
```

## Create Locations in WA and NSW


``` r
# create a list of locations for use in testing
locs <- list(
  "Merredin" = c(x = 118.28, y = -31.48),
  "Corrigin" = c(x = 117.87, y = -32.33),
  "Tamworth" = c(x = 150.84, y = -31.07)
)
```

## Extract the GRDC AgroEcological Zones

See `?extract_ae_zone()` for more help on how to use this function.


``` r
z <- extract_ae_zone(xy = locs)
```

## Extract the Soil Order

See `?extract_daas_soil_order()` for more help on how to use this function.


``` r
s <- extract_daas_soil_order(xy = locs)
#> Reading layer `soilAtlas2M_ASC_Conversion' from data source 
#>   `/vsizip///var/folders/vz/txwj1tx51txgw7zv_b5c5_3m0000gn/T//RtmpM5Vn9r/6f804e8b-2de9-4c88-adfa-918ec327c32f.zip/SoilAtlas2M_ASC_Conversion_v01/soilAtlas2M_ASC_Conversion.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 22584 features and 7 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 112.8959 ymin: -43.63287 xmax: 153.6362 ymax: -10.49096
#> Geodetic CRS:  GDA94
```

## Get Weather Data for these Locations in 2020

Using the previously created list of GPS points, fetch station observation weather data from SILO for 2020.
This is a non-working example, replace `your_api_key` with your email address below.
See `?extract_patched_point()` for more help on how to use this function.


``` r
three_sites <-
  extract_patched_point(
    xy = locs,
    start_date = "20200101",
    end_date = "20201231"
  )
#> You have requested station observation data but some rows in this
#> dataset have data codes for interpolated data.
#> Check the 'data_source' columns and `get_patched_point()` or
#> `get_data_drill()` documentation for further details on codes and
#> references.
#> 
#> You have requested station observation data but some rows in this
#> dataset have data codes for interpolated data.
#> Check the 'data_source' columns and `get_patched_point()` or
#> `get_data_drill()` documentation for further details on codes and
#> references.
```

## Join the Weather Data with AE Zone, Soil Order and Site Information

Now using `dplyr::left_join()`, create a single `data.frame()` of the location, GPS coordinates, agroecological zone and weather data.


``` r
full_dat <- left_join(z, three_sites) |>
  left_join(s)
#> Joining with `by = join_by(location, x, y)`
#> Joining with `by = join_by(location, x, y)`
full_dat
#>       location      x      y       ae_zone station_code         station_name
#>         <char>  <num>  <num>        <char>       <char>               <char>
#>    1: Corrigin 117.87 -32.33    WA Central       010536             Corrigin
#>    2: Corrigin 117.87 -32.33    WA Central       010536             Corrigin
#>    3: Corrigin 117.87 -32.33    WA Central       010536             Corrigin
#>    4: Corrigin 117.87 -32.33    WA Central       010536             Corrigin
#>    5: Corrigin 117.87 -32.33    WA Central       010536             Corrigin
#>   ---                                                                       
#> 1094: Tamworth 150.84 -31.07 NSW NE/Qld SE       055325 Tamworth Airport AWS
#> 1095: Tamworth 150.84 -31.07 NSW NE/Qld SE       055325 Tamworth Airport AWS
#> 1096: Tamworth 150.84 -31.07 NSW NE/Qld SE       055325 Tamworth Airport AWS
#> 1097: Tamworth 150.84 -31.07 NSW NE/Qld SE       055325 Tamworth Airport AWS
#> 1098: Tamworth 150.84 -31.07 NSW NE/Qld SE       055325 Tamworth Airport AWS
#>       distance  year   day       date air_tmax air_tmax_source air_tmin
#>          <num> <num> <int>     <Date>    <num>           <int>    <num>
#>    1:      0.3  2020     1 2020-01-01     37.4              25     15.3
#>    2:      0.3  2020     2 2020-01-02     23.0               0     16.0
#>    3:      0.3  2020     3 2020-01-03     26.0               0     12.5
#>    4:      0.3  2020     4 2020-01-04     34.0               0      8.0
#>    5:      0.3  2020     5 2020-01-05     38.0               0     12.0
#>   ---                                                                  
#> 1094:      0.6  2020    27 2020-12-27     29.8               0     15.0
#> 1095:      0.6  2020    28 2020-12-28     26.8               0     18.3
#> 1096:      0.6  2020    29 2020-12-29     28.9               0     17.5
#> 1097:      0.6  2020    30 2020-12-30     29.4               0     14.9
#> 1098:      0.6  2020    31 2020-12-31     30.0               0     16.1
#>       air_tmin_source  elev_m et_morton_actual et_morton_actual_source
#>                 <int>  <char>            <num>                   <int>
#>    1:              25 295.0 m              3.3                      26
#>    2:              25 295.0 m              6.4                      26
#>    3:               0 295.0 m              3.4                      26
#>    4:               0 295.0 m              4.6                      26
#>    5:               0 295.0 m              7.4                      26
#>   ---                                                                 
#> 1094:               0 394.9 m              5.5                      26
#> 1095:               0 394.9 m              4.1                      26
#> 1096:               0 394.9 m              2.6                      26
#> 1097:               0 394.9 m              6.6                      26
#> 1098:               0 394.9 m              6.2                      26
#>       et_morton_potential et_morton_potential_source et_morton_wet
#>                     <num>                      <int>         <num>
#>    1:                11.0                         26           7.2
#>    2:                 6.4                         26           6.4
#>    3:                 7.3                         26           5.4
#>    4:                 8.4                         26           6.5
#>    5:                 8.8                         26           8.1
#>   ---                                                             
#> 1094:                 6.9                         26           6.2
#> 1095:                 4.9                         26           4.5
#> 1096:                 5.3                         26           3.9
#> 1097:                 6.6                         26           6.6
#> 1098:                 8.1                         26           7.1
#>       et_morton_wet_source et_short_crop et_short_crop_source et_tall_crop
#>                      <int>         <num>                <int>        <num>
#>    1:                   26           8.0                   26         10.4
#>    2:                   26           4.4                   26          4.7
#>    3:                   26           5.4                   26          6.7
#>    4:                   26           7.0                   26          8.9
#>    5:                   26           7.3                   26          9.0
#>   ---                                                                     
#> 1094:                   26           5.3                   26          6.3
#> 1095:                   26           3.5                   26          4.2
#> 1096:                   26           3.8                   26          4.8
#> 1097:                   26           5.2                   26          5.9
#> 1098:                   26           6.0                   26          7.1
#>       et_tall_crop_source evap_comb evap_comb_source evap_morton_lake
#>                     <int>     <num>            <int>            <num>
#>    1:                  26       9.5               25              8.1
#>    2:                  26      10.4                0              6.6
#>    3:                  26       8.0                0              6.2
#>    4:                  26       8.0                0              7.5
#>    5:                  26      10.6                0              8.4
#>   ---                                                                
#> 1094:                  26       5.5               25              6.3
#> 1095:                  26       3.6               25              4.5
#> 1096:                  26       2.6               25              4.0
#> 1097:                  26       4.3               25              6.8
#> 1098:                  26       5.0               25              7.3
#>       evap_morton_lake_source evap_pan evap_pan_source evap_syn evap_syn_source
#>                         <int>    <num>           <int>    <num>           <int>
#>    1:                      26      9.5              25     11.1              26
#>    2:                      26     10.4               0      7.2              26
#>    3:                      26      8.0               0      8.3              26
#>    4:                      26      8.0               0     10.1              26
#>    5:                      26     10.6               0     10.4              26
#>   ---                                                                          
#> 1094:                      26      5.5              25      6.7              26
#> 1095:                      26      3.6              25      4.5              26
#> 1096:                      26      2.6              25      5.1              26
#> 1097:                      26      4.3              25      6.5              26
#> 1098:                      26      5.0              25      7.5              26
#>        extracted station_y station_x   mslp mslp_source radiation
#>           <Date>     <num>     <num>  <num>       <int>     <num>
#>    1: 2025-11-29  -32.3292  117.8733 1012.7          25      31.0
#>    2: 2025-11-29  -32.3292  117.8733 1010.8           0      27.1
#>    3: 2025-11-29  -32.3292  117.8733 1017.2           0      26.8
#>    4: 2025-11-29  -32.3292  117.8733 1020.9           0      30.9
#>    5: 2025-11-29  -32.3292  117.8733 1015.9           0      30.8
#>   ---                                                            
#> 1094: 2025-11-29  -31.0742  150.8362 1015.7           0      24.9
#> 1095: 2025-11-29  -31.0742  150.8362 1012.7           0      15.7
#> 1096: 2025-11-29  -31.0742  150.8362 1011.4           0      14.0
#> 1097: 2025-11-29  -31.0742  150.8362 1014.2           0      26.4
#> 1098: 2025-11-29  -31.0742  150.8362 1015.8           0      28.7
#>       radiation_source rainfall rainfall_source rh_tmax rh_tmax_source rh_tmin
#>                  <int>    <num>           <int>   <num>          <int>   <num>
#>    1:               42      0.0               0    22.6             26    83.5
#>    2:               42      0.0               0    71.2             26   100.0
#>    3:               42      0.0               0    37.5             26    87.0
#>    4:               42      0.0               0    26.3             26   100.0
#>    5:               42      0.0               0    33.4             26   100.0
#>   ---                                                                         
#> 1094:               42      2.0               0    46.5             26   100.0
#> 1095:               42     10.2               0    61.9             26   100.0
#> 1096:               42      1.0               0    50.0             26    99.6
#> 1097:               42     18.8               0    52.7             26   100.0
#> 1098:               42      0.0               0    44.5             26   100.0
#>       rh_tmin_source    vp vp_deficit vp_deficit_source vp_source
#>                <int> <num>      <num>             <int>     <int>
#>    1:             26  14.5       32.7                26        25
#>    2:             26  20.0        5.2                26         0
#>    3:             26  12.6       14.9                26         0
#>    4:             26  14.0       22.7                26         0
#>    5:             26  22.1       24.1                26         0
#>   ---                                                            
#> 1094:             26  19.5       14.3                26         0
#> 1095:             26  21.8        9.3                26         0
#> 1096:             26  19.9       13.8                26         0
#> 1097:             26  21.6       11.6                26         0
#> 1098:             26  18.9       15.8                26         0
#>       daas_soil_order
#>                <fctr>
#>    1:         Sodosol
#>    2:         Sodosol
#>    3:         Sodosol
#>    4:         Sodosol
#>    5:         Sodosol
#>   ---                
#> 1094:        Dermosol
#> 1095:        Dermosol
#> 1096:        Dermosol
#> 1097:        Dermosol
#> 1098:        Dermosol
```

## Creating a Pipeline

You can use R's native pipe, `|>`, to string the whole set of commands together into a pipeline to ingest, extract information and download weather data for your GPS points.
Here, we will import data from a CSV file, extract the values, fetch the weather data and create a `data.frame` object with all the AE Zone, soil order, weather and site information.

The CSV file is included in this package so that you can run this example yourself by providing your own e-mail address for the SILO API key.

The `left_join` takes the existing `data.frame` that has the location information, AE Zone and soil order as `x = .`.
The `y` for the left join is then the `get_patched_point` and details are provided to that API, again the original `data.frame` that has the `x` and `y` information and `Sitename` are provided as `.$colname`.

The final step is to save the `data.frame` as an object, `complete_data`.


``` r
# create a data.frame of the soil order and AE Zone
locs <- read_csv(system.file(
  "extdata",
  "sample_points.csv",
  package = "fifo",
  mustWork = TRUE
))
#> Rows: 3 Columns: 3
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): site
#> dbl (2): x, y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

soil_zone <- xy |>
  left_join(
    x = extract_ae_zone(xy = locs),
    y = extract_daas_soil_order(xy = locs),
    by = c("x", "y", "location")
  )
#> Reading layer `soilAtlas2M_ASC_Conversion' from data source 
#>   `/vsizip///var/folders/vz/txwj1tx51txgw7zv_b5c5_3m0000gn/T//RtmpM5Vn9r/6f804e8b-2de9-4c88-adfa-918ec327c32f.zip/SoilAtlas2M_ASC_Conversion_v01/soilAtlas2M_ASC_Conversion.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 22584 features and 7 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 112.8959 ymin: -43.63287 xmax: 153.6362 ymax: -10.49096
#> Geodetic CRS:  GDA94

# join the `soil_zone` with weather data from SILO
complete_data <- left_join(
  x = soil_zone,
  y = extract_patched_point(
    xy = locs,
    start_date = "20200101",
    end_date = "20201231"
  )
)
#> You have requested station observation data but some rows in this
#> dataset have data codes for interpolated data.
#> Check the 'data_source' columns and `get_patched_point()` or
#> `get_data_drill()` documentation for further details on codes and
#> references.
#> 
#> You have requested station observation data but some rows in this
#> dataset have data codes for interpolated data.
#> Check the 'data_source' columns and `get_patched_point()` or
#> `get_data_drill()` documentation for further details on codes and
#> references.
#> 
#> Joining with `by = join_by(location, x, y)`

complete_data
#>       location      x      y       ae_zone daas_soil_order station_code
#>         <char>  <num>  <num>        <char>          <fctr>       <char>
#>    1: Corrigin 117.87 -32.33    WA Central         Sodosol       010536
#>    2: Corrigin 117.87 -32.33    WA Central         Sodosol       010536
#>    3: Corrigin 117.87 -32.33    WA Central         Sodosol       010536
#>    4: Corrigin 117.87 -32.33    WA Central         Sodosol       010536
#>    5: Corrigin 117.87 -32.33    WA Central         Sodosol       010536
#>   ---                                                                  
#> 1094: Tamworth 150.84 -31.07 NSW NE/Qld SE        Dermosol       055325
#> 1095: Tamworth 150.84 -31.07 NSW NE/Qld SE        Dermosol       055325
#> 1096: Tamworth 150.84 -31.07 NSW NE/Qld SE        Dermosol       055325
#> 1097: Tamworth 150.84 -31.07 NSW NE/Qld SE        Dermosol       055325
#> 1098: Tamworth 150.84 -31.07 NSW NE/Qld SE        Dermosol       055325
#>               station_name distance  year   day       date air_tmax
#>                     <char>    <num> <num> <int>     <Date>    <num>
#>    1:             Corrigin      0.3  2020     1 2020-01-01     37.4
#>    2:             Corrigin      0.3  2020     2 2020-01-02     23.0
#>    3:             Corrigin      0.3  2020     3 2020-01-03     26.0
#>    4:             Corrigin      0.3  2020     4 2020-01-04     34.0
#>    5:             Corrigin      0.3  2020     5 2020-01-05     38.0
#>   ---                                                              
#> 1094: Tamworth Airport AWS      0.6  2020    27 2020-12-27     29.8
#> 1095: Tamworth Airport AWS      0.6  2020    28 2020-12-28     26.8
#> 1096: Tamworth Airport AWS      0.6  2020    29 2020-12-29     28.9
#> 1097: Tamworth Airport AWS      0.6  2020    30 2020-12-30     29.4
#> 1098: Tamworth Airport AWS      0.6  2020    31 2020-12-31     30.0
#>       air_tmax_source air_tmin air_tmin_source  elev_m et_morton_actual
#>                 <int>    <num>           <int>  <char>            <num>
#>    1:              25     15.3              25 295.0 m              3.3
#>    2:               0     16.0              25 295.0 m              6.4
#>    3:               0     12.5               0 295.0 m              3.4
#>    4:               0      8.0               0 295.0 m              4.6
#>    5:               0     12.0               0 295.0 m              7.4
#>   ---                                                                  
#> 1094:               0     15.0               0 394.9 m              5.5
#> 1095:               0     18.3               0 394.9 m              4.1
#> 1096:               0     17.5               0 394.9 m              2.6
#> 1097:               0     14.9               0 394.9 m              6.6
#> 1098:               0     16.1               0 394.9 m              6.2
#>       et_morton_actual_source et_morton_potential et_morton_potential_source
#>                         <int>               <num>                      <int>
#>    1:                      26                11.0                         26
#>    2:                      26                 6.4                         26
#>    3:                      26                 7.3                         26
#>    4:                      26                 8.4                         26
#>    5:                      26                 8.8                         26
#>   ---                                                                       
#> 1094:                      26                 6.9                         26
#> 1095:                      26                 4.9                         26
#> 1096:                      26                 5.3                         26
#> 1097:                      26                 6.6                         26
#> 1098:                      26                 8.1                         26
#>       et_morton_wet et_morton_wet_source et_short_crop et_short_crop_source
#>               <num>                <int>         <num>                <int>
#>    1:           7.2                   26           8.0                   26
#>    2:           6.4                   26           4.4                   26
#>    3:           5.4                   26           5.4                   26
#>    4:           6.5                   26           7.0                   26
#>    5:           8.1                   26           7.3                   26
#>   ---                                                                      
#> 1094:           6.2                   26           5.3                   26
#> 1095:           4.5                   26           3.5                   26
#> 1096:           3.9                   26           3.8                   26
#> 1097:           6.6                   26           5.2                   26
#> 1098:           7.1                   26           6.0                   26
#>       et_tall_crop et_tall_crop_source evap_comb evap_comb_source
#>              <num>               <int>     <num>            <int>
#>    1:         10.4                  26       9.5               25
#>    2:          4.7                  26      10.4                0
#>    3:          6.7                  26       8.0                0
#>    4:          8.9                  26       8.0                0
#>    5:          9.0                  26      10.6                0
#>   ---                                                            
#> 1094:          6.3                  26       5.5               25
#> 1095:          4.2                  26       3.6               25
#> 1096:          4.8                  26       2.6               25
#> 1097:          5.9                  26       4.3               25
#> 1098:          7.1                  26       5.0               25
#>       evap_morton_lake evap_morton_lake_source evap_pan evap_pan_source
#>                  <num>                   <int>    <num>           <int>
#>    1:              8.1                      26      9.5              25
#>    2:              6.6                      26     10.4               0
#>    3:              6.2                      26      8.0               0
#>    4:              7.5                      26      8.0               0
#>    5:              8.4                      26     10.6               0
#>   ---                                                                  
#> 1094:              6.3                      26      5.5              25
#> 1095:              4.5                      26      3.6              25
#> 1096:              4.0                      26      2.6              25
#> 1097:              6.8                      26      4.3              25
#> 1098:              7.3                      26      5.0              25
#>       evap_syn evap_syn_source  extracted station_y station_x   mslp
#>          <num>           <int>     <Date>     <num>     <num>  <num>
#>    1:     11.1              26 2025-11-29  -32.3292  117.8733 1012.7
#>    2:      7.2              26 2025-11-29  -32.3292  117.8733 1010.8
#>    3:      8.3              26 2025-11-29  -32.3292  117.8733 1017.2
#>    4:     10.1              26 2025-11-29  -32.3292  117.8733 1020.9
#>    5:     10.4              26 2025-11-29  -32.3292  117.8733 1015.9
#>   ---                                                               
#> 1094:      6.7              26 2025-11-29  -31.0742  150.8362 1015.7
#> 1095:      4.5              26 2025-11-29  -31.0742  150.8362 1012.7
#> 1096:      5.1              26 2025-11-29  -31.0742  150.8362 1011.4
#> 1097:      6.5              26 2025-11-29  -31.0742  150.8362 1014.2
#> 1098:      7.5              26 2025-11-29  -31.0742  150.8362 1015.8
#>       mslp_source radiation radiation_source rainfall rainfall_source rh_tmax
#>             <int>     <num>            <int>    <num>           <int>   <num>
#>    1:          25      31.0               42      0.0               0    22.6
#>    2:           0      27.1               42      0.0               0    71.2
#>    3:           0      26.8               42      0.0               0    37.5
#>    4:           0      30.9               42      0.0               0    26.3
#>    5:           0      30.8               42      0.0               0    33.4
#>   ---                                                                        
#> 1094:           0      24.9               42      2.0               0    46.5
#> 1095:           0      15.7               42     10.2               0    61.9
#> 1096:           0      14.0               42      1.0               0    50.0
#> 1097:           0      26.4               42     18.8               0    52.7
#> 1098:           0      28.7               42      0.0               0    44.5
#>       rh_tmax_source rh_tmin rh_tmin_source    vp vp_deficit vp_deficit_source
#>                <int>   <num>          <int> <num>      <num>             <int>
#>    1:             26    83.5             26  14.5       32.7                26
#>    2:             26   100.0             26  20.0        5.2                26
#>    3:             26    87.0             26  12.6       14.9                26
#>    4:             26   100.0             26  14.0       22.7                26
#>    5:             26   100.0             26  22.1       24.1                26
#>   ---                                                                         
#> 1094:             26   100.0             26  19.5       14.3                26
#> 1095:             26   100.0             26  21.8        9.3                26
#> 1096:             26    99.6             26  19.9       13.8                26
#> 1097:             26   100.0             26  21.6       11.6                26
#> 1098:             26   100.0             26  18.9       15.8                26
#>       vp_source
#>           <int>
#>    1:        25
#>    2:         0
#>    3:         0
#>    4:         0
#>    5:         0
#>   ---          
#> 1094:         0
#> 1095:         0
#> 1096:         0
#> 1097:         0
#> 1098:         0
```
