## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
# Load the package
library(ERA5Flux)

# Specify the ERA5-Land variables you want to get
my_variables <- c("surface_solar_radiation_downwards")

# Point to the folder containing the unzipped site folders and requested files manifest
# For the purposes of this demo, an example data folder will be used
# Please point to your own existing folder for your own workflow
my_AmeriFlux_folder <- system.file("extdata", "example_AmeriFlux", package = "ERA5Flux")

# Generate the AmeriFlux site metadata file
my_site_metadata <- get_site_metadata(folder = my_AmeriFlux_folder,
                                      selected_variables = my_variables)

my_site_metadata

## ----eval = F-----------------------------------------------------------------
# # Download the land-sea mask if you haven't done so already
# get_land_sea_mask()

## ----eval = F-----------------------------------------------------------------
# # Paste your own API key
# my_key <- "my_own_ECMWF_key"
# 
# # Set the path to your land-sea mask
# my_path_to_mask <- "lsm_1279l4_0.1x0.1.grb_v4_unpack.nc"
# 
# # Point to the folder where you want the ERA5-Land data to download to
# # For the purposes of this demo, some example placeholder text is shown
# # Please point to your own existing folder for your own workflow
# my_ERA5_download_path <- "my_own_path_to_ERA5_download_folder"
# 
# # Download the ERA5-Land data
# download_ERA5(my_key = my_key,
#               site_metadata = my_site_metadata,
#               mask = my_path_to_mask,
#               download_path = my_ERA5_download_path)

## -----------------------------------------------------------------------------
# Point to a folder containing ERA5-Land .nc files
# For the purposes of this demo, an example data folder will be used
# Please point to your own existing folder for your own workflow
site_folder <- system.file("extdata", "example_path_to_ERA5_download_folder", package = "ERA5Flux")

# Create a temporary directory to export our output to
output_filepath <- tempdir()

# Specify a site name
site_name <- "US_GL2"
# Specify the site latitude and longitude coordinates
site_lat <- as.numeric(my_site_metadata$lat)
site_lon <- as.numeric(my_site_metadata$lon)

# Convert netCDF data to a CSV file
netcdf_to_csv(site_folder, output_filepath, site_name, site_lat, site_lon, full_year = FALSE)

# Read the CSV back in
data <- read.csv(list.files(output_filepath, pattern = "US_GL2", full.names = TRUE))

head(data)

## -----------------------------------------------------------------------------
# Point to AmeriFlux CSV data
# For the purposes of this demo, an example data file will be used
# Please point to your own existing file for your own workflow
ameriflux_file <- system.file("extdata", 
                              "example_AmeriFlux", 
                              "AMF_US-GL2_BASE-BADM_2-5", 
                              "AMF_US-GL2_BASE_HH_2-5.csv", 
                              package = "ERA5Flux")

# Point to ERA5-Land CSV data
# For the purposes of this demo, an example data file will be used
# Please point to your own existing file for your own workflow
era5_file <- list.files(output_filepath, pattern = "US_GL2", full.names = TRUE)

# List AmeriFlux variable(s) to be merged 
ameriflux_var <- c("SW_IN")
# List ERA5-Land variable(s) to be merged 
era5_var <- c("ssrd")

# Merge them together
merged_data <- merge_ERA5_Flux(filename_FLUX = ameriflux_file,
                               filename_ERA5 = era5_file,
                               varname_FLUX = ameriflux_var,
                               varname_ERA5 = era5_var)

merged_data[3162:3182,]

## -----------------------------------------------------------------------------
# Specify the blending rule(s)
# If you have multiple variables, specify a rule for each variable
blending_rule <- c("replace")

# Blend them together
blended_data <- blend_ERA5_Flux(merged_data = merged_data,
                                varname_FLUX = ameriflux_var,
                                varname_ERA5 = era5_var,
                                blending_rule = blending_rule)

blended_data[3162:3182,]

