addProduct.R 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #' Add New Product to MODIS Inventory
  2. #'
  3. #' @description
  4. #' \code{addProduct} is a non-exported helper function to add a new entry to the
  5. #' list of satellite products featured by \strong{MODIS} (see
  6. #' \code{MODIS:::MODIS_Products}).
  7. #'
  8. #' @param product Character. Name of the product that should be added to the
  9. #' inventory, see \code{\link{getProduct}}.
  10. #' @param sensor Character. Sensor type, defaults to 'MODIS'.
  11. #' @param platform Character. Satellite platform on which the specified 'sensor'
  12. #' is mounted, defaults to "Terra".
  13. #' @param pf1,pf2 Character. Online server paths.
  14. #' @param topic Character. The official name of 'product'.
  15. #' @param type Character. Product type, defaults to 'Tile'.
  16. #' @param res Character. Spatial resolution of 'product', e.g. "1000m".
  17. #' @param temp_res Character. Temporal resolution of 'product', e.g. "8 Day".
  18. #' @param internalseparator Character. Separator string matching the product's
  19. #' naming convention, defaults to '\\.' for MODIS products.
  20. #' @param server Character. Server to download the data from (more than one
  21. #' entry is possible).
  22. #' @param path_ext Character. Path to folder containing file
  23. #' 'MODIS_Products.RData'. When working with RStudio projects (.Rproj), this
  24. #' usually defaults to 'inst/external'.
  25. #' @param overwrite Logical. If \code{TRUE}, the initial '.RData' file located
  26. #' in 'path_ext' will be overwritten.
  27. #' @param ... Currently not used.
  28. #'
  29. #' @return
  30. #' A 'list' holding the updated contents of file 'MODIS_Products.RData'.
  31. #'
  32. #' @author
  33. #' Florian Detsch
  34. #'
  35. #' @seealso
  36. #' \code{MODIS:::MODIS_Products}, \code{\link{getProduct}}.
  37. #'
  38. #' @examples
  39. #' \dontrun{
  40. #' ## E.g., add MODIS evapotranspiration product
  41. #' MODIS:::addProduct(product = "MOD16A2", sensor = "MODIS", platform = "Combined",
  42. #' pf1 = "MOLT", pf2 = "MOD", res = "1000m", temp_res = "8 Day",
  43. #' topic = "Global Terrestrial Evapotranspiration", server = "NTSG")
  44. #' }
  45. #'
  46. # #' @export addProduct
  47. #' @name addProduct
  48. addProduct <- function(product, sensor = "MODIS", platform = c("Terra", "Aqua"),
  49. pf1, pf2, topic, type = c("Tile", "Swath", "CMG"), res,
  50. temp_res, internalseparator = "\\.",
  51. server = c("LPDAAC", "LAADS"),
  52. path_ext = "inst/external", overwrite = FALSE, ...) {
  53. ## load list of current products
  54. load(paste0(path_ext, "/MODIS_Products.RData"))
  55. ## factor to character conversion
  56. log_was_factor <- logical(length = length(MODIS_Products))
  57. for (i in seq(MODIS_Products)) {
  58. if (is.factor(MODIS_Products[[i]])) {
  59. log_was_factor[i] <- TRUE
  60. MODIS_Products[[i]] <- as.character(MODIS_Products[[i]])
  61. }
  62. }
  63. ## id of last and new entry
  64. int_len_all <- sapply(MODIS_Products, length)
  65. int_id_last <- unique(int_len_all)
  66. int_id_new <- int_id_last + 1
  67. ## add new product
  68. MODIS_Products$SENSOR[int_id_new] <- sensor
  69. MODIS_Products$PRODUCT[int_id_new] <- product
  70. MODIS_Products$PLATFORM[int_id_new] <- platform[1]
  71. MODIS_Products$PF1[int_id_new] <- pf1
  72. MODIS_Products$PF2[int_id_new] <- pf2
  73. MODIS_Products$TOPIC[int_id_new] <- topic
  74. MODIS_Products$TYPE[int_id_new] <- type[1]
  75. MODIS_Products$RES[int_id_new] <- res
  76. MODIS_Products$TEMP_RES[int_id_new] <- temp_res
  77. MODIS_Products$INTERNALSEPARATOR[int_id_new] <- internalseparator
  78. MODIS_Products$SOURCE <- append(MODIS_Products$SOURCE, list(server))
  79. ## character to factor conversion
  80. for (i in seq(MODIS_Products)) {
  81. if (log_was_factor[i]) {
  82. MODIS_Products[[i]] <- as.factor(MODIS_Products[[i]])
  83. }
  84. }
  85. ## output storage (optional)
  86. if (overwrite) {
  87. file_out <- paste0(path_ext, "/MODIS_Products.RData")
  88. save(MODIS_Products, file = file_out)
  89. }
  90. ## return updated list of products
  91. return(MODIS_Products)
  92. }