|
| 1 | +#' Returns a list of habitable boxes for atlantis groups based on temperature forcing |
| 2 | +#' |
| 3 | +#'@description |
| 4 | +#'Determine if Groups have suitable habitat (set of polygons/boxes by layer) based on temperature thresholds. |
| 5 | +#' |
| 6 | +#'Horizontal redistribution proportions (FXXX_SY and recruit_hdist) and |
| 7 | +#'vertical distributions (vertDAY,vertNIGHT, recruit_vdistrib) are NOT used. |
| 8 | +#' |
| 9 | +#' |
| 10 | +#'@param paramList A list of parameter files (Output of \code{get_atl_paramfiles()}) |
| 11 | +#'@param speciesCodes A character string of the species/group name of interest. Default is NULL (All species) |
| 12 | +#'@param timeFrame Numeric. Section of time series of interest. For example, 10:20 (between year 10 and 20), -1 (last year of the timeseries), -10 (last 10 years of the time series) |
| 13 | +#' |
| 14 | +#'@return A data frame. Box/layers which are habitable over time range. The columns are: |
| 15 | +#'\item{group}{Species/Group name} |
| 16 | +#'\item{layer}{Polygon layer} |
| 17 | +#'\item{habitableBoxes}{The Boxes/polygons in which are habitable based on temperature thresholds} |
| 18 | +#' |
| 19 | +#' @section Layers: |
| 20 | +#' 1 = Surface, n is sediment |
| 21 | +#' |
| 22 | +#' |
| 23 | +#'@examples |
| 24 | +#'\dontrun{ |
| 25 | +#'# Declare paths to files required |
| 26 | +#' paramList <- list() |
| 27 | +#' paramList$bgm.file <- "Full path to bgm file" |
| 28 | +#' paramList$biol.prm <- "Full path to biology prm file" |
| 29 | +#' paramList$run.prm <- "Full path to run prm file" |
| 30 | +#' |
| 31 | +#' # Get all habitable boxes in the last year of the run for all species |
| 32 | +#' get_habitable_boxes(paramList, speciesCodes=NULL, timeFrame = -1) |
| 33 | +#' |
| 34 | +#' # Get all habitable boxes for HERRING and WHITE HAKE for the period of the last 10 years |
| 35 | +#' # Note: if a box is considered habitable for any of the last 10 years it will be present in the output |
| 36 | +#' diag_temp_thresholds(paramList, speciesCodes=c("HER","WHK"), timeFrame = -10) |
| 37 | +#' |
| 38 | +#' # Get all habitable boxes for HERRING between the 10th and 11th year of the temperature forcing data |
| 39 | +#' diag_temp_thresholds(paramList, speciesCodes=c("HER"), timeFrame = 10:11) |
| 40 | +#'} |
| 41 | +#' |
| 42 | +#' |
| 43 | +#'@export |
| 44 | + |
| 45 | +get_habitable_boxes <- function( |
| 46 | + paramList, |
| 47 | + speciesCodes = NULL, |
| 48 | + timeFrame = -1 |
| 49 | +) { |
| 50 | + # check to see if all parameter files are available |
| 51 | + |
| 52 | + check_param_files(paramList$run.prm) |
| 53 | + check_param_files(paramList$bgm.file) |
| 54 | + check_param_files(paramList$biol.prm) |
| 55 | + |
| 56 | + ## Grab the min and max temperature by polygon from the biology.prm file |
| 57 | + temperatureLimits <- get_param_move_temp(paramList$biol.prm) |
| 58 | + |
| 59 | + # check to see if species codes are valid |
| 60 | + if (is.null(speciesCodes)) { |
| 61 | + speciesCodes <- unique(temperatureLimits$group) |
| 62 | + } else { |
| 63 | + # check to see if speciesCodes is in the temperatureLimits |
| 64 | + if (any(!speciesCodes %in% unique(temperatureLimits$group))) { |
| 65 | + stop(paste0( |
| 66 | + "Some species Codes not found: ", |
| 67 | + paste0(speciesCodes, collapse = ",") |
| 68 | + )) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + # get the temperature forcing data by time/polygon/layer |
| 73 | + # increase layer value to match other outputs |
| 74 | + temperatureData <- get_forcing_temperature(paramList, plotFigs = F) |> |
| 75 | + dplyr::mutate(layer = as.numeric(levels(layer)[layer]) + 1) |
| 76 | + sedimentLayer <- max(temperatureData$layer) |
| 77 | + # read in the output frequency to scale the recruitment time diagnostic |
| 78 | + toutinc <- get_run_prm(paramList$run.prm, "toutinc") |
| 79 | + numValsPerYear <- 365 / toutinc$value |
| 80 | + # read in the time step |
| 81 | + dt <- get_run_prm(paramList$run.prm, "dt")$value |
| 82 | + |
| 83 | + # get boundary boxes |
| 84 | + boxcoords <- atlantistools::load_box(paramList$bgm.file) |
| 85 | + bboxes <- atlantistools::get_boundary(boxcoords) |
| 86 | + |
| 87 | + # extract values of time based on timeFrame argument passed |
| 88 | + maxModelTime <- max(temperatureData$time) |
| 89 | + if ((length(timeFrame) == 1) && timeFrame < 0) { |
| 90 | + # last so many years |
| 91 | + timeToFilter <- (maxModelTime - abs(timeFrame)):maxModelTime |
| 92 | + } else { |
| 93 | + timeToFilter <- timeFrame |
| 94 | + } |
| 95 | + |
| 96 | + ## Diagnostic to indicate which boxes in the model are habitable, based on temperature, |
| 97 | + # compared to the forcing time series. |
| 98 | + |
| 99 | + outdf <- NULL |
| 100 | + # loop over layers |
| 101 | + for (ilayer in sort(unique(temperatureData$layer))) { |
| 102 | + # use the forcing temp data temperatureData for each layer |
| 103 | + data <- temperatureData |> |
| 104 | + dplyr::filter(layer == ilayer) |> |
| 105 | + dplyr::select(-variable, -layer) |
| 106 | + if (nrow(data) == 0) { |
| 107 | + next |
| 108 | + } |
| 109 | + |
| 110 | + # loop over species |
| 111 | + # check to see if any species are affected by temperature |
| 112 | + # Determine if temperatureLimits fall inside the range of the temperature data |
| 113 | + for (species in speciesCodes) { |
| 114 | + # obtain the temperature thresholds for each species |
| 115 | + speciesData <- temperatureLimits |> |
| 116 | + dplyr::filter(group == species) |
| 117 | + # select the value of speciesData where limit = min |
| 118 | + # and the value of speciesData where limit = max |
| 119 | + minV <- speciesData |> |
| 120 | + dplyr::filter(limit == "min") |> |
| 121 | + dplyr::select(value) |> |
| 122 | + dplyr::pull() |
| 123 | + maxV <- speciesData |> |
| 124 | + dplyr::filter(limit == "max") |> |
| 125 | + dplyr::select(value) |> |
| 126 | + dplyr::pull() |
| 127 | + |
| 128 | + # check to see if the species thresholds fall within the temperature data |
| 129 | + # Habitable range based on time frame of interest |
| 130 | + |
| 131 | + habitableBoxes <- data |> |
| 132 | + dplyr::filter( |
| 133 | + atoutput > minV & atoutput < maxV, |
| 134 | + time > min(timeToFilter) & time <= max(timeToFilter) |
| 135 | + ) |> |
| 136 | + dplyr::pull(polygon) |> |
| 137 | + unique() |
| 138 | + |
| 139 | + # if there are no instances move on to next species |
| 140 | + if (length(habitableBoxes) <= 0) { |
| 141 | + next |
| 142 | + } |
| 143 | + |
| 144 | + outdflayer <- data.frame( |
| 145 | + group = species, |
| 146 | + layer = ilayer, |
| 147 | + habitableBoxes = habitableBoxes |
| 148 | + ) |
| 149 | + |
| 150 | + outdf <- rbind(outdf, outdflayer) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return(outdf) |
| 155 | +} |
0 commit comments