# Different treatments of uStar threshold

The recommended way of dealing with the uncertain uStar threshold for filtering the half-hourly data, is to repeat all the processing steps with several bootstrapped estimates of the threshold as in `vignette('useCase')`.

First, some setup.

    #+++ load libraries used in this vignette
    library(REddyProc)
    library(dplyr)
    #+++ define directory for outputs
    outDir <- tempdir()  # CRAN policy dictates to write only to this dir in examples
    #outDir <- "out"     # to write to subdirectory of current users dir
    #+++ Add time stamp in POSIX time format to example data
    EddyDataWithPosix.F <- fConvertTimeToPosix(Example_DETha98, 'YDH',Year.s = 'Year'
        ,Day.s = 'DoY',Hour.s = 'Hour')

## Not applying uStar filtering

Subsequent processing steps can be performed without further uStar filtering using `sEddyProc_sMDSGapFill`. Corresponding result columns then have no uStar specific suffix.

    EddyProc.C <- sEddyProc$new('DE-Tha', EddyDataWithPosix.F,
        c('NEE','Rg','Tair','VPD', 'Ustar'))
    EddyProc.C$sMDSGapFill('NEE')
    grep("NEE.*_f$",names(EddyProc.C$sExportResults()), value = TRUE)

    ## [1] "NEE_f"

## User-specified uStar threshold

The user can provide value for uStar-filtering before gapfilling, using `sEddyProc_sMDSGapFillAfterUstar`. Output columns for this uStar scenario use the suffix as specified by argument `UstarSuffix.s` which defaults to “WithUstar”.

The friction velocity, uStar, needs to be in column named “uStar” of the input dataset.

    EddyProc.C <- sEddyProc$new('DE-Tha', EddyDataWithPosix.F,
        c('NEE','Rg','Tair','VPD', 'Ustar'))
    uStar <- 0.46
    EddyProc.C$sMDSGapFillAfterUstar('NEE', UstarThres.df = uStar)
    grep("NEE.*_f$",names(EddyProc.C$sExportResults()), value = TRUE)

    ## [1] "NEE_WithUstar_f"

## Sinlge uStar threshold estimate

The uStar threshold can be estimated from the uStar-NEE relationship from the data without its uncertainty by bootstrap.

    EddyProc.C <- sEddyProc$new('DE-Tha', EddyDataWithPosix.F,
        c('NEE','Rg','Tair','VPD', 'Ustar'))
    # estimating the thresholds based on the data (without bootstrap)
    (uStarTh <- EddyProc.C$sEstUstarThreshold()$uStarTh)

    ##   aggregationMode seasonYear  season     uStar
    ## 1          single         NA    <NA> 0.4162500
    ## 2            year       1998    <NA> 0.4162500
    ## 3          season       1998 1998001 0.4162500
    ## 4          season       1998 1998003 0.4162500
    ## 5          season       1998 1998006 0.3520000
    ## 6          season       1998 1998009 0.3369231
    ## 7          season       1998 1998012 0.1740000

    # may plot saturation of NEE with UStar for a specified season to pdf
    EddyProc.C$sPlotNEEVersusUStarForSeason(levels(uStarTh$season)[3], dir = outDir )

Next, the annual estimate is used as the default in gap-filling. Output columns use the suffix as specified by argument `UstarSuffix.s` which defaults to “WithUstar”.

    EddyProc.C$sMDSGapFillAfterUstar('NEE')
    grep("NEE.*_f$",names(EddyProc.C$sExportResults()), value = TRUE)

    ## [1] "NEE_WithUstar_f"

## See also

A more advanced case of user-specified seasons for uStar threshold estimate is given in `vignette('DEGebExample')`.