Skip to content

Instantly share code, notes, and snippets.

@agila5
Created March 31, 2025 12:40
Show Gist options
  • Save agila5/43158156bd154099caa25d25e0dbe313 to your computer and use it in GitHub Desktop.
Save agila5/43158156bd154099caa25d25e0dbe313 to your computer and use it in GitHub Desktop.
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE

# Download global NDVI data which average the values from 2024-05-01 to 2024-05-10
options(timeout = 1200)
tmp_nc <- tempfile(fileext = ".nc")
download.file(
  url = "https://globalland.vito.be/download/netcdf/ndvi/ndvi_300m_v2_10daily/2024/20240501/c_gls_NDVI300_202405010000_GLOBE_OLCI_V2.0.1.nc", 
  destfile = tmp_nc, 
  mode = "wb", 
  quiet = !interactive()
)

# Read-in the data
global_NDVI <- read_stars(
  .x = tmp_nc, 
  sub = "NDVI", 
  quiet = FALSE
)
#> NDVI,

# Italian bbox for subsetting
bbox_italy <- st_bbox(c(xmin = 6.6273, xmax = 18.5208, ymin = 36.619987, ymax = 47.0945), crs = "OGC:CRS84")

# Subset NDVI and 
italy_NDVI <- global_NDVI[bbox_italy] |> st_as_stars()

# Plot
plot(italy_NDVI, col = \(n, alpha) terrain.colors(n, alpha, rev = TRUE))
#> downsample set to 5

Created on 2025-03-31 with reprex v2.1.1.9000

Session info
sessioninfo::session_info()
#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :
#> running command '"quarto"
#> TMPDIR=C:/Users/andre/AppData/Local/Temp/Rtmp6RI4NB/file60fc628f5e60 -V' had
#> status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31 ucrt)
#>  os       Windows 11 x64 (build 26100)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  Italian_Italy.utf8
#>  ctype    Italian_Italy.utf8
#>  tz       Europe/Rome
#>  date     2025-03-31
#>  pandoc   3.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>  quarto   NA @ C:\\PROGRA~1\\RStudio\\RESOUR~1\\app\\bin\\quarto\\bin\\quarto.exe
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  abind       * 1.4-8      2024-09-12 [1] CRAN (R 4.4.2)
#>  class         7.3-22     2023-05-03 [2] CRAN (R 4.4.2)
#>  classInt      0.4-11     2025-01-08 [1] CRAN (R 4.4.2)
#>  cli           3.6.4      2025-02-13 [1] CRAN (R 4.4.3)
#>  curl          6.2.1      2025-02-19 [1] CRAN (R 4.4.2)
#>  DBI           1.2.3      2024-06-02 [1] CRAN (R 4.4.2)
#>  digest        0.6.37     2024-08-19 [1] CRAN (R 4.4.2)
#>  e1071         1.7-16     2024-09-16 [1] CRAN (R 4.4.2)
#>  evaluate      1.0.3      2025-01-10 [1] CRAN (R 4.4.2)
#>  fastmap       1.2.0      2024-05-15 [1] CRAN (R 4.4.2)
#>  fs            1.6.5      2024-10-30 [1] CRAN (R 4.4.2)
#>  glue          1.8.0      2024-09-30 [1] CRAN (R 4.4.2)
#>  htmltools     0.5.8.1    2024-04-04 [1] CRAN (R 4.4.2)
#>  KernSmooth    2.23-24    2024-05-17 [2] CRAN (R 4.4.2)
#>  knitr         1.49       2024-11-08 [1] CRAN (R 4.4.2)
#>  lifecycle     1.0.4      2023-11-07 [1] CRAN (R 4.4.2)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.4.2)
#>  proxy         0.4-27     2022-06-09 [1] CRAN (R 4.4.2)
#>  Rcpp          1.0.14     2025-01-12 [1] CRAN (R 4.4.2)
#>  reprex        2.1.1.9000 2025-02-25 [1] Github (tidyverse/reprex@07cd5d7)
#>  rlang         1.1.5      2025-01-17 [1] CRAN (R 4.4.2)
#>  rmarkdown     2.29       2024-11-04 [1] CRAN (R 4.4.2)
#>  rstudioapi    0.17.1     2024-10-22 [1] CRAN (R 4.4.2)
#>  sessioninfo   1.2.3      2025-02-05 [1] CRAN (R 4.4.2)
#>  sf          * 1.0-19     2024-11-05 [1] CRAN (R 4.4.2)
#>  stars       * 0.6-8      2025-02-01 [1] CRAN (R 4.4.2)
#>  units         0.8-7      2025-03-10 [1] Github (r-quantities/units@cb8869c)
#>  withr         3.0.2      2024-10-28 [1] CRAN (R 4.4.2)
#>  xfun          0.51.2     2025-02-20 [1] https://yihui.r-universe.dev (R 4.4.2)
#>  xml2          1.3.6      2023-12-04 [1] CRAN (R 4.4.2)
#>  yaml          2.3.10     2024-07-26 [1] CRAN (R 4.4.2)
#> 
#>  [1] C:/Users/andre/AppData/Local/R/win-library/4.4
#>  [2] C:/Program Files/R/R-4.4.2/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment