## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)
library(pizzarr)

has_zarrs <- pizzarr:::.pizzarr_env$zarrs_available
has_s3 <- has_zarrs && ("s3" %in% pizzarr_compiled_features())
has_gcs <- has_zarrs && ("gcs" %in% pizzarr_compiled_features())
has_blosc <- requireNamespace("blosc", quietly = TRUE)

# Each gate probes the service its chunks actually use. Reaching one host says
# nothing about the other, and a gate that opens on the wrong probe fails the
# vignette build rather than skipping it.
reachable <- function(probe_url) {
  tryCatch({
    con <- url(probe_url)
    on.exit(try(close(con), silent = TRUE))
    length(readLines(con, warn = FALSE)) > 0
  }, error = function(e) FALSE, warning = function(w) FALSE)
}

# This vignette sets AWS_ENDPOINT below. Environment variables are
# process-global, and vignettes can share an R session, so stash the incoming
# value and restore it at the end rather than leaking the OSN endpoint into
# whatever builds next.
old_aws_endpoint <- Sys.getenv("AWS_ENDPOINT", unset = NA)

# Every chunk below that touches the network is also gated on reachability,
# so the vignette builds offline.
online <- reachable("https://usgs.osn.mghpcc.org/mdmf/gdp/gridMET.zarr/.zgroup")

# The GCS section reads a different service, so it gets its own probe against
# the same bucket over HTTPS.
gcs_online <- reachable(
  "https://storage.googleapis.com/pangeo-data/ECCO_basins.zarr/.zgroup"
)

## -----------------------------------------------------------------------------
pizzarr_compiled_features()

## ----eval = online------------------------------------------------------------
url <- "https://usgs.osn.mghpcc.org/mdmf/gdp/gridMET.zarr"

store <- HttpStore$new(url)
g <- zarr_open(store)
g

## ----eval = online------------------------------------------------------------
store$listdir()

## ----eval = online && has_blosc-----------------------------------------------
lat <- g$get_item("lat")
lat

lat$get_item(list(slice(1, 5)))$data

## ----eval = online && has_blosc-----------------------------------------------
tmmx <- g$get_item("max_air_temperature")
tmmx

## ----eval = has_s3 && online--------------------------------------------------
# Sys.setenv(AWS_ENDPOINT = "https://usgs.osn.mghpcc.org")
# 
# s3_url <- "s3://mdmf/gdp/gridMET.zarr"
# 
# zarrs_open_array_metadata(s3_url, "lat")

## ----eval = has_s3 && online--------------------------------------------------
# zarrs_get_subset(s3_url, "lat", list(c(0L, 5L)), NULL)

## ----eval = has_s3 && online--------------------------------------------------
# zarrs_close_store(s3_url)

## ----eval = has_gcs && gcs_online---------------------------------------------
# Sys.setenv(GOOGLE_SKIP_SIGNATURE = "true")
# 
# gs_url <- "gs://pangeo-data/ECCO_basins.zarr"
# 
# zarrs_open_array_metadata(gs_url, "basin_mask")$shape

## ----include = FALSE----------------------------------------------------------
if (has_gcs && gcs_online) {
  try(zarrs_close_store("gs://pangeo-data/ECCO_basins.zarr"), silent = TRUE)
}
Sys.unsetenv("GOOGLE_SKIP_SIGNATURE")

## ----include = FALSE----------------------------------------------------------
if (online) try(zarrs_close_store("https://usgs.osn.mghpcc.org/mdmf/gdp/gridMET.zarr"),
                silent = TRUE)

# Restore the endpoint setting; see the note at the top of this vignette.
if (is.na(old_aws_endpoint)) {
  Sys.unsetenv("AWS_ENDPOINT")
} else {
  Sys.setenv(AWS_ENDPOINT = old_aws_endpoint)
}

