Skip to content

Commit

Permalink
added calc_eact()
Browse files Browse the repository at this point in the history
  • Loading branch information
stineb committed Jan 7, 2024
1 parent 8156c15 commit be9c203
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Remotes:
bczernecki/climate
LazyData: true
ByteCompile: true
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Suggests:
ggplot2,
rmarkdown,
Expand Down
22 changes: 11 additions & 11 deletions R/calc_vp.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ calc_vp <- function(

# calculate atmopheric pressure (Pa) assuming standard
# conditions at sea level (elv=0)
if (is.na(elv) && is.na(patm)){

warning(
"
calc_vp(): Either patm or elv must be provided
if eact is not given.
")
vp <- NA

} else {
# if (is.na(elv) && is.na(patm)){
#
# warning(
# "
# calc_vp(): Either patm or elv must be provided
# if eact is not given.
# ")
# vp <- NA
#
# } else {

patm <- ifelse(is.na(patm),
calc_patm(elv),
patm)

# Calculate VP.
vp <- calc_vp_inst(qair = qair, patm = patm)
}
# }
return( vp )

}
Expand Down
65 changes: 40 additions & 25 deletions R/calc_vpd.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ calc_vpd <- function(
##-----------------------------------------------------------------------

## calculate atmopheric pressure (Pa) assuming standard conditions at sea level (elv=0)
if (is.na(elv) && is.na(patm) && is.na(eact)){
# if (is.na(elv) && is.na(patm) && is.na(eact)){
#
# warning("calc_vpd(): Either patm or elv must be provided if eact is not given.")
# vpd <- NA
#
# } else {

warning("calc_vpd(): Either patm or elv must be provided if eact is not given.")
vpd <- NA

} else {

if (is.na(eact)){
# if (is.na(eact)){
patm <- ifelse(is.na(patm),
calc_patm(elv),
ingestr::calc_patm(elv),
patm)
}
# }

## Calculate VPD as mean of VPD based on Tmin and VPD based on Tmax if they are availble.
## Otherwise, use just tc for calculating VPD.
Expand All @@ -57,7 +57,7 @@ calc_vpd <- function(
calc_vpd_inst( qair=qair, eact=eact, tc=tmax, patm=patm))/2,
calc_vpd_inst( qair=qair, eact=eact, tc=tc, patm=patm)
)
}
# }
return( vpd )
}

Expand Down Expand Up @@ -94,20 +94,13 @@ calc_vpd_inst <- function(
## eact = actual vapor pressure, Pa
##-----------------------------------------------------------------------

if (is.na(eact)){
# kTo = 288.15 # base temperature, K (Prentice, unpublished)
kR = 8.3143 # universal gas constant, J/mol/K (Allen, 1973)
kMv = 18.02 # molecular weight of water vapor, g/mol (Tsilingiris, 2008)
kMa = 28.963 # molecular weight of dry air, g/mol (Tsilingiris, 2008)

## calculate the mass mixing ratio of water vapor to dry air (dimensionless)
wair <- qair / (1 - qair)

## calculate water vapor pressure
rv <- kR / kMv
rd <- kR / kMa
eact = patm * wair * rv / (rd + wair * rv)
}
# if actual vapour pressure (eact) is not available, calculate it from
# specific humidity
eact <- ifelse(
is.na(eact),
calc_eact(qair, patm),
eact
)

## calculate saturation water vapour pressure in Pa
esat <- 611.0 * exp( (17.27 * tc)/(tc + 237.3) )
Expand All @@ -117,7 +110,29 @@ calc_vpd_inst <- function(

## this empirical equation may lead to negative values for VPD
## (happens very rarely). assume positive...
vpd <- max( 0.0, vpd )
vpd <- ifelse(
vpd < 0,
0,
vpd
)

return( vpd )
}

calc_eact <- function(qair, patm){

# kTo <- 288.15 # base temperature, K (Prentice, unpublished)
kR <- 8.3143 # universal gas constant, J/mol/K (Allen, 1973)
kMv <- 18.02 # molecular weight of water vapor, g/mol (Tsilingiris, 2008)
kMa <- 28.963 # molecular weight of dry air, g/mol (Tsilingiris, 2008)

## calculate the mass mixing ratio of water vapor to dry air (dimensionless)
wair <- qair / (1 - qair)

## calculate water vapor pressure
rv <- kR / kMv
rd <- kR / kMa
eact <- patm * wair * rv / (rd + wair * rv)

return(eact)
}

0 comments on commit be9c203

Please sign in to comment.