Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created May 26, 2025 02:12
Show Gist options
  • Save mdsumner/8a892a2f9ce0416201d6af83365b95a1 to your computer and use it in GitHub Desktop.
Save mdsumner/8a892a2f9ce0416201d6af83365b95a1 to your computer and use it in GitHub Desktop.
files <- tibble::tibble(filename = vsi_read_dir(vsidir)) |>
  dplyr::mutate(source = glue::glue("{vsidir}/{filename}")) |>
  dplyr::mutate(longitude = glue::glue("vrt://{source}?sd_name=longitude"),
                latitude = glue::glue("vrt://{source}?sd_name=latitude"))

files <- do.call(rbind, lapply(vars, \(.x) dplyr::filter(files, stringr::str_detect(filename, glue::glue("^{.x}_")))))

i <- 1
## we can probably save this for subsequent runs
lon <- read_ds(new(GDALRaster, files$longitude[i]))

gdalraster::set_config_option("GDAL_MEM_ENABLE_OPEN", "YES")
lonv <- matrix((lon  + 180) %% 360 - 180, attr(lon, "gis")$dim[2], byrow = TRUE)

mkvrt <- function(x, y, z, ...) {
  vapour::vapour_vrt(z, geolocation = c(x, y), ...)
}

clargs <- c("-co", "COMPRESS=ZSTD", "-of", "GTiff",
            "-multi", "-wo", "NUM_THREADS=31", "-co", "INTERLEAVE=BAND", "-co", "TILED=NO")
for (i in seq_along(files$filename)) {
 ncfile <- file.path("/scratch/pawsey0973/mdsumner/tiffiles2", gsub("\\.nc$", "_epsg3031.tif", files$filename[i]))
 if (file.exists(ncfile)) {
   ## nothing
 } else {
   curl::curl_download(gsub("/vsicurl/", "", files$source[i]), basename(files$source[i]), quiet = FALSE)
   dsn <- tail(vapour::vapour_sds_names(basename(files$source[i])), 1)

 warp(new(GDALRaster, mkvrt(dsn::mem(t(lonv)), files$latitude[2], dsn)), ncfile, "EPSG:3031",
      cl_arg = clargs)
 unlink(basename(files$source[i]))

 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment