Skip to content

Commit

Permalink
Refrain from recomputing VPD for CRU based on potentially non-existin…
Browse files Browse the repository at this point in the history
…g column 'vapr'

This fixes VPD for CRU, when vapr was not requested.
This commit moves computation of vpd with calc_vpd() inside of ingest_globalfields() for CRU, watch_wfdei, and wfde5. This results in consistent code between these 3 sources and also irrespective of the user calling ingest() or ingest_bysite(). Hence, this reduces code replication.
Furthermore, calc_vpd() is now re-computed (in ingest() or ingest_bysite()) only in cases when there is bias correction of `vapr` with WorldClim data. This code is still repeated across ingest() and ingest_bysite().
  • Loading branch information
fabern committed Dec 20, 2024
1 parent 845dc95 commit 6fd1629
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 66 deletions.
30 changes: 3 additions & 27 deletions R/ingest.R
Original file line number Diff line number Diff line change
Expand Up @@ -647,35 +647,11 @@ ingest <- function(
}

} else {

# Calculate vapour pressure deficit from specific humidity
# this calculates this variable for cases where there is
# no bias correction

if ("vpd" %in% getvars){

if (source == "watch_wfdei" || source == "wfde5"){
# use daily mean temperature
ddf <- ddf %>%
rowwise() %>%
dplyr::mutate(
vapr = calc_vp(qair = qair, patm = patm),
vpd = calc_vpd(eact = vapr, tc = temp)
) %>%
ungroup()

} else if (source == "cru"){
# use daily minimum and maximum temperatures
ddf <- ddf %>%
rowwise() %>%
dplyr::mutate(
vpd = calc_vpd(eact = vapr, tmin = tmin, tmax = tmax)
) %>%
ungroup()
}

# For cases where there is no bias correction,
# (of sources cru, watch_wfdei, wfde5; but not ndep)
# vapour pressure deficit has already been computed within `ingest_globalfields()`
}

}

} else if (source == "gee"){
Expand Down
42 changes: 3 additions & 39 deletions R/ingest_bysite.R
Original file line number Diff line number Diff line change
Expand Up @@ -448,46 +448,10 @@ ingest_bysite <- function(
}

} else {

# Calculate vapour pressure deficit from specific humidity
# this calculates this variable for cases where there is
# no bias correction

if ("vpd" %in% getvars){

# xxxxxxx
if (!("vapr" %in% names(df_tmp))){
# calculate vapour pressure from specific humidity - needed for bias correction with worldclim data
if (source == "watch_wfdei"){
# specific humidity (qair, g g-1) is read, convert to vapour pressure (vapr, Pa)
df_tmp <- df_tmp %>%
rowwise() %>%
dplyr::mutate(vapr = calc_vp(qair = qair, patm = patm)) %>%
ungroup()

} else if (source == "cru"){
# vapour pressure is read from file, convert from hPa to Pa
df_tmp <- df_tmp %>%
dplyr::mutate(vapr = 1e2 * vap) %>%
dplyr::select(-vap)

}
}

if (source == "watch_wfdei"){
# use daily mean temperature
df_tmp <- df_tmp %>%
rowwise() %>%
dplyr::mutate(vpd = calc_vpd(eact = vapr, tc = temp)) %>%
ungroup()

} else if (source == "cru"){
# use daily minimum and maximum temperatures
df_tmp <- df_tmp %>%
rowwise() %>%
dplyr::mutate(vpd = calc_vpd(eact = vapr, tmin = tmin, tmax = tmax)) %>%
ungroup()
}
# For cases where there is no bias correction,
# (of sources cru, watch_wfdei, wfde5; but not ndep)
# vapour pressure deficit has already been computed within `ingest_globalfields()`
}
}

Expand Down
14 changes: 14 additions & 0 deletions R/ingest_globalfields.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ ingest_globalfields <- function(
dplyr::rename(patm = myvar),
by = c("sitename", "date")
)

# calculate VPD based on humidity, air temperature, and atmospheric pressure
df_out <- df_out %>%
rowwise() %>%
mutate(vapr = calc_vp(qair = qair, patm = patm)) %>%
mutate(vpd = calc_vpd(eact = vapr, tc = temp)) %>%
ungroup()
}

# precipitation
Expand Down Expand Up @@ -163,6 +170,13 @@ ingest_globalfields <- function(
dplyr::rename(patm = myvar),
by = c("sitename", "date")
)

# calculate VPD based on humidity, air temperature, and atmospheric pressure
df_out <- df_out %>%
rowwise() %>%
mutate(vapr = calc_vp(qair = qair, patm = patm)) %>%
mutate(vpd = calc_vpd(eact = vapr, tc = temp)) %>%
ungroup()
}

# precipitation
Expand Down

0 comments on commit 6fd1629

Please sign in to comment.