1

2
#' @title Create a Tidy Data Frame From CRU CL v.2.0 Climatology Variables on Local Disk
3
#'
4
#'@description Automates importing \acronym{CRU} \acronym{CL} v.2.0 climatology
5
#' data and creates a tidy data frame of the data.  If requested, minimum and
6
#' maximum temperature may also be automatically calculated as described in the
7
#' data readme.txt file.  This function can be useful if you have network
8
#' connection issues that mean automated downloading of the files using \R
9
#' does not work properly.  In this instance it is recommended to use an
10
#' \acronym{FTP} client (\emph{e.g.}, FileZilla), web browser or command line
11
#' command (\emph{e.g.}, wget or curl) to download the files, save locally and
12
#' use this function to import the data into \R.
13
#'
14
#'Nomenclature and units from readme.txt:
15
#'\describe{
16
#'\item{pre}{precipitation (millimetres/month)}
17
#'  \describe{
18
#'    \item{cv}{cv of precipitation (percent)}
19
#'  }
20
#'\item{rd0}{wet-days (number days with >0.1mm rain per month)}
21
#'\item{tmp}{mean temperature (degrees Celsius)}
22
#'\item{dtr}{mean diurnal temperature range (degrees Celsius)}
23
#'\item{reh}{relative humidity (percent)}
24
#'\item{sunp}{sunshine (percent of maximum possible (percent of day length))}
25
#'\item{frs}{ground-frost (number of days with ground-frost per month)}
26
#'\item{wnd}{10 metre windspeed (metres/second)}
27
#'\item{elv}{elevation (automatically converted to metres)}
28
#'}
29
#'For more information see the description of the data provided by
30
#' \acronym{CRU}, \url{https://crudata.uea.ac.uk/cru/data/hrg/tmc/readme.txt}
31
#'
32
#' @param pre Logical. Fetch precipitation (millimetres/month) from server and
33
#'  return in the data frame? Defaults to \code{FALSE}.
34
#' @param pre_cv Logical. Fetch cv of precipitation (percent) from server and
35
#' return in the data frame? Defaults to \code{FALSE}. NOTE. Setting this to
36
#' \code{TRUE} will always results in \strong{pre} being set to \code{TRUE} and
37
#' returned as well.
38
#' @param rd0 Logical. Fetch wet-days (number days with >0.1millimetres rain per
39
#' month) and return in the data frame? Defaults to \code{FALSE}.
40
#' @param dtr Logical. Fetch mean diurnal temperature range (degrees Celsius)
41
#' and return it in the data frame? Defaults to \code{FALSE}.
42
#' @param tmp Logical. Fetch temperature (degrees Celsius) and return it in the
43
#' data frame? Defaults to \code{FALSE}.
44
#' @param tmn Logical. Calculate minimum temperature values (degrees Celsius)
45
#' and return it in the data frame? Defaults to \code{FALSE}.
46
#' @param tmx Logical. Calculate maximum temperature (degrees Celsius) and
47
#' return it in the data frame? Defaults to \code{FALSE}.
48
#' @param reh Logical. Fetch relative humidity and return it in the data frame?
49
#' Defaults to \code{FALSE}.
50
#' @param sunp Logical. Fetch sunshine, percent of maximum possible (percent of
51
#' day length) and return it in data frame? Defaults to \code{FALSE}.
52
#' @param frs Logical. Fetch ground-frost records (number of days with ground-
53
#' frost per month) and return it in data frame? Defaults to \code{FALSE}.
54
#' @param wnd Logical. Fetch 10m wind speed (metres/second) and return it in the
55
#' data frame? Defaults to \code{FALSE}.
56
#' @param elv Logical. Fetch elevation (converted to metres) and return it in
57
#' the data frame? Defaults to \code{FALSE}.
58
#' @param dsn Local file path where \acronym{CRU} \acronym{CL} v.2.0 .dat.gz
59
#' files are located.
60
#'
61
#' @examples
62
#' \donttest{
63
#' # Create a data frame of temperature from locally available files in the
64
#' # tempdir() directory.
65
#'
66
#' download.file(
67
#'   url = "https://crudata.uea.ac.uk/cru/data/hrg/tmc/grid_10min_tmp.dat.gz",
68
#'   destfile = file.path(tempdir(), "grid_10min_tmp.dat.gz")
69
#' )
70
#'
71
#' CRU_tmp <- create_CRU_df(tmp = TRUE, dsn = tempdir())
72
#'
73
#' CRU_tmp
74
#'}
75
#'
76
#' @seealso
77
#' \code{\link{get_CRU_df}}
78
#'
79
#' @return A tidy data frame of \acronym{CRU} \acronym{CL} v. 2.0 climatology
80
#' elements as a \code{\link[tibble]{tibble}} object
81
#'
82
#' @author Adam H Sparks, \email{adamhsparks@@gmail.com}
83
#'
84
#' @note
85
#' This package automatically converts elevation values from kilometres to
86
#' metres.
87
#' @export create_CRU_df
88

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

109 1
  .validate_dsn(dsn)
110

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

125 1
  if (length(files) == 0) {
126 0
    stop(
127 0
      "\nNo CRU CL 2.0 data files were found in ",
128 0
      dsn,
129
      ". ",
130 0
      "Please check that you have the proper file location.\n"
131
    )
132
  }
133

134 1
  d <- .create_df(tmn, tmx, tmp, dtr, pre, pre_cv, elv, files)
135 1
  return(tibble::as_tibble(d))
136
}

Read our documentation on viewing source code .

Loading