Skip to contents

This function is used to blend data from AmeriFlux and data from ERA5, ensuring they both have the same start and end timestamps. Please first make sure to run the merge function (merge_ERA5_FLUX()) first, because output of merge function will be used as input of this blending function.

Usage

blend_ERA5_Flux(
  merged_data = NULL,
  varname_FLUX = NULL,
  varname_ERA5 = NULL,
  blending_rule = NULL
)

Arguments

merged_data

(data.frame) A data frame that has a datetime stamp column named "time" with the format: "%Y-%m-%d %H:%M:%S". The time step of the "time" column is the same with that of AmeriFlux file. It also includes the columns of AmeriFlux and ERA5 data that were merged together.

varname_FLUX

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

varname_ERA5

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

blending_rule

(character) There are four types of blending rules that can be used to blend AmeriFlux and ERA5 variables:

  • "lm": Linear regression with slope. Fits a linear model with slope, FLUX ~ ERA5, then only fills missing values in FLUX with predicted values from ERA5.

  • "lm_no_intercept": Linear regression without slope. Fits a linear model without slope, FLUX ~ ERA5, then only fills missing values in FLUX with predicted values from ERA5.

  • "replace": Replace Ameriflux variable with ERA5 variable.

  • "automatic": Checks for non-missing FLUX values. If ≥50% present then uses "lm" approach. If <50% present then fallback to "replace".

Value

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

  • 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.

  • Variable column with the blending rule applied on it. Name of the blended column is similar to AmeriFlux column name but with addition of "_f".

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")
# Run the merge function first, because its output will be used as input for this blending function
# Merge AmeriFlux and ERA5 data together
merged_data <- merge_ERA5_FLUX(filename_FLUX, filename_ERA5, varname_FLUX, varname_ERA5)
# Specify the blending rule(s)
blending_rule <- c('lm_no_intercept')
# Blend AmeriFlux and ERA5 data together
merg_blend <- blend_ERA5_Flux(merged_data, varname_FLUX, varname_ERA5, blending_rule)
#> Processing: P using rule: lm_no_intercept
head(merg_blend)
#>                  time        tp  P P_f
#> 1 2002-01-01 00:00:00 0.2252227 NA  NA
#> 2 2002-01-01 01:00:00 0.4244113 NA  NA
#> 3 2002-01-01 02:00:00 0.3902548 NA  NA
#> 4 2002-01-01 03:00:00 0.6315456 NA  NA
#> 5 2002-01-01 04:00:00 0.2987110 NA  NA
#> 6 2002-01-01 05:00:00 0.3121161 NA  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)
# Specify the blending rule(s)
blending_rule <- c('replace', 'automatic')
# Blend AmeriFlux and ERA5 data together
merg_blend <- blend_ERA5_Flux(merged_data, varname_FLUX, varname_ERA5, blending_rule)
#> Processing: SW_IN using rule: replace
#> Processing: TA using rule: automatic
#> Fitting linear model with formula: TA ~ t2m
head(merg_blend)
#>                  time ssrd      t2m SW_IN TA SW_IN_f     TA_f
#> 1 2020-01-01 00:00:00    0 22.04965    NA NA       0 21.24062
#> 2 2020-01-01 00:30:00    0 21.83630    NA NA       0 20.95910
#> 3 2020-01-01 01:00:00    0 21.62295    NA NA       0 20.67757
#> 4 2020-01-01 01:30:00    0 21.40405    NA NA       0 20.38873
#> 5 2020-01-01 02:00:00    0 21.18514    NA NA       0 20.09988
#> 6 2020-01-01 02:30:00    0 20.99367    NA NA       0 19.84723