R/getStreamDistMatsInOrder.R

Defines functions getStreamDistMatInOrder

Documented in getStreamDistMatInOrder

#' Retrieve stream distance matrices by network (in order of network number)
#' 
#'@description
#'
#'This function retrieves the distance matrices that have been created for a SpatialStreamNetwork and orders them by network name. 
#'
#'@param x An object of class SpatialStreamNetwork. The folder where the data for the SpatialStreamNetwork are stored must contain the distance matrix files generated by \code{createDistMat} from the package SSN.
#'@param Name A string indicating whether the distance matrices for the observed or prediction sites should be extracted. This defaults to "obs" for the observed sites. Replacing this with "preds" will yield the matrices for the prediction sites.
#'@return A list of the distance matrices for the SpatialStreamNetwork, ordered numerically by the network ID.
#'
#'@details
#'
#'This function addresses the problem that the function \code{getStreamDistMat} returns a list of distance matrices but not in order of network ID. It orders the network IDs in alphanumeric order, meaning that, if a SpatialStreamNetwork has 10 networks, the order will appear as 1, 10, 2, 3, 4, ..., etc. 
#'
#'@export
getStreamDistMatInOrder <- function(x, Name = "obs"){
  
  # Get stream distance matrices
  
  unordered.distmats <- getStreamDistMat(x, Name)
  
  # If for preds, identify A, B matrices and remove
  
  if(Name != "obs"){
    
    ind <- identifyObsxPredsABMatrices(unordered.distmats)
    
    unordered.distmats <- unordered.distmats[-ind]
    
  }
  
  # Names are in alphabetical order
  
  unordered.names <- names(unordered.distmats)
  
  # Strip away the parts of the names that include dist.net
  
  unordered.names <- gsub("dist.net", "", unordered.names)
  
  # Convert to numbers
  
  unordered.names <- as.numeric(unordered.names)
  
  # Find correct sorting order
  
  sort.order <- order(unordered.names)
  
  # Sort list
  
  ordered.distmats <- unordered.distmats[sort.order]
  
  # Return result
  
  return(ordered.distmats)
  
}
apear9/SSNdesign documentation built on Feb. 19, 2020, 4:29 a.m.