1

2
#' @noRd
3
#' @import data.table
4
.get_CRU <-
5
  function(pre,
6
           pre_cv,
7
           rd0,
8
           tmp,
9
           dtr,
10
           reh,
11
           tmn,
12
           tmx,
13
           sunp,
14
           frs,
15
           wnd,
16
           elv,
17
           cache_dir) {
18 1
    dtr_file <- "grid_10min_dtr.dat.gz"
19 1
    tmp_file <- "grid_10min_tmp.dat.gz"
20 1
    reh_file <- "grid_10min_reh.dat.gz"
21 1
    elv_file <- "grid_10min_elv.dat.gz"
22 1
    pre_file <- "grid_10min_pre.dat.gz"
23 1
    sun_file <- "grid_10min_sunp.dat.gz"
24 1
    wnd_file <- "grid_10min_wnd.dat.gz"
25 1
    frs_file <- "grid_10min_frs.dat.gz"
26 1
    rd0_file <- "grid_10min_rd0.dat.gz"
27

28
    # check if pre_cv or tmx/tmn (derived) are true, make sure proper ----------
29
    # parameters set TRUE
30 1
    if (isTRUE(pre_cv)) {
31 1
      pre <- TRUE
32
    }
33

34 1
    if (isTRUE(tmn) | isTRUE(tmx)) {
35 1
      dtr <- tmp <- TRUE
36
    }
37
    # create object list to filter downloads -----------------------------------
38 1
    object_list <- c(dtr, tmp, reh, elv, pre, sunp, wnd, frs, rd0)
39

40 1
    files <-
41 1
      c(
42 1
        dtr_file,
43 1
        tmp_file,
44 1
        reh_file,
45 1
        elv_file,
46 1
        pre_file,
47 1
        sun_file,
48 1
        wnd_file,
49 1
        frs_file,
50 1
        rd0_file
51
      )
52 1
    names(files) <-
53 1
      names(object_list) <-
54 1
      c(
55 1
        "dtr_file",
56 1
        "tmp_file",
57 1
        "reh_file",
58 1
        "elv_file",
59 1
        "pre_file",
60 1
        "sun_file",
61 1
        "wnd_file",
62 1
        "frs_file",
63 1
        "rd0_file"
64
      )
65

66
    # filter downloaded --------------------------------------------------------
67
    # which files are being requested?
68 1
    files <- files[object_list %in% !isTRUE(files)]
69

70
    # which files are locally available?
71 1
    cache_dir_contents <-
72 1
      list.files(cache_dir, pattern = ".dat.gz$")
73

74
    # which files requested need to be downloaded
75 1
    dl_files <- files[!(files %in% cache_dir_contents)]
76

77
    # download files -----------------------------------------------------------
78 1
    if (length(dl_files) > 0) {
79

80 1
      CRU_url <- "https://crudata.uea.ac.uk/cru/data/hrg/tmc/"
81 1
      dl_files <- as.list(paste0(CRU_url, dl_files))
82

83 1
      tryCatch(
84 1
        for (f in seq_along(dl_files)) {
85 1
          curl::curl_download(
86 1
            url = dl_files[[f]],
87 1
            destfile = (file.path(cache_dir, basename(dl_files[[f]]))),
88 1
            mode = "wb"
89
          )
90
        },
91 1
        error = function(x) {
92 0
          manage_cache$delete_all()
93 0
          stop("\nThe file downloads have failed.\n
94 0
               \nPlease start the download again.\n")
95
        }
96
      )
97
    }
98

99
    # filter files from cache directory in case there are local files for which
100
    # we do not want data
101 1
    cache_dir_contents <- as.list(list.files(cache_dir,
102 1
                                             pattern = ".dat.gz$"))
103

104 1
    files <- cache_dir_contents[cache_dir_contents %in% files]
105

106
    # add full file path to the files
107 1
    files <- file.path(cache_dir, files)
108

109
    # fill the space with a "\" for R, if one exists
110 1
    files <- gsub(" ", "\\ ", files, fixed = TRUE)
111

112 1
    return(files)
113
    }

Read our documentation on viewing source code .

Loading