|
| 1 | +#' @title decompress_gz (DEPRECATED) |
| 2 | +#' |
| 3 | +#' @description This function decompresses all .gz files in a given folder |
| 4 | +#' |
| 5 | +#' @param input_dir directory path where all the files to read have been stored |
| 6 | +#' |
| 7 | +#' @export |
| 8 | +#' |
| 9 | +#' @examples |
| 10 | +#' \dontrun{ |
| 11 | +#' decompress_gz(input_dir = "/var/tmp/moc0/forestfire/") |
| 12 | +#' } |
| 13 | +#' |
| 14 | + |
| 15 | +decompress_gz <- function(input_dir = getwd()){ |
| 16 | + |
| 17 | + .Deprecated() |
| 18 | + message(paste("NOTE: Since GEFF v2.2 this function is no longer needed.", |
| 19 | + "It is now deprecated and", |
| 20 | + "will be removed in the next release.")) |
| 21 | + |
| 22 | + list_of_gzfiles <- list.files(path = input_dir, |
| 23 | + pattern = "*.gz", full.names = TRUE) |
| 24 | + |
| 25 | + # Decompress any gz files |
| 26 | + for (i in seq_along(list_of_gzfiles)){ |
| 27 | + |
| 28 | + R.utils::gunzip(list_of_gzfiles[i], overwrite = TRUE) |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +#' @title import_geff_data_from_tar (DEPRECATED) |
| 36 | +#' |
| 37 | +#' @description This function imports GEFF data (reanalysis or realtime) from a |
| 38 | +#' tar file into a raster stack. Depending on the user's query to the web |
| 39 | +#' portal, the stack can contain different variables. |
| 40 | +#' |
| 41 | +#' @param archive file path to the tar file |
| 42 | +#' @param stack_ncfiles logical (TRUE by default) variable to decided wheteher |
| 43 | +#' decompressed nc files should be stacked or not. |
| 44 | +#' |
| 45 | +#' @export |
| 46 | +#' |
| 47 | +#' @return If \code{stack_ncfiles} is TRUE, the function returns a RasterBrick. |
| 48 | +#' If \code{stack_ncfiles} is FALSE, it returns the list of uncompressed files. |
| 49 | +#' |
| 50 | +#' @examples |
| 51 | +#' \dontrun{ |
| 52 | +#' s <- import_geff_data_from_tar(archive = "test.tar") |
| 53 | +#' } |
| 54 | +#' |
| 55 | + |
| 56 | +import_geff_data_from_tar <- function(archive, stack_ncfiles = TRUE){ |
| 57 | + |
| 58 | + .Deprecated() |
| 59 | + message(paste("NOTE: Since GEFF v2.2 this function is no longer needed.", |
| 60 | + "It is now deprecated and", |
| 61 | + "will be removed in the next release.")) |
| 62 | + |
| 63 | + # From .tar to .gz, and finally to .nc |
| 64 | + my_temp_dir <- tempdir() |
| 65 | + list_of_gzfiles <- file.path(my_temp_dir, utils::untar(tarfile = archive, |
| 66 | + exdir = my_temp_dir, |
| 67 | + list = TRUE)) |
| 68 | + |
| 69 | + # Untar files |
| 70 | + utils::untar(tarfile = archive, exdir = my_temp_dir) |
| 71 | + |
| 72 | + # Decompress any gz files |
| 73 | + for (i in seq_along(list_of_gzfiles)){ |
| 74 | + |
| 75 | + R.utils::gunzip(list_of_gzfiles[i]) |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + if (stack_ncfiles == TRUE){ |
| 80 | + |
| 81 | + s <- raster::stack(tools::file_path_sans_ext(list_of_gzfiles)) |
| 82 | + |
| 83 | + return(raster::brick(s)) |
| 84 | + |
| 85 | + } else { |
| 86 | + |
| 87 | + return(tools::file_path_sans_ext(list_of_gzfiles)) |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +#' @title mean_percs (DEPRECATED) |
| 95 | +#' |
| 96 | +#' @description Calculate average percentile |
| 97 | +#' |
| 98 | +#' @param vals is the a raster object |
| 99 | +#' @param perc_val is the percentile value used as a threshold |
| 100 | +#' @param mod defines if the values considered for the mean are above ("gt") or |
| 101 | +#' below ("lt") the threshold |
| 102 | +#' |
| 103 | +#' @export |
| 104 | +#' |
| 105 | + |
| 106 | +mean_percs <- function(vals, perc_val, mod){ |
| 107 | + |
| 108 | + .Deprecated() |
| 109 | + message(paste("NOTE: Since GEFF v2.2 this function is no longer needed.", |
| 110 | + "It is now deprecated and", |
| 111 | + "will be removed in the next release.")) |
| 112 | + |
| 113 | + .Deprecated(msg = "This function is deprecated.") |
| 114 | + |
| 115 | + p_val <- stats::quantile(vals, perc_val / 100, na.rm = TRUE) |
| 116 | + |
| 117 | + if (mod == "gt") { |
| 118 | + |
| 119 | + v_perc <- vals[vals >= p_val] |
| 120 | + |
| 121 | + } else if (mod == "lt") { |
| 122 | + |
| 123 | + v_perc <- vals[vals <= p_val] |
| 124 | + |
| 125 | + } else { |
| 126 | + |
| 127 | + stop("mod should be 'lt' or 'gt'") |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + return(mean(v_perc, na.rm = TRUE)) |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +#' @title stack_netcdf_files (DEPRECATED) |
| 137 | +#' |
| 138 | +#' @description This function merges all the netcdf files in a given directory |
| 139 | +#' over the time dimension. It saves the merged file in the working directory. |
| 140 | +#' |
| 141 | +#' @param input_dir is the directory where all the files to read are stored |
| 142 | +#' @param varname name of the variable to extract |
| 143 | +#' @param pattern regular expression pattern to select a subset of files |
| 144 | +#' @param recursive logical (TRUE by default). If set to TRUE it looks in |
| 145 | +#' folders and subfolders |
| 146 | +#' @param output_file output filename (including path) |
| 147 | +#' |
| 148 | +#' @export |
| 149 | +#' |
| 150 | +#' @examples |
| 151 | +#' \dontrun{ |
| 152 | +#' # Mergetime using single variable nc files |
| 153 | +#' stack_netcdf_files(input_dir = "/var/tmp/moc0/forestfire", |
| 154 | +#' varname = NULL, |
| 155 | +#' pattern = "geff_reanalysis_an_fwis_fwi_", |
| 156 | +#' recursive = TRUE, |
| 157 | +#' output_file = "outfile.nc") |
| 158 | +#' } |
| 159 | +#' |
| 160 | + |
| 161 | +stack_netcdf_files <- function(input_dir = NULL, |
| 162 | + varname = NULL, |
| 163 | + pattern = NULL, |
| 164 | + recursive = FALSE, |
| 165 | + output_file = NULL){ |
| 166 | + |
| 167 | + .Deprecated("raster::stack()") |
| 168 | + message(paste("NOTE: Since GEFF v2.2 this function is no longer needed.", |
| 169 | + "It is now deprecated and", |
| 170 | + "will be removed in the next release.")) |
| 171 | + |
| 172 | + if (is.null(input_dir)) stop("Please specify data folder 'input_dir'!") |
| 173 | + |
| 174 | + ifiles <- list.files(path = input_dir, |
| 175 | + pattern = pattern, |
| 176 | + recursive = recursive, |
| 177 | + full.names = TRUE) |
| 178 | + |
| 179 | + if (is.null(output_file)) output_file <- tempfile(fileext = ".nc") |
| 180 | + |
| 181 | + if (is.null(varname)) varname <- "" |
| 182 | + s <- raster::stack(x = ifiles, varname = varname) |
| 183 | + |
| 184 | + if (!is.null(output_file)) { |
| 185 | + raster::writeRaster(s, filename = output_file, |
| 186 | + format = "CDF", overwrite = TRUE) |
| 187 | + } |
| 188 | + |
| 189 | + return(s) |
| 190 | + |
| 191 | +} |
0 commit comments