getProduct.R 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #' Check and Create Product-Related Information
  2. #'
  3. #' @description
  4. #' On user side, it is a funtion to find the desidered product. On package site,
  5. #' it generates central internal information to hande files.
  6. #'
  7. #' @param x \code{character}. MODIS filename, product name, regular expression
  8. #' passed to \code{pattern} in \code{\link{grep}}, or missing.
  9. #' @param quiet \code{logical}, defaults to \code{FALSE}.
  10. #'
  11. #' @return
  12. #' An invisible \code{list} with information usable by other functions or, if
  13. #' 'x' is missing, a \code{data.frame} with information about all products
  14. #' available.
  15. #'
  16. #' @author
  17. #' Matteo Mattiuzzi
  18. #'
  19. #' @examples
  20. #' getProduct() # list available products
  21. #'
  22. #' # or use regular expression style
  23. #' getProduct("M.D11C3")
  24. #' getProduct("M*D11C")
  25. #'
  26. #' # or get information about specific product
  27. #' internal_info <- getProduct("MOD11C3", quiet = TRUE)
  28. #' internal_info
  29. #'
  30. #' @export getProduct
  31. #' @name getProduct
  32. getProduct <- function(x=NULL,quiet=FALSE)
  33. {
  34. #load(system.file("external", "MODIS_Products.RData", package="MODIS"))
  35. if (is.null(x)) { # if x isn't provided, return table of supported files.
  36. cls = c("SENSOR", "PRODUCT", "TOPIC", "PLATFORM","TYPE", "RES", "TEMP_RES")
  37. products = as.data.frame(MODIS_Products[cls])
  38. products = data.frame(products[order(products$PRODUCT), ]
  39. , row.names = 1:nrow(products))
  40. return(products)
  41. }
  42. if (is.list(x) && names(x) %in% c("request", "PRODUCT", "TOPIC", "DATE", "TILE", "TILEV", "TILEH", "CCC", "PROCESSINGDATE", "FORMAT", "SENSOR", "PLATFORM", "PF1", "PF2", "PF3", "TOPIC", "TYPE", "RES", "TEMP_RES", "INTERNALSEPARATOR"))
  43. {
  44. # if TRUE than it is a result from a getProduct() call. A good idea would be to have a CLASS for it!
  45. return(x)
  46. }
  47. ## moody but seams to work!!
  48. inbase <- basename(x) # if x is a filename(+path) remove the path
  49. isProduct = any(sapply(inbase, function(i) grepl(i, getProduct()[, 2])))
  50. tmp = if (!isProduct) {
  51. isFile <- TRUE
  52. sapply(strsplit(inbase, "\\."), "[[", 1)
  53. } else {
  54. isFile <- FALSE
  55. inbase
  56. }
  57. product = sapply(tmp, function(i) skipDuplicateProducts(i, quiet = quiet))
  58. pattern <- sub(pattern="MXD", replacement="M.D", x=product, ignore.case=TRUE) # make a regEx out of "x"
  59. info <- listPather(MODIS_Products,
  60. grep(paste(pattern, collapse = "|")
  61. , MODIS_Products$PRODUCT,ignore.case=TRUE))
  62. if (length(info$PRODUCT) == 0) {
  63. if (!quiet)
  64. cat("No product found with the name ", inbase
  65. , ". Try 'getProduct()' to list available products.\n", sep = "")
  66. return(NULL)
  67. }
  68. if (info$SENSOR[1]=="MODIS")
  69. {
  70. info$PRODUCT <- toupper(info$PRODUCT)
  71. }
  72. if (isFile)
  73. { # in this case it must be a filename
  74. names(x) = "request"
  75. fname = getInfo(x, product = info$PRODUCT, type = info$TYPE)
  76. result <- c(x, fname, info)
  77. result <- result[!duplicated(names(result))]
  78. result <- as.list(sapply(result,function(x)as.character(x)))
  79. return(invisible(result))
  80. } else # if not a file
  81. {
  82. if (!quiet)
  83. {
  84. for (i in seq_along(info$PRODUCT))
  85. {
  86. cat(paste(info$PRODUCT[i],'the',info$TEMP_RES[i],info$TYPE[i], info$TOPIC[i],'product from',info$SENSOR[i], info$PLATFORM[i],'with a ground resolution of', info$RES[i],'\n', sep = " "))
  87. }
  88. }
  89. if (info$SENSOR[1] == "MODIS")
  90. {
  91. PD <- substr(info$PRODUCT, 4, nchar(as.character(info$PRODUCT)))
  92. return(
  93. invisible(
  94. list(request = inbase, PF1 = as.character(info$PF1),
  95. PF2 = as.character(info$PF2), PF3 = as.character(info$PF3)
  96. , PD = PD, PLATFORM = as.character(info$PLATFORM),
  97. TYPE = as.character(info$TYPE), PRODUCT = as.character(info$PRODUCT),
  98. SENSOR = as.character(info$SENSOR), SOURCE=info$SOURCE)
  99. )
  100. )
  101. } ## else if ... (add additional sensors)
  102. }
  103. }