1

2
#' @title Download and Create a List of Raster Stack Objects From CRU CL v. 2.0 Climatology Variables
3
#'
4
#'@description This function automates downloading and importing CRU CL v. 2.0
5
#' climatology data into \R and creates a list of raster stacks of the
6
#' data.  If requested, minimum and maximum temperature may also be
7
#' automatically calculated as described in the data readme.txt file.  Data may
8
#' be cached for later use by this function, saving time downloading files in
9
#' future use of the function.
10
#'
11
#' Nomenclature and units from readme.txt:
12
#' \describe{
13
#' \item{pre}{precipitation (millimetres/month)}
14
#'   \describe{
15
#'     \item{cv}{cv of precipitation (percent)}
16
#'   }
17
#' \item{rd0}{wet-days (number days with >0.1mm rain per month)}
18
#' \item{tmp}{mean temperature (degrees Celsius)}
19
#' \item{dtr}{mean diurnal temperature range (degrees Celsius)}
20
#' \item{reh}{relative humidity (percent)}
21
#' \item{sunp}{sunshine (percent of maximum possible (percent of day length))}
22
#' \item{frs}{ground-frost (number of days with ground-frost per month)}
23
#' \item{wnd}{10 metre windspeed (metres/second)}
24
#' \item{elv}{elevation (automatically converted to metres)}
25
#' }
26
#' For more information see the description of the data provided by CRU,
27
#' \url{https://crudata.uea.ac.uk/cru/data/hrg/tmc/readme.txt}
28
#'
29
#' @param pre Logical.  Fetch precipitation (millimetres/month) from server and
30
#' return in the data frame?  Defaults to \code{FALSE}.
31
#' @param pre_cv Logical.  Fetch cv of precipitation (percent) from server and
32
#' return in the data frame?  Defaults to \code{FALSE}.  NOTE.  Setting this to
33
#' \code{TRUE} will always results in \strong{pre} being set to \code{TRUE} and
34
#' returned as well.
35
#' @param rd0 Logical.  Fetch wet-days (number days with >0.1millimetres rain
36
#' per month) and return in the data frame? Defaults to \code{FALSE}.
37
#' @param dtr Logical.  Fetch mean diurnal temperature range (degrees Celsius)
38
#' and return it in the data frame?  Defaults to \code{FALSE}.
39
#' @param tmp Logical.  Fetch temperature (degrees Celsius) and return it in the
40
#' data frame?  Defaults to \code{FALSE}.
41
#' @param tmn Logical.  Calculate minimum temperature values (degrees Celsius)
42
#' and return it in the data frame?  Defaults to \code{FALSE}.
43
#' @param tmx Logical.  Calculate maximum temperature (degrees Celsius) and
44
#' return it in the data frame?  Defaults to \code{FALSE}.
45
#' @param reh Logical.  Fetch relative humidity and return it in the data frame?
46
#' Defaults to FALSE.
47
#' @param sunp Logical.  Fetch sunshine, percent of maximum possible (percent of
48
#' day length) and return it in data frame?  Defaults to \code{FALSE}.
49
#' @param frs Logical. Fetch ground-frost records (number of days with ground-
50
#' frost per month) and return it in data frame?  Defaults to \code{FALSE}.
51
#' @param wnd Logical.  Fetch 10m wind speed (metres/second) and return it in
52
#' the data frame? Defaults to \code{FALSE}.
53
#' @param elv Logical.  Fetch elevation (converted to metres) and return it in
54
#' the data frame?  Defaults to \code{FALSE}.
55
#' @param cache Logical.  Store CRU CL v. 2.0 data files locally for later use?
56
#' If \code{FALSE}, the downloaded files are removed when R session is closed.
57
#' To take advantage of cached files in future sessions, use \code{cache = TRUE}
58
#' after the initial download and caching.  Defaults to \code{FALSE}.
59
#'
60
#' @examples
61
#' \donttest{
62
#' # Download data and create a raster stack of precipitation and temperature
63
#' # without caching the data files
64
#' CRU_pre_tmp <- get_CRU_stack(pre = TRUE, tmp = TRUE)
65
#'
66
#' CRU_pre_tmp
67
#' }
68
#'
69
#' @seealso
70
#' \code{\link{create_CRU_stack}}
71
#' \code{\link{manage_cache}}
72
#'
73
#' @return A \code{\link[base]{list}} of \code{\link{raster}}
74
#' \code{\link[raster]{stack}} objects of CRU CL v. 2.0 climatology elements
75
#'
76
#' @author Adam H. Sparks, \email{adamhsparks@@gmail.com}
77
#'
78
#' @note
79
#' This package automatically converts elevation values from kilometres to
80
#' metres.
81
#'
82
#' This package crops all spatial outputs to an extent of ymin = -60, ymax = 85,
83
#' xmin = -180, xmax = 180.  Note that the original wind data include land area
84
#' for parts of Antarctica, these data are excluded in the raster stacks
85
#' generated by this function.
86
#'
87
#' @export get_CRU_stack
88

89
get_CRU_stack <-
90
  function(pre = FALSE,
91
           pre_cv = FALSE,
92
           rd0 = FALSE,
93
           tmp = FALSE,
94
           dtr = FALSE,
95
           reh = FALSE,
96
           tmn = FALSE,
97
           tmx = FALSE,
98
           sunp = FALSE,
99
           frs = FALSE,
100
           wnd = FALSE,
101
           elv = FALSE,
102
           cache = FALSE) {
103 1
    if (!isTRUE(pre) & !isTRUE(pre_cv) & !isTRUE(rd0) & !isTRUE(tmp) &
104 1
        !isTRUE(dtr) & !isTRUE(reh) & !isTRUE(tmn) & !isTRUE(tmx) &
105 1
        !isTRUE(sunp) & !isTRUE(frs) & !isTRUE(wnd) & !isTRUE(elv)) {
106 1
      stop("\nYou must select at least one element for download.\n",
107 1
           call. = FALSE)
108
    }
109

110 1
    cache_dir <- .set_cache(cache)
111

112 1
    files <- .get_CRU(pre,
113 1
                      pre_cv,
114 1
                      rd0,
115 1
                      tmp,
116 1
                      dtr,
117 1
                      reh,
118 1
                      tmn,
119 1
                      tmx,
120 1
                      sunp,
121 1
                      frs,
122 1
                      wnd,
123 1
                      elv,
124 1
                      cache_dir)
125

126 1
    if (isTRUE(pre_cv)) {
127 0
      pre <- TRUE
128
    }
129

130 1
    s <- .create_stacks(tmn, tmx, tmp, dtr, pre, pre_cv, files)
131 1
    return(s)
132
  }

Read our documentation on viewing source code .

Loading