Skip to contents

This function is used to merge data from AmeriFlux and data from ERA5, ensuring they both have the same start and end timestamps.

Usage

merge_ERA5_FLUX(
  filename_FLUX = NULL,
  filename_ERA5 = NULL,
  varname_FLUX = NULL,
  varname_ERA5 = NULL
)

Arguments

filename_FLUX

(character) The file path to AmeriFlux BASE data downloaded from https://ameriflux.lbl.gov/.

filename_ERA5

(character) The file path to a CSV file of meterological data downloaded from ERA5 https://cds.climate.copernicus.eu/datasets/reanalysis-era5-single-levels?tab=overview. Please note that the original ERA5 files are in .nc format. You may want to convert these files into CSV format using the function netcdf_to_csv().

varname_FLUX

(character) A vector of variable names in AmeriFlux BASE data to be merged with ERA5 data.

varname_ERA5

(character) A vector of variable names in ERA5 data to be merged with AmeriFlux BASE data.

Value

(data.frame) A data frame with the following characteristics:

  • Datetime stamp column named "time" with the format: "%Y-%m-%d %H:%M:%S".

  • Time step of the "time" column is the same with that of AmeriFlux file.

  • It also includes the columns of varname_FLUX, the columns of varname_ERA5.

Note

Please note that the length of varname_FLUX must be the same as the length of varname_ERA5; at the same location, varname_FLUX and varname_ERA5 should refer to the same variable despite the fact that AmeriFlux and ERA5 may use different names for the same variable. For example, for incoming shortwave radiation, ERA5 uses "ssrd", but AmeriFlux uses "SW_IN".

Author

Ammara Talib and Junna Wang

Examples


# First example
# Point to AmeriFlux data
filename_FLUX <- system.file("extdata", "AMF_BR-Sa1_BASE-BADM_5-5.zip", package = "ERA5Flux")
# Point to ERA5 data
filename_ERA5 <- system.file("extdata", "BR-Sa1_tp_2002_2011.csv", package = "ERA5Flux")
# List AmeriFlux variable(s) to be merged with ERA5
varname_FLUX <- c("P")
# List ERA5 variable(s) to be merged with AmeriFlux
varname_ERA5 <- c("tp")
# Merge AmeriFlux and ERA5 data together
merged_data <- merge_ERA5_FLUX(filename_FLUX, filename_ERA5, varname_FLUX, varname_ERA5)
head(merged_data)
#>                  time        tp  P
#> 1 2002-01-01 00:00:00 0.2252227 NA
#> 2 2002-01-01 01:00:00 0.4244113 NA
#> 3 2002-01-01 02:00:00 0.3902548 NA
#> 4 2002-01-01 03:00:00 0.6315456 NA
#> 5 2002-01-01 04:00:00 0.2987110 NA
#> 6 2002-01-01 05:00:00 0.3121161 NA

# Second example
# Point to AmeriFlux data
filename_FLUX <- system.file("extdata", "AMF_US-EvM_BASE-BADM_2-5.zip", package = "ERA5Flux")
# Point to ERA5 data
filename_ERA5 <- system.file("extdata", "US-EvM_ERA_2020_2023_hr.csv", package = "ERA5Flux")
# List AmeriFlux variable(s) to be merged with ERA5
varname_FLUX <- c('SW_IN', 'TA')
# List ERA5 variable(s) to be merged with AmeriFlux
varname_ERA5 <- c('ssrd', 't2m')
# Merge AmeriFlux and ERA5 data together
merged_data <- merge_ERA5_FLUX(filename_FLUX, filename_ERA5, varname_FLUX, varname_ERA5)
head(merged_data)
#>                  time ssrd      t2m SW_IN TA
#> 1 2020-01-01 00:00:00    0 22.04965    NA NA
#> 2 2020-01-01 00:30:00    0 21.83630    NA NA
#> 3 2020-01-01 01:00:00    0 21.62295    NA NA
#> 4 2020-01-01 01:30:00    0 21.40405    NA NA
#> 5 2020-01-01 02:00:00    0 21.18514    NA NA
#> 6 2020-01-01 02:30:00    0 20.99367    NA NA