Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions eddy4r.york/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
^eddy4R\.york\.Rproj$
^\.Rproj\.user$
^testthat
^\.github$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions eddy4r.york/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ Imports:
lubridate,
magrittr,
purrr,
rlang,
stats,
stringr,
tibble,
tidyr,
tidyselect,
utils
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions eddy4r.york/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export(wrap.rot)
export(wrap.towr)
export(write.REYN)
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
17 changes: 15 additions & 2 deletions eddy4r.york/R/def.para.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param fileMask string mask that can be passed to \code{as.POSIXct(format = mask)} to decode the date in the file name
#' @param species vector of species names. Match with IntlNatu naming
#' @param speciesRatioName created automatically from species. name of gas species following the rtioMoleDry<spc> format
#' @param speciesFluxName created automatically from species. name of gas species following the flux<spc> format
#' @param unitList defaults to \code{default_unit_list()} plus species field based off of species argument
#' speices is a list with names that match speciesRatioName describing the units per species. Defaults to mol<spc> mol-1Dry
#' @param freq data aquisition frequency of input data
Expand Down Expand Up @@ -87,6 +88,7 @@ def.para = function(
subDir = c("none", "monthly", "daily")[1],
species = NULL,
speciesRatioName = NULL,
speciesFluxName = NULL,
unitList = NULL,
freq = 5,
files = NULL,
Expand Down Expand Up @@ -151,6 +153,13 @@ def.para = function(
#Ellipsis
...){

# This throws an error if an argument that doesn't have a default AND isn't used in the function has not had a value supplied.
# e.g x = function(y){print("hello")};x() doesn't error but the below does.
# x = function(y){sapply(ls(environment()), get, envir = environment(), inherits = FALSE);print("hello")};x()
# This means we can get away with using c(as.list(environment()), list(...)) to capture the args
# while being sure all the defaults have been supplied
sapply(ls(environment()), get, envir = environment(), inherits = FALSE)

# Get all arguments into a list
para = c(as.list(environment()), list(...))

Expand Down Expand Up @@ -178,19 +187,23 @@ def.para = function(
para$speciesRatioName = paste0("rtioMoleDry", species)
}

if(is.null(speciesFluxName)){
para$speciesFluxName = paste0("flux", species)
}

if(is.null(para$unitList$species)){
para$unitList$species = stats::setNames(as.list(paste0("mol", species, " mol-1Dry")), para$speciesRatioName)
}

para$ListGasSclr = purrr::map2(para$species,
para$ListGasSclr = purrr::map2(para$speciesFluxName,
para$unitList$species,
~{
list(Conv = "densMoleAirDry",
Unit = base::data.frame(InpVect = "m s-1",
InpSclr = .y,
Conv = "mol m-3",
Out = "mol m-2 s-1"),
NameOut = paste0("flux",.x))
NameOut = .x)
}) %>%
stats::setNames(para$speciesRatioName)
}
Expand Down
14 changes: 8 additions & 6 deletions eddy4r.york/R/def.para.tmp.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ def.para.tmp = function(paraMain, skip_scalar){

paraCall = paraMain$call

skipRtio = paraMain$speciesRatioName[which(paraMain$species %in% c(skip_scalar))]
skipCcvPos = which(paraMain$lagVars %in% skipRtio)
skipSpeciesPosition = which(paraMain$species %in% skip_scalar)
skipRtio = paraMain$speciesRatioName[which(paraMain$species %in% skip_scalar)]
skipLagVarPos = which(paraMain$lagVars %in% skipRtio)

# edit the original call to only the species we have
paraCall$species = paraCall$species[paraCall$species != skip_scalar]
paraCall$lagDefaults = eval(paraCall$lagDefaults)[-skipCcvPos]
paraCall$lagRangeLimit = eval(paraCall$lagRangeLimit)[-skipCcvPos]
paraCall$species = eval(paraCall$species)[-skipSpeciesPosition]
paraCall$lagDefaults = eval(paraCall$lagDefaults)[-skipLagVarPos]
paraCall$lagRangeLimit = eval(paraCall$lagRangeLimit)[-skipLagVarPos]
paraCall$lagVars = eval(paraCall$lagVars)[-skipLagVarPos]

# If there are no species left, make it NULL
if(length(paraCall$species) == 0){
paraCall$species = NULL
}

# rebuild para list with new species argument
do.call(get("def.para", getNamespace("eddy4R.york")), as.list(paraCall))
eval(paraCall)

}

7 changes: 7 additions & 0 deletions eddy4r.york/R/eddy4R.york-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang .data
## usethis namespace: end
NULL
2 changes: 1 addition & 1 deletion eddy4r.york/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ assign_input_units = function(eddy.data,
attr(x = eddy.data$presAtm, which = "unit") = unitList$pres

# wind vectors
for(var in c("veloXaxs", "veloYaxs", "veloZaxs")){
for(var in c("veloXaxs", "veloYaxs", "veloZaxs", "veloXaxsInp", "veloYaxsInp", "veloZaxsInp")){
attr(x = eddy.data[,var], which = "unit") = unitList$velo
}

Expand Down
7 changes: 7 additions & 0 deletions eddy4r.york/R/wrap.anem.cor.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ wrap.anem.cor = function(eddy.data,
eddy.data$veloZaxs[eddy.data$veloZaxs < 0 ] = eddy.data$veloZaxs[eddy.data$veloZaxs < 0 ]*1.289
}


# Preserve Input Vectors for Wind Direction Calculation -------------------

eddy.data$veloXaxsInp = eddy.data$veloXaxs
eddy.data$veloYaxsInp = eddy.data$veloYaxs
eddy.data$veloZaxsInp = eddy.data$veloZaxs

# Return
eddy.data
}
28 changes: 25 additions & 3 deletions eddy4r.york/R/wrap.towr.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,21 @@ wrap.towr = function(paraMain,
})

if(!is.null(skip_scalar)){
if(skip_scalar == "valid_error"){
if("valid_error" %in% skip_scalar){
next
}
}

if(length(skip_scalar) > 0){

# need to actually remove the missing columns from eddy.data, otherwise we fall foul of unit checking in def.stat.sta.diff()
skipRtio = paraMain$speciesRatioName[which(paraMain$species %in% skip_scalar)]
eddy.data = eddy.data[!names(eddy.data) %in% skipRtio]
# eddy.data = eddy.data |>
# dplyr::select(-dplyr::any_of(skipRtio))

para = eddy4R.york::def.para.tmp(paraMain, skip_scalar)

}else{
para = paraMain
}
Expand Down Expand Up @@ -156,14 +164,16 @@ wrap.towr = function(paraMain,
if(is.null(lag_out)){next}

eddy.data = lag_out$eddy.data
}else{
lag_out = NULL
}

# Handle missing values ---------------------------------------------------
eddy.data = tryCatch({
eddy4R.york::def.miss.hndl(eddy.data,
missingMethod = para$missingMethod,
missingThreshold = para$missingThreshold,
aggregationPeriod = para$aggregationPeriod,
aggregationDuration = para$aggregationDuration,
freq = para$freq)},
error = function(e){
eddy4R.york::log_message(wrap_tower_log, "error", "Handel Missing Values", aggregationPeriod[i,], e)
Expand Down Expand Up @@ -210,6 +220,17 @@ wrap.towr = function(paraMain,

if(is.null(REYN)){next}


# Calculate Extra Parameters for Mean File --------------------------------
REYN$mean = REYN$mean |>
dplyr::mutate(distRgh = eddy4R.turb::def.dist.rgh(distZaxsMeas = .data$distZaxsMeas,
distObkv = .data$distObkv,
veloXaxs = .data$veloXaxs,
veloFric = .data$veloFric),
windDir = eddy4R.base::def.pol.cart(matrix(c(.data$veloYaxsInp,.data$veloXaxsInp),ncol=2))
)


# stationarity testing ----------------------------------------------------
REYN$stna = tryCatch({
eddy4R.turb::def.stna(data=REYN$data,
Expand Down Expand Up @@ -260,14 +281,15 @@ wrap.towr = function(paraMain,

# flux error calculations -------------------------------------------------
REYN$error = tryCatch({

eddy4R.turb::def.ucrt.samp(data = NULL,
distIsca=REYN$isca,
valuMean=REYN$mean,
coefCorr=REYN$corr,
distMean=mean(REYN$data$unixTime-min(REYN$data$unixTime)),
timeFold = 0,
spcsNameRtio = para$speciesRatioName,
spcsNameFlux = paste0("flux", para$species))},
spcsNameFlux = para$speciesFluxName)},
error = function(e){
eddy4R.york::log_message(wrap_tower_log, "error", "def.ucrt.samp", aggregationPeriod[i,], e)
return(NULL)
Expand Down
4 changes: 2 additions & 2 deletions eddy4r.york/R/write.REYN.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ write.REYN = function(REYN,
subDir){

REYN = c(REYN, lag_out)
# lag out can contain eddy.data, which is the same as REYN$data, so we NULL it here to avoid duplication
REYN$eddy.data = NULL

unixTimeMin = min(REYN$data$unixTime,na.rm = T)
Expand Down Expand Up @@ -68,9 +69,8 @@ write.REYN = function(REYN,
file = gzfile(file.path(DirOut,"fast_data", .y, outputFile)),
row.names = F)

}else{
next
}

# write other outputs
}else{
# write or append .csv
Expand Down
3 changes: 3 additions & 0 deletions eddy4r.york/man/def.para.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading