R/polarview.R

Defines functions polarview_jpeg_tarball polarview_get_geotransform polarview_tifname polarview_files

Documented in polarview_files

#' Polarview files
#'
#' Imagery from www.polarview.aq
#'
#' The JPEGs are simple images, the GeoTIFFs are 16-bit integers (haven't explored further)
#' @return tibble data frame of file names, with columns `fullname` and `date`
#' @importFrom dplyr arrange distinct filter mutate
#' @importFrom rlang .data
#' @name polarview
#' @export
#' @param type jpeg or tarball
#' @importFrom stringr str_detect str_extract str_replace
#' @examples
#' \dontrun{
#'   files <- polarview_files()
#'   tiffiles <- polarview_files(type = "tarball")
#' }
polarview_files <- function(type = c("jpeg", "tarball")) {
  pattern <-  "www.polarview.aq/images"

  type <- match.arg(type)
  pattern <- c(pattern, switch(type,
         tarball = "104_S1geotiff.*tif\\.tar\\.gz$",
         jpeg = "106_S1jpgsmall.*jpg$"))

  files <- .find_files_generic(pattern)

  if (nrow(files) < 1)
    stop("no files found")
  files <- dplyr::mutate(files, date = as.POSIXct(as.Date(stringr::str_extract(basename(.data$fullname), "[0-9]{8}"),
                                                          "%Y%m%d"),tz = "UTC"))

  dplyr::arrange(dplyr::distinct(files, date, .keep_all = TRUE), date)  %>%
    dplyr::select(.data$date, .data$fullname, .data$root)

}
polarview_tifname <- function(x) {
  gsub("\\.tar\\.gz$", "", basename(x))
}
#' @keywords internal
#' @noRd
#' @examples
#' \dontrun{
#'   files <- polarview_files(type = "jpeg")
#'   tarball <- polarview_jpeg_tarball(files$fullname[20:24])
#'
#'   tarball <- na.omit(tarball)
#'   ## must be valid tarball paths here (not missing)
#'   polarview_get_geotransform(tarball)
#' }
polarview_get_geotransform <- function(x) {

  tfwname <- gsub("tif$", "tfw", polarview_tifname(x))

  out <- vector("list", length(x))
  for (i in seq_along(x)) {
    system(sprintf("tar -zxvf %s %s", x[i], tfwname[i]))
    out[[i]] <- readLines(tfwname[i])
    file.remove(tfwname[i])
  }
  out
}

polarview_jpeg_tarball <- function(jpeg) {
  ## we have a jpeg path
  #files <- polarview_files(type = "jpeg"); jpeg <- files$fullname[20]

  patt <- gsub("\\.jpg$", "", basename(jpeg))
  tfiles <- polarview_files(type = "tarball")
  ## return corresponding tarball (or NA)
  out <- rep(NA_character_, length(jpeg))
  for (i in seq_along(out)) {
    val <- grep(patt[i], tfiles$fullname, value = TRUE)
    if (length(val) < 1) {
      val <- NA_character_
    }
    out[i] <- val
  }
  out
}
AustralianAntarcticDivision/raadfiles documentation built on Feb. 15, 2024, 6:14 p.m.