UpdateSubsets.R 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. UpdateSubsets <-
  2. function(LoadDat, StartDate = FALSE, Dir = ".")
  3. {
  4. if(StartDate) details <- LoadDat[!duplicated(data.frame(LoadDat$lat, LoadDat$long, LoadDat$end.date, LoadDat$start.date)), ]
  5. if(!StartDate) details <- LoadDat[!duplicated(data.frame(LoadDat$lat, LoadDat$long, LoadDat$end.date)), ]
  6. cat("Found", nrow(details), "unique time-series in original file\n")
  7. # Year or posixt date format?
  8. Year <- FALSE
  9. POSIXt <- FALSE
  10. posix.compatible <- try(as.POSIXlt(details$end.date), silent = TRUE)
  11. if(any(class(details$end.date) == "POSIXt") | all(class(posix.compatible) != "try-error")) POSIXt <- TRUE
  12. if(all(is.numeric(details$end.date) & nchar(details$end.date) == 4) &
  13. any(class(posix.compatible) == "try-error")) Year <- TRUE
  14. if(!Year & !POSIXt) stop("Date information in LoadDat is not recognised as years or as POSIXt format.")
  15. if(Year & POSIXt) stop("Date information in LoadDat is recognised as both year and POSIXt formats.")
  16. if(Year) endyear <- details$end.date
  17. if(POSIXt) endyear <- as.numeric(format(details$end.date, "%Y"))
  18. if(StartDate){
  19. if(Year) startyear <- details$start.date
  20. if(POSIXt) startyear <- as.numeric(format(details$start.date, "%Y"))
  21. }
  22. #ID <- ifelse(any(names(details) == "ID"), TRUE, FALSE)
  23. #fmt <- '%.5f'
  24. #if(StartDate){
  25. # if(ID){
  26. # ## Check that all author-given IDs will be unique for each unique time-series, and check that they won't cause issues with product information
  27. # n.unique <- length(unique(details$ID)) == nrow(details)
  28. # if(!n.unique){
  29. # cat('Number of IDs is not unique.\n')
  30. # details$ID <- paste("Lat", sprintf(fmt, details$lat), "Lon", sprintf(fmt, details$long), "Start", startyear, "End", endyear, sep = "")
  31. # }
  32. # } else {
  33. # details$ID <- paste("Lat", sprintf(fmt, details$lat), "Lon", sprintf(fmt, details$long), "Start", startyear, "End", endyear, sep = "")
  34. # }
  35. #}
  36. #if(!StartDate){
  37. # if(ID){
  38. # ## Check that all author-given IDs will be unique for each unique time-series, and check that they won't cause issues with product information
  39. # n.unique <- length(unique(details$ID)) == nrow(details)
  40. # if(!n.unique){
  41. # details$ID <- paste("Lat", sprintf(fmt, details$lat), "Lon", sprintf(fmt, details$long), "End", endyear, sep = "")
  42. # }
  43. # } else {
  44. # details$ID <- paste("Lat", sprintf(fmt, details$lat), "Lon", sprintf(fmt, details$long), "End", endyear, sep = "")
  45. # }
  46. #}
  47. filelist <- list.files(path = Dir, pattern = ".asc")
  48. cat("Found", length(filelist), "subsets previously downloaded\n")
  49. whichSubsetsDownloaded <- c()
  50. for(file in filelist)
  51. {
  52. dataFile <- read.csv(file.path(Dir, file), as.is = TRUE, header = FALSE)
  53. dataLat <- substr(dataFile[1,9],
  54. regexpr("Lat", dataFile[1,9])+3,
  55. regexpr("Lon", dataFile[1,9])-1)
  56. dataLong <- substr(dataFile[1,9],
  57. regexpr("Lon", dataFile[1,9])+3,
  58. regexpr("Samp", dataFile[1,9])-1)
  59. startModisDate <- dataFile[1,8]
  60. endModisDate <- dataFile[nrow(dataFile),8]
  61. startYears <- substr(startModisDate, 2, 5)
  62. startDays <- substr(startModisDate, 6, 8)
  63. startPosixDate <- as.Date(paste0(startYears, "-01-01")) + (as.numeric(startDays) - 1)
  64. endYears <- substr(endModisDate, 2, 5)
  65. endDays <- substr(endModisDate, 6, 8)
  66. endPosixDate <- as.Date(paste0(endYears, "-01-01")) + (as.numeric(endDays) - 1)
  67. if(Year)
  68. {
  69. subsetMetadata <- data.frame(lat = as.numeric(dataLat),
  70. long = as.numeric(dataLong),
  71. start.date = startYears,
  72. end.date = endYears)
  73. whichSubsetsDownloaded <- c(whichSubsetsDownloaded,
  74. with(subsetMetadata,
  75. which(sprintf("%.5f", lat) == sprintf("%.5f", details$lat) &
  76. sprintf("%.5f", long) == sprintf("%.5f", details$long) &
  77. start.date == details$start.date &
  78. end.date == details$end.date)))
  79. }
  80. if(POSIXt)
  81. {
  82. subsetMetadata <- data.frame(lat = as.numeric(dataLat),
  83. long = as.numeric(dataLong),
  84. start.date = startPosixDate,
  85. end.date = endPosixDate)
  86. ## Find the interval length for the downloaded data band, to set the
  87. ## flexibility allowed when matching subset dates.
  88. secondDate <- dataFile[2,8]
  89. secondYears <- substr(secondDate, 2, 5)
  90. secondDays <- substr(secondDate, 6, 8)
  91. secondDate <- as.Date(paste0(secondYears, "-01-01")) + (as.numeric(secondDays) - 1)
  92. intervalLength <- as.numeric(secondDate - startPosixDate)
  93. whichSubsetsDownloaded <- c(whichSubsetsDownloaded,
  94. with(subsetMetadata,
  95. which(sprintf("%.5f", lat) == sprintf("%.5f", details$lat) &
  96. sprintf("%.5f", long) == sprintf("%.5f", details$long) &
  97. (as.Date(details$start.date) <= start.date & start.date < as.Date(details$start.date)+intervalLength) &
  98. (end.date <= as.Date(details$end.date) & as.Date(details$start.date) < end.date+intervalLength))))
  99. }
  100. }
  101. return(details[-whichSubsetsDownloaded, ])
  102. }