Skip to content

Commit

Permalink
Doc update
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedia committed Jun 26, 2018
1 parent 9a1b2a7 commit 403dee2
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 36 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(hurs2huss)
export(hurs2w)
export(huss2hurs)
export(rad2cc)
export(tas2ws)
export(udConvertGrid)
import(transformeR)
importFrom(magrittr,"%<>%")
Expand Down
22 changes: 12 additions & 10 deletions R/huss2hurs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#' @importFrom magrittr %>% %<>% extract2
#' @importFrom udunits2 ud.are.convertible
#' @family derivation
#' @template templateRefHumidity
#' @examples
#' data("ps.iberia")
#' data("tas.iberia")
Expand All @@ -53,31 +54,32 @@ huss2hurs <- function(huss, ps, tas) {
checkSeason(huss, ps, tas)
# Check units
u.huss <- getGridUnits(huss)
if (u.huss != "1") {
if (!ud.are.convertible(u.huss, "1")) {
stop("Non compliant huss units (should be convertible to \'1\')")
if (u.huss != "kg/kg") {
if (!ud.are.convertible(u.huss, "kg/kg")) {
stop("Non compliant huss units (should be convertible to \'kg/kg\')")
}
message("Converting units ...")
huss <- udConvertGrid(huss, new.units = "1")
message("[", Sys.time(), "] Converting units ...")
huss <- udConvertGrid(huss, new.units = "kg/kg")
}
huss %<>% redim(member = TRUE)
message("[", Sys.time(), "] Calculating Relative humidity ...")
ws <- suppressMessages(tas2ws(tas, ps)) %>% redim(member = TRUE)
tas <- NULL
coords <- getCoordinates(huss)
n.mem <- getShape(huss, "member")
l <- lapply(1:n.mem, function(x) {
a <- subsetGrid(huss, members = x, drop = TRUE) %>% redim(member = FALSE) %>% extract2("Data") %>% array3Dto2Dmat()
b <- subsetGrid(ws, members = x, drop = TRUE) %>% redim(member = FALSE) %>% extract2("Data") %>% array3Dto2Dmat()
w <- a/(1 - a) # w = mixing ratio
w <- a/(1 - a) # w = mixing (mass) ratio
a <- 100 * w/b
huss$Data <- mat2Dto3Darray(a, x = coords$x, y = coords$y)
ps$Data <- mat2Dto3Darray(a, x = coords$x, y = coords$y)
a <- b <- NULL
return(huss)
return(ps)
})
tas <- ps <- huss <- NULL
huss <- ws <- ps <- NULL
hurs <- suppressWarnings(bindGrid(l, dimension = "member"))
hurs$Variable$varName <- "hurs"
hurs$Variable$level <- NULL
wt$Variable$level <- NULL
attr(hurs$Variable, "units") <- "%"
attr(hurs$Variable, "longname") <- "Surface_air_relative_humidity"
attr(hurs$Variable, "description") <- "Estimated relative humidity from saturation pressure and specific humidity"
Expand Down
10 changes: 6 additions & 4 deletions R/rad2cc.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ rad2cc <- function(rsds = NULL, rlds = NULL, rtds = NULL) {
if (!ud.are.convertible(u1, "W.m-2")) {
stop("Non compliant rsds units (should be convertible to \'W.m-2\')")
}
message("Converting units ...")
message("[", Sys.time(), "] Converting units ...")
rsds <- udConvertGrid(rsds, new.units = "W.m-2") %>% redim(member = TRUE)
}
if (u2 != "W.m-2") {
if (!ud.are.convertible(u2, "W.m-2")) {
stop("Non compliant rlds units (should be convertible to \'W.m-2\')")
}
message("Converting units ...")
message("[", Sys.time(), "] Converting units ...")
rlds <- udConvertGrid(rlds, new.units = "W.m-2") %>% redim(member = TRUE)
}
if (!is.null(rtds)) message("NOTE: rtds argument will be ignored, and calculated from rlds and rsds provided")
Expand All @@ -94,13 +94,15 @@ rad2cc <- function(rsds = NULL, rlds = NULL, rtds = NULL) {
alpha <- matrix(90 + ref.coords[ ,2], ncol = nrow(ref.coords), nrow = length(jday), byrow = TRUE) - delta
R0 <- 990 * sin(alpha * 2 * pi / 360) - 30
n.mem <- getShape(rtds, "member")
cc <- rtds
l <- lapply(1:n.mem, function(x) {
tot.rad <- subsetGrid(rtds, members = x, drop = TRUE) %>% redim(member = FALSE) %>% extract2("Data") %>% array3Dto2Dmat()
a <- suppressWarnings(exp(log((R0 - tot.rad) / (.75 * R0)) / 3.4))
a[which(R0 - tot.rad < 0)] <- 0
rtds$Data <- mat2Dto3Darray(a, x = coords$x, y = coords$y)
return(rtds)
cc$Data <- mat2Dto3Darray(a, x = coords$x, y = coords$y)
return(cc)
})
rtds <- NULL
cc <- suppressWarnings(bindGrid(l, dimension = "member"))
cc$Variable$varName <- "clt"
cc$Variable$level <- NULL
Expand Down
33 changes: 19 additions & 14 deletions R/tas2ws.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@
#' @description Calculates the saturation pressure using near-surface air temperature and surface pressure data
#' @param tas Near-surface air temperature
#' @param ps Surface pressure
#' @return A climate4R grid of saturation pressure (in Pascals)
#' @return A climate4R grid of saturation pressure (in atm)
#' @author J. Bedia, S. Herrera
#' @keywords internal
#' @export
#' @import transformeR
#' @importFrom magrittr %>% %<>% extract2
#' @importFrom udunits2 ud.are.convertible
#' @template templateUnits
#' @template templateRefHumidity
#' @family derivation
#' @examples \dontrun{
#' @examples
#' data("ps.iberia")
#' data("tas.iberia")
#' ws <- tas2ws(tas.iberia, ps.iberia)
#' library(visualizeR)
#' spatialPlot(climatology(ws), backdrop.theme = "coastline")}
#' \dontrun{
#' require(visualizeR)
#' spatialPlot(climatology(ws), backdrop.theme = "coastline")
#' }

tas2ws <- function(tas, ps) {
# Consistency checks:
Expand All @@ -53,20 +57,21 @@ tas2ws <- function(tas, ps) {
if (!ud.are.convertible(u.ps, "Pa")) {
stop("Non compliant ps units (should be convertible to \'Pascals')")
}
message("Converting units ...")
message("[", Sys.time(), "] Converting units ...")
ps <- udConvertGrid(ps, new.units = "Pa") %>% redim(member = TRUE)
}
if (u.tas != "K") {
if (!ud.are.convertible(u.tas, "K")) {
stop("Non compliant tas units (should be convertible to \'Kelvin\')")
}
message("Converting units ...")
message("[", Sys.time(), "] Converting units ...")
tas <- udConvertGrid(tas, new.units = "K") %>% redim(member = TRUE)
}
message("Calculating saturation pressure ...")
message("[", Sys.time(), "] Calculating saturation pressure ...")
source(file.path(find.package(package = "convertR"), "constants.R"), local = TRUE)
coords <- getCoordinates(ps)
n.mem <- getShape(tas, "member")
wt <- ps
l <- lapply(1:n.mem, function(x) {
a <- subsetGrid(tas, members = x, drop = TRUE) %>% redim(member = FALSE) %>% extract2("Data") %>% array3Dto2Dmat()
b <- subsetGrid(ps, members = x, drop = TRUE) %>% redim(member = FALSE) %>% extract2("Data") %>% array3Dto2Dmat()
Expand All @@ -75,17 +80,17 @@ tas2ws <- function(tas, ps) {
a[iceMask] <- es0 * exp((6293/T0) - (6293/a[iceMask]) - 0.555 * log(abs(a[iceMask]/T0)))
a[waterMask] <- es0 * exp((6808/T0) - (6808/a[waterMask]) - 5.09 * log(abs(a[waterMask]/T0)))
a <- (Rd/Rv) * (a/(b - a))
ps$Data <- mat2Dto3Darray(a, x = coords$x, y = coords$y)
wt$Data <- mat2Dto3Darray(a, x = coords$x, y = coords$y)
a <- b <- NULL
return(ps)
return(wt)
})
tas <- NULL
tas <- ps <- NULL
wt <- suppressWarnings(bindGrid(l, dimension = "member"))
wt$Variable$varName <- "ws"
wt$Variable$level <- NULL
attr(wt$Variable, "units") <- "Pa"
attr(wt$Variable, "longname") <- "Saturation_pressure"
attr(wt$Variable, "description") <- "Estimated saturation pressure"
message("Done")
attr(wt$Variable, "units") <- "atm"
attr(wt$Variable, "longname") <- "Vapour_pressure_of_water"
attr(wt$Variable, "description") <- "The vapour pressure of water is the pressure at which water vapour is in thermodynamic equilibrium with its condensed state"
message("[", Sys.time(), "] Done.")
invisible(wt)
}
2 changes: 1 addition & 1 deletion inst/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Rd <- 287.058 # dry air constant J/(K kg)
epsilon <- Rd/Rv # adimensional
T0 <- 273.15 # K
es0 <- 611 # Pa
GammaST <- .0065 # (dT/dz)^st standard atmosphere vertical gradient of the temperature in the troposphere (0.0065 (K/m^-1))
GammaST <- 6.5e-03 # (dT/dz)^st standard atmosphere vertical gradient of the temperature in the troposphere (0.0065 (K/m^-1))
57 changes: 57 additions & 0 deletions man/hurs2huss.Rd

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

68 changes: 68 additions & 0 deletions man/hurs2w.Rd

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

8 changes: 7 additions & 1 deletion man/huss2hurs.Rd

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

3 changes: 2 additions & 1 deletion man/rad2cc.Rd

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

17 changes: 12 additions & 5 deletions man/tas2ws.Rd

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

0 comments on commit 403dee2

Please sign in to comment.