From 203b8ce8fd67b4440e89f357bee8bf8216f348ac Mon Sep 17 00:00:00 2001 From: noctiluc3nt Date: Thu, 10 Oct 2024 19:37:46 +0200 Subject: [PATCH] added some diagnostics (vpd,z0,...) --- NAMESPACE | 4 ++++ R/auxilliary.R | 4 ++-- R/constants.R | 8 ++++++++ R/diagnostics-meteorology.R | 21 +++++++++++++++++++++ R/diagnostics-turbulence.R | 29 +++++++++++++++++++++++++++++ R/ec-processing.R | 1 + README.md | 4 ++-- man/Reddy-package.Rd | 17 +++++++++++++++++ man/alpha.Rd | 12 ++++++++++++ man/calc_cov.Rd | 1 + man/calc_evapotranspiration.Rd | 19 +++++++++++++++++++ man/calc_spectrum1D.Rd | 21 +++++++++++++++++++++ man/calc_vpd.Rd | 19 +++++++++++++++++++ man/deaccumulate1h.Rd | 2 +- man/shift2maxccf.Rd | 2 +- man/ustar2z0.Rd | 17 +++++++++++++++++ 16 files changed, 175 insertions(+), 6 deletions(-) create mode 100644 man/Reddy-package.Rd create mode 100644 man/alpha.Rd create mode 100644 man/calc_evapotranspiration.Rd create mode 100644 man/calc_spectrum1D.Rd create mode 100644 man/calc_vpd.Rd create mode 100644 man/ustar2z0.Rd diff --git a/NAMESPACE b/NAMESPACE index 9274db2..c209751 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ export(calc_cov) export(calc_csi) export(calc_dshear) export(calc_ef) +export(calc_evapotranspiration) export(calc_flux_footprint) export(calc_gustfactor) export(calc_iw) @@ -27,10 +28,12 @@ export(calc_quadrant_analysis) export(calc_ri) export(calc_satvaporpressure) export(calc_spectrum) +export(calc_spectrum1D) export(calc_ti) export(calc_tke) export(calc_ustar) export(calc_var) +export(calc_vpd) export(calc_vtke) export(calc_windDirection) export(calc_windSpeed2D) @@ -65,6 +68,7 @@ export(scale_phiu) export(scale_phiw) export(shift2maxccf) export(sos2Ts) +export(ustar2z0) importFrom(MASS,kde2d) importFrom(RcppRoll,roll_mean) importFrom(RcppRoll,roll_sd) diff --git a/R/auxilliary.R b/R/auxilliary.R index 1242dc0..b1032d8 100644 --- a/R/auxilliary.R +++ b/R/auxilliary.R @@ -37,7 +37,7 @@ binning=function(var1,var2,bins) { #' Shifting two timeseries to match maximum cross-correlation #' -#'@description Shifts two timeseries to match their maximum cross-correlation +#'@description Shifts two timeseries to match their maximum cross-correlation (can be used e.g. for lag-time correction) #'@param var1 vector, first timeseries #'@param var2 vector, second timeseries #'@param plot logical, should the cross-correlation be plotted? default \code{plot = TRUE} @@ -61,7 +61,7 @@ shift2maxccf=function(var1,var2,plot=TRUE) { n=max(n1,n2) #calc cross-correlation notna=(!is.na(var1) & !is.na(var2)) - cc=ccf(var1[notna],var2[notna]) + cc=ccf(var1[notna],var2[notna]) maxcc=max(cc$acf) #max positive cross-correlation lag=cc$lag[which(cc$acf==max(cc$acf))] #respective time lag if (plot == TRUE) { diff --git a/R/constants.R b/R/constants.R index 25fa485..f0d488f 100644 --- a/R/constants.R +++ b/R/constants.R @@ -114,4 +114,12 @@ M_CO2 = function() { #' @keywords internal M_CH4 = function() { return(0.01604) +} + +#' Charnock constant (alpha) +#' +#' Charnock constant (alpha) used to convert friction velocity to surface roughness length +#' @keywords internal +alpha = function() { + return(0.016) } \ No newline at end of file diff --git a/R/diagnostics-meteorology.R b/R/diagnostics-meteorology.R index d4caccc..0d1b07e 100644 --- a/R/diagnostics-meteorology.R +++ b/R/diagnostics-meteorology.R @@ -13,6 +13,27 @@ calc_satvaporpressure = function(temp) { return(a*exp(b*temp/(temp+c))*1000) } +#' Vapor pressure deficit (VPD) +#' +#'@description Calculates vapor pressure deficit (VPD) from temperature and relative humidity using Arrhenius formula +#'@param temp temperature [K] +#'@param rh relative humidity [%] +#'@return VPD, vapor pressure deficit [Pa] +#'@export +#' +calc_vpd = function(temp,rh) { + #calculate saturation pressure here using Arrhenius formula + a=-1.0440397*10^4 + b=-11.29465 + c=-2.7022355*10^-2 + d=1.289036*10^-5 + e=-2.4780681*10^-9 + f=6.5459673 + temp=temp*5/9 #K to deg R (Rankine scale) + es=exp(a/temp+b+c*temp+d*temp^2+e*temp^3+f*log(temp)) + return(es*(1-rh/100)) +} + #' Potential temperature #' #'@description Calculates potential temperature for given temperature and pressure diff --git a/R/diagnostics-turbulence.R b/R/diagnostics-turbulence.R index 4bea4ef..e4580e8 100644 --- a/R/diagnostics-turbulence.R +++ b/R/diagnostics-turbulence.R @@ -157,3 +157,32 @@ calc_br = function(sh,lh) { calc_ef = function(sh,lh) { return(lh/(sh+lh)) } + +#' Evapotranspiration +#' +#'@description Calculates evapotranspiration from latent heat flux +#'@param lh latent heat flux [W/m^2] +#'@param temp temperature [K] (optional), if provided, the latent heat of vaporization is calculated temperature-dependent +#' +#'@return evapotranspiration [kg/(s*m^2)] +#'@export +#' +calc_evapotranspiration = function(lh,temp=NULL) { + lv=Lv(temp) + return(lh/lv) +} + + +### surface roughness and related concepts ### + +#' Calculates surface roughness length z0 from friction velocity using the simple estimate from Charnock, 1955 +#' +#'@description Calculates surface roughness z0 from friction velocity using the simple estimate from Charnock, 1955: z0 = alpha*ustar^2/g with alpha=0.016 and g=9.81 m/s^2 +#'@param ustar friction velocity [m/s] +#' +#'@return surface roughness length [m] +#'@export +#' +ustar2z0 = function(ustar) { + return(alpha()*ustar^2/g()) +} \ No newline at end of file diff --git a/R/ec-processing.R b/R/ec-processing.R index 2b9e2a9..bb854ad 100644 --- a/R/ec-processing.R +++ b/R/ec-processing.R @@ -387,6 +387,7 @@ cov2cf = function(cov_co2w,rho=NULL) { #'set.seed(5) #'x=rnorm(100) #'y=rnorm(100) +#'y[1:10]=NA #'cov_xy=calc_cov(x,y) #' calc_cov = function(x,y) { diff --git a/README.md b/README.md index 926b662..2627c9f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Reddy: An open-source package for analyzing eddy-covariance measurements +# Reddy: A toolbox for analyzing eddy-covariance measurements [![CRAN status](https://www.r-pkg.org/badges/version/Reddy)](https://cran.r-project.org/package=Reddy) [![Last Commit](https://img.shields.io/github/last-commit/noctiluc3nt/Reddy)](https://github.com/noctiluc3nt/Reddy) @@ -31,7 +31,7 @@ The Reddy package provides functions for the post-processing, analysis and evalu - `spectrum.R`: wrapper for the rbase spectrum function to derive averaged FFT spectra and compare them with theoretical slopes - `surface-energy-balance.R`: visualization of surface energy balance, residual flux and closure ratio - + ### Usage diff --git a/man/Reddy-package.Rd b/man/Reddy-package.Rd new file mode 100644 index 0000000..50b92ee --- /dev/null +++ b/man/Reddy-package.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/package-description.R +\docType{package} +\name{Reddy package} +\alias{Reddy package} +\title{Introduction} +\description{ +EC postprocessing and analysis +} +\details{ +to be detailed +} +\references{ +\itemize{ +\item Mack, L., Berntsen, T.K., Vercauteren, N., Pirk, N. (2024). Transfer Efficiency and Organization in Turbulent Transport over Alpine Tundra. Boundary-Layer Meteorology 190, 38. doi: https://doi.org/10.1007/s10546-024-00879-5 +} +} diff --git a/man/alpha.Rd b/man/alpha.Rd new file mode 100644 index 0000000..64c7aaf --- /dev/null +++ b/man/alpha.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/constants.R +\name{alpha} +\alias{alpha} +\title{Charnock constant (alpha)} +\usage{ +alpha() +} +\description{ +Charnock constant (alpha) used to convert friction velocity to surface roughness length +} +\keyword{internal} diff --git a/man/calc_cov.Rd b/man/calc_cov.Rd index 3a88166..9ccfbe7 100644 --- a/man/calc_cov.Rd +++ b/man/calc_cov.Rd @@ -21,6 +21,7 @@ Calculates cov(x,y) set.seed(5) x=rnorm(100) y=rnorm(100) +y[1:10]=NA cov_xy=calc_cov(x,y) } diff --git a/man/calc_evapotranspiration.Rd b/man/calc_evapotranspiration.Rd new file mode 100644 index 0000000..cb21a63 --- /dev/null +++ b/man/calc_evapotranspiration.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/diagnostics-turbulence.R +\name{calc_evapotranspiration} +\alias{calc_evapotranspiration} +\title{Evapotranspiration} +\usage{ +calc_evapotranspiration(lh, temp = NULL) +} +\arguments{ +\item{lh}{latent heat flux [W/m^2]} + +\item{temp}{temperature [K] (optional), if provided, the latent heat of vaporization is calculated temperature-dependent} +} +\value{ +evapotranspiration [kg/(s*m^2)] +} +\description{ +Calculates evapotranspiration from latent heat flux +} diff --git a/man/calc_spectrum1D.Rd b/man/calc_spectrum1D.Rd new file mode 100644 index 0000000..8fcf9e9 --- /dev/null +++ b/man/calc_spectrum1D.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/spectrum.R +\name{calc_spectrum1D} +\alias{calc_spectrum1D} +\title{Spectrum of timeseries} +\usage{ +calc_spectrum1D(ts, tres = 0.05, nbins = 100, method = "fft", plot = TRUE, ...) +} +\arguments{ +\item{ts}{timeseries} + +\item{nbins}{number of bins used to average the spectrum, default \code{nbins=100}} + +\item{plot}{should the spectrum be plotted? default \code{plot=TRUE}} +} +\value{ +binned spectrum +} +\description{ +Calculates and plots the averaged turbulence spectrum (using FFT or DCT) +} diff --git a/man/calc_vpd.Rd b/man/calc_vpd.Rd new file mode 100644 index 0000000..b06e784 --- /dev/null +++ b/man/calc_vpd.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/diagnostics-meteorology.R +\name{calc_vpd} +\alias{calc_vpd} +\title{Vapor pressure deficit (VPD)} +\usage{ +calc_vpd(temp, rh) +} +\arguments{ +\item{temp}{temperature [K]} + +\item{rh}{relative humidity [%]} +} +\value{ +VPD, vapor pressure deficit [Pa] +} +\description{ +Calculates vapor pressure deficit (VPD) from temperature and relative humidity using Arrhenius formula +} diff --git a/man/deaccumulate1h.Rd b/man/deaccumulate1h.Rd index d469398..49715ec 100644 --- a/man/deaccumulate1h.Rd +++ b/man/deaccumulate1h.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/auxilliary.R +% Please edit documentation in R/model-utils.R \name{deaccumulate1h} \alias{deaccumulate1h} \title{deaccumulation} diff --git a/man/shift2maxccf.Rd b/man/shift2maxccf.Rd index 0256c77..ec5a9a4 100644 --- a/man/shift2maxccf.Rd +++ b/man/shift2maxccf.Rd @@ -17,7 +17,7 @@ shift2maxccf(var1, var2, plot = TRUE) a matrix cotaining timeseries \code{var1} and \code{var2} as columns after shifting to the maximum cross-correlation } \description{ -Shifts two timeseries to match their maximum cross-correlation +Shifts two timeseries to match their maximum cross-correlation (can be used e.g. for lag-time correction) } \examples{ ts1=runif(10) diff --git a/man/ustar2z0.Rd b/man/ustar2z0.Rd new file mode 100644 index 0000000..dabd5f1 --- /dev/null +++ b/man/ustar2z0.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/diagnostics-turbulence.R +\name{ustar2z0} +\alias{ustar2z0} +\title{Calculates surface roughness length z0 from friction velocity using the simple estimate from Charnock, 1955} +\usage{ +ustar2z0(ustar) +} +\arguments{ +\item{ustar}{friction velocity [m/s]} +} +\value{ +surface roughness length [m] +} +\description{ +Calculates surface roughness z0 from friction velocity using the simple estimate from Charnock, 1955: z0 = alpha*ustar^2/g with alpha=0.016 and g=9.81 m/s^2 +}