addCollection.R 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #' Add New Product to MODIS Collections
  2. #'
  3. #' @description
  4. #' \code{addCollection} is a non-exported helper function to add a new product
  5. #' column to the product collections managed by \strong{MODIS} (see
  6. #' \code{MODIS:::collections}). Once added, the specified product will be
  7. #' tracked and, if required, kept up-to-date by \code{\link{getCollection}}.
  8. #'
  9. #' @param product Character. Name of the product that should be added to the
  10. #' 'collections' dataset, see \code{\link{getCollection}}.
  11. #' @param collection Numeric. Optional information about available collections.
  12. #' If not supplied, this defaults to 'NA' and the user is required to manually
  13. #' retrieve information about available collections via
  14. #' \code{getCollection(..., forceCheck = TRUE)}. Note that the latter operation
  15. #' requires the previous execution of \code{MODIS:::addProduct} and
  16. #' \code{MODIS:::addServer} to make the newly added product available to
  17. #' \code{\link{getCollection}}.
  18. #' @param path_ext Character. Path to folder containing file
  19. #' 'MODIS_Products.RData'. When working with RStudio projects (.Rproj), this
  20. #' usually defaults to 'inst/external'.
  21. #' @param overwrite Logical. If \code{TRUE}, the initial '.RData' file located
  22. #' in 'path_ext' will be overwritten.
  23. #' @param ... Currently not used.
  24. #'
  25. #' @return
  26. #' A 'data.frame' which, for each product featured by \strong{MODIS}, holds
  27. #' information about available collections.
  28. #'
  29. #' @seealso
  30. #' \code{MODIS:::collections}.
  31. #'
  32. #' @author
  33. #' Florian Detsch
  34. #'
  35. #' @examples
  36. #' \dontrun{
  37. #' ## E.g., add collection of MODIS evapotranspiration product
  38. #' MODIS:::addCollection(product = "MOD16A2", collection = 105)
  39. #' }
  40. #'
  41. # #' @export addCollection
  42. #' @name addCollection
  43. addCollection <- function(product, collection = NA,
  44. path_ext = "inst/external", overwrite = FALSE, ...) {
  45. ## load list of current products
  46. load(paste0(path_ext, "/collections.RData"))
  47. ## id of last and new entry
  48. int_id_last <- ncol(MODIScollection)
  49. int_id_new <- int_id_last + 1
  50. ## if missing, append NA entries to 'collection'
  51. int_len <- length(collection)
  52. if (int_len < nrow(MODIScollection))
  53. collection[(int_len+1):nrow(MODIScollection)] <- NA
  54. ## add new collection
  55. MODIScollection <- cbind(MODIScollection, collection)
  56. names(MODIScollection)[int_id_new] <- product
  57. ## output storage
  58. if (overwrite) {
  59. file_out <- paste0(path_ext, "/collections.RData")
  60. save(MODIScollection, file = file_out)
  61. }
  62. ## return updated collections dataset
  63. return(MODIScollection)
  64. }