Commit b38b581a authored by Conor Anderson's avatar Conor Anderson

`gcm_anomalies()`: Filtering, garbage collection.

parent 19582f34
Pipeline #13385873 passed with stages
in 25 minutes and 59 seconds
......@@ -29,6 +29,7 @@ importFrom(readr,write_csv)
importFrom(stats,aggregate)
importFrom(stats,sd)
importFrom(tibble,as_tibble)
importFrom(tibble,has_name)
importFrom(tibble,tibble)
importFrom(utils,capture.output)
importFrom(utils,count.fields)
......
......@@ -3,20 +3,25 @@
#' @param datin a \code{tbl_df} produced by gcm_anomalies().
#'
#' @importFrom dplyr filter
#' @importFrom tibble has_name
#' @export
calc_anoms <- function(datin) {
dat_proj <- filter(datin, Scenario != "historical")
dat_hist <- filter(datin, Scenario == "historical")
if (has_name(datin, "Ensembles")) {
stop("It seems you already averaged the ensembles. This function needs to be run first.")
}
for (i in 1:nrow(dat_hist)) {
dat_proj[dat_proj$Model == dat_hist$Model[i] & dat_proj$Variable == dat_hist$Variable[i], 5] <- unlist(dat_hist[i, 5])
dat_proj[dat_proj$Model == dat_hist$Model[i] & dat_proj$Variable == dat_hist$Variable[i] & dat_proj$Ensemble == dat_hist$Ensemble[i], 5] <- unlist(dat_hist[i, 5])
for (col in 6:ncol(dat_proj)) {
rows <- which(dat_proj$Model == dat_hist$Model[i] & dat_proj$Variable == dat_hist$Variable[i])
rows <- which(dat_proj$Model == dat_hist$Model[i] & dat_proj$Variable == dat_hist$Variable[i] & dat_proj$Ensemble == dat_hist$Ensemble[i])
for (row in rows) {
dat_proj[row, col] <- dat_proj[row, col] - dat_hist[i, 5]
}
}
}
dat_proj
dat_proj[!is.na(dat_proj[[5]]),]
}
#' Generate table GCM anomaly data
#'
#' @param dir character; path to the directory containing the \code{.nc} files you wish to process
#' @param filters character; vector of filters to limit the \code{.nc} to be used.
#' @param lat numeric; the latitude of the target cell
#' @param lon numeric; the longitude of the target cell
#' @param timeslice numeric; 1-16: annual (0), monthly (1-12), seasonal (13-16)
......@@ -23,7 +24,7 @@
#' @examples
#' \dontrun{annual <- gcm_anomalies(getwd(), -7.023833, -76.465222, 0, 1971:2000, calc_anom = TRUE)}
gcm_anomalies <- function(dir = getwd(), lat, lon, timeslice = 0, baseline, start = 2011, simple_names = FALSE, calc_anom = TRUE, ensemble_average = TRUE) {
gcm_anomalies <- function(dir = getwd(), filters, lat, lon, timeslice = 0, baseline, start = 2011, simple_names = FALSE, calc_anom = TRUE, ensemble_average = TRUE) {
## Generate projection periods based on baseline length and start year
number_of_periods <- (2100 - start + 1) %/% length(baseline)
......@@ -40,6 +41,10 @@ gcm_anomalies <- function(dir = getwd(), lat, lon, timeslice = 0, baseline, star
## Get a list of NetCFD files in the directory
files_list <- grep("*.nc", dir(dir), value = TRUE)
if (!missing(filters)) {
files_list <- files_list[grep(paste(filters, collapse="|"), files_list)]
}
## Prepare a table with the right dimensions
dat <- as_tibble(matrix(NA, nrow = length(files_list), ncol = 5 + number_of_periods))
......@@ -58,14 +63,14 @@ gcm_anomalies <- function(dir = getwd(), lat, lon, timeslice = 0, baseline, star
nc_nc <- open.nc(file.path(dir, files_list[nc_file]))
nc_summary <- capture.output(print.nc(nc_nc))
time_string <- sub(".*?time:units = \\\"(.*?)\\\".*", "\\1", nc_summary[grep("time:units =", nc_summary)])
nc_var <- var.get.nc(nc_nc, var)
nc_time <- as.yearmon(utcal.nc(time_string, var.get.nc(nc_nc, "time"), type="s"))
nc_time <- as.yearmon(utcal.nc(att.get.nc(nc_nc, "time", "units"), var.get.nc(nc_nc, "time"), type="s"))
nc_lat <- var.get.nc(nc_nc, "lat")
nc_lon <- var.get.nc(nc_nc, "lon")
close.nc(nc_nc)
rm(nc_nc)
# Convert -180--180 longitudes to 0--360 if necessary
if (min(nc_lon >= 0)) lon <- ifelse(lon <0, 360 + lon, lon)
......@@ -118,6 +123,10 @@ gcm_anomalies <- function(dir = getwd(), lat, lon, timeslice = 0, baseline, star
dat[nc_file,] <- row
colnames(dat) <- col_names
rm(nc_var, nc_time, nc_lat, nc_lon)
gc()
setTxtProgressBar(prog, value = nc_file)
}
......
......@@ -3,6 +3,7 @@ Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
QuitChildProcessesOnExit: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
......@@ -18,3 +19,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
......@@ -4,13 +4,15 @@
\alias{gcm_anomalies}
\title{Generate table GCM anomaly data}
\usage{
gcm_anomalies(dir = getwd(), lat, lon, timeslice = 0, baseline,
gcm_anomalies(dir = getwd(), filters, lat, lon, timeslice = 0, baseline,
start = 2011, simple_names = FALSE, calc_anom = TRUE,
ensemble_average = TRUE)
}
\arguments{
\item{dir}{character; path to the directory containing the \code{.nc} files you wish to process}
\item{filters}{character; vector of filters to limit the \code{.nc} to be used.}
\item{lat}{numeric; the latitude of the target cell}
\item{lon}{numeric; the longitude of the target cell}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment